X-Git-Url: https://git.nihav.org/?a=blobdiff_plain;f=src%2Fio%2Fintcode.rs;h=a898a10444415dd419f90a940996c64cabbb29ac;hb=1f7e9371e2ec548b6821bb01ce7ba82c4cd8068d;hp=141377dc686f09e16993c958c7e705ada9d210c0;hpb=d7fcdd86dd9f353e6f7b19bf84592698c4b020be;p=nihav.git diff --git a/src/io/intcode.rs b/src/io/intcode.rs index 141377d..a898a10 100644 --- a/src/io/intcode.rs +++ b/src/io/intcode.rs @@ -33,7 +33,7 @@ fn read_unary(br: &mut BitReader, terminator: u32) -> BitReaderResult { } } -fn read_unary_lim(br: &mut BitReader, terminator: u32, len: u32) -> BitReaderResult { +fn read_unary_lim(br: &mut BitReader, len: u32, terminator: u32) -> BitReaderResult { let mut res: u32 = 0; loop { if br.read(1)? == terminator { return Ok(res); } @@ -71,11 +71,11 @@ fn read_rice(br: &mut BitReader, k: u8) -> BitReaderResult { } fn read_gamma(br: &mut BitReader) -> BitReaderResult { - let mut ret = 0; + let mut ret = 1; while br.read(1)? != 1 { ret = (ret << 1) | br.read(1)?; } - Ok(ret) + Ok(ret - 1) } fn read_gammap(br: &mut BitReader) -> BitReaderResult { @@ -96,11 +96,12 @@ fn uval_to_sval0pm(uval: u32) -> i32 { } impl<'a> IntCodeReader for BitReader<'a> { + #[inline(always)] fn read_code(&mut self, t: UintCodeType) -> BitReaderResult { match t { UintCodeType::UnaryOnes => read_unary(self, 0), UintCodeType::UnaryZeroes => read_unary(self, 1), - UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, term, len), + UintCodeType::LimitedUnary(len, term) => read_unary_lim(self, len, term), UintCodeType::Unary012 => read_unary_lim(self, 2, 0), UintCodeType::Unary210 => read_unary210(self), UintCodeType::Golomb(m) => read_golomb(self, m),