differential tutorial

This tutorial runs a randomized differential campaign: it generates seeded rows with MPFR (binary, IEEE decimal) and libmpdec (decimal_gda), validates floating against them through the mare_mark runner, reads the JSONL record, and replays and minimizes a failure.

I want toUse
check the committed fixturesjust random-diff smoke
run a campaignjust random-diff run …
read the recordthe JSONL events
reproduce one casejust random-diff replay <id>
shrink a failing casejust random-diff minimize <id>
run another backend--target wasm, js, wasm-gc

Quick start

The committed fixtures need no oracle: their expected values are in the rows.

just random-diff smoke
binary: 650/650 passed (testdata/binary/mpfr-4.2.2-random.txt)
binary-regression: 163/163 passed (testdata/binary/random-regressions.txt)
decimal_gda: 650/650 passed (testdata/decimal_gda/random/libmpdec-random.decTest)
decimal_gda-regression: 0/10 passed, 10 known failures (xfail) (testdata/decimal_gda/random/random-regressions.decTest)
decimal: 468/468 passed (testdata/decimal/random/mpfr-decimal-random.txt)

smoke builds the synchronous floating-conformance executable for the native target and runs the fixture files through it. A known failure recorded in a regression fixture is annotated xfail with its issue number; the smoke run requires exactly those rows to fail, so a fix shows up as an unexpected pass.

Everyday tasks

Decimal elementary functions against MPFR

The IEEE decimal package’s elementary functions have one family each, decimal-<function> (decimal-exp, decimal-atan2, decimal-pown, decimal-exp2m1, decimal-log10p1, …), checked against certified MPFR enclosures instead of libmpdec. They are not in the default set (there are as many families as functions); name them with --family, or add all of them with --decimal. A case id reads m<seed>.<function>.<index>.

just random-diff run --family decimal-exp --family decimal-atan2 --count 100000 --target native
just random-diff replay m20261118.atan2.137

Operands and contexts are drawn so that results reach overflow, underflow and the subnormal range; exact results are not sampled (an enclosure cannot certify them), and exponent ranges stay within ±20,000 because the cost of the exact endpoint conversion grows steeply beyond that.

Run a campaign

A campaign needs the oracles: a C compiler with MPFR and GMP (Homebrew puts them in /opt/homebrew; pass --mpfr-prefix elsewhere) for the binary families, and Python 3 with the decimal module for the decimal families.

just random-diff run --family add --family decimal_gda-add --count 100000 --target native
2 shards, 100000 cases per family, seed 20261118, targets native
[1/2] native add            0+100000  ok    4.3s
[2/2] native decimal_gda-add  0+100000  ok   46.6s
native: 200,000 cases
    add               100,000
    decimal_gda-add   100,000
batches validated 200, invalid 0
records and summary: .tmp/random-diff/run-20261118-120000

--family is repeatable and defaults to every family. The binary families are add sub mul div sqrt fma strtofr getstr shortest elem elem2 elem3 elem4 (elem2 holds exp2m1, exp10m1, log2p1 and log10p1, elem3 holds rsqrt, compound and powr, elem4 holds asinpi, acospi, atanpi and atan2pi, which elem does not draw); the decimal_gda families (General Decimal Arithmetic against libmpdec) are decimal_gda-add decimal_gda-subtract decimal_gda-multiply decimal_gda-divide decimal_gda-squareroot decimal_gda-fma decimal_gda-apply decimal_gda-tosci decimal_gda-toeng decimal_gda-exp decimal_gda-ln decimal_gda-log10 decimal_gda-power. The decimal_gda-*wide families sample the contexts on which the elementary functions refuse by design and are opt-in. The decimal-<function> families belong to the IEEE decimal package (see above). The former prefixes dec- (now decimal_gda-) and decm- (now decimal-), and the option --decm (now --decimal), are deprecated aliases and still accepted. --count is per family, --shard bounds the rows generated per file, --batch the cases validated together, --jobs the parallel shards.

The run directory holds records-<target>.jsonl (append-only, one file per backend) and summary.json with the exact number of cases per backend, family and operation. The command exits with 1 when a batch disagreed.

Read the record

python3 -c "
import json
for line in open('.tmp/random-diff/run-20261118-120000/records-native.jsonl'):
    event = json.loads(line)
    if event['type'] != 'validation': continue
    print(event['status'], event['operands'], event['scale'][:60])
" | head -3
valid ['20261118.add.0', '20261118.add.999', '1000'] # mpfr-random-v1 mpfr=4.2.2 gmp=6.3.0 fam
valid ['20261118.add.1000', '20261118.add.1999', '1000'] # mpfr-random-v1 mpfr=4.2.2 gmp=6.3.0 fam
valid ['20261118.add.2000', '20261118.add.2999', '1000'] # mpfr-random-v1 mpfr=4.2.2 gmp=6.3.0 fam

Each validation names the batch by its first and last case id and its size. An invalid one is followed by a validation_failure that carries the minimized case.

Replay a case

just random-diff replay 20261118.add.1234
just random-diff replay 20261118.add.1000..20261118.add.1999 --target wasm
just random-diff replay d20261118.divide.77

The generators are pure functions of seed, family and index, so the id is the whole reproduction. The output is the frontend’s summary; a failing row is printed with the expected and the actual value and flags.

Minimize a failure

just random-diff minimize d20261118.add.7352 --output testdata/decimal_gda/random/random-regressions.decTest

The minimizer tries simpler variants (smaller precision, plain rounding, no clamp, narrower exponent range, shorter operands, operands rewritten as 1E+k), asks the oracle for the correct answer of each variant, and keeps a variant when the library still disagrees. The result is a row the oracle produced itself, so it is a consistent regression case, not an edited one.

Sweep the range limits

The random rows rarely land on an underflow or overflow threshold. For the binary elementary functions, tools/range_boundary_rows.py writes rows on the thresholds themselves, for every precision in a range, with the oracle columns left as ?:

python3 tools/range_boundary_rows.py --p-min 2 --p-max 300 --out .tmp/boundary.in
(head -n 1 .tmp/boundary.in; .tmp/random-diff/bin/generate_mpfr_random_oracle eval < .tmp/boundary.in) > .tmp/boundary.rows
moon run tooling/src/cli --target native --release -- --backend mpfr .tmp/boundary.rows

The generator binary is built by any just random-diff run. A failing row is named with its expected and actual value and flags, so it can be pasted into a regression fixture.

Other backends

--target selects the backend of the harness: native, wasm, js or llvm (the mare_mark runner has no Wasm-GC runtime). For Wasm-GC, run the same fixtures through the synchronous runner:

just random-diff smoke --target wasm-gc
just random-diff replay 20261118.add.1234 --target wasm-gc

Going further

Common pitfalls

  • A stale moon earlier on PATH. The build runs moon; make sure the one you meant comes first.
  • No MPFR headers. The generator build fails with mpfr.h not found; set MPFR_PREFIX or pass --mpfr-prefix.
  • Seeds above 2^31. The harness takes the seed as a 32-bit integer and the tool reduces it; the case ids keep the full seed.
  • Treating a disagreement as a bug. The oracles are implementations. Read the clause first.

Next steps

Run a longer campaign on every backend, then read verification for how the published counts were produced.