aorecomp

Anarchy Online classic in the browser

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


client/tests/atof.c (2610B)


      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     const char *cases[] = {"", "0", "-0", "1", "-2.5", "  3.75tail", ".5", "+.5",
     17         "1e3", "1e", "1e+", "0x1.8p+1", "nan", "inf", "infinity", "1.#INF", "1.#QNAN",
     18         "1e309", "-1e309", "1e-400", "1e-308", "2.2250738585072014e-308",
     19         "4.9406564584124654e-324", "1d2", "-", "\v\f\r\n\t42", "0.1", "1.2345678901234567"};
     20     const uint64_t expected[] = {0, 0, 0x8000000000000000ull, 0x3ff0000000000000ull,
     21         0xc004000000000000ull, 0x400e000000000000ull, 0x3fe0000000000000ull, 0x3fe0000000000000ull,
     22         0x408f400000000000ull, 0x3ff0000000000000ull, 0x3ff0000000000000ull, 0, 0, 0, 0,
     23         0x3ff0000000000000ull, 0x3ff0000000000000ull, 0x7ff0000000000000ull, 0xfff0000000000000ull,
     24         0, 0x000730d67819e8d2ull, 0x0010000000000000ull, 1, 0x4059000000000000ull, 0,
     25         0x4045000000000000ull, 0x3fb999999999999aull, 0x3ff3c0ca428c59fbull};
     26 #ifdef _WIN32
     27     HMODULE library = LoadLibraryA("msvcrt.dll");
     28     double (__cdecl *convert)(const char *) = (void *)GetProcAddress(library, "atof");
     29     int *(__cdecl *get_errno)(void) = (void *)GetProcAddress(library, "_errno");
     30     assert(convert && get_errno);
     31 #else
     32     assert(guest_map("atof test", 0x50000000, 0x10000));
     33     g_fs_base = 0x50000000;
     34 #endif
     35     for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) {
     36 #ifdef _WIN32
     37         *get_errno() = 777;
     38 #else
     39         strcpy((char *)ADDR(0x50000100), cases[i]);
     40         MEM32(guest_crt_errno_address()) = 777;
     41         g_esp = 0x5000ff00;
     42         MEM32(g_esp + 4) = 0x50000100;
     43         g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456;
     44         g_fp_top = 1;
     45         g_st[0] = 13.5;
     46 #endif
     47 
     48 #ifdef _WIN32
     49         double value = convert(cases[i]);
     50         int error = *get_errno();
     51 #else
     52         guest_crt_lookup("atof")();
     53         double value = g_st[0];
     54         int error = MEM32(guest_crt_errno_address());
     55 #endif
     56 
     57 #ifndef _WIN32
     58         assert(g_esp == 0x5000ff04 && g_fp_top == 2 && g_st[1] == 13.5);
     59         assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456);
     60 #endif
     61         uint64_t bits;
     62         memcpy(&bits, &value, sizeof(bits));
     63         assert(bits == expected[i] && error == 777);
     64     }
     65     puts("atof: 28 decimal, exponent, overflow, subnormal, signed zero and rejected-format cases; errno and x87 ABI passed");
     66 }