-
Notifications
You must be signed in to change notification settings - Fork 37
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
WIP: Implement multi-word division for BigInt #387
Conversation
Codecov Report
@@ Coverage Diff @@
## master #387 +/- ##
==========================================
+ Coverage 92.86% 92.97% +0.10%
==========================================
Files 65 66 +1
Lines 15970 16303 +333
==========================================
+ Hits 14830 15157 +327
- Misses 1140 1146 +6
Continue to review full report at Codecov.
|
0e6aa1e
to
214f263
Compare
@9il auto b = BigInt!4.fromHexString("c39b18a9f06fd8e962d99935cea0707f79a222050aaeaaaed17feb7aa76999d7");
auto rem = b /= UInt!128.fromHexString("f79a222050aaeaaa417fa25a2ac93291");
// fails
// b is BigIntView!(ulong, WordEndian.little)(BigUIntView!(ulong, WordEndian.little)([8989662390899871995, 14572942669551396987, 0, 0]), false)
// rhs is BigIntView!(ulong, WordEndian.little)(BigUIntView!(ulong, WordEndian.little)([8989662390899871995, 14572942669551396987]), false)
assert(b == BigInt!4.fromHexString("ca3d7e25aebe687b7cc1b250b44690fb"));
assert(rem == UInt!128.fromHexString("bf4c87424431d21563f23b1fc00d75ac")); Should I automatically normalize the quotient view after dividing? Or would you be fine with replacing ///
bool opEquals(scope BigUIntView!(const W, endian) rhs)
const @safe pure nothrow @nogc scope
{
return this.normalized.coefficients == rhs.normalized.coefficients;
} |
source/mir/bignum/low_level_view.d
Outdated
|
||
// Similarly here, this is specified as qhat*v[n-2] > rhat*b + u[j+n-2] | ||
// but we can just get away with a shift instead. | ||
while (qhat >= b || qhat*v[n-2] > (rhat << shift) + u[j+n-2]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just qhat > uint.max
and remove b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Simplifying the while statement to just while (qhat > uint.max)
causes test failure, but I'll pull out the b
variable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean qhat >= b
=> qhat > uint.max
. The other part of condition is OK
Yes, it should be normalized inside |
auto rem = a /= b; | ||
assert(a == UInt!128.fromHexString("103e70bb237983337")); | ||
assert(rem == UInt!64.fromHexString("4932c5448b170cb3")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also we need %=
, /
, and %
operations. And standalone divRem
method that performs both division and remainder for Uints. All /=
, %=
, /
, and %
can call it instead of the divm
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I got this implemented in UInt -- should it also be implemented in BigInt?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
ab7b931
to
f6e9b0d
Compare
@9il able to review? |
auto rem = a /= b; | ||
assert(a == UInt!128.fromHexString("103e70bb237983337")); | ||
assert(rem == UInt!64.fromHexString("4932c5448b170cb3")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No
f5b98e5
to
313771b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are still pending comments.
FYI: This is ready for further review @9il |
35431ed
to
84f4453
Compare
This PR implements a version of Algorithm D (specified by Donald Knuth in TOACP Vol 2. 4.3, C implementation by Henry S. Warren Jr.), which aims to implement traditional "school-book" division for arbitrarily long numbers.
This is part of the process of getting #373 merged.
Status