client/tests/cifmod.c (2914B)
1 #include <assert.h> 2 #include <math.h> 3 #include <stdint.h> 4 #include <stdio.h> 5 #include <string.h> 6 #ifdef _WIN32 7 #include <windows.h> 8 static void *function; 9 static int *(__cdecl *get_errno)(void); 10 #else 11 #include "guest_crt.h" 12 recomp_func_t recomp_lookup(uint32_t address) { abort(); } 13 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 14 #endif 15 16 static uint32_t digest = 2166136261u; 17 static void compare(double dividend, double divisor) 18 { 19 double value, sentinel = 13.5; 20 #ifdef _WIN32 21 *get_errno() = 777; 22 #else 23 MEM32(guest_crt_errno_address()) = 777; 24 g_esp = 0x5000ff00; 25 g_ebx = 123; g_esi = 234; g_edi = 345; g_ebp = 456; 26 g_fp_top = 0; 27 fp_push_impl(g_st, &g_fp_top, sentinel); 28 fp_push_impl(g_st, &g_fp_top, dividend); 29 fp_push_impl(g_st, &g_fp_top, divisor); 30 #endif 31 32 #ifdef _WIN32 33 __asm__ volatile("fldl %1; fldl %2; fldl %3; call *%4; fstpl %0; fstpl %1" 34 : "=m"(value), "+m"(sentinel) 35 : "m"(dividend), "m"(divisor), "r"(function) 36 : "eax", "ecx", "edx", "st", "memory", "cc"); 37 int error = *get_errno(); 38 #else 39 guest_crt_lookup("_CIfmod")(); 40 value = g_st[0]; 41 sentinel = g_st[1]; 42 int error = MEM32(guest_crt_errno_address()); 43 #endif 44 45 assert(sentinel == 13.5); 46 assert(error == ((!isfinite(dividend) || !isfinite(divisor) || !divisor) ? 33 : 777)); 47 #ifndef _WIN32 48 assert(g_esp == 0x5000ff04 && g_fp_top == 2); 49 assert(g_ebx == 123 && g_esi == 234 && g_edi == 345 && g_ebp == 456); 50 #endif 51 uint64_t bits; 52 memcpy(&bits, &value, sizeof(bits)); 53 digest = (digest ^ (uint32_t)bits) * 16777619u; 54 digest = (digest ^ (uint32_t)(bits >> 32)) * 16777619u; 55 digest = (digest ^ error) * 16777619u; 56 } 57 58 int main(void) 59 { 60 #ifdef _WIN32 61 HMODULE library = LoadLibraryA("msvcrt.dll"); 62 function = (void *)GetProcAddress(library, "_CIfmod"); 63 get_errno = (void *)GetProcAddress(library, "_errno"); 64 assert(function && get_errno); 65 #else 66 assert(guest_map("fmod fixture", 0x50000000, 0x10000)); 67 g_fs_base = 0x50000000; 68 #endif 69 const double values[] = {0, -0., 1, -1, 2.5, -2.5, 720.25, -720.25, 70 0x1p-1074, -0x1p-1074, 0x1.fffffffffffffp+1023}; 71 for (unsigned i = 0; i < sizeof(values) / sizeof(values[0]); ++i) 72 for (unsigned j = 2; j < sizeof(values) / sizeof(values[0]); ++j) 73 compare(values[i], values[j]); 74 uint32_t random = 12345; 75 for (unsigned i = 0; i < 4096; ++i) { 76 random = random * 1664525u + 1013904223u; 77 double dividend = (int32_t)random / 1024.; 78 random = random * 1664525u + 1013904223u; 79 double divisor = ((random & 0xffff) + 1) / 256.; 80 compare(dividend, divisor); 81 } 82 compare(1, 0); 83 compare(INFINITY, 1); 84 compare(1, INFINITY); 85 compare(NAN, 1); 86 compare(1, NAN); 87 assert(digest == 0x07d7d50b); 88 printf("CIfmod: 4200 cases, digest %08x, errno and x87 ABI passed\n", digest); 89 }