aorecomp

Anarchy Online classic in the browser

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


client/runtime/x87crt.c (3559B)


      1 /*
      2  * x87crt: the msvcrt helpers that take their arguments on the x87 stack.
      3  *
      4  * native32's bridge copies a native callee's *stack* arguments and moves an
      5  * x87 result back onto the lifted FPU stack, but lifted code keeps its FPU
      6  * stack in g_st[], not in the CPU's registers. A helper that reads its input
      7  * from st(0) therefore ran natively on whatever the real FPU held. MSVC
      8  * compiles every float->integer cast as `call _ftol`, and the intrinsic forms
      9  * of pow, fmod, sqrt, sin... as `call _CIpow` and friends, so a guest that
     10  * links msvcrt.dll dynamically got the integer indefinite (0x80000000) from
     11  * every cast. A title that links the CRT statically lifts these and never
     12  * notices.
     13  *
     14  * So these are native32 built-ins, run inside the lifted model: operands
     15  * popped from g_st (st(0) the right-hand one for the binary ones, as the
     16  * compiler pushes `x` then `y`), the result pushed back or returned in
     17  * edx:eax. Semantics are the documented C ones; nothing here is derived from
     18  * another runtime's source.
     19  */
     20 #include <math.h>
     21 #include "recomp_types.h"
     22 
     23 /* Included at the end of native32.c, so every host that builds native32.c
     24  * gets these without a change to its build. */
     25 #define X87_POP()   fp_pop_impl(g_st, &g_fp_top)
     26 #define X87_PUSH(v) fp_push_impl(g_st, &g_fp_top, (v))
     27 
     28 /* _ftol: st(0) to a 64-bit integer, truncating whatever the control word
     29  * says (that is the C cast), popped, in edx:eax. A value fild loaded exactly
     30  * keeps its exact integer (g_st_i64 shadow). */
     31 void native32_shim_ftol(void) {
     32     int64_t q = fp_st0_to_i64(g_st[0], g_st_i64[0], 0x0C7F);   /* RC = chop */
     33     (void)X87_POP();
     34     g_eax = (uint32_t)q;
     35     g_edx = (uint32_t)((uint64_t)q >> 32);
     36     g_esp += 4;
     37 }
     38 
     39 /* _controlfp(new, mask) and _control87: the FPU's precision and rounding
     40  * mode. Run natively they set the real control word, which native code
     41  * (an original module, Galaxy's built mixers) does use; but lifted fistp and
     42  * frndint round by g_fpu_cw. Unreal sets its mode this way at startup, and
     43  * its renderer's rasterisation went wrong without it. So the real call runs,
     44  * then the hardware word it left is copied into g_fpu_cw. cdecl: the caller
     45  * pops the two arguments. */
     46 typedef unsigned int (__cdecl *controlfp_t)(unsigned int, unsigned int);
     47 
     48 static void control_word(const char* name) {
     49     static controlfp_t fn[2];
     50     int k = name[8] == '8';                       /* _control87 */
     51     if (!fn[k]) fn[k] = (controlfp_t)GetProcAddress(LoadLibraryA("msvcrt.dll"), name);
     52     uint16_t cw;
     53     g_eax = fn[k](MEM32(g_esp + 4), MEM32(g_esp + 8));
     54     __asm fnstcw cw
     55     g_fpu_cw = cw;
     56     g_esp += 4;
     57 }
     58 
     59 void native32_shim_controlfp(void) { control_word("_controlfp"); }
     60 void native32_shim_control87(void) { control_word("_control87"); }
     61 
     62 #define UNARY(name, expr)                                                   \
     63     void native32_shim_##name(void) { double x = X87_POP(); X87_PUSH(expr); g_esp += 4; }
     64 #define BINARY(name, expr)                                                    \
     65     void native32_shim_##name(void) {                                         \
     66         double y = X87_POP(), x = X87_POP(); X87_PUSH(expr); g_esp += 4; }
     67 
     68 UNARY(CIsqrt, sqrt(x))
     69 UNARY(CIsin, sin(x))
     70 UNARY(CIcos, cos(x))
     71 UNARY(CItan, tan(x))
     72 UNARY(CIasin, asin(x))
     73 UNARY(CIacos, acos(x))
     74 UNARY(CIatan, atan(x))
     75 UNARY(CIexp, exp(x))
     76 UNARY(CIlog, log(x))
     77 UNARY(CIlog10, log10(x))
     78 UNARY(CIsinh, sinh(x))
     79 UNARY(CIcosh, cosh(x))
     80 UNARY(CItanh, tanh(x))
     81 BINARY(CIpow, pow(x, y))
     82 BINARY(CIfmod, fmod(x, y))
     83 BINARY(CIatan2, atan2(x, y))