diff --git a/stew/bitseqs.nim b/stew/bitseqs.nim index 7ee6721..9fa0099 100644 --- a/stew/bitseqs.nim +++ b/stew/bitseqs.nim @@ -82,7 +82,7 @@ template loopOverWords(lhs, rhs: BitSeq, var lhsWord: WordType when hasRhs: var rhsWord: WordType - var firstByteOfLastWord, lastByteOfLastWord, markerPos: int + var firstByteOfLastWord, lastByteOfLastWord: int # TODO: Returing a `var` value from an iterator is always safe due to # the way inlining works, but currently the compiler reports an error @@ -103,15 +103,14 @@ template loopOverWords(lhs, rhs: BitSeq, if lastWordSize == 0: firstByteOfLastWord = bytesCount - sizeof(WordType) lastByteOfLastWord = bytesCount - 1 - initBitsVars() - markerPos = sizeof(WordType) * 8 - 1 dec fullWordsCount else: firstByteOfLastWord = bytesCount - lastWordSize lastByteOfLastWord = bytesCount - 1 - initBitsVars() - markerPos = log2trunc(lhsWord) - when hasRhs: doAssert log2trunc(rhsWord) == markerPos + + initBitsVars() + let markerPos = log2trunc(lhsWord) + when hasRhs: doAssert log2trunc(rhsWord) == markerPos lhsWord.lowerBit markerPos when hasRhs: rhsWord.lowerBit markerPos diff --git a/tests/test_bitseqs.nim b/tests/test_bitseqs.nim index 3068f68..d141d8f 100644 --- a/tests/test_bitseqs.nim +++ b/tests/test_bitseqs.nim @@ -57,14 +57,18 @@ suite "Bit fields": check b[j] == (j == 1) test "overlaps": - for bitCount in [63, 62]: + for bitCount in [1, 62, 63, 64, 91, 127, 128, 129]: checkpoint &"trying bit count {bitCount}" var a = BitSeq.init(bitCount) b = BitSeq.init(bitCount) - a.raiseBit(4) - b.raiseBit(5) + + for pos in [4, 8, 9, 12, 29, 32, 63, 64, 67]: + if pos + 2 < bitCount: + a.raiseBit(pos) + b.raiseBit(pos + 2) check: not a.overlaps(b) not b.overlaps(a) +