tools/client/test_portable_cipow.py (2129B)
1 """Compare the guest x87 power ABI and results with Windows MSVCRT.""" 2 import math 3 import pathlib 4 import struct 5 import subprocess 6 7 8 root = pathlib.Path(__file__).resolve().parents[2] 9 decompiled = root / "client" 10 work = root / "local/client/portable-cipow" 11 work.mkdir(parents=True, exist_ok=True) 12 reference = work / "reference.exe" 13 subprocess.run([ 14 str(root / "toolchains/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), 15 "-O2", "tests/cipow.c", "-o", str(reference), 16 ], cwd=decompiled, check=True) 17 expected = subprocess.check_output([str(reference), str(work / "reference.bin")], text=True, timeout=30) 18 linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] 19 wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] 20 subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/cipow"], check=True) 21 actual = subprocess.check_output(wsl + ["./build/linux/cipow", "build/linux/cipow.bin"], text=True, timeout=30) 22 assert actual == expected, (expected, actual) 23 windows = (work / "reference.bin").read_bytes() 24 linux = (decompiled / "build/linux/cipow.bin").read_bytes() 25 assert len(windows) == len(linux) == 4352 * 28 26 maximum = 0 27 for offset in range(0, len(windows), 28): 28 assert windows[offset:offset + 16] == linux[offset:offset + 16] 29 assert windows[offset + 24:offset + 28] == linux[offset + 24:offset + 28], offset // 28 30 expected_value = struct.unpack_from("<d", windows, offset + 16)[0] 31 actual_value = struct.unpack_from("<d", linux, offset + 16)[0] 32 if math.isnan(expected_value): 33 assert math.isnan(actual_value) 34 elif math.isinf(expected_value): 35 assert actual_value == expected_value 36 else: 37 assert math.isfinite(actual_value) 38 distance = abs(struct.unpack_from("<Q", windows, offset + 16)[0] - 39 struct.unpack_from("<Q", linux, offset + 16)[0]) 40 assert distance <= 2, (offset // 28, expected_value, actual_value, distance) 41 maximum = max(maximum, distance) 42 print("Windows == Linux contract: " + actual.strip()) 43 print("Maximum finite result difference:", maximum, "ULPs")