Skip to content

fract_f16_probe: v_fract_f16 rounds first, then the omod (SHADER-338) - #2198

Open
Sanonymous01 wants to merge 2 commits into
ItsNotRexed:mainfrom
Sanonymous01:measure/fract-f16-omod
Open

Sanonymous01 wants to merge 2 commits into
ItsNotRexed:mainfrom
Sanonymous01:measure/fract-f16-omod

Conversation

@Sanonymous01

Copy link
Copy Markdown
Contributor

SHADER-338 (claimed in #1902, listed in #2078). Depends on: #1970 (its commit is the first one here; this PR adds fe42df58).

docs/design.md ("Float min/max/med3 with NaN, v_fract") left open in which order v_fract_f16 rounds and applies mul:2/mul:4/div:2. This measures it exhaustively on RDNA2 and records the rule. There is no translator change: in the MODE the translator assumes, the modifier is ignored, which #1970 already implements.

Measured (gfx1036, raw kernels through tools/hw_oracle)

All 65,536 f16 inputs × 8 forms (no modifier, mul:2, mul:4, div:2, each also with clamp) × 4 MODEs (IEEE 0/1 × denorm_mode_16_64 0/3, DX10_CLAMP 1), in wave32, plus one 1,024-input block per MODE and form in wave64. That is 2,129,920 words, and rule() in the new tools/fract_f16_probe.py reproduces all of them:

  • The fract is rounded to f16 first, then the modifier scales it. The rounding includes the 0x3c00 → 0x3bff fix-up. So -2^-14 (0x8400) gives 0x3fff, 0x43ff and 0x37ff with mul:2, mul:4 and div:2. Scaling the exact 1 - 2^-15 and rounding once would give 0x4000, 0x4400 and 0x3800. 2,049 inputs tell the two orders apart.
  • The modifier acts only under IEEE 0 with f16 denormals flushed, as for the other f16 results (SHADER-135, Docs: SHADER-135 on RDNA2 (gfx1036): f16 omod and clamp, v_fma_mix* constants, NaNs, denormals and single rounding #1722). In the other three MODEs all eight forms give the no-modifier word, or the clamp word for the clamp forms.
  • Where the modifier acts, the product is exact except below 2^-14. There it is flushed to +0 before rounding: 0x07ff with div:2 gives +0, not 0x0400.
  • clamp comes last and gives +0 for a NaN.
  • Special values:
    • fract(±inf) is 0xfe00.
    • A NaN passes through. A signalling NaN is quieted under IEEE 1, keeping its payload, and kept under IEEE 0.
    • With f16 denormals flushed, a denormal input reads as zero.

The translator against the rule

The translator side runs in the MODE the translator assumes (IEEE 0, f16 denormals kept) through spirv_mini_interp. With #1970, --all gives 516,096/516,096 words that follow the rule. Two kinds of words are not compared bit for bit:

  • NaN results only have to be NaNs, because SPIR-V doesn't fix NaN bits.
  • NaN and infinity inputs under clamp are left out, because the interpreter refuses FClamp on a NaN (difference 2 in docs/shader_validation.md).

Without #1970 (upstream main), the translator scales the f32 fract before narrowing. That differs from the hardware on 51,199 inputs per modifier in the translator's MODE. Even against the MODE where the modifier acts, it differs on 4,095 to 5,118 non-NaN inputs. tests/test_fract_f16_probe.py fails against that translator and passes with #1970.

Files

  • tools/fract_f16_probe.py: the measured rule, with three sides:
    • rule: 21 hand-worked rows.
    • translator: a sample of inputs, or --all.
    • hw: with --oracle hw, the full sweep above.
  • tests/test_fract_f16_probe.py: the rule against hand-worked words, and the translator on one block of inputs (the negative denormals). It needs no GPU and runs in about 8 s.
  • docs/design.md: the "not measured" sentence is replaced by the rule.
  • docs/shader_validation.md: a new section "v_fract_f16 with an output modifier on RDNA2 (SHADER-338)".

#2139 edits the same design.md row, on the f32 side. Whichever lands second needs a one-line rebase, and I'll do it here if needed.

Testing

AI-assisted (Claude Code). No game files.

  • python -I tools/fract_f16_probe.py --oracle hw: rule 21/21, translator 26,208/26,208, hw 2,129,920/2,129,920, exit 0. Run on an AMD RDNA2 iGPU (gfx1036), AMD Windows driver 32.0.21044.64, Windows 11.
  • python -I tools/fract_f16_probe.py --all: translator 516,096/516,096, exit 0.
  • python -I tests/test_fract_f16_probe.py -v: 6/6. Against upstream's translator without Translator: no omod on f16 results, clamp kept, as RDNA2 with f16 denormals kept (SHADER-297) #1970 it fails, as intended.
  • python -I -m unittest discover -s tests/semantic: 1,671 OK.
  • pytest tests/test_f16_hw_words.py tests/test_fract_f16_probe.py tests/test_fma_mix_probe.py tests/test_docs_repo_paths.py: 69 passed.
  • regen_fixtures.py --check and compileall: clean.

🤖 Generated with Claude Code

@Rainmaker33

Copy link
Copy Markdown

Tested fe42df5 on NVIDIA GeForce RTX 3090 (Ampere, subgroup 32..32), driver 610.88 (Vulkan 1.4.341), Windows 11 Pro 26300: pass.

The PR's diff applied cleanly to main 4eb6155. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8; harness programs from a CMake build of that main; overlay layers off.

  • scripts/run_gpu_tests.py: 137/181 pass, 44 skipped (wave64), no failures (main: 136/180). ps_f16_omod and ps_fmac_f16_mods: 4096/4096 pixels exact.
  • The touched tests/semantic files (test_valu_fract_f16, test_valu_f16_round, test_f16_fma_rounding, test_rule_gpu_fixtures and others): OK.

@tumbione

Copy link
Copy Markdown
Contributor

Tested fe42df58 (includes #1970's commit) on NVIDIA GeForce RTX 5090, driver 616.56, Windows 11 (10.0.26300): pass

llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. Test programs built with scripts/build_runtime.ps1 from 4eb6155d, this branch's merge base; the branch changes no C++ or CMake, so they match this commit. No AMD hardware here, so the --oracle hw sweep was not run.

Run Result
run_gpu_tests.py --filter ps_f16_omod, 3 runs pass 3/3, 4096/4096 pixels exact
run_gpu_tests.py --filter ps_fmac_f16_mods, 3 runs pass 3/3, 4096/4096 pixels exact
Full run_gpu_tests.py 137/181 passed, 44 skipped, 0 failed (main 4eb6155d here: 136/180, 44 skipped)
python -I tests/test_fract_f16_probe.py 6 tests OK (about 10 s)
python -m pytest tests/test_f16_hw_words.py -q 43 passed
python -I -m unittest discover -s tests/semantic 1671 tests OK (2 skipped)
tests/test_shader_checks.py, regen_fixtures.py --check 60 OK, exit 0

No game data involved. AI-assisted (Claude Code): it ran everything above on this machine.

Sanonymous01 and others added 2 commits November 22, 2026 19:27
On RDNA2 an omod (mul:2 / mul:4 / div:2) on an f16 result acts only with
IEEE 0 and f16 denormals flushed. With f16 denormals kept, the MODE the
translator assumes, the hardware leaves the result as it is (gfx1036 raw
kernels, ItsNotRexed#1722). The translator scaled every f16 result anyway.

Gen.clamp_f16 keeps the clamp and drops the omod. It replaces clamp_mods
for the f16 results: v_fmac/fma_f16, v_med3/min3/max3_f16, the f16 VOP2
and VOP1 ops, v_sin/cos_f16, v_interp_p2_f16 and the mix ops (clamp only
there). v_fract_f16 now always gets its 0x3c00 -> 0x3bff fix-up.

The game decides nothing here. Over the 26,503 shaders of 1.0.2.1, none of
the 201,864 f16-result instructions carries an omod (102,365 f32 ones do),
and the only MODE writes are the 9,417 FP_ROUND ones. Translating the whole
corpus before and after gives 26,503 byte-identical modules, all passing
spirv-val.

New GPU fixture ps_f16_omod: four words, the same as the gfx1036 raw kernel
gives with f16 denormals kept (the scaled words with them flushed). The
tests that expected the omod now follow the hardware. design.md also loses
a stale duplicate of the v_fmac_f16 output-modifier row.

AI-assisted.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
docs/design.md left open in which order v_fract_f16 rounds and applies
mul:2 / mul:4 / div:2. Measured on gfx1036 (RDNA2, raw kernels) over all
65,536 f16 inputs, no modifier, the three omods and each with clamp, in
the four MODEs IEEE 0/1 x f16 denormals flushed/kept, wave32 plus a wave64
block each: 2,129,920 words, all reproduced by fract_f16_probe.rule().

- The fract is rounded to f16 first (with 0x3c00 -> 0x3bff), and the omod
  scales that: -2^-14 mul:2 gives 0x3fff, not 0x4000.
- The omod acts only under IEEE 0 with f16 denormals flushed, as for the
  other f16 results; its product flushes below 2^-14 before rounding
  (0x07ff div:2 is +0). clamp comes last; a NaN gives +0.
- fract(+-inf) is 0xfe00; an sNaN is quieted only under IEEE 1.

In the MODE the translator assumes (f16 denormals kept) the omod is
ignored, so with SHADER-297 (ItsNotRexed#1970, the first commit here) the translator
gives every non-NaN word the hardware gives. tools/fract_f16_probe.py
checks the rule against the translator and, with --oracle hw, the GPU;
tests/test_fract_f16_probe.py runs without one. No translator change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Sanonymous01
Sanonymous01 force-pushed the measure/fract-f16-omod branch from fe42df5 to 5653b18 Compare November 22, 2026 19:44
@Sanonymous01

Copy link
Copy Markdown
Contributor Author

Rebased on #1970's new head 80e070a1 over main 1628210 (head 5653b189). AI-assisted.

The conflicts were documentation only. In docs/design.md, this PR's sentence went into the "Float min/max/med3 with NaN, v_fract" row as main rewrote it this round, and main's other rows are unchanged. In docs/shader_validation.md, the new section now comes after main's new salu_diff.py section.

Tested on an RDNA2 iGPU (gfx1036), AMD driver 32.0.21044.64, Windows 11: pass.

  • python -I tools/fract_f16_probe.py --oracle hw: rule 21/21 hand-worked rows; translator 26,208/26,208 words; hardware 2,129,920/2,129,920 words follow the rule
  • tests/test_fract_f16_probe.py, tests/test_docs_repo_paths.py: 8 passed
  • python -I -m unittest discover -s tests/semantic: 4,142 OK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants