"""--fma exact (SHADER-497): fused multiply-adds emulated from single-rounded f32 operations (Gen.fma_exact). RDNA2's v_fma_f32, v_fmac_f32, v_fmaak_f32, v_fmamk_f32 and v_fma_mix* round once. GLSL Fma may be split into two roundings by a driver (Intel UHD Graphics, Xe-LP, Windows driver 101.7088: cs_fma_fused words 0, 2, 3 and 5), and a device without VK_KHR_shader_fma can't take --fma khr. --fma exact builds the single rounding from NoContraction FMul/FAdd/FSub: Dekker's exact product, TwoSum, a round-to-odd sum of the two error terms and one final rounding (Boldo and Melquiond, IEEE Transactions on Computers 57(4), 2008), with power-of-two scaling that keeps every intermediate in the normal range. The interpreter rounds every FMul/FAdd/FSub to binary32, so these tests run the emitted instructions as a driver would, and compare each word with an exact rational reference (si.fma_f32). """ import math import os import random import shutil import struct import subprocess import sys import tempfile import unittest HERE = os.path.dirname(os.path.abspath(__file__)) ROOT = os.path.dirname(os.path.dirname(HERE)) TOOLS_DIR = os.environ.get("LEONIDA_TOOLS_DIR") or os.path.join(ROOT, "tools") sys.path.insert(0, TOOLS_DIR) sys.path.insert(0, HERE) import rdna2spirv # noqa: E402 import spirv_mini_interp as si # noqa: E402 from support import mrt0_words, translate_lines # noqa: E402 EXIT = ["exp mrt0 v0, v1, v2, v3 done vm", "s_endpgm"] EXACT_FMA = si.fma_f32 def split_fma(x, y, z): """GLSL Fma on a driver that rounds the product first, as the Intel UHD driver does for cs_fma_fused.""" return si.f32(si.f32(x * y) + z) def run_split(spv): """mrt0_words with the interpreter's Fma split into two roundings, so only the emulation can be exact.""" si.fma_f32 = split_fma try: return mrt0_words(spv) finally: si.fma_f32 = EXACT_FMA def run_split_flushing(spv): """run_split on a driver that also flushes every f32 denormal result to a zero of its sign.""" keep = si.f32 def f32_ftz(v): r = keep(v) return math.copysign(0.0, r) if r != 0.0 and abs(r) < 2.0 ** -126 else r si.f32 = f32_ftz try: return run_split(spv) finally: si.f32 = keep def f32_of(w): return struct.unpack("