Skip to content

refactor: Replace _isnan and _finite with std::isnan and std::isfinite - #3686

Merged
xezon merged 6 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/isnan-isfinite
Nov 19, 2026
Merged

xezon merged 6 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/isnan-isfinite

Conversation

@CryoTheRenegade

@CryoTheRenegade CryoTheRenegade commented Nov 14, 2026 •

Copy link
Copy Markdown

The NaN checks call _isnan and _finite, which are MSVC-only CRT functions from <float.h>. This PR replaces them with the standard std::isnan and std::isfinite, in the same way #3532 and #3652 added standard functions for VC6 through the adapters.

+Dependencies/Utility/Utility/cmath_adapter.h
+  VC6: std::isnan, std::isfinite      // float and double overloads that call _isnan / _finite from <float.h>
+  modern compilers: #include <cmath>
 Core/GameEngine/Include/Precompiled/PreRTS.h
+  #include <Utility/cmath_adapter.h>  // covers the GameEngine call sites
 Core/Libraries/Source/WWVegas/WW3D2/motchan.cpp, sortingrenderer.cpp, streakRender.cpp
+  #include <Utility/cmath_adapter.h>  // each WW3D2 call site includes it, as these files do for bit_adapter.h
-if (_isnan(force->x) || _isnan(force->y) || _isnan(force->z))
+if (std::isnan(force->x) || std::isnan(force->y) || std::isnan(force->z))
  • Adapter: it is named after <cmath>, the header it stands in for, like bit_adapter.h for <bit>. Neither VC6 nor STLport 4.5.3 declares isnan or isfinite. The header comment lists how the VC6 functions differ: they have only float and double overloads, and a float is converted to double before the check. That conversion keeps NaN and infinity, so the result is the same.
  • Call sites: 103 calls on 38 lines in 14 files:
    • PhysicsUpdate.cpp, Thing.cpp, AIUpdate.cpp, Object.cpp and Drawable.cpp in both games.
    • GameCommon.cpp, motchan.cpp, sortingrenderer.cpp and streakRender.cpp in Core.
    • 18 of the calls are in 6 commented-out asserts. They are replaced too, so no _isnan is left.
  • Types: every argument is a float, and every result is used as a condition, never as a number. The return type changing from int to bool therefore changes nothing.
  • How the names were found before: the call sites got them from <float.h>, through PreRTS.h and wwmath.h. There was no compat shim to remove.
  • WWMath::Is_Valid_Float and Is_Valid_Double (from review): they test the exponent bits by hand and read the value through an unsigned long*, which breaks the strict aliasing rule. They now read the bits with std::bit_cast. std::isfinite would also do, but wwmath.h would need <cmath>, and MSVC's <cmath> adds float overloads of sin and fabs. On ClangCL that turns double math into float math in mapper.cpp, dazzle.cpp and projector.cpp, which do not include <cmath> today. Release code has no callers; only a WWASSERT in animobj.cpp uses them.

Evidence

  • Disassembly: ClangCL and VC6 Release objects of all changed sources, for both games, compared function by function against main.

    Function ClangCL VC6
    normalizeAngle cvtss2sd + call __imp___isnan becomes an inline ucomiss x, x; jp (43 → 35 instructions) unchanged
    Object::reactToTransformChange the same for 3 checks (131 → 115) unchanged
    PhysicsBehavior::applyForce the same for 3 checks (91 → 80) unchanged
    PhysicsBehavior::update the same for 3 checks; in Zero Hour 4 dtor$ cleanup funclets change only their frame offsets unchanged
    PhysicsBehavior::applyRandomRotation (Zero Hour) the __LINE__ arguments to GameLogicRandomValueReal go up by 1, because a brace moved to its own line above the same
    Object::updateObjValuesFromMapProperties (Generals) unchanged two independent stack loads swap order; it has no NaN check, and the diff persists without the brace commit
    all other functions in the 18 objects unchanged unchanged

    ucomiss x, x sets the parity flag exactly when x is NaN, so each check returns the same value as before. All other float arithmetic in these functions is the same as main. The __LINE__ argument of GetGameLogicRandomValueReal is used only in its DEBUG_LOG. Apart from the two rows above, VC6 generates identical code in every object.

  • Whole build: every ClangCL and VC6 Release object of both games, built from the PR base and from this branch, compared the same way.

    • ClangCL differs only in the functions above and in the __TIME__ string of WinMain. So adding <cmath> to PreRTS.h changes nothing else, because the GameEngine files already got it through the STL headers.
    • VC6 has no float arithmetic or call target differences. Its remaining differences are anonymous namespace hashes and block order, which also differ in BinkLoader.cpp and DbgHelpLoader.cpp, which include none of the changed files.

Claude Opus 5.5 was used to aid in implementation and disassembly

Council review

Rounds: 2

Delegate Round 1 Round 2
Claude Opus 5.5 9/10 9.5/10
GPT-6.1 Sol 9/10 10/10
Gemini 4 Carbon 9/10 no score

@coderabbitai

coderabbitai Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The change adds a compatibility header for standard NaN and finite checks. It replaces platform-specific checks in core engine, WW3D2, Generals, and GeneralsMD code. Existing validation responses remain unchanged.

Changes

Math compatibility and validation

Layer / File(s) Summary
Add and wire the math compatibility header
Dependencies/Utility/Utility/cmath_adapter.h, Dependencies/Utility/CMakeLists.txt, Core/GameEngine/Include/Precompiled/PreRTS.h, Core/Libraries/Source/WWVegas/WWMath/wwmath.h
The new header provides std::isnan and std::isfinite overloads for MSVC versions below 1300 and includes <cmath> for other compiler configurations. The utility build list and engine headers include the adapter.
Update core engine math checks
Core/GameEngine/Source/Common/System/GameCommon.cpp, Core/Libraries/Source/WWVegas/WW3D2/motchan.cpp, Core/Libraries/Source/WWVegas/WW3D2/sortingrenderer.cpp, Core/Libraries/Source/WWVegas/WW3D2/streakRender.cpp
Angle handling and WW3D2 validation use std::isnan and std::isfinite instead of platform-specific functions. Existing filtering, assertions, and diagnostics remain unchanged.
Update game position and physics checks
Generals/Code/GameEngine/Source/Common/Thing/Thing.cpp, Generals/Code/GameEngine/Source/GameClient/Drawable.cpp, Generals/Code/GameEngine/Source/GameLogic/Object/Object.cpp, Generals/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp, Generals/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp, GeneralsMD/Code/GameEngine/Source/Common/Thing/Thing.cpp, GeneralsMD/Code/GameEngine/Source/GameClient/Drawable.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Object.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/PhysicsUpdate.cpp
Position, locomotor, and physics checks in both game variants use std::isnan. Existing assertions, early returns, and object-handling responses remain unchanged.

Estimated code review effort: 2 (Simple) | ~15 minutes

Change: Refactor

Suggested reviewers: xezon

Merge Risk: Minimal · up to 910c3

This refactor swaps platform-specific NaN and finite checks for standard calls, and a compatibility header covers the older compiler. No behavior change is expected, and no merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: replacing MSVC-specific _isnan and _finite calls with standard std::isnan and std::isfinite. The raw summary and objectives confirm this is the central change across t…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description states that the PR replaces the MSVC-only _isnan and _finite CRT calls with the standard std::isnan and std::isfinite. It names the adapter header cmath_adapter.h, the call sites in Ga…
Full details: Title check

Explanation

The title names the main change: replacing MSVC-specific _isnan and _finite calls with standard std::isnan and std::isfinite. The raw summary and objectives confirm this is the central change across the GameEngine, WW3D2, and adapter files. The title uses the refactor: prefix and is specific.

Full details: Description check

Explanation

The description states that the PR replaces the MSVC-only _isnan and _finite CRT calls with the standard std::isnan and std::isfinite. It names the adapter header cmath_adapter.h, the call sites in GameEngine and WW3D2, the VC6 compatibility approach, and the disassembly evidence. This matches the changeset, which adds the adapter, updates PreRTS.h and wwmath.h, and changes the NaN checks in both game trees.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c44b387d-52d5-4b05-8f36-339552a6c38d
Commits

Reviewing files that changed from the base of the PR and between de7fbee and 910c365.

Files selected for processing: 1

  • Core/Libraries/Source/WWVegas/WW3D2/streakRender.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@CryoTheRenegade
CryoTheRenegade marked this pull request as ready for review November 16, 2026 19:38
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T03:39:38.479918Z e6d35c1 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] The PR appears safe to merge.

Summary

This PR replaces Microsoft-only floating-point checks with standard names while keeping VC6 support.

  • Game and renderer checks use standard names for NaN and infinity.
  • WWMath validity checks read floating-point bits with std::bit_cast.

No new actionable issues were found. The earlier formatting finding is fixed.

Reviews (5) · Last reviewed commit: "refactor(wwmath): Read the float bits wi..." · Reviewed by Greptile

CryoTheRenegade and others added 2 commits November 16, 2026 20:38
_isnan and _finite are MSVC CRT functions from <float.h>. Use the standard
functions instead. The new cmath_adapter.h gives VC6 std::isnan and
std::isfinite built on _isnan and _finite, because neither VC6 nor
STLport 4.5.3 has them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CryoTheRenegade and others added 3 commits November 17, 2026 01:11
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…isnan

wwmath.h does not call std::isnan or std::isfinite, so it should not include the adapter. The three WW3D2 files that do now include it themselves, as they do for bit_adapter.h.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

Does this cover all source files? I see no tools.

@xezon xezon added Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing labels Nov 18, 2026
…Float and Is_Valid_Double

Both functions read the value through an unsigned long pointer, which
breaks the strict aliasing rule. std::bit_cast copies the bits instead.
std::isfinite would also do, but it needs <cmath> in wwmath.h, and
MSVC's <cmath> adds float overloads of sin and fabs that change the
math of WW3D2 files that do not include it today.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CryoTheRenegade

Copy link
Copy Markdown
Author

Does this cover all source files? I see no tools.

Yes, no tool calls _isnan or _finite.

@xezon
xezon merged commit 1f876ba into TheSuperHackers:main Nov 19, 2026
57 of 59 checks passed
@CryoTheRenegade
CryoTheRenegade deleted the refactor/isnan-isfinite branch November 19, 2026 21:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Minor Severity: Minor < Major < Critical < Blocker Refactor Edits the code with insignificant behavior changes, is never user facing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants