client/tests/cipow.c (2896B)
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, double exponent, 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 fp_push_impl(g_st, &g_fp_top, exponent); 28 #endif 29 30 #ifdef _WIN32 31 __asm__ volatile("fldl %1; fldl %2; fldl %3; call *%4; fstpl %0; fstpl %1" 32 : "=m"(value), "+m"(sentinel) : "m"(input), "m"(exponent), "r"(function) 33 : "eax", "ecx", "edx", "st", "memory", "cc"); 34 int error = *get_errno(); 35 #else 36 guest_crt_lookup("_CIpow")(); 37 value = g_st[0]; sentinel = g_st[1]; 38 int error = MEM32(guest_crt_errno_address()); 39 #endif 40 41 assert(sentinel == 13.5); 42 if (input == 2 && exponent == 2) assert(value == 4 && error == 777); 43 if (input == -2 && exponent == 2) assert(value == 4 && error == 777); 44 if (input == 2 && exponent == -2) assert(value == .25 && error == 777); 45 if (input == -1 && exponent == .5) assert(isnan(value) && error == 33); 46 #ifndef _WIN32 47 assert(g_esp == 0x5000ff04 && g_fp_top == 2); 48 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 49 #endif 50 assert(fwrite(&input, 8, 1, output) == 1); 51 assert(fwrite(&exponent, 8, 1, output) == 1); 52 assert(fwrite(&value, 8, 1, output) == 1); 53 assert(fwrite(&error, 4, 1, output) == 1); 54 } 55 56 int main(int argc, char **argv) 57 { 58 assert(argc == 2); 59 #ifdef _WIN32 60 HMODULE library = LoadLibraryA("msvcrt.dll"); 61 function = (void *)GetProcAddress(library, "_CIpow"); 62 get_errno = (void *)GetProcAddress(library, "_errno"); 63 assert(function && get_errno); 64 #else 65 assert(guest_map("power fixture", 0x50000000, 0x10000)); 66 g_fs_base = 0x50000000; 67 #endif 68 FILE *output = fopen(argv[1], "wb"); 69 assert(output); 70 const double values[] = {0, -0., 1, -1, 2, -2, .5, -.5, 71 0x1p-1074, 1e308, 1e-300, 1024, -1024, INFINITY, -INFINITY, NAN}; 72 for (unsigned i = 0; i < 16; ++i) for (unsigned j = 0; j < 16; ++j) 73 compare(values[i], values[j], output); 74 uint32_t random = 12345; 75 for (unsigned i = 0; i < 4096; ++i) { 76 random = random * 1664525u + 1013904223u; 77 double input = .25 + (random & 65535) / 16384.; 78 random = random * 1664525u + 1013904223u; 79 compare(input, (int32_t)random / 134217728., output); 80 } 81 assert(!fclose(output)); 82 puts("CIpow: 4352 cases and x87 ABI passed"); 83 }