Files
mandy/runtime/test/shaders/ps_f16_const.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

21 lines
1013 B
NASM

; Inline float constants in f16 ops are f16 values: the constant sits in the low half of the operand and the high
; half is zero (so the high half that op_sel selects from a constant reads +0.0). They are not f32 bit patterns.
; v0 = 0.25 | 3.0 << 16 (low | high half); destinations start as 0xaaaa5555.
; v_add_f16 1.0 + 0.25 = 1.25
; v_med3_f16 v0, -4.0, 0.5 [1,0,0,1]: med(3, -4, 0.5) = 0.5 into the high half
; v_fma_f16 v0, 2.0, 0.5 [0,0,1,0]: 0.25 * 2 + (high half of the constant = 0) = 0.5
; v_cvt_f32_f16 0.5 = 0x3f000000
; The words are the ones an RX 6800 (gfx1030) gave for this body run as a raw kernel, wave32 and wave64.
; Expected mrt0 bits: 0xaaaa3d00, 0x38005555, 0xaaaa3800, 0x3f000000
v_mov_b32 v0, 0x42003400
v_mov_b32 v2, 0xaaaa5555
v_mov_b32 v3, 0xaaaa5555
v_mov_b32 v4, 0xaaaa5555
v_mov_b32 v5, 0xaaaa5555
v_add_f16 v2, 1.0, v0
v_med3_f16 v3, v0, -4.0, 0.5 op_sel:[1,0,0,1]
v_fma_f16 v4, v0, 2.0, 0.5 op_sel:[0,0,1,0]
v_cvt_f32_f16 v5, 0.5
exp mrt0 v2, v3, v4, v5 done vm
s_endpgm