feat(rdna2): implement 16-bit float transcendentals (VOP1/VOP3) in interpreter and HLSL (1,299/1,490) #310

Manually merged
github merged 5 commits from github-bot/gh-310 into gh-pr/310/base 2026-11-26 15:26:32 +00:00 AGit
Owner

@⁠Lewandox on GitHub · 2026-11-26 02:14 UTC · original
Lewandox:feat/rdna2-f16-transcendentals → main · +526 −2 · 4 files · 5 commits · merged 2026-11-26 15:26 UTC

Change

Implement the 7 RDNA 2 16-bit float (f16) transcendental instructions in both the reference interpreter (PrototypeScarlett/emu/rdna2_exec_f16.py) and the HLSL recompiler (PrototypeScarlett/emu/rdna2_hlsl_f16.py), covering both VOP1 and VOP3 encodings (+14 encodings, raising interpreter coverage from 1,285/1,490 to 1,299/1,490):

  • v_rcp_f16 (VOP1 opcode 84) — 1.0 / S0.f16
  • v_sqrt_f16 (VOP1 opcode 85) — sqrt(S0.f16)
  • v_rsq_f16 (VOP1 opcode 86) — 1.0 / sqrt(S0.f16)
  • v_log_f16 (VOP1 opcode 87) — log2(S0.f16)
  • v_exp_f16 (VOP1 opcode 88) — pow(2.0, S0.f16)
  • v_sin_f16 (VOP1 opcode 96) — sin(S0.f16 * 2 * pi)
  • v_cos_f16 (VOP1 opcode 97) — cos(S0.f16 * 2 * pi)

Key details

  • Primary source: AMD "RDNA 2" Instruction Set Architecture (Reference Guide, 30 November 2020, SHA-256 22aec03f2706dba2e148ffde1f3a25d5cdd42c71a96f95240aeb48d995d44795), Section 12.8, VOP1 opcodes 84–88 and 96–97 (printed pages 147–149), cross-checked against GPUOpen amdgpu_isa_rdna2.xml.
  • Exact turn reduction for v_sin_f16 / v_cos_f16: inputs are reduced modulo 1 turn (x - trunc(x)) and mapped to the first octant [0, 0.125] before multiplying by 2 * pi, so large finite integers (+-65504), quarter-turns (+-0.25, 0.5, 0.75), and negative subnormals (-2^-24) match the ISA functional examples (V_SIN_F16(0xfbff) => 0x0000, V_COS_F16(0x3400) => 0x0000, V_SIN_F16(0x8000) => 0x8000) with zero f32/f64 rounding drift across all 65,536 binary16 inputs.
  • VOP3 & wave64 & graphics: automatically supports op_sel, abs, neg, and clamp in VOP3, preserves high/low halves and inactive EXEC lanes, refuses unsourced omod and SDWA/DPP forms in both interpreter and HLSL, and works in compute (wave32 & wave64) and graphics shaders without touching any other module.
  • Canonical NaN: follows emu/rdna2_exec_f16.py and emu/rdna2_hlsl_f16.py (0x7E00).

Verification

python -m pytest tests/test_rdna2_f16_transcendentals.py tests/test_rdna2_f16.py tests/test_rdna2_f16_fract_frexp.py tests/test_rdna2_f16_pkrtz.py tests/test_rdna2_f16_rounding.py tests/test_rdna2_hlsl_f16.py -vv
→ 104 passed, 6 skipped in 1.28s

python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools
→ All checks passed!

python -m pytest -q
→ 1363 passed, 328 skipped in 72.11s (0:01:12)
  • GPU tests (@gpu) were skipped locally because dxc / anyxboxx_gpu_run.exe are not installed on this machine; CI runs the Windows WARP GPU parity suite (both wave32 and wave64 tests are included in tests/test_rdna2_f16_transcendentals.py).
  • Exhaustive Python verification confirmed 0 bit-level differences between the f64 interpreter formulas and the f32 HLSL helper formulas across all 65,536 possible binary16 inputs for rcp, sqrt, rsq, log2, sin, and cos (and 1 tie-boundary difference at 0x11c5 for exp2, well within the ISA's 0.51 ULP specification).

Oracle and test data

  • ISA oracle: AMD RDNA 2 ISA PDF (Nov 2020, §12.8 pp. 147–149) functional examples and IEEE 754 binary16 arithmetic.
  • New test data: 100% synthetic instruction encodings and test vectors built from scratch in tests/test_rdna2_f16_transcendentals.py (GPL-3.0-or-later, same as repository).
  • Failure case: All tests in tests/test_rdna2_f16_transcendentals.py fail with Untranslated / Untranslatable without this change and pass with it.

Checklist

  • The PR covers a single reviewable behaviour.
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error (E_NOTIMPL), never a fake success.
  • README: the same change in all 9 languages (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: N/A (pure ISA specification work).
  • Code taken from another project: none.

Made with AI assistance.

**@⁠Lewandox** on GitHub · 2026-11-26 02:14 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/310) `Lewandox:feat/rdna2-f16-transcendentals` → `main` · +526 −2 · 4 files · 5 commits · merged 2026-11-26 15:26 UTC ## Change Implement the 7 RDNA 2 16-bit float (`f16`) transcendental instructions in both the reference interpreter (`PrototypeScarlett/emu/rdna2_exec_f16.py`) and the HLSL recompiler (`PrototypeScarlett/emu/rdna2_hlsl_f16.py`), covering both **VOP1** and **VOP3** encodings (**+14 encodings**, raising interpreter coverage from **1,285/1,490** to **1,299/1,490**): - `v_rcp_f16` (VOP1 opcode 84) — `1.0 / S0.f16` - `v_sqrt_f16` (VOP1 opcode 85) — `sqrt(S0.f16)` - `v_rsq_f16` (VOP1 opcode 86) — `1.0 / sqrt(S0.f16)` - `v_log_f16` (VOP1 opcode 87) — `log2(S0.f16)` - `v_exp_f16` (VOP1 opcode 88) — `pow(2.0, S0.f16)` - `v_sin_f16` (VOP1 opcode 96) — `sin(S0.f16 * 2 * pi)` - `v_cos_f16` (VOP1 opcode 97) — `cos(S0.f16 * 2 * pi)` ### Key details - **Primary source**: AMD *"RDNA 2" Instruction Set Architecture* (Reference Guide, 30 November 2020, SHA-256 `22aec03f2706dba2e148ffde1f3a25d5cdd42c71a96f95240aeb48d995d44795`), Section 12.8, VOP1 opcodes 84–88 and 96–97 (printed pages 147–149), cross-checked against GPUOpen `amdgpu_isa_rdna2.xml`. - **Exact turn reduction for `v_sin_f16` / `v_cos_f16`**: inputs are reduced modulo 1 turn (`x - trunc(x)`) and mapped to the first octant `[0, 0.125]` before multiplying by `2 * pi`, so large finite integers (`+-65504`), quarter-turns (`+-0.25`, `0.5`, `0.75`), and negative subnormals (`-2^-24`) match the ISA functional examples (`V_SIN_F16(0xfbff) => 0x0000`, `V_COS_F16(0x3400) => 0x0000`, `V_SIN_F16(0x8000) => 0x8000`) with zero f32/f64 rounding drift across all 65,536 binary16 inputs. - **VOP3 & wave64 & graphics**: automatically supports `op_sel`, `abs`, `neg`, and `clamp` in VOP3, preserves high/low halves and inactive EXEC lanes, refuses unsourced `omod` and SDWA/DPP forms in both interpreter and HLSL, and works in compute (wave32 & wave64) and graphics shaders without touching any other module. - **Canonical NaN**: follows `emu/rdna2_exec_f16.py` and `emu/rdna2_hlsl_f16.py` (`0x7E00`). ## Verification ``` python -m pytest tests/test_rdna2_f16_transcendentals.py tests/test_rdna2_f16.py tests/test_rdna2_f16_fract_frexp.py tests/test_rdna2_f16_pkrtz.py tests/test_rdna2_f16_rounding.py tests/test_rdna2_hlsl_f16.py -vv → 104 passed, 6 skipped in 1.28s python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! python -m pytest -q → 1363 passed, 328 skipped in 72.11s (0:01:12) ``` - GPU tests (`@gpu`) were skipped locally because `dxc` / `anyxboxx_gpu_run.exe` are not installed on this machine; CI runs the Windows WARP GPU parity suite (both wave32 and wave64 tests are included in `tests/test_rdna2_f16_transcendentals.py`). - Exhaustive Python verification confirmed 0 bit-level differences between the f64 interpreter formulas and the f32 HLSL helper formulas across all 65,536 possible binary16 inputs for `rcp`, `sqrt`, `rsq`, `log2`, `sin`, and `cos` (and 1 tie-boundary difference at `0x11c5` for `exp2`, well within the ISA's 0.51 ULP specification). ## Oracle and test data - **ISA oracle**: AMD RDNA 2 ISA PDF (Nov 2020, §12.8 pp. 147–149) functional examples and IEEE 754 binary16 arithmetic. - **New test data**: 100% synthetic instruction encodings and test vectors built from scratch in `tests/test_rdna2_f16_transcendentals.py` (GPL-3.0-or-later, same as repository). - **Failure case**: All tests in `tests/test_rdna2_f16_transcendentals.py` fail with `Untranslated` / `Untranslatable` without this change and pass with it. ## Checklist - [x] The PR covers a single reviewable behaviour. - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error (`E_NOTIMPL`), never a fake success. - [x] README: the same change in all 9 languages (`test_readme_sync.py` checks it). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: N/A (pure ISA specification work). - [x] Code taken from another project: none. _Made with AI assistance._
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:42:40 +00:00
Author
Owner

@⁠LooperSalty on GitHub · 2026-11-26 10:38 UTC · original

Review (rule 4, oracle): the description says the f64 interpreter formula and the f32 HLSL helper differ on one binary16 input for v_exp_f16 (0x11c5, a tie boundary). Rule 4 asks for byte-for-byte parity between the HLSL and the reference interpreter, and the tests deliberately avoid ties ("no 0.5 ULP ties"), so this known divergence is not covered by any test.

Please:

  1. Make half_exp2 and F16Exp round the same way (same intermediate precision and same tie rule), so 0x11c5 matches, or refuse the input by name if it cannot be made exact.
  2. Add a parity test over every finite binary16 input (65,536 values, interpreter vs HLSL on GPU/WARP) for v_rcp/sqrt/rsq/log/exp/sin/cos_f16, so the claimed exhaustive check lives in the suite and not only in the PR text.
  3. Keep the ISA functional examples as the interpreter oracle (those are fine).

The rest (VOP1/VOP3, op_sel/abs/neg/clamp, refusals of omod/SDWA/DPP, synthetic encodings, sources cited) looks good.

🤖 Generated with Claude Code

**@⁠LooperSalty** on GitHub · 2026-11-26 10:38 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/310#issuecomment-7189638463) Review (rule 4, oracle): the description says the f64 interpreter formula and the f32 HLSL helper differ on one binary16 input for `v_exp_f16` (`0x11c5`, a tie boundary). Rule 4 asks for byte-for-byte parity between the HLSL and the reference interpreter, and the tests deliberately avoid ties ("no 0.5 ULP ties"), so this known divergence is not covered by any test. Please: 1. Make `half_exp2` and `F16Exp` round the same way (same intermediate precision and same tie rule), so `0x11c5` matches, or refuse the input by name if it cannot be made exact. 2. Add a parity test over every finite binary16 input (65,536 values, interpreter vs HLSL on GPU/WARP) for `v_rcp/sqrt/rsq/log/exp/sin/cos_f16`, so the claimed exhaustive check lives in the suite and not only in the PR text. 3. Keep the ISA functional examples as the interpreter oracle (those are fine). The rest (VOP1/VOP3, op_sel/abs/neg/clamp, refusals of omod/SDWA/DPP, synthetic encodings, sources cited) looks good. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Author
Owner

@⁠Lewandox on GitHub · 2026-11-26 14:52 UTC · original

Thanks for the thorough review! Addressed all three points in the latest commit:

  1. Exact rounding agreement on 0x11c5 (and all 65,536 binary16 inputs): replaced the hardware exp2(x) call in F16Exp (PrototypeScarlett/emu/rdna2_hlsl_f16.py) with range reduction (2^x = 2^n \cdot 2^f, f \in [-0.5, 0.5]), a split-\ln(2) precise float polynomial (f \cdot \text{LN2\_HI} is exact in f32 for all 11-bit f16 significands), and TwoSum + round-to-odd (same pattern as F16Fma) before scaling by 2^n and F32ToF16. This eliminates double rounding so F16Exp(0x11c5) == 0x3c01 == half_exp2(0x11c5). Also replaced hardware log2/sin/cos in F16Log and F16Trig with deterministic precise float range-reduced polynomials so GPU SFU ULP differences cannot flip ties across vendors/WARP.
  2. Exhaustive 65,536-input GPU/WARP parity test: added test_f16_transcendentals_exhaustive_parity_on_gpu in tests/test_rdna2_f16_transcendentals.py, parameterized over all 7 instructions (v_rcp/sqrt/rsq/log/exp/sin/cos_f16), running a Wave32 loop over every binary16 encoding (0x0000..0xFFFF) in both the reference interpreter (rdna2_exec.run) and translated HLSL on GPU/WARP (rdna2_gpu.run), plus 0x11C5 -> 0x3C01 in ISA_CASES and GPU_INPUTS.
  3. ISA functional examples: kept all existing ISA_CASES interpreter oracle tests intact.
**@⁠Lewandox** on GitHub · 2026-11-26 14:52 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/310#issuecomment-7195455617) Thanks for the thorough review! Addressed all three points in the latest commit: 1. **Exact rounding agreement on `0x11c5` (and all 65,536 binary16 inputs)**: replaced the hardware `exp2(x)` call in `F16Exp` (`PrototypeScarlett/emu/rdna2_hlsl_f16.py`) with range reduction ($2^x = 2^n \cdot 2^f$, $f \in [-0.5, 0.5]$), a split-$\ln(2)$ `precise float` polynomial ($f \cdot \text{LN2\_HI}$ is exact in f32 for all 11-bit f16 significands), and TwoSum + round-to-odd (same pattern as `F16Fma`) before scaling by $2^n$ and `F32ToF16`. This eliminates double rounding so `F16Exp(0x11c5) == 0x3c01 == half_exp2(0x11c5)`. Also replaced hardware `log2`/`sin`/`cos` in `F16Log` and `F16Trig` with deterministic `precise float` range-reduced polynomials so GPU SFU ULP differences cannot flip ties across vendors/WARP. 2. **Exhaustive 65,536-input GPU/WARP parity test**: added `test_f16_transcendentals_exhaustive_parity_on_gpu` in `tests/test_rdna2_f16_transcendentals.py`, parameterized over all 7 instructions (`v_rcp/sqrt/rsq/log/exp/sin/cos_f16`), running a Wave32 loop over every binary16 encoding (`0x0000..0xFFFF`) in both the reference interpreter (`rdna2_exec.run`) and translated HLSL on GPU/WARP (`rdna2_gpu.run`), plus `0x11C5 -> 0x3C01` in `ISA_CASES` and `GPU_INPUTS`. 3. **ISA functional examples**: kept all existing `ISA_CASES` interpreter oracle tests intact.
github-bot changed target branch from main to gh-pr/310/base 2026-11-26 15:26:31 +00:00
github manually merged commit 494a0b273f into gh-pr/310/base 2026-11-26 15:26:32 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.