commit d1239f7976cac42c39a7c590379f849d90958f88
parent 3fa0fa87e28dac11fc72ee52f79513dc507c9eee
Author: anon <anon@example.com>
Date: Thu, 15 Oct 2026 13:30:39 +0000
Implement guest x87 arccosine for Linux animation math
Diffstat:
4 files changed, 136 insertions(+), 0 deletions(-)
diff --git a/decompiled/Makefile.linux b/decompiled/Makefile.linux
@@ -202,6 +202,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/atoi
./build/linux/atof
./build/linux/cifmod
+ ./build/linux/ciacos build/linux/ciacos.bin
./build/linux/image-row
./build/linux/ctype
./build/linux/itoa
@@ -327,3 +328,8 @@ build/linux/image-row: tests/image_row.c src/bbr.dll/lifted-0047.c runtime/guest
$(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections $(filter %.c,$^) -Wl,--gc-sections -lm -o $@
test: build/linux/image-row
+
+build/linux/ciacos: tests/ciacos.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/ciacos
diff --git a/decompiled/runtime/guest_crt.c b/decompiled/runtime/guest_crt.c
@@ -425,6 +425,15 @@ static void crt_floor(void)
g_esp += 4;
}
+static void crt_ciacos(void)
+{
+ double input = fp_pop_impl(g_st, &g_fp_top);
+ double value = acos(input);
+ if (!isfinite(input) || fabs(input) > 1) MEM32(guest_crt_errno_address()) = 33;
+ 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);
@@ -908,6 +917,7 @@ recomp_func_t guest_crt_lookup(const char *name)
{"_ftol", crt_ftol},
{"floor", crt_floor},
{"_CIfmod", crt_cifmod},
+ {"_CIacos", crt_ciacos},
{"atof", crt_atof},
{"isspace", crt_isspace},
{"_itoa", crt_itoa},
diff --git a/decompiled/tests/ciacos.c b/decompiled/tests/ciacos.c
@@ -0,0 +1,79 @@
+#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, 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);
+#endif
+
+#ifdef _WIN32
+ __asm__ volatile("fldl %1; fldl %2; call *%3; fstpl %0; fstpl %1"
+ : "=m"(value), "+m"(sentinel) : "m"(input), "r"(function)
+ : "eax", "ecx", "edx", "st", "memory", "cc");
+ int error = *get_errno();
+#else
+ guest_crt_lookup("_CIacos")();
+ value = g_st[0]; sentinel = g_st[1];
+ int error = MEM32(guest_crt_errno_address());
+#endif
+
+ assert(sentinel == 13.5);
+ assert(error == (!isfinite(input) || fabs(input) > 1 ? 33 : 777));
+ if (!isfinite(input) || fabs(input) > 1) assert(isnan(value));
+ else assert(value >= 0 && value <= 3.14159265358979324);
+ if (input == 1) assert(value == 0 && !signbit(value));
+#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(&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, "_CIacos");
+ get_errno = (void *)GetProcAddress(library, "_errno");
+ assert(function && get_errno);
+#else
+ assert(guest_map("acos fixture", 0x50000000, 0x10000));
+ g_fs_base = 0x50000000;
+#endif
+ FILE *output = fopen(argv[1], "wb");
+ assert(output);
+ const double values[] = {0, -0., 1, -1, .5, -.5, 0x1p-1074, -0x1p-1074,
+ 0x1.fffffffffffffp-1, -0x1.fffffffffffffp-1, 0x1.0000000000001p0,
+ -0x1.0000000000001p0, 2, -2, INFINITY, -INFINITY, NAN};
+ for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); ++i) compare(values[i], output);
+ uint32_t random = 12345;
+ for (unsigned i = 0; i < 4096; ++i) {
+ random = random * 1664525u + 1013904223u;
+ compare((int32_t)random / 2147483648., output);
+ }
+ assert(!fclose(output));
+ puts("CIacos: 4113 cases, domain errors and x87 ABI passed");
+}
diff --git a/tools/reconstruction/test_portable_ciacos.py b/tools/reconstruction/test_portable_ciacos.py
@@ -0,0 +1,41 @@
+"""Compare the guest x87 arccosine 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-ciacos"
+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/ciacos.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/ciacos"], check=True)
+actual = subprocess.check_output(wsl + ["./build/linux/ciacos", "build/linux/ciacos.bin"], text=True, timeout=30)
+assert actual == expected, (expected, actual)
+windows = (work / "reference.bin").read_bytes()
+linux = (decompiled / "build/linux/ciacos.bin").read_bytes()
+assert len(windows) == len(linux) == 4113 * 20
+maximum = 0
+for offset in range(0, len(windows), 20):
+ assert windows[offset:offset + 8] == linux[offset:offset + 8]
+ assert windows[offset + 16:offset + 20] == linux[offset + 16:offset + 20]
+ expected_value = struct.unpack_from("<d", windows, offset + 8)[0]
+ actual_value = struct.unpack_from("<d", linux, offset + 8)[0]
+ if math.isnan(expected_value):
+ assert math.isnan(actual_value)
+ else:
+ assert math.isfinite(actual_value)
+ distance = abs(struct.unpack_from("<Q", windows, offset + 8)[0] -
+ struct.unpack_from("<Q", linux, offset + 8)[0])
+ assert distance <= 2, (offset // 20, expected_value, actual_value, distance)
+ maximum = max(maximum, distance)
+print("Windows == Linux contract: " + actual.strip())
+print("Maximum finite result difference:", maximum, "ULPs")