1:1 source port of the engine and game code; Windows build and system layer; docs. No game files or SDK headers.
This commit is contained in:
parent
c3b032a45a
commit
0ecb47e9cb
3871 changed files with 91080 additions and 1640063 deletions
64
tools/port/emu_stubs/math.py
Normal file
64
tools/port/emu_stubs/math.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
"""libm import stubs (float functions) for the emulator, behaving like 2009 Darwin i386 libm.
|
||||
|
||||
i386 float functions return their result in x87 ST0. A Python hook cannot push the x87 stack directly, so the
|
||||
stub stores the float in a scratch word and jumps to a tiny trampoline `flds [scratch]; ret` (the caller's return
|
||||
address is still on the stack, so the trampoline's `ret` returns to the caller, which pops ST0 as usual).
|
||||
|
||||
sinf/cosf: Darwin's libm of that era evaluates them in double precision and rounds to float, i.e. the result is
|
||||
the correctly rounded float of the double result. The double sin/cos of the host libm is used for that.
|
||||
Setting `g.h_libm_host = True` makes the stubs call the host's sinf/cosf instead (the libm a port runs on), so a
|
||||
differential test can separate libm rounding differences from differences in the ported code.
|
||||
Helpers are prefixed h_ so the stub loader (which registers callables starting with a single '_') skips them.
|
||||
"""
|
||||
import ctypes
|
||||
import math
|
||||
import struct
|
||||
|
||||
from unicorn.x86_const import UC_X86_REG_ESP, UC_X86_REG_EIP
|
||||
|
||||
|
||||
def h_f32(x):
|
||||
return struct.unpack('<f', struct.pack('<f', x))[0]
|
||||
|
||||
|
||||
def h_arg_f32(g, i):
|
||||
esp = g.uc.reg_read(UC_X86_REG_ESP)
|
||||
return struct.unpack('<f', bytes(g.uc.mem_read(esp + 4 + 4 * i, 4)))[0]
|
||||
|
||||
|
||||
def h_fld_trampoline(g):
|
||||
"""Returns (code, scratch): code = `flds [scratch]; ret`, placed on its own page, scratch on another page."""
|
||||
t = getattr(g, 'h_fld_tramp', None)
|
||||
if t is None:
|
||||
block = g.malloc(0x3000)
|
||||
code = (block + 0xfff) & ~0xfff
|
||||
scratch = code + 0x1000
|
||||
g.uc.mem_write(code, b'\xd9\x05' + struct.pack('<I', scratch) + b'\xc3')
|
||||
t = g.h_fld_tramp = (code, scratch)
|
||||
return t
|
||||
|
||||
|
||||
def h_ret_f32(g, value):
|
||||
code, scratch = h_fld_trampoline(g)
|
||||
g.uc.mem_write(scratch, struct.pack('<f', value))
|
||||
g.uc.reg_write(UC_X86_REG_EIP, code)
|
||||
|
||||
|
||||
h_libc = ctypes.CDLL(None)
|
||||
for _n in ('sinf', 'cosf'):
|
||||
getattr(h_libc, _n).restype = ctypes.c_float
|
||||
getattr(h_libc, _n).argtypes = [ctypes.c_float]
|
||||
|
||||
|
||||
def h_libm(g, name, model, x):
|
||||
if getattr(g, 'h_libm_host', False):
|
||||
return getattr(h_libc, name)(x)
|
||||
return model(x) if math.isfinite(x) else math.nan
|
||||
|
||||
|
||||
def _sinf(g):
|
||||
h_ret_f32(g, h_libm(g, 'sinf', math.sin, h_arg_f32(g, 0)))
|
||||
|
||||
|
||||
def _cosf(g):
|
||||
h_ret_f32(g, h_libm(g, 'cosf', math.cos, h_arg_f32(g, 0)))
|
||||
Loading…
Add table
Add a link
Reference in a new issue