BF2.US bf2-rsdeveloper wiki Parity Discord bf2.us
On this page

Float precision: 24 bits, 53 bits, and a store

BF22142Heroes

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.

v = t[i] x (1 - f) + t[i + 1] x f (the engine curve's blend) Battlefield 2 / 2142 0x006090e0 Heroes 1.126 0x007c6d9c t[i] x (1 - f) register t[i] x (1 - f) register t[i+1] x f register t[i+1] x f register sum register sum stored as float use register value use the stored float How wide is the register's result? 24 bits a float in memory 24 bits register, precision control 24 bits: a client under Direct3D 9 53 bits register, precision control 53 bits: a dedicated server 64 bits register, full width 24 bits: a store changes nothing, the two forms agree. 53 bits: the store rounds, the last bit can differ.
One statement, two compilers. Battlefield 2 keeps the sum in the register and uses it; Heroes stores it as a float first. The bars: how many bits of the result survive.

The two precisions#

Processx87 control wordRegister results are rounded to
a dedicated server0x027F, the C runtime's default53 bits
a client0x007F: Direct3D 9 lowers it when it creates its device24 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.

Precisionn x 0.01f x 100.0fStick fully forward (1.0) is sent as
53 bitsn x 0.99999997764..., truncated99
24 bitsrounds back to n100

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:

crates/bf2-physics/src/fpu.rs (the idea)
// 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 register

with_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:

FunctionBattlefield 2 / 2142Heroes 1.126
the engine curve's read0x006090e0: x x 100 and the blend stay in the register0x007d0993, 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 step0x00698a40: times the reciprocal of the inertia0x009588d6: divided by the inertia; the new speed, its magnitude and the product with the sign are each stored
the 3x3 and affine matrix productssix of nine sums in another order0x0088fc8f, 0x0088f817: every element (a0 b0 + a1 b1) + a2 b2, stored
the rigid inverse0x0088f55f: 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.

A decompiler does not show this

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#

CheckWhat it comparesResult
node oracle, 53 bits (0x027F)400 cases, 9,600 ticks: the full node state and transform after every tick, ours against the original's functionevery tick bit-exact
node oracle, 24 bits (0x007F)200 cases, 4,800 ticksevery tick bit-exact
mutation checksstoring one register product early; using the register where the listing storeseach fails the test (the first two in server mode only, as expected)
Heroes rowssix pairs each at 24 and 53 bits per product, from the listing's exact arithmetictests/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):

CrateWhat it is
crates/bf2-physicsphysics: collision queries, materials, rigid-body integration
crates/bf2-coreshared types and helpers; which game a process runs
crates/bf2-gameplaythe 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.

Source: 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.