Skip to content

cvc::physics: dot() and vec3 * s give the same bits in every TU, contracted or not - #1635

Merged
transfix merged 1 commit into
masterfrom
fix/physics-helpers-no-contract
Nov 29, 2026
Merged

transfix merged 1 commit into
masterfrom
fix/physics-helpers-no-contract

Conversation

@transfix

@transfix transfix commented Nov 29, 2026 •

Copy link
Copy Markdown
Owner

Follows #1606. cvc/physics/types.h's inline dot() gave bits that depended on the caller's contraction flags. In a shared macOS Debug build that reached libcvc itself, because dyld binds libcvc's own calls to the test executable's copy. #1606 worked around it by building three test files with -ffp-contract=off, and any new physics test that called dot() without the flag would have hit it again. dot() and vec3 * s now give the uncontracted bits in every translation unit, whatever its -ffp-contract or /fp. No golden changes.

Cause

A compiler may fuse a product and the sum it feeds into one FMA, which rounds once where the source rounds twice:

  • clang does this by default (-ffp-contract=on), within one expression, where the target has an FMA: aarch64, or x86-64 with -mfma.
  • gcc does it across statements (-ffp-contract=fast, its C++ default).
  • MSVC does it under /fp:contract and /fp:fast.

libcvc's deterministic sources are built with -ffp-contract=off (/fp:precise), and the goldens are their results. types.h's helpers are inline, so every caller compiled its own copy with its own flags:

  • dot()'s body a.x * b.x + a.y * b.y + a.z * b.z fused under clang's and gcc's defaults (two FMAs).
  • A caller's p + v * s fused under -ffp-contract=fast, once operator* and operator+ were inlined into it.

Fix

Each product is rounded before anything is added to it.

  • gcc and clang on aarch64 and x86-64: detail::rounded(x) is an empty asm on the product ("+w" / "+x"), as glibc's math_opt_barrier is. It emits no instruction, and dot() stays inline. It is applied in dot() and operator*.
  • MSVC: #pragma fp_contract(off) over the operators and dot() only, inside float_control(push) / pop. MSVC never contracts under /fp:precise or /fp:strict (libcvc's default) in any case.
  • Other targets get no barrier. wasm32 has no scalar FMA.

The header comment, the src/cvc/tests/CMakeLists.txt comments and docs/PHYSICS_API.md now say so. #1606's per-TU flags stay. physics_field_test.cpp and ph_train_test.cpp still need theirs for their own arithmetic (the coast distance, the double side of EXPECT_DOUBLE_EQ). physics_dyn_boat_plant_test.cpp no longer needs its flag (see Testing), but it is kept, as the bike and four-wheel tests keep theirs.

How the mechanism was chosen

Each candidate was checked in the generated code, not taken from documentation. The aarch64 checks used clang 20.1 (zig 0.15.2) and clang 22.1.8 (cvcpkg llvm) for arm64-apple-macos, which gave identical results. The x86-64 checks used gcc 16.1 (cvcpkg mingw-w64-gcc) with -mfma, and MSVC 19.44 with /arch:AVX2. "Holds" means the out-of-line copy (-O0) and the inlined copy (-O2) had no FMA.

candidate clang on clang fast gcc on gcc fast inlined
today's body fuses fuses fuses (even at -O0) fuses yes
#pragma clang fp contract(off) in the body holds fuses (ignored) n/a n/a yes
#pragma STDC FP_CONTRACT OFF holds fuses fuses (ignored) fuses yes
products in named temporaries holds fuses holds fuses yes
__attribute__((optimize("fp-contract=off"))) n/a n/a holds holds no: gcc stops inlining dot()
__builtin_assoc_barrier on the products not in clang not in clang holds holds yes
empty asm on the products (this PR) holds holds holds holds yes

The pragma also held under clang's -ffp-contract=fast-honor-pragmas. MSVC's fp_contract(off) + float_control held under /fp:contract and /fp:fast, inlined too. After the pop, the including file's own a * b + c still contracted, so the pragma does not leak.

Testing

  • arm64 asm (clang 20 and 22; off, on, fast, fast-honor-pragmas; -O0 and -O2): no FMA in dot()'s out-of-line copy, operator*'s, an inlined dot(), or a caller's p + v * s. A plain a * b + c in the same file still fuses, which shows the TU really contracts. The inlined dot() is fmul, fmul, fadd, fmul, fadd, the uncontracted order.
  • x86-64 results, on an i7-12700H, which has FMA. A hash of the bits over 200000 random inputs covers dot() inlined and through its out-of-line copy, plus p + v * s and t * b - a, inlined and through operator*'s copy. Every build printed the -ffp-contract=off hash, 56bec10b08a80cd5. The builds were gcc 16 and clang 20 with -mfma (off, on, fast; -O0, -O2), MSVC 19.44 /arch:AVX2 (/fp:strict, precise, contract, fast; /Od, /O2) and clang-cl 22 (/fp:precise, /fp:fast). With today's header, 11 of those builds printed one of five other hashes: gcc and clang on (-O0 and -O2), gcc and clang fast at -O2, MSVC /fp:contract and /fp:fast at /O2, and clang-cl (whose /fp:precise is -ffp-contract=on). Under clang fast, the inlined dot() even disagreed with its own out-of-line copy in 49187 of 200000 cases.
  • Warnings: gcc -Wall -Wextra -Wpedantic -Werror, clang -Weverything (the one hit is the existing __CVC_PHYSICS_TYPES_H__ guard name), MSVC /W4 /WX. clang-format 18.1.8 is clean.
  • Windows (this box): a fresh Release shared MSVC build (Ninja, cvcpkg deps prefixes) of libcvc and the 40 test targets that reach types.h (physics, ph, ocean, algo, holon, avatar, scene world). 794 tests passed and 28 skipped (optional deps). physics_replay_test matched all 1240 steps of its trace. All four goldens passed, as did PhysicsFluidSurface's and PhysicsOceanSurface's.
  • macOS: ci/full/physics-no-contract has this commit's tree plus one temporary commit (ab3cd2c), which builds physics_dyn_boat_plant_test.cpp with the compiler's default contraction again. That is the setting under which DynBoatPlant's Debug golden failed before tests: the wake, boat-plant and tape tests without FMA contraction (master's macOS lanes) #1606. CI (recipe-first) 46202725919 is green on all six jobs. Both macOS lanes ran macos-26-arm64 20261123.0526.1 with AppleClang 21.0.0. ctest passed 100% (8781 tests in Debug, 8783 in Release), and no test needed a retry. PhysicsFieldWake, PhysicsFieldWakeShore and DynBoatPlant's CrossPlatformGoldenHash and PhTrain.TheTapesDerivativesMatchFiniteDifferences passed in both lanes. So a contracted test TU calling dot() no longer moves the Debug goldens.
    The same push's CI/CD 46202725942 passed every finished job but one (package-linux / libcvc-debug was still running when this was written): package-macos / libcvc-release-grpc-static failed PoolKillOwner.TwoAtOnceBothReturn (state_exec_router_test.cpp:3707, "the case missed its window (raise kMany)") on both tries. That is a thread-overlap timing test that includes no physics header, and master's last completed run of that job (46189598631) passed it.

Other inline helpers with the same exposure (not changed here)

I compiled each header that the physics, ocean and algo exact sources reach (66 headers) with clang for arm64, -ffp-contract=on -femit-all-decls, and listed every inline libcvc function whose IR has an llvm.fmuladd. In cvc/physics/ it is only dot(). Elsewhere:

helper contracts exposure
ocean::ewave::x_of / y_of (ewave.h), ocean::shore_grid::x / y (shore.h) origin + i * dx Called by ewave.cpp / shore.cpp (exact) and by default-flag TUs (cvcGL's cvcgl_ewave.cpp among them). In a macOS Debug build the copy libcvc calls can be a contracted one.
height_field::max_x / max_y (geometry/height_field.h) _x0 + _dx * n Called by nav's and world's exact sources and by many default-flag ariadne TUs. Same Debug exposure.
algo::add_gust_front (algo/gust_front.h) 6 sites No exact source calls it. world/weather.cpp and ariadne/scene_weather.cpp (default flags) get different bits on arm64 from ocean_wind_test (built without contraction).
nav's header-only models: nav::diff::* (diff_rollout.h, dyn_*.h, 33 functions), nav::detail::ipc_b / ipc_pw_knee (nav_potential.h), nav::kinematic_step (plant_types.h) throughout Already handled by per-TU flags on their tests (#747, #750, #837). A barrier on every product there would be a rewrite, so the flags stay the right tool.
ticks_per_step (core/clock_follower.h), ocean::texel_k (ocean/h0.h) 1 site each Harmless: one feeds only a 1e-6 tolerance check, and the other's product (0.5 * n) is exact.

The small grid helpers (x_of, shore_grid::x, max_x) could take the same barrier. The barrier would need a home that ocean (which may include only cvc/algo) and geometry can both include, since the physics headers include only the standard library. That would be a follow-up.

…racted or not

cvc/physics/types.h's inline dot() fused into FMAs in any translation unit
built with contraction: clang's default -ffp-contract=on on aarch64, gcc's
default -ffp-contract=fast, MSVC's /fp:contract and /fp:fast.  The
deterministic sources are built with -ffp-contract=off, so the bits depended on
the caller's flags, and in a shared macOS Debug build dyld bound libcvc's own
calls to a test executable's contracted copy (the cause #1606 worked around
with per-TU flags).  Under -ffp-contract=fast the same held for a caller's
p + v * s.

Each product is now rounded before anything is added to it:

- gcc and clang on aarch64 and x86-64: detail::rounded(), an empty asm on the
  product ("+w" / "+x", as glibc's math_opt_barrier).  No instruction, and
  dot() stays inline.
- MSVC: #pragma fp_contract(off) around the operators and dot() only
  (float_control push/pop).

Checked in the generated code first.  For arm64 macOS, clang 20 and 22 under
-ffp-contract=off, on, fast and fast-honor-pragmas, at -O0 (the weak copy) and
-O2 (inlined): no FMA in dot(), operator* or a caller's p + v * s.  The
instruction sequence is the uncontracted one.  On an x86-64 with FMA, a hash
over 200000 random inputs (inlined, the out-of-line copies, p + v * s) is the
-ffp-contract=off hash for gcc 16, clang 20, MSVC 19.44 and clang-cl 22 in
every mode.  The old header gave five other hashes.  What did not work:
#pragma clang fp contract(off) and #pragma STDC FP_CONTRACT OFF (clang
disregards them under -ffp-contract=fast, gcc ignores them), named
temporaries (stop only -ffp-contract=on), gcc's optimize("fp-contract=off")
(gcc then will not inline dot()).  gcc's __builtin_assoc_barrier works, but
clang has no such builtin.

Goldens are untouched: the hardened helpers compute what the
-ffp-contract=off sources always did.
@transfix
transfix merged commit 1834c3d into master Nov 29, 2026
32 checks passed
@transfix
transfix deleted the fix/physics-helpers-no-contract branch November 29, 2026 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant