cpu: lower FSIN, FCOS, FSINCOS, FPTAN, and FPATAN instructions in x87_lower #1656

Merged
Ammmae0x merged 7 commits from upstream-pr/1663 into main 2026-11-25 18:39:00 +00:00
Ammmae0x commented 2026-11-24 16:41:24 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T16:41:24Z.


Change

Lower FSIN, FCOS, FSINCOS, FPTAN, and FPATAN in the opt-in x87 leaf-function profile (tools/x87_lower.py, tools/x87_soft.h), and add the source-owned differential fixture cpu_aot_x87_trig (tests/cpu/x87_trig.asm, tests/cpu/x87_trig.def, tests/cpu/x87_trig.py, CMakeLists.txt, docs/experiments.md).

  • FSIN, FCOS, FSINCOS, and FPTAN compute 64-bit extended-precision results in pure integer arithmetic (256-bit Payne-Hanek range reduction modulo pi/2 and 128-bit Taylor series) when |ST(0)| < 2^63, setting status-word C2 = 0 and leaving C0/C1/C3 undefined.
  • For finite |ST(0)| >= 2^63, FSIN and FCOS set C2 = 1 and leave ST(0) unchanged; for FSINCOS and FPTAN, finite |ST(0)| >= 2^63 is outside the fixed static-depth profile because hardware sets C2 = 1 without pushing while static-depth lowering advances the stack depth by 1, so the emitted code gates M.FSINCOS and M.FPTAN with if (c2) x87_trap(); (__ud2() on MSVC, __builtin_trap() on GCC/Clang) before pushing the extra slot, ending the translated function with an illegal-instruction trap (STATUS_ILLEGAL_INSTRUCTION / 0xC000001D) instead of continuing with a divergent stack (ptan_c2_reset also pins that FPTAN clears a prior C2 = 1 from FTST).
  • FPATAN computes atan2(ST(1), ST(0)) into ST(1) and pops ST(0), leaving C0-C3 undefined.
  • TRIG_C2 status tracking in tools/x87_lower.py makes only C2 known after FSIN/FCOS/FSINCOS/FPTAN so FNSTSW AX + SAHF (JP) or TEST AH, 04h is accepted while readers of unknown C0/C1/C3 (or C2 after FPATAN) are rejected.
  • Updated check_rejections() in the seven earlier tests/cpu/x87_*.py suites (x87_add.py, x87_compare.py, x87_iarith.py, x87_moves.py, x87_sqrt.py, x87_status.py, x87_sub.py) to use still-unsupported f2xm1 / fyl2x in place of the newly supported trig instructions.

Verification

Built cpu_x87_harness, cpu_x87_trig, cpu_x87_trig_lifted, and all 8 mutated control DLLs in both dev (Debug) and release (/O2) with MSVC via & .\tools\dev.ps1:

  • ctest --preset dev --no-tests=error -R "^cpu_aot_x87_trig$" -V -> 1/1 passed (99184 cases over 18 exports matched native and translated DLLs; lifted out-of-range sincos_m80/ptan_m80/ptan_c2_reset checks assert STATUS_ILLEGAL_INSTRUCTION (0xC000001D) with untouched output buffers; 8 mutated controls all mismatched: noc2, c262 with SEH accounting, sincosswap, ptanno1, patanswap, sinnegzero, snan, pc53; 15 rejected sequences).
  • ctest --preset release --no-tests=error -R "^cpu_aot_x87_trig$" --output-on-failure -> 1/1 passed.
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> form coverage checks passed
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> Checked 16 Markdown files and 348 links.
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> Checked 1270 tracked source, script and build/CI files: no bidirectional controls.

Evidence and follow-up

  • Source-owned MASM fixture tests/cpu/x87_trig.asm generated by tests/cpu/x87_trig.py asm with ; coverage: source cpu_aot_x87_trig on line 1; independent 150-digit Decimal reference model in tests/cpu/x87_trig.py.
  • Documented in docs/experiments.md under x87 in the shared lowering.

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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6586754392 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T16:41:24Z. --- ## Change Lower `FSIN`, `FCOS`, `FSINCOS`, `FPTAN`, and `FPATAN` in the opt-in x87 leaf-function profile (`tools/x87_lower.py`, `tools/x87_soft.h`), and add the source-owned differential fixture `cpu_aot_x87_trig` (`tests/cpu/x87_trig.asm`, `tests/cpu/x87_trig.def`, `tests/cpu/x87_trig.py`, `CMakeLists.txt`, `docs/experiments.md`). - `FSIN`, `FCOS`, `FSINCOS`, and `FPTAN` compute 64-bit extended-precision results in pure integer arithmetic (256-bit Payne-Hanek range reduction modulo `pi/2` and 128-bit Taylor series) when `|ST(0)| < 2^63`, setting status-word `C2 = 0` and leaving `C0`/`C1`/`C3` undefined. - For finite `|ST(0)| >= 2^63`, `FSIN` and `FCOS` set `C2 = 1` and leave `ST(0)` unchanged; for `FSINCOS` and `FPTAN`, finite `|ST(0)| >= 2^63` is outside the fixed static-depth profile because hardware sets `C2 = 1` without pushing while static-depth lowering advances the stack depth by 1, so the emitted code gates `M.FSINCOS` and `M.FPTAN` with `if (c2) x87_trap();` (`__ud2()` on MSVC, `__builtin_trap()` on GCC/Clang) before pushing the extra slot, ending the translated function with an illegal-instruction trap (`STATUS_ILLEGAL_INSTRUCTION` / `0xC000001D`) instead of continuing with a divergent stack (`ptan_c2_reset` also pins that `FPTAN` clears a prior `C2 = 1` from `FTST`). - `FPATAN` computes `atan2(ST(1), ST(0))` into `ST(1)` and pops `ST(0)`, leaving `C0`-`C3` undefined. - `TRIG_C2` status tracking in `tools/x87_lower.py` makes only `C2` known after `FSIN`/`FCOS`/`FSINCOS`/`FPTAN` so `FNSTSW AX` + `SAHF` (`JP`) or `TEST AH, 04h` is accepted while readers of unknown `C0`/`C1`/`C3` (or `C2` after `FPATAN`) are rejected. - Updated `check_rejections()` in the seven earlier `tests/cpu/x87_*.py` suites (`x87_add.py`, `x87_compare.py`, `x87_iarith.py`, `x87_moves.py`, `x87_sqrt.py`, `x87_status.py`, `x87_sub.py`) to use still-unsupported `f2xm1` / `fyl2x` in place of the newly supported trig instructions. ## Verification Built `cpu_x87_harness`, `cpu_x87_trig`, `cpu_x87_trig_lifted`, and all 8 mutated control DLLs in both `dev` (Debug) and `release` (`/O2`) with MSVC via `& .\tools\dev.ps1`: - `ctest --preset dev --no-tests=error -R "^cpu_aot_x87_trig$" -V` -> 1/1 passed (99184 cases over 18 exports matched native and translated DLLs; `lifted` out-of-range `sincos_m80`/`ptan_m80`/`ptan_c2_reset` checks assert `STATUS_ILLEGAL_INSTRUCTION` (`0xC000001D`) with untouched output buffers; 8 mutated controls all mismatched: `noc2`, `c262` with SEH accounting, `sincosswap`, `ptanno1`, `patanswap`, `sinnegzero`, `snan`, `pc53`; 15 rejected sequences). - `ctest --preset release --no-tests=error -R "^cpu_aot_x87_trig$" --output-on-failure` -> 1/1 passed. - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` - `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` -> `Checked 16 Markdown files and 348 links.` - `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` -> `Checked 1270 tracked source, script and build/CI files: no bidirectional controls.` ## Evidence and follow-up - Source-owned MASM fixture `tests/cpu/x87_trig.asm` generated by `tests/cpu/x87_trig.py asm` with `; coverage: source cpu_aot_x87_trig` on line 1; independent 150-digit `Decimal` reference model in `tests/cpu/x87_trig.py`. - Documented in `docs/experiments.md` under `x87 in the shared lowering`. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-24 17:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T16:57:46Z.

Review: COMMENTED


Review of head 2853202d241fae7437b64d2914c380c1c117515e (comment only, no approval or change request).

Verified

All of this ran on Linux x86-64. The x87 instructions were executed natively on the host CPU. The translated semantics were compiled with g++ using a small shim for the MSVC intrinsics (_umul128, _udiv128, _addcarry_u64, _subborrow_u64, _BitScanReverse64) around the unmodified tools/x87_soft.h.

  • check_rejections() of tests/cpu/x87_{add,compare,iarith,moves,sqrt,status,sub,trig}.py plus x87_{div,mul,fprem,bcd}.py all pass on the PR head (needed pefile and iced-x86). The seven older tests are only touched in their rejection tables: fsin, fsincos, fptan, fpatan, fcos used to be the "unsupported" example, and are now f2xm1 or fyl2x. That is coherent and not noise.
  • The PE-based paths (emit, check) cannot run here, so I rebuilt that path without a PE. I lowered five exports (sin_m80, cos_m80, sincos_m80, ptan_m80, patan_m80) with the real lower(..., x87=True) from hand-encoded bytes, compiled them, and ran all 32,876 of the PR's own cases for those five names. Every case passed the PR's own reference() and lifted_matches(). The same cases through the host's native instructions all pass the PR's own native_matches(). The full case list is 98,633 cases over 17 exports, as the doc says.
  • Differential fuzz against native fsin/fcos/fsincos/fptan/fpatan (about 80,000 random trials with random depth 0 to 7 and control words 0x007f, 0x027f, 0x037f, 0x047f, 0x087f, 0x0a7f, 0x0c7f). I compared fxsave state, the C2 bit and depth. NaN (quiet and signaling), +-Inf, unnormals, pseudo-infinity, pseudo-NaN, +-0, denormals and pseudo-denormals, and all fpatan sign and quadrant combinations matched bit for bit. The sole exceptions are the out-of-range and tiny-directed-rounding cases below.
  • Precision control: native results are identical for PC = 24, 53 and 64 bits. The model's "PC is ignored, RC is followed" is right. Native vs model on 17,792 moderate-argument cases over 6 control words: at most 1 ULP, 97% bit-identical.
  • Accuracy of the model against a 150-digit Decimal reference (same reference as the test): 0 ULP error on about 30,000 evaluations (in-range finite, random exponents -60..62, arguments near k*pi/2 for k up to 2^60, fpatan ratios near the reduction thresholds). C2 matched native on every in-range case.

Mutation checks

Each mutation was applied in the review worktree, the five-export lowered build was re-run, then reverted with git restore. A clean build passes.

# Mutation Result
M1 fptan pushes 0.0 instead of 1.0 detected (520 mismatches)
M2 fsincos sin/cos slots swapped in x87_lower.py detected (517)
M3 C2 threshold u.e >= 63 -> 64 in the sin/cos core detected (sin_m80 2, cos_m80 2)
M4 fpatan stack effect does not pop detected (translation error, "stack not empty at return")
M5 fsin(-0) returns +0 detected
M6 fptan never sets C2 (lowering) not detected
M7 fpatan operands swapped in lowering detected (28,508)
M8 fsincos out-of-range second value indefinite -> x not detected
M9 fptan out-of-range pushed value indefinite -> 1.0 not detected
M10 fpatan(+-0, x<0) loses pi detected (139)
M11 SNaN not quieted detected
M12 rounding to 53 bits detected
M13 reduction quadrant k&3 -> (k+1)&3 detected
M14 fptan without push in _effect detected (translation error)

M6, M8 and M9 survive because of the test gap in finding 2.

Findings

P2: fsincos/fptan with |ST(0)| >= 2^63 push a value, but hardware does not. tools/x87_soft.h:775 and :828, tools/x87_lower.py:320.
The SDM says that for out-of-range operands C2 is set and the stack is left unchanged. The translator pushes unconditionally (depth + 1) and writes indefinite into the new top. Repro: fld [prior=1.5]; fld [x=2^64]; fsincos; fstp; fstp; fstp (same for fptan).

  • Native: ST0 = 2^64 (unchanged), ST1 = 1.5, ST2 = empty (the third pop stores indefinite).
  • Translated: ST0 = indefinite, ST1 = 2^64, ST2 = 1.5.

With fptan; fstp st(0) the translated code keeps x as the "tan" while the hardware drops x. The doc paragraph says ST(0) is left unchanged for all four instructions, which is not what the lowered code produces for fsincos/fptan. The TOP reported by fnstsw also differs in that path. The static-depth design cannot represent a conditional push, so either reject fsincos/fptan unless the out-of-range case is provably excluded (or a C2 branch rebalances the depth), or state in the doc that the out-of-range case is outside the profile.

P2: that out-of-range case is not exercised for fsincos/fptan (tests/cpu/x87_trig.py:43-52, :451). sincos_m80, sincos_sum, ptan_m80, ptan_to_m64 use m80_in, which filters out |x| >= 2^63; sincos_depth7 and ptan_depth7 use m64x7_in. Only fsin/fcos get out-of-range inputs, and only sin_c2_readers reads C2 (the sahf/test ah,4 form). Survivors M6, M8 and M9 show this. The single-op out-of-range case happens to agree with hardware in sincos_m80 only because a pop on an empty stack stores indefinite. The reference() model also encodes the push (trig_op returns [v, INDEFINITE]), so fixing the first finding would need the reference updated too.

P2 (doc and tolerance): "finite in-range m80 results match within 4 ULPs natively" (docs/experiments.md, the cpu_aot_x87_trig paragraph) is not true in general, and native_matches is weaker than it reads.

  • For |x| > 8, it only requires the native result to be zero or finite (large branch).
  • For |x| <= 8 it excludes results below 2^-50 (and fptan above 2^50).
  • On this host, vs the exact value: fsin/fcos/fsincos/fptan for 8 < |x| < 2^63 are off by more than 4 ULP in 85% to 96% of random samples (worst about 10^19 ULP, near multiples of pi/2, from the hardware's 66-bit pi).
  • For |x| <= 8 and |result| >= 2^-50, hardware exceeds 4 ULP in 3 of ~1040 fcos, 8 of ~2000 fsincos, and 40 of ~1050 fptan samples (12 ULP for fcos, a larger deviation for fptan near the poles).

The translated model is the more accurate side (0 ULP vs the exact reference), so the translation is not bit-compatible with hardware near multiples of pi/2. The tolerance as written is acceptable for the fixed case set, but the claim should say it holds for the listed cases and is hardware-dependent. The check also passes on this host, but a different CPU's microcode could shift borderline cases.

nit: |x| < 2^-64 with a directed rounding control (checked entry only). tools/x87_soft.h:781, :829. x87_round_u128(..., force_inexact=false) returns x itself, so fsin(1.5*2^-70) under RC = toward zero returns x, while native returns x - 1 ulp (bfff...ff:3fb9); a denormal 0x...01 gives native 0 and model 0x...01. Within the 1 ULP tolerance, and only reachable through fldcw in the checked entry, so I would only document it.

No issue found with: tag/TOP handling at fixed depth for the in-range cases, fpatan pop and ST(1) write, fpatan argument order, signed zeros, SNaN/QNaN quieting, fsin(inf) giving the indefinite QNaN, the 2^63 boundary (2^63 - 1ulp is in range, 2^63 is out), or the added reject cases (underflow and depth-8 push, readers of C0/C1/C3).

Not verified

  • Windows, MSVC and MASM were not run. The .asm, .def, CMake registration (cpu_aot_x87_trig, the nine DLL variants, preset behavior) and the PE-based emit/check entry points were not executed; x87_trig.asm was only compared to SEQUENCES by reading.
  • The eight built-in control variants (noc2, c262, sincosswap, ptanno1, patanswap, sinnegzero, snan, pc53) were not run; my own mutations cover the same behaviors, and the mismatch counts in the doc (318, 22, 627, ...) were not reproduced.
  • Only one host CPU was available; the doc's Ryzen 7 numbers were not reproduced. Hardware results near multiples of pi/2 can differ by vendor.
  • The sincos_depth7, ptan_depth7, patan_depth8, *_to_m64 and *_to_m32 exports were not run through the lowered path (the semantic checks above cover the same helpers).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6043712270 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#pullrequestreview-6043712270). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T16:57:46Z. **Review:** COMMENTED --- Review of head `2853202d241fae7437b64d2914c380c1c117515e` (comment only, no approval or change request). ## Verified All of this ran on Linux x86-64. The x87 instructions were executed natively on the host CPU. The translated semantics were compiled with g++ using a small shim for the MSVC intrinsics (`_umul128`, `_udiv128`, `_addcarry_u64`, `_subborrow_u64`, `_BitScanReverse64`) around the unmodified `tools/x87_soft.h`. - `check_rejections()` of `tests/cpu/x87_{add,compare,iarith,moves,sqrt,status,sub,trig}.py` plus `x87_{div,mul,fprem,bcd}.py` all pass on the PR head (needed `pefile` and `iced-x86`). The seven older tests are only touched in their rejection tables: `fsin`, `fsincos`, `fptan`, `fpatan`, `fcos` used to be the "unsupported" example, and are now `f2xm1` or `fyl2x`. That is coherent and not noise. - The PE-based paths (`emit`, `check`) cannot run here, so I rebuilt that path without a PE. I lowered five exports (`sin_m80`, `cos_m80`, `sincos_m80`, `ptan_m80`, `patan_m80`) with the real `lower(..., x87=True)` from hand-encoded bytes, compiled them, and ran all 32,876 of the PR's own cases for those five names. Every case passed the PR's own `reference()` and `lifted_matches()`. The same cases through the host's native instructions all pass the PR's own `native_matches()`. The full case list is 98,633 cases over 17 exports, as the doc says. - Differential fuzz against native `fsin`/`fcos`/`fsincos`/`fptan`/`fpatan` (about 80,000 random trials with random depth 0 to 7 and control words 0x007f, 0x027f, 0x037f, 0x047f, 0x087f, 0x0a7f, 0x0c7f). I compared `fxsave` state, the C2 bit and depth. NaN (quiet and signaling), +-Inf, unnormals, pseudo-infinity, pseudo-NaN, +-0, denormals and pseudo-denormals, and all `fpatan` sign and quadrant combinations matched bit for bit. The sole exceptions are the out-of-range and tiny-directed-rounding cases below. - Precision control: native results are identical for PC = 24, 53 and 64 bits. The model's "PC is ignored, RC is followed" is right. Native vs model on 17,792 moderate-argument cases over 6 control words: at most 1 ULP, 97% bit-identical. - Accuracy of the model against a 150-digit `Decimal` reference (same reference as the test): 0 ULP error on about 30,000 evaluations (in-range finite, random exponents -60..62, arguments near k*pi/2 for k up to 2^60, `fpatan` ratios near the reduction thresholds). C2 matched native on every in-range case. ## Mutation checks Each mutation was applied in the review worktree, the five-export lowered build was re-run, then reverted with `git restore`. A clean build passes. | # | Mutation | Result | |---|---|---| | M1 | `fptan` pushes 0.0 instead of 1.0 | detected (520 mismatches) | | M2 | `fsincos` sin/cos slots swapped in `x87_lower.py` | detected (517) | | M3 | C2 threshold `u.e >= 63` -> `64` in the sin/cos core | detected (`sin_m80` 2, `cos_m80` 2) | | M4 | `fpatan` stack effect does not pop | detected (translation error, "stack not empty at return") | | M5 | `fsin(-0)` returns +0 | detected | | M6 | `fptan` never sets C2 (lowering) | **not detected** | | M7 | `fpatan` operands swapped in lowering | detected (28,508) | | M8 | `fsincos` out-of-range second value `indefinite` -> `x` | **not detected** | | M9 | `fptan` out-of-range pushed value `indefinite` -> `1.0` | **not detected** | | M10 | `fpatan(+-0, x<0)` loses pi | detected (139) | | M11 | SNaN not quieted | detected | | M12 | rounding to 53 bits | detected | | M13 | reduction quadrant `k&3` -> `(k+1)&3` | detected | | M14 | `fptan` without push in `_effect` | detected (translation error) | M6, M8 and M9 survive because of the test gap in finding 2. ## Findings **P2: `fsincos`/`fptan` with `|ST(0)| >= 2^63` push a value, but hardware does not. `tools/x87_soft.h:775` and `:828`, `tools/x87_lower.py:320`.** The SDM says that for out-of-range operands C2 is set and the stack is left unchanged. The translator pushes unconditionally (depth + 1) and writes `indefinite` into the new top. Repro: `fld [prior=1.5]; fld [x=2^64]; fsincos; fstp; fstp; fstp` (same for `fptan`). - Native: ST0 = 2^64 (unchanged), ST1 = 1.5, ST2 = empty (the third pop stores `indefinite`). - Translated: ST0 = `indefinite`, ST1 = 2^64, ST2 = 1.5. With `fptan; fstp st(0)` the translated code keeps `x` as the "tan" while the hardware drops `x`. The doc paragraph says ST(0) is left unchanged for all four instructions, which is not what the lowered code produces for `fsincos`/`fptan`. The `TOP` reported by `fnstsw` also differs in that path. The static-depth design cannot represent a conditional push, so either reject `fsincos`/`fptan` unless the out-of-range case is provably excluded (or a C2 branch rebalances the depth), or state in the doc that the out-of-range case is outside the profile. **P2: that out-of-range case is not exercised for `fsincos`/`fptan` (`tests/cpu/x87_trig.py:43-52`, `:451`).** `sincos_m80`, `sincos_sum`, `ptan_m80`, `ptan_to_m64` use `m80_in`, which filters out `|x| >= 2^63`; `sincos_depth7` and `ptan_depth7` use `m64x7_in`. Only `fsin`/`fcos` get out-of-range inputs, and only `sin_c2_readers` reads C2 (the `sahf`/`test ah,4` form). Survivors M6, M8 and M9 show this. The single-op out-of-range case happens to agree with hardware in `sincos_m80` only because a pop on an empty stack stores `indefinite`. The `reference()` model also encodes the push (`trig_op` returns `[v, INDEFINITE]`), so fixing the first finding would need the reference updated too. **P2 (doc and tolerance): "finite in-range m80 results match within 4 ULPs natively" (`docs/experiments.md`, the `cpu_aot_x87_trig` paragraph) is not true in general, and `native_matches` is weaker than it reads.** - For `|x| > 8`, it only requires the native result to be zero or finite (`large` branch). - For `|x| <= 8` it excludes results below 2^-50 (and `fptan` above 2^50). - On this host, vs the exact value: `fsin`/`fcos`/`fsincos`/`fptan` for `8 < |x| < 2^63` are off by more than 4 ULP in 85% to 96% of random samples (worst about 10^19 ULP, near multiples of pi/2, from the hardware's 66-bit pi). - For `|x| <= 8` and `|result| >= 2^-50`, hardware exceeds 4 ULP in 3 of ~1040 `fcos`, 8 of ~2000 `fsincos`, and 40 of ~1050 `fptan` samples (12 ULP for `fcos`, a larger deviation for `fptan` near the poles). The translated model is the more accurate side (0 ULP vs the exact reference), so the translation is not bit-compatible with hardware near multiples of pi/2. The tolerance as written is acceptable for the fixed case set, but the claim should say it holds for the listed cases and is hardware-dependent. The check also passes on this host, but a different CPU's microcode could shift borderline cases. **nit: `|x| < 2^-64` with a directed rounding control (checked entry only). `tools/x87_soft.h:781`, `:829`.** `x87_round_u128(..., force_inexact=false)` returns `x` itself, so `fsin(1.5*2^-70)` under RC = toward zero returns `x`, while native returns `x - 1 ulp` (`bfff...ff:3fb9`); a denormal `0x...01` gives native `0` and model `0x...01`. Within the 1 ULP tolerance, and only reachable through `fldcw` in the checked entry, so I would only document it. No issue found with: tag/TOP handling at fixed depth for the in-range cases, `fpatan` pop and `ST(1)` write, `fpatan` argument order, signed zeros, SNaN/QNaN quieting, `fsin(inf)` giving the indefinite QNaN, the 2^63 boundary (`2^63 - 1ulp` is in range, `2^63` is out), or the added reject cases (underflow and depth-8 push, readers of C0/C1/C3). ## Not verified - Windows, MSVC and MASM were not run. The `.asm`, `.def`, CMake registration (`cpu_aot_x87_trig`, the nine DLL variants, preset behavior) and the PE-based `emit`/`check` entry points were not executed; `x87_trig.asm` was only compared to `SEQUENCES` by reading. - The eight built-in control variants (`noc2`, `c262`, `sincosswap`, `ptanno1`, `patanswap`, `sinnegzero`, `snan`, `pc53`) were not run; my own mutations cover the same behaviors, and the mismatch counts in the doc (318, 22, 627, ...) were not reproduced. - Only one host CPU was available; the doc's Ryzen 7 numbers were not reproduced. Hardware results near multiples of pi/2 can differ by vendor. - The `sincos_depth7`, `ptan_depth7`, `patan_depth8`, `*_to_m64` and `*_to_m32` exports were not run through the lowered path (the semantic checks above cover the same helpers). AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-24 21:00:45 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:23:54Z.

Review: COMMENTED


Re-review of head 5a784b5039fe8955b61f570cdef326db56943cba. My previous review was at 2853202d; the PR added one commit (5a784b50: docs, tools/x87_lower.py comment, tests/cpu/x87_trig.{py,asm,def}). Comment only, not an approval.

Verdict: 0 P1 / 1 P2 still open (documented, not fixed) / 1 P3. The test-gap and the tolerance-claim findings are addressed. The out-of-range fsincos/fptan behaviour is now written down but the translated code still diverges from hardware.

Linux limits. Linux x86-64, Python 3 with pefile/iced-x86 in a venv. The x87 instructions ran natively on the host CPU; the translated code was compiled with g++ plus a small shim for the MSVC intrinsics. I could not build the PE or run the MASM/MSVC path, ctest, or the eight built-in control variants. I rebuilt the lowering for sin_m80, cos_m80, sincos_m80, ptan_m80 and ptan_c2_reset from gas-assembled bytes (the PR's own asm_lines text, lower(..., x87=True)), not from the PR's PE flow. The mutation counts and case totals in the doc (99184 cases, 18 exports, 318/24/627/1150/84768/8/132/27168) are the author's and were not reproduced.

Previous findings

  • P2 (out-of-range fsincos/fptan push a value; hardware does not): documented, NOT fixed. tools/x87_lower.py and docs/experiments.md now say finite |ST(0)| >= 2^63 for those two is outside the static-depth profile. tools/x87_soft.h is unchanged. I re-ran it on this head with the lowered code against the native instruction (x = 2^63, -2^63, 2^64, 1e30):
    • sincos_m80 and ptan_m80: C2 = 1 on both sides, but native leaves x on the stack and the third pop reads empty, so the slots are [indefinite at +0, x at +16]. The lowered code writes [x at +0, indefinite at +16]. Same for ptan_c2_reset.
    • sin_m80 and cos_m80: identical to hardware (C2 = 1, x unchanged).
      The doc is accurate about this. But "outside the profile" is not enforced anywhere: the translator accepts any fptan/fsincos and silently produces a stack that differs from hardware whenever the runtime input is out of range. If that is acceptable for the title's code paths, say so in the PR description as a known limitation. A cheap hardening that keeps the static depth: have the lowered code stop (fault or trap) when |x| >= 2^63 for those two instructions instead of continuing with a different stack.
  • P2 (that case was untested): closed for the translated DLL. The check now runs sincos_m80 and ptan_m80 on the lifted DLL at 2^63, -2^63 and 2^64 and compares with reference(). Mutations of the kind I listed before (fptan never sets C2; fsincos out-of-range second value x instead of indefinite; fptan out-of-range pushed value 1.0 instead of indefinite) change exactly the bytes this new check compares, so they should now fail it; I did not re-run those three through the PR's own check, which needs the PE. Note the check pins the known-divergent behaviour: the native side deliberately still filters those inputs out.
  • P2 (4-ULP tolerance claim): fixed in the text. The doc now limits it to the listed fixture cases (|x| <= 8, magnitudes >= 2^-50, <= 2^50 for fptan), says native_matches only checks finiteness for |x| > 8 and tiny or near-pole results, and calls borderline cases hardware-dependent. On my host the lifted-vs-native comparison for |x| <= 8 had 2 inputs of 54 over 4 ULP (4.7231 and -4.7235, next to 3pi/2; 7 and 8 ULP), which matches the hardware-pi remark; C2 matched on all in-range inputs.
  • nit (tiny |x| < 2^-64 under directed rounding): documented in the same paragraph.

New in this commit

  • ptan_c2_reset (fldz; fldz; fdivp; ftst; pop; fld x; fptan) lowers (lower accepts fldz, fdivp, ftst). Native vs lifted over my 54 in-range inputs: C2 equal on all (fptan clears the C2 set by ftst). The reference gets fldz, fdivp and ftst handlers.
  • The five older tests only change their rejection examples; I ran check_rejections() for x87_{trig,add,compare,iarith,moves,sqrt,status,sub,div,mul,fprem,bcd} at this SHA: all print ok.

Other checks: git diff --check origin/main...HEAD, tests/docs_links_check.py (16 files, 348 links at this base), tests/bidi_controls_check.py (1270 files): clean. tests/form_coverage_check.py: passed. git merge-tree --write-tree origin/main HEAD: clean, but the branch is 363 commits behind main, so please rebase before relying on CI (P3).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046779190 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#pullrequestreview-6046779190). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:23:54Z. **Review:** COMMENTED --- Re-review of head `5a784b5039fe8955b61f570cdef326db56943cba`. My previous review was at `2853202d`; the PR added one commit (`5a784b50`: docs, `tools/x87_lower.py` comment, `tests/cpu/x87_trig.{py,asm,def}`). Comment only, not an approval. **Verdict: 0 P1 / 1 P2 still open (documented, not fixed) / 1 P3.** The test-gap and the tolerance-claim findings are addressed. The out-of-range `fsincos`/`fptan` behaviour is now written down but the translated code still diverges from hardware. **Linux limits.** Linux x86-64, Python 3 with pefile/iced-x86 in a venv. The x87 instructions ran natively on the host CPU; the translated code was compiled with g++ plus a small shim for the MSVC intrinsics. I could not build the PE or run the MASM/MSVC path, `ctest`, or the eight built-in control variants. I rebuilt the lowering for `sin_m80`, `cos_m80`, `sincos_m80`, `ptan_m80` and `ptan_c2_reset` from gas-assembled bytes (the PR's own `asm_lines` text, `lower(..., x87=True)`), not from the PR's PE flow. The mutation counts and case totals in the doc (99184 cases, 18 exports, 318/24/627/1150/84768/8/132/27168) are the author's and were not reproduced. **Previous findings** - **P2 (out-of-range `fsincos`/`fptan` push a value; hardware does not): documented, NOT fixed.** `tools/x87_lower.py` and `docs/experiments.md` now say finite `|ST(0)| >= 2^63` for those two is outside the static-depth profile. `tools/x87_soft.h` is unchanged. I re-ran it on this head with the lowered code against the native instruction (x = 2^63, -2^63, 2^64, 1e30): - `sincos_m80` and `ptan_m80`: C2 = 1 on both sides, but native leaves x on the stack and the third pop reads empty, so the slots are `[indefinite at +0, x at +16]`. The lowered code writes `[x at +0, indefinite at +16]`. Same for `ptan_c2_reset`. - `sin_m80` and `cos_m80`: identical to hardware (C2 = 1, x unchanged). The doc is accurate about this. But "outside the profile" is not enforced anywhere: the translator accepts any `fptan`/`fsincos` and silently produces a stack that differs from hardware whenever the runtime input is out of range. If that is acceptable for the title's code paths, say so in the PR description as a known limitation. A cheap hardening that keeps the static depth: have the lowered code stop (fault or trap) when `|x| >= 2^63` for those two instructions instead of continuing with a different stack. - **P2 (that case was untested): closed for the translated DLL.** The check now runs `sincos_m80` and `ptan_m80` on the lifted DLL at 2^63, -2^63 and 2^64 and compares with `reference()`. Mutations of the kind I listed before (fptan never sets C2; `fsincos` out-of-range second value `x` instead of indefinite; `fptan` out-of-range pushed value `1.0` instead of indefinite) change exactly the bytes this new check compares, so they should now fail it; I did not re-run those three through the PR's own `check`, which needs the PE. Note the check pins the known-divergent behaviour: the native side deliberately still filters those inputs out. - **P2 (4-ULP tolerance claim): fixed in the text.** The doc now limits it to the listed fixture cases (`|x| <= 8`, magnitudes `>= 2^-50`, `<= 2^50` for `fptan`), says `native_matches` only checks finiteness for `|x| > 8` and tiny or near-pole results, and calls borderline cases hardware-dependent. On my host the lifted-vs-native comparison for `|x| <= 8` had 2 inputs of 54 over 4 ULP (4.7231 and -4.7235, next to 3pi/2; 7 and 8 ULP), which matches the hardware-pi remark; C2 matched on all in-range inputs. - **nit (tiny `|x| < 2^-64` under directed rounding): documented** in the same paragraph. **New in this commit** - `ptan_c2_reset` (`fldz; fldz; fdivp; ftst; pop; fld x; fptan`) lowers (`lower` accepts `fldz`, `fdivp`, `ftst`). Native vs lifted over my 54 in-range inputs: C2 equal on all (`fptan` clears the C2 set by `ftst`). The reference gets `fldz`, `fdivp` and `ftst` handlers. - The five older tests only change their rejection examples; I ran `check_rejections()` for `x87_{trig,add,compare,iarith,moves,sqrt,status,sub,div,mul,fprem,bcd}` at this SHA: all print ok. **Other checks:** `git diff --check origin/main...HEAD`, `tests/docs_links_check.py` (16 files, 348 links at this base), `tests/bidi_controls_check.py` (1270 files): clean. `tests/form_coverage_check.py`: passed. `git merge-tree --write-tree origin/main HEAD`: clean, but the branch is 363 commits behind `main`, so please rebase before relying on CI (P3). AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-24 21:01:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T20:10:14Z.


Updated PR #1663 (commit 5a784b50) to address the P2 findings and nits:

  1. Out-of-range fsincos/fptan profile boundary: Explicitly documented in tools/x87_lower.py and docs/experiments.md that finite |ST(0)| >= 2^63 for fsincos and fptan is outside the fixed static-depth profile (hardware leaves the stack unchanged without pushing when C2 = 1, whereas static-depth lowering unconditionally advances the stack depth by 1 and pushes x87_indefinite).
  2. Test coverage (ptan_c2_reset + lifted out-of-range checks): Added ptan_c2_reset in tests/cpu/x87_trig.py (regenerating tests/cpu/x87_trig.asm and tests/cpu/x87_trig.def) so ftst sets C2 = 1 before an in-range fptan resets C2 = 0, and added lifted-DLL checks for sincos_m80 and ptan_m80 at |x| >= 2^63 (C2 = 1 and x87_indefinite in the pushed slot).
  3. Native ULP claim and directed-rounding note: Qualified the 4-ULP claim in docs/experiments.md to the listed fixture cases (|x| <= 8, magnitudes >= 2^-50, and <= 2^50 for fptan), noting native_matches checks |x| > 8 and tiny/near-pole outputs only for finiteness and is host-dependent, and documented the |x| < 2^-64 directed-rounding nit.

Verified cpu_aot_x87_trig in both dev (99184 cases over 18 exports, 8 mutated controls, 15 rejected sequences) and release, plus form_coverage_check.py, docs_links_check.py, and bidi_controls_check.py. AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7143139248 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#issuecomment-7143139248). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T20:10:14Z. --- Updated PR #1663 (commit `5a784b50`) to address the P2 findings and nits: 1. **Out-of-range `fsincos`/`fptan` profile boundary**: Explicitly documented in `tools/x87_lower.py` and `docs/experiments.md` that finite `|ST(0)| >= 2^63` for `fsincos` and `fptan` is outside the fixed static-depth profile (hardware leaves the stack unchanged without pushing when `C2 = 1`, whereas static-depth lowering unconditionally advances the stack depth by 1 and pushes `x87_indefinite`). 2. **Test coverage (`ptan_c2_reset` + lifted out-of-range checks)**: Added `ptan_c2_reset` in `tests/cpu/x87_trig.py` (regenerating `tests/cpu/x87_trig.asm` and `tests/cpu/x87_trig.def`) so `ftst` sets `C2 = 1` before an in-range `fptan` resets `C2 = 0`, and added `lifted`-DLL checks for `sincos_m80` and `ptan_m80` at `|x| >= 2^63` (`C2 = 1` and `x87_indefinite` in the pushed slot). 3. **Native ULP claim and directed-rounding note**: Qualified the 4-ULP claim in `docs/experiments.md` to the listed fixture cases (`|x| <= 8`, magnitudes `>= 2^-50`, and `<= 2^50` for `fptan`), noting `native_matches` checks `|x| > 8` and tiny/near-pole outputs only for finiteness and is host-dependent, and documented the `|x| < 2^-64` directed-rounding nit. Verified `cpu_aot_x87_trig` in both `dev` (99184 cases over 18 exports, 8 mutated controls, 15 rejected sequences) and `release`, plus `form_coverage_check.py`, `docs_links_check.py`, and `bidi_controls_check.py`. AI-assisted.
Ammmae0x commented 2026-11-24 21:01:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T20:42:06Z.


Updated PR #1663 (merged upstream/main and pushed commit 28f1c4c5) to resolve the remaining P2 finding and P3 rebase note:

  1. Trap on out-of-range FSINCOS/FPTAN: Added x87_trap() in tools/x87_soft.h (__ud2() on MSVC, __builtin_trap() on GCC/Clang) and gated M.FSINCOS and M.FPTAN in tools/x87_lower.py with if (c2) x87_trap(); before pushing the extra slot so runtime |ST(0)| >= 2^63 traps cleanly instead of continuing with a divergent stack.
  2. Verification & docs: Updated tests/cpu/x87_trig.py to assert STATUS_ILLEGAL_INSTRUCTION (0xC000001D) with untouched output buffers for sincos_m80, ptan_m80, and ptan_c2_reset at |x| >= 2^63 (plus SEH mismatch accounting for c262), and updated docs/experiments.md.

Verified cpu_aot_x87_trig in both dev (99184 cases over 18 exports, out-of-range trap checks, 8 mutated controls, 15 rejected sequences) and release, plus tests/form_coverage_check.py, tests/docs_links_check.py (18 files, 376 links), and tests/bidi_controls_check.py (1338 files). AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7143940024 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#issuecomment-7143940024). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T20:42:06Z. --- Updated PR #1663 (merged `upstream/main` and pushed commit `28f1c4c5`) to resolve the remaining P2 finding and P3 rebase note: 1. **Trap on out-of-range `FSINCOS`/`FPTAN`**: Added `x87_trap()` in `tools/x87_soft.h` (`__ud2()` on MSVC, `__builtin_trap()` on GCC/Clang) and gated `M.FSINCOS` and `M.FPTAN` in `tools/x87_lower.py` with `if (c2) x87_trap();` before pushing the extra slot so runtime `|ST(0)| >= 2^63` traps cleanly instead of continuing with a divergent stack. 2. **Verification & docs**: Updated `tests/cpu/x87_trig.py` to assert `STATUS_ILLEGAL_INSTRUCTION` (`0xC000001D`) with untouched output buffers for `sincos_m80`, `ptan_m80`, and `ptan_c2_reset` at `|x| >= 2^63` (plus SEH mismatch accounting for `c262`), and updated `docs/experiments.md`. Verified `cpu_aot_x87_trig` in both `dev` (99184 cases over 18 exports, out-of-range trap checks, 8 mutated controls, 15 rejected sequences) and `release`, plus `tests/form_coverage_check.py`, `tests/docs_links_check.py` (18 files, 376 links), and `tests/bidi_controls_check.py` (1338 files). AI-assisted.
tedin7 commented 2026-11-24 21:27:19 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T21:24:51Z.

Review: COMMENTED


Re-review of head 28f1c4c5e88f328814cb1e0e8256bb1c71ac7e84 (my previous review was at 5a784b50; the PR added the commit "trap on out-of-range FSINCOS/FPTAN in x87_lower" and merged main). Comment only, not an approval.

Verdict: 0 P1 / 0 P2. The open P2 from the last round (out-of-range fsincos/fptan continued with a stack that differs from hardware) is fixed.

What changed and what I checked

  • tools/x87_lower.py:782-785: the lowered fsincos and fptan now run if (c2) x87_trap(); right after the helper call, before anything is stored. tools/x87_soft.h:755-762 adds x87_trap() (__ud2() on MSVC, __builtin_trap() otherwise).
  • c2 is only set in one place for these two helpers: finite |ST(0)| >= 2^63 (x87_soft.h:785 and :837). Infinity, NaN, unsupported encodings and zero keep c2 = false, so the trap cannot fire on inputs the profile accepts. FSIN/FCOS are unchanged (they match hardware: C2 = 1, ST(0) unchanged).
  • tests/cpu/x87_trig.py:661-683: the out-of-range check now calls sincos_m80, ptan_m80 and ptan_c2_reset on the lifted DLL at 2^63, -2^63, 2^64 and 2^100 and requires an SEH 0xC000001D and an untouched output buffer. The control loop (:695-708) counts a trap as a mismatch for the mutated DLLs.
  • Docs and the x87_lower.py header comment now say the translated code traps for those inputs.

Commands run locally at 28f1c4c5

  • git diff --check origin/main...HEAD: exit 0
  • tests/docs_links_check.py: 18 Markdown files, 376 links OK
  • tests/bidi_controls_check.py: 1338 files OK
  • tests/form_coverage_check.py: passed
  • check_rejections() from tests/cpu/x87_trig.py: "rejected 15 sequences outside the profile" OK
  • git merge-tree --write-tree origin/main HEAD: clean. The branch is 85 commits behind main.
  • x2w_pipe.py win2025 1663 --targets cpu_x87_harness,cpu_x87_trig,cpu_x87_trig_{lifted,noc2,c262,sincosswap,ptanno1,patanswap,sinnegzero,snan,pc53} --regex '^cpu_aot_x87_trig$': Windows Server 2025 Debug (dev) 1/1 passed (27.0 s) and Release /O2 1/1 passed (23.1 s). This runs the real PE path, so the SEH check in x87_trig.py was exercised.

Mutations (Linux, local, reverted)

I could not build the PR's PE on Linux, so I took the exact statements lower() emits for fsincos and fptan (printed from the PR's own lowering), compiled them with tools/x87_soft.h using g++ and a small shim for the MSVC intrinsics, and ran each input in a forked child (6 out-of-range inputs and 3 in-range inputs per instruction).

  • As in the PR: in-range 2^0, 2^10, 2^62 exit 0; out-of-range +/-2^63, +/-2^64, +/-2^100 all die with SIGILL for both instructions. bad=0.
  • Mutation 1, delete if (c2) x87_trap(); from both statements: out-of-range runs exit 0, bad=12. Killed.
  • Mutation 2, change the range test in x87_soft.h from u.e >= 63 to u.e >= 62: in-range 2^62 for fsincos is trapped, bad=1. Killed.

Remaining notes (not blocking)

  • P3 / behaviour: a trap ends the whole translated function (illegal-instruction exception) instead of returning a stack that differs from hardware. That is the safer choice and it is documented; just make sure the PR description says it explicitly, since the earlier text still describes the old "C2 = 1 and push indefinite" behaviour.
  • P3 / warning: the generated control fixtures (cpu_x87_trig_snan.cpp(541), cpu_x87_trig_pc53.cpp(541)) compile with MSVC warning C4805 (| between uint64_t and bool). It does not change a result; I did not check whether it predates this PR.
  • Not reproduced here: the author's mismatch counts and the Ryzen-specific numbers in the docs; the tolerance statements are unchanged from the last round.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6047663256 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#pullrequestreview-6047663256). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T21:24:51Z. **Review:** COMMENTED --- Re-review of head `28f1c4c5e88f328814cb1e0e8256bb1c71ac7e84` (my previous review was at `5a784b50`; the PR added the commit "trap on out-of-range FSINCOS/FPTAN in x87_lower" and merged `main`). Comment only, not an approval. **Verdict: 0 P1 / 0 P2. The open P2 from the last round (out-of-range `fsincos`/`fptan` continued with a stack that differs from hardware) is fixed.** ## What changed and what I checked - `tools/x87_lower.py:782-785`: the lowered `fsincos` and `fptan` now run `if (c2) x87_trap();` right after the helper call, before anything is stored. `tools/x87_soft.h:755-762` adds `x87_trap()` (`__ud2()` on MSVC, `__builtin_trap()` otherwise). - `c2` is only set in one place for these two helpers: finite `|ST(0)| >= 2^63` (`x87_soft.h:785` and `:837`). Infinity, NaN, unsupported encodings and zero keep `c2 = false`, so the trap cannot fire on inputs the profile accepts. `FSIN`/`FCOS` are unchanged (they match hardware: C2 = 1, ST(0) unchanged). - `tests/cpu/x87_trig.py:661-683`: the out-of-range check now calls `sincos_m80`, `ptan_m80` and `ptan_c2_reset` on the lifted DLL at 2^63, -2^63, 2^64 and 2^100 and requires an SEH `0xC000001D` and an untouched output buffer. The control loop (`:695-708`) counts a trap as a mismatch for the mutated DLLs. - Docs and the `x87_lower.py` header comment now say the translated code traps for those inputs. ## Commands run locally at 28f1c4c5 - `git diff --check origin/main...HEAD`: exit 0 - `tests/docs_links_check.py`: 18 Markdown files, 376 links OK - `tests/bidi_controls_check.py`: 1338 files OK - `tests/form_coverage_check.py`: passed - `check_rejections()` from `tests/cpu/x87_trig.py`: "rejected 15 sequences outside the profile" OK - `git merge-tree --write-tree origin/main HEAD`: clean. The branch is 85 commits behind `main`. - `x2w_pipe.py win2025 1663 --targets cpu_x87_harness,cpu_x87_trig,cpu_x87_trig_{lifted,noc2,c262,sincosswap,ptanno1,patanswap,sinnegzero,snan,pc53} --regex '^cpu_aot_x87_trig$'`: Windows Server 2025 Debug (`dev`) 1/1 passed (27.0 s) and Release `/O2` 1/1 passed (23.1 s). This runs the real PE path, so the SEH check in `x87_trig.py` was exercised. ## Mutations (Linux, local, reverted) I could not build the PR's PE on Linux, so I took the exact statements `lower()` emits for `fsincos` and `fptan` (printed from the PR's own lowering), compiled them with `tools/x87_soft.h` using g++ and a small shim for the MSVC intrinsics, and ran each input in a forked child (6 out-of-range inputs and 3 in-range inputs per instruction). - As in the PR: in-range 2^0, 2^10, 2^62 exit 0; out-of-range +/-2^63, +/-2^64, +/-2^100 all die with SIGILL for both instructions. `bad=0`. - Mutation 1, delete `if (c2) x87_trap();` from both statements: out-of-range runs exit 0, `bad=12`. Killed. - Mutation 2, change the range test in `x87_soft.h` from `u.e >= 63` to `u.e >= 62`: in-range 2^62 for `fsincos` is trapped, `bad=1`. Killed. ## Remaining notes (not blocking) - **P3 / behaviour:** a trap ends the whole translated function (illegal-instruction exception) instead of returning a stack that differs from hardware. That is the safer choice and it is documented; just make sure the PR description says it explicitly, since the earlier text still describes the old "C2 = 1 and push indefinite" behaviour. - **P3 / warning:** the generated control fixtures (`cpu_x87_trig_snan.cpp(541)`, `cpu_x87_trig_pc53.cpp(541)`) compile with MSVC warning C4805 (`|` between `uint64_t` and `bool`). It does not change a result; I did not check whether it predates this PR. - Not reproduced here: the author's mismatch counts and the Ryzen-specific numbers in the docs; the tolerance statements are unchanged from the last round. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 13:00:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:13:25Z.

Review: COMMENTED


Review of head 25ea2e28e62556f9406c95fb9cd9f421b6aa8920 (comment only, no approval).

Result: no P1, no P2. Two nits and one CI note below.

What I checked

  • git diff --stat origin/main...HEAD: 14 files, +1528/-14. Title and wording are neutral, no local-probe or title references.
  • python3 tests/docs_links_check.py -> Checked 18 Markdown files and 448 links.
  • python3 tests/bidi_controls_check.py -> Checked 1613 tracked ... files: no bidirectional controls.
  • git diff --check origin/main...HEAD -> clean.
  • python3 -B tests/startup_run_sh_check.py -> startup run.sh: 0 failed. The red Linux job (Run Python checks) failed on startup_run_sh_check.py ("mutant 'watcher without --render' was not detected"). That file is not in this diff and passes locally on this head, so it looks unrelated or flaky. A re-run should settle it.
  • tests/cpu/x87_trig.py needs the Windows harness and DLLs, so the new cpu_aot_x87_trig test cannot run on Linux. It is covered by the pending Windows CI job, and I did not queue the Windows VM.

Independent differential run (Linux, outside the repo)

I compiled tools/x87_soft.h with a small shim for the MSVC intrinsics. I compared x87_sin, x87_cos, x87_sincos, x87_ptan and x87_patan against the host's real x87 instructions. Inputs were 200000 random operands per group: moderate, tiny, up to 2^64 and near 2^63. About 994k compared results.

  • C2 (bit 0x0400) matched the hardware for FSIN, FCOS, FSINCOS and FPTAN in every case: 0 mismatches.
  • FPATAN: results were within 1 ULP of the hardware in every case where the exponents agreed.
  • FSIN, FCOS, FSINCOS and FPTAN: results differ from the hardware by up to hundreds of ULPs for |x| near multiples of pi/2 and large |x|. I checked one case against a 120-digit Decimal reference: the header was within 1 ULP of the exact value, while the hardware was off. This matches the documented use of a tolerance for native comparison, so it is not a defect in the PR.
  • Mutation: I changed const int qc = (q + 1) & 3; to (q + 3) & 3; in a copy of x87_soft.h. FCOS mismatches rose from 106489 to 158637 in my harness, so the cos quadrant logic is observable. I did not mutate the PR's own Windows test, because it needs the Windows harness. The PR already ships eight mutated control DLLs (noc2, c262, sincosswap, ptanno1, patanswap, sinnegzero, snan, pc53) that the check requires to mismatch. Windows CI will show whether they are all killed.

Findings

  • P1: none.
  • P2: none.
  • nit tools/x87_lower.py (FSIN/FCOS statement): x87_sw is overwritten with only C2. This is correct, because C0, C1 and C3 are undefined for these ops, but a one-line comment in the generated code would help readers of the lowered output.
  • nit tools/x87_soft.h (x87_trap): the finite |ST(0)| >= 2^63 case for FSINCOS/FPTAN traps by design. Please keep the Windows CI evidence of the trap path (sincos_depth7 / ptan_depth7 and the out-of-range check at tests/cpu/x87_trig.py:661) in the PR body.

Needs before Ready

  • Windows CI (Build and test on Windows) must go green. It was still pending when I reviewed.
  • Re-run the Linux job to clear the unrelated startup_run_sh_check failure.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6057390654 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#pullrequestreview-6057390654). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T12:13:25Z. **Review:** COMMENTED --- Review of head `25ea2e28e62556f9406c95fb9cd9f421b6aa8920` (comment only, no approval). **Result: no P1, no P2.** Two nits and one CI note below. ### What I checked - `git diff --stat origin/main...HEAD`: 14 files, +1528/-14. Title and wording are neutral, no local-probe or title references. - `python3 tests/docs_links_check.py` -> `Checked 18 Markdown files and 448 links.` - `python3 tests/bidi_controls_check.py` -> `Checked 1613 tracked ... files: no bidirectional controls.` - `git diff --check origin/main...HEAD` -> clean. - `python3 -B tests/startup_run_sh_check.py` -> `startup run.sh: 0 failed`. The red Linux job (`Run Python checks`) failed on `startup_run_sh_check.py` ("mutant 'watcher without --render' was not detected"). That file is not in this diff and passes locally on this head, so it looks unrelated or flaky. A re-run should settle it. - `tests/cpu/x87_trig.py` needs the Windows harness and DLLs, so the new `cpu_aot_x87_trig` test cannot run on Linux. It is covered by the pending Windows CI job, and I did not queue the Windows VM. ### Independent differential run (Linux, outside the repo) I compiled `tools/x87_soft.h` with a small shim for the MSVC intrinsics. I compared `x87_sin`, `x87_cos`, `x87_sincos`, `x87_ptan` and `x87_patan` against the host's real x87 instructions. Inputs were 200000 random operands per group: moderate, tiny, up to 2^64 and near 2^63. About 994k compared results. - C2 (bit 0x0400) matched the hardware for FSIN, FCOS, FSINCOS and FPTAN in every case: 0 mismatches. - FPATAN: results were within 1 ULP of the hardware in every case where the exponents agreed. - FSIN, FCOS, FSINCOS and FPTAN: results differ from the hardware by up to hundreds of ULPs for |x| near multiples of pi/2 and large |x|. I checked one case against a 120-digit Decimal reference: the header was within 1 ULP of the exact value, while the hardware was off. This matches the documented use of a tolerance for native comparison, so it is not a defect in the PR. - Mutation: I changed `const int qc = (q + 1) & 3;` to `(q + 3) & 3;` in a copy of `x87_soft.h`. FCOS mismatches rose from 106489 to 158637 in my harness, so the cos quadrant logic is observable. I did not mutate the PR's own Windows test, because it needs the Windows harness. The PR already ships eight mutated control DLLs (`noc2`, `c262`, `sincosswap`, `ptanno1`, `patanswap`, `sinnegzero`, `snan`, `pc53`) that the check requires to mismatch. Windows CI will show whether they are all killed. ### Findings - P1: none. - P2: none. - nit `tools/x87_lower.py` (FSIN/FCOS statement): `x87_sw` is overwritten with only C2. This is correct, because C0, C1 and C3 are undefined for these ops, but a one-line comment in the generated code would help readers of the lowered output. - nit `tools/x87_soft.h` (`x87_trap`): the finite |ST(0)| >= 2^63 case for FSINCOS/FPTAN traps by design. Please keep the Windows CI evidence of the trap path (`sincos_depth7` / `ptan_depth7` and the out-of-range check at `tests/cpu/x87_trig.py:661`) in the PR body. ### Needs before Ready - Windows CI (`Build and test on Windows`) must go green. It was still pending when I reviewed. - Re-run the Linux job to clear the unrelated `startup_run_sh_check` failure. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 13:00:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T12:32:18Z.

Review: COMMENTED


Review of head 5d8cf6fe04e1a1685c26597de801da93d177b157 (FSIN, FCOS, FSINCOS, FPTAN, FPATAN lowering).

Verdict: no P1, no P2. Two nits below.

Checks run locally (Linux)

  • gh pr checks 1663: Linux Python checks, Windows build and test, and gfx1013 fixtures all pass; the /O2 job was skipped.
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files and 451 links checked.
  • python3 tests/bidi_controls_check.py: 1618 files, no bidirectional controls.
  • git diff --check origin/main...HEAD: clean.
  • python3 -I -m py_compile tests/cpu/x87_trig.py tools/x87_lower.py: OK.
  • python3 tests/cpu/x87_trig.py asm /tmp/t.asm /tmp/t.def: output is byte-identical to the committed x87_trig.asm and x87_trig.def.
  • check and check_rejections could not run here. They need the Windows harness and iced_x86, and the Windows CI job covers cpu_aot_x87_trig.

Independent numeric check of tools/x87_soft.h

  • I built it on Linux with a small shim for the MSVC intrinsics and compared it with libm long double.
  • Inputs were 16 values from 1e-10 to 8e18.
  • x87_sincos, x87_ptan and x87_patan (16 atan2 pairs) agreed to within 1e-19 relative error.
  • x87_sincos and x87_ptan set C2 for 1e19 (above 2^63), as the code comments say.

Mutation

  • I changed sub = !swapped || !sx to sub = !swapped in x87_patan, in a copy of the header.
  • My comparison harness flagged 3 of 16 atan2 pairs. This is a separate local harness, not the repo test.
  • The PR's own controls (sincosswap, patanswap, c262, and others) need Windows to run, so I did not run them.

Code review

  • The stack-depth effects in _effect look right. FSINCOS and FPTAN push one, FPATAN pops one, FSIN and FCOS keep the depth.
  • C2 is tracked as known, with C0, C1 and C3 undefined, through TRIG_C2 and _after. The rejection list in check_rejections covers the readers of the undefined bits.
  • Trapping on finite |x| >= 2^63 for FSINCOS and FPTAN is a sound choice for a static-depth profile. It is documented in the module docstring and in docs/experiments.md.

Nits

  • N1: the PR is not a Draft (isDraft=false), and mergeable was UNKNOWN when I checked.
  • N2: docs/experiments.md is a single very long paragraph. It is hard to review, but it matches the file's existing style.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6057646043 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#pullrequestreview-6057646043). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T12:32:18Z. **Review:** COMMENTED --- Review of head `5d8cf6fe04e1a1685c26597de801da93d177b157` (FSIN, FCOS, FSINCOS, FPTAN, FPATAN lowering). **Verdict: no P1, no P2.** Two nits below. **Checks run locally (Linux)** - `gh pr checks 1663`: Linux Python checks, Windows build and test, and gfx1013 fixtures all pass; the /O2 job was skipped. - `python3 tests/docs_links_check.py`: self-test passed, 18 Markdown files and 451 links checked. - `python3 tests/bidi_controls_check.py`: 1618 files, no bidirectional controls. - `git diff --check origin/main...HEAD`: clean. - `python3 -I -m py_compile tests/cpu/x87_trig.py tools/x87_lower.py`: OK. - `python3 tests/cpu/x87_trig.py asm /tmp/t.asm /tmp/t.def`: output is byte-identical to the committed `x87_trig.asm` and `x87_trig.def`. - `check` and `check_rejections` could not run here. They need the Windows harness and `iced_x86`, and the Windows CI job covers `cpu_aot_x87_trig`. **Independent numeric check of `tools/x87_soft.h`** - I built it on Linux with a small shim for the MSVC intrinsics and compared it with libm `long double`. - Inputs were 16 values from 1e-10 to 8e18. - `x87_sincos`, `x87_ptan` and `x87_patan` (16 atan2 pairs) agreed to within 1e-19 relative error. - `x87_sincos` and `x87_ptan` set C2 for 1e19 (above 2^63), as the code comments say. **Mutation** - I changed `sub = !swapped || !sx` to `sub = !swapped` in `x87_patan`, in a copy of the header. - My comparison harness flagged 3 of 16 atan2 pairs. This is a separate local harness, not the repo test. - The PR's own controls (`sincosswap`, `patanswap`, `c262`, and others) need Windows to run, so I did not run them. **Code review** - The stack-depth effects in `_effect` look right. FSINCOS and FPTAN push one, FPATAN pops one, FSIN and FCOS keep the depth. - C2 is tracked as known, with C0, C1 and C3 undefined, through `TRIG_C2` and `_after`. The rejection list in `check_rejections` covers the readers of the undefined bits. - Trapping on finite |x| >= 2^63 for FSINCOS and FPTAN is a sound choice for a static-depth profile. It is documented in the module docstring and in `docs/experiments.md`. **Nits** - N1: the PR is not a Draft (`isDraft=false`), and mergeable was `UNKNOWN` when I checked. - N2: `docs/experiments.md` is a single very long paragraph. It is hard to review, but it matches the file's existing style. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-25 13:00:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T12:26:02Z.


Merged upstream/main (4514fa09, including #2323 which stabilizes tests/startup_run_sh_check.py) into cpu/x87-trig at commit 5d8cf6fe to clear the unrelated Linux Python CI failure; re-ran tests/form_coverage_check.py, tests/docs_links_check.py (448 links), and tests/bidi_controls_check.py (exit 0). Note: cpu_aot_x87_trig (and all 23 x87 CTests) passed on both dev (Debug) and release (/O2) on Windows x64 after merging the x87 environment updates, including the sincos_depth7/ptan_depth7 trap path.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7162366180 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#issuecomment-7162366180). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T12:26:02Z. --- Merged `upstream/main` (`4514fa09`, including `#2323` which stabilizes `tests/startup_run_sh_check.py`) into `cpu/x87-trig` at commit `5d8cf6fe` to clear the unrelated Linux Python CI failure; re-ran `tests/form_coverage_check.py`, `tests/docs_links_check.py` (448 links), and `tests/bidi_controls_check.py` (exit 0). Note: `cpu_aot_x87_trig` (and all 23 `x87` CTests) passed on both `dev` (Debug) and `release` (`/O2`) on Windows x64 after merging the x87 environment updates, including the `sincos_depth7`/`ptan_depth7` trap path. AI-assisted.
upstream-sync closed this pull request 2026-11-25 19:03:23 +00:00
tedin7 commented 2026-11-25 19:04:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T18:22:03Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 59865488b893a62ebf57ed36dce852352a0df0fe:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_x87_harness cpu_x87_trig cpu_x87_trig_lifted cpu_x87_trig_noc2 cpu_x87_trig_c262 cpu_x87_trig_sincosswap cpu_x87_trig_ptanno1 cpu_x87_trig_patanswap cpu_x87_trig_sinnegzero cpu_x87_trig_snan cpu_x87_trig_pc53 -- -j 2): 4.87s (configure 1.75s)
    • Test (ctest --preset dev -R '^cpu_aot_x87_trig$' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (24.65s: cpu_aot_x87_trig 24.53s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_x87_harness cpu_x87_trig cpu_x87_trig_lifted cpu_x87_trig_noc2 cpu_x87_trig_c262 cpu_x87_trig_sincosswap cpu_x87_trig_ptanno1 cpu_x87_trig_patanswap cpu_x87_trig_sinnegzero cpu_x87_trig_snan cpu_x87_trig_pc53 -- -j 2): 24.66s (configure 1.75s)
    • Test (ctest --preset release -R '^cpu_aot_x87_trig$' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (20.92s: cpu_aot_x87_trig 20.82s)
  • sccache: 18/18 hits; wall 60.3s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7170810107 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1663#issuecomment-7170810107). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T18:22:03Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `59865488b893a62ebf57ed36dce852352a0df0fe`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_x87_harness cpu_x87_trig cpu_x87_trig_lifted cpu_x87_trig_noc2 cpu_x87_trig_c262 cpu_x87_trig_sincosswap cpu_x87_trig_ptanno1 cpu_x87_trig_patanswap cpu_x87_trig_sinnegzero cpu_x87_trig_snan cpu_x87_trig_pc53 -- -j 2`): `4.87s` (configure `1.75s`) - Test (`ctest --preset dev -R '^cpu_aot_x87_trig$' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`24.65s`: `cpu_aot_x87_trig` `24.53s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_x87_harness cpu_x87_trig cpu_x87_trig_lifted cpu_x87_trig_noc2 cpu_x87_trig_c262 cpu_x87_trig_sincosswap cpu_x87_trig_ptanno1 cpu_x87_trig_patanswap cpu_x87_trig_sinnegzero cpu_x87_trig_snan cpu_x87_trig_pc53 -- -j 2`): `24.66s` (configure `1.75s`) - Test (`ctest --preset release -R '^cpu_aot_x87_trig$' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`20.92s`: `cpu_aot_x87_trig` `20.82s`) - `sccache`: 18/18 hits; wall `60.3s` AI-assisted; commands listed were executed locally.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!1656
No description provided.