aorecomp

Anarchy Online classic in the browser

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


commit dc14f99c79fc87460678a58ad1ea7caf83ebea77
parent 6126dc368b7cef99e8ecb369ed671904a2b1e0c3
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 12:32:03 +0000

Implement legacy x87 remainder semantics on Linux

Diffstat:
Mdecompiled/Makefile.linux | 6++++++
Mdecompiled/runtime/guest_crt.c | 15+++++++++++++++
Adecompiled/tests/cifmod.c | 89+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atools/reconstruction/test_portable_cifmod.py | 23+++++++++++++++++++++++
4 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux @@ -201,6 +201,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/splitpath ./build/linux/atoi ./build/linux/atof + ./build/linux/cifmod ./build/linux/ctype ./build/linux/itoa ./build/linux/graphics @@ -315,3 +316,8 @@ build/linux/itoa: tests/itoa.c runtime/guest_crt.c runtime/guest_heap.c runtime/ $(CC) $(CPPFLAGS) $(CFLAGS) $(filter %.c,$^) -lm -o $@ test: build/linux/itoa + +build/linux/cifmod: tests/cifmod.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/cifmod diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c @@ -425,6 +425,20 @@ static void crt_floor(void) g_esp += 4; } +static void crt_cifmod(void) +{ + double divisor = fp_pop_impl(g_st, &g_fp_top); + double dividend = fp_pop_impl(g_st, &g_fp_top); + double value = fmod(dividend, divisor); + if (!dividend && divisor && isfinite(divisor)) value = 0; + /* Legacy MSVCRT treats every non-finite operand as a domain error. */ + if (isinf(divisor) && !isnan(dividend)) value = -NAN; + if (!isfinite(dividend) || !isfinite(divisor) || !divisor) + MEM32(guest_crt_errno_address()) = 33; + fp_push_impl(g_st, &g_fp_top, value); + g_esp += 4; +} + static void crt_atof(void) { const char *input = (const char *)ADDR(MEM32(g_esp + 4)); @@ -893,6 +907,7 @@ recomp_func_t guest_crt_lookup(const char *name) {"??2@YAPAXI@Z", crt_new}, {"??3@YAXPAX@Z", crt_free}, {"_ftol", crt_ftol}, {"floor", crt_floor}, + {"_CIfmod", crt_cifmod}, {"atof", crt_atof}, {"isspace", crt_isspace}, {"_itoa", crt_itoa}, diff --git a/decompiled/tests/cifmod.c b/decompiled/tests/cifmod.c @@ -0,0 +1,89 @@ +#include <assert.h> +#include <math.h> +#include <stdint.h> +#include <stdio.h> +#include <string.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 uint32_t digest = 2166136261u; +static void compare(double dividend, double divisor) +{ + 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, dividend); + fp_push_impl(g_st, &g_fp_top, divisor); +#endif + +#ifdef _WIN32 + __asm__ volatile("fldl %1; fldl %2; fldl %3; call *%4; fstpl %0; fstpl %1" + : "=m"(value), "+m"(sentinel) + : "m"(dividend), "m"(divisor), "r"(function) + : "eax", "ecx", "edx", "st", "memory", "cc"); + int error = *get_errno(); +#else + guest_crt_lookup("_CIfmod")(); + value = g_st[0]; + sentinel = g_st[1]; + int error = MEM32(guest_crt_errno_address()); +#endif + + assert(sentinel == 13.5); + assert(error == ((!isfinite(dividend) || !isfinite(divisor) || !divisor) ? 33 : 777)); +#ifndef _WIN32 + assert(g_esp == 0x5000ff04 && g_fp_top == 2); + assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); +#endif + uint64_t bits; + memcpy(&bits, &value, sizeof(bits)); + digest = (digest ^ (uint32_t)bits) * 16777619u; + digest = (digest ^ (uint32_t)(bits >> 32)) * 16777619u; + digest = (digest ^ error) * 16777619u; +} + +int main(void) +{ +#ifdef _WIN32 + HMODULE library = LoadLibraryA("msvcrt.dll"); + function = (void *)GetProcAddress(library, "_CIfmod"); + get_errno = (void *)GetProcAddress(library, "_errno"); + assert(function && get_errno); +#else + assert(guest_map("fmod fixture", 0x50000000, 0x10000)); + g_fs_base = 0x50000000; +#endif + const double values[] = {0, -0., 1, -1, 2.5, -2.5, 720.25, -720.25, + 0x1p-1074, -0x1p-1074, 0x1.fffffffffffffp+1023}; + for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); ++i) + for (unsigned j = 2; j < sizeof(values) / sizeof(values[0]); ++j) + compare(values[i], values[j]); + uint32_t random = 12345; + for (unsigned i = 0; i < 4096; ++i) { + random = random * 1664525u + 1013904223u; + double dividend = (int32_t)random / 1024.; + random = random * 1664525u + 1013904223u; + double divisor = ((random & 0xffff) + 1) / 256.; + compare(dividend, divisor); + } + compare(1, 0); + compare(INFINITY, 1); + compare(1, INFINITY); + compare(NAN, 1); + compare(1, NAN); + assert(digest == 0x07d7d50b); + printf("CIfmod: 4200 cases, digest %08x, errno and x87 ABI passed\n", digest); +} diff --git a/tools/reconstruction/test_portable_cifmod.py b/tools/reconstruction/test_portable_cifmod.py @@ -0,0 +1,23 @@ +"""Compare guest x87 remainder with Windows MSVCRT.""" +import pathlib +import subprocess + + +root = pathlib.Path(__file__).resolve().parents[2] +decompiled = root / "decompiled" +work = root / "tools/reconstruction/work/portable-cifmod" +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/cifmod.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/cifmod"], check=True) +actual = subprocess.check_output(wsl + ["./build/linux/cifmod"], text=True, timeout=30) +assert actual == expected, (expected, actual) +print("Windows == Linux contract: " + actual.strip()) + +