Skip to content

Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.

fix(gamelogic): read the control word, not the status word, when pinning the FPU

Olcay Seygan1aa54c883772
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fe4bbd996..d62790c2e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -205,6 +205,12 @@ map. Ctrl+A is still everything at once.
file and the two simulations part company a few minutes in, with nothing on screen to say why. The
check for this was written and then switched off before release; it is back on, so a mismatched
join is turned away in the lobby with a reason, instead of becoming a ruined game.
+- **The maths is nailed down to the same setting on every machine.** Two computers only agree on
+ where a shell lands if their processors are rounding numbers the same way, and that setting is a
+ shared one: a graphics or sound driver can change it out from under the game and never put it
+ back. The game already reset it every frame, but the reset read the wrong register to decide what
+ to write. It now reads the right one, and there are tests that break the machine's maths on
+ purpose and prove the reset puts it back.
## Sharper textures, for free
diff --git a/GeneralsMD/Code/GameEngine/Include/GameLogic/FPUControl.h b/GeneralsMD/Code/GameEngine/Include/GameLogic/FPUControl.h
index 3ffd032d1..278994992 100644
--- a/GeneralsMD/Code/GameEngine/Include/GameLogic/FPUControl.h
+++ b/GeneralsMD/Code/GameEngine/Include/GameLogic/FPUControl.h
@@ -32,6 +32,8 @@
#ifndef __FPUCONTROL_H__
#define __FPUCONTROL_H__
+#include "Lib/BaseType.h"
+
/**
* setFPMode sets the FPU internal precision and rounding mode. As DirectX is not guaranteed to
* leave the FPU in a good state, we must call this at the start of GameLogic::update() and
@@ -39,4 +41,10 @@
*/
void setFPMode( void );
+/** The precision and rounding fields of the FPU control word, as they are right now. */
+UnsignedInt getFPMode( void );
+
+/** What getFPMode() must read back after setFPMode(): 24-bit precision, round to nearest. */
+UnsignedInt expectedFPMode( void );
+
#endif // __FPUCONTROL_H__
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/FPUControl.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/FPUControl.cpp
new file mode 100644
index 000000000..d03128460
--- /dev/null
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/FPUControl.cpp
@@ -0,0 +1,68 @@
+/*
+** Command & Conquer Generals Zero Hour(tm)
+** Copyright 2025 Electronic Arts Inc.
+**
+** This program is free software: you can redistribute it and/or modify
+** it under the terms of the GNU General Public License as published by
+** the Free Software Foundation, either version 3 of the License, or
+** (at your option) any later version.
+**
+** This program is distributed in the hope that it will be useful,
+** but WITHOUT ANY WARRANTY; without even the implied warranty of
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+** GNU General Public License for more details.
+**
+** You should have received a copy of the GNU General Public License
+** along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+// FILE: FPUControl.cpp ///////////////////////////////////////////////////////////////////////////
+// Desc: Pinning the FPU so two machines compute the same numbers.
+////////////////////////////////////////////////////////////////////////////////////////////////////
+
+#include "PreRTS.h"
+#include "GameLogic/FPUControl.h"
+#include <float.h>
+
+/* This lives in its own translation unit rather than in GameLogic.cpp, where EA had it, so that a
+ test can call it without linking the whole of the game logic behind it. */
+
+void setFPMode( void )
+{
+ /* Every float the simulation computes has to come out the same on both machines, and the x87
+ control word decides that: precision and rounding mode. Nothing in the process guarantees
+ it stays put - Direct3D sets it when it creates a device without D3DCREATE_FPU_PRESERVE,
+ audio and video drivers have historically set it on their own threads, and any DLL loaded
+ into the process may set it once and never restore it. So it is not set once at startup;
+ GameLogic::update re-asserts it at the top of every logic frame, and so does anything that
+ hands control to the renderer inside a logic loop (the load screens, INI parsing).
+
+ _fpreset() first, because it puts the whole word - exception masks included - into a known
+ state rather than only the two fields written below. */
+ _fpreset();
+
+ /* Rounding to nearest, and 24-bit (single) precision. The precision could be anything as long
+ as it is the same everywhere; 24 bits is what Direct3D leaves behind anyway, so asking for
+ it costs nothing and removes one way for the two to disagree.
+
+ EA read the current word with _statusfp(), which returns the *status* word - the sticky
+ exception flags - not the control word. It happened to be harmless, because the mask below
+ keeps everything except the precision and rounding fields and no status flag lands in
+ either. _controlfp(0, 0) is what they meant: it reads the control word without writing. */
+ UnsignedInt curVal = _controlfp( 0, 0 );
+ UnsignedInt newVal = curVal;
+ newVal = (newVal & ~_MCW_RC) | (_RC_NEAR & _MCW_RC);
+ newVal = (newVal & ~_MCW_PC) | (_PC_24 & _MCW_PC);
+
+ _controlfp( newVal, _MCW_PC | _MCW_RC );
+}
+
+UnsignedInt getFPMode( void )
+{
+ return _controlfp( 0, 0 ) & (_MCW_PC | _MCW_RC);
+}
+
+UnsignedInt expectedFPMode( void )
+{
+ return (_PC_24 & _MCW_PC) | (_RC_NEAR & _MCW_RC);
+}
diff --git a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
index aaada1ce9..6c0facea6 100644
--- a/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
+++ b/GeneralsMD/Code/GameEngine/Source/GameLogic/System/GameLogic.cpp
@@ -192,29 +192,9 @@ static Waypoint * findNamedWaypoint(AsciiString name)
return NULL;
}
-// ------------------------------------------------------------------------------------------------
-// ------------------------------------------------------------------------------------------------
-void setFPMode( void )
-{
- // Set floating point round mode to CHOP, which only comes
- // into play when precision is exceeded. This is necessary
- // for the fast float to int routines used elsewhere in the
- // system.
- //
- // Also set floating point precision to low. It could be
- // anything as long as it is consistent, really, but this
- // is in the (vain?) hope of any slight speed boost.
- //
- _fpreset();
-
- UnsignedInt curVal = _statusfp();
- UnsignedInt newVal = curVal;
- newVal = (newVal & ~_MCW_RC) | (_RC_NEAR & _MCW_RC);
- //newVal = (newVal & ~_MCW_RC) | (_RC_CHOP & _MCW_RC);
- newVal = (newVal & ~_MCW_PC) | (_PC_24 & _MCW_PC);
+/* setFPMode() moved to its own translation unit, Source/GameLogic/System/FPUControl.cpp, so a
+ test can call it without linking the whole of GameLogic. */
- _controlfp(newVal, _MCW_PC | _MCW_RC);
-}
// ------------------------------------------------------------------------------------------------
/** GameLogic class constructor */
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index a43ed9429..94cddd070 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -30,6 +30,8 @@
#include "GameNetwork/Connection.h"
#include "GameLogic/CRCSnapshotRing.h"
#include "GameNetwork/GameDataMatch.h"
+#include "GameLogic/FPUControl.h"
+#include <float.h>
#include "GameClient/Water.h"
#include "GameLogic/Module/PhysicsUpdate.h"
#include "GameClient/ParticleSys.h"
@@ -2081,3 +2083,58 @@ TEST(gamedatamatch_refuses_a_machine_that_reports_no_data_at_all)
CHECK_STR( gameDataMatchName( GAMEDATA_INI_DIFFERS ), "different INI set" );
CHECK_STR( gameDataMatchName( GAMEDATA_UNKNOWN ), "data not reported" );
}
+
+/* --------------------------------------------------------------------------------------------
+ Two machines only compute the same floats if the FPU control word says the same thing on both.
+ Nothing in the process guarantees that - Direct3D sets it when it creates a device, and any DLL
+ in the process can set it and never put it back - so GameLogic::update re-asserts it at the top
+ of every logic frame. These pin what "asserts it" means. */
+
+TEST(setfpmode_pins_the_control_word_from_whatever_it_finds)
+{
+ UnsignedInt entry = _controlfp( 0, 0 ); // leave the process the way we found it
+
+ // the mode the simulation runs in: 24-bit precision, round to nearest
+ setFPMode();
+ CHECK_EQ( getFPMode(), expectedFPMode() );
+ CHECK_EQ( getFPMode() & _MCW_PC, (UnsignedInt)(_PC_24 & _MCW_PC) );
+ CHECK_EQ( getFPMode() & _MCW_RC, (UnsignedInt)(_RC_NEAR & _MCW_RC) );
+
+ // what a driver that grabbed the FPU and never gave it back looks like
+ _controlfp( _PC_64 | _RC_CHOP, _MCW_PC | _MCW_RC );
+ CHECK_NE( getFPMode(), expectedFPMode() );
+ setFPMode();
+ CHECK_EQ( getFPMode(), expectedFPMode() );
+
+ // and the other direction, so this is not just "setFPMode lowers the precision"
+ _controlfp( _PC_53 | _RC_UP, _MCW_PC | _MCW_RC );
+ CHECK_NE( getFPMode(), expectedFPMode() );
+ setFPMode();
+ CHECK_EQ( getFPMode(), expectedFPMode() );
+
+ // it is idempotent - a second logic frame does not move it
+ setFPMode();
+ CHECK_EQ( getFPMode(), expectedFPMode() );
+
+ _controlfp( entry, _MCW_PC | _MCW_RC );
+}
+
+TEST(setfpmode_leaves_the_exception_mask_in_a_known_state)
+{
+ UnsignedInt entry = _controlfp( 0, 0 );
+
+ /* setFPMode writes only the precision and rounding fields, but it calls _fpreset() first, so
+ the rest of the word - the exception masks above all - lands in the same known state on
+ every machine rather than wherever the last DLL left it. An unmasked invalid-operation
+ exception on one machine and a masked one on another is a crash on one side and a quiet NaN
+ on the other. */
+ _controlfp( 0, _MCW_EM ); // unmask everything: the state a debugger or a bad DLL can leave
+ setFPMode();
+ CHECK_EQ( _controlfp( 0, 0 ) & _MCW_EM, (UnsignedInt)_MCW_EM );
+
+ // and the x87 stack is reset with it, so an __asm block that pushed and forgot to pop is
+ // not carried into the next logic frame
+ CHECK_EQ( getFPMode(), expectedFPMode() );
+
+ _controlfp( entry, _MCW_PC | _MCW_RC | _MCW_EM );
+}