mirror of
https://github.com/status-im/nim-stew.git
synced 2025-02-02 15:23:49 +00:00
fixes 'nimFirstOne' for Nim v1.0.2
This commit is contained in:
parent
11b6a831cb
commit
53c1ebe9d4
@ -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.
|
## 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
|
# 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))
|
32 + firstOneNim(uint32(x shr 32'u32))
|
||||||
else:
|
else:
|
||||||
firstOneNim(uint32(x))
|
firstOneNim(uint32(x))
|
||||||
|
@ -15,6 +15,7 @@ template test() =
|
|||||||
doAssert firstOne(0b00010010'u8) == 2
|
doAssert firstOne(0b00010010'u8) == 2
|
||||||
doAssert firstOne(0b11111111'u8) == 1
|
doAssert firstOne(0b11111111'u8) == 1
|
||||||
doAssert firstOne(0b100000000000000000000000000000000'u64) == 33
|
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(0b00000000'u8) == 8
|
||||||
doAssert leadingZeros(0b00000001'u8) == 7
|
doAssert leadingZeros(0b00000001'u8) == 7
|
||||||
|
Loading…
x
Reference in New Issue
Block a user