aorecomp

Anarchy Online classic in the browser

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


commit 44d5e433e63565f8f9af2edbc08d698cc0bcf62c
parent 99ee5ae9d62d80333c445d0662029f8a27c0c26a
Author: anon <anon@example.com>
Date:   Fri, 16 Oct 2026 06:43:28 +0000

Implement finite classification with Windows differential checks

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/runtime/guest_crt.c | 8++++++++
Adecompiled/tests/finite.c | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_finite.py | 20++++++++++++++++++++
4 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -213,6 +213,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/devices build/linux/device-pixels.rgb ./build/linux/data-record ./build/linux/qsort + ./build/linux/finite build/linux/surfaces: tests/surfaces.c runtime/linux_graphics.c runtime/linux_window.c runtime/gl_renderer.c runtime/guest_heap.c runtime/guest_memory.c runtime/recomp_state.c runtime/linux_graphics.h runtime/linux_window.h runtime/gl_renderer.h runtime/guest_heap.h runtime/guest_memory.h runtime/recomp_types.h mkdir -p $(@D) @@ -352,3 +353,8 @@ build/linux/qsort: tests/qsort.c runtime/guest_crt.c runtime/guest_heap.c runtim $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ test: build/linux/qsort + +build/linux/finite: tests/finite.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/finite diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -459,6 +459,13 @@ static void crt_floor(void) g_esp += 4; } +static void crt_finite(void) +{ + /* Inspect the exponent without evaluating a signaling NaN on the host FPU. */ + g_eax = (MEM32(g_esp + 8) & 0x7ff00000u) != 0x7ff00000u; + g_esp += 4; +} + static void crt_ceil(void) { fp_push_impl(g_st, &g_fp_top, ceil(MEMD(g_esp + 4))); @@ -971,6 +978,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"_ftol", crt_ftol}, {"floor", crt_floor}, {"ceil", crt_ceil}, + {"_finite", crt_finite}, {"_CIfmod", crt_cifmod}, {"_CIacos", crt_ciacos}, {"_CIpow", crt_cipow}, diff --git a/decompiled/tests/finite.c b/decompiled/tests/finite.c @@ -0,0 +1,67 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#ifdef _WIN32 +#include <windows.h> +#else +#include "guest_crt.h" +#include "guest_memory.h" +recomp_func_t recomp_lookup(uint32_t address) { abort(); } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +#endif + +int main(void) +{ +#ifdef _WIN32 + HMODULE crt = LoadLibraryA("msvcrt.dll"); + int (__cdecl *finite)(double) = (void *)GetProcAddress(crt, "_finite"); + int *(__cdecl *error)(void) = (void *)GetProcAddress(crt, "_errno"); + assert(finite && error); +#else + assert(guest_map("finite stack", 0x50000000, 0x10000)); + g_fs_base = 0x50004000; + g_ebx = 0x12345678; g_esi = 0x23456789; + g_edi = 0x3456789a; g_ebp = 0x456789ab; + g_fp_top = 7; g_st[7] = 1.25; +#endif + unsigned finite_count = 0; + for (unsigned sign = 0; sign < 2; ++sign) + for (unsigned exponent = 0; exponent < 2048; ++exponent) + for (unsigned payload = 0; payload < 3; ++payload) { + uint64_t bits = ((uint64_t)sign << 63) | ((uint64_t)exponent << 52); + if (payload == 1) bits |= 1; + if (payload == 2) bits |= 0xfffffffffffff; + double value; + memcpy(&value, &bits, 8); +#ifdef _WIN32 + *error() = 777; +#else + g_esp = 0x5000ff00; + MEM32(g_esp) = RECOMP_RETADDR; + memcpy((void *)ADDR(g_esp + 4), &bits, 8); + MEM32(guest_crt_errno_address()) = 777; +#endif + +#ifdef _WIN32 + unsigned result = finite(value); +#else + guest_crt_lookup("_finite")(); + unsigned result = g_eax; +#endif + + assert(result == (exponent != 2047)); + finite_count += result; +#ifdef _WIN32 + assert(*error() == 777); +#else + assert(MEM32(guest_crt_errno_address()) == 777); + assert(g_esp == 0x5000ff04 && !memcmp((void *)ADDR(0x5000ff04), &bits, 8)); + assert(g_ebx == 0x12345678 && g_esi == 0x23456789); + assert(g_edi == 0x3456789a && g_ebp == 0x456789ab); + assert(g_fp_top == 7 && g_st[7] == 1.25); +#endif + } + assert(finite_count == 12282); + puts("finite: 12288 exponent/sign/payload combinations, errno, arguments and guest ABI passed"); +} diff --git a/tools/reconstruction/test_portable_finite.py b/tools/reconstruction/test_portable_finite.py @@ -0,0 +1,20 @@ +"""Compare IEEE-754 classification with Windows MSVCRT without evaluating NaNs.""" +import pathlib +import subprocess + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-finite" +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/finite.c", "-o", str(reference), +], cwd=decompiled, check=True) +expected = subprocess.check_output([str(reference)], 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/finite"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/finite"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux: " + actual.strip())