Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions tests/semantic/test_interp_computed_ij_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"""
from __future__ import annotations

import bisect
import math
import os
import pathlib
Expand Down Expand Up @@ -49,6 +50,27 @@ def pack_f16_pair(lo: float, hi: float) -> int:
return struct.unpack("<I", struct.pack("<ee", lo, hi))[0]


_F16_POS = [struct.unpack("<e", struct.pack("<H", b))[0] for b in range(0x7C00)]


def f16_rtz_bits(x: float) -> int:
"""Narrow ``x`` to f16 bits toward zero, matching v_interp_p2_f16 (SHADER-468)."""
if math.isnan(x):
return 0x7E00
if math.isinf(x):
return 0x7C00 | (0x8000 if x < 0 else 0)
sign = 0x8000 if x < 0 else 0
a = abs(x)
if a >= 65520.0:
return sign | 0x7BFF
i = bisect.bisect_right(_F16_POS, a) - 1
return sign | (i if i >= 0 else 0)


def f16_rtz(x: float) -> float:
return half_to_float(f16_rtz_bits(x))


def ulp32(x: float) -> float:
"""Binary32 ULP of f32(x) (subnormal-aware, distinct from Python's binary64 math.ulp)."""
v = abs(f32(x))
Expand Down Expand Up @@ -241,11 +263,12 @@ def test_perspective_fixture_branch_and_f16_halves(self):
self.assertLess(abs(got[0] - want_x), 2e-6)
self.assertLess(abs(got[1] - ref_y), 2e-6)
self.assertLess(abs(got[1] - want_y), 1e-4)
# On 64x64 (|alpha| + |beta| <= 32 px), 1.5 narrows to exact f16 (0 ulp); see 2048px test below for wide drift.
self.assertEqual(got[2], float(struct.unpack("<e", struct.pack("<e", ref_lo))[0]))
# On 64x64 (|alpha| + |beta| <= 32 px), 1.5 narrows toward zero (SHADER-468) to exact f16 (0 ulp);
# see 2048px test below for wide drift.
self.assertEqual(got[2], f16_rtz(ref_lo))
self.assertEqual(got[2], want_f16_lo)
self.assertEqual(got[3], float(struct.unpack("<e", struct.pack("<e", ref_hi))[0]))
self.assertLessEqual(abs(got[3] - float(struct.unpack("<e", struct.pack("<e", want_f16_hi))[0])), 2**-10)
self.assertEqual(got[3], f16_rtz(ref_hi))
self.assertLessEqual(abs(got[3] - f16_rtz(want_f16_hi)), 2**-10)

def test_j_written_between_ops_and_non_v0_v1_registers(self):
"""Computed I/J stored purely in v8/v9 (neither v0 nor v1 in frag_inputs) and J updated between p1 and p2."""
Expand Down Expand Up @@ -376,10 +399,13 @@ def make_lines(v10_imm: str) -> list[str]:
quad1, _ = perspective_quad_inputs(spv_one, self.VERTS_PERSP, {1: self.ATTR1_WORDS})
outs0 = run_quad(spv_zero, lambda lane: quad0[lane])
outs1 = run_quad(spv_one, lambda lane: quad1[lane])
# LO_HALVES = (1.5, 3.5, 5.5) at (0.25, 0.5) -> 1.5 + 0.25*2.0 + 0.5*4.0 = 4.0 (f16 0x4400).
# LO_HALVES = (1.5, 3.5, 5.5) at (0.25, 0.5) -> analytical 4.0 (0x4400); 1-pixel binary32 quad
# quantization lands ~3e-6..3e-7 below 4.0, which v_interp_p2_f16 narrows toward zero (SHADER-468) to 0x43ff.
for lane in range(4):
ref_p1lv, _, _ = quad_jacobian_ref64(quad0, lane, 0.25, 0.5, "%in_f16h1_0", 0)
self.assertEqual(outs0[lane]["%out_mrt0"], outs1[lane]["%out_mrt0"])
self.assertEqual(outs0[lane]["%out_mrt0"][0] & 0xFFFF, 0x4400)
self.assertEqual(outs0[lane]["%out_mrt0"][0] & 0xFFFF, f16_rtz_bits(ref_p1lv))
self.assertEqual(outs0[lane]["%out_mrt0"][0] & 0xFFFF, 0x43FF)

def test_1080p_perspective_precision_subpixel_vs_full_viewport_jump(self):
"""At 1920x1080 with perspective W = (1.0, 2.5, 4.0), sub-pixel barycentric shifts stay within ~5e-6, while
Expand Down
Loading