80-bit Ghosts: x87 Precision in Decompilation
// status: budding
When you translate a 2000 x86 game engine into JavaScript, Rust, or WebGL, floating-point math feels like the easy part. IEEE-754 is IEEE-754, right? A float is 32 bits, a double is 64 bits, and Math.tan is tangent.
That assumption survived about forty-eight hours in our Deus Ex decompilation. Then an intro camera rotated to 74.99874°, a character mesh pose diverged by 1,257 ULPs, Metal's GLSL compiler fused a * b + c into a single fma(), and forty-one million volumetric fog texels disagreed by a hair. Every single bug came from the same ghost: the 80-bit x87 FPU inside a 2000 Pentium II.
Three control words on one thread
Every x87 register (ST(0)..ST(7)) holds an 80-bit extended-precision float: 1 sign bit, a 15-bit biased exponent (range up to 2^{\pm 16383}), an explicit integer bit, and a 64-bit significand.
Unlike SSE or WebAssembly — where every instruction (addss vs addsd, f32.add vs f64.add) carries its own width — x87 arithmetic reads its rounding precision from two bits (PC, bits 8–9) of the thread's 16-bit FPU control word:
| Control word | Precision (PC) | Significand | Where Deus Ex actually runs it |
|---|---|---|---|
0x037f | PC 64 (extended) | 64 bits | Game thread after frame 1 (ULevel::Tick, CreateMasterFrame, Galaxy Update & reverb setup) |
0x027f | PC 53 (double) | 53 bits | Windows thread start-up (URender::Init, FLightManager::Init lookup tables, first 2 ticks) |
0x007f | PC 24 (single) | 24 bits | The entire render pass between URender::PreRender and URender::PostRender |
Why does URender::PreRender drop the whole CPU to 0x007f (appEnableFastMath(1)) on every single frame, and PostRender put it back to 0x037f (appEnableFastMath(0))? Because on a 1998 Pentium II, 24-bit FDIV and FSQRT took 18 cycles instead of 38!
That single control-word switch caught us completely off-guard. For months we validated ULodMesh::GetFrame, DrawLodMesh, and volumetric fog against our Unicorn emulator at 0x037f. When we finally attached a live debugger (tools/dx-fpu-win.c) to the retail DeusEx.exe, every single one of 292,065 hits inside the render pass reported 0x007f. Re-checking GetFrame at 0x007f showed the old 64-bit translation had differed in 7,562 of 29,823 vertex coordinates (up to 1,257 ULPs).
Notice the crucial subtlety of PC 24: it rounds the significand to 24 bits while keeping the 15-bit x87 exponent. For ordinary numbers, PC 24 matches IEEE binary32 (f32) bit-for-bit — except when a product dips below 2^{-126} (where f32 flushes into coarse subnormals while PC 24 stays completely normal!). That's why our Rust fog and mesh kernels run fast f32, guard |r| <= f32::MIN_POSITIVE, and rerun those rare subnormal texels on an exact 80-bit model.
Transcendentals (FPTAN and FCOS) aren't even deterministic across silicon
Worse still: x87 precision control does not apply to transcendental instructions (FPTAN, FCOS, FSIN), and Intel, AMD, and Rosetta 2 all disagree in the last bit of their 64-bit results!
In Deus Ex, FSceneNode::ComputeRenderSize computes the projection focal length from the camera's FOV half-angle via Core's appTan (FLD qword; FPTAN; FSTP ST(0)):
\text{Proj.Z} = \frac{\text{float32}(\text{Width}) \cdot 0.5}{\operatorname{appTan}\!\left(\text{float32}(\text{FOV}) \cdot 0.008726646259971648\right)}
Across 208,181 real hardware probes on AMD x87 silicon, FPTAN missed the correctly rounded IEEE double 39 times — every single one within 0.581 extended ULPs of a halfway midpoint between two doubles! The intro cutscene 00_Intro happens to pan through 74.99874°, right beside one of those razor-thin midpoints.
Rather than guessing with Math.tan, our engine computes \tan(x) to 256 bits of fixed-point precision using BigInt Taylor series (x87TanCandidates), checks whether the true value lies within an 8-extended-unit uncertainty band of a halfway midpoint, and returns both candidate neighbours whenever hardware could round either way. And for Galaxy's reverb low-pass coefficient k = \sqrt{(a+2)a} - a where a = 1 - \cos(2\pi f_c / f_s): AMD FCOS misses the correctly rounded 64-bit cosine at 5,820 of 44,101 cutoffs (Rosetta 2 misses at 6,989), yet after storing k and 1 - k to 32-bit float, zero of all 44,101 cutoffs differ.
FMA contraction on modern GPUs
When we ported ULodMesh::GetFrame to a WebGL2 vertex-pose pass, the same PC 24 binary32 equations ran on the GPU — and immediately failed bit-for-bit verification on Apple Silicon!
Why? Because a * b + c on a Pentium II executes two separate instructions (FMUL with 24-bit rounding, then FADD with 24-bit rounding). Modern GLSL/Metal compilers silently contract a * b + c into a single fused multiply-add (fma(a, b, c)), skipping the intermediate rounding. A single 1-ULP shift in a rotated mesh vertex propagates through normal normalization and Gouraud shading. Wrapping every GPU multiply and add behind a no-contraction barrier restores exact 2000 Pentium II arithmetic on a 2026 GPU.
The 1.0001 and -0.5 pixel-center ghosts
Even after every floating-point register agrees to the last bit, wireframes and HUD overlays still shift by a pixel unless you reproduce two classic 1999 rasterizer quirks baked into Render.dll and D3DDrv.dll:
FX15 = (SizeX + 1.0001) * 0.5inComputeRenderSize— Tim Sweeney's tiny0.0001bias nudged exact screen-center half-integers off the rounding boundary so software-rendered scanlines wouldn't crack along the seam.-0.5inD3DDrv— Direct3D 7 placed pixel centers at integer coordinates (0, 0) while texel centers sat at (0.5, 0.5), soD3DDrvsubtracted0.5from every screen-space vertex before submission. Drop either constant and crisp 1-pixel UI borders blur across two rows.
Part of the Deus Ex in the Browser notes · see BSP Occlusion & Span Buffers · Analytic Volumetric Fog · Vertex Animation Without Bones · back to Notes.