fixes 'nimFirstOne' for Nim v1.0.2

This commit is contained in:
andri lim 2019-10-22 18:53:36 +07:00 committed by zah
parent 11b6a831cb
commit 53c1ebe9d4
2 changed files with 8 additions and 1 deletions

View File

@ -61,7 +61,13 @@ func firstOneNim(x: uint64): int =
## Returns the 1-based index of the least significant set bit of x, or if x is zero, returns zero.
# https://graphics.stanford.edu/%7Eseander/bithacks.html#ZerosOnRightMultLookup
if uint32(x) == 0:
template convert[T](x: uint64): T =
when nimvm:
T(x and high(T))
else:
cast[T](x)
if convert[uint32](x) == 0:
32 + firstOneNim(uint32(x shr 32'u32))
else:
firstOneNim(uint32(x))

View File

@ -15,6 +15,7 @@ template test() =
doAssert firstOne(0b00010010'u8) == 2
doAssert firstOne(0b11111111'u8) == 1
doAssert firstOne(0b100000000000000000000000000000000'u64) == 33
doAssert firstOne(0b00000010_00000000_00000000_00000000_00000000_00000000_00000000_00000000'u64) == 8*7 + 2
doAssert leadingZeros(0b00000000'u8) == 8
doAssert leadingZeros(0b00000001'u8) == 7