Skip to content

Fp multiplication loses representable results near the minimum exponent #487

Open

Description

@jmh530

Summary

Fp multiplication can return zero for a representable nonzero result near the minimum exponent, including multiplication by one.

Investigating a fix also exposed a related failure when narrowing a representable subnormal Fp!256 value to Fp!128.

Reproduced on upstream revision 2da2123 with optimized Windows DMD 2.113.0 and LDC 1.43.0.

Multiplication reproducer

import mir.bignum.fp: Fp;
import std.stdio;

void main()
{
    auto x = Fp!128(1);
    x.exponent = long.min;

    const identity = x * Fp!128(1);
    writeln(!!identity.coefficient); // false; expected true

    auto a = Fp!128(1);
    a.exponent = long.min / 2 - 32;

    const square = a * a;
    writeln(!!square.coefficient); // false; expected true
}

For the identity, the expected coefficient and exponent are unchanged.

For the square, Fp!128(1) has coefficient 2^127. The exact squared value can therefore be represented with coefficient 2^127 and exponent long.min + 63. The result should not underflow.

Apparent cause

extendedMul detects overflow of the raw exponent addition before accounting for the product's representation.

When that addition underflows, the current TODO: subnormal branch unconditionally clears the coefficient. Some products could instead be retained at long.min by shifting the product coefficient.

Related narrowing failure

Simply preserving that coefficient is insufficient because narrowing subnormal values has another boundary failure:

import mir.bignum.fp: Fp;

void main()
{
    auto wide = Fp!256(1);
    wide.exponent = long.min;
    wide.coefficient >>= 129; // coefficient is now 2^126

    const narrow = cast(Fp!128)wide;
}

The value fits in Fp!128 with the same coefficient and exponent, but this asserts in the fixed-width integer shift implementation.

The underflow path in Fp.opCast calculates:

ret.coefficient >>= cast(uint)(ret.exponent - exponent.min);

After exponent addition wraps, this produces an invalid shift count rather than the required small right shift.

Suggested fix direction

Handle these paths together:

  • Preserve representable subnormal products when the raw exponent sum underflows.
  • Calculate the exponent deficit without signed overflow.
  • Correct narrowing at the exponent floor.
  • Handle rounding when a shift discards nonzero bits.
  • Return zero only when the result actually rounds to zero.

Regression tests should cover multiplication by one, exact squares near the boundary, subnormal narrowing, rounding boundaries, genuine underflow, and multiple coefficient widths. Ordinary multiplication performance should also be checked.

A simple multiplication-only shift patch fixed the initial examples but exposed the narrowing failure above, so it was not retained as a complete fix.

Context

Found during libmir/mir-stat#156. That PR restores a direct exponent-one return in its integer-power helper, avoiding an unnecessary multiplication, but does not repair general Fp multiplication.

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

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions