aorecomp

Anarchy Online classic in the browser

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


client/tests/finite.c (2336B)


      1 #include <assert.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #ifdef _WIN32
      6 #include <windows.h>
      7 #else
      8 #include "guest_crt.h"
      9 #include "guest_memory.h"
     10 recomp_func_t recomp_lookup(uint32_t address) { abort(); }
     11 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     12 #endif
     13 
     14 int main(void)
     15 {
     16 #ifdef _WIN32
     17     HMODULE crt = LoadLibraryA("msvcrt.dll");
     18     int (__cdecl *finite)(double) = (void *)GetProcAddress(crt, "_finite");
     19     int *(__cdecl *error)(void) = (void *)GetProcAddress(crt, "_errno");
     20     assert(finite && error);
     21 #else
     22     assert(guest_map("finite stack", 0x50000000, 0x10000));
     23     g_fs_base = 0x50004000;
     24     g_ebx = 0x12345678; g_esi = 0x23456789;
     25     g_edi = 0x3456789a; g_ebp = 0x456789ab;
     26     g_fp_top = 7; g_st[7] = 1.25;
     27 #endif
     28     unsigned finite_count = 0;
     29     for (unsigned sign = 0; sign < 2; ++sign)
     30         for (unsigned exponent = 0; exponent < 2048; ++exponent)
     31             for (unsigned payload = 0; payload < 3; ++payload) {
     32                 uint64_t bits = ((uint64_t)sign << 63) | ((uint64_t)exponent << 52);
     33                 if (payload == 1) bits |= 1;
     34                 if (payload == 2) bits |= 0xfffffffffffff;
     35                 double value;
     36                 memcpy(&value, &bits, 8);
     37 #ifdef _WIN32
     38                 *error() = 777;
     39 #else
     40                 g_esp = 0x5000ff00;
     41                 MEM32(g_esp) = RECOMP_RETADDR;
     42                 memcpy((void *)ADDR(g_esp + 4), &bits, 8);
     43                 MEM32(guest_crt_errno_address()) = 777;
     44 #endif
     45 
     46 #ifdef _WIN32
     47                 unsigned result = finite(value);
     48 #else
     49                 guest_crt_lookup("_finite")();
     50                 unsigned result = g_eax;
     51 #endif
     52 
     53                 assert(result == (exponent != 2047));
     54                 finite_count += result;
     55 #ifdef _WIN32
     56                 assert(*error() == 777);
     57 #else
     58                 assert(MEM32(guest_crt_errno_address()) == 777);
     59                 assert(g_esp == 0x5000ff04 && !memcmp((void *)ADDR(0x5000ff04), &bits, 8));
     60                 assert(g_ebx == 0x12345678 && g_esi == 0x23456789);
     61                 assert(g_edi == 0x3456789a && g_ebp == 0x456789ab);
     62                 assert(g_fp_top == 7 && g_st[7] == 1.25);
     63 #endif
     64             }
     65     assert(finite_count == 12282);
     66     puts("finite: 12288 exponent/sign/payload combinations, errno, arguments and guest ABI passed");
     67 }