Skip to content

Commit 8e56324

Browse files
xiphmontbarrbrain
authored andcommitted
Widen useful range of BitWriter's write_quniform()
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.
1 parent 41c700a commit 8e56324

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/ec.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -805,17 +805,17 @@ impl<W: io::Write> BCodeWriter for BitWriter<W, BigEndian> {
805805
}
806806
}
807807
fn write_quniform(&mut self, n: u16, v: u16) -> Result<(), std::io::Error> {
808-
/* Encodes a value v in [0, n-1] quasi-uniformly */
809-
if n <= 1 {
810-
return Ok(());
811-
};
812-
let l = 31 ^ ((n - 1) + 1).leading_zeros();
813-
let m = (1 << l) - n;
814-
if v < m {
815-
self.write(l - 1, v)
808+
if n > 1 {
809+
let l = msb(n as i32) as u8 + 1;
810+
let m = (1 << l) - n;
811+
if v < m {
812+
self.write(l as u32 - 1, v)
813+
} else {
814+
self.write(l as u32 - 1, m + ((v - m) >> 1))?;
815+
self.write(1, (v - m) & 1)
816+
}
816817
} else {
817-
self.write(l - 1, m + ((v - m) >> 1))?;
818-
self.write_bit(((v - m) & 1) != 0)
818+
Ok(())
819819
}
820820
}
821821
fn write_subexpfin(

0 commit comments

Comments
 (0)