Skip to content

cvc::algo::det: cross-platform exp, log and pow; cvc::ocean's sea is the same on every platform - #1138

Merged
transfix merged 1 commit into
masterfrom
feat/ocean-det-math
Nov 11, 2026
Merged

transfix merged 1 commit into
masterfrom
feat/ocean-det-math

Conversation

@transfix

Copy link
Copy Markdown
Owner

The prerequisite for O6 in docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md: cvc::ocean's sea gives the same bits on every platform. O6 folds h0 and the shore layer into the physics state_hash, which a native run and a wasm run must agree on. Until now both called libm's exp, log, pow, sin and cos, which MSVC, glibc, Apple and musl each round their own way.

cvc/algo/det_math.h

  • Moved: cvc::physics's private det_math is now cvc::algo::det, public and standard library only. Like the rest of cvc::algo, it depends on no other libcvc library.
  • New functions: exp, log, pow, sinh, cosh, acos and hypot. As before, they use only correctly rounded operations and exact scaling, in a fixed order, built without FMA contraction.
    • log is fdlibm's arrangement with its series run out to 2z¹²/25.
    • pow follows libm's special cases (C99 Annex F). y = 1, 2, −1 and 0.5 are a single correctly rounded operation. Any other y is e^(y log x), with log x carried as a double-double and multiplied by y exactly (Dekker), so a large y log x keeps its digits. An earlier plain e^(y log x) reached 17 ULP at x^60, and squaring for integer y doubles its error at each step.
    • hypot scales by a's power of two, so it neither overflows nor underflows.
  • Physics is unchanged. src/cvc/physics/det_math.h is now a namespace alias, cvc::physics::det = cvc::algo::det, so every physics call site stays as it was. The existing functions' code did not change.

cvc::ocean

  • spectrum, h0 and shore call det:: in place of std:: for every transcendental (sqrt, floor and fabs are already exact).
  • No FMA contraction in their sources (-ffp-contract=off, /fp:precise), as for physics.
  • The configure-time guard now lets cvc::ocean include cvc::algo, which depends on nothing, so the sea's library still needs only the standard library.
  • Fast-marching order: it pops (value, cell) pairs, so every standard library takes cells in the same order.

Tests

  • algo_det_math_test (was physics_det_math_test; now linked against libcvc rather than compiling det_math in):

    • each new function against libm over the ranges the ocean uses;
    • libm's edge cases, including pow's whole special-case table;
    • bit pins for the new functions next to the old ones.
  • Worst errors measured against MSVC's libm:

    Function Worst error (ULP)
    exp 1
    log 1
    pow 1, and 2 for exponents up to 60
    sinh, cosh 2
    acos 4
    hypot 0

    Each bound is that plus one, as the file already does for another platform's libm.

  • ocean_spectrum_test, OceanH0.IsTheSameOnEveryPlatform: digests of the spectrum on a grid of k and of three cascades' h0, with wind sea, swell and finite depth.

  • ocean_shore_test, OceanShore.IsTheSameOnEveryPlatform: a digest of a sloped island's shore layer (all ten planes, evaluate() and swash() at three times). Its grid is built from correctly rounded operations only.

  • Where the pins come from: all were measured on Windows (MSVC, x86_64). This PR's Linux and macOS CI runs are the cross-platform check. A platform that disagrees has found a leak to fix, never a baseline to re-record.

Checked locally (Windows Release, GL build)

  • algo_det_math_test 11, ocean_spectrum_test 14, ocean_shore_test 16.
  • Every physics_* test, physics_replay_test (the recorded trace's hashes), cvcgl_ocean_fft and cvcgl_physics_scene_binding pass.
  • lsystem_coast --frames 80 --shore-check:
    • still no displaced vertex onto dry ground;
    • runup up to 3.00 m, with about 1000 beach cells in its reach.
  • git clang-format 18.1.8 is clean on the changed lines.

Next

O6: the fluid_surface hook on cvc::physics::field, the deterministic CPU FFT of the physics band, the pinned time base and pycvc bindings.

…the same on every platform

Before O6 puts the sea into the physics state_hash, h0 and the shore layer must give the same
bits on every platform; they called libm, whose transcendentals each platform rounds its own
way (docs/roadmap/OCEAN-AND-WEATHER-ROADMAP.md, B1's open item).

- cvc::physics's private det_math moves to cvc::algo as cvc/algo/det_math.h (standard library
  only), and gains exp, log, pow, sinh, cosh, acos and hypot. pow carries log x as a double-double
  and multiplies it by y exactly, so it stays within 3 ULP of libm even where y log x is large;
  y = 1, 2, -1 and 0.5 are one correctly rounded operation each. physics/det_math.h is now a
  namespace alias, so the physics step is unchanged (its bit pins and physics_replay_test hold).
- cvc::ocean's spectrum, h0 and shore layer call det:: in place of std::, and are built without
  FMA contraction. cvc::ocean may now include cvc::algo (which depends on nothing).
- algo_det_math_test (was physics_det_math_test) measures each new function against libm over
  the ranges the ocean uses, checks libm's special cases (pow's C99 Annex F table), and pins
  bits. ocean_spectrum_test and ocean_shore_test pin digests of the spectrum, h0 and the shore
  layer's planes, evaluate() and swash(), measured on Windows, for CI to check elsewhere.
@transfix
transfix enabled auto-merge (squash) November 10, 2026 22:31
@transfix
transfix merged commit a41ccb5 into master Nov 11, 2026
22 of 33 checks passed
@transfix
transfix deleted the feat/ocean-det-math branch November 11, 2026 00:33
transfix added a commit that referenced this pull request Nov 11, 2026
…me (O6a) (#1139)

On top of #1138. cvc/physics/fluid_surface.h: fluid_surface (level, height, velocity, digest), still_surface and wave_train_surface (linear Airy trains, det:: math). field::surface (fluid only) clips each cell's top to the surface and drives drag with its velocity. world::field_time() is anchored to the clock by follow()'s first call, or set by set_field_time. With a surface, save() carries the field-time anchor and each surface's digest, so state_hash covers them and restore refuses other surfaces; without one the format is unchanged. physics_field_test adds B6's tests: half draft on calm water, heave in phase with a swell at its period, the field time, hashing and restore.
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