Shaders: preserve NaN bits in f16 transcendentals #3717
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3717"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-28T10:47:23Z.
Preserves source NaN bit patterns (sign and payload, including signalling NaNs) in the seven 16-bit floating-point transcendentals (
v_rcp_f16,v_sqrt_f16,v_rsq_f16,v_log_f16,v_exp_f16,v_sin_f16,v_cos_f16).Problem
Measured natively on RDNA2 (
gfx1031full 65,536-input sweep across wave32/wave64, IEEE off), these seven instructions pass input NaNs (both signalling and quiet) through with their sign and payload preserved after sourceabs/negmodifiers. Previously, lowering converted the result throughnarrow16, which quieted signalling NaNs (for examplev_rcp_f16(0x7c01)produced0x7e01instead of0x7c01) and could alter NaN payloads on the host.Changes
crates/victory-shader/src/emit/lower.rs(522,761 B<524,288 B): addhalf_fsrc_bitsto retain the 16-bit source bits alongside the f32 value; for the seven f16 transcendentals withoutclamp, apply bit-levelabs/negto the source halfword and select it when(nan & 0x7FFF) > 0x7C00. Theclamppath and rules covered by#3206,#3609,#3617,#3365are left untouched.crates/victory-shader/src/emit/valu_tests.rs: addf16_transcendentals_preserve_nan_bitscovering0x7c01,0xfc35,0x7e42, and0xfe7facross all seven instructions.Verification
upstream/main63bcca4, confirmed in fork negative CI46131133362and locally):f16_transcendentals_preserve_nan_bitsfails (v_rcp_f16(0x7c01)=0x7e01, want 0x7c01).cargo test --locked -p victory-shader --lib --features host -- f16_transcendentals_preserve_nan_bits: 1 passed (local hardware adapter + WARP).transcendental,v_sin_and_v_cos,f16_results_roundall PASS.cargo clippy --locked -p victory-shader --all-targets --features host -- -D warnings: clean (exit 0).cargo run --locked -p xtask -- policy:asset policy: 191 files ok.AI-assisted; synthetic tests only, no game files.
— Devin & Neon
Imported from Marvisak/VIctory. Originally posted by cloph-dsp on 2026-11-28T11:38:20Z.
Independent local verification (Windows, Rust 1.100.0/MSVC, no game data):
cb593fcf6651068eb796eec471f64d6c7fbba0c6, base/parent63bcca43c6a00a758510398fa43096ce7e4f7df0.cargo test --locked -p victory-shader --lib -- emit::valu_tests::f16_transcendentals_preserve_nan_bits --exact --test-threads=1 --quiet→ 1 passed, 0 failed, 305 filtered, exit 0.git grep -Ffor the test name on the base returned no match (test is new on this PR);git diff --check 63bcca4 HEADclean (2 files, +89/-5).CARGO_TARGET_DIR(never shared) withCARGO_BUILD_JOBS=2.AI-assisted (Hy4 flash).
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T12:27:42Z.
I combined your exact
cb593fcwith #37295f57f5ein integration commitd1b369e. Both commits are direct parents.The extra WARP test covers reciprocal and square root with all four
abs/negcombinations, both source and destination half selections, and four signaling or quiet NaN patterns. In all 128 cases, the expected sign and payload matched and the untouched destination half kept its sentinel value.Your seven-operation regression, the new modifier test, and the inverse-bitcast regression all passed on Windows WARP with Agility 1.620.2. Strict shader Clippy and the asset policy passed. @Moon0755, the integration test is available for merge coordination. Please retain the original Git authors on cherry-pick or rebase.
AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T14:58:08Z.
NVIDIA row for this PR: it passes on Ampere, and the test fails on this card without the fix.
RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head
cb593fcon main63bcca4(one commit, clean).f16_transcendentals_preserve_nan_bits: 1 passed, 0 failed, in 3 of 3 runs. It ran on both the hardware adapter and WARP: no "skipped" line.Negative control: the same test with main's
crates/victory-shader/src/emit/lower.rs(git diff --stat HEAD: 1 file, 5 insertions, 63 deletions) fails in 3 of 3 runs, on the hardware adapter, at the first case:hardware: v_rcp_f16(0x7c01) = 0x7fff, want 0x7c01. On this card main returns the canonical0x7fff, not the0x7e01the description quotes; with the PR the source bits come back on both adapters.cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index:63bcca4: 298 passed, 3 failed, 3 ignored.The 3 failures are the same on both:
stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zerosandsynthetic_tests::gathers_at_a_level_match_the_hardware. The skipped test crashes the binary on this card (open issue #2571).Not tested: a title run; the combination with #3729.
AI-assisted (Claude Code, Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-28T15:51:49Z.
Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head
cb593fcon63bcca4.f16_transcendentals_preserve_nan_bitspasses on the hardware adapter and on WARP; neither was skipped.victory-shader --lib --features host: 303 passed, 0 failed, 3 ignored.AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-28T21:56:22Z.
AMD Radeon 780M (RDNA3) + WARP, Windows 11, head
cb593fc. Before (main63bcca4+ this PR's test): fails on the hardware adapter,v_rcp_f16(0x7c01)gives0x7e01(quieted) where0x7c01is wanted. After: passes, 5 of 5 runs. Full suite: 552 passed. AI-assisted (Claude).Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T22:04:09Z.
I tested #3717 and #3617 together at their exact heads
cb593fcand8bfad4c. Git merged them cleanly, but the new NaN path returned before the rounding rule in #3617 was reached. The plain merge produced 14 mismatches across 168 WARP readbacks, all for finite inputs withFP_ROUND=3.c6dbf69applies nearest-even rounding inside the transcendental path. The extended test mixes finite inputs and four signaling or quiet NaNs in the same shader for each of the seven operations, withFP_ROUNDset to 0 and 3. On WARP 1.0.22 with Agility 1.620.2, all 168 readbacks matched, including the untouched destination half. The existing NaN and FP32-to-FP16 conversion controls also passed.Strict shader Clippy passed with default features and all features. The asset policy passed with
lower.rsat 522,941 bytes.Integration branch. Please retain the Git authors on cherry-pick or rebase, or add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>if folding my changes into another commit.AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T22:07:08Z.
@Moon0755, I checked this PR at
cb593fccombined with #3617 at8bfad4c. Git merged both exact heads into 15edfeb. With FP_ROUND=3, that tree fails the half-transcendental rounding test for all seven opcodes. The reciprocal case returns 0x2b87 where 0x2b88 is expected. The NaN regression passes on the same tree.Follow-up 15f2978 puts nearest-even rounding in the seven-operation path that also preserves source NaN bits. Other half results keep their MODE rounding. Both PR heads remain parents of the merge below the fix.
Both regressions passed on signed WARP 1.0.22 with Agility 1.620.2 and DXIL validation. Four adjacent rounding and sine/cosine tests, all 33 lowerer tests, both strict shader Clippy configurations and the asset policy passed. The combined lowerer is 522,948 bytes. Your native report and the recent Radeon 780M runs tested the individual PR heads. The next step is author review and a native run of the comparison branch. #3717 Radeon report · #3617 Radeon report
Please retain the Git authors on cherry-pick or rebase, or include Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding the fix into another commit.
AI-assisted with OpenAI Codex.
Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-28T22:18:31Z.
@andrewpozdnakov7-jpg native run of both comparison branches on AMD Radeon 780M (RDNA3, driver 32.0.13022.3006, Windows 11), each tree built in its own target directory:
half_transcendentals_round_to_nearest_in_both_modesf16_transcendentals_preserve_nan_bits15edfeb15f2978(codex2/pr3717-rounding-nan)c6dbf69(review/3617-3717-f16-transcendentals)On the plain merge the hardware fails on more than reciprocal:
rcp(0x4c40)gives0xa55a2b87(expected…2b88, as on WARP), andsqrt(0x3200)gives0xa55a36ed(expected…36ee), both with FP_ROUND=3. The untouched destination half0xa55ais kept in every case. Both fixes clear it, and all 37emit::valu_testspass on15f2978.AI-assisted (Claude).
Imported from Marvisak/VIctory. Originally posted by Moon0755 on 2026-11-28T22:43:18Z.
@andrewpozdnakov7-jpg thank you, you are right.
32a02ebis on this PR now.Reproduced. The plain merge of
cb593fcand8bfad4cfailshalf_transcendentals_round_to_nearest_in_both_modeshere on WARP, on an RTX 2060 and on an AMD Vega iGPU (rcp(0x4c40)withFP_ROUND=3:0x2b87for0x2b88), and the NaN test passes. The path that keeps NaN bits returned throughnarrow16before #3617's rule was reached.The change.
32a02ebtakes thelower.rschange of yourc6dbf69as it is (the file is byte-identical to yours) withCo-authored-byfor you: in the path that keeps NaN bits, the seven instructions narrow to nearest even whateverFP_ROUNDsays. It does not bring #3617 itself into this PR. A new test,f16_transcendentals_keep_nearest_rounding_with_nan_inputs, pins it atFP_ROUND=3. The rule is Leonida's sweep on an RX 6700 XT (a73a57e, all 65,536 inputs: the f16 field ofFP_ROUNDis ignored).Together. This head merged with #3617
8bfad4cpasses all three tests on WARP, the RTX 2060 and the Radeon, so either PR can go in first.lower.rsis 522,941 bytes here (+180). Merged with #3617 it is 523,395, because #3617's rule infloat_resultcan then no longer be reached for these seven; yourc6dbf69removes it and gets back to 522,941. That clean-up fits whichever of the two lands second.Your note on #3716 is right as well: #3716, #3617 and this one together pass the size limit. That needs code moved out of
lower.rs, which is not for this PR.— Devin & Fable
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-28T23:39:39Z.
Follow-up to my NVIDIA report above, at the new head:
32a02ebfixes the interaction with #3617 on the RTX 3070 too.RTX 3070 (desktop), driver 616.56, Windows 11 Pro 10.0.26100, debug. Head
32a02ebon main63bcca4(two commits, clean).f16_transcendentals_preserve_nan_bitsandf16_transcendentals_keep_nearest_rounding_with_nan_inputs: 2 passed, 0 failed, 0 ignored, in 3 of 3 default runs (hardware adapter and WARP, no "skipped" line) and in 3 of 3 runs withVICTORY_WARP=1.8bfad4c(--no-ff;lower.rsis 523,395 bytes, as you measured): those two plushalf_transcendentals_round_to_nearest_in_both_modes: 3 passed, 0 failed, 0 ignored, in 3 of 3 default runs and 3 of 3VICTORY_WARP=1runs.cb593fcmerged with8bfad4c, once each:half_transcendentals_round_to_nearest_in_both_modesfails on the hardware adapter and on WARP, for all seven opcodes withFP_ROUND=3, one ulp low (rcp(0x4c40):0xa55a2b87, expected0xa55a2b88;sqrt(0x3200):0xa55a36ed, expected0xa55a36ee). The untouched high half is kept, andf16_transcendentals_preserve_nan_bitspasses on that tree. So this card showed the same failure as the RTX 2060 and the Vega.cargo test --locked -p victory-shader --lib -- --skip heap_direct_texture_reads_mark_their_heap_index: 300 passed, 3 failed, 3 ignored (atcb593fcit was 299 passed). The 3 failures are the same ones main has on this card: the twostride_testsandsynthetic_tests::gathers_at_a_level_match_the_hardware.AI use: Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-29T18:48:21Z.
RDNA1 row for the new head
32a02eb: RX 5700 XT (Navi 10, driver 32.0.21045.29003), Windows 11 26200. The fixture runs the hardware adapter and WARP in turn, and neither was skipped.f16_transcendentals_preserve_nan_bits+f16_transcendentals_keep_nearest_rounding_with_nan_inputshalf_transcendentals_round_to_nearest_in_both_modes32a02ebalone32a02ebmerged with #36178bfad4c(--no-ff,ef92bd2)Full
victory-shader --lib --features hostat32a02eb: 304 passed, 0 failed, 3 ignored. A third run of the PR head alone didn't build because of a local file lock, so it's left out.With Moon0755's RTX 2060 and Vega iGPU rows, upipibi's RTX 3070 and kilolimaVI's 780M, the fixed interaction with #3617 now checks out on RDNA1 too.
AI use: Claude Code (Claude Opus 5.5).
Imported from Marvisak/VIctory. Originally posted by passerbyyhx on 2026-11-30T23:21:08Z.
Exhaustive half-NaN payload readback on
32a02eb33fdcbe3abe669762305cb5541eff827c: each of the seven guest operations ran on all 2,046 binary16 NaN encodings. The RTX 4090 Laptop GPU and Microsoft WARP each matched all 14,322 expected bit patterns. With the same fixture on the clean base63bcca43c6a00a758510398fa43096ce7e4f7df0, the mismatch counts were 14,315 and 7,154 respectively. For example, reciprocal of0x7c01returned0x7fffon the base 4090 path and0x7e01on base WARP. This checks NaN payload bits on the updated head; the finite-input rounding checks in the other reports cover a different case.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.