Skip to content

rdna2spirv: v_ldexp_f64 follows a MODE.FP_ROUND toward-zero write - #6218

Open
cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:ldexp-f64-mode-rtz
Open

cynbun wants to merge 1 commit into
ItsNotRexed:mainfrom
cynbun:ldexp-f64-mode-rtz

Conversation

@cynbun

@cynbun cynbun commented Nov 23, 2026

Copy link
Copy Markdown

Coordination: finding and claim. The f64 sibling of #5310 (which fixes v_ldexp_f16 only); no overlap with its hunks.

What was wrong

FP_ROUND[3:2] is the round mode of the double- and half-precision results (RDNA2 ISA, MODE register). The translator tracks it per invocation in %mode_r16 (docs/design.md, Known approximations: after s_setreg_imm32_b32 hwreg(HW_REG_MODE, 2, 2), 3 the f16 results and v_add_f64 round toward zero, a finite overflow gives the largest finite value; v_add_f64 was measured that way on an RX 6800). The v_ldexp_f64 branch (tools/rdna2spirv.py:10602 on 561b65a8) never read %mode_r16, so it emitted the same code in both modes. v_ldexp_f64 (D = S0 * 2^S1) only rounds when the result leaves the normal range, and both of those cases came out wrong toward zero:

v_ldexp_f64 v[0:1], v[2:3], v4 after the toward-zero write S1 expected main this PR
0x7fefffff_ffffffff (DBL_MAX) 1 0x7fefffff_ffffffff 0x7ff00000_00000000 (inf) 0x7fefffff_ffffffff
0xffefffff_ffffffff 1 0xffefffff_ffffffff 0xfff00000_00000000 0xffefffff_ffffffff
0x3ff00000_00000003 -1023 0x00080000_00000001 0x00080000_00000002 0x00080000_00000001
0x3fefffff_ffffffff -1022 0x000fffff_ffffffff 0x00100000_00000000 0x000fffff_ffffffff
0x3ff80000_00000000 -1075 0x00000000_00000000 0x00000000_00000001 0x00000000_00000000

Without a MODE write (or after writing 0 back) nothing changes: overflow is ±inf and a subnormal result rounds to nearest even, as measured on gfx1036 (the existing row).

Fix (tools/rdna2spirv.py, the v_ldexp_f64 branch only)

Only when the program writes the field (Gen.round_mode), as #5310 does for f16. While %mode_r16 is 3:

  • an overflow gives sign | 0x7FEFFFFF_FFFFFFFF instead of sign | 0x7FF00000_00000000;
  • the subnormal path never rounds up (truncation), so no carry into the smallest normal either.

clamp still applies after the rounding (+DBL_MAX clamps to 1.0, as +inf did). NaN, inf and zero inputs pass unchanged. A program without a MODE write gets the same SPIR-V as before.

Reference and confirmation

  • RDNA2 ISA, MODE register: FP_ROUND[3:2] is the double/half-precision round mode; V_LDEXP_F64 is a double result (IEEE 754 scaleB rounds by the rounding-direction attribute). The same field already drives v_add_f64 in the translator, measured on an RX 6800.
  • Not measured on RDNA2 for this op. tools/mode_round_probe.py gets two cases (ldexp_f64_ovf, ldexp_f64_sub) an RDNA2 owner can run with python -I tools/mode_round_probe.py; the new test checks that the probe's words are the translator's.
  • docs/design.md: the v_ldexp_f64 contract row says this (I left the Known approximations row alone, since rdna2spirv: v_ldexp_f16 follows a MODE.FP_ROUND toward-zero write #5310 edits it).

Tests

  • New tests/semantic/test_ldexp_f64_mode_round.py, hand-written programs: 18 cases (overflow both signs, S1 clamped to 4096, inf/NaN/-0, the subnormal round-up, tie, carry-into-min-normal and below-min-subnormal cases in both modes, exact results), an exact fractions.Fraction reference checked against the cases and a seeded sweep (overflow edge, subnormal range, subnormal operands) in both modes, switching back to nearest even with a source neg, clamp after the toward-zero overflow, and the probe words. 13 of its checks fail on main, all pass here.
  • GPU: the same 16 inputs (8 per mode) as a wave32 compute program on an NVIDIA GeForce RTX 5080 (driver 615.78.08, Linux) through vk_compute_test: every word equals the CPU result (toward-zero words wrong on main, right here). spirv-val --target-env vulkan1.2 clean.
  • python -m unittest discover -s tests/semantic: 6156 tests OK (1 skipped, 1 expected failure, as on main). Also tests/test_mode_round.py (pytest, 74 passed), tests/test_mode_round_probe.py, tools/mode_round_probe.py --self-test, test_design_contract_rows, test_docs_design_rows, test_docs_table_rows, test_docs_repeated_lines, test_semantic_translator_use, test_text_write_line_endings, compileall.

No game data used. AI-assisted (Claude Code).

🤖 Generated with Claude Code

FP_ROUND[3:2] is the round mode of the double- and half-precision results (RDNA2 ISA, MODE register), and the
translator tracks it in %mode_r16 for the f16 results and v_add_f64. The v_ldexp_f64 branch never read it, so after
s_setreg_imm32_b32 hwreg(HW_REG_MODE, 2, 2), 3 an overflow still gave +-inf and a subnormal result still rounded to
nearest even. While the mode is toward zero an overflow now gives +-DBL_MAX and a subnormal result is truncated.
Programs that never write the field emit the same code as before. The f64 sibling of ItsNotRexed#5310 (v_ldexp_f16).

Tests: tests/semantic/test_ldexp_f64_mode_round.py (cases, exact rational reference sweep, mode switch back, clamp,
the probe's words); tools/mode_round_probe.py gets two v_ldexp_f64 cases for an RDNA2 owner to run.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant