On this page
Float precision: 24 bits, 53 bits, and a store
The three games are 32-bit x87 programs. The x87 computes in 80-bit registers and rounds each result to a precision that the process chooses; a value only becomes a 32-bit float when it is stored to memory. Two things follow, and both decide whether a port's vehicle ends a lap where the original's does: the process's precision, and the places where the compiler stored.
The two precisions#
| Process | x87 control word | Register results are rounded to |
|---|---|---|
| a dedicated server | 0x027F, the C runtime's default | 53 bits |
| a client | 0x007F: Direct3D 9 lowers it when it creates its device | 24 bits |
So the server and the client of the same game compute the same expression to different floats. Of 1,000 ordinary pairs of matrices, the affine product at 24 bits is another float than at 53 in 994. The original lives with that (the control block corrects the client); a port that wants its client to predict its server as well as the original's does must reproduce both.
A small case that shows it: a player action's axis. 0x0055bdf0 computes n = (int)clamp(v x 100, +-32767) and returns n x 0.01f in a register; the caller multiplies by 100.0f and truncates.
| Precision | n x 0.01f x 100.0f | Stick fully forward (1.0) is sent as |
|---|---|---|
| 53 bits | n x 0.99999997764..., truncated | 99 |
| 24 bits | rounds back to n | 100 |
How the port does it#
Every ported function that computes in the register takes the precision as an argument and is written over a register type:
// R is the register at the process's precision (X87Precision::Single = 24 bits, Double = 53).
// R::f(x) loads a float; a * b, a + b compute and round to R's precision
// .spill() stores to a float: this is where the original has an fstp to memory
let t = (f(a) * f(b)).spill(); // stored: a float from here on
let u = f(t) + f(c) * f(d); // the product stays in the registerwith_x87!(p, body::<R>(..)) picks the register type for the X87Precision it is given. Where the listing stores, the Rust calls .spill(); where the listing keeps a value on the register stack, the Rust keeps the R. The comparison instructions are reproduced too: fcom followed by test ah, 0x41 treats a NaN in another way than test ah, 5 does, and each compare in the Rust names the form it copies (gt_un, le_un, ...).
The transcendental instructions are a special case. fsin and fpatan give 64-bit results whose last bits depend on the processor; the helicopter rotor's ground-tilt term multiplies one in the register. On x86 hosts the port runs those instructions on the hardware (x87_hw in crates/bf2-physics/src/vehicle_engine.rs), with the precision control set as the original has it.
Where Heroes parts from Battlefield 2#
Heroes was built years later with another compiler setting. Functions that are statement for statement Battlefield 2's store intermediate results that Battlefield 2 keeps in the register:
| Function | Battlefield 2 / 2142 | Heroes 1.126 |
|---|---|---|
| the engine curve's read | 0x006090e0: x x 100 and the blend stay in the register | 0x007d0993, 0x007c6d55, 0x007c6d9c: x x 100 is stored as a float before its whole part is taken, and the blend is stored and answered from the store |
| the wheel's speed step | 0x00698a40: times the reciprocal of the inertia | 0x009588d6: divided by the inertia; the new speed, its magnitude and the product with the sign are each stored |
| the 3x3 and affine matrix products | six of nine sums in another order | 0x0088fc8f, 0x0088f817: every element (a0 b0 + a1 b1) + a2 b2, stored |
| the rigid inverse | 0x0088f55f: its own pairing of the translation's three products |
Try the two forms#
The piece below runs the curve's blend in Battlefield 2's form and in Heroes' form, at both precisions, for the two table entries and the x you choose, and counts over a thousand values of x how often the two forms give another float.
What a store does depends on the process:
- At 24 bits every register result is already rounded to a float's width, so storing it changes nothing: the two forms of a store agree.
- At 53 bits the register holds more than a float does. The store rounds, and the next operation starts from another number: the two forms differ, usually in the last bit, and only for some inputs.
- A changed order of a sum shows at both precisions: on a Heroes client in most products, on a Heroes server where a product nearly cancels (10 of 2,000 such pairs, none of 2,000 ordinary ones).
In the port these are arms inside the same Rust functions, chosen by is_heroes(): EngineCurve::evaluate_heroes, the Heroes arm of wheel_speed_step_p, heroes_sum / mul3x3 / mul_affine in bf2-gameplay tools/math.rs, mul33_p and invert_rigid_p in bf2-physics. No call site changed.
Ghidra's C shows a * b + c for both forms. Whether a product was stored is only in the disassembly (an fstp dword and a reload, or none). Every function on this page was read in the listing to its end before its arm was written, and the rows of its tests come from doing the listing's arithmetic exactly.
How it is tested#
| Check | What it compares | Result |
|---|---|---|
node oracle, 53 bits (0x027F) | 400 cases, 9,600 ticks: the full node state and transform after every tick, ours against the original's function | every tick bit-exact |
node oracle, 24 bits (0x007F) | 200 cases, 4,800 ticks | every tick bit-exact |
| mutation checks | storing one register product early; using the register where the listing stores | each fails the test (the first two in server mode only, as expected) |
| Heroes rows | six pairs each at 24 and 53 bits per product, from the listing's exact arithmetic | tests/fire_comps_bfh126.rs, tests/vehicle_nodes_bfh.rs, tests/wheel_speed_step_bfh126.rs |
The cases vary rotation, velocities (tiny values too), mass, drag, the centre of mass, water depth, the template class and the sleep counter; forces are scaled by 40 in an eighth of the ticks to reach the limits.
Why it matters to a player#
A vehicle is a chaotic system: one last bit in a wheel's speed is a centimetre after a few seconds and another line through a corner after a lap. On the server that only means our server's lap is not the original's. On the client it means a correction, because the client predicts with the same code (the control block). Getting the bits right is what makes a driven vehicle feel calm.
Notes: notes/subsystems/physics-dynamics.md. Code: crates/bf2-physics/src/fpu.rs, crates/bf2-core (X87Precision in fpu.rs).
Where precision is set per test case: the oracle.
In the code#
The crates this page names, each with its line of docs/release/LAYOUT.md (the list is gathered by the wiki's build from the page itself):
| Crate | What it is |
|---|---|
crates/bf2-physics | physics: collision queries, materials, rigid-body integration |
crates/bf2-core | shared types and helpers; which game a process runs |
crates/bf2-gameplay | the behaviour of gameplay objects: weapons, projectiles, soldiers, vehicles |
Files this page names: crates/bf2-physics/src/fpu.rs, crates/bf2-physics/src/vehicle_engine.rs.
docs/dev/vehicles/float-precision.md · built from bf2-rs e3b4f05 · part of the Battlefield 2 / 2142 / Heroes reimplementation. Not affiliated with EA or DICE.