Skip to content

Fix x86 BigInt division by powers of two - #506

Open
jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/fix-x86-power-two-division
Open

jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/fix-x86-power-two-division

Conversation

@jmh530

@jmh530 jmh530 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

BigInt!2(1) /= BigInt!2(1) raises STATUS_INTEGER_OVERFLOW (0xc0000095) on Windows LDC x86 because the single-limb division path forwards a power-of-two divisor to an assembly routine whose contract excludes it. The failure reproduces with both -O0 and -O2 -enable-inlining.

Route power-of-two divisors through the existing portable divider inside the x86 dispatcher, preserving the incoming remainder and retaining assembly for other divisors.

Fixes #505.

Regression coverage

  • Check 1,920 low-level combinations against native ulong arithmetic: every uint power of two, eight non-power-of-two controls, three incoming remainder choices, four input patterns, and lengths from one through four limbs.
  • Cover compile-time division with a nonzero incoming remainder.
  • Cover 1 / 1, BigInt quotient/modulo, and scalar division for every uint power of two.
  • Confirm that removing only the dispatch correction makes the new low-level regression reproduce the original overflow.

Validation

  • Bignum tests with DIP1000: 14 modules passed in each of 12 configurations. Windows LDC 1.43.0 and DMD 2.113.0 covered x86/x64, debug and optimized; Linux LDC 1.43.0 and DMD 2.113.1 covered x64, debug and optimized.
  • Windows LDC x86 DUB ci-bignum-test / unittest-ci, combined build: 19 modules passed. An isolated recipe selected the existing mir-core 1.7.4 by path and omitted the unused silly configuration for offline resolution.
  • Windows LDC x86, unoptimized and optimized: the original BigInt and negative-binomial reproducers, 2,560 independent division references, and the downstream symmetry checks passed, including 840 exact-rational inverse references and Fp32/64/128 predecessor checks.
  • git diff --check passed.

Linux x86 execution was not validated: compilation reached linking, but the local WSL installation lacks the required system multilib files. These are local validation results; remote CI has not run for a PR.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

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)
@jmh530

jmh530 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

CI failures are due to an outstanding Codecov issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BigInt division by powers of two can trap on x86

1 participant