Skip to content

rdna2spirv: lower v_ldexp_f64 bit-exactly via 32-bit integer ops (gfx1036-measured) - #3543

Open
timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-ldexp-f64
Open

timelery wants to merge 1 commit into
ItsNotRexed:mainfrom
timelery:shader-ldexp-f64

Conversation

@timelery

Copy link
Copy Markdown
Contributor

Summary

Implements v_ldexp_f64 (D.f64 = S0.f64 × 2^(S1.i32)) in tools/rdna2spirv.py as a pure 32-bit integer lowering on the (lo, hi) dword pair — requiring neither OpCapability Float64 nor OpCapability Int64, even when VOP3 clamp is present.

Claimed in #3232 (v_ldexp_f64).

Hardware-measured RDNA2 contract (gfx1036, wave32 & wave64)

Measured natively via tools/hw_oracle on an AMD Radeon RDNA2 iGPU (gfx1036, Windows driver 32.0.21042.62) under the game MODE (ieee=0, dx10_clamp=1, denorm32=0, denorm16_64=3):

  • Specials: ±0, ±inf, and both quiet and signalling NaNs pass through bit-for-bit unchanged (after S0 abs/neg).
  • Subnormal inputs (denorm16_64=3): normalized via FindUMsb before adding clamp(S1, -4096, 4096) to the exponent field.
  • Subnormal outputs (denorm16_64=3): shifted right by sh = 1 - e2 (1..53) and rounded to nearest even (carry out of bit 51 into bit 52 yields ±min_norm; sh >= 54 flushes to ±0).
  • Output modifiers:
    • clamp applies identically in both denorm16_64=3 and denorm16_64=0 (dx10_clamp=1: NaN, -0.0, and <0 → +0.0; >1.0 including +inf → 1.0), lowered bit-exactly in 32-bit integer ops.
    • mul:2/mul:4/div:2 are ignored by hardware when denorm16_64=3 and scale when denorm16_64=0, so they are rejected as Unsupported rather than silently diverging across modes.
  • Immediate S0 encodings: inline integers -16..64 sign-extend across 64 bits (1 → min_sub, -1 → 0xFFFFFFFF_FFFFFFFF), while 32-bit literals (0x3ff00000, 65, -17) occupy bits [63:32] with [31:0] = 0.

Changes

  • tools/rdna2spirv.py: pure 32-bit integer lowering for v_ldexp_f64 (bounded shift amounts 0..31, VGPR/SGPR/inline/32-bit-literal/64-bit-literal S0, abs/neg/clamp, destination aliasing, rejection of omod and invalid operands/destinations).
  • tests/semantic/test_alu_ldexp_f64.py: 8 unit tests verifying zero Float64/Int64 capabilities, spirv_asm.assemble / spirv-val, gfx1036-pinned specials, all 52 subnormal input bits, all 54 subnormal shift boundaries and exact half-ULP ties, abs/neg/clamp, operand/literal/aliasing forms, seeded random sweep, and rejected forms.
  • docs/design.md: added v_ldexp_f64 row to the translator contract table.

Verification

  • Hardware oracle: AMD Radeon RDNA2 iGPU (gfx1036), Windows driver 32.0.21042.62, tools/hw_oracle (wave32 & wave64): 870 / 870 cases bit-exact between native gfx1036 and translated SPIR-V via spirv_mini_interp.py (plus separate probes confirming inline vs. 32-bit literal S0 placement and omod under denorm16_64=3 vs. 0).
  • Automated checks (no game files):
    • python -I tests/semantic/test_alu_ldexp_f64.py -v — 8/8 OK
    • python -I tests/semantic/test_alu_frexp_f64.py -v — 8/8 OK
    • python -I tests/semantic/regen_fixtures.py --check — OK
    • python -I tests/test_docs_links.py — 5/5 OK
    • python -I tests/test_docs_repo_paths.py — 2/2 OK
    • python -m compileall -q make_game.py tools tests — OK
  • Not run on the shader corpus.

Disclosure

AI-assisted implementation and testing (verified against native gfx1036 hardware oracle, spirv_mini_interp, and spirv_asm).

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested d98acce on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • python3 -m pytest tests/semantic/test_alu_ldexp_f64.py -q -> 8 passed, 197 subtests passed
  • python3 -I scripts/run_gpu_tests.py --build <build> -> 184/199 passed, 14 skipped, 0 failed (expected D3D12-owned skips)

No runtime C++ changes; GPU suite run from the PR worktree against the existing main runtime build. Did not run tools/hw_oracle on this Deck.

AI-assisted (OpenCode).

@MKmajster

Copy link
Copy Markdown

Tested d98acce on a GPU: NVIDIA GeForce RTX 3070 (Ampere, wave32), driver 617.42, Windows 11 24H2 (26100). The translated v_ldexp_f64 runs bit-exact against ref_ldexp_f64 on this card: 10,512 / 10,512 cases. Until now the lowering had only been checked through spirv_mini_interp and run_gpu_tests.py, which has no v_ldexp_f64 fixture.

How: a throwaway harness (not part of this PR) builds wave32 compute programs of 200 v_ldexp_f64 v[10:11], <S0>, v3 each, assembled with llvm-mc 23.1.6 (gfx1013), translated with this branch's rdna2spirv.py, run with vk_compute_test ... dump, and compares every (lo, hi) pair with the PR's own ref_ldexp_f64 from tests/semantic/test_alu_ldexp_f64.py. Only lane 0 runs, one wave per group. Every operand goes through v_or_b32 vN, <literal>, v0, so the driver can't constant-fold the integer lowering away. Per seed, 3,504 cases:

  • the 17 specials (±0, ±inf, quiet and signalling NaNs of both signs, min/max subnormal, min normal, max finite, 1.0, ...) × 26 S1 values (0, ±1, ±2, ±52, ±53, ±1022, 1023, -1023, 1024, -1074, -1075, 2046, -2098, ±4096, ±4097, ±5000, INT_MAX, INT_MIN);
  • every subnormal input bit 0..51, with random low bits, × S1 in {0, 1, 52, 60, -1, -10};
  • normals pushed into and through the subnormal range (S1 = -1..-59), with half-ULP tie mantissas and both signs;
  • 1,500 random 64-bit patterns × random S1 (small, ±2200, ±6000, full 32-bit);
  • 360 with abs/neg/-|x| on S0, and 300 with VOP3 clamp (values around [0, 1] plus random and specials).

Seeds 1, 2 and 3: 3,504 / 3,504 bit-exact each. Negative control: the same seed-1 run against a reference with the low mantissa bit flipped reports 3,504 / 3,504 mismatches, so the comparison does look at the words. The modules pass spirv-val. Their only 64-bit capability is the Int64 every translated compute shader carries (cs_round_f32_denorm and cs_cvt_pk_u8 have the same set), with no Float64.

Not covered: wave64 (this GPU has 32-wide subgroups only), SGPR/inline/literal S0 encodings (S0 is always a VGPR pair here), and omod rejection (left to the semantic tests).

No game files. AI-assisted (Claude).

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested d98acced (base f847748c) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host, Python 3.14.4): pass.

  • tests/semantic/test_alu_ldexp_f64.py: 8 passed (197 subtests) on this head. With tools/ from f847748c, 202 fail with rdna2spirv.Unsupported: v_ldexp_f64 and 3 pass.
  • scripts/run_gpu_tests.py, whole suite: 152/199 passed, 46 skipped, 1 failed, identical per test to main f847748c on this card. The failure is ps_sdwa_f16_const, the NVIDIA f16 denormal flush on main (rdna2spirv: widen f16 denormal operands without UnpackHalf2x16 (NVIDIA reads them as 0) #3220 / rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) #3040), not this PR.
  • cs_f64_add and ps_f64_mods pass, so the existing f64 paths still work on the GPU.
  • ctest on the Linux build (CMake + Ninja, GCC): 41/41 passed.
  • Not covered: the GPU suite has no v_ldexp_f64 fixture, so the integer lowering ran on the CPU only. The cases near the f64 denormal and overflow edges would make a good cs_* fixture with exact words. They don't depend on the device's f64 float controls, since the lowering is integer ops.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested a59367f merged onto main aa2988c on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed. main aa2988c gives 164/210, 46 skipped, 0 failed. All 214 test shaders translate and pass spirv-val.
  • tests/semantic/test_alu_ldexp_f64.py: ran 8 tests in 2.463s; OK.

AI-assisted (Claude Code).

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.

5 participants