From 6772a597661cb71972f2b1bdcb0482c9fde50cdf Mon Sep 17 00:00:00 2001 From: legacy-code-archaeologist <359844974+legacy-code-archaeologist@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:09:15 +0100 Subject: [PATCH] shader: lower v_cvt_f16_u16, v_cvt_f16_i16, v_cvt_u16_f16 and v_cvt_i16_f16 in direct compute Lower VOP1 and VOP3A v_cvt_f16_u16, v_cvt_f16_i16, v_cvt_u16_f16 and v_cvt_i16_f16 in DirectCompute::float_alu() preserving D[31:16] on gfx10. Int16/u16 -> f16 converts via sitofp/uitofp and packed_half_rne() round-to-nearest-even (with optional VOP3 clamp to [0.0, 1.0]); f16 -> i16/u16 widens via legacyF16ToF32, truncates toward zero, saturates to [-32768, 32767] / [0, 65535], maps NaN to 0, and supports VOP3 abs/neg. Also add all four conversions to half_binary in DirectCompute::lower() so the default binary16 rounding/denormal MODE contract is enforced. Fixes #3148 Refs #2935 AI-assisted. --- docs/experiments/native-direct-cvt-f16-i16.md | 39 ++ src/gfx1013_direct_compute.inl | 70 ++- tests/cmake/native_half_int_cvt.cmake | 19 + tests/native_cvt_f16_i16_check.py | 398 ++++++++++++++++++ 4 files changed, 525 insertions(+), 1 deletion(-) create mode 100644 docs/experiments/native-direct-cvt-f16-i16.md create mode 100644 tests/cmake/native_half_int_cvt.cmake create mode 100644 tests/native_cvt_f16_i16_check.py diff --git a/docs/experiments/native-direct-cvt-f16-i16.md b/docs/experiments/native-direct-cvt-f16-i16.md new file mode 100644 index 0000000000..838acae2c0 --- /dev/null +++ b/docs/experiments/native-direct-cvt-f16-i16.md @@ -0,0 +1,39 @@ +# Binary16 <-> 16-bit integer conversions in the direct C++ compute path (#3148) + +`src/gfx1013_direct_compute.inl` now lowers `v_cvt_f16_u16`, `v_cvt_f16_i16`, `v_cvt_u16_f16`, and `v_cvt_i16_f16` +(VOP1 and VOP3A) in the direct C++ compute path (`DirectCompute::float_alu()`): + +- **High-half preservation:** Every operation reads `S0[15:0]`, writes the converted 16-bit result into `D[15:0]`, and + preserves `D[31:16]`, matching LLVM `GCNSubtarget::zeroesHigh16BitsOfDest` (`"On gfx10, all 16-bit instructions preserve + the high bits"`). +- **`v_cvt_f16_u16` / `v_cvt_f16_i16`:** Sign-extends (for `i16`) or zero-extends (for `u16`) `S0[15:0]`, converts to + binary32 (`sitofp`/`uitofp`), rounds to nearest-even binary16 with `packed_half_rne()` (e.g. `2049 -> 2048`, `2051 -> + 2052`, `65519 -> 65504`, `65520 -> +inf`, `32767 -> 32768.0h`), and optionally applies VOP3 `clamp` (`[0.0, 1.0]`). +- **`v_cvt_u16_f16` / `v_cvt_i16_f16`:** Applies optional VOP3 `abs`/`neg` on `S0[15:0]`, widens with + `@dx.op.legacyF16ToF32`, truncates toward zero, saturates to `[0, 65535]` / `[-32768, 32767]`, and maps NaN to `0`. +- **Mode & modifier contract:** Included in `half_binary` in `DirectCompute::lower()` so the default FP16 rounding + (`round16 = 0` RNE) and denormal (`denorm16 = 3` keep, `FP16_OVFL = 0`) mode contract is enforced. Refuses DPP, SDWA, + output modifiers (`omod`), `clamp` on `*_f16` integer targets, `op_sel` high-half bits, `abs`/`neg` on integer sources, + unused VOP3 `src1`/`src2`, and non-default FP16 modes. Inline float constants use the same binary16 bit table as + `v_add_f16`. + +## Fixture + +[`tests/native_cvt_f16_i16_check.py`](../../tests/native_cvt_f16_i16_check.py) via +[`tests/cmake/native_half_int_cvt.cmake`](../../tests/cmake/native_half_int_cvt.cmake) (`native_cvt_f16_i16_ir`, +`native_cvt_f16_i16_controls`, `native_cvt_f16_i16_hardware`): + +- **IR (`native_cvt_f16_i16_ir`):** Evaluates lane 0 across 82 conversion cases (VGPR, SGPR, literal, inline integer, + inline float, VOP3 `clamp`, `abs`, `neg`, `-|x|`, rounding ties, saturation, subnormals, infinities, NaNs, and + non-zero upper halves) plus 38 refusal cases (DPP, SDWA, `omod`, `op_sel`, illegal `abs`/`neg`/`clamp`, `src1`, and + MODE writes). +- **Controls (`native_cvt_f16_i16_controls`):** Compiles the full 82-case shader to DXIL via `compile_dxil` and checks + `dxc.exe -dumpbin` plus all 38 refusals. +- **Hardware (`native_cvt_f16_i16_hardware`):** Replays all 82 conversions on hardware via `native_store_replay --direct`. + +Measured on 27.11.2026, `main` `429f11d5` plus this change, Windows 10.0.26100 (`dev` preset): `native_cvt_f16_i16_ir` +(`1.09s`: 82 conversions + 38 refusals), `native_cvt_f16_i16_controls` (`3.84s`), all 47 `native_*_(controls|ir)` tests +(`54.43s`), and `tests/docs_links_check.py` (`101` Markdown files, `589` links) passed; `native_cvt_f16_i16_hardware` +skipped (`77`, Quadro M1000M does not support shader model 6.6 with wave32, so GPU readback is not measured). Five +broken-lowering sensitivity checks (`zero_high_half`, `drop_ordered_nan`, `swap_signed`, `drop_clamp`, +`wrong_inline_float`) each failed `native_cvt_f16_i16_ir`. diff --git a/src/gfx1013_direct_compute.inl b/src/gfx1013_direct_compute.inl index fb6e462aea..a44922482b 100644 --- a/src/gfx1013_direct_compute.inl +++ b/src/gfx1013_direct_compute.inl @@ -1148,6 +1148,73 @@ struct DirectCompute { vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535"))); return true; } + if (n == "v_cvt_f16_u16" || n == "v_cvt_f16_i16" || n == "v_cvt_u16_f16" || n == "v_cvt_i16_f16") { + if (graphics_input) + return false; + const bool to_f16 = n == "v_cvt_f16_u16" || n == "v_cvt_f16_i16"; + const bool is_signed = n == "v_cvt_f16_i16" || n == "v_cvt_i16_f16"; + if (i.dpp_control || permuted) + refuse(i.pc, "unsupported " + string(n) + " DPP form"); + if (i.subword) + refuse(i.pc, "unsupported " + string(n) + " SDWA form"); + if (i.scale || (!to_f16 && i.clamp)) + refuse(i.pc, "unsupported " + string(n) + " clamp or omod"); + if (i.wide && (i.a & 0x7800)) + refuse(i.pc, "unsupported " + string(n) + " high-half op_sel"); + if ((to_f16 && (i.abs || i.neg)) || (i.wide && (((i.abs | i.neg) & 6) || i.src[1] || i.src[2]))) + refuse(i.pc, "unsupported " + string(n) + " operand modifiers"); + if (!half_mode_known || ((mode_bits ^ 0xf0u) & 0x8000ccu)) + refuse(i.pc, "unsupported binary16 rounding or denormal mode"); + const unsigned r = i.src[0]; + string bits; + if (r >= 240 && r <= 248) { + constexpr unsigned halves[]{0x3800, 0xb800, 0x3c00, 0xbc00, 0x4000, + 0xc000, 0x4400, 0xc400, 0x3118}; + bits = to_string(halves[r - 240]); + } else if (r == 255) { + bits = to_string(i.literal & 65535u); + } else if (r >= 128 && r <= 192) { + bits = to_string((r - 128) & 65535u); + } else if (r >= 193 && r <= 208) { + bits = to_string((192u - r) & 65535u); + } else { + bits = ir.binary("and", operand32(i, r), "65535"); + } + string result; + if (to_f16) { + string f; + if (is_signed) { + auto s16 = ir.binary("ashr", ir.binary("shl", bits, "16"), "16"); + f = ir.value("sitofp i32 " + s16 + " to float"); + } else { + f = ir.value("uitofp i32 " + bits + " to float"); + } + auto fbits = ir.value("bitcast float " + f + " to i32"); + result = packed_half_rne(fbits); + if (i.clamp) { + auto nan = cmp("ugt", ir.binary("and", result, "32767"), "31744"); + auto low = ir.value("or i1 " + nan + ", " + cmp("ne", ir.binary("and", result, "32768"), "0")); + result = select(low, "0", select(cmp("ugt", result, "15359"), "15360", result)); + } + } else { + if (i.abs & 1) + bits = ir.binary("and", bits, "32767"); + if (i.neg & 1) + bits = ir.binary("xor", bits, "32768"); + auto f = ir.invoke("float", "legacyF16ToF32", 131, {{"i32", bits}}, "nounwind readnone"); + auto lower = ir.value("fcmp oge float " + f + ", " + (is_signed ? "-32768.0" : "0.0")); + auto upper = ir.value("fcmp olt float " + f + ", " + (is_signed ? "32768.0" : "65536.0")); + auto safe = select_float(ir.value("and i1 " + lower + ", " + upper), f, "0.0"); + auto converted = ir.value(string(is_signed ? "fptosi" : "fptoui") + " float " + safe + " to i32"); + auto bounded = select(upper, converted, is_signed ? "32767" : "65535"); + bounded = select(lower, bounded, is_signed ? "32768" : "0"); + auto ordered = ir.value("fcmp ord float " + f + ", " + f); + result = ir.binary("and", select(ordered, bounded, "0"), "65535"); + } + auto kept = ir.binary("and", read(i.dst), "4294901760"); + vector_write(i.dst, ir.binary("or", kept, ir.binary("and", result, "65535"))); + return true; + } if (n == "v_ldexp_f32") { // pixel_aot.py ldexp32 (#3013): exact binary32 scaling of S0 by 2^S1. Zero, Inf and NaN pass unchanged, // a denormal S0 is normalized rather than flushed, and a denormal result rounds to nearest even. S1 is @@ -2998,7 +3065,8 @@ struct DirectCompute { if (!p.reached[k]) continue; const auto &op = p.code[k]; - if (binary16_alu(op.name)) + if (binary16_alu(op.name) || op.name == "v_cvt_f16_u16" || op.name == "v_cvt_f16_i16" || + op.name == "v_cvt_u16_f16" || op.name == "v_cvt_i16_f16") half_binary = true; if (op.name == "s_round_mode" || op.name == "s_denorm_mode" || op.name == "s_setreg_b32" || op.name == "s_setreg_imm32_b32") diff --git a/tests/cmake/native_half_int_cvt.cmake b/tests/cmake/native_half_int_cvt.cmake new file mode 100644 index 0000000000..482b921b9d --- /dev/null +++ b/tests/cmake/native_half_int_cvt.cmake @@ -0,0 +1,19 @@ +# v_cvt_f16_u16, v_cvt_f16_i16, v_cvt_u16_f16 and v_cvt_i16_f16 in the direct C++ compute path. +# Named to sort after native_f16_alu.cmake so native_compute_ir_dump is already defined. +if(TARGET native_compute_ir_dump AND EXISTS "${INTAKE_PYTHON}") + add_test(NAME native_cvt_f16_i16_ir COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" ir + "$") + set_tests_properties(native_cvt_f16_i16_ir PROPERTIES TIMEOUT 60) +endif() +if(TARGET compile_dxil AND TARGET native_store_replay AND + EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll") + add_test(NAME native_cvt_f16_i16_controls COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" controls + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_cvt_f16_i16_controls PROPERTIES TIMEOUT 60) + add_test(NAME native_cvt_f16_i16_hardware COMMAND "${INTAKE_PYTHON}" -B + "${CMAKE_CURRENT_SOURCE_DIR}/tests/native_cvt_f16_i16_check.py" hardware + "$" "${DXC_BIN_DIR}") + set_tests_properties(native_cvt_f16_i16_hardware PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 60) +endif() diff --git a/tests/native_cvt_f16_i16_check.py b/tests/native_cvt_f16_i16_check.py new file mode 100644 index 0000000000..e92cc74afb --- /dev/null +++ b/tests/native_cvt_f16_i16_check.py @@ -0,0 +1,398 @@ +"""Direct compute binary16 <-> 16-bit integer conversions (v_cvt_f16_u16, v_cvt_f16_i16, v_cvt_u16_f16, v_cvt_i16_f16).""" +import math +from pathlib import Path +import re +import struct +import subprocess +import sys +import tempfile + +from native_dpp_quad_check import request +from native_direct_container_check import words as pack_words +import native_f16_alu_check as F +from native_scalar_lds_check import scalar, sop1 + + +OPS = { + 'v_cvt_f16_u16': (0x50, 0x1D0, False, True), + 'v_cvt_f16_i16': (0x51, 0x1D1, True, True), + 'v_cvt_u16_f16': (0x52, 0x1D2, False, False), + 'v_cvt_i16_f16': (0x53, 0x1D3, True, False), +} +HI_DST = 0xABCD0000 +HI_SRC = 0xBEEF0000 +DST_SEED = HI_DST | 0x5555 + + +def vop1(op, vdst, src0): + return 0x7E000000 | ((vdst & 0xFF) << 17) | ((op & 0xFF) << 9) | (src0 & 0x1FF) + + +def vop3_unary(op, vdst, src0, *, abs0=0, neg0=0, clamp=0, omod=0, op_sel=0, src1=0): + w0 = (0xD4000000 | ((op & 0x3FF) << 16) | ((clamp & 1) << 15) | + ((op_sel & 0xF) << 11) | ((abs0 & 7) << 8) | (vdst & 0xFF)) + w1 = ((neg0 & 7) << 29) | ((omod & 3) << 27) | ((src1 & 0x1FF) << 9) | (src0 & 0x1FF) + return [w0, w1] + + +def v_mov_lit(vdst, val): + return [0x7E0002FF | ((vdst & 0xFF) << 17), val & 0xFFFFFFFF] + + +def half_bits_to_f32(bits): + return struct.unpack('> 16) & 0x8000 + mag = fbits & 0x7FFFFFFF + exp = mag >> 23 + frac = mag & 0x7FFFFF + if mag == 0: + out = sign + elif exp > 142: + out = sign | 0x7C00 + elif exp <= 112: + out = sign | ((0x800000 | frac) >> (126 - exp)) if exp >= 102 else sign + else: + out = sign | (((exp - 112) << 10) | (frac >> 13)) + if clamp: + if (out & 0x7FFF) > 0x7C00 or (out & 0x8000): + out = 0 + elif out > 0x3BFF: + out = 0x3C00 + return out + + +def ref_cvt_int_f16(signed, raw, *, abs0=0, neg0=0, nan_check=True): + """Truncate toward zero and saturate binary16 to 16-bit int; NaN gives 0.""" + bits = raw & 0xFFFF + if abs0: + bits &= 0x7FFF + if neg0: + bits ^= 0x8000 + f = half_bits_to_f32(bits) + if math.isnan(f): + return 0 if nan_check else (0x8000 if signed else 0) + lo, hi = (-32768, 32767) if signed else (0, 65535) + if f <= lo: + return lo & 0xFFFF + if f >= hi: + return hi & 0xFFFF + return int(math.trunc(f)) & 0xFFFF + + +def cases(): + items = [] + + def add(op, kind, src_val, *, src_code=257, abs0=0, neg0=0, clamp=0, wide=False, want=None): + vop1_op, vop3_op, signed, to_f16 = OPS[op] + if want is None: + raw = F.INLINE_F16[src_code] if kind == 'inline_float' else src_val + want = (ref_cvt_f16_int(signed, raw, clamp=bool(clamp)) if to_f16 + else ref_cvt_int_f16(signed, raw, abs0=abs0, neg0=neg0)) + items.append((op, vop1_op, vop3_op, signed, to_f16, kind, src_val, src_code, abs0, neg0, clamp, wide, want)) + + # v_cvt_f16_u16 (RNE rounding + overflow at 65520 + clamp + source kinds) + for v in (0, 1, 2048, 2049, 2051, 32768, 65504, 65519, 65520, 65535): + add('v_cvt_f16_u16', 'vgpr', HI_SRC | v) + add('v_cvt_f16_u16', 'sgpr', HI_SRC | 2051) + add('v_cvt_f16_u16', 'lit', 0xBEEF0803) # 2051 -> 2052 (0x6804) + add('v_cvt_f16_u16', 'inline_int', 0, src_code=128) + add('v_cvt_f16_u16', 'inline_int', 64, src_code=192) + add('v_cvt_f16_u16', 'inline_int', 0xFFFF, src_code=193) # -1 low 16 bits = 65535 -> +inf + add('v_cvt_f16_u16', 'inline_float', 0x3C00, src_code=242) # 1.0 bits (15360) -> 15360.0 (0x7380) + add('v_cvt_f16_u16', 'vop3', HI_SRC | 2051, wide=True) + for v in (0, 1, 65535): + add('v_cvt_f16_u16', 'vop3', v, clamp=1, wide=True) + + # v_cvt_f16_i16 (signed boundaries, RNE ties, clamp, source kinds) + for v in (0, 1, 0xFFFF, 0xFFFD, 2049, (-2049) & 0xFFFF, (-2051) & 0xFFFF, 0x7FFF, 0x8000): + add('v_cvt_f16_i16', 'vgpr', HI_SRC | v) + add('v_cvt_f16_i16', 'sgpr', HI_SRC | 0xFFFD) # -3.0 + add('v_cvt_f16_i16', 'lit', 0xCAFE8000) + add('v_cvt_f16_i16', 'inline_int', 0xFFFF, src_code=193) # -1.0 (0xBC00) + add('v_cvt_f16_i16', 'inline_int', (-16) & 0xFFFF, src_code=208) # -16.0 + add('v_cvt_f16_i16', 'inline_float', 0x3C00, src_code=242) + for v in (0xFFFD, 0, 1, 0x7FFF): + add('v_cvt_f16_i16', 'vop3', v, clamp=1, wide=True) + + # v_cvt_u16_f16 (truncation, clamping at 0..65535, NaN->0, abs/neg, source kinds) + for h in (0x0000, 0x8000, 0x0001, 0x8001, 0x3800, 0xB800, 0x4180, 0xC180, 0x7BFF, 0x7C00, 0xFC00, 0x7E00, 0xFE01): + add('v_cvt_u16_f16', 'vgpr', HI_SRC | h) + add('v_cvt_u16_f16', 'sgpr', HI_SRC | 0x4180) + add('v_cvt_u16_f16', 'lit', 0x12344580) # 5.5 -> 5 + add('v_cvt_u16_f16', 'inline_float', 0x4400, src_code=246) # 4.0 -> 4 + add('v_cvt_u16_f16', 'inline_float', 0xBC00, src_code=243) # -1.0 -> 0 + add('v_cvt_u16_f16', 'vop3', HI_SRC | 0xC180, abs0=1, wide=True) # |-2.75| -> 2 + add('v_cvt_u16_f16', 'vop3', HI_SRC | 0xC180, neg0=1, wide=True) # -(-2.75) -> 2 + add('v_cvt_u16_f16', 'vop3', HI_SRC | 0x4180, abs0=1, neg0=1, wide=True) # -|2.75| -> 0 + + # v_cvt_i16_f16 (truncation, clamping at -32768..32767, NaN->0, abs/neg, source kinds) + for h in (0x0000, 0x8000, 0x0001, 0x8001, 0x3800, 0xB800, 0x4180, 0xC180, + 0x77FF, 0x7800, 0x7BFF, 0xF800, 0xF801, 0xFBFF, 0x7C00, 0xFC00, 0x7E00, 0xFE01): + add('v_cvt_i16_f16', 'vgpr', HI_SRC | h) + add('v_cvt_i16_f16', 'sgpr', HI_SRC | 0xC180) + add('v_cvt_i16_f16', 'lit', 0xABCD7800) # 32768.0 -> 32767 + add('v_cvt_i16_f16', 'inline_float', 0xC400, src_code=247) # -4.0 -> -4 (0xFFFC) + add('v_cvt_i16_f16', 'vop3', HI_SRC | 0xF800, abs0=1, wide=True) # |-32768.0| -> 32767 + add('v_cvt_i16_f16', 'vop3', HI_SRC | 0xF800, neg0=1, wide=True) # -(-32768.0) -> 32767 + add('v_cvt_i16_f16', 'vop3', HI_SRC | 0x7800, abs0=1, neg0=1, wide=True) # -|32768.0| -> -32768 + + assert len(items) < F.STRIDE + return items + + +def emit_program(items): + code = [sop1(3, 20, 4), sop1(3, 21, 128), 0xF408080A, 0xFA000020, + scalar(0x1E, 7, 5, 128 + F.SHIFT), F.vmove(8, 7), sop1(3, 126, 129), + 0xBFA40000, 0xBFA5000F, 0xBA800881, 0] + want = [] + for index, (op, vop1_op, vop3_op, _, _, kind, src_val, src_code, abs0, neg0, clamp, wide, expected) in enumerate(items): + code += v_mov_lit(3, DST_SEED) + if kind == 'vgpr': + code += v_mov_lit(1, src_val) + [vop1(vop1_op, 3, 257)] + elif kind == 'sgpr': + code += [sop1(3, 10, 255), src_val, vop1(vop1_op, 3, 10)] + elif kind == 'lit': + code += [vop1(vop1_op, 3, 255), src_val] + elif kind in ('inline_int', 'inline_float'): + code += [vop1(vop1_op, 3, src_code)] + elif kind == 'vop3': + assert wide + code += v_mov_lit(1, src_val) + vop3_unary(vop3_op, 3, 257, abs0=abs0, neg0=neg0, clamp=clamp) + else: + raise AssertionError((op, kind)) + code += F.store(4 * index, 3) + want.append(HI_DST | expected) + code.append(0xBF810000) + return code, want + + +def check_mutants(items): + def match(got, expected): + return got == (HI_DST | expected) + + for op, _, _, signed, to_f16, _, _, _, _, _, clamp, _, expected in items: + assert match(HI_DST | expected, expected) + + # Zeroed upper half must fail. + assert any(not match(expected, expected) for _, _, _, _, _, _, _, _, _, _, _, _, expected in items) + + # Round-toward-zero instead of RNE on f16<-int must fail (2051 / 65519 / 32767). + assert any( + ref_cvt_f16_int(signed, F.INLINE_F16[sc] if kind == 'inline_float' else sv, clamp=bool(cl), rne=False) != exp + for _, _, _, signed, to_f16, kind, sv, sc, _, _, cl, _, exp in items if to_f16 + ) + + # Skipping NaN->0 on int<-f16 must fail. + assert any( + ref_cvt_int_f16(signed, F.INLINE_F16[sc] if kind == 'inline_float' else sv, abs0=a0, neg0=n0, nan_check=False) != exp + for _, _, _, signed, to_f16, kind, sv, sc, a0, n0, _, _, exp in items if not to_f16 + ) + + +def refusal_programs(): + programs = [] + for op, (vop1_op, vop3_op, _, to_f16) in OPS.items(): + programs.append(([vop1(vop1_op, 3, 250), 0xFF00B101, 0xBF810000], 'unsupported DPP arithmetic')) + programs.append(([vop1(vop1_op, 3, 249), 0x06060601, 0xBF810000], 'unimplemented direct SDWA operand profile')) + programs.append((vop3_unary(vop3_op, 3, 257, omod=1) + [0xBF810000], f'unsupported {op} clamp or omod')) + programs.append((vop3_unary(vop3_op, 3, 257, op_sel=1) + [0xBF810000], f'unsupported {op} high-half op_sel')) + programs.append((vop3_unary(vop3_op, 3, 257, op_sel=8) + [0xBF810000], f'unsupported {op} high-half op_sel')) + programs.append((vop3_unary(vop3_op, 3, 257, src1=1) + [0xBF810000], f'unsupported {op} operand modifiers')) + if to_f16: + programs.append((vop3_unary(vop3_op, 3, 257, abs0=1) + [0xBF810000], f'unsupported {op} operand modifiers')) + programs.append((vop3_unary(vop3_op, 3, 257, neg0=1) + [0xBF810000], f'unsupported {op} operand modifiers')) + else: + programs.append((vop3_unary(vop3_op, 3, 257, clamp=1) + [0xBF810000], f'unsupported {op} clamp or omod')) + + for op in ('v_cvt_f16_i16', 'v_cvt_i16_f16'): + insn = vop1(OPS[op][0], 3, 257) + programs.append(([0xBFA4000C, insn, 0xBF810000], 'unsupported binary16 rounding or denormal mode')) + programs.append(([0xBFA50000, insn, 0xBF810000], 'unsupported binary16 rounding or denormal mode')) + programs.append(([0xBA8005C1, 1, insn, 0xBF810000], 'unsupported binary16 rounding or denormal mode')) + programs.append(([0xB98005C1, insn, 0xBF810000], 'unsupported binary16 rounding or denormal mode')) + return programs + + +def _float_val(token, values): + if token.startswith('%'): + kind, bits = values[token] + assert kind == 'float', (token, kind) + return F._f32(bits) + return float(token) + + +def _fcmp(pred, a, b): + if pred == 'ord': + return not (math.isnan(a) or math.isnan(b)) + if pred == 'oge': + return a >= b + if pred == 'olt': + return a < b + if pred == 'ogt': + return a > b + raise AssertionError((pred, a, b)) + + +def _fptoi(signed, f): + val = int(math.trunc(f)) + return (val & 0xFFFFFFFF) + + +def interpret_cvt(ir_text): + """Evaluate lane 0 of the lowered DXIL IR, handling int<->float conversions and fcmp.""" + body = ir_text.split('define void @main()', 1)[1].split('\n}', 1)[0] + blocks, label, buf = {}, None, [] + for raw in body.splitlines(): + line = raw.strip() + if not line or line == '{': + continue + if line.endswith(':') and ' ' not in line: + if label is not None: + blocks[label] = buf + label, buf = line[:-1], [] + continue + if label is None: + raise AssertionError(line) + buf.append(line) + if label is not None: + blocks[label] = buf + values, memory, stores = {}, {}, [] + label, steps = 'entry', 0 + while steps < 200000: + steps += 1 + followed = False + for line in blocks[label]: + if line.startswith('br '): + label = F._branch(line, values) + followed = True + break + if line == 'ret void': + return sorted(stores) + m = re.fullmatch(r'(%\w+) = (.+)', line) + if m: + name, rhs = m.group(1), m.group(2) + if rhs.startswith(('sitofp i32 ', 'uitofp i32 ')): + parts = re.fullmatch(r'(sitofp|uitofp) i32 (\S+) to float', rhs) + raw = F._i32(parts.group(2), values) + val = float(F._signed(raw) if parts.group(1) == 'sitofp' else raw) + values[name] = ('float', struct.unpack('