From c1348817b6cf1a0eb9610c1c5f5a4cf842bf635d Mon Sep 17 00:00:00 2001 From: jmh530 Date: Fri, 13 Nov 2026 09:13:27 -0500 Subject: [PATCH] Fix x86 BigInt division by powers of two The x86 multibyteDivAssign routine excludes power-of-two divisors, but the single-limb BigInt division path can pass them directly. Even BigInt!2(1) /= BigInt!2(1) raises STATUS_INTEGER_OVERFLOW with Windows LDC 1.43.0 in both unoptimized and optimized builds. Route power-of-two divisors through the existing portable divider within the x86 dispatcher, preserving the incoming remainder and retaining assembly for other divisors. Add regression coverage for every uint power of two, one- through four-limb inputs, nonzero incoming remainders, and non-power-of-two controls against ulong arithmetic. Cover compile-time evaluation and public BigInt quotient, remainder, and scalar division paths. AI-Assisted-By: Codex (GPT-6 Astra Medium) --- source/mir/bignum/integer.d | 32 +++++++++++++++ source/mir/bignum/internal/phobos_kernel.d | 47 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/source/mir/bignum/integer.d b/source/mir/bignum/integer.d index 0991c151..84fa8ecd 100644 --- a/source/mir/bignum/integer.d +++ b/source/mir/bignum/integer.d @@ -1044,6 +1044,38 @@ struct BigInt(uint size64) } } +// Division by a single-limb power of two must also work through the +// BigInt divisor path, which uses the low-level division dispatcher. +version(mir_bignum_test) +@safe pure @nogc unittest +{ + auto one = BigInt!2(1); + auto divisorOne = BigInt!2(1); + one /= divisorOne; + assert(one == 1); + + foreach (shift; 0 .. 32) + { + const divisor = 1u << shift; + auto bigDivisor = BigInt!2(divisor); + foreach (dividend; [1UL, 0x0123_4567_89ab_cdefUL, ulong.max]) + { + auto quotient = BigInt!2(dividend); + quotient /= bigDivisor; + assert(quotient == dividend / divisor); + + auto remainder = BigInt!2(dividend); + remainder %= bigDivisor; + assert(remainder == dividend % divisor); + + auto scalarQuotient = BigInt!2(dividend); + const scalarRemainder = scalarQuotient /= divisor; + assert(scalarQuotient == dividend / divisor); + assert(scalarRemainder == dividend % divisor); + } + } +} + /// Check @nogc toString impl version(mir_bignum_test) @safe pure @nogc unittest { diff --git a/source/mir/bignum/internal/phobos_kernel.d b/source/mir/bignum/internal/phobos_kernel.d index 4a77b30e..534b74ea 100644 --- a/source/mir/bignum/internal/phobos_kernel.d +++ b/source/mir/bignum/internal/phobos_kernel.d @@ -194,7 +194,12 @@ uint multibyteDivAssign()(uint[] dest, uint divisor, uint overflow) if (__ctfe) return std.internal.math.biguintnoasm.multibyteDivAssign(dest, divisor, overflow); else version (D_InlineAsm_X86) + { + // The x86 assembly divider requires a non-power-of-two divisor. + if ((divisor & (divisor - 1)) == 0) + return std.internal.math.biguintnoasm.multibyteDivAssign(dest, divisor, overflow); return std.internal.math.biguintx86.multibyteDivAssign(dest, divisor, overflow); + } else version (LDC_ARM_asm) return std.internal.math.biguintarm.multibyteDivAssign(dest, divisor, overflow); else @@ -202,6 +207,48 @@ uint multibyteDivAssign()(uint[] dest, uint divisor, uint overflow) } else alias multibyteDivAssign = std.internal.math.biguintnoasm.multibyteDivAssign; +version(mir_bignum_test) +@safe unittest +{ + // Cover every uint power of two, including 1 and the high bit, and + // non-powers of two that continue to use the x86 assembly divider. + immutable uint[8] otherDivisors = + [3u, 6u, 7u, 10u, 0x7fff_ffffu, 0x8000_0001u, 0xffff_fffeu, uint.max]; + immutable uint[4][4] inputs = [ + [0u, 0u, 0u, 0u], + [1u, 0u, 0u, 0u], + [uint.max, uint.max, uint.max, uint.max], + [0x0123_4567u, 0x89ab_cdefu, 0xfedc_ba98u, 0x7654_3210u]]; + foreach (i; 0 .. 32 + otherDivisors.length) + { + const divisor = i < 32 ? 1u << i : otherDivisors[i - 32]; + foreach (overflow; [0u, divisor / 2, divisor - 1]) + foreach (input; inputs) + foreach (length; 1 .. input.length + 1) + { + uint[4] actual = input; + uint[4] expected = input; + ulong remainder = overflow; + foreach_reverse (ref limb; expected[0 .. length]) + { + const value = (remainder << 32) | limb; + limb = cast(uint)(value / divisor); + remainder = value % divisor; + } + const result = multibyteDivAssign(actual[0 .. length], divisor, overflow); + assert(actual == expected); + assert(result == remainder); + } + } + + // The dispatcher's compile-time path must remain usable as well. + static assert(() { + uint[2] value = [1u, 1u]; + const remainder = multibyteDivAssign(value[], 2, 1); + return value == [0x8000_0000u, 0x8000_0000u] && remainder == 1; + }()); +} + version (HaveAsmVersion) void multibyteAddDiagonalSquares()(uint[] dest, const(uint)[] src) {