shader: lower v_ldexp_f32 and v_ldexp_f16 #74
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/uiliannn--VItoriaBr!74
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-74"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@lcg007xd-c on GitHub · 2026-11-25 21:47 UTC · original
lcg007xd-c:shader-ldexp→main· +145 −0 · 2 files · 1 commits · openWhat is broken
v_ldexp_f32(VOP3 0x362) andv_ldexp_f16(VOP2 0x3B, VOP3 0x13B) decode, but a program using either fails to translate withv_ldexp_f32 is not supported(orv_ldexp_f16 …). I found them by translating a one-instruction program for every opcode the decoder names. Compilers emitv_ldexpfor HLSL'sldexpand 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.iandD.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)ldexp(x, n, limit, parts): clampsnto[-limit, limit]and multipliesxbypartsnormal powers of two, all withn's sign. Past the limit, every finite non-zero result is already 0 or infinity, so a huge exponent such asi32::MINcannot wrap.limit = 279, in three steps of at most 93. A smaller limit would be wrong:f32::MAX * 2^-277is still the smallest denormal, and2^-149 * 2^277already overflows. Each step only scales, so a normal result is exact. NaN, infinities and signed zeros pass through the multiplications.2^nwith|n| ≤ 41. That product is exact in f32. It is then rounded to f16 once, by the same path as the other f16 operations, soomodand clamp apply too.abs/negon 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 thelower.rschange 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):i32::MAXandi32::MINexponents, −0, andf32::MAX·2⁻¹²⁷.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:2⁻¹⁴⁹,1.5·2⁻¹⁴⁹,2⁻¹⁵⁰,f32::MAX·2⁻²⁵⁴,f32::MAX·2⁻²⁷⁷,f32::MAX·2⁻²⁷⁸), a denormal input, NaN and −inf;1.5·2⁻²⁴→ 2),0x7BFF·2³²⁷⁶⁷→ inf, and anop_selcase (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.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
checkjob is red on main itself right now (victory-gpu: no methodbytes_fromontexture::Layout), independent of this change.🤖 Generated with 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]>@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) beforev_ldexp_f16. That agrees with this change reading only S1's low 16 bits as a signed exponent.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.