aorecomp

Anarchy Online classic in the browser

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


client/tests/ciacos.c (2683B)


      1 #include <assert.h>
      2 #include <math.h>
      3 #include <stdint.h>
      4 #include <stdio.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 static void *function;
      8 static int *(__cdecl *get_errno)(void);
      9 #else
     10 #include "guest_crt.h"
     11 recomp_func_t recomp_lookup(uint32_t address) { abort(); }
     12 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     13 #endif
     14 
     15 static void compare(double input, FILE *output)
     16 {
     17     double value, sentinel = 13.5;
     18 #ifdef _WIN32
     19     *get_errno() = 777;
     20 #else
     21     MEM32(guest_crt_errno_address()) = 777;
     22     g_esp = 0x5000ff00;
     23     g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
     24     g_fp_top = 0;
     25     fp_push_impl(g_st, &g_fp_top, sentinel);
     26     fp_push_impl(g_st, &g_fp_top, input);
     27 #endif
     28 
     29 #ifdef _WIN32
     30     __asm__ volatile("fldl %1; fldl %2; call *%3; fstpl %0; fstpl %1"
     31         : "=m"(value), "+m"(sentinel) : "m"(input), "r"(function)
     32         : "eax", "ecx", "edx", "st", "memory", "cc");
     33     int error = *get_errno();
     34 #else
     35     guest_crt_lookup("_CIacos")();
     36     value = g_st[0]; sentinel = g_st[1];
     37     int error = MEM32(guest_crt_errno_address());
     38 #endif
     39 
     40     assert(sentinel == 13.5);
     41     assert(error == (!isfinite(input) || fabs(input) > 1 ? 33 : 777));
     42     if (!isfinite(input) || fabs(input) > 1) assert(isnan(value));
     43     else assert(value >= 0 && value <= 3.14159265358979324);
     44     if (input == 1) assert(value == 0 && !signbit(value));
     45 #ifndef _WIN32
     46     assert(g_esp == 0x5000ff04 && g_fp_top == 2);
     47     assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
     48 #endif
     49     assert(fwrite(&input, 8, 1, output) == 1);
     50     assert(fwrite(&value, 8, 1, output) == 1);
     51     assert(fwrite(&error, 4, 1, output) == 1);
     52 }
     53 
     54 int main(int argc, char **argv)
     55 {
     56     assert(argc == 2);
     57 #ifdef _WIN32
     58     HMODULE library = LoadLibraryA("msvcrt.dll");
     59     function = (void *)GetProcAddress(library, "_CIacos");
     60     get_errno = (void *)GetProcAddress(library, "_errno");
     61     assert(function && get_errno);
     62 #else
     63     assert(guest_map("acos fixture", 0x50000000, 0x10000));
     64     g_fs_base = 0x50000000;
     65 #endif
     66     FILE *output = fopen(argv[1], "wb");
     67     assert(output);
     68     const double values[] = {0, -0., 1, -1, .5, -.5, 0x1p-1074, -0x1p-1074,
     69         0x1.fffffffffffffp-1, -0x1.fffffffffffffp-1, 0x1.0000000000001p0,
     70         -0x1.0000000000001p0, 2, -2, INFINITY, -INFINITY, NAN};
     71     for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); ++i) compare(values[i], output);
     72     uint32_t random = 12345;
     73     for (unsigned i = 0; i < 4096; ++i) {
     74         random = random * 1664525u + 1013904223u;
     75         compare((int32_t)random / 2147483648., output);
     76     }
     77     assert(!fclose(output));
     78     puts("CIacos: 4113 cases, domain errors and x87 ABI passed");
     79 }