From b93da13ee878bcdb3e5418dfe787a933bb820fd4 Mon Sep 17 00:00:00 2001 From: Prashant Date: Mon, 9 Feb 2026 20:49:00 +0530 Subject: [PATCH 1/6] std.bigint: addedBigInt constructor for Int128 Fixes issue 10945. --- std/bigint.d | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/std/bigint.d b/std/bigint.d index 7fea64cd6d5..f7271907714 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -180,6 +180,11 @@ public: assert(cast(long) b2 == -0x01_02_03_04_05_06L); } + import std.int128 : Int128; + this(Int128 x) pure nothrow @safe { + this = BigInt(x.data.hi)*(BigInt(1)<<64) + BigInt(x.data.lo); + } + /// Construct a `BigInt` from a built-in integral type. this(T)(T x) pure nothrow @safe if (isIntegral!T) From a975c61d8fb1fe432ba9d3cebb3a06b8bb987cd2 Mon Sep 17 00:00:00 2001 From: Prashant Date: Mon, 9 Feb 2026 21:34:27 +0530 Subject: [PATCH 2/6] add comment before public constructor. --- std/bigint.d | 1 + 1 file changed, 1 insertion(+) diff --git a/std/bigint.d b/std/bigint.d index f7271907714..0a935092bb3 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -180,6 +180,7 @@ public: assert(cast(long) b2 == -0x01_02_03_04_05_06L); } + /// Construct `BigInt` from `Int128` import std.int128 : Int128; this(Int128 x) pure nothrow @safe { this = BigInt(x.data.hi)*(BigInt(1)<<64) + BigInt(x.data.lo); From 96d4e7939b9eb78d0466c55334816b3a878976bf Mon Sep 17 00:00:00 2001 From: Prashant Date: Tue, 10 Feb 2026 18:38:41 +0530 Subject: [PATCH 3/6] included the opAssign for assignment from int128 ans unittest for sanity check --- std/bigint.d | 90 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/std/bigint.d b/std/bigint.d index 0a935092bb3..0626e1a2e83 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -180,11 +180,6 @@ public: assert(cast(long) b2 == -0x01_02_03_04_05_06L); } - /// Construct `BigInt` from `Int128` - import std.int128 : Int128; - this(Int128 x) pure nothrow @safe { - this = BigInt(x.data.hi)*(BigInt(1)<<64) + BigInt(x.data.lo); - } /// Construct a `BigInt` from a built-in integral type. this(T)(T x) pure nothrow @safe @@ -194,6 +189,91 @@ public: opAssign(x); } + /// Construct `BigInt` from `Int128` + import std.int128 : Int128; + 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 + { + Int128 x; + BigInt b; + BigInt re; + + x = Int128(0L); + b = BigInt(x); + re = BigInt(0L); + assert(b == re); + + x = Int128(42L); + b = BigInt(x); + re = BigInt(42L); + assert(b == re); + + x = Int128(-42L); + b = BigInt(x); + re = BigInt(-42L); + assert(b == re); + + x = Int128(-1L); + b = BigInt(x); + re = BigInt(-1L); + assert(b == re); + + x = (Int128(1L) << 100) + Int128(12345L); + b = BigInt(x); + re = (BigInt(1L) << 100) + BigInt(12345L); + assert(b == re); + + x = -((Int128(1L) << 100) + Int128(12345L)); + b = BigInt(x); + re = -((BigInt(1L) << 100) + BigInt(12345L)); + assert(b == re); + + x = Int128.min; + b = BigInt(x); + re = -(BigInt(1L) << 127); + assert(b == re); + } + /// @safe unittest { From 26b5167563e1e5c7b2b4f912fa5c36a2e4f8bcee Mon Sep 17 00:00:00 2001 From: Prashant Date: Tue, 10 Feb 2026 19:00:16 +0530 Subject: [PATCH 4/6] style fixed --- std/bigint.d | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/std/bigint.d b/std/bigint.d index 0626e1a2e83..39cf91ae877 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -217,7 +217,7 @@ public: // Now (hi, lo) is the positive magnitude if (hi != 0) { - ulong[2] mag = [cast(ulong)hi, lo]; + ulong[2] mag = [cast(ulong) hi, lo]; data.fromMagnitude(mag[]); } else @@ -234,6 +234,7 @@ public: /// @safe unittest { + import std.int128; Int128 x; BigInt b; BigInt re; From 8cd3fe904a55d97d4c02d0aaca95ba35f4a6350e Mon Sep 17 00:00:00 2001 From: Prashant Date: Tue, 10 Feb 2026 19:27:19 +0530 Subject: [PATCH 5/6] b = BigInt(x); to b = x; conversion --- std/bigint.d | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/std/bigint.d b/std/bigint.d index 39cf91ae877..6abadf40ad9 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -240,37 +240,37 @@ public: BigInt re; x = Int128(0L); - b = BigInt(x); + b = x; re = BigInt(0L); assert(b == re); x = Int128(42L); - b = BigInt(x); + b = x; re = BigInt(42L); assert(b == re); x = Int128(-42L); - b = BigInt(x); + b = x; re = BigInt(-42L); assert(b == re); x = Int128(-1L); - b = BigInt(x); + b = x; re = BigInt(-1L); assert(b == re); x = (Int128(1L) << 100) + Int128(12345L); - b = BigInt(x); + b = x; re = (BigInt(1L) << 100) + BigInt(12345L); assert(b == re); x = -((Int128(1L) << 100) + Int128(12345L)); - b = BigInt(x); + b = x; re = -((BigInt(1L) << 100) + BigInt(12345L)); assert(b == re); x = Int128.min; - b = BigInt(x); + b = x; re = -(BigInt(1L) << 127); assert(b == re); } From 7466894ff5344cf190547ce2bcfd204cb684d6d8 Mon Sep 17 00:00:00 2001 From: prashant suthar Date: Wed, 11 Mar 2026 12:17:44 +0530 Subject: [PATCH 6/6] Remove blank line before BigInt constructor Removed an unnecessary blank line before the constructor. --- std/bigint.d | 1 - 1 file changed, 1 deletion(-) diff --git a/std/bigint.d b/std/bigint.d index 6abadf40ad9..24788f89d0b 100644 --- a/std/bigint.d +++ b/std/bigint.d @@ -180,7 +180,6 @@ public: assert(cast(long) b2 == -0x01_02_03_04_05_06L); } - /// Construct a `BigInt` from a built-in integral type. this(T)(T x) pure nothrow @safe if (isIntegral!T)