Skip to content

cpu: lower FDIV and FDIVR forms in the x87 leaf profile - #433

Merged
R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-x87-div
Nov 22, 2026
Merged

R0Y00 merged 1 commit into
R0Y00:mainfrom
denn0x:cpu/lower-x87-div

Conversation

@denn0x

@denn0x denn0x commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Change

Refs #406. This is the division step of the x87 plan, on top of the add (#422) and multiply (#431) profiles. The opt-in profile of lower(..., x87=True) now also accepts these forms of both fdiv and fdivr:

  • an m32 or m64 operand;
  • st, st(i) and st(i), st;
  • the popping form fdivp st(i), st or fdivrp st(i), st, for i from 1 to 7.

fdivr and fdivrp divide the source by the destination. The entry/exit contract, the static stack depths and the existing rejections are unchanged. As before, a popping form with st(0) as the destination is rejected.

Out of scope: this PR does not add fsub/fsubr, fadd memory and register forms, fidiv, fsqrt or fprem. They remain rejected and are checked. Conversions, constants, comparisons and stack faults remain separate steps.

Arithmetic (tools/x87_soft.h).

  • x87_div: divides the 64-bit significands with _udiv128. The dividend is x << 64, or x << 63 when x >= y, so the quotient lies in [2^63, 2^64). A nonzero remainder becomes a sticky bit, which is enough to round to 53 bits. The result goes through the same x87_round as the sum and the product: extended exponent range, denormalized first below the normal range.
  • Special cases: zero by zero and infinity by infinity give the indefinite. A finite nonzero value by zero gives a signed infinity, the masked divide-by-zero response. Infinity by a finite value gives a signed infinity, and a finite value by infinity or zero by a finite value gives a signed zero.
  • Sign and NaNs: the sign is the exclusive or of the operand signs. NaN and invalid operands use the existing x87_special_operands, and memory operands keep a signaling NaN signaling, as in cpu: lower FMUL forms in the x87 leaf profile #431.
  • x87_divr(a, b): this is x87_div(b, a).

Fixture

tests/cpu/x87_div.asm and .def are generated by tests/cpu/x87_div.py asm. The 17 exports are:

  • fdivp st(1), st stored to m80, m64 and m32;
  • fdivrp st(1), st;
  • all four st, st(i) / st(i), st forms of fdiv and fdivr;
  • x / x through fdiv st, st(0);
  • fdiv and fdivr with an m32 and an m64 operand;
  • float / float and double / double;
  • two three-value chains through fdivp st(1), st, fdivp st(2), st and fdivrp st(1), st.

Decode check. emit requires each export's decoded x87 instructions to be exactly the forms its sequence names (mnemonic plus operands or memory size). The register encodings dc f0+i and dc f8+i are the ones that some assemblers swap between fdiv and fdivr, so a swap would fail before anything runs.

Harness and checks. As in #422 and #431:

  • the exports run through the fnsave/frstor harness with control word 0x027F;
  • native and translated DLLs get identical input bytes;
  • the output block and 64 guard bytes on each side of the input and output are compared against an independent Fraction model;
  • the live-value check runs around all 17 exports.

Cases. The 23939 cases include:

  • every pair of the 34 special 80-bit encodings;
  • every special m80 value against 17 m32 and 18 m64 special encodings, in both orders;
  • exact quotients (2^53 + 1) k / k, which sit halfway between 53-bit neighbours;
  • constructed quotients whose 64-bit significand ends exactly on a 53-bit tie with a nonzero remainder, so only the remainder decides the rounding;
  • x / 1, 1 / x, x / x and small-integer quotients;
  • quotients beyond the binary64 range, stored to m80, m64 and m32;
  • quotients at the extended overflow and underflow thresholds, and denormal results;
  • NaN ties by sign;
  • random operands.

Negative controls. Seven mutated translations must each mismatch:

Control Mutation Mismatches
pc64 the quotient rounds to 64 significand bits 8785/23939
double the quotient rounds in the binary64 exponent range 14373/23939
nosticky a nonzero remainder is ignored 253/23939
divzero a finite value by zero gives the indefinite 602/23939
reverse fdivr divides in the fdiv order 6829/23939
zerosign a zero quotient is always +0 551/23939
memquiet m32/m64 operands are quieted before the NaN rules 54/23939

Rejections. Translation rejects ten sequences with the expected diagnostic:

  • fdivp st(0), st and fdivrp st(0), st;
  • fdiv m32 on an empty stack;
  • fdivr st(2), st with two values;
  • fdivrp with one value;
  • fidiv m32 and fidivr m16;
  • fsqrt, fprem and fadd m32.

Change to an existing test. cpu_aot_x87_mul listed fdiv st, st(1) among its rejections. This PR brings that form into the profile, so that case now uses fsub st, st(1), which is still rejected. The test keeps its nine rejections and its case and control counts.

Verification

Tested at 6335f4a on 97016d9. Windows 11 build 26200, MSVC 19.50.35720, Windows SDK 10.0.26100.0, Python 3.13, AMD Ryzen 7 7700 (AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD).

cmake --build --preset dev --target cpu_x87_div_lifted
ctest --preset dev --no-tests=error -R '^cpu_aot_x87_div$' -V
98: 23939 cases over 17 exports (output block and guards) matched the reference in native and translated DLLs; each export returned with control 0x027f and an empty stack
98: processor: AMD64 Family 25 Model 97 Stepping 2, AuthenticAMD
98: x87_call kept a live caller ST(0) value and control word around all 17 native and translated exports
98: control pc64: 8785/23939 mismatches
98: control double: 14373/23939 mismatches
98: control nosticky: 253/23939 mismatches
98: control divzero: 602/23939 mismatches
98: control reverse: 6829/23939 mismatches
98: control zerosign: 551/23939 mismatches
98: control memquiet: 54/23939 mismatches
98: rejected 10 sequences outside the profile: fdivp st(0), st; fdivrp st(0), st; fdiv m32 on an empty stack; fdivr st(2), st with two values; fdivrp with one value; fidiv m32; fidivr m16; fsqrt; fprem; fadd m32
1/1 Test #98: cpu_aot_x87_div ..................   Passed
  • x87 regressions: ctest --preset dev --no-tests=error -R '^cpu_aot_x87_' gives 3 of 3 passed (x87_add, x87_mul, x87_div).
  • Full suite: ctest --preset dev --no-tests=error -j 8 --output-junit <repo>/out/test-results.xml gives 148 passed, 1 failed and 2 capability skips (bvh_wave64_warp, wave_shuffle_warp) out of 151. The run includes the GPU tests. The failure is gpu_alias_lookup_writes_hardware, the RDNA 4 array-atomics probe recorded in gpu_alias_lookup_writes_hardware: array atomics probe reads 256 per buffer on RX 9070 XT #369, which fails the same way on main. This change touches no GPU code.
  • Pure-Python checks: tests/form_coverage_check.py and tests/docs_links_check.py pass.
  • Fork CI: the Windows build-and-test, Linux Python and gfx1013 LLVM jobs passed at 6335f4a in the fork's Windows checks run 44976826150. All three x87 tests ran and passed there (tests 71 to 73 of 123).

Coverage. python tools/form_coverage.py out/test-results.xml gives x64 65 of 948 forms, and 61 without cpu_aot_x87_div. The added forms are fdiv, fdivp, fdivr and fdivrp.

Not measured:

  • Intel CPUs. All measurements are on one AMD Zen 4 part, and the fork CI runner's vendor is not recorded.
  • st(3) to st(7) operands. Only st(0), st(1) and st(2) are exercised.
  • The status word (ZE, IE, PE and the rest), as in cpu: lower an opt-in x87 leaf profile with FADDP #422.

Overlap

I trial-merged this branch with all 49 open cpu: PRs. Every merge that is clean against main is also clean against this branch. The changes stay inside the x87 files, one CPU_FIXTURES line after "x87_mul", a CMake block after the cpu_aot_x87_mul test, and the x87 section of docs/experiments.md. No open PR touches x87.

Evidence

The fixture is synthetic and original to this repository (MIT). docs/experiments.md documents the division forms, the special cases, the decode check, the controls and the reproduction command.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted (Claude Code).

The opt-in x87 profile of lower() now accepts FDIV and FDIVR with m32
and m64 operands, ST(0), ST(i) and ST(i), ST(0), and FDIVP and FDIVRP
ST(i), ST(0). x87_div rounds a 64-bit significand quotient with a sticky
bit for the remainder to 53 bits in the extended exponent range; zero by
zero and infinity by infinity give the indefinite, a finite value by zero
gives a signed infinity. Popping forms with ST(0) as the destination
stay rejected.

cpu_aot_x87_div compares 17 exports over 23939 cases through the x87
harness, with the live-value check, a decode check of every x87 form and
seven mutated controls. cpu_aot_x87_mul's rejection of fdiv, now in the
profile, becomes a rejection of fsub.

Refs R0Y00#406

AI-assisted (Claude Code).
@grahamster2

Copy link
Copy Markdown
Contributor

Independent check on Windows: differential fuzzing of this PR merged locally onto main 73ed4b7, run with Windows 11 build 26200, MSVC 19.50 and ML64. It generated 1,250 random x87-profile functions using every division form this PR adds, mixed with FLD/FSTP (m32/m64/m80), FADDP and FMUL, at stack depths up to 8. Each ran natively and translated on about 300 inputs, through tests/cpu/x87_harness.asm. Inputs included ±0, binary and extended denormals, pseudo-denormals and unnormals, ±Inf, signaling and quiet NaNs with payloads, range-boundary values and rounding ties. The 256-byte output block, guard bytes, control word and tags were compared bit-exactly. Result: 0 mismatches. The same harness detects five deliberate faults injected into x87_soft.h: precision, NaN ordering, sign of zero, denormal rounding position, and the denormal-to-normal carry.

@R0Y00
R0Y00 merged commit fb4fb85 into R0Y00:main Nov 22, 2026
3 checks passed
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.

3 participants