differential design

Design goal

Fixed corpora prove that the library agrees with a reference on the rows someone chose. A randomized campaign asks a different question: on inputs nobody chose, over the whole range of precisions, exponents, rounding directions and operand shapes, does the library ever disagree with two mature implementations of the same arithmetic? The harness must make that question cheap to ask millions of times, make every answer reproducible from a seed, and make a disagreement small enough to read.

Mathematical background

Both oracles are implementations, not specifications.

  • Binary. GNU MPFR rounds every result correctly in its own exponent range and has no subnormal numbers. IEEE 754-2019 §4.3 defines the five rounding directions and §7.5 defines the exceptions; underflow (§7.5) is tiny and inexact, with tininess detected before or after rounding (§7.5, “the implementer chooses”). The generator reproduces the IEEE results from MPFR in three steps: it sets MPFR’s exponent range to the range of the row’s BinaryContext, widened downwards by the precision so that results below the smallest normal number are still computed to full precision; it calls mpfr_subnormalize, which re-rounds such a result to the subnormal grid using the ternary value of the first rounding, so no double rounding occurs; and it derives the underflow flag from the definition instead of reading MPFR’s own flag, once for each tininess rule.

  • decimal_gda. libmpdec (Python decimal) implements the General Decimal Arithmetic specification, and the library’s GDA package implements the same text. Two things differ and are handled in the generator, not by ignoring rows: libmpdec lumps Division_undefined into Invalid_operation, and the GDA tosci and toeng rows apply the context precision to their operand (dtfile.html, “Processing of a test case line”). Further differences of the power operation are registered as named expected differences of the decimal_gda-power family. A row of such a class is never skipped, and its expected value is never what this library returns: the generator takes the value from an independent source (exact arithmetic, or libmpdec at a higher precision), checks the facts the class stands on, writes the row with that value and a comment line expected-difference <class>: libmpdec gives ..., and a row outside every shape keeps libmpdec’s result and fails when the two differ. The one class whose value could only be a copy of the library’s own algorithm is dropped (integer-power-accuracy, below).

    • power-of-ten #351: power(10^k, y) with a non-integral y and an integral k*y. GDA 1.70 allows an inexact result to be up to one unit in the last place in error; the library returns the exact value 10ky10^{ky} in every rounding mode, libmpdec can be one unit in the last place away (and flags Subnormal for an exact result at the smallest normal number). Checked: libmpdec’s value is within one unit in the last place of the exact one. The family draws about one case in 25 from this class, with a random rounding mode, because its other rows are nearest-even only.
    • integer-power-accuracy: an integral exponent, the result one unit in the last place from libmpdec’s. libmpdec rounds correctly; the library follows the decNumber algorithm (working precision digits + digits(n) + 2, half-even, the base inverted first for a negative exponent), whose error is below 0.55 units in the last place (0.6 for a negative exponent), and power.decTest pins a result that is not within 0.5 units (powx1215, “A (rare) case where the last digit is not within 0.5 ULP”). The only value the generator could write for such a case is the result of a Python copy of that algorithm, which the library also implements, so the case gets no row: the generator recognises the class only to drop it (the ids of the other cases do not change), and the decTest corpus, whose rows come from decNumber itself, is the oracle for these results.
    • noninteger-power-rounding: a non-integral exponent where libmpdec is not the correctly rounded value and the library is. The expected value is the correct rounding recomputed at precision + 60 digits by libmpdec, used only when the bounds of that value round alike; libmpdec’s must be within one unit of it.
    • underflow-with-inexact-subnormal: libmpdec reports Inexact and Subnormal without Underflow. GDA 1.70, Underflow: “the result is both inexact and subnormal” (and power.decTest powx392 expects Underflow Subnormal Inexact Rounded). The row expects libmpdec’s conditions plus Underflow, only for that shape.
    • subnormal-before-rounding: GDA 1.70, Subnormal: the adjusted exponent is less than Emin “before any rounding”, libmpdec decides it on the rounded result. For an integral power whose exact value, formed exactly, is below 10emin⁡10^{e_{\min}} while libmpdec’s rounded result is 10emin⁡10^{e_{\min}} itself, the row expects Subnormal and Underflow added to libmpdec’s conditions.

    Rows with an exponent of zero in a clamped context were a defect of this library (power(x, 0) was folded down, which decNumber and libmpdec do not do), fixed in the pull request of issue 405; they need no registration.

  • IEEE decimal elementary functions. The IEEE decimal package has functions (exp, atan2, pown, …) that libmpdec either lacks or answers only to nearest-even, so their oracle is MPFR through its enclosure, not through one result: each decimal operand is bracketed by 2048-bit directed bounds, the function is evaluated at 768 bits in both directions on every corner, the enclosure is widened by one ulp, and both endpoints are rounded to the decimal context with Python decimal. A case becomes a row only if the endpoints round to one representation with one flag set and the rounded value lies outside the enclosure, which also makes the inexact flag certain. Underflow and Subnormal are recomputed for both tininess rules from the same endpoints, because libmpdec knows only one. Exact results (an enclosure that contains the rounded value) are not sampled; they belong to the white-box tests of each function.

A disagreement is therefore a lead, not a verdict. Each one is triaged against the clause of IEEE 754-2019 or of the GDA specification before it is called a defect, and the cases where the oracle is the one that deviates are listed in the test-data READMEs.

Design decisions

Every case is a pure function of (seed, family, index)

Both generators seed a SplitMix64 stream from the triple, so row 1,234,567 of a ten-million-row run is regenerated by asking for one row. A failure record therefore needs only a case id, never the row, and an interrupted run resumes by shard. The streams use integer arithmetic only (no libm), so a seed means the same rows on every machine.

Structured inputs, not only uniform ones

Uniform random bits almost never hit the cases where arithmetic is wrong: ties, alignment thresholds, cancellation, the first subnormal, the last finite number. The generators build these on purpose. For an exact operation the precision is set to one bit (digit) fewer than the exact result has, which turns the result into a halfway case for any operand pair; a perturbation far below the last bit gives the “just above” and “just below” neighbours. Operand exponent gaps are drawn around the precision, around each operand’s width and around twice the precision, where the alignment logic changes. Boundary rows place the exact result next to the smallest normal number or the overflow threshold, the places where tininess-before and tininess-after, or directed and nearest rounding, part ways. A mutation check keeps this honest: swapping the two tininess rules in the executor fails 0.15% to 1.2% of the rows of an operation, and swapping round-to-nearest-even with nearest-away fails 0.9% to 2.8%.

mare_mark validates batches

The runner validates the library against the oracle before it measures it, records every event in append-only JSONL, derives the run seed, and ships a shrinker and a replay specification. The harness uses all four. A dataset is a batch of consecutive cases; the implementation under test runs the batch through the matching frontend; the reference oracle is the oracle rows themselves (every case must agree). Validating per batch keeps the record at roughly one validation line per thousand cases instead of one per case, and a batch that disagrees is delta-debugged by halving until one case remains, so the record names the failing case rather than a thousand suspects.

The runner’s timing machinery is a by-product. The protocol uses one iteration per batch and no warm-up, so the cost is a small multiple of the execution time itself, and the timings are not claimed as benchmarks.

Oracle programs stay outside MoonBit

MoonBit never calls MPFR or Python. The generators run first and write rows; the harness only reads rows. That keeps the library’s tests free of native dependencies, lets the same corpus run on native, JavaScript, Wasm and Wasm-GC, and means an oracle upgrade is a regeneration, not a code change.

Correctness and invariants

  • A batch passes only if every case in it passed; a case that no frontend can execute (unsupported or diagnostic rows) is a failure, not a skip.
  • The expected value of a case is stored in its row; the harness never recomputes it.
  • execute_batch returns one entry per case, in order; a frontend that returns a different count is an infrastructure failure.
  • The failure shrinker only ever halves a batch, so it terminates in at most log2(batch) steps and never produces an input that was not in the corpus.

Alternatives rejected

  • A binding to MPFR from MoonBit. It would make moon test depend on a native library and exclude every other backend.
  • One validation line per case. A million-case run would write a gigabyte of JSONL that says “valid” a million times.
  • The runner’s full measurement protocol. Twenty-fold slowdown for numbers this harness does not use.
  • Discarding rows on which the oracles are known to deviate. They are filtered where they are generated (documented per family), so the denominator of a published count is the number of rows executed.

Boundaries

  • The harness reports agreement with MPFR and libmpdec on the sampled space. It does not prove the library correct, and it does not cover operations the oracles do not offer (for example decimal elementary functions other than exp, ln, log10 and power).
  • Nearest-away results below the smallest normal number are not compared: mpfr_subnormalize has no nearest-away mode.
  • The decimal elementary functions are sampled only with contexts below the decNumber limit of 10^6 on the exponent range, beyond which the library answers Invalid_context by design.
  • Wasm-GC cannot run the harness (no async runtime); it runs the same corpora through the synchronous floating-conformance runner without the mare_mark record.