xshader: match the card in every exact division step and f16 dot product #615

Merged
JohnVonWells merged 1 commit from upstream-pr/615 into main 2026-12-01 13:55:11 +00:00
JohnVonWells commented 2026-12-01 10:21:18 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JohnVonWells on 2026-12-01T10:21:18Z.


This follows #614: it closes every difference that PR's check still found. Every form that check compares now matches the card's word, except the three the card itself only approximates. Each rule below was measured natively on a gfx1030 and is now in the translation. Builds on #614; until that merges, this branch also holds its commit.

What the card does

v_div_scale_f32 / _f64. Measured over all 65,536 f32 exponent pairs with edge and random mantissas (1,572,864 words), and over every f64 exponent against each boundary region plus 100,000 random pairs (4,535,808 words). The final model reproduces every word.

  • The tests compare the raw exponent fields. A denormal counts as exponent 0 and isn't normalized first.
  • A quotient is out of range from an exponent difference of 96 (f64: 768) either way. The translation used -126 / -1022 on the low side.
  • An S1 exponent of 253 (2045) or more, whatever the mantissa, counts as a denormal reciprocal. When that holds together with a tiny quotient, the denominator form is scaled down, not up.
  • A zero S1 or S2 gives 0xffc00000 (f64: 0xfff8…), with VCC still from the exponents.
  • MODE's denormal bits flush the sources and the result.

v_div_fixup_*. Fits all 4,096 records under all 1,024 MODE values, in each width.

  • MODE flushes S1 and S2, but never S0.
  • A quotient underflows at an exponent difference of -151 (f64: -1076) or less. This was measured densely around the boundary, and the mantissa doesn't matter. The f16 form has no such test.
  • An infinite or NaN S0 overflows.
  • Both underflow and overflow round by MODE: the smallest denormal or zero, infinity or the largest finite.

v_div_fmas_f32 scales by 2^±64 before its one rounding, keeps denormal sources and follows MODE, like the f64 form in #614. It had been a native mad and a multiply. The new tb_f32_fma_scaled is exact, on the 128-bit helpers of the f64 code.

v_fma_f64, v_mul_f64, v_add_f64 with f64 output denormals off. The new boundary inputs and the round modes with flushing (0x004, 0x008, 0x00c) found three faults in tb_f64_norm128 and the shortcuts:

  • A result just below 2^-1022 that rounds up to it is kept. The card detects tininess after rounding; the translation flushed it.
  • Every other tiny result is a signed zero under every round mode. The translation returned the smallest denormal under round-up.
  • 0 + b and 0 * x + c flush a denormal b or c.

v_fma_f64: zero times infinity gives the default NaN even when S2 is a NaN. This was measured with true zeros, signaling NaNs and under every MODE value.

v_trig_preop_f64 holds 1,184 bits of 2/π, where the translation's table had 1,216. Over 25,600 extra inputs, 1,184 bits gave 0 differences and 1,216 gave 35.

v_dot2_f32_f16 / v_dot2c_f32_f16. MODE doesn't change any result.

  • The exact sum of both products and S2 is rounded once to nearest even. The translation rounded the f32 additions separately.
  • A denormal S2 is flushed.
  • The result is the first NaN of S0.lo, S1.lo, S0.hi, S1.hi and S2, made quiet.

The model matched all 4,096 records under three MODE values, plus 19,200 extra cases: zeros, signs, f16 denormals and tiny results. The new helper needs the f64 helpers, so output.rs adds them when an f16 dot product is present. The VOP3P form with negation, op_sel or clamp still takes the float path.

The check

Changes to check_translated.py:

  • Two new input classes sit on the division scaling boundaries, and dot products now include cancelling products.
  • It covers 23 MODE values instead of 17, among them every round mode with denormals flushed.
  • It translates every record, not only the first 960.

Results, in words differing out of 93,840 per form:

main this branch
v_add_f64 20 0
v_mul_f64 627 0
v_fma_f64 244 0
v_div_scale_f64 (value, VCC; numerator and denominator forms) 5,079–7,498 0
v_div_scale_f32 (same) 17,848–29,695 0
v_div_fmas_f64, VCC clear / set 1,159 / 71,513 0 / 0
v_div_fmas_f32, VCC clear / set 14,606 / 58,480 0 / 0
v_div_fixup_f64 / _f32 / _f16 17,110 / 28,174 / 13,204 0 / 0 / 0
v_trig_preop_f64 803 0
v_dot2c_f32_f16, v_dot2_f32_f16 10,718 each 0

The main column is the negative control: main's emitter (717a74c4), through the same inputs and MODE values. v_rcp_f64, v_rsq_f64 and v_sqrt_f64 are approximate on the card and still differ; the check reports them without failing.

Tests

  • division_scale_follows_the_measured_thresholds, division_fixup_follows_the_measured_ranges and f64_arithmetic_flushes_after_rounding fail without the change.
  • division_steps_follow_the_measured_mode_rules is updated for the exact f32 fma, the shorter table and the exact dot product.
  • cargo test --lib: 709 passed.
  • Formatting: clean. Clippy: only 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:5583688744 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/615). Originally posted by [JohnVonWells](https://github.com/JohnVonWells) on 2026-12-01T10:21:18Z. --- This follows #614: it closes every difference that PR's check still found. Every form that check compares now matches the card's word, except the three the card itself only approximates. Each rule below was measured natively on a gfx1030 and is now in the translation. Builds on #614; until that merges, this branch also holds its commit. ## What the card does **`v_div_scale_f32` / `_f64`.** Measured over all 65,536 f32 exponent pairs with edge and random mantissas (1,572,864 words), and over every f64 exponent against each boundary region plus 100,000 random pairs (4,535,808 words). The final model reproduces every word. - The tests compare the raw exponent fields. A denormal counts as exponent 0 and isn't normalized first. - A quotient is out of range from an exponent difference of 96 (f64: 768) either way. The translation used -126 / -1022 on the low side. - An S1 exponent of 253 (2045) or more, whatever the mantissa, counts as a denormal reciprocal. When that holds together with a tiny quotient, the denominator form is scaled down, not up. - A zero S1 or S2 gives `0xffc00000` (f64: `0xfff8…`), with VCC still from the exponents. - MODE's denormal bits flush the sources and the result. **`v_div_fixup_*`.** Fits all 4,096 records under all 1,024 MODE values, in each width. - MODE flushes S1 and S2, but never S0. - A quotient underflows at an exponent difference of -151 (f64: -1076) or less. This was measured densely around the boundary, and the mantissa doesn't matter. The f16 form has no such test. - An infinite or NaN S0 overflows. - Both underflow and overflow round by MODE: the smallest denormal or zero, infinity or the largest finite. **`v_div_fmas_f32`** scales by 2^±64 before its one rounding, keeps denormal sources and follows MODE, like the f64 form in #614. It had been a native `mad` and a multiply. The new `tb_f32_fma_scaled` is exact, on the 128-bit helpers of the f64 code. **`v_fma_f64`, `v_mul_f64`, `v_add_f64` with f64 output denormals off.** The new boundary inputs and the round modes with flushing (`0x004`, `0x008`, `0x00c`) found three faults in `tb_f64_norm128` and the shortcuts: - A result just below 2^-1022 that rounds up to it is kept. The card detects tininess after rounding; the translation flushed it. - Every other tiny result is a signed zero under every round mode. The translation returned the smallest denormal under round-up. - `0 + b` and `0 * x + c` flush a denormal b or c. **`v_fma_f64`:** zero times infinity gives the default NaN even when S2 is a NaN. This was measured with true zeros, signaling NaNs and under every MODE value. **`v_trig_preop_f64`** holds 1,184 bits of 2/π, where the translation's table had 1,216. Over 25,600 extra inputs, 1,184 bits gave 0 differences and 1,216 gave 35. **`v_dot2_f32_f16` / `v_dot2c_f32_f16`.** MODE doesn't change any result. - The exact sum of both products and S2 is rounded once to nearest even. The translation rounded the f32 additions separately. - A denormal S2 is flushed. - The result is the first NaN of S0.lo, S1.lo, S0.hi, S1.hi and S2, made quiet. The model matched all 4,096 records under three MODE values, plus 19,200 extra cases: zeros, signs, f16 denormals and tiny results. The new helper needs the f64 helpers, so `output.rs` adds them when an f16 dot product is present. The VOP3P form with negation, op_sel or clamp still takes the float path. ## The check Changes to `check_translated.py`: - Two new input classes sit on the division scaling boundaries, and dot products now include cancelling products. - It covers 23 MODE values instead of 17, among them every round mode with denormals flushed. - It translates every record, not only the first 960. Results, in words differing out of 93,840 per form: | | main | this branch | |---|---:|---:| | `v_add_f64` | 20 | 0 | | `v_mul_f64` | 627 | 0 | | `v_fma_f64` | 244 | 0 | | `v_div_scale_f64` (value, VCC; numerator and denominator forms) | 5,079–7,498 | 0 | | `v_div_scale_f32` (same) | 17,848–29,695 | 0 | | `v_div_fmas_f64`, VCC clear / set | 1,159 / 71,513 | 0 / 0 | | `v_div_fmas_f32`, VCC clear / set | 14,606 / 58,480 | 0 / 0 | | `v_div_fixup_f64` / `_f32` / `_f16` | 17,110 / 28,174 / 13,204 | 0 / 0 / 0 | | `v_trig_preop_f64` | 803 | 0 | | `v_dot2c_f32_f16`, `v_dot2_f32_f16` | 10,718 each | 0 | The main column is the negative control: main's emitter (717a74c4), through the same inputs and MODE values. `v_rcp_f64`, `v_rsq_f64` and `v_sqrt_f64` are approximate on the card and still differ; the check reports them without failing. ## Tests - `division_scale_follows_the_measured_thresholds`, `division_fixup_follows_the_measured_ranges` and `f64_arithmetic_flushes_after_rounding` fail without the change. - `division_steps_follow_the_measured_mode_rules` is updated for the exact f32 fma, the shorter table and the exact dot product. - `cargo test --lib`: 709 passed. - Formatting: clean. Clippy: only 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.
1093i3511 commented 2026-12-01 17:20:35 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by 1093i3511 on 2026-12-01T17:20:35Z.


@JohnVonWells

Since #615 (7327dab4), 3 ProjectScarlett programs fail to compile (12 requests) in a Series S prepare on fb17e160:

error: use of undeclared identifier 'tb_u128_sub'

DOT_HELPERS (text/helper_code.rs) is pasted into every program that uses any v_dot*. Since #615 it holds tb_dot2_f16, which calls the tb_u128_* helpers and tb_f32_norm128. Those come from float64_arith::HELPERS, which emitter/output.rs adds only for v_dot2_f32_f16 and v_dot2c_f32_f16. A program with only other dot products (here the integer ones) gets tb_dot2_f16 without them, and DXC rejects the undeclared names even though nothing calls the function. Translating one of them (7baa6a35…) shows tb_dot2_f16 at line 157 and no tb_u128_* definition anywhere.

The other two are 6da9859f… and 1d52e9cf…. Two possible fixes: add float64_arith::HELPERS whenever DOT_HELPERS goes in, or move tb_dot2_f16 out of DOT_HELPERS into the block that is added for the f32 dot products. The morning prepare on 717a74c4 (before #615) had 0 failures.

<!-- greyfinch-sync:brcly/TuxBox:comment:7319146265 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/615#issuecomment-7319146265). Originally posted by [1093i3511](https://github.com/1093i3511) on 2026-12-01T17:20:35Z. --- @JohnVonWells Since #615 (7327dab4), 3 ProjectScarlett programs fail to compile (12 requests) in a Series S prepare on fb17e160: ``` error: use of undeclared identifier 'tb_u128_sub' ``` `DOT_HELPERS` (`text/helper_code.rs`) is pasted into every program that uses any `v_dot*`. Since #615 it holds `tb_dot2_f16`, which calls the `tb_u128_*` helpers and `tb_f32_norm128`. Those come from `float64_arith::HELPERS`, which `emitter/output.rs` adds only for `v_dot2_f32_f16` and `v_dot2c_f32_f16`. A program with only other dot products (here the integer ones) gets `tb_dot2_f16` without them, and DXC rejects the undeclared names even though nothing calls the function. Translating one of them (`7baa6a35…`) shows `tb_dot2_f16` at line 157 and no `tb_u128_*` definition anywhere. The other two are `6da9859f…` and `1d52e9cf…`. Two possible fixes: add `float64_arith::HELPERS` whenever `DOT_HELPERS` goes in, or move `tb_dot2_f16` out of `DOT_HELPERS` into the block that is added for the f32 dot products. The morning prepare on 717a74c4 (before #615) had 0 failures.
JohnVonWells commented 2026-12-01 17:56:43 +00:00 (Migrated from github.com)

Imported from brcly/TuxBox. Originally posted by JohnVonWells on 2026-12-01T17:56:43Z.


@1093i3511 Thanks, you're right, and sorry for the breakage. The fix is in #631.

<!-- greyfinch-sync:brcly/TuxBox:comment:7320113274 --> Imported from [brcly/TuxBox](https://github.com/brcly/TuxBox/pull/615#issuecomment-7320113274). Originally posted by [JohnVonWells](https://github.com/JohnVonWells) on 2026-12-01T17:56:43Z. --- @1093i3511 Thanks, you're right, and sorry for the breakage. The fix is in #631.
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!615
No description provided.