From 680c0a3c94f0bb84a2773bc9a95dc5399b6925fb Mon Sep 17 00:00:00 2001 From: pacien Date: Sun, 25 Nov 2018 16:45:35 +0100 Subject: Fix bitreader look-ahead overflow --- src/integers.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/integers.nim') diff --git a/src/integers.nim b/src/integers.nim index fddbfdc..7b0f166 100644 --- a/src/integers.nim +++ b/src/integers.nim @@ -15,13 +15,16 @@ # along with this program. If not, see . const wordBitLength* = 8 -const wordBitMask* = 0b1111_1111'u8 proc `/^`*[T: Natural](x, y: T): T = (x + y - 1) div y proc truncateToUint8*(x: SomeUnsignedInt): uint8 = - (x and wordBitMask).uint8 + (x and uint8.high).uint8 + +proc leastSignificantBits*[T: SomeUnsignedInt](x: T, bits: int): T = + let maskOffset = sizeof(T) * wordBitLength - bits + if maskOffset >= 0: (x shl maskOffset) shr maskOffset else: x iterator chunks*(totalBitLength: int, chunkType: typedesc[SomeInteger]): tuple[index: int, chunkBitLength: int] = let chunkBitLength = sizeof(chunkType) * wordBitLength -- cgit v1.2.3