shader: lower v_frexp_mant/v_frexp_exp for f32 and f16 #76

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

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

What is broken

The four frexp instructions decode, but a program using any of them fails to translate with v_frexp_mant_f32 is not supported (or the matching name):

Instruction VOP1 VOP3
v_frexp_exp_i32_f32 0x3F 0x1BF
v_frexp_mant_f32 0x40 0x1C0
v_frexp_mant_f16 0x59 0x1D9
v_frexp_exp_i16_f16 0x5A 0x1DA

I found them by translating a one-instruction program for every opcode the decoder names. Compilers emit them for HLSL's frexp and inside log/pow/division expansions.

Semantics (AMD's RDNA2 ISA, §12.8, opcodes 63, 64, 89, 90)

  • Significand and exponent: they split x = significand · 2^exponent with the significand in [0.5, 1) and the input's sign, as C's frexp does.
  • Infinity and NaN: "if S0 == ±INF || S0 == NAN: D = S0" for the significand, and the exponent is 0.
  • Zero: keeps its bits and has exponent 0, as in C.
  • Denormals: normalized, so 2⁻¹⁴⁹ gives (0.5, −148).

The change (emit/lower.rs)

  • New helper frexp(bits, half), worked on the float's bits in integers:
    • A normal value's exponent field becomes the field of 0.5, and the exponent is field − (bias − 1).
    • A denormal's leading one (DXIL FirstbitHi) moves to the implicit bit. Its exponent comes from that bit's position.
    • Zero, infinity and NaN keep their bits and get exponent 0.
    • Because nothing goes through float arithmetic, the result does not depend on whether the host flushes denormals.
  • f32 forms (in valu_op): omod/clamp on the significand go through write_f, as for any float result.
  • f16 forms (in valu16_op): abs/neg are applied to the sign bit. The result is written to the selected 16-bit half. omod/clamp on v_frexp_mant_f16 go through the usual f16 result path.

Tests

  • emit::lower::tests::frexp_translates (every host): all four in both VOP1 and VOP3 form decode to their names, and a program that stores the result translates. Without the lower.rs change it fails: v_frexp_mant_f32 [7e008101, 7e000000]: GFX10 code at 0x24: v_frexp_mant_f32 is not supported.

  • valu_tests::v_frexp_split_floats_like_c_frexp (Windows/GPU): hand-computed significand and exponent pairs. They cover 8, −3, 0.75, 1, f32::MAX, the smallest normal, three denormals, ±0, +inf and NaN, plus the f16 equivalents (65504, 2⁻¹⁴, 2⁻²⁴, −0, inf, NaN).

  • CPU run: I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's lli. I also compared against an independent f64 reference:

    • f32: 200 scrambled bit patterns plus 14 edge cases;
    • f16: every 97th of the 65536 patterns plus edges, with junk in the high half;
    • modifiers: VOP3 neg (f32), abs (f16), and omod ×2 (f16 significand).

    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. I also have no RDNA2 hardware, so the sNaN case follows the ISA's text (the bits pass through) and is not checked on a card.

🤖 Generated with Claude Code

**@⁠lcg007xd-c** on GitHub · 2026-11-25 21:53 UTC · [original](https://github.com/uiliannn/VItoriaBr/pull/76) `lcg007xd-c:shader-frexp` → `main` · +199 −0 · 2 files · 1 commits · open ## What is broken The four frexp instructions decode, but a program using any of them fails to translate with `v_frexp_mant_f32 is not supported` (or the matching name): | Instruction | VOP1 | VOP3 | |---|---|---| | `v_frexp_exp_i32_f32` | 0x3F | 0x1BF | | `v_frexp_mant_f32` | 0x40 | 0x1C0 | | `v_frexp_mant_f16` | 0x59 | 0x1D9 | | `v_frexp_exp_i16_f16` | 0x5A | 0x1DA | I found them by translating a one-instruction program for every opcode the decoder names. Compilers emit them for HLSL's `frexp` and inside log/pow/division expansions. ## Semantics (AMD's RDNA2 ISA, §12.8, opcodes 63, 64, 89, 90) - **Significand and exponent:** they split `x = significand · 2^exponent` with the significand in `[0.5, 1)` and the input's sign, as C's `frexp` does. - **Infinity and NaN:** "if S0 == ±INF || S0 == NAN: D = S0" for the significand, and the exponent is 0. - **Zero:** keeps its bits and has exponent 0, as in C. - **Denormals:** normalized, so `2⁻¹⁴⁹` gives `(0.5, −148)`. ## The change (`emit/lower.rs`) - **New helper `frexp(bits, half)`,** worked on the float's bits in integers: - A normal value's exponent field becomes the field of 0.5, and the exponent is `field − (bias − 1)`. - A denormal's leading one (DXIL `FirstbitHi`) moves to the implicit bit. Its exponent comes from that bit's position. - Zero, infinity and NaN keep their bits and get exponent 0. - Because nothing goes through float arithmetic, the result does not depend on whether the host flushes denormals. - **f32 forms** (in `valu_op`): `omod`/clamp on the significand go through `write_f`, as for any float result. - **f16 forms** (in `valu16_op`): `abs`/`neg` are applied to the sign bit. The result is written to the selected 16-bit half. `omod`/clamp on `v_frexp_mant_f16` go through the usual f16 result path. ## Tests - **`emit::lower::tests::frexp_translates`** (every host): all four in both VOP1 and VOP3 form decode to their names, and a program that **stores** the result translates. Without the `lower.rs` change it fails: `v_frexp_mant_f32 [7e008101, 7e000000]: GFX10 code at 0x24: v_frexp_mant_f32 is not supported`. - **`valu_tests::v_frexp_split_floats_like_c_frexp`** (Windows/GPU): hand-computed significand and exponent pairs. They cover 8, −3, 0.75, 1, `f32::MAX`, the smallest normal, three denormals, ±0, +inf and NaN, plus the f16 equivalents (65504, `2⁻¹⁴`, `2⁻²⁴`, −0, inf, NaN). - **CPU run:** I ran these on Linux through the CPU harness of #62, which runs the emitted DXIL with LLVM's `lli`. I also compared against an independent f64 reference: - **f32:** 200 scrambled bit patterns plus 14 edge cases; - **f16:** every 97th of the 65536 patterns plus edges, with junk in the high half; - **modifiers:** VOP3 `neg` (f32), `abs` (f16), and `omod ×2` (f16 significand). 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. I also have no RDNA2 hardware, so the sNaN case follows the ISA's text (the bits pass through) and is not checked on a card. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
VOP1 0x3F/0x40/0x59/0x5A (and their VOP3 forms) decoded but failed to
translate ("v_frexp_mant_f32 is not supported"). They split a float as
C's frexp does (AMD's RDNA2 ISA): the significand in [0.5, 1) with the
input's sign, and the exponent, so x = significand * 2^exponent.
Infinities and NaNs keep their bits with exponent 0, as the ISA says;
zero likewise.

Both are worked out on the float's bits in integers: a normal value's
exponent field becomes 0.5's; a denormal is normalized from its leading
one (FirstbitHi), so the result does not depend on whether the host
flushes denormals. The f16 forms write a 16-bit half; omod and clamp on
v_frexp_mant_f16 go through the usual f16 result path.

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:14 +00:00
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/76/head:github-bot/gh-76'
git switch 'github-bot/gh-76'

Merge

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