aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


tools/client/test_portable_ciacos.py (2036B)


      1 """Compare the guest x87 arccosine 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-ciacos"
     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/ciacos.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/ciacos"], check=True)
     21 actual = subprocess.check_output(wsl + ["./build/linux/ciacos", "build/linux/ciacos.bin"], text=True, timeout=30)
     22 assert actual == expected, (expected, actual)
     23 windows = (work / "reference.bin").read_bytes()
     24 linux = (decompiled / "build/linux/ciacos.bin").read_bytes()
     25 assert len(windows) == len(linux) == 4113 * 20
     26 maximum = 0
     27 for offset in range(0, len(windows), 20):
     28     assert windows[offset:offset + 8] == linux[offset:offset + 8]
     29     assert windows[offset + 16:offset + 20] == linux[offset + 16:offset + 20]
     30     expected_value = struct.unpack_from("<d", windows, offset + 8)[0]
     31     actual_value = struct.unpack_from("<d", linux, offset + 8)[0]
     32     if math.isnan(expected_value):
     33         assert math.isnan(actual_value)
     34     else:
     35         assert math.isfinite(actual_value)
     36         distance = abs(struct.unpack_from("<Q", windows, offset + 8)[0] -
     37                        struct.unpack_from("<Q", linux, offset + 8)[0])
     38         assert distance <= 2, (offset // 20, expected_value, actual_value, distance)
     39         maximum = max(maximum, distance)
     40 print("Windows == Linux contract: " + actual.strip())
     41 print("Maximum finite result difference:", maximum, "ULPs")