-
-
Notifications
You must be signed in to change notification settings - Fork 765
std.bigint: support construction from Int128 #10948
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
base: master
Are you sure you want to change the base?
Changes from all commits
b93da13
a975c61
96d4e79
26b5167
8cd3fe9
7466894
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 |
|---|---|---|
|
|
@@ -188,6 +188,92 @@ public: | |
| opAssign(x); | ||
| } | ||
|
|
||
| /// Construct `BigInt` from `Int128` | ||
| import std.int128 : Int128; | ||
|
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. Putting this import here means that it will be affected by the To fix this, move the import to the top of the module, outside the declaration of |
||
| this(Int128 x) pure nothrow @safe { | ||
| data = data.init; | ||
| opAssign(x); | ||
| } | ||
|
|
||
| /// Assignment from `Int128`. | ||
| BigInt opAssign(T : Int128)(T x) @safe | ||
| { | ||
| sign = false; | ||
|
|
||
| ulong lo = x.data.lo; | ||
| long hi = x.data.hi; | ||
|
|
||
| // Determine sign and get absolute value | ||
| if (hi < 0) | ||
| { | ||
| sign = true; | ||
|
|
||
| // Two's complement negate | ||
| lo = ~lo + 1; | ||
| hi = ~hi + (lo == 0); | ||
| } | ||
|
|
||
| // Now (hi, lo) is the positive magnitude | ||
| if (hi != 0) | ||
| { | ||
| ulong[2] mag = [cast(ulong) hi, lo]; | ||
| data.fromMagnitude(mag[]); | ||
| } | ||
| else | ||
| { | ||
| data = lo; | ||
| } | ||
|
|
||
| if (data.isZero) | ||
| sign = false; | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /// | ||
| @safe unittest | ||
| { | ||
| import std.int128; | ||
| Int128 x; | ||
| BigInt b; | ||
| BigInt re; | ||
|
|
||
| x = Int128(0L); | ||
| b = x; | ||
| re = BigInt(0L); | ||
| assert(b == re); | ||
|
|
||
| x = Int128(42L); | ||
| b = x; | ||
| re = BigInt(42L); | ||
| assert(b == re); | ||
|
|
||
| x = Int128(-42L); | ||
| b = x; | ||
| re = BigInt(-42L); | ||
| assert(b == re); | ||
|
|
||
| x = Int128(-1L); | ||
| b = x; | ||
| re = BigInt(-1L); | ||
| assert(b == re); | ||
|
|
||
| x = (Int128(1L) << 100) + Int128(12345L); | ||
| b = x; | ||
| re = (BigInt(1L) << 100) + BigInt(12345L); | ||
| assert(b == re); | ||
|
|
||
| x = -((Int128(1L) << 100) + Int128(12345L)); | ||
| b = x; | ||
| re = -((BigInt(1L) << 100) + BigInt(12345L)); | ||
| assert(b == re); | ||
|
|
||
| x = Int128.min; | ||
| b = x; | ||
| re = -(BigInt(1L) << 127); | ||
| assert(b == re); | ||
| } | ||
|
|
||
| /// | ||
| @safe unittest | ||
| { | ||
|
|
||
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.
Not sure if this is needed, but I think the doc comment should be after the import.