-
Notifications
You must be signed in to change notification settings - Fork 29
Add fastLog2 operation, closes #67 #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5049ce4
784bab4
063ffcd
e4ff649
c195b08
854787c
1d958ed
7917980
5ee0804
3892344
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -385,6 +385,56 @@ proc main() = | |
| doAssert "fedcba9876543210".initBigInt(base = 16) == b | ||
| doAssert "ftn5qj1r58cgg".initBigInt(base = 32) == b | ||
|
|
||
| block: # fastLog2 | ||
| let a = one shl 31 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need some "not-power-of-two" tests too.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any idea of easily checkable tests then ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also test values with all 1's, e.g. As for running the tests yourself, you can just
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would do some sanity checks, e.g. log of 7, 8, 9, 24, 32, 48, etc., just to make sure it works for the basics. And then do what @konsumlamm suggests. |
||
| let b = a shl 1 | ||
| let c = initBigInt(0xfedcba9876543210'u64) | ||
| let d = initBigInt("ffffffffffffffffff", base = 16) | ||
|
|
||
| # first numbers | ||
| doAssert fastLog2(2.initBigInt) == 1 | ||
| doAssert fastLog2(3.initBigInt) == 1 | ||
| doAssert fastLog2(4.initBigInt) == 2 | ||
| doAssert fastLog2(5.initBigInt) == 2 | ||
| doAssert fastLog2(7.initBigInt) == 2 | ||
| doAssert fastLog2(8.initBigInt) == 3 | ||
| doAssert fastLog2(24.initBigInt) == 4 | ||
| doAssert fastLog2(32.initBigInt) == 5 | ||
| doAssert fastLog2(48.initBigInt) == 5 | ||
|
|
||
| # one limb | ||
| doAssert fastLog2(a) == 31 | ||
|
|
||
| # two limbs and more | ||
| doAssert fastLog2(b) == 32 | ||
| doAssert fastLog2(b+a) == 32 | ||
| doAssert fastLog2(c+b+a) == 63 | ||
|
|
||
| doAssert fastLog2(d) == 71 | ||
| doAssert fastLog2(d + one) == 72 | ||
| doAssert fastLog2(d - one) == 71 | ||
| doAssert fastLog2(-d) == 71 | ||
| doAssert fastLog2(-d - one) == 72 | ||
| doAssert fastLog2(-d + one) == 71 | ||
|
|
||
| # negative BigInts | ||
| doAssert fastLog2(-2.initBigInt) == 1 | ||
| doAssert fastLog2(-3.initBigInt) == 1 | ||
| doAssert fastLog2(-4.initBigInt) == 2 | ||
| doAssert fastLog2(-5.initBigInt) == 2 | ||
| doAssert fastLog2(-7.initBigInt) == 2 | ||
| doAssert fastLog2(-8.initBigInt) == 3 | ||
| doAssert fastLog2(-24.initBigInt) == 4 | ||
| doAssert fastLog2(-32.initBigInt) == 5 | ||
| doAssert fastLog2(-48.initBigInt) == 5 | ||
| doAssert fastLog2(-a) == 31 | ||
| doAssert fastLog2(-b) == 32 | ||
|
|
||
| # edge cases | ||
| doAssert fastLog2(one) == 0 | ||
| doAssert fastLog2(zero) == -1 | ||
|
|
||
|
|
||
| block: # pow | ||
| let a = "14075287".initBigInt | ||
| doAssert pow(a, 0) == one | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.