aorecomp

Anarchy Online classic in the browser

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


commit 55005cffeb7eb2bb25081dc2afd9a77e68c67365
parent b6e5a65e0d89e3ba01e81b557974d4ca98934363
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 15:32:16 +0000

Implement guest x87 power with MSVCRT differential coverage

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/runtime/guest_crt.c | 14++++++++++++++
Adecompiled/tests/cipow.c | 83+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_cipow.py | 43+++++++++++++++++++++++++++++++++++++++++++
4 files changed, 146 insertions(+), 0 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -203,6 +203,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/atof ./build/linux/cifmod ./build/linux/ciacos build/linux/ciacos.bin + ./build/linux/cipow build/linux/cipow.bin ./build/linux/image-row ./build/linux/ctype ./build/linux/itoa @@ -335,6 +336,11 @@ build/linux/ciacos: tests/ciacos.c runtime/guest_crt.c runtime/guest_heap.c runt test: build/linux/ciacos +build/linux/cipow: tests/cipow.c runtime/guest_crt.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_crt.h runtime/guest_heap.h runtime/guest_memory.h + $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ + +test: build/linux/cipow + build/linux/data-record: tests/data_record.c src/ar.dll/lifted-0001.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h mkdir -p $(@D) $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@ diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -440,6 +440,19 @@ static void crt_ciacos(void) g_esp += 4; } +static void crt_cipow(void) +{ + double exponent = fp_pop_impl(g_st, &g_fp_top); + double base = fp_pop_impl(g_st, &g_fp_top); + double value = pow(base, exponent); + /* MSVCRT treats NaN operands and (+/-1)^(-infinity) as domain errors. */ + if (isnan(base) || isnan(exponent) || (fabs(base) == 1 && exponent == -INFINITY)) value = -NAN; + if (isnan(value)) MEM32(guest_crt_errno_address()) = 33; + else if (isinf(value) && isfinite(base) && isfinite(exponent)) MEM32(guest_crt_errno_address()) = 34; + fp_push_impl(g_st, &g_fp_top, value); + g_esp += 4; +} + static void crt_cifmod(void) { double divisor = fp_pop_impl(g_st, &g_fp_top); @@ -925,6 +938,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"ceil", crt_ceil}, {"_CIfmod", crt_cifmod}, {"_CIacos", crt_ciacos}, + {"_CIpow", crt_cipow}, {"atof", crt_atof}, {"isspace", crt_isspace}, {"_itoa", crt_itoa}, diff --git a/decompiled/tests/cipow.c b/decompiled/tests/cipow.c @@ -0,0 +1,83 @@ +#include <assert.h> +#include <math.h> +#include <stdint.h> +#include <stdio.h> +#ifdef _WIN32 +#include <windows.h> +static void *function; +static int *(__cdecl *get_errno)(void); +#else +#include "guest_crt.h" +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +#endif + +static void compare(double input, double exponent, FILE *output) +{ + double value, sentinel = 13.5; +#ifdef _WIN32 + *get_errno() = 777; +#else + MEM32(guest_crt_errno_address()) = 777; + g_esp = 0x5000ff00; + g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; + g_fp_top = 0; + fp_push_impl(g_st, &g_fp_top, sentinel); + fp_push_impl(g_st, &g_fp_top, input); + fp_push_impl(g_st, &g_fp_top, exponent); +#endif + +#ifdef _WIN32 + __asm__ volatile("fldl %1; fldl %2; fldl %3; call *%4; fstpl %0; fstpl %1" + : "=m"(value), "+m"(sentinel) : "m"(input), "m"(exponent), "r"(function) + : "eax", "ecx", "edx", "st", "memory", "cc"); + int error = *get_errno(); +#else + guest_crt_lookup("_CIpow")(); + value = g_st[0]; sentinel = g_st[1]; + int error = MEM32(guest_crt_errno_address()); +#endif + + assert(sentinel == 13.5); + if (input == 2 && exponent == 2) assert(value == 4 && error == 777); + if (input == -2 && exponent == 2) assert(value == 4 && error == 777); + if (input == 2 && exponent == -2) assert(value == .25 && error == 777); + if (input == -1 && exponent == .5) assert(isnan(value) && error == 33); +#ifndef _WIN32 + assert(g_esp == 0x5000ff04 && g_fp_top == 2); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); +#endif + assert(fwrite(&input, 8, 1, output) == 1); + assert(fwrite(&exponent, 8, 1, output) == 1); + assert(fwrite(&value, 8, 1, output) == 1); + assert(fwrite(&error, 4, 1, output) == 1); +} + +int main(int argc, char **argv) +{ + assert(argc == 2); +#ifdef _WIN32 + HMODULE library = LoadLibraryA("msvcrt.dll"); + function = (void *)GetProcAddress(library, "_CIpow"); + get_errno = (void *)GetProcAddress(library, "_errno"); + assert(function && get_errno); +#else + assert(guest_map("power fixture", 0x50000000, 0x10000)); + g_fs_base = 0x50000000; +#endif + FILE *output = fopen(argv[1], "wb"); + assert(output); + const double values[] = {0, -0., 1, -1, 2, -2, .5, -.5, + 0x1p-1074, 1e308, 1e-300, 1024, -1024, INFINITY, -INFINITY, NAN}; + for (unsigned i = 0; i < 16; ++i) for (unsigned j = 0; j < 16; ++j) + compare(values[i], values[j], output); + uint32_t random = 12345; + for (unsigned i = 0; i < 4096; ++i) { + random = random * 1664525u + 1013904223u; + double input = .25 + (random & 65535) / 16384.; + random = random * 1664525u + 1013904223u; + compare(input, (int32_t)random / 134217728., output); + } + assert(!fclose(output)); + puts("CIpow: 4352 cases and x87 ABI passed"); +} diff --git a/tools/reconstruction/test_portable_cipow.py b/tools/reconstruction/test_portable_cipow.py @@ -0,0 +1,43 @@ +"""Compare the guest x87 power ABI and results with Windows MSVCRT.""" +import math +import pathlib +import struct +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-cipow" +work.mkdir(parents=True, exist_ok=True) +reference = work / "reference.exe" +subprocess.run([ + str(root / "tools/rust/llvm-mingw-20261006-ucrt-x86_64/bin/i686-w64-mingw32-clang.exe"), + "-O2", "tests/cipow.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference), str(work / "reference.bin")], text=True, timeout=30) +linux_path = "/mnt/" + decompiled.drive[0].lower() + decompiled.as_posix()[2:] +wsl = ["wsl", "-d", "Ubuntu-26.04", "--cd", linux_path, "--exec"] +subprocess.run(wsl + ["make", "-s", "-f", "Makefile.linux", "build/linux/cipow"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/cipow", "build/linux/cipow.bin"], text=True, timeout=30) +assert actual == expected, (expected, actual) +windows = (work / "reference.bin").read_bytes() +linux = (decompiled / "build/linux/cipow.bin").read_bytes() +assert len(windows) == len(linux) == 4352 * 28 +maximum = 0 +for offset in range(0, len(windows), 28): + assert windows[offset:offset + 16] == linux[offset:offset + 16] + assert windows[offset + 24:offset + 28] == linux[offset + 24:offset + 28], offset // 28 + expected_value = struct.unpack_from("<d", windows, offset + 16)[0] + actual_value = struct.unpack_from("<d", linux, offset + 16)[0] + if math.isnan(expected_value): + assert math.isnan(actual_value) + elif math.isinf(expected_value): + assert actual_value == expected_value + else: + assert math.isfinite(actual_value) + distance = abs(struct.unpack_from("<Q", windows, offset + 16)[0] - + struct.unpack_from("<Q", linux, offset + 16)[0]) + assert distance <= 2, (offset // 28, expected_value, actual_value, distance) + maximum = max(maximum, distance) +print("Windows == Linux contract: " + actual.strip()) +print("Maximum finite result difference:", maximum, "ULPs")