tools: record the approximate float instructions on an RDNA 2 card #469

Merged
JeffreyBytes merged 10 commits from upstream-pr/469 into main 2026-11-29 14:37:45 +00:00
JeffreyBytes commented 2026-11-29 12:20:52 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JeffreyBytes on 2026-11-29T12:20:52Z.


Summary

Third piece of T5 (#412): what v_rcp_f32, v_rcp_iflag_f32, v_rsq_f32, v_sqrt_f32, v_log_f32, v_exp_f32, v_sin_f32, v_cos_f32, their f16 forms and v_ldexp_f32 return on an RDNA 2 card. The ISA document only gives these a precision bound, so the card is the only source for the exact bits, and they're everywhere in the recorded shaders (v_rcp_f32 in 31,124, v_rsq_f32 in 29,630, v_sqrt_f32 in 26,727).

Same route as #420 and #437: hand-written kernels through ROCm, MODE[9:0] set with s_setreg_b32, all 1,024 MODE values, on an RX 6700 XT. Inputs are 9,280 fixed f32 words, every one of the 65,536 halves, 12,224 ldexp lanes, and dense sweeps of one or two binades per f32 instruction (8.4M or 16.8M values each).

This time there's a second witness too. vulkan_approx.py runs the same inputs through RADV as GLSL compute shaders and only records when the driver's disassembly shows the instruction.

What the card does

Instructions MODE bits that matter
v_rcp_f32, v_rcp_iflag_f32, v_rsq_f32, v_sqrt_f32, v_log_f32, v_exp_f32, v_cos_f32 0x200 IEEE only. Denormal sources read as zeros of their sign and results below the smallest normal become zeros, whatever MODE says. No rounding field
v_sin_f32 0x230 IEEE, and it's the only one that follows the f32 denormal controls
v_ldexp_f32 0x233 as v_mul_f32, exact
f16 forms 0x200 plus one or both of 0x40/0x80, per instruction table in the doc

The f16 forms are correctly rounded for every half, except 14 sine and 4 cosine halves that sit almost exactly on a midpoint.

The f32 forms are not correctly rounded and I couldn't find a model that reproduces them:

correctly rounded otherwise
v_rcp_f32 89.3% one unit off
v_rsq_f32 89.0% one unit off
v_sqrt_f32 84.9% one unit off, almost always low
v_log_f32 76.7% one unit off
v_exp_f32 95.6% one unit off
v_sin_f32, v_cos_f32 48%, 36% within 2^-22.9 absolute

Every rcp/rsq/sqrt/log/exp result is one of the two floats either side of the exact value, never further. rsq isn't rcp of sqrt, sqrt isn't x times rsq, and a per-piece quadratic doesn't fit rcp. So rdna2_float.approx() gives the card's word only where it's fixed (special cases, exact results, f16) and within() holds the rest to the bound. Both hold for every recorded word. The receipt keeps a hash of each sweep rather than the words.

Other things worth knowing:

  • Angles are in turns, and the fraction is taken exactly at any magnitude. No 256-turn limit shows up; sin(1,000,000.25) is 1.
  • A zero sine or cosine result is +0 whatever the sign of the angle, unless the source is -0.
  • v_rcp_iflag_f32 gave v_rcp_f32's word on every input, sweeps included.
  • rsq(-0) is -inf, sqrt(-0) is -0, log(±0) is -inf, sin and cos of ±inf are 0xffc00000. Full table in the doc.

The Vulkan witness

For rcp, rsq, sqrt, log, exp (f32 and f16) and ldexp the driver emits the instruction alone, and the Vulkan words equal the ROCm words on every input and sweep, under the MODE each SPIR-V float mode maps to (0xc0 default, 0xf0 denorm-preserve, 0x00 denorm-flush).

Sine and cosine can't be compared directly. RADV multiplies the angle by 1/(2π) first, so the shader runs v_mul_f32 then v_sin_f32. The test feeds the model the correctly rounded product; every f16 word is reproduced and every f32 word is inside the bound.

Under denorm-preserve RADV wraps rcp, rsq, sqrt and log in a scale-by-2^24 sequence for denormal sources. The test models that sequence. One of them looks like a driver bug, not a hardware one: for log2 the undo step multiplies by -24 where it should subtract 24, so log2(2^-149) comes back as 3000.0. I haven't looked for a Mesa report yet.

I also ran vulkan_approx.py on a Radeon 890M (RDNA 3.5) out of curiosity about a newer generation. Every word matched the RX 6700 XT, sweeps included. That receipt isn't in this PR since the card isn't RDNA 2; I can add it if it's wanted.

Against the translator and the ALU check

Nothing in the translator or the check changes here. The doc has a table of what vector.rs emits for each instruction and where an exact IEEE host has to differ from the card. That part is from reading the code, not from running the translator. The ones that aren't just "one unit":

  • sin and cos: the translator's sin((x - round(x)) * 6.28318530718) gives sin(-0) = +0 where the card gives -0, a tiny nonzero value at half and quarter turns where the card gives exactly +0, and a slope six units above the card's for tiny angles.
  • A host that keeps f32 denormals differs on every denormal source (rcp, rsq, sqrt, log) and on results below 2^-126. The card flushes these under every MODE.
  • v_exp_f16 is exp2 in f32 then rounded to f16, which rounds twice: exp(0x11c5) gives 0x3c00, the card 0x3c01.
  • The ray tracing path (ray/alu.rs) still writes sin(x * 6.283185307179586f) without dropping whole turns, which the main translator fixed in 2a22586.

For check_basic_alu_gpu.py: its 1-unit bounds for rcp, rsq and sqrt are as tight as they can be. The 4-unit bounds for log and exp and the 2^-20 trig bound are looser than the card needs, which the comment there says is for the hosts' own functions. Its inputs have no denormals, so it never meets the cases where its models and the card disagree.

What's still open

  • One card for the full MODE sweep. The Vulkan run agrees with it but goes through the same GPU.
  • The f32 words are bounded, not predicted.
  • The gfx1030 assembler refuses the legacy and clamp forms (v_rcp_legacy_f32, v_rsq_clamp_f32 and so on), so those aren't measured. v_rcp_iflag_f32 has no GLSL route, so it has no Vulkan witness.
  • Same GFX10.3-only caveat as #420.

Layout

  • tools/native_rdna2/approx.hip, approx_inputs.py, check_approx.py; rdna2_float.approx() and within()
  • tools/native_rdna2/vulkan_approx.py; vulkan_float.py gets a Card.run_raw split so both share the runner
  • evidence/native-rdna2-approx.json (197 KB), evidence/native-rdna2-vulkan-approx.json (697 KB)
  • tests/test_native_rdna2_approx.py and tests/test_native_rdna2_vulkan_approx.py, no GPU. The first reproduces the f16 tables under the console's MODE by default; --exhaustive does every MODE value in about a minute
  • a section in docs/CONSOLE_REFERENCE.md, the T5 row in docs/ROADMAP.md, the README in tools/native_rdna2

Tested

  • The checks relevant to this change passed
  • Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below
  • Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them:

No Lanes scenario and no NVIDIA run, for the same reasons as #420 and #437: this runs native instructions outside TuxBox's providers, only on an RDNA 2 card, and changes no provider, translator or shared check code.

GPU, driver and backend: AMD Radeon RX 6700 XT (1002:73df, gfx1031), Linux 7.3.6, ROCm under /opt/rocm with HSA_OVERRIDE_GFX_VERSION=10.3.0 (set by the script); RADV, Mesa 26.3.1 for the Vulkan route. No Proton involved.

Commands run and their results:

python tools/native_rdna2/check_approx.py --work build/native-rdna2-approx --output build/native-rdna2-approx.json
  PASS

python tools/native_rdna2/vulkan_approx.py verify
  PASS
python tools/native_rdna2/vulkan_float.py verify
  PASS      (unchanged by the runner split)

python -m unittest tests.test_native_rdna2_approx tests.test_native_rdna2_vulkan tests.test_native_rdna2_vulkan_approx tests.test_native_rdna2_float
  Ran 28 tests, OK (40 s)

python -m unittest discover -s tests
  Ran 650 tests, OK (skipped=21)

python tuxbox.py check --no-gpu
  passed   Build the shader compiler
  passed   Python tests
  passed   Shader compiler tests
  passed   DXBC tests
  passed   XG layout tests
  passed   Storage codec tests
  passed   D3D12 Windows typecheck

Checks

docs/RESULTS.md and docs/progress.svg don't change; the new receipts aren't a type those views count.

<!-- greyfinch-sync:brcly/TuxBox:issue:5550646939 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/469). Originally posted by [JeffreyBytes](https://github.com/JeffreyBytes) on 2026-11-29T12:20:52Z. --- ## Summary Third piece of T5 (#412): what `v_rcp_f32`, `v_rcp_iflag_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_log_f32`, `v_exp_f32`, `v_sin_f32`, `v_cos_f32`, their f16 forms and `v_ldexp_f32` return on an RDNA 2 card. The ISA document only gives these a precision bound, so the card is the only source for the exact bits, and they're everywhere in the recorded shaders (`v_rcp_f32` in 31,124, `v_rsq_f32` in 29,630, `v_sqrt_f32` in 26,727). Same route as #420 and #437: hand-written kernels through ROCm, `MODE[9:0]` set with `s_setreg_b32`, all 1,024 MODE values, on an RX 6700 XT. Inputs are 9,280 fixed f32 words, every one of the 65,536 halves, 12,224 ldexp lanes, and dense sweeps of one or two binades per f32 instruction (8.4M or 16.8M values each). This time there's a second witness too. `vulkan_approx.py` runs the same inputs through RADV as GLSL compute shaders and only records when the driver's disassembly shows the instruction. ### What the card does | Instructions | MODE bits that matter | | | --- | --- | --- | | `v_rcp_f32`, `v_rcp_iflag_f32`, `v_rsq_f32`, `v_sqrt_f32`, `v_log_f32`, `v_exp_f32`, `v_cos_f32` | `0x200` | IEEE only. Denormal sources read as zeros of their sign and results below the smallest normal become zeros, whatever MODE says. No rounding field | | `v_sin_f32` | `0x230` | IEEE, and it's the only one that follows the f32 denormal controls | | `v_ldexp_f32` | `0x233` | as `v_mul_f32`, exact | | f16 forms | `0x200` plus one or both of `0x40`/`0x80`, per instruction | table in the doc | The f16 forms are correctly rounded for every half, except 14 sine and 4 cosine halves that sit almost exactly on a midpoint. The f32 forms are not correctly rounded and I couldn't find a model that reproduces them: | | correctly rounded | otherwise | | --- | ---: | --- | | `v_rcp_f32` | 89.3% | one unit off | | `v_rsq_f32` | 89.0% | one unit off | | `v_sqrt_f32` | 84.9% | one unit off, almost always low | | `v_log_f32` | 76.7% | one unit off | | `v_exp_f32` | 95.6% | one unit off | | `v_sin_f32`, `v_cos_f32` | 48%, 36% | within 2^-22.9 absolute | Every rcp/rsq/sqrt/log/exp result is one of the two floats either side of the exact value, never further. rsq isn't rcp of sqrt, sqrt isn't x times rsq, and a per-piece quadratic doesn't fit rcp. So `rdna2_float.approx()` gives the card's word only where it's fixed (special cases, exact results, f16) and `within()` holds the rest to the bound. Both hold for every recorded word. The receipt keeps a hash of each sweep rather than the words. Other things worth knowing: - Angles are in turns, and the fraction is taken exactly at any magnitude. No 256-turn limit shows up; sin(1,000,000.25) is 1. - A zero sine or cosine result is +0 whatever the sign of the angle, unless the source is -0. - `v_rcp_iflag_f32` gave `v_rcp_f32`'s word on every input, sweeps included. - rsq(-0) is -inf, sqrt(-0) is -0, log(±0) is -inf, sin and cos of ±inf are `0xffc00000`. Full table in the doc. ### The Vulkan witness For rcp, rsq, sqrt, log, exp (f32 and f16) and ldexp the driver emits the instruction alone, and the Vulkan words equal the ROCm words on every input and sweep, under the MODE each SPIR-V float mode maps to (`0xc0` default, `0xf0` denorm-preserve, `0x00` denorm-flush). Sine and cosine can't be compared directly. RADV multiplies the angle by 1/(2π) first, so the shader runs `v_mul_f32` then `v_sin_f32`. The test feeds the model the correctly rounded product; every f16 word is reproduced and every f32 word is inside the bound. Under denorm-preserve RADV wraps rcp, rsq, sqrt and log in a scale-by-2^24 sequence for denormal sources. The test models that sequence. One of them looks like a driver bug, not a hardware one: for `log2` the undo step multiplies by -24 where it should subtract 24, so `log2(2^-149)` comes back as 3000.0. I haven't looked for a Mesa report yet. I also ran `vulkan_approx.py` on a Radeon 890M (RDNA 3.5) out of curiosity about a newer generation. Every word matched the RX 6700 XT, sweeps included. That receipt isn't in this PR since the card isn't RDNA 2; I can add it if it's wanted. ### Against the translator and the ALU check Nothing in the translator or the check changes here. The doc has a table of what `vector.rs` emits for each instruction and where an exact IEEE host has to differ from the card. That part is from reading the code, not from running the translator. The ones that aren't just "one unit": - sin and cos: the translator's `sin((x - round(x)) * 6.28318530718)` gives sin(-0) = +0 where the card gives -0, a tiny nonzero value at half and quarter turns where the card gives exactly +0, and a slope six units above the card's for tiny angles. - A host that keeps f32 denormals differs on every denormal source (rcp, rsq, sqrt, log) and on results below 2^-126. The card flushes these under every MODE. - `v_exp_f16` is `exp2` in f32 then rounded to f16, which rounds twice: exp(`0x11c5`) gives `0x3c00`, the card `0x3c01`. - The ray tracing path (`ray/alu.rs`) still writes `sin(x * 6.283185307179586f)` without dropping whole turns, which the main translator fixed in 2a22586. For `check_basic_alu_gpu.py`: its 1-unit bounds for rcp, rsq and sqrt are as tight as they can be. The 4-unit bounds for log and exp and the 2^-20 trig bound are looser than the card needs, which the comment there says is for the hosts' own functions. Its inputs have no denormals, so it never meets the cases where its models and the card disagree. ### What's still open - One card for the full MODE sweep. The Vulkan run agrees with it but goes through the same GPU. - The f32 words are bounded, not predicted. - The gfx1030 assembler refuses the legacy and clamp forms (`v_rcp_legacy_f32`, `v_rsq_clamp_f32` and so on), so those aren't measured. `v_rcp_iflag_f32` has no GLSL route, so it has no Vulkan witness. - Same GFX10.3-only caveat as #420. ### Layout - `tools/native_rdna2/approx.hip`, `approx_inputs.py`, `check_approx.py`; `rdna2_float.approx()` and `within()` - `tools/native_rdna2/vulkan_approx.py`; `vulkan_float.py` gets a `Card.run_raw` split so both share the runner - `evidence/native-rdna2-approx.json` (197 KB), `evidence/native-rdna2-vulkan-approx.json` (697 KB) - `tests/test_native_rdna2_approx.py` and `tests/test_native_rdna2_vulkan_approx.py`, no GPU. The first reproduces the f16 tables under the console's MODE by default; `--exhaustive` does every MODE value in about a minute - a section in `docs/CONSOLE_REFERENCE.md`, the T5 row in `docs/ROADMAP.md`, the README in `tools/native_rdna2` ## Tested - [x] The checks relevant to this change passed - [x] Changed or new behaviour is covered by a Lanes scenario, or the reason it cannot be is stated below - [x] Where the table calls for AMD and NVIDIA, they passed on both. Otherwise, say which vendor still needs to run them: No Lanes scenario and no NVIDIA run, for the same reasons as #420 and #437: this runs native instructions outside TuxBox's providers, only on an RDNA 2 card, and changes no provider, translator or shared check code. GPU, driver and backend: AMD Radeon RX 6700 XT (`1002:73df`, gfx1031), Linux 7.3.6, ROCm under `/opt/rocm` with `HSA_OVERRIDE_GFX_VERSION=10.3.0` (set by the script); RADV, Mesa 26.3.1 for the Vulkan route. No Proton involved. Commands run and their results: ```text python tools/native_rdna2/check_approx.py --work build/native-rdna2-approx --output build/native-rdna2-approx.json PASS python tools/native_rdna2/vulkan_approx.py verify PASS python tools/native_rdna2/vulkan_float.py verify PASS (unchanged by the runner split) python -m unittest tests.test_native_rdna2_approx tests.test_native_rdna2_vulkan tests.test_native_rdna2_vulkan_approx tests.test_native_rdna2_float Ran 28 tests, OK (40 s) python -m unittest discover -s tests Ran 650 tests, OK (skipped=21) python tuxbox.py check --no-gpu passed Build the shader compiler passed Python tests passed Shader compiler tests passed DXBC tests passed XG layout tests passed Storage codec tests passed D3D12 Windows typecheck ``` ## Checks `docs/RESULTS.md` and `docs/progress.svg` don't change; the new receipts aren't a type those views count.
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
brcly/TuxBox!469
No description provided.