shader: lower v_ldexp_f32 and v_ldexp_f16 #74

Open
github-bot wants to merge 1 commit from github-bot/gh-74 into main AGit
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 21:47 UTC · original
lcg007xd-c:shader-ldexp → main · +145 −0 · 2 files · 1 commits · open

What is broken

v_ldexp_f32 (VOP3 0x362) and v_ldexp_f16 (VOP2 0x3B, VOP3 0x13B) decode, but a program using either fails to translate with v_ldexp_f32 is not supported (or v_ldexp_f16 …). I found them by translating a one-instruction program for every opcode the decoder names. Compilers emit v_ldexp for HLSL's ldexp and in exp/pow expansions.

Semantics

AMD's RDNA2 ISA (§12.12 opcode 866, §12.7 opcode 59) defines them as D.f = S0.f * 2^S1.i and D.f16 = S0.f16 * 2^S1.i16. S1 is a signed integer, and only its low 16 bits count for f16.

The change (emit/lower.rs)

  • Shared helper ldexp(x, n, limit, parts): clamps n to [-limit, limit] and multiplies x by parts normal powers of two, all with n's sign. Past the limit, every finite non-zero result is already 0 or infinity, so a huge exponent such as i32::MIN cannot wrap.
  • f32: limit = 279, in three steps of at most 93. A smaller limit would be wrong: f32::MAX * 2^-277 is still the smallest denormal, and 2^-149 * 2^277 already overflows. Each step only scales, so a normal result is exact. NaN, infinities and signed zeros pass through the multiplications.
  • Shortcut (marked in the code): a result that ends in the f32 denormals can round twice, once at the step that leaves the normal range and again at a later step.
  • f16: the f16 is widened to f32 and multiplied once by 2^n with |n| ≤ 41. That product is exact in f32. It is then rounded to f16 once, by the same path as the other f16 operations, so omod and clamp apply too.
  • Modifiers: abs/neg on the integer operand are refused.

Tests

  • emit::lower::tests::ldexp_translates (every host): the VOP3 f32 form and both f16 forms decode to their names, and a program that stores the result translates. Without the lower.rs change it fails: v_ldexp_f32 [d7620000, 00020501]: GFX10 code at 0x24: v_ldexp_f32 is not supported.

  • valu_tests::v_ldexp_f32_and_f16_scale_by_powers_of_two (Windows/GPU):

    • f32: 1.5·2³, 2⁻¹²⁶, 2¹²⁷, overflow at 2¹²⁸, i32::MAX and i32::MIN exponents, −0, and f32::MAX·2⁻¹²⁷.
    • f16: 1.5·2³, 2¹⁵, overflow at 2¹⁶, the smallest denormal 2⁻²⁴, an exponent with junk in its high half, −32768, and −1·2⁻¹.
    • I left denormal f32 results out of the GPU case, because a host that flushes them would fail it for a reason that is not the translation's.
  • CPU run: I ran these cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I added:

    • f32 denormal results (2⁻¹⁴⁹, 1.5·2⁻¹⁴⁹, 2⁻¹⁵⁰, f32::MAX·2⁻²⁵⁴, f32::MAX·2⁻²⁷⁷, f32::MAX·2⁻²⁷⁸), a denormal input, NaN and −inf;
    • an f64 reference sweep over 5 values × 9 exponents;
    • the f16 VOP3 form, a tie that rounds to even (1.5·2⁻²⁴ → 2), 0x7BFF·2³²⁷⁶⁷ → inf, and an op_sel case (src0 and result in the high halves).

    All match.

Run here (Linux, Rust 1.98.1):

  • cargo test -p victory-shader --lib --locked: 188 passed, 0 failed, 2 ignored.
  • Portable clippy and CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings: clean.

Not run here: the GPU case, since I have no Windows machine. The Windows check job is red on main itself right now (victory-gpu: no method bytes_from on texture::Layout), independent of this change.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:47 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/74) `lcg007xd-c:shader-ldexp` → `main` · +145 −0 · 2 files · 1 commits · open ## What is broken `v_ldexp_f32` (VOP3 **0x362**) and `v_ldexp_f16` (VOP2 **0x3B**, VOP3 **0x13B**) decode, but a program using either fails to translate with `v_ldexp_f32 is not supported` (or `v_ldexp_f16 …`). I found them by translating a one-instruction program for every opcode the decoder names. Compilers emit `v_ldexp` for HLSL's `ldexp` and in exp/pow expansions. ## Semantics AMD's RDNA2 ISA (§12.12 opcode 866, §12.7 opcode 59) defines them as `D.f = S0.f * 2^S1.i` and `D.f16 = S0.f16 * 2^S1.i16`. S1 is a signed integer, and only its low 16 bits count for f16. ## The change (`emit/lower.rs`) - **Shared helper `ldexp(x, n, limit, parts)`:** clamps `n` to `[-limit, limit]` and multiplies `x` by `parts` normal powers of two, all with `n`'s sign. Past the limit, every finite non-zero result is already 0 or infinity, so a huge exponent such as `i32::MIN` cannot wrap. - **f32:** `limit = 279`, in three steps of at most 93. A smaller limit would be wrong: `f32::MAX * 2^-277` is still the smallest denormal, and `2^-149 * 2^277` already overflows. Each step only scales, so a normal result is exact. NaN, infinities and signed zeros pass through the multiplications. - **Shortcut (marked in the code):** a result that ends in the f32 denormals can round twice, once at the step that leaves the normal range and again at a later step. - **f16:** the f16 is widened to f32 and multiplied once by `2^n` with `|n| ≤ 41`. That product is exact in f32. It is then rounded to f16 once, by the same path as the other f16 operations, so `omod` and clamp apply too. - **Modifiers:** `abs`/`neg` on the integer operand are refused. ## Tests - **`emit::lower::tests::ldexp_translates`** (every host): the VOP3 f32 form and both f16 forms decode to their names, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `v_ldexp_f32 [d7620000, 00020501]: GFX10 code at 0x24: v_ldexp_f32 is not supported`. - **`valu_tests::v_ldexp_f32_and_f16_scale_by_powers_of_two`** (Windows/GPU): - **f32:** 1.5·2³, 2⁻¹²⁶, 2¹²⁷, overflow at 2¹²⁸, `i32::MAX` and `i32::MIN` exponents, −0, and `f32::MAX·2⁻¹²⁷`. - **f16:** 1.5·2³, 2¹⁵, overflow at 2¹⁶, the smallest denormal 2⁻²⁴, an exponent with junk in its high half, −32768, and −1·2⁻¹. - I left denormal f32 results out of the GPU case, because a host that flushes them would fail it for a reason that is not the translation's. - **CPU run:** I ran these cases on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I added: - f32 denormal results (`2⁻¹⁴⁹`, `1.5·2⁻¹⁴⁹`, `2⁻¹⁵⁰`, `f32::MAX·2⁻²⁵⁴`, `f32::MAX·2⁻²⁷⁷`, `f32::MAX·2⁻²⁷⁸`), a denormal input, NaN and −inf; - an f64 reference sweep over 5 values × 9 exponents; - the f16 VOP3 form, a tie that rounds to even (`1.5·2⁻²⁴` → 2), `0x7BFF·2³²⁷⁶⁷` → inf, and an `op_sel` case (src0 and result in the high halves). All match. **Run here** (Linux, Rust 1.98.1): - `cargo test -p victory-shader --lib --locked`: 188 passed, 0 failed, 2 ignored. - Portable clippy and `CARGO_FEATURE_PURE=1 cargo clippy -p victory-shader --all-targets --target x86_64-pc-windows-msvc -- -D warnings`: clean. **Not run here:** the GPU case, since I have no Windows machine. The Windows `check` job is red on main itself right now (`victory-gpu`: no method `bytes_from` on `texture::Layout`), independent of this change. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP3 0x362 (v_ldexp_f32) and VOP2 0x3B / VOP3 0x13B (v_ldexp_f16)
decoded but failed to translate ("v_ldexp_f32 is not supported"). AMD's
RDNA2 ISA: D = S0 * 2^S1, S1 a signed 32-bit (16-bit for f16) integer.

The exponent is clamped where every finite non-zero result is already
0 or infinity (+-279 for f32, +-41 for f16), so a huge one cannot wrap.
f32 scales in three multiplications by normal powers of two of the
exponent's sign, exact for normal results (a denormal result can round
twice: marked as a shortcut). f16 scales once in f32, where the product
is exact, and rounds to f16 once like the other f16 operations.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:43:12 +00:00
Author
Owner

@⁠lcg007xd-c on GitHub · 2026-11-25 22:03 UTC · original

One more reference for the f16 form's exponent: Mesa's ACO (aco_select_nir_alu.cpp, nir_op_ldexp) converts the exponent to a 16-bit integer with saturation (v_cvt_pk_i16_i32 …, 0) before v_ldexp_f16. That agrees with this change reading only S1's low 16 bits as a signed exponent.

**@⁠lcg007xd-c** on GitHub · 2026-11-25 22:03 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/74#issuecomment-7175876993) One more reference for the f16 form's exponent: Mesa's ACO (`aco_select_nir_alu.cpp`, `nir_op_ldexp`) converts the exponent to a 16-bit integer with saturation (`v_cvt_pk_i16_i32 …, 0`) before `v_ldexp_f16`. That agrees with this change reading only S1's low 16 bits as a signed exponent.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/74/head:github-bot/gh-74'
git switch 'github-bot/gh-74'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-74'
git switch 'github-bot/gh-74'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-74'
git switch 'github-bot/gh-74'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-74'
git switch 'main'
git merge --squash 'github-bot/gh-74'
git switch 'main'
git merge --ff-only 'github-bot/gh-74'
git switch 'main'
git merge 'github-bot/gh-74'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.