xshader: follow the card's MODE rules in the division steps #614

Merged
JohnVonWells merged 1 commit from upstream-pr/614 into main 2026-12-01 13:54:33 +00:00
JohnVonWells commented 2026-12-01 09:33:20 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JohnVonWells on 2026-12-01T09:33:20Z.


The f64 arithmetic, the division steps and v_trig_preop_f64 had only unit tests and a GPU check over five fixed inputs under MODE 0xc0, against Python floats. This adds a check that compares the translation with the card itself, and fixes the faults it found that the measurements settle.

The check

tools/native_rdna2/arith64.hip runs each form natively on a gfx1030 under all 1024 MODE values, over 4,096 seeded records: edge and random values, products and sums near the smallest normal, and quotients whose exponents are far apart. check_translated.py then translates one LLVM-assembled program with every form for each of 17 MODE values, runs it through D3D12 over 960 of those records, and compares every result word with the native one. No reference model is involved, so a difference can only come from the translation. The README has a section on it, and the receipt is evidence/native-rdna2-translated-arith64.json.

v_add_f64, v_mul_f64 and v_fma_f64 already matched in every word.

What the card does, and the fixes

  • v_div_fmas_f64 with VCC set scales by 2^128 when the exponent of S2 is at least 1024 (|S2| ≥ 2), else by 2^-128. The translation always scaled by 2^64. The card scales the exact result before its one rounding: in 438 lanes only a single rounding matched, and in none did only a double rounding match. The new tb_f64_fma_scaled adds the exponent before tb_f64_norm128, and tb_f64_fma calls it with k = 0.
  • v_div_fmas_f64 keeps denormal sources. MODE bit 0x40 doesn't change any of its results, so the helper passes mode | 0x40.
  • v_div_fmas_f32 scales by 2^64 or 2^-64 by the same rule. Only the direction is fixed; this form still rounds twice and still ignores MODE (see below).
  • The fixup forms (f16, f32, f64) return a signaling NaN source unchanged, and quiet it only when MODE.IEEE is set. This held in every lane with a NaN source, under every MODE value. The translation always quieted it.
  • v_trig_preop_f64 flushes a denormal result when MODE bit 0x80 is clear. The translation always rounded with MODE 0xc0.
  • The f16 dot products (v_dot2c_f32_f16, and v_dot2_f32_f16, which uses the same helper) return a NaN result quiet under every MODE value.

Differing words, out of 16,320 per form, on main and with this change:

Form main this change
v_div_fmas_f64, VCC set 12,782 0
v_div_fmas_f64, VCC clear 77 0
v_div_fmas_f32, VCC set 10,720 3,104
v_div_fixup_f64 2,319 1,787
v_div_fixup_f32 4,958 4,524
v_div_fixup_f16 1,423 989
v_trig_preop_f64 49 15
v_dot2c_f32_f16 1,479 595

Main is 717a74c4. The emitter on main is the negative control: the same inputs and MODE values, through the same check.

Still open

None of the remaining differences come from these rules:

  • v_div_scale_* scales and sets VCC for quotients the ISA text calls normal, so its thresholds need measuring.
  • The fixup forms: no remaining difference has a NaN source. They are overflow and underflow, where the card returns 0 or infinity.
  • v_div_fmas_f32 needs an exact f32 fma that follows MODE.
  • The dot products: 425 of the 595 remaining differences are the NaN the card picks, and 170 are the rounding of the last bit.
  • v_trig_preop_f64: the 15 remaining differences all come from one input.
  • v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximate on the card. The receipt reports their differences, but they don't fail the check.

Tests

  • division_steps_follow_the_measured_mode_rules fails without the change.
  • cargo test --lib: 706 passed.
  • Formatting and clippy: clean, apart from the existing unknown-lint warning.
  • python tuxbox.py check on an RX 6950 XT: all 17 steps passed, including the shader translator GPU checks f64 and rdna2-gaps.

Run on AMD only. A run on an NVIDIA card is still missing.

AI-assisted.

<!-- greyfinch-sync:brcly/TuxBox:issue:5582925486 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/614). Originally posted by [JohnVonWells](https://github.com/JohnVonWells) on 2026-12-01T09:33:20Z. --- The f64 arithmetic, the division steps and `v_trig_preop_f64` had only unit tests and a GPU check over five fixed inputs under MODE 0xc0, against Python floats. This adds a check that compares the translation with the card itself, and fixes the faults it found that the measurements settle. ## The check `tools/native_rdna2/arith64.hip` runs each form natively on a gfx1030 under all 1024 MODE values, over 4,096 seeded records: edge and random values, products and sums near the smallest normal, and quotients whose exponents are far apart. `check_translated.py` then translates one LLVM-assembled program with every form for each of 17 MODE values, runs it through D3D12 over 960 of those records, and compares every result word with the native one. No reference model is involved, so a difference can only come from the translation. The README has a section on it, and the receipt is `evidence/native-rdna2-translated-arith64.json`. `v_add_f64`, `v_mul_f64` and `v_fma_f64` already matched in every word. ## What the card does, and the fixes - **`v_div_fmas_f64` with VCC set** scales by 2^128 when the exponent of S2 is at least 1024 (|S2| ≥ 2), else by 2^-128. The translation always scaled by 2^64. The card scales the exact result before its one rounding: in 438 lanes only a single rounding matched, and in none did only a double rounding match. The new `tb_f64_fma_scaled` adds the exponent before `tb_f64_norm128`, and `tb_f64_fma` calls it with k = 0. - **`v_div_fmas_f64` keeps denormal sources.** MODE bit 0x40 doesn't change any of its results, so the helper passes `mode | 0x40`. - **`v_div_fmas_f32`** scales by 2^64 or 2^-64 by the same rule. Only the direction is fixed; this form still rounds twice and still ignores MODE (see below). - **The fixup forms** (f16, f32, f64) return a signaling NaN source unchanged, and quiet it only when MODE.IEEE is set. This held in every lane with a NaN source, under every MODE value. The translation always quieted it. - **`v_trig_preop_f64`** flushes a denormal result when MODE bit 0x80 is clear. The translation always rounded with MODE 0xc0. - **The f16 dot products** (`v_dot2c_f32_f16`, and `v_dot2_f32_f16`, which uses the same helper) return a NaN result quiet under every MODE value. Differing words, out of 16,320 per form, on main and with this change: | Form | main | this change | |---|---:|---:| | `v_div_fmas_f64`, VCC set | 12,782 | 0 | | `v_div_fmas_f64`, VCC clear | 77 | 0 | | `v_div_fmas_f32`, VCC set | 10,720 | 3,104 | | `v_div_fixup_f64` | 2,319 | 1,787 | | `v_div_fixup_f32` | 4,958 | 4,524 | | `v_div_fixup_f16` | 1,423 | 989 | | `v_trig_preop_f64` | 49 | 15 | | `v_dot2c_f32_f16` | 1,479 | 595 | Main is 717a74c4. The emitter on main is the negative control: the same inputs and MODE values, through the same check. ## Still open None of the remaining differences come from these rules: - **`v_div_scale_*`** scales and sets VCC for quotients the ISA text calls normal, so its thresholds need measuring. - **The fixup forms:** no remaining difference has a NaN source. They are overflow and underflow, where the card returns 0 or infinity. - **`v_div_fmas_f32`** needs an exact f32 fma that follows MODE. - **The dot products:** 425 of the 595 remaining differences are the NaN the card picks, and 170 are the rounding of the last bit. - **`v_trig_preop_f64`:** the 15 remaining differences all come from one input. - **`v_rcp_f64`, `v_rsq_f64` and `v_sqrt_f64`** are approximate on the card. The receipt reports their differences, but they don't fail the check. ## Tests - `division_steps_follow_the_measured_mode_rules` fails without the change. - `cargo test --lib`: 706 passed. - Formatting and clippy: clean, apart from the existing unknown-lint warning. - `python tuxbox.py check` on an RX 6950 XT: all 17 steps passed, including the shader translator GPU checks `f64` and `rdna2-gaps`. Run on AMD only. A run on an NVIDIA card is still missing. AI-assisted.
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!614
No description provided.