Skip to content

xshader: lower exact f16 frexp/ldexp and normalized 16-bit packs - #287

Merged
brcly merged 1 commit into
brcly:mainfrom
JohnVonWells:xshader/exact-half
Nov 27, 2026
Merged

brcly merged 1 commit into
brcly:mainfrom
JohnVonWells:xshader/exact-half

Conversation

@JohnVonWells

Copy link
Copy Markdown
Contributor

Adds six half-precision and normalized-pack forms that the translator refused: v_frexp_mant_f16, v_frexp_exp_i16_f16, v_ldexp_f16, v_cvt_pknorm_i16_f16, v_cvt_pknorm_u16_f16 and v_cvt_pknorm_i16_f32.

Fix: v_cvt_pknorm_u16_f32 was sometimes off by one near a rounding tie. round(saturate(x) * 65535.0) rounds the product to f32 first, which can create a false tie, the same problem tb_norm_f16 already avoids. It now uses tb_norm_f32. The native hardware agrees with the exact model.

They live in a new module, half_exact.rs, and work on the stored bits with integer arithmetic, like the f64 forms. They follow MODE: the f16 round field (bits 3:2), the f16 denormal fields (bits 7:6) and IEEE NaN quieting (bit 9). The f16 packs reuse the existing exact tb_norm_f16. For v_cvt_pknorm_i16_f32, the new tb_norm_f32 rounds the exact product once. They accept plain VGPR operands (VOP1/VOP2 without SDWA or DPP, or VOP3 without modifiers or op_sel), in compute shaders only.

Semantics from native execution

tools/native_rdna2/check_half.py runs the instructions natively on an RX 6950 XT under all 1024 MODE[9:0] values and works out which MODE bits change any result. It then compares an exact rational reference model on every combination of those bits: 180,224 comparisons, no mismatches. It also covers the existing v_cvt_pknorm_u16_f32. The hardware settled these details:

  • v_ldexp_f16 takes the exponent from the low 16 bits of S1, as a signed number. For example, 0xffff0003 scales by 2^3.
  • Denormal results of v_ldexp_f16 round according to the MODE round field, and the denormal flush tests tininess after rounding.
  • The frexp and ldexp forms keep the high half of the destination. The packs write the whole word.
  • The packs do not depend on MODE, apart from f16 input denormals (bit 6) for the f16 packs. NaN gives 0.

GPU control

tools/check_half_exact_gpu.py runs the translated shaders on a D3D12 GPU against the same model. It uses eleven MODE values written with s_setreg_imm32_b32, VOP1/VOP2 and VOP3 encodings, dynamic stale destinations, and frexp/ldexp forms that write over their own source. Result on the RX 6950 XT: 4,368 inputs and 39,312 results, all correct.

  • Baseline: the previous translator refuses all 13 programs.
  • Negative controls: the old float rounding of v_cvt_pknorm_u16_f32 fails 102 of 312 dispatches. That is the previous translator's behaviour for that form. A 32-bit ldexp exponent fails 294 of 312, and frexp/ldexp without the kept high half fail 312 of 312.

Coverage of single decodable ALU instructions goes from 498 to 504 translated.

half-exact is now part of tools/check_translator_gpu.py. python tuxbox.py check on the RX 6950 XT passes completely: Lanes, the D3D12 regression controls and all translator checks. There is no run on an NVIDIA card yet.

Evidence: evidence/native-rdna2-half.json, evidence/half-exact-control.json.

AI-assisted.

Add v_frexp_mant_f16, v_frexp_exp_i16_f16, v_ldexp_f16,
v_cvt_pknorm_i16_f16, v_cvt_pknorm_u16_f16 and v_cvt_pknorm_i16_f32 in a
new half_exact module. They work on the stored bits and follow MODE: the
f16 round field, f16 denormals and IEEE NaN quieting. The f16 packs reuse
tb_norm_f16; the new tb_norm_f32 rounds the exact product once. Plain
VGPR operands, compute only.

Fix v_cvt_pknorm_u16_f32, which rounded x * 65535 to float before
rounding to an integer and could be off by one near a tie. It now uses
tb_norm_f32 as well.

Native RDNA2 execution under all 1024 MODE values
(tools/native_rdna2/check_half.py) settled the details: v_ldexp_f16 takes
a signed 16-bit exponent from the low half of S1, frexp and ldexp keep
the high destination half, and the packs ignore MODE apart from f16
input denormals. 180,224 comparisons with the exact model, no mismatches,
including the existing v_cvt_pknorm_u16_f32.

tools/check_half_exact_gpu.py runs the translated shaders on a D3D12 GPU
under eleven MODE values: 4,368 inputs, 39,312 results, zero mismatches.
The old float rounding fails 102 of 312 dispatches, a 32-bit ldexp
exponent 294, and full-word frexp/ldexp writes all 312.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NhDbTJdTcjebsL5u7pwFoY
@brcly
brcly merged commit bde76ad into brcly:main Nov 27, 2026
2 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.

2 participants