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.

perf(baselib): convert floats to ints with a cast, not the x87 stack

Olcay Seygan18e2d154835e
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5a3ce753c..f969b6cec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -988,6 +988,17 @@ burning base has dozens of things doing the asking. Every effect now knows where
question is answered in one step instead of hundreds — and an effect that dies is no longer hunted
down that same list before it can be cleared away.
+**And every decimal turned into a whole number.** The game does that constantly — a position, a
+health value, a countdown, hundreds of times per unit per turn. It was doing it with a scrap of
+hand-written 1999 assembly language that pushed the number through the processor's old
+floating-point stack, which was the fast way to do it when the game was written. On any processor
+made this century the plain, obvious conversion is a single instruction that does exactly the same
+thing, and because the old one was written inline it also stopped the compiler from improving
+anything near it — and it sits in a file the whole game is built on top of. It is the plain
+conversion now. It is also one less way for two machines in an online match to disagree with each
+other: the old route asked the processor what rounding mode it happened to be in, and that is not
+something a game gets to control.
+
## Sound, video, and getting it to start at all
- **Audio is real**, through the game's own audio library. The sound SDK was stripped from the source
diff --git a/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h b/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h
index 79c1183ae..49174220b 100644
--- a/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h
+++ b/GeneralsMD/Code/Libraries/Include/Lib/BaseType.h
@@ -243,15 +243,42 @@ __forceinline float fast_float_ceil(float f)
}
//-------------------------------------------------------------------------------------------------
-#define REAL_TO_INT(x) ((Int)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_UNSIGNEDINT(x) ((UnsignedInt)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_SHORT(x) ((Short)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_UNSIGNEDSHORT(x) ((UnsignedShort)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_BYTE(x) ((Byte)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_UNSIGNEDBYTE(x) ((UnsignedByte)(fast_float2long_round(fast_float_trunc(x))))
-#define REAL_TO_CHAR(x) ((Char)(fast_float2long_round(fast_float_trunc(x))))
+
+/* Float to integer, and it is a plain cast.
+ *
+ * EA wrote these as fast_float2long_round(fast_float_trunc(x)): mask the fractional mantissa bits
+ * off with a block of inline assembly, then push the result through the x87 stack with fld/fistp.
+ * In 1999 that beat the compiler, which had to spill the FPU control word twice to honour C's
+ * round-toward-zero rule. On a machine with SSE2 - which is every machine that can run this, and
+ * which is what MSVC targets by default for x86 - a cast to Int is a single cvttss2si that already
+ * truncates toward zero, and the two-step version is a memory round trip, an x87 stack push and an
+ * x87 stack pop to reach the same answer.
+ *
+ * The cost is not just the instructions. These macros sit in a header the whole game includes, and
+ * an __asm block in an inlined function stops MSVC optimizing across it: every expression in this
+ * file's neighbourhood - and REAL_TO_INT alone has 235 call sites, most of them in the simulation -
+ * was compiled with one hand tied. It is also where the fast_float_trunc register clobber lived,
+ * the one that ended every run at the main menu until the "xor ebx,ebx" was found.
+ *
+ * The result is identical for every value that fits in the destination, and the intermediate cast
+ * to Int is kept where the destination is narrower so the two-step narrowing is exactly the one the
+ * long-returning original did. Better than identical in one respect: fast_float2long_round rounds
+ * by the FPU's current mode, which the comment on it warns "tends to be left in unpredictable modes
+ * by various system bits of code" - it was only safe here because fast_float_trunc had already made
+ * the value integral. cvttss2si has no mode to be left in. The trunc/floor/ceil helpers below are
+ * untouched; they still have callers that want a Real back.
+ *
+ * Pinned by real_to_int_agrees_with_the_assembly_it_replaced in test_gameengine.cpp. */
+
+#define REAL_TO_INT(x) ((Int)(x))
+#define REAL_TO_UNSIGNEDINT(x) ((UnsignedInt)(Int)(x))
+#define REAL_TO_SHORT(x) ((Short)(Int)(x))
+#define REAL_TO_UNSIGNEDSHORT(x) ((UnsignedShort)(Int)(x))
+#define REAL_TO_BYTE(x) ((Byte)(Int)(x))
+#define REAL_TO_UNSIGNEDBYTE(x) ((UnsignedByte)(Int)(x))
+#define REAL_TO_CHAR(x) ((Char)(Int)(x))
#define DOUBLE_TO_REAL(x) ((Real) (x))
-#define DOUBLE_TO_INT(x) ((Int) (fast_float2long_round(fast_float_trunc(x))))
+#define DOUBLE_TO_INT(x) ((Int)(x))
#define INT_TO_REAL(x) ((Real) (x))
// once we've ceiled/floored, trunc and round are identical, and currently, round is faster... (srj)
diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cpp
index 4b07bdd64..28a62c2d8 100644
--- a/GeneralsMD/Code/Tests/test_gameengine.cpp
+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp
@@ -421,6 +421,100 @@ TEST(fast_float_trunc_leaves_ebx_alone)
CHECK_NEAR( fast_float_trunc( -3.75f ), -3.0f, 0.0001f );
}
+/* REAL_TO_INT and its siblings used to be fast_float2long_round(fast_float_trunc(x)) - an inline
+ assembly mantissa mask followed by an x87 fld/fistp - and are now a plain cast, which on any
+ SSE2 target is the single cvttss2si instruction that does both. The claim is that nothing
+ changed except the speed, so the witness runs the two side by side. The assembly helpers are
+ still in BaseType.h (FAST_REAL_TRUNC and the floor/ceil macros use them), so this is the real
+ old expression, not a re-implementation of it. */
+
+static Int oldRealToInt( Real f )
+{
+ return (Int)(fast_float2long_round(fast_float_trunc(f)));
+}
+
+TEST(real_to_int_agrees_with_the_assembly_it_replaced)
+{
+ /* The values that decide it: either side of zero, either side of one, exact halves, exact
+ integers, and the ends of the range a 32 bit signed conversion is defined over. Anything
+ whose magnitude reaches 2^31 is out of range for both spellings and both give INT_MIN, so
+ the sweep stops below it. */
+ static const Real kInteresting[] =
+ {
+ 0.0f, -0.0f, 0.25f, -0.25f, 0.5f, -0.5f, 0.75f, -0.75f,
+ 1.0f, -1.0f, 1.5f, -1.5f, 1.9999999f, -1.9999999f,
+ 2.5f, -2.5f, 3.75f, -3.75f, 30.0f, -30.0f,
+ 127.5f, -127.5f, 128.0f, -128.0f, 255.9f, -255.9f, 256.0f,
+ 32767.9f, -32768.0f, 65535.9f, 65536.0f,
+ 1.0e6f, -1.0e6f, 16777216.0f, -16777216.0f, 16777217.0f,
+ 1.0e9f, -1.0e9f, 2147483520.0f, -2147483520.0f,
+ 1.1754944e-38f, -1.1754944e-38f // smallest normal float, either sign
+ };
+
+ for( Int i = 0; i < (Int)(sizeof(kInteresting)/sizeof(kInteresting[0])); ++i )
+ {
+ Real f = kInteresting[i];
+ CHECK_EQ( REAL_TO_INT( f ), oldRealToInt( f ) );
+
+ // the narrowing spellings truncate to Int first, exactly as the long-returning original did
+ CHECK_EQ( (Int)REAL_TO_SHORT( f ), (Int)(Short)oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_UNSIGNEDSHORT( f ), (Int)(UnsignedShort)oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_BYTE( f ), (Int)(Byte)oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_UNSIGNEDBYTE( f ), (Int)(UnsignedByte)oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_CHAR( f ), (Int)(Char)oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_UNSIGNEDINT( f ), (Int)(UnsignedInt)oldRealToInt( f ) );
+ }
+
+ /* And a sweep, because a hand-picked list is a hand-picked list. The generator is a plain LCG
+ over the float's bit pattern, rejected down to the range the conversion is defined over, so
+ the same 20000 values are tested on every machine that runs this. */
+ UnsignedInt bits = 0x12345678;
+ Int tested = 0;
+ for( Int n = 0; n < 400000 && tested < 20000; ++n )
+ {
+ bits = bits * 1664525u + 1013904223u;
+
+ Real f;
+ memcpy( &f, &bits, sizeof(f) );
+
+ // NaNs, infinities and everything at or past 2^31 are out of range for both spellings
+ if( !(f == f) || f >= 2147483520.0f || f <= -2147483520.0f )
+ continue;
+
+ ++tested;
+ CHECK_EQ( REAL_TO_INT( f ), oldRealToInt( f ) );
+ CHECK_EQ( (Int)REAL_TO_UNSIGNEDBYTE( f ), (Int)(UnsignedByte)oldRealToInt( f ) );
+ }
+
+ CHECK_EQ( tested, 20000 );
+}
+
+TEST(real_to_int_does_not_care_what_rounding_mode_it_is_called_in)
+{
+ /* The old spelling's second half was an fld/fistp, and fistp rounds by the FPU's current mode -
+ its own comment says the mode "tends to be left in unpredictable modes by various system bits
+ of code". It was only safe because the mantissa mask ran first. A cast has no mode at all,
+ which is one less way for two machines to disagree about the same simulation. */
+ const UnsignedInt callersMode = _controlfp( 0, 0 );
+
+ _controlfp( _RC_CHOP, _MCW_RC );
+ const Int chop = REAL_TO_INT( -3.75f ) * 1000 + REAL_TO_INT( 3.75f );
+
+ _controlfp( _RC_UP, _MCW_RC );
+ CHECK_EQ( REAL_TO_INT( -3.75f ) * 1000 + REAL_TO_INT( 3.75f ), chop );
+
+ _controlfp( _RC_DOWN, _MCW_RC );
+ CHECK_EQ( REAL_TO_INT( -3.75f ) * 1000 + REAL_TO_INT( 3.75f ), chop );
+
+ _controlfp( _RC_NEAR, _MCW_RC );
+ CHECK_EQ( REAL_TO_INT( -3.75f ) * 1000 + REAL_TO_INT( 3.75f ), chop );
+
+ // truncation toward zero, in every one of them
+ CHECK_EQ( chop, -3 * 1000 + 3 );
+
+ _controlfp( callersMode, _MCW_PC | _MCW_RC );
+}
+
/* The length is a parameter and not a strlen() call on purpose: anything the compiler
emits between the two blocks below is free to use these registers itself, so the
witness only stays honest while the call is the only thing in between. */