Files
mandy/runtime/test/shaders/ps_f16_pack.asm
shelbykb2 3a83c29eae GPU tests for f16 op_sel and partial writes, hardware-measured; f16 inline constants fixed (SHADER-13)
Covers the "16-bit results" contract row, which ended with "not yet GPU-verified".

Hardware evidence: AMD RX 6800 (gfx1030), the instructions run as a raw compute kernel in wave32 and
wave64, IEEE off, DX10 clamp on, denormals kept, round to nearest. The bytes assemble identically for
gfx1013 and gfx1030 (llvm-mc 21; the kernels themselves were assembled by the driver's comgr).
- v_med3_f16: all 16 op_sel patterns, neg/abs on a selected high half, clamp, destination also a source.
- v_add/sub/mul/max/min_f16 (_e32, _e64 with neg/abs/clamp), v_fmac_f16 (_e32, _e64), f16 unary
  (rndne/floor/ceil/trunc/fract/sqrt/rcp/rsq, _e32 and _e64), v_cvt_f16_f32: low halves in, low half out,
  high half of the destination kept; the v_fmac_f16 accumulator is the low half of the destination.
  336 words per wave size, all as the rule says and as the translator computes.
- Not expressible: op_sel on the VOP2/VOP1 ops in VOP3 encoding (v_add/sub/mul/max/min_f16_e64,
  v_fmac_f16_e64, unary _e64). llvm-mc rejects the modifier and its disassembler drops the bits, so
  nothing was measured there and nothing was hand-encoded.

Translator fix (tools/rdna2spirv.py, new src_h): an inline float constant in an f16 operand is the f16
value in the low half over a zero high half. The translator unpacked the f32 pattern, whose low half is
zero, so v_add_f16 d, 1.0, v added 0.0 and v_cvt_f32_f16 d, 0.5 gave 0. Measured for v_add/mul/max_f16,
v_sub_f16_e64, v_fma_f16, v_fmac_f16, v_rcp_f16, v_med3_f16, v_cmp_lt_f16, v_cvt_f32_f16, v_cos_f16,
v_pk_add/mul_f16 and v_pack_b32_f16 (0.15915494 is 0x3118); the high half op_sel selects from a constant
reads +0.0. v_fma_mix* operands were not
measured and are unchanged.

Tests:
- runtime/test/shaders/ps_f16_{med3_opsel,med3_mods,vop2,vop2b,vop1,vop1b,const,const2,pack}.asm, wired into
  check.py and scripts/run_gpu_tests.py. Their expected words are the ones the hardware gave for the same
  instructions. Vulkan, AMD Radeon RX 6800, Windows, driver 26.11.2: 9/9 pass; with one expected word
  flipped per test 0/9 pass; without the translator fix ps_f16_const, ps_f16_const2 and ps_f16_pack fail.
- tests/test_f16_hw_words.py: header, check.py and run_gpu_tests.py hold the hardware words; a
  plain-Python reference of the rule and the translator (tests/spirv_interp.py) both give them. 12 of its
  tests fail without the fix.
- tests/semantic: ps_f16_vop2 fixture and value test; the v_sin_f16 mutation anchor follows the edit.
- tests/spirv_interp.py: Floor/Ceil/Trunc/RoundEven keep the sign of a zero result (trunc(-0.75) is
  0x8000 on the hardware and on Vulkan), Sin/Cos, component-wise vector arithmetic.
- python -m unittest discover -s tests/semantic: 388 -> 389, OK. pytest tests: 1467 -> 1514 passed.
  scripts/run_gpu_tests.py (test tools rebuilt): 48/49 -> 57/58 passed, 1 skipped.

docs/design.md: the "16-bit results" row says what was verified and what is not expressible; new row
"f16 inline constants".

AI-assisted.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 17:17:58 -07:00

17 lines
908 B
NASM

; v_pack_b32_f16 with inline float constants and op_sel: dst = half of src0 | half of src1 << 16. A constant is
; an f16 value in the low half of the operand with a zero high half (see ps_f16_const).
; v0 = 0.25 | 3.0 << 16, v1 = 4.0 | 0.25 << 16 (low | high half).
; 1.0, v0: 0x3c00 | 0x3400 << 16 v0, 0.5: 0x3400 | 0x3800 << 16
; 2.0, v0 op_sel:[1,0,0]: the high half of the constant (0) | 0x3400 << 16
; v0, v1 op_sel:[1,0,0]: the high half of v0 (0x4200) | the low half of v1 (0x4400) << 16
; The words are the ones an RX 6800 (gfx1030) gave for this body run as a raw kernel, wave32 and wave64.
; Expected mrt0 bits: 0x34003c00, 0x38003400, 0x34000000, 0x44004200
v_mov_b32 v0, 0x42003400
v_mov_b32 v1, 0x34004400
v_pack_b32_f16 v2, 1.0, v0
v_pack_b32_f16 v3, v0, 0.5
v_pack_b32_f16 v4, 2.0, v0 op_sel:[1,0,0]
v_pack_b32_f16 v5, v0, v1 op_sel:[1,0,0]
exp mrt0 v2, v3, v4, v5 done vm
s_endpgm