upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/semantic/test_buffer_load_format.py
diff options
context:
space:
mode:
authorBetterJdue <BetterJdue@users.noreply.github.com>2026-11-22 14:36:40 +0100
committerClaude <noreply@anthropic.com>2026-11-22 14:50:45 +0100
commit9fa652f7a26febbfaa4e44e8876446cba8ef4e31 (patch)
tree921b6e35d60613c8c2440173abaa6b179c72203e /tests/semantic/test_buffer_load_format.py
parent89b047cd8fab50675a43d76722a6a8a881f55dc2 (diff)
d16 typed buffer loads round the exact UNORM/SNORM quotient once, as RDNA2 does (BUILD-161)
docs/design.md narrowed the f16 of a typed d16 buffer load from the f32 decode with round to nearest even, and called that rounding inferred, not measured. tools/d16_format_probe.py measures it through tools/hw_oracle: every UNORM8, SNORM8, UNORM10, UNORM16 and SNORM16 value through buffer_load_format_d16_x, tbuffer_load_format_d16_x and buffer_load_format_x, in wave32 and wave64, against several candidate rules, with a 16_FLOAT load, the kept high half of d16_x and the staged inputs as controls. On an RX 6700 (gfx1031) the hardware rounds the exact quotient once, straight to f16. Rounding to f32 first is 1 ulp high on exactly four values (UNORM16 0xffaf and 0xffef, SNORM16 0x7ff7 and 0x8009); over every width up to 16 bits only two f32 quotients land on an f16 halfway point (0x3f7ff000, 0x3f7fb000). Gen.d16_norm_f16 takes the f16 below for those two, for normalized formats only. The f32 (non-d16) result was already right. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests/semantic/test_buffer_load_format.py')
-rw-r--r--tests/semantic/test_buffer_load_format.py41
1 files changed, 38 insertions, 3 deletions
diff --git a/tests/semantic/test_buffer_load_format.py b/tests/semantic/test_buffer_load_format.py
index c5cf73a70..30f32bb31 100644
--- a/tests/semantic/test_buffer_load_format.py
+++ b/tests/semantic/test_buffer_load_format.py
@@ -33,6 +33,7 @@ MEMORY = {BASE: 0x11223344, BASE + 4: 0xDEADBEEF, BASE + 8: 0xCAFEF00D, BASE + 1
33# V# FORMAT codes (word3 bits 12-18), from rdna2spirv.FORMATS 33# V# FORMAT codes (word3 bits 12-18), from rdna2spirv.FORMATS
34FMT_U32, FMT_U32X2, FMT_U32X4, FMT_UINT8X4, FMT_UINT16X2, FMT_UINT16X4, FMT_F16 = 20, 62, 75, 60, 27, 69, 13 34FMT_U32, FMT_U32X2, FMT_U32X4, FMT_UINT8X4, FMT_UINT16X2, FMT_UINT16X4, FMT_F16 = 20, 62, 75, 60, 27, 69, 13
35FMT_F32, FMT_F32X2, FMT_F32X3, FMT_UNORM8, FMT_UNORM8X4 = 22, 64, 74, 1, 56 35FMT_F32, FMT_F32X2, FMT_F32X3, FMT_UNORM8, FMT_UNORM8X4 = 22, 64, 74, 1, 56
36FMT_UNORM16, FMT_SNORM16, FMT_UNORM16X2 = 7, 8, 23
36DST_SEL_XYZW = 4 | 5 << 3 | 6 << 6 | 7 << 9 37DST_SEL_XYZW = 4 | 5 << 3 | 6 << 6 | 7 << 9
37# DST_SEL (X, 0, 1, 1): code 4, 0, 1, 1 — SHADER-86 measures float 1.0 vs integer 1 38# DST_SEL (X, 0, 1, 1): code 4, 0, 1, 1 — SHADER-86 measures float 1.0 vs integer 1
38DST_SEL_X_0_1_1 = 4 | 0 << 3 | 1 << 6 | 1 << 9 39DST_SEL_X_0_1_1 = 4 | 0 << 3 | 1 << 6 | 1 << 9
@@ -150,9 +151,9 @@ class D16DestinationHalves(unittest.TestCase):
150 151
151 152
152class D16NormalizedRounding(unittest.TestCase): 153class D16NormalizedRounding(unittest.TestCase):
153 """d16 narrows a normalized value to f16 with round to nearest even, like the d16 image results (#489; inferred 154 """d16 narrows a normalized value to f16 with round to nearest even (#489; measured for typed buffer loads on an
154 for the hardware, not measured). These bytes are among the about half of unorm8 values where round toward zero, 155 RX 6700, BUILD-161). These bytes are among the about half of unorm8 values where round toward zero, which
155 which PackHalf2x16 becomes on AMD drivers, gives 1 ulp less.""" 156 PackHalf2x16 becomes on AMD drivers, gives 1 ulp less."""
156 157
157 def test_unorm8_x(self): 158 def test_unorm8_x(self):
158 # byte 5 is 0xbe: 190/255 is 0x39f6 to nearest even, 0x39f5 toward zero 159 # byte 5 is 0xbe: 190/255 is 0x39f6 to nearest even, 0x39f5 toward zero
@@ -163,6 +164,40 @@ class D16NormalizedRounding(unittest.TestCase):
163 self.assertEqual(load_format(FMT_UNORM8X4, 4, 4, d16=True)[:2], (0x39F63B7F, 0x3AF7396D)) 164 self.assertEqual(load_format(FMT_UNORM8X4, 4, 4, d16=True)[:2], (0x39F63B7F, 0x3AF7396D))
164 165
165 166
167class D16NormalizedRoundsOnce(unittest.TestCase):
168 """BUILD-161, measured on an RX 6700 (gfx1031) with tools/d16_format_probe.py: d16 rounds the exact UNORM/SNORM
169 quotient once, straight to f16. The f32 decode followed by f16_bits rounds twice, which is 1 ulp high where the
170 f32 lands exactly on an f16 halfway point: UNORM16 0xffaf and 0xffef, SNORM16 0x7ff7 and 0x8009."""
171
172 def d16_x(self, fmt, half, op="buffer_load_format", mods=""):
173 return load_format(fmt, 0, 1, memory={BASE: half}, d16=True, op=op, mods=mods)[0]
174
175 def test_unorm16_and_snorm16_halfway_values(self):
176 for fmt, half, want in ((FMT_UNORM16, 0xFFEF, 0x3BFF), (FMT_UNORM16, 0xFFAF, 0x3BFD),
177 (FMT_SNORM16, 0x7FF7, 0x3BFF), (FMT_SNORM16, 0x8009, 0xBBFF)):
178 with self.subTest(fmt=fmt, half=hex(half)):
179 self.assertEqual(self.d16_x(fmt, half), 0x12340000 | want)
180
181 def test_mtbuf_takes_the_same_rule(self):
182 self.assertEqual(self.d16_x(FMT_UNORM16, 0xFFEF, "tbuffer_load_format", " format:[BUF_FMT_16_UNORM]"),
183 0x12343BFF)
184 self.assertEqual(self.d16_x(FMT_SNORM16, 0x8009, "tbuffer_load_format", " format:[BUF_FMT_16_SNORM]"),
185 0x1234BBFF)
186
187 def test_neighbours_and_the_f32_result_are_unchanged(self):
188 self.assertEqual(self.d16_x(FMT_UNORM16, 0xFFEE), 0x12343BFF) # 65518/65535: not a halfway f32
189 self.assertEqual(self.d16_x(FMT_UNORM16, 0xFFFF), 0x12343C00)
190 self.assertEqual(load_format(FMT_UNORM16, 0, 1, memory={BASE: 0xFFEF})[0], 0x3F7FF000)
191
192 def test_a_float_format_on_the_halfway_value_still_ties_to_even(self):
193 self.assertEqual(self.d16_x(FMT_F32, 0x3F7FF000), 0x12343C00)
194 self.assertEqual(self.d16_x(FMT_F32, 0xBF7FB000), 0x1234BBFE)
195
196 def test_both_halves_of_a_pair(self):
197 got = load_format(FMT_UNORM16X2, 0, 2, memory={BASE: 0xFFAF << 16 | 0xFFEF}, d16=True)[0]
198 self.assertEqual(got, 0x3BFD3BFF)
199
200
166class CrossingDwordBoundary(unittest.TestCase): 201class CrossingDwordBoundary(unittest.TestCase):
167 """An element that starts mid-dword must read the following dwords, not re-read dword 0. 202 """An element that starts mid-dword must read the following dwords, not re-read dword 0.
168 203