Skip to content

Commit 930b93f

Browse files
committed
Add fastLog2 operation
1 parent bc3b2fa commit 930b93f

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/bigints.nim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,5 @@ iterator `..<`*(a, b: BigInt): BigInt =
959959
while res < b:
960960
yield res
961961
inc res
962+
963+
func fastLog2*(a: BigInt): int = bitops.fastLog2(a.limbs[a.limbs.high]) + 32*(a.limbs.len-1)

tests/tbigints.nim

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,16 @@ template main() =
379379
doAssert pow(zero, 0) == one
380380
doAssert pow(zero, 1) == zero
381381

382+
block: # fastLog2
383+
let a = one shl 31
384+
let b = a shl 1
385+
# one limb
386+
doAssert fastLog2(a) == 31
387+
# two limbs
388+
doAssert fastLog2(b) == 32
389+
# negative BigInt
390+
doAssert fastLog2(-a) == 31
391+
doAssert fastLog2(-b) == 32
392+
382393
static: main()
383394
main()

0 commit comments

Comments
 (0)