Skip to content

Commit

Permalink
Widen useful range of BitWriter's write_quniform()
Browse files Browse the repository at this point in the history
The versions of write_quiniform() used by the arithmetic packer and the uncompressed bitwriter vary considerably.  For some reason, the BitWriter uses a much 'narrower' implementation that overflows with inputs of more than a few bits.

This patch replaces the BitWriter's implementation with one like in the arithmetic packer.  This allows its use in coding non-uniform tile header fields.
  • Loading branch information
xiphmont committed May 14, 2020
1 parent 6186069 commit 490a3c9
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/ec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,17 +804,17 @@ impl<W: io::Write> BCodeWriter for BitWriter<W, BigEndian> {
}
}
fn write_quniform(&mut self, n: u16, v: u16) -> Result<(), std::io::Error> {
/* Encodes a value v in [0, n-1] quasi-uniformly */
if n <= 1 {
return Ok(());
};
let l = 31 ^ ((n - 1) + 1).leading_zeros();
let m = (1 << l) - n;
if v < m {
self.write(l - 1, v)
if n > 1 {
let l = msb(n as i32) as u8 + 1;
let m = (1 << l) - n;
if v < m {
self.write(l as u32 - 1, v)
} else {
self.write(l as u32 - 1, m + ((v - m) >> 1))?;
self.write(1, (v - m) & 1)
}
} else {
self.write(l - 1, m + ((v - m) >> 1))?;
self.write_bit(((v - m) & 1) != 0)
Ok(())
}
}
fn write_subexpfin(
Expand Down

0 comments on commit 490a3c9

Please sign in to comment.