Commit Graph

12 Commits

Author SHA1 Message Date
63a3cd6b75 fix(tests): preserve 64-bit double precision in spirv_interp glsl() 2026-11-22 19:24:00 +02:00
e0d00812f7 Merge origin/main into shader35-mode-round
Conflicts in tools/rdna2spirv.py (f16_out keeps main's fract flag and narrows with f16_res; v_interp_p2_f16 keeps main's clamp and narrows with f16_res; v_fma_mix goes through main's f16_out) and docs/design.md (main's new rows kept, our f16 rounding and Known approximations rows kept).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 09:05:53 +01:00
1293cc2656 Translator: hardware NaN rules for float min/max/med3, v_fract below 1.0
Measured on an AMD Radeon RX 6800 (gfx1030) with the hw-oracle tool from the
AnyPS5 project, all four IEEE/DX10_CLAMP combinations, wave32 and wave64:

- v_min/v_max f32/f16, each half of v_pk_min/max_f16, and v_min3/v_max3 now
  emit NMin/NMax (one NaN operand gives the other) instead of FMin/FMax.
- v_med3_f32/_f16: FClamp(z, NMin(x,y), NMax(x,y)), and with OpIsNan of each
  operand that can be a NaN, select NMin(NMin(x,y), z) (hardware: min3 when
  any operand is NaN). Non-NaN constants get no check; no select when none.
- v_fract_f32: Fract, then 1.0 -> 0x3f7fffff by compare and select (the NaN
  of fract(+-inf) is kept). v_fract_f16: the same, then a packed 0x3c00 ->
  0x3bff (not after an output multiplier).

Other FMin/FMax/FClamp uses (clamp bit, LOD and conversion clamps) are
unchanged. GPU fixtures ps_fnan_med3 and ps_fnan_fract read their inputs
from memory; on the RX 6800 the two med3 words differed before this change.
tests/semantic/test_float_nan_fract.py runs the measured rows; both
interpreters gain NMin/NMax, and spirv_mini_interp OpIsNan and exact Fract.

tests/semantic/test_alu_med3.py (merged after this branch was cut): its float
reference used Python min/max, which return the first argument for equal
zeros. Hardware orders -0 below +0 (RX 6800: med3(-0, +0, 1.0) = +0,
med3(+0, -0, -1.0) = -0 in every operand order), so the reference now sorts
with that order, and spirv_mini_interp's FMin/FMax/FClamp order zeros the same
way as its NMin/NMax.

Rebased onto 8df277f. Two upstream tests were written against the old
behaviour and are updated: tests/semantic/test_valu_f32_min3_max3_mad.py
(its min3/max3 reference used first-operand order for mixed zeros, and its
negative control replaced FMin/FMax; now -0 < +0, as measured on the RX 6800,
and the control swaps NMin/NMax) and tests/semantic/test_valu_f16_round.py
(it asserted that the interpreter rejects Fract; it now asserts the value).
Upstream had added an OpIsNan/OpIsInf branch to spirv_mini_interp, so this
commit's own OpIsNan branch is dropped (it shadowed upstream's). docs/design.md
notes that an output modifier on v_fract_f16 is applied before narrowing to
f16, an order not measured on hardware.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 22:25:05 -07:00
e157fddad9 Translator: MODE f16/f64 rounding (s_setreg FP_ROUND[3:2]) (SHADER-35)
The game sets MODE.FP_ROUND[3:2] (f16/f64 rounding) inside its shaders: 9,417 writes in 6,333 of 26,503
shaders, 6,694 toward zero (3) and 2,723 back to nearest even (0); no other MODE field, no s_denorm_mode or
s_round_mode. The translator ignored it.

- The field is kept in a per-invocation Function variable (%mode_r16) when the program writes it.
- Every VALU f16 result (f16_out, v_pk_*_f16 via f16_pack2, v_cvt_f16_f32, v_sin/cos_f16, v_interp_p2_f16,
  v_fma_mix{lo,hi}_f16) goes through f16_res: main's integer f16_bits, or f16_bits(rtz=True) while the mode
  is toward zero. Integer ops only either way (SHADER-56).
- f16 add/sub and v_pk_add_f16 step the f32 sum one ulp toward zero when its exact TwoSum error shows it
  rounded away from zero (2 - 2^-24 is an f32 tie that rounds to 2.0; toward zero it is 0x3FFF). f16 FMAs
  need nothing extra: fma_f16_odd's round-to-odd f32 value (SHADER-55) narrows exactly in either direction.
- v_add_f64 steps one ulp toward zero the same way (finite overflow -> +-DBL_MAX), on the raw sum, before
  clamp/omod.
- Other MODE writes (other bits, partial field, +inf/-inf, register values), s_denorm_mode and s_round_mode
  raise Unsupported; none occur in the corpus.
- Shaders that never write MODE emit the same code.

tests/test_mode_round.py; tests/spirv_interp.py gains binary64 arithmetic, double <-> v2uint bitcasts and
vector Fma/FMin/FMax/FClamp. docs/design.md: Known approximations and the f16 rounding row.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 03:11:57 +01:00
78475e9d55 Merge pull request #606 from bbareth 2026-11-21 20:38:55 -05:00
6f4bd2a9bb Merge remote-tracking branch 'origin/main' into shader-55-56
# Conflicts:
#	docs/design.md
#	tests/spirv_interp.py
#	tools/rdna2spirv.py
2026-11-22 02:22:31 +01:00
298a69da7e Merge pull request #551 from eIBart0 2026-11-21 20:09:19 -05:00
bad14edbef tests/spirv_interp.py: OpBitcast keeps signalling-NaN bits (#509, as #533)
u2f()/f2u() went through struct's binary32 <-> double conversion, which
quiets a signalling NaN (sets bit 22), so a word in 0x7f800001..
0x7fbfffff / 0xff800001..0xffbfffff that passed through an OpBitcast
came back changed: 0xffa3a095 became 0xffe3a095. This is the second
interpreter (test_values, test_f16_op_sel_values, test_f16_hw_words,
test_interp_p1_p2); #533 fixed the same bug in spirv_mini_interp.

Same fix: u2f() returns a float subclass that keeps the pattern and
f2u() returns it; copies, selects, composites and stores pass it on,
and arithmetic gives a plain float. New test_spirv_interp_bits.py
checks four sNaN words through every pass-through path, OpIsNan, and
that arithmetic still gives a NaN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-22 02:08:48 +01:00
8b5ef5f62a Part 2: narrow f16 ALU results with integer ops, not PackHalf2x16 (SHADER-56, #513)
Drivers fold PackHalf2x16 -> UnpackHalf2x16 to a no-op, so a following f16 op saw the unrounded f32 (#513:
v_pk_add_f16 -> v_pk_mul_f16 1 ulp off under --wave64-emulate on NVIDIA; v_cvt_pkrtz_f16_f32 rounding to
nearest on llvmpipe). Gen.f16_bits converts with integer ops (RTE, or RTZ with +-65504 for pkrtz) and is used
by f16_out, v_cvt_f16_f32, v_pk_*_f16, f16 sin/cos, v_interp_p2_f16, v_fma_mix* and f16_rtz.
tests/spirv_interp.py gains OpU{Less,Greater}ThanEqual and OpFUnordNotEqual.
2026-11-22 01:53:21 +01:00
8ad2ed54ad rdna2spirv: pack v_pk_*_f16 and f16_out with integer IEEE-754 RTE (SHADER-70)
Closes #513
2026-11-22 00:41:06 +00:00
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
439887f04f Tests: check computed values with a small SPIR-V interpreter
tests/spirv_interp.py runs the straight-line, single-lane SPIR-V the translator
emits for branch-free shaders (binary32 rounding after every op, GLSL.std.450
subset, Function/Output variables). Unknown opcodes raise NotImplementedError.

- test_values.py: 49 value checks over the supported instruction families, with
  references written in plain Python from the ISA definitions.
- test_f16_op_sel_values.py: 84 value checks for op_sel on f16 VOP3 ops (all
  select combinations, neg/abs/clamp, fmac).
- listing.py: hand-written listing helper. shader_ir.parse_file treats the first
  line of a listing as its header, so the helper writes one; test_f16_op_sel.py
  used to lose its first instruction and now uses the helper.

None of these need llvm-mc or spirv-tools. Mutation-checked: breaking the
op_sel source index or the destination merge makes them fail.
2026-11-21 17:46:28 +01:00