Skip to content

Commit 1c5613d

Browse files
committed
fix: Replace sqrt with deterministic Sqrt in GameLogic to fix x87 FPU lockstep desyncs
1 parent 793d2e7 commit 1c5613d

26 files changed

Lines changed: 3720 additions & 3714 deletions

Core/Libraries/Include/Lib/trig.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ Real Tan(Real);
2929
Real ACos(Real);
3030
Real ASin(Real x);
3131
Real Atan2(Real y, Real x);
32+
Real Sqrt(Real x);

GeneralsMD/Code/GameEngine/Source/Common/System/Trig.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Real Atan2(Real y, Real x)
7777
return WWMath::Atan2(y, x);
7878
}
7979

80+
Real Sqrt(Real x)
81+
{
82+
return WWMath::Sqrt(x);
83+
}
84+
8085
#ifdef REGENERATE_TRIG_TABLES
8186
void initTrig()
8287
{

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ Object *AI::findClosestEnemy( const Object *me, Real range, UnsignedInt qualifie
719719
}
720720

721721
Real distSqr = ThePartitionManager->getDistanceSquared(me, theEnemy, FROM_BOUNDINGSPHERE_2D);
722-
Real dist = sqrt(distSqr);
722+
Real dist = Sqrt(distSqr);
723723
Int modifier = dist/getAiData()->m_attackPriorityDistanceModifier;
724724
Int modPriority = curPriority-modifier;
725725
if (modPriority < 1)

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIPlayer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ Object *AIPlayer::buildStructureWithDozer(const ThingTemplate *bldgPlan, BuildLi
647647
dx = dozer->getPosition()->x - pos.x;
648648
dy = dozer->getPosition()->y - pos.y;
649649

650-
Int count = sqrt(dx*dx+dy*dy)/(PATHFIND_CELL_SIZE_F/2);
650+
Int count = Sqrt(dx*dx+dy*dy)/(PATHFIND_CELL_SIZE_F/2);
651651
if (count<2) count = 2;
652652
Int i;
653653
color.green = 1;
@@ -1350,7 +1350,7 @@ Int AIPlayer::getPlayerSuperweaponValue(Coord3D *center, Int playerNdx, Real rad
13501350
Real dy = center->y - pos.y;
13511351
if (dx*dx+dy*dy<radSqr)
13521352
{
1353-
Real dist = sqrt(dx*dx+dy*dy);
1353+
Real dist = Sqrt(dx*dx+dy*dy);
13541354
Real factor = 1.0f - (dist/(2*radius)); // 1.0 in center, 0.5 on edges.
13551355
Real value = pObj->getTemplate()->calcCostToBuild(pPlayer);
13561356
if (pObj->isKindOf(KINDOF_COMMANDCENTER))
@@ -3136,7 +3136,7 @@ void AIPlayer::computeCenterAndRadiusOfBase(Coord3D *center, Real *radius)
31363136
Real radSqr = dx*dx+dy*dy;
31373137
if (radSqr>maxRadSqr) maxRadSqr=radSqr;
31383138
}
3139-
*radius = sqrt(maxRadSqr);
3139+
*radius = Sqrt(maxRadSqr);
31403140
}
31413141

31423142
//----------------------------------------------------------------------------------------------------------

GeneralsMD/Code/GameEngine/Source/GameLogic/AI/AIStates.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3678,7 +3678,7 @@ StateReturnType AIAttackMoveToState::update()
36783678
if (distSqr < sqr(ATTACK_CLOSE_ENOUGH_CELLS*PATHFIND_CELL_SIZE_F)) {
36793679
return ret;
36803680
}
3681-
DEBUG_LOG(("AIAttackMoveToState::update Distance from goal %f, retrying.", sqrt(distSqr)));
3681+
DEBUG_LOG(("AIAttackMoveToState::update Distance from goal %f, retrying.", Sqrt(distSqr)));
36823682

36833683
ret = STATE_CONTINUE;
36843684
m_retryCount--;

GeneralsMD/Code/GameEngine/Source/GameLogic/Map/PolygonTrigger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void PolygonTrigger::updateBounds() const
286286
Real halfWidth = (m_bounds.hi.x - m_bounds.lo.x) / 2.0f;
287287
Real halfHeight = (m_bounds.hi.y + m_bounds.lo.y) / 2.0f;
288288

289-
m_radius = sqrt(halfHeight*halfHeight + halfWidth*halfWidth);
289+
m_radius = Sqrt(halfHeight*halfHeight + halfWidth*halfWidth);
290290
}
291291

292292

GeneralsMD/Code/GameEngine/Source/GameLogic/Map/TerrainLogic.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2369,7 +2369,7 @@ void TerrainLogic::setWaterHeight( const WaterHandle *water, Real height, Real d
23692369
center.z = 0.0f; // irrelevant
23702370

23712371
// the max radius to scan around us is the diagonal of the bounding region
2372-
Real maxDist = sqrt( affectedRegion.width() * affectedRegion.width() +
2372+
Real maxDist = Sqrt( affectedRegion.width() * affectedRegion.width() +
23732373
affectedRegion.height() * affectedRegion.height() );
23742374

23752375
// scan the objects in the area of the water affected
@@ -2879,7 +2879,7 @@ void TerrainLogic::createCraterInTerrain(Object *obj)
28792879
deltaX = ( i * MAP_XY_FACTOR ) - pos->x;
28802880
deltaY = ( j * MAP_XY_FACTOR ) - pos->y;
28812881

2882-
Real distance = sqrt( sqr( deltaX ) + sqr( deltaY ) );
2882+
Real distance = Sqrt( sqr( deltaX ) + sqr( deltaY ) );
28832883

28842884
if ( distance < radius ) //inside circle
28852885
{

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/DumbProjectileBehavior.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ static Bool calcTrajectory(
176176

177177
// calculating the pitch requires a bit more effort.
178178
Real horizDistSqr = sqr(dx) + sqr(dy);
179-
Real horizDist = sqrt(horizDistSqr);
179+
Real horizDist = Sqrt(horizDistSqr);
180180

181181
// calc the two possible pitches that will cover the given horizontal range.
182182
// (this is actually only true if dz==0, but is a good first guess)
@@ -290,7 +290,7 @@ static Bool calcTrajectory(
290290
#endif
291291

292292
vx = velocity*cosPitches[preferred];
293-
Real actualRange = (vx*(vz + sqrt(root)))/gravity;
293+
Real actualRange = (vx*(vz + Sqrt(root)))/gravity;
294294
const Real CLOSE_ENOUGH_RANGE = 5.0f;
295295
if (tooClose || (actualRange < horizDist - CLOSE_ENOUGH_RANGE))
296296
{
@@ -369,7 +369,7 @@ void DumbProjectileBehavior::projectileFireAtObjectOrPosition( const Object *vic
369369
// Some weapons want to scale their start speed to the range
370370
Real minRange = detWeap->getMinimumAttackRange();
371371
Real maxRange = detWeap->getUnmodifiedAttackRange();
372-
Real range = sqrt(ThePartitionManager->getDistanceSquared( projectile, &victimPosToUse, FROM_CENTER_2D ) );
372+
Real range = Sqrt(ThePartitionManager->getDistanceSquared( projectile, &victimPosToUse, FROM_CENTER_2D ) );
373373
Real rangeRatio = (range - minRange) / (maxRange - minRange);
374374
m_flightPathSpeed = (rangeRatio * (weaponSpeed - minWeaponSpeed)) + minWeaponSpeed;
375375
}
@@ -611,7 +611,7 @@ UpdateSleepTime DumbProjectileBehavior::update()
611611
Real distVictimMovedSqr = sqr(delta.x) + sqr(delta.y) + sqr(delta.z);
612612
if (distVictimMovedSqr > 0.1f)
613613
{
614-
Real distVictimMoved = sqrtf(distVictimMovedSqr);
614+
Real distVictimMoved = Sqrt(distVictimMovedSqr);
615615
if (distVictimMoved > d->m_flightPathAdjustDistPerFrame)
616616
distVictimMoved = d->m_flightPathAdjustDistPerFrame;
617617
delta.normalize();

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/GenerateMinefieldBehavior.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void GenerateMinefieldBehavior::placeMinesAlongLine(const Coord3D& posStart, con
248248

249249
Real dx = posEnd.x - posStart.x;
250250
Real dy = posEnd.y - posStart.y;
251-
Real len = sqrt(sqr(dx) + sqr(dy));
251+
Real len = Sqrt(sqr(dx) + sqr(dy));
252252
Real mineRadius = mineTemplate->getTemplateGeometryInfo().getBoundingCircleRadius();
253253
Real mineDiameter = mineRadius * 2.0f;
254254
Real mineJitter = mineRadius*d->m_randomJitter;

GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Behavior/MinefieldBehavior.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void MinefieldBehavior::setScootParms(const Coord3D& start, const Coord3D& end)
570570
if (start.z > endOnGround.z)
571571
{
572572
// figure out how long it will take to fall, and replace scoot time with that
573-
UnsignedInt fallingTime = REAL_TO_INT_CEIL(sqrtf(2.0f * (start.z - endOnGround.z) / fabs(TheGlobalData->m_gravity)));
573+
UnsignedInt fallingTime = REAL_TO_INT_CEIL(Sqrt(2.0f * (start.z - endOnGround.z) / fabs(TheGlobalData->m_gravity)));
574574
// we can scoot after we land, but don't want to stop scooting before we land
575575
if (scootFromStartingPointTime < fallingTime)
576576
scootFromStartingPointTime = fallingTime;
@@ -588,7 +588,7 @@ void MinefieldBehavior::setScootParms(const Coord3D& start, const Coord3D& end)
588588
Real dx = endOnGround.x - start.x;
589589
Real dy = endOnGround.y - start.y;
590590
Real dz = endOnGround.z - start.z;
591-
Real dist = sqrt(sqr(dx) + sqr(dy));
591+
Real dist = Sqrt(sqr(dx) + sqr(dy));
592592
if (dist <= 0.1f && fabs(dz) <= 0.1f)
593593
{
594594
obj->setPosition(&endOnGround);

0 commit comments

Comments
 (0)