"""Value-level regression tests for the recent RDNA2 -> SPIR-V semantic fixes. Each pixel shader ends with `exp mrt0 v.., v.., v.., v..`, so four 32-bit words are checked per shader. The expectations are written down by hand as constants and recomputed by plain-Python reference functions (checked against each other in OracleTests). Neither the constants nor the references use the translator or the SPIR-V interpreter. ISA semantics used (AMD RDNA2 ISA reference, VALU/VOP3/VOP3P chapters): v_mul_legacy_f32 0 * anything, inf/NaN included, is +0.0; otherwise a normal IEEE binary32 multiply. v_lshlrev_b16 D.lo = (S1.lo << (S0.lo & 15)) & 0xffff; the VOP3 form keeps D's high half. v_lshrrev_b16 same with >>; v_ashrrev_i16 sign-extends S1.lo before the arithmetic shift. v_pk_lshrev_b16 / per half: D.h = (S1.half <> (S0.half & 15)) & 0xffff. op_sel picks the source half v_pk_lshlrev_b16 / used for the low result half, op_sel_hi for the high result half. v_pk_ashrrev_i16 v_add_nc_u16 D.lo = (S0.lo + S1.lo) & 0xffff; the VOP3 form keeps D's high half (carry is dropped). v_sin_f16/v_cos_f16 input is a number of turns: sin/cos(S * 2*pi), rounded to binary16; high half kept. v_add/sub/mul_f16, low halves in, D.lo out, D's high half kept; neg/abs in the VOP3 form apply to the low v_fmac_f16 half that is read. v_fmac_f16: D.lo = S0.lo * S1.lo + D.lo. The ps_f16_vop2 words were measured on an RX 6800 (gfx1030), wave32 and wave64 (see its .asm header). s_buffer_load_dwordx2 with destination vcc: dword 0 -> vcc_lo, dword 1 -> vcc_hi. v_interp_mov_f32 reads the flat (provoking-vertex) input attribute; the translator must declare it Flat. v_perm_b32 byte i = BYTE_PERMUTE({S0, S1}, S2 byte i) over 8 bytes, S1 = bytes 0-3: selector 0-7 picks that byte; 8/9/10/11 give 0xff if bit 7 of byte 1/3/5/7 is set, else 0; 12 gives 0; 13 and above give 0xff. v_alignbit_b32 D = ({S0, S1} >> (S2 & 31)) & 0xffffffff. v_med3_i32/u32 the middle of three values, compared signed or unsigned. v_mul_hi_u32/i32 bits 63:32 of the unsigned or signed 64-bit product. v_bcnt_u32_b32 D = popcount(S0) + S1. v_ffbl_b32/ffbh_u32 index of the lowest set bit / count of leading zeros; 0xffffffff when S0 = 0. v_xad_u32/add3_u32 D = (S0 ^ S1) + S2 / D = S0 + S1 + S2, both mod 2^32. s_min/max_i32/u32 D = S0 < S1 ? S0 : S1 (max: >), SCC = 1 when S0 is chosen, so a tie gives S1 and SCC 0. v_fmamk_f32/madmk_f32 D = S0 * K + S1, K the literal (printed vdst, src0, K, vsrc1); fmamk is fused. v_fmaak_f32/madak_f32 D = S0 * S1 + K (printed vdst, src0, vsrc1, K). s_ff1_i32_b32 / index of the lowest set / clear bit of S0, -1 (0xffffffff) when there is none. An inline s_ff0_i32_b32 constant or literal source is a 32-bit value, not a lane mask (SHADER-46). v_mad_u32_u16 / D = S0.lo * S1.lo + S2 mod 2^32, the halves zero- (u16) or sign-extended (i16); op_sel bit v_mad_i32_i16 0/1 reads the high half of S0/S1. D is a whole dword (nothing of the old value is kept). v_subrev_f16 D.lo = S1.lo - S0.lo; the high half of D is kept in the VOP2 and the VOP3 form. s_*_saveexec_b32/b64, a constant used as a lane mask gives lane L its bit L: of the 32-bit value for a b32 op, s_andn2_wrexec_*, of the 64-bit value for a b64 op (inline constants sign-extended, literals with a zero s_mov/s_not/s_and_b64 high word). cs_w32_mask_imm / cs_w64_mask_imm run every lane of a wave (LaneMaskConstants). into EXEC or VCC The appended cases for ps_ops5/ps_ops6 also pin the op_sel source-half selection for packed shifts, which the GPU-checked ps_ops4 does not exercise. cs_bounds (compute) pins the buffer num_records bounds checks: an out-of-bounds store must not modify memory and an out-of-bounds load must return 0. cs_fmt_1010102 (compute) pins the channel order of the 10:10:10:2 typed-buffer formats: the names list the channel widths from the top bit, so 2_10_10_10 (50) is R10G10B10A2 and 10_10_10_2 (44) has a 2-bit x (Mesa gfx10_format_table.py, ac_formats.c). """ import math import os import struct import sys import unittest import support import spirv_mini_interp as si # Independent constants, written down by hand (derivations are in the reference functions). EXPECTED_LEGACY_MUL = [0x00000000, 0x00000000, 0x00000000, 0x40C00000] EXPECTED_OPS4 = [0x34000010, 0x00120800, 0xBEEF0000, 0x12343C00] EXPECTED_OPS5 = [0x12340010, 0xBEEF0800, 0xAAAAFFFF, 0xDEADBC00] EXPECTED_OPS6 = [0xF800FFFF, 0x01000010, 0x00800800, 0x8000FFF0] EXPECTED_OPS7 = [0x00FF8077, 0xFF0000FF, 0x89ABCDEF, 0x00070003] EXPECTED_OPS8 = [0x80000000, 0xBFFFFFFF, 0x000F100E, 0x0F0F0F1F] EXPECTED_VCC_X2 = [0x11223344, 0x55667788, 0x00000000, 0x00000000] EXPECTED_F16_VOP2 = [0xAAAA4000, 0xAAAAC000, 0xAAAA3A00, 0x42003F00] # hardware-measured EXPECTED_FLAT = [0x00000101, 0x00000102, 0x3F000000, 0x3E800000] EXPECTED_SCC_MINMAX = [0x00030205, 0x00030207, 0x00030209, 0x00030203] EXPECTED_FMA_LITERAL = [0x41B80000, 0x41800000, 0x40E00000, 0x41A40000] EXPECTED_BOUNDS = {0x00: 0x12345678, 0x04: 0x12345678, 0x08: 0x00000000, 0x0C: 0xEEEEEEEE} EXPECTED_FMT_1010102 = {0x00: 0x00000000, 0x04: 0x3F800000, 0x08: 0x00000000, 0x0C: 0x3F800000, 0x10: 0x3F800000, 0x14: 0x00000000, 0x18: 0x3F800000, 0x1C: 0x00000000, 0x20: 0x00000000, 0x24: 0x3F800000, 0x28: 0x00000000, 0x2C: 0x3F800000, 0x30: 0xC00FFC00, 0x34: 0x003FF003, 0x38: 0xE00003FF, 0x3C: 0xFFE00003} EXPECTED_SFF_IMM = [0x00000002, 0x00000001, 0x00000014, 0x00000010] EXPECTED_VALU16_MAD = [0x00000002, 0x00030002, 0x00008000, 0x00000004] EXPECTED_VALU16_SUBREV = [0xAAAABC00, 0xAAAAC000, 0xAAAA3C00, 0x42003800] def _f32_bits(x): return struct.unpack("> (16 * n)) & 0xFFFF def one(shalf, vhalf): amount = half(sh, shalf) & 15 x = half(val, vhalf) if kind == "ashr": return (_s16(x) >> amount) & 0xFFFF return ((x << amount) if kind == "lshl" else (x >> amount)) & 0xFFFF return one(sel[0], sel[1]) | (one(selhi[0], selhi[1]) << 16) def reference_ops4(): v0, shift = 0x12348001, 0x00080004 sin = _half(math.sin(_unhalf(0x3400) * 2.0 * math.pi)) # 0.25 turns -> 1.0 add = (0xBEEF0000 & 0xFFFF0000) | ((0x0001 + 0xFFFF) & 0xFFFF) # carry out of the low half is dropped return [_pk_shift("lshl", shift, v0), _pk_shift("lshr", shift, v0), add, 0x12340000 | sin] def reference_ops5(): lshl = 0x12340000 | ((0x8001 << 4) & 0xFFFF) lshr = 0xBEEF0000 | ((0x8001 >> 4) & 0xFFFF) ashr = 0xAAAA0000 | ((_s16(0xFFF0) >> 4) & 0xFFFF) cos = 0xDEAD0000 | _half(math.cos(_unhalf(0x3800) * 2.0 * math.pi)) # 0.5 turns -> -1.0 return [lshl, lshr, ashr, cos] def reference_ops6(): return [_pk_shift("ashr", 0x00040004, 0x8000FFF0), # op_sel:[1,0,0] -> low result shifts by the high half of src0; op_sel_hi:[0,0,0] -> high result # shifts by the low half. Both use the low half of the value. _pk_shift("lshl", 0x00040008, 0x12348001, sel=(1, 0), selhi=(0, 0)), _pk_shift("lshr", 0x00040008, 0xBEEF8001, sel=(1, 0), selhi=(0, 0)), _pk_shift("ashr", 0, 0x8000FFF0)] def _perm(s0, s1, sel): data = list(s1.to_bytes(4, "little")) + list(s0.to_bytes(4, "little")) out = 0 for i in range(4): k = (sel >> (8 * i)) & 0xFF if k >= 13: b = 0xFF elif k == 12: b = 0 elif k >= 8: b = 0xFF if data[2 * (k - 8) + 1] & 0x80 else 0 else: b = data[k] out |= b << (8 * i) return out def _s(v): return v - (1 << 32) if v & 0x80000000 else v def reference_ops7(): m = 0xFFFFFFFF med_i = sorted([_s(0xFFFFFFFB), 7, 3])[1] & m med_u = sorted([0xFFFFFFFB, 7, 3])[1] return [_perm(0x80112233, 0x4455E677, 0x0C0D0700), _perm(0x80112233, 0x4455E677, 0x0B0A0908), # sign of bytes 1, 3, 5, 7: E6, 44, 22, 80 (((0x12345678 << 32) | 0x9ABCDEF0) >> (36 & 31)) & m, ((med_u << 16) + med_i) & m] def reference_ops8(): m = 0xFFFFFFFF bcnt = bin(0xF0F00001).count("1") + 5 ffbl = (0x00010000 & -0x00010000).bit_length() - 1 ffbh = 32 - 0x00010000.bit_length() xad = ((0xF0F0F0F0 ^ m) + 1) & m signed_high = ((_s(0xFFFFFFFE) * 0x40000000) >> 32) & m return [(0x80000001 * 0xFFFFFFFF) >> 32, signed_high ^ 0x40000000, ((ffbh << 16) + (ffbl << 8) + bcnt) & m, (xad + 16 + m) & m] # v_ffbh_u32(0) = 0xffffffff def reference_scc_minmax(): def minmax(op, x, y): """(D, SCC) of s_min/s_max _i32/_u32 on 32-bit words: SCC = 1 only when S0 is the one chosen.""" kx, ky = (_s(x), _s(y)) if op.endswith("i32") else (x, y) pick = kx < ky if op.startswith("min") else kx > ky return x if pick else y, int(pick) words = [] m1 = 0xFFFFFFFF # the inline constant -1 as a register word for op, tie, strict in (("min_i32", 5, (m1, 1)), ("max_i32", 7, (1, m1)), ("min_u32", 9, (1, m1)), ("max_u32", 3, (m1, 1))): d, scc_tie = minmax(op, tie, tie) _, scc_strict = minmax(op, *strict) words.append(d | ((1 if scc_tie else 2) << 8) | ((3 if scc_strict else 4) << 16)) return words def reference_fma_literal(): # Every product and sum is exact in f32, so fused and unfused give the same bits. k1, k2 = 10.0, 0.5 return [_f32_bits(2.0 * k1 + 3.0), _f32_bits(2.0 * 3.0 + k1), # fmamk, fmaak _f32_bits(4.0 * k2 + 5.0), _f32_bits(4.0 * 5.0 + k2)] # madmk, madak def reference_f16_vop2(): a, b, acc = _unhalf(0x3E00), _unhalf(0x3800), _unhalf(0x3C00) # low halves: 1.5, 0.5, accumulator 1.0 return [0xAAAA0000 | _half(a + b), 0xAAAA0000 | _half(-a - abs(b)), 0xAAAA0000 | _half(a * b), 0x42000000 | _half(a * b + acc)] def reference_vcc_x2(): return [0x11223344, 0x55667788, 0, 0] def reference_flat(): return [0x101, 0x101 + 1, _f32_bits(0.5), _f32_bits(0.25)] def reference_bounds(): # num_records = 16: store 0 and load 0 are in bounds, the store and load at offset 16 are not. The OOB load # feeds a store of 0 into offset 8; the OOB store must leave offset 16 untouched (it is not in the map). return {0x00: 0x12345678, 0x04: 0x12345678, 0x08: 0, 0x0C: 0xEEEEEEEE} WIDTHS_1010102 = {50: (10, 10, 10, 2), # 2_10_10_10_UNORM, R10G10B10A2 44: (2, 10, 10, 10)} # 10_10_10_2_UNORM def _unorm_unpack(word, widths): out, bit = [], 0 for w in widths: out.append(_f32_bits(((word >> bit) & ((1 << w) - 1)) / ((1 << w) - 1))) bit += w return out def _unorm_pack(values, widths): word, bit = 0, 0 for v, w in zip(values, widths): word |= round(min(max(v, 0.0), 1.0) * ((1 << w) - 1)) << bit bit += w return word def reference_fmt_1010102(): # Every field is 0 or full scale, so the decoded floats are exact. src50, src44, stored = 0xC00FFC00, 0x003FF003, (1.0, 0.0, 0.5, 1.0) words = (_unorm_unpack(src50, WIDTHS_1010102[50]) + _unorm_unpack(src44, WIDTHS_1010102[44]) + _unorm_unpack(src50, WIDTHS_1010102[50]) + [src50, src44, _unorm_pack(stored, WIDTHS_1010102[50]), _unorm_pack(stored, WIDTHS_1010102[44])]) return {4 * i: w for i, w in enumerate(words)} def _ff1(x): """Index of the lowest set bit of a 32-bit value, 0xffffffff if none.""" x &= 0xFFFFFFFF return (x & -x).bit_length() - 1 if x else 0xFFFFFFFF def reference_sff_imm(): # ps_sff_imm: s_ff1 of the inline constant 4 and of the literal 0x00100000; s_ff0 of the inline constant 61 # (0b111101) and of the literal 0x0000ffff (the lowest clear bit is the lowest set bit of ~S0). return [_ff1(4), _ff1(~61), _ff1(0x00100000), _ff1(~0x0000FFFF)] def _mad_32_16(signed, s0, s1, s2, sel=(0, 0)): """v_mad_u32_u16 / v_mad_i32_i16: the selected 16-bit halves, extended, multiplied, plus S2, mod 2^32.""" a, b = (s0 >> (16 * sel[0])) & 0xFFFF, (s1 >> (16 * sel[1])) & 0xFFFF if signed: a, b = _s16(a), _s16(b) return (a * b + s2) & 0xFFFFFFFF def reference_valu16_mad(): v0, v1, v3, v5, v6, v7 = 0x1234FFFF, 0x5678FFFE, 0x00030000, 0x00018000, 0xFFFF7FFF, 0x40000000 return [_mad_32_16(False, v0, v1, v3), _mad_32_16(True, v0, v1, v3), _mad_32_16(True, v5, v6, v7), _mad_32_16(True, v5, v6, 5, sel=(1, 1))] def reference_valu16_subrev(): a, b, d = _unhalf(0x3E00), _unhalf(0x3800), _unhalf(0x3C00) # low halves: v0 1.5, v1 0.5, v5 1.0 return [0xAAAA0000 | _half(b - a), 0xAAAA0000 | _half(-b - a), 0xAAAA0000 | _half(min(1.0, b + a)), 0x42000000 | _half(a - d)] def _isg1_dxbc(elements): """A DXBC container with one ISG1 input signature. elements: (register, mask, system value, component type).""" payload = bytearray(struct.pack(" 0 (a start-only check, 0xf < 18, would read) self.check("buffer_load_dword v2, v1, s[4:7], 0 offen", 1, [None], nr=18, offset=0xF) # offset 0xe, NR 18: bytes 0xe..0x11 fit -> data (base_low 2: base + O aligned, see the class docstring) self.check("buffer_load_dword v2, v1, s[4:7], 0 offen", 1, [0xE], nr=18, offset=0xE, base_low=2) def test_multi_dword_load_is_bounded_per_dword(self): # vx4a-k0[4..7], lane 3: x4 at offset 3, NR 18 -> d0..d2 data, d3 (bytes 15..18) 0 # (base_low here and below makes base + O aligned for the interpreter, see the class docstring) self.check("buffer_load_dwordx4 v[2:5], v1, s[4:7], 0 offen", 4, [3, 7, 0xB, None], nr=18, offset=3, base_low=1) # lane 2: d3 = bytes 14..17 fits -> all four data self.check("buffer_load_dwordx4 v[2:5], v1, s[4:7], 0 offen", 4, [2, 6, 0xA, 0xE], nr=18, offset=2, base_low=2) # vmix2-k0[4..7]: x4 off offset:13, NR 20 -> data, 0, 0, 0 self.check("buffer_load_dwordx4 v[2:5], off, s[4:7], 0 offset:13", 4, [13, None, None, None], nr=20, base_low=3) # x2 at 8, NR 0xe: d0 data, d1 0 self.check("buffer_load_dwordx2 v[2:3], v1, s[4:7], 0 offen", 2, [8, None], nr=0xE, offset=8) def test_offsets_are_summed_before_the_check(self): # vx1-k*[7..13] style: soffset 3 + inst_offset 2 + vaddr 0xb = 0x10; NR 0x16 -> x2: data, 0 self.check("buffer_load_dwordx2 v[2:3], v1, s[4:7], s8 offen offset:2", 2, [0x10, None], nr=0x16, offset=0xB, extra=("s_mov_b32 s8, 3",)) def test_base_low_bits_kept_in_the_address_not_in_the_bound(self): # vx4a-k3[4], [7] measured base & 3 = 3 with x4 at offset 3, NR 18 -> d0..d2 data at base + 3.., d3 0. # Stand-in at offset 5 so that base + O is aligned for the interpreter (class docstring): d0..d2 data, # d3 (bytes 0x11..0x14) 0; a bound that added base & 3 would also zero d2 (3 + 5 + 8 + 4 = 0x14 > 18). self.check("buffer_load_dwordx4 v[2:5], v1, s[4:7], 0 offen", 4, [5, 9, 0xD, None], nr=18, offset=5, base_low=3) def test_num_records_zero_reads_nothing(self): self.check("buffer_load_dwordx4 v[2:5], v1, s[4:7], 0 offen", 4, [None] * 4, nr=0, offset=0) # Modes 1 and 2 (OOB_SELECT) check the index or NUM_RECORDS once for the whole load; modes 0 and 3 are per dword # (docs/buffer_bounds.md; tests/semantic/test_buffer_oob_select.py has the full table). def test_structured_buffer_keeps_its_first_byte_check(self): # OOB_SELECT 1, stride 4, NR 2 records (8 bytes): no idxen, so index 0 < 2 and every dword is loaded self.check("buffer_load_dwordx4 v[2:5], v1, s[4:7], 0 offen", 4, [4, 8, 0xC, 0x10], nr=2, offset=4, word1=4 << 16, word3=0x11016FAC) def test_idxen_keeps_its_first_byte_check(self): # OOB_SELECT 1, idxen with index 0 and offset:0xc, NR 0xe: index 0 < 0xe, so both dwords are loaded self.check("buffer_load_dwordx2 v[2:3], v1, s[4:7], 0 idxen offset:12", 2, [0xC, 0x10], nr=0xE, offset=0, word3=0x11016FAC) def test_store_is_per_dword(self): # x2 at 0xc with NR 0x10: the first dword ends at 0x10 and lands, the second (ends at 0x14) is dropped lines = ["v_cmpx_eq_u32_e32 0, v0", f"s_mov_b32 s4, {VEC_REGION:#x}", "s_mov_b32 s5, 0", "s_mov_b32 s6, 0x10", "s_mov_b32 s7, 0x31016fac", "v_mov_b32 v1, 0xc", "v_mov_b32 v2, 0x11111111", "v_mov_b32 v3, 0x22222222", "buffer_store_dwordx2 v[2:3], v1, s[4:7], 0 offen", "s_endpgm"] spv = support.translate_lines("cs_vec_store", lines, fssm=CS_BOUNDS_FSSM) memory = {} support.run_fragment(spv, inputs={"%wg_id": (0, 0, 0), "%local_id": (0, 0, 0)}, push_constants=[0, 0, 0x100, 0x31016FAC] + [0] * 28, memory=memory) self.assertEqual(memory, {VEC_REGION + 0xC: 0x11111111}) class MeasuredBoundsShaders(unittest.TestCase): """The four GPU tests of the measured SMEM / raw MUBUF rules (runtime/test/shaders/cs_smem_bounds.asm, cs_smem_stride_align.asm, cs_smem_sload_neg.asm, cs_buf_tail.asm), run here in the interpreter on a 256-byte heap filled with 0xee like vk_compute_test's. The words are the ones derived in each .asm header.""" WANT = { "cs_smem_bounds": [0, 0x5EED0010, 0x5EED000C, 0, 0x5EED000C, 0x5EED0010, 0, 0], "cs_smem_stride_align": [0x5EED0010, 0, 0, 0x5EED0000, 0x5EED0000, 0x5EED0000, 0x5EED0000, 0x5EED0004], "cs_smem_sload_neg": [0x5EED0004, 0x5EED0000, 0x5EED0004, 0x5EED0004, 0x5EED0008, 0x5EED000C, 0xEEEEEEEE, 0xEEEEEEEE], "cs_buf_tail": [0, 0x5EED000C, 0x5EED0008, 0x5EED000C, 0, 0, 0x5EED0010, 0], "cs_oob_select": [0x5EED000C, 0, 0x5EED0014, 0, 0x5EED0018, 0xEEEEEEEE, 0, 0], "cs_oob_tail": [0, 0, 0x5EED0010, 0, 0xBEEF0000, 0xEEEEEEEE, 0, 0x5EED0010], } def test_words(self): for name, want in self.WANT.items(): with self.subTest(shader=name): spv = support.translate_fixture(name, fssm=CS_BOUNDS_FSSM) memory = {a: 0xEEEEEEEE for a in range(0, 0x100, 4)} support.run_fragment(spv, inputs={"%wg_id": (0, 0, 0), "%local_id": (0, 0, 0)}, push_constants=[0, 0, 0xFFFFFFFF, 0x31016FAC] + [0] * 28, memory=memory) self.assertEqual([f"{memory[4 * i]:#010x}" for i in range(8)], [f"{w:#010x}" for w in want]) self.assertEqual(set(memory), set(range(0, 0x100, 4)), "nothing written outside the heap") class LaneMaskConstants(unittest.TestCase): """runtime/test/shaders/cs_w32_mask_imm.asm and cs_w64_mask_imm.asm: a constant read as a lane mask (saveexec, wrexec, s_and_b64 / s_not_b64 / s_mov_b64 into EXEC or VCC) gives lane L its bit L, not bit 0 for every lane. Run on all lanes of one wave (32 on a 32-wide subgroup, 64 on a 64-wide one, no --wave64-emulate) with a heap of 0xee bytes; the words are the GPU check's (check.py CS_DUMPS), derived by hand in each .asm header.""" WANT = {"cs_w32_mask_imm": (32, [2056, 20, 2, 6152, 2050, 4112, 258, 32]), "cs_w64_mask_imm": (64, [2056, 1084, 828, 2052, 319, 2104, 516, 2064])} # Bit 0 of the constant for every lane, the translation before the fix (in the .asm headers). OLD = {"cs_w32_mask_imm": [0xFFFFFF00, 32, 32, 0xFFFFFF00, 2072, 0xFFFFFF00, 0xFFFFFF00, 32], "cs_w64_mask_imm": [0xFFFFFF00] * 7 + [2080]} def run_words(self, name, lanes): sys.path.insert(0, os.path.join(support.ROOT, "runtime", "test", "shaders")) from compute_fixture import compute_header header = compute_header(os.path.join(support.ROOT, "runtime", "test", "shaders", name + ".asm")) spv = support.translate_fixture(name, fssm=bytes(header)) memory = {a: 0xEEEEEEEE for a in range(0, 0x100, 4)} si.run_workgroup(spv, lanes, lanes, inputs_for=lambda i: {"%wg_id": (0, 0, 0), "%local_id": (i, 0, 0), "%sg_lane": i}, push_constants=[0, 0, 0xFFFFFFFF, 0x31016FAC] + [0] * 28, memory=memory) self.assertEqual(set(memory), set(range(0, 0x100, 4)), "nothing written outside the heap") return [memory[4 * i] for i in range(8)] def test_words(self): for name, (lanes, want) in self.WANT.items(): with self.subTest(shader=name): got = self.run_words(name, lanes) self.assertEqual([f"{w:#010x}" for w in got], [f"{w:#010x}" for w in want]) self.assertNotEqual(got, self.OLD[name]) def test_words_follow_from_the_masks(self): # Plain-Python reference: word = set lanes | lowest lane << 8 of each case's resulting mask. def word(m): return 0xFFFFFF00 if m == 0 else bin(m).count("1") | ((m & -m).bit_length() - 1) << 8 m32, m64 = 0xFFFFFFFF, (1 << 64) - 1 w32 = [word(0xFF00 & m32), word(0x55555555 | 0xFF), word(5 & m32), word(0xFF00FF00 & ~0xFFFF & m32), word(0x3FF & ~0xFF), word(-16 & m32 & ~0xFFFF), word(0x80000002 | 0), word(-1 & m32)] w64 = [word(0xFF00 & m64), word(-16 & m64), word(-8 & m64 & ~0x10), word(0xFF00 & ~0xF0F0 & m64), word(-2 & m64), word(~0xFF & m64), word(0x3C), bin(0xFF00FF).count("1") | 8 << 8] self.assertEqual(w32, self.WANT["cs_w32_mask_imm"][1]) self.assertEqual(w64, self.WANT["cs_w64_mask_imm"][1]) if __name__ == "__main__": unittest.main()