shader: lower v_frexp_exp_i32_f64 and v_frexp_mant_f64 in direct compute #3634

Merged
Ammmae0x merged 1 commit from upstream-pr/3634 into main 2026-11-30 19:58:01 +00:00
Ammmae0x commented 2026-11-30 18:40:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-30T18:40:48Z.


What changed

Exact binary64 frexp with pure 32-bit integer operations only, following the v_frexp_*_f32 shape and the v_ldexp_f64 subnormal normalizer: no DXIL double, no i64, no fcmp, and neither DoublePrecisionFloatShaderOps nor Int64ShaderOps.

  • v_frexp_exp_i32_f64 (VOP1 0x3c, VOP3 0x1bc) writes one 32-bit VGPR (v255 allowed) with the signed exponent e where x = m * 2^e and |m| in [0.5, 1).
  • v_frexp_mant_f64 (VOP1 0x3d, VOP3 0x1bd) writes a 64-bit VGPR pair (v255 refused) with the normalized significand m in (-1, -0.5] U [0.5, 1), keeping the input sign after abs/neg.
  • Normal inputs give exp - 1022 and a mantissa with biased exponent 1022 (0x3FE00000). A subnormal is normalized by the same 6-step shift search as v_ldexp_f64 ({32, 16, 8, 4, 2, 1}, every shift amount a constant in [0, 31]), giving 1 - steps - 1022 and the normalized field. The k == 32 step tests mh == 0 && ml < 2097152: 2097151 takes the 32-shift and 2097152 does not, pinned by the boundary values.
  • Zero, Inf and NaN keep the exponent 0 and pass the mantissa unchanged after abs/neg, so NaN payloads survive (mantissa compared bit-for-bit in IR and by class on hardware).
  • Sources are VGPR pairs, aligned even SGPR pairs, integer 0 (128 -> +0.0) and the inline binary64 constants 240..247 (lo = 0). Only under the default MODE for the shared f16/f64 fields (denormals kept); the binary16 MODE tracking now also covers both ops.
  • Writing v[d:d+1] invalidates lane spills on both halves (clear_vector_spills, mantissa op only); the exponent op writes only v[d], so it succeeds over a symbolic lane spill in v[d+1].

Tests

tests/native_frexp_f64_check.py model|ir|controls|hardware (ctest --preset dev --no-tests=error -R '^native_frexp_f64_' -V). Details in docs/experiments/native-direct-frexp-f64.md.

  • model: 90 boundary values (incl. ±0.0, ±MIN_SUB (exp -1073), lo-only subnormals 2^-1073..2^-1043, the cross-word boundary 0x00000000_00200000, ±MAX_SUB, ±MIN_NORM, ±0.5, ±(1 - 1ulp), ±1.0, ±1.5, ±2.0, ±DBL_MAX, ±Inf, sNaN, qNaN) x 2 ops x abs/neg/abs+neg cross-checked against math.frexp; 11 self-mutations killed.
  • ir: boundary values + seeded sweep of 100 random doubles through the IR interpreter in chunks of at most 1024 stores (the F.store 12-bit offset wrap is why the dumps are split), e64 forms (SGPR pairs, inline 128/240..247, abs/neg/abs+neg, v255 exponent destination), wave64 container text-only (no @waveExchange, {i32 64, i32 1, i32 1} metadata), and the exponent op succeeding over a symbolic lane spill in v11.
  • controls: 23 refusals with exact error substrings and non-zero exit codes (modifiers incl. clamp on both ops, source/dst guards incl. v255 mantissa destination, MODE, symbolic spill overwrites), plus the positive control that the exponent op compiles over a spill in v11.
  • hardware: 4 programs x 4 cases x 3 words per lane; mantissa NaN pairs compared by class, exponent words exact; returns 77 on missing hardware caps.

Linux results (this host cannot compile the translation unit or run DXC, so ir/controls/hardware run in the WIN-VERIFY round):

/tmp/pe-env/bin/python -B tests/native_frexp_f64_check.py model
native_frexp_f64 model: 90 values x 2 ops x abs/neg/absneg matched math.frexp; 11 self-mutations killed
/tmp/pe-env/bin/python -B tests/form_coverage_check.py
form coverage checks passed
/tmp/pe-env/bin/python -B tests/docs_links_check.py
Checked 188 Markdown files and 740 links.
/tmp/pe-env/bin/python -B tests/bidi_controls_check.py
Checked 2744 tracked source, script and build/CI files: no bidirectional controls.
/tmp/pe-env/bin/python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux
all 72 checks passed
git diff --check            # clean
git merge-tree --write-tree origin/main HEAD   # exits 0

Mutation audit

Every C++ guard/constant is pinned by a killing test (see the docs note for the full mapping): modifier and MODE gates by the controls refusals; the half_binary MODE-loop addition by the conditional s_denorm_mode refusal; destination/source pair acceptance by the controls refusals and the ir e64 chunk; abs/neg by the ir modifier cases and the hardware e64 program; the 1022/1074 biases, the k == 32 threshold (2097151 vs 2097152), the low-word normalizer path, the 20-bit field mask, the sign, the keep rule and the abs-before-neg order by the model values/mutations and the ir boundary values. The clear_vector_spills pair entry is for the mantissa op only; write() checks has_spill(r) on the pre-instruction state, so the controls spill refusals pin that both vector_write calls execute write() for the mantissa op, while the ir spill dump and the controls positive control pin that the exponent op writes only v[d].

Notes

  • An SGPR destination is not encodable for VOP1/VOP3 (the decoder always maps VDST to 256 + vdst), so the destination guard's SGPR half is defense in depth; the encodable half is pinned by the v255 mantissa refusal and the v255 exponent acceptance in ir.
  • The C++ executions run in the WIN-VERIFY round, since this host cannot compile the translation unit.

Refs #2935, #3518

Windows verification (ec737728)

git merge-tree --write-tree upstream/main HEAD -> ea9f517b2fc1f927248b57ac295dde5d4a73cca5 (clean)
git diff --check upstream/main...HEAD -> clean
ctest --preset dev --no-tests=error -R ^native_frexp_f64_ --output-on-failure
  3/3 passed (4.35s: ir 0.72s, controls 1.66s, hardware 1.84s)
ctest --preset release --no-tests=error -R ^native_frexp_f64_ -E cpu_native_archive_ --output-on-failure
  3/3 passed (3.03s: ir 0.35s, controls 1.19s, hardware 1.37s)
GPU adapter: NVIDIA GeForce RTX 4050 Laptop GPU (10de:28e1, driver 32.0.15.9186)
python -B tests/native_frexp_f64_check.py model -> passed (90 values x 2 ops x abs/neg/absneg; 11 self-mutations killed)
python -B tests/form_coverage_check.py -> passed
python -B tests/docs_links_check.py -> Checked 189 Markdown files and 740 links.
python -B tests/bidi_controls_check.py -> Checked 2746 tracked source, script and build/CI files: no bidirectional controls.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5571476906 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3634). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-30T18:40:48Z. --- ## What changed Exact binary64 frexp with pure 32-bit integer operations only, following the `v_frexp_*_f32` shape and the `v_ldexp_f64` subnormal normalizer: no DXIL `double`, no `i64`, no `fcmp`, and neither `DoublePrecisionFloatShaderOps` nor `Int64ShaderOps`. - `v_frexp_exp_i32_f64` (VOP1 `0x3c`, VOP3 `0x1bc`) writes one 32-bit VGPR (`v255` allowed) with the signed exponent `e` where `x = m * 2^e` and `|m|` in `[0.5, 1)`. - `v_frexp_mant_f64` (VOP1 `0x3d`, VOP3 `0x1bd`) writes a 64-bit VGPR pair (`v255` refused) with the normalized significand `m` in `(-1, -0.5] U [0.5, 1)`, keeping the input sign after `abs`/`neg`. - Normal inputs give `exp - 1022` and a mantissa with biased exponent `1022` (`0x3FE00000`). A subnormal is normalized by the same 6-step shift search as `v_ldexp_f64` (`{32, 16, 8, 4, 2, 1}`, every shift amount a constant in `[0, 31]`), giving `1 - steps - 1022` and the normalized field. The `k == 32` step tests `mh == 0 && ml < 2097152`: `2097151` takes the 32-shift and `2097152` does not, pinned by the boundary values. - Zero, Inf and NaN keep the exponent `0` and pass the mantissa unchanged after `abs`/`neg`, so NaN payloads survive (mantissa compared bit-for-bit in IR and by class on hardware). - Sources are VGPR pairs, aligned even SGPR pairs, integer `0` (`128` -> `+0.0`) and the inline binary64 constants `240..247` (`lo = 0`). Only under the default MODE for the shared f16/f64 fields (denormals kept); the binary16 MODE tracking now also covers both ops. - Writing `v[d:d+1]` invalidates lane spills on both halves (`clear_vector_spills`, mantissa op only); the exponent op writes only `v[d]`, so it succeeds over a symbolic lane spill in `v[d+1]`. ## Tests `tests/native_frexp_f64_check.py model|ir|controls|hardware` (`ctest --preset dev --no-tests=error -R '^native_frexp_f64_' -V`). Details in `docs/experiments/native-direct-frexp-f64.md`. - `model`: 90 boundary values (incl. `±0.0`, `±MIN_SUB` (`exp -1073`), lo-only subnormals `2^-1073..2^-1043`, the cross-word boundary `0x00000000_00200000`, `±MAX_SUB`, `±MIN_NORM`, `±0.5`, `±(1 - 1ulp)`, `±1.0`, `±1.5`, `±2.0`, `±DBL_MAX`, `±Inf`, `sNaN`, `qNaN`) x 2 ops x abs/neg/abs+neg cross-checked against `math.frexp`; 11 self-mutations killed. - `ir`: boundary values + seeded sweep of 100 random doubles through the IR interpreter in chunks of at most 1024 stores (the `F.store` 12-bit offset wrap is why the dumps are split), e64 forms (SGPR pairs, inline `128`/`240..247`, abs/neg/abs+neg, `v255` exponent destination), wave64 container text-only (no `@waveExchange`, `{i32 64, i32 1, i32 1}` metadata), and the exponent op succeeding over a symbolic lane spill in `v11`. - `controls`: 23 refusals with exact error substrings and non-zero exit codes (modifiers incl. clamp on both ops, source/dst guards incl. `v255` mantissa destination, MODE, symbolic spill overwrites), plus the positive control that the exponent op compiles over a spill in `v11`. - `hardware`: 4 programs x 4 cases x 3 words per lane; mantissa NaN pairs compared by class, exponent words exact; returns 77 on missing hardware caps. Linux results (this host cannot compile the translation unit or run DXC, so `ir`/`controls`/`hardware` run in the WIN-VERIFY round): ```text /tmp/pe-env/bin/python -B tests/native_frexp_f64_check.py model native_frexp_f64 model: 90 values x 2 ops x abs/neg/absneg matched math.frexp; 11 self-mutations killed /tmp/pe-env/bin/python -B tests/form_coverage_check.py form coverage checks passed /tmp/pe-env/bin/python -B tests/docs_links_check.py Checked 188 Markdown files and 740 links. /tmp/pe-env/bin/python -B tests/bidi_controls_check.py Checked 2744 tracked source, script and build/CI files: no bidirectional controls. /tmp/pe-env/bin/python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linux all 72 checks passed git diff --check # clean git merge-tree --write-tree origin/main HEAD # exits 0 ``` ## Mutation audit Every C++ guard/constant is pinned by a killing test (see the docs note for the full mapping): modifier and MODE gates by the controls refusals; the `half_binary` MODE-loop addition by the conditional `s_denorm_mode` refusal; destination/source pair acceptance by the controls refusals and the ir e64 chunk; `abs`/`neg` by the ir modifier cases and the hardware e64 program; the `1022`/`1074` biases, the `k == 32` threshold (`2097151` vs `2097152`), the low-word normalizer path, the 20-bit field mask, the sign, the keep rule and the abs-before-neg order by the model values/mutations and the ir boundary values. The `clear_vector_spills` pair entry is for the mantissa op only; `write()` checks `has_spill(r)` on the pre-instruction state, so the controls spill refusals pin that both `vector_write` calls execute `write()` for the mantissa op, while the ir spill dump and the controls positive control pin that the exponent op writes only `v[d]`. ## Notes - An SGPR destination is not encodable for VOP1/VOP3 (the decoder always maps VDST to `256 + vdst`), so the destination guard's SGPR half is defense in depth; the encodable half is pinned by the `v255` mantissa refusal and the `v255` exponent acceptance in `ir`. - The C++ executions run in the WIN-VERIFY round, since this host cannot compile the translation unit. Refs #2935, #3518 ### Windows verification (`ec737728`) ```text git merge-tree --write-tree upstream/main HEAD -> ea9f517b2fc1f927248b57ac295dde5d4a73cca5 (clean) git diff --check upstream/main...HEAD -> clean ctest --preset dev --no-tests=error -R ^native_frexp_f64_ --output-on-failure 3/3 passed (4.35s: ir 0.72s, controls 1.66s, hardware 1.84s) ctest --preset release --no-tests=error -R ^native_frexp_f64_ -E cpu_native_archive_ --output-on-failure 3/3 passed (3.03s: ir 0.35s, controls 1.19s, hardware 1.37s) GPU adapter: NVIDIA GeForce RTX 4050 Laptop GPU (10de:28e1, driver 32.0.15.9186) python -B tests/native_frexp_f64_check.py model -> passed (90 values x 2 ops x abs/neg/absneg; 11 self-mutations killed) python -B tests/form_coverage_check.py -> passed python -B tests/docs_links_check.py -> Checked 189 Markdown files and 740 links. python -B tests/bidi_controls_check.py -> Checked 2746 tracked source, script and build/CI files: no bidirectional controls. ``` AI-assisted. <!-- WIN-VERIFY: passed | sha=ec737728f7e22999a8762d378e3743ccd8e91a54 | targets=compile_dxil native_compute_ir_dump native_store_replay | ctest=^native_frexp_f64_ | release=true -->
lukaslw-lab commented 2026-11-30 19:48:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by lukaslw-lab on 2026-11-30T19:48:15Z.

Review: APPROVED


Reviewed ec737728 by reading the diff; not built or run.

Verdict: approve. The lowering matches binary64 frexp for every input class, and the guards match the existing f64 ops. No MUST.

  • NIT The hardware check compares mantissa NaNs by class (tests/native_frexp_f64_check.py:535), but the lowering uses only integer ops, so the payload (including sNaN 0x7FF4000000000001 in HW_SPECIAL) should survive bit-for-bit on any device. An exact compare would make the hardware run pin the keep path too. Today only model and ir cover it (nan_canon).
  • Checked, no finding:
    • Subnormal exponent (gfx1013_direct_compute.inl:2547): after the search, the field is in [2^52, 2^53), so x = (f·2^s / 2^53) · 2^(53 − s − 1074) and e = 53 − steps − 1074 is right. The k == 32 step (mh == 0 && ml < 2^21) never pushes the high word past bit 20, and the other steps reach every shift from 21 to 31 when the top bit is in lo[21..31].
    • Normal inputs: exp − 1022, and the mantissa high word is sign | 0x3FE00000 | field. Zero/Inf/NaN go through keep, after abs/neg, like v_ldexp_f64.
    • Sources: the 240..247 high words are right; literals, odd SGPRs and VCC are refused. Overlapping src/dst pairs are safe (lo/hi are read before both writes).
    • MODE: same 0x8000cc gate as v_ldexp_f64 / v_min_f64. Both ops added to half_binary (:5623).
    • clear_vector_spills pair entry only for the mantissa op (gfx1013_compute_native.cpp:643), so the exponent op over a spill in v[d+1] stays legal.

Tests: model checks the model against math.frexp; ir runs the real lowering on the boundary values, sweep and e64 forms; controls reaches every refusal; hardware checks 16 cases on a device. CI ran only model (Linux Python checks). The native_frexp_f64_* CTest entries are not in the Windows pr preset filter, so ir/controls/hardware only have the author's local run at this head (RTX 4050, dev + release).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6119369927 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3634#pullrequestreview-6119369927). Originally posted by [lukaslw-lab](https://github.com/lukaslw-lab) on 2026-11-30T19:48:15Z. **Review:** APPROVED --- Reviewed `ec737728` by reading the diff; not built or run. **Verdict: approve.** The lowering matches binary64 frexp for every input class, and the guards match the existing f64 ops. No MUST. - **NIT** The hardware check compares mantissa NaNs by class (`tests/native_frexp_f64_check.py:535`), but the lowering uses only integer ops, so the payload (including sNaN `0x7FF4000000000001` in `HW_SPECIAL`) should survive bit-for-bit on any device. An exact compare would make the hardware run pin the keep path too. Today only `model` and `ir` cover it (`nan_canon`). - Checked, no finding: - Subnormal exponent (`gfx1013_direct_compute.inl:2547`): after the search, the field is in `[2^52, 2^53)`, so `x = (f·2^s / 2^53) · 2^(53 − s − 1074)` and `e = 53 − steps − 1074` is right. The `k == 32` step (`mh == 0 && ml < 2^21`) never pushes the high word past bit 20, and the other steps reach every shift from 21 to 31 when the top bit is in `lo[21..31]`. - Normal inputs: `exp − 1022`, and the mantissa high word is `sign | 0x3FE00000 | field`. Zero/Inf/NaN go through `keep`, after abs/neg, like `v_ldexp_f64`. - Sources: the 240..247 high words are right; literals, odd SGPRs and VCC are refused. Overlapping src/dst pairs are safe (`lo`/`hi` are read before both writes). - MODE: same `0x8000cc` gate as `v_ldexp_f64` / `v_min_f64`. Both ops added to `half_binary` (`:5623`). - `clear_vector_spills` pair entry only for the mantissa op (`gfx1013_compute_native.cpp:643`), so the exponent op over a spill in `v[d+1]` stays legal. Tests: `model` checks the model against `math.frexp`; `ir` runs the real lowering on the boundary values, sweep and e64 forms; `controls` reaches every refusal; `hardware` checks 16 cases on a device. CI ran only `model` (Linux Python checks). The `native_frexp_f64_*` CTest entries are not in the Windows `pr` preset filter, so `ir`/`controls`/`hardware` only have the author's local run at this head (RTX 4050, dev + release).
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!3634
No description provided.