Skip to content

Preserve representable Fp products and conversions at the exponent floor - #488

Open
jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/fp-multiplication-underflow
Open

jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/fp-multiplication-underflow

Conversation

@jmh530

@jmh530 jmh530 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #487.

Fp multiplication currently discards a product when the sum of the operand exponents falls below long.min, even when the coefficient makes the result representable. This can turn multiplication by one into zero near the exponent floor.

Handle these products by shifting and rounding the coefficient instead of immediately returning zero. Ordinary multiplication rounds directly to its destination precision, avoiding double rounding through an intermediate wide subnormal. extendedMul retains its wider result, and genuine underflow preserves signed zero.

The resulting subnormal values also need to survive conversions between Fp widths. Preserve coefficients that already fit when narrowing, and limit normalization when widening to the available exponent range.

Regression coverage

Cover multiplication by one, representable squares, halfway rounding with ties to even, signed zero, widening and narrowing subnormals, and a concrete double-rounding counterexample.

Validation

  • Full Windows DMD and LDC suites: 75 modules passed each.
  • Full LDC suite with DIP1000 lifetime checks: 75 modules passed.
  • Focused optimized Linux DMD and LDC suites: 14 modules passed each.
  • Independent exact-integer references: 2,316 multiplication cases and 900 conversion cases matched.
  • Paired ordinary Fp!128 multiplication benchmarks showed approximately 0–2.4% overhead on Windows LDC and no consistent slowdown on DMD across three tested cases.

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

Handle multiplication exponent underflow without discarding products
that remain representable through their coefficients. Round directly
to the destination precision using nearest, ties-to-even rounding,
avoiding double rounding through an intermediate wide subnormal.
Keep extendedMul's wider result and preserve signed zero on underflow.

Preserve fitting subnormal coefficients during narrowing and limit
widening normalization to the available exponent range. Use an
unsigned distance from the exponent floor for the boundary check
and correct the underflow shift direction.

Add regressions for multiplication by one, representable squares,
halfway rounding, signed zero, subnormal conversions, and a product
that previously rounded twice.

Fixes libmir#487.

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

jmh530 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Failing CircleCI is unrelated

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.

Fp multiplication loses representable results near the minimum exponent

1 participant