Skip to content

Translator: v_frexp_mant_f32, v_frexp_exp_i32_f32, v_ldexp_f32 (SHADER-89) - #1328

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
dev-ewann:feat/frexp-ldexp-f32
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
dev-ewann:feat/frexp-ldexp-f32

Conversation

@dev-ewann

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-89 (#1218, listed as claimed by @dev-ewann; status posted in #1218)

What and why

v_frexp_mant_f32, v_frexp_exp_i32_f32 and v_ldexp_f32 were rejected as Unsupported by tools/rdna2spirv.py. They are now translated bit-exactly on the binary32 word (no float arithmetic, so no dependence on the driver's Ldexp/Frexp extended instructions):

  • A normal input has a biased exponent of 1..254. v_frexp_mant_f32 sets the exponent field to 126 (mantissa in [0.5, 1), sign kept); v_frexp_exp_i32_f32 returns the biased exponent minus 126.
  • Zero and denormals (read as zero) give a signed zero and exponent 0; infinities and NaNs give the input unchanged and exponent 0.
  • v_ldexp_f32 is S0 × 2^S1, S1 a signed integer: clamped to [-512, 512] and added to the exponent field; above 254 the result is ±infinity, at 0 or below it is ±0 (no denormal results); zero, infinities and NaNs ignore S1.
  • Neg/abs, clamp and the output multipliers apply as on other float ops (not to the integer exponent result).

The rule is in the contract table of docs/design.md (new row after Float-to-int saturation). Source: the RDNA 2 ISA reference pseudocode. Not measured on hardware: the denormal input and result handling (the f32 denormal mode is not modelled) and the NaN payload are the translator's choice, and the row says so.

Tested

AI-assisted (Claude). CPU only, no GPU, no game files, not run on the shader corpus.

  • New tests/semantic/test_alu_frexp_ldexp.py (11 tests): known values, 60 random normals checked against math.frexp/math.ldexp, round trip mant × 2^exp == input, zero/inf/NaN, overflow and underflow saturation (including INT_MIN and INT_MAX exponents), neg/abs/clamp/mul:2.
  • Against main's translator (rules reverted) all of them error (156); with the change: 11 OK.
  • Mutations of the new code, each reverted: normal bound 254→255 (2 fail), exponent bias 126→127 (67), mantissa exponent 0x3F0…→0x3F8… (67), signed zero dropped (2), ldexp validity bound (2), S1 clamp removed (1), overflow→0 (18), underflow→inf (13), output modifiers dropped on ldexp (1).
  • python3 -I -B -m unittest discover -s tests/semantic on top of main (c0ac5fd): 1182 tests, OK (1 skipped). python3 -m compileall -q make_game.py tools tests: ok.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@kapitche

Copy link
Copy Markdown
Contributor

Tested c9fd097 on Linux (WSL2 Ubuntu), Python 3.12, CPU only: pass.

  • python -m unittest discover -s tests/semantic: 1181 OK. With main's tools/rdna2spirv.py, 156 fail in test_alu_frexp_ldexp.py.
  • Special cases follow the test:
    • signed zero gives exp 0;
    • inf and NaN pass through mant/ldexp, with exp 0;
    • ldexp clamps the exponent to ±512, then saturates.
  • f32 denormals aren't modelled (inputs read as zero, and tiny results flush to ±0). That's documented in the test, not hardware-checked.

@MIGHTYLUV

Copy link
Copy Markdown

Tested c9fd097 on Windows 11 x64 (Python 3.14.6, CPU): pass

  • python -m compileall -q make_game.py tools tests: exit 0
  • python -I make_game.py --help: exit 0
  • python -I tests/semantic/test_alu_frexp_ldexp.py -v: 11 passed (150 subtests passed)
  • Bitwise exponent extraction and unbiased scaling correctly match IEEE-754 binary32 reference.
    No game files. AI-assisted.

@Rainmaker33

Copy link
Copy Markdown

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

The PR's diff applied cleanly to main c0ac5fd. VS 2022, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8.

  • tests/semantic/test_alu_frexp_ldexp.py: OK. tests/semantic: 1182 run, OK.
  • scripts/run_gpu_tests.py: 98/126 pass, 28 skipped (wave64), the same counts as main. There is no GPU fixture for v_frexp_*/v_ldexp_f32 yet, so the new ops were only checked on the CPU here.

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested c9fd097b344653fc971a075345de9fbb009871c2 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I assembled six synthetic wave32 compute shaders that directly execute v_frexp_mant_f32, v_frexp_exp_i32_f32 and v_ldexp_f32 through this translator. For each of 12 input cases, vk_compute_test read back the raw mantissa, integer exponent, explicitly scaled value and the mantissa/exponent round trip.

All 48 words matched, including minimum and maximum normals, signed zero, the negative subnormal input, signed underflow and overflow, extreme signed scale operands, negative infinity and quiet/signaling NaN payloads. The negative subnormal followed the implemented signed-zero rule. The two NaNs retained their input bits and returned exponent zero.

Six modules passed SPIR-V validation and all 11 focused semantic tests passed. Linux 7.0.0-43-generic, llvm-mc 21.1.8 and SPIRV-Tools 2026.1. This verifies the Vulkan translation on NVIDIA. Native RDNA denormal handling and NaN payload behavior remain open.

AI-assisted verification.

@bobcatchris

Copy link
Copy Markdown
Contributor

Two separate red checks here, and neither is a defect in your translator change.

windows (run 45011474729): the CPU runtime tests step fails only on

49/67 Test #49: d3d12_predicated_copy ...***Failed
98% tests passed, 1 tests failed out of 66

That is the test that made main red at c0ac5fd, fixed on main by merged #1314 (675e35b: no Vulkan device is a CTest skip, 77). Your head c9fd097b predates it, so a rebase onto main (f85582e) or a job re-run clears it; main passes all four workflows at f85582e.

linux-gpu-tools in the same run: the failure is in mozilla-actions/sccache-action, Unexpected HTTP response: 403 on its start step and then Unable to locate executable file: undefined on the post step — a cache-service/credentials problem on the runner, before any of your code ran. Worth a re-run rather than a change.

check, change-scope, no-game-data, both semantic-tests legs and translator-tests pass.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested c9fd097 on RTX 4090 (driver 621.14, 32-wide subgroups), Windows 11 26200: pass

  • RelWithDebInfo Ninja build of runtime/ (MSVC, Vulkan SDK 1.4.363.0); ctest: 65/65
  • python -I scripts/run_gpu_tests.py: 98/126 passed, 28 skipped (wave64 tests skip without emulation); with LEONIDA_W64_EMULATE=1: 126/126 passed. main f85582e on the same machine: 105/133 passed, 28 skipped and 133/133 passed
  • python -m unittest discover -s tests/semantic: 1182 OK (main: 1178)
  • python -m pytest tests --ignore=tests/semantic: 3290 passed, 28 skipped (main: 3365 passed, 33 skipped)

The totals are below main's because this branch is 80 commits behind main, so it lacks the tests added since then. Nothing failed or regressed.

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

@exhawk

exhawk commented Nov 22, 2026

Copy link
Copy Markdown

Tested c9fd097b344653fc971a075345de9fbb009871c2 on an NVIDIA GeForce RTX 3090 Ti (Ampere), driver 617.42, Vulkan 1.4.351, Windows 11 Pro 25H2 (build 26200.9974).

All 516 GPU result words matched bit for bit against an independent Python reference, with 172/172 matches each for v_frexp_mant_f32, v_frexp_exp_i32_f32 and v_ldexp_f32. One synthetic compute shader took 172 distinct input pairs at dispatch time, including 64 seeded normal inputs (seed 1328). Each VKC_DUMP also contained the exact two input words echoed by the shader.

As a negative control, I replaced only v_ldexp_f32 with a move of 0x12345678 and repeated all 172 dispatches. In every dump, only the scaled result changed. The same checker flagged all 172 cases.

This adds direct GPU coverage of the three instructions on Windows Ampere alongside the RTX 3090 stock-suite run and the RTX 5090 Linux shader test.

AI-assisted testing with synthetic data.

@ItsNotRexed
ItsNotRexed merged commit 2837c2f into ItsNotRexed:main Nov 22, 2026
8 of 10 checks passed
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.

9 participants