Skip to content

physics: plant_log's scenes pick the same cells on every standard library - #1121

Merged
transfix merged 6 commits into
masterfrom
fix/physics-portable-draws
Nov 11, 2026
Merged

transfix merged 6 commits into
masterfrom
fix/physics-portable-draws

Conversation

@transfix

@transfix transfix commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

cvc::physics' last seeded picks that each standard library mapped its own way: plant_log_scene.cpp's three std::uniform_int_distribution<std::size_t> picks are now nav::detail::pick_below, the same as cvc::nav's. A seed scatters the same plant-log scene on Linux, Windows, macOS and wasm.

Stacked on #1118. #1114 has merged, and master is merged in here. #1118's commit (a5c1a54) still comes first. Once #1118 merges, this PR's diff is only 75ad660.

What changes

  • scatter (the city and bundle starts and goals), plan_for's route goal in the 5-15% ring (bundle), and the city's new goal at each arrival: pick_below in place of uniform_int_distribution<std::size_t>.
  • PlantLogScene.CityStartsAreTheSameOnEveryStandardLibrary: pins seed 7's eight Humvee starts as city cells. Each is read back from the rear axle after the 0.5 s settle, which leaves it within 0.02 of its cell's centre (cells are 9.9 m). libc++'s own draw began (5, 55), (57, 5), (20, 75).
  • One line each in plant_log_scene.h's doc and the test file's header.

size_t takes the same path as int on libstdc++ and MSVC

I checked this by running one program built three ways: MSVC 19.44, g++ 13.2 (libstdc++), and em++ 21 (libc++, run under node).

library uniform_int_distribution<std::size_t>, 32-bit mt19937, n from 1 to 2^32 - 1 vs pick_below
libstdc++ (64-bit size_t) the urngrange == UINT32_MAX branch: 32-bit Lemire 0 of 260000 differ, streams in step
libstdc++ with a mersenne_twister_engine<uint64_t, 32, ...> (glibc's std::mt19937, where uint_fast32_t is 64 bits) the same branch 0 of 230000
MSVC STL 14.44 (_Rng_from_urng_v2, 64-bit _Udiff) one 32-bit draw, 128-bit product, low 32 bits rejected below (2^32 - n) % n 0 of 260000
libc++ masks the low bits and rejects 237086 of 260000 differ

So Linux and Windows draw exactly what they drew. On Windows, before and after this change:

  • every sample of the Humvee's and the RoboRacer's swerve and city drives (seeds 7, 100 and 101, 20 s) hashes the same;
  • physics_plant_log_test's whole output is the same, the [power-fit] lines included;
  • the new test passes against the library from before this change too.

The real draws stay as they are

frac, amp and adv (the speeds and the swerve's gates) stay std::uniform_real_distribution<double>. libstdc++ and libc++ agree bit for bit. MSVC implements P0952R2's generate_canonical, which truncates to 53 bits where the other two round, so 8996 of 40000 doubles differ, each by at most 2^-53 of the canonical. All three take two draws per value, so the picks after them stay in step. plant_log keeps only floats from these draws, and none of 160000 such floats differ. Writing these draws out would change Linux's or Windows' draws, so they stay.

Gates

None was loosened over macOS's plant-log draws, so none is tightened here. PlantLogPowerConvergence's "hit another vehicle" filter (#875) is about Linux-vs-Windows floating point and stays.

Tests (Windows, MSVC 19.44, RelWithDebInfo)

  • physics_plant_log_test: 16 of 16 pass, including the Convergence test.
  • nav_plant_test (without the Stress/Convergence/Benchmark/Performance/Integration suites): 23 of 23 pass.
  • All changed lines are clang-format 18.1.8 clean.

Not covered: the bundle scene's ring pick is not driven by any test, because it needs a city bundle.

NavSimWorldAgents.EachPolicyDrivesItsOwnAgents failed on package-macos with
pd.pos[2] == po.pos[2] == -5.26315784. scatter_free drew its cells with
std::uniform_int_distribution, whose mapping each standard library picks for
itself. libstdc++ and MSVC both use Lemire's multiply-and-reject on a 32-bit
draw, so they agree. libc++ masks the low ceil(log2 n) bits and rejects. With
seed 11, libc++ gave agent 1 a start and a goal in one column (45), so it drove
straight along -y and its x stayed at -5.2631578 under both policies. Only y
differed. #1095 loosened the check to the whole world's positions, which hid
the different scene rather than fixing it.

- sim_world.cpp: pick_below writes out Lemire's mapping, so a seed is one scene
  on every platform: the one Linux and Windows always drew. Their worlds are
  unchanged bit for bit; macOS and wasm (libc++) now draw them too. The colours
  stay std::uniform_real_distribution: it takes one draw on each library, so
  the picks after it stay in step.
- nav_sim_world_test: ScatterFreePicksTheSameCellsOnEveryStandardLibrary pins
  seed 11's cells on an open field.
- nav_sim_world_agents_test: the policies must drive agents 1 and 3 to
  different points, not merely to different whole worlds. A point, not one
  coordinate, since an agent driving along a column keeps its x.
…every standard library

The rest of cvc::nav's seeded draws, written out the way #1114 wrote out scatter_free's.

- detail/seeded_draw.h: pick_below (#1114's Lemire multiply and reject, moved out of
  sim_world.cpp and shared) and seeded_shuffle.
- training_scene::sample_starts_goals: its three picks (goals anywhere, starts with a clearance,
  local goals) are pick_below. libstdc++ and MSVC map a 32-bit draw this way already, so Linux and
  Windows draw the routes they always drew; macOS and wasm (libc++, low bits and reject) now draw
  them too.
- sim_world::from_occupancy, clustered: the k-means centres come from seeded_shuffle instead of
  std::shuffle. The three libraries all shuffle differently: libstdc++ takes two swaps from one
  Lemire draw while n * n fits in 32 bits, MSVC's std::shuffle maps each draw by its remainder
  (its older _Rng_from_urng, not uniform_int_distribution's Lemire), and libc++ swaps from the
  other end. seeded_shuffle is libstdc++'s written out, so Linux keeps its groups and Windows,
  macOS and wasm change: on the pinned field (twelve agents, three groups) MSVC's shuffle gave 34
  of 40 seeds other groups.

Checked against the libraries themselves with one program built by MSVC 19.44, g++ 13.2
(libstdc++) and em++ 21 (libc++, run under node): pick_below matches uniform_int_distribution<int>
on libstdc++ and MSVC for every case (230000 picks, n from 1 to INT_MAX, the draw streams in
step), seeded_shuffle matches libstdc++'s std::shuffle for every case (n up to 70001), and the
written-out draws hash the same under all three.

Tests:
- NavCoefTrain.StartsAndGoalsAreTheSameOnEveryStandardLibrary pins seed 7's cells in each of the
  three ways on a 40 x 30 field with a wall. libc++'s own draw began {19, 4, 31, 13}. The clearance
  and the goal range sit at least 0.019 from every cell's square-root distance, so rounding moves
  no cell in or out.
- NavSimWorld.ClusteredGroupsAreTheSameOnEveryStandardLibrary pins seed 20's groups of twelve;
  each agent is at least 15% nearer (squared) its own centre than the next at every step.
- NavSimWorld.SeededShuffleIsTheSameOnEveryStandardLibrary pins odd, even and past-paired lengths.

The gates loosened because macOS drew other routes:
- DynBike.KinematicLimitFollowsBikeStep: the open-loop gap is gated at 0.1 rr again (#717 had
  moved it to 0.2 rr because each library drew other routes). It is 0.0107 on these routes, the
  same with the test compiled /fp:contract /arch:AVX2, which contracts its arithmetic to about
  1260 fused multiply-adds as clang does on arm64. The closed-loop worst agent stays reported, not
  gated: on these same routes it parts by 1.80 normally and by 0.017 with the fused arithmetic,
  while the medians barely move (0.00045 / 0.17% against 0.00032 / 0.13%).
- DiffExtForce / DiffExtRollout and the four-wheel ext checks keep their switch filter and
  quotas: the shared draws still put one to five cases on a switch, and the quotas were set over
  100 swept seeds, not per library. Their comments, and NAV_TRAINING.md, no longer say the draws
  differ by library.

Windows (MSVC, RelWithDebInfo): nav_sim_world 7/7, nav_sim_world_agents 17/17, nav_coef_train
31/31, nav_dyn_bike 23/23, nav_dyn_4w 23/23, nav_potential 17/17, nav 64/64. Every pre-existing
test's output is identical to the base commit's, number for number.
…rary

plant_log_scene.cpp's three seeded picks were std::uniform_int_distribution<std::size_t>: the
city's and the bundle's scatter, the bundle's route goals in the 5-15% ring, and the city's new
goal at each arrival. They are nav::detail::pick_below now (#1118's), as cvc::nav's own are.

Checked by running one program built by MSVC 19.44, g++ 13.2 (libstdc++) and em++ 21 (libc++,
under node):
- With a 32-bit mt19937, uniform_int_distribution<std::size_t> on 64-bit libstdc++ and MSVC still
  takes the 32-bit Lemire path while the range is below 2^32 (libstdc++'s urngrange == UINT32_MAX
  branch; MSVC's _Rng_from_urng_v2 with one 32-bit draw and a 128-bit product): 0 of 260000
  picks differ from pick_below, n from 1 to 2^32 - 1, the streams in step. The same with a
  mersenne_twister_engine<uint64_t, 32, ...>, which is glibc's std::mt19937 (uint_fast32_t is 64
  bits there). libc++ masks and rejects: 237086 of 260000 differ.
- So Linux and Windows draw what they drew. On Windows the drives are bit for bit the same before
  and after (every sample of the Humvee's and the RoboRacer's swerve and city, seeds 7, 100 and
  101, 20 s), and so is physics_plant_log_test's output, the power fits included.
- The speeds and the swerve's gates stay std::uniform_real_distribution<double>: libstdc++ and
  libc++ agree bit for bit, and MSVC's P0952R2 generate_canonical truncates where they round, at
  most 2^-53 of the canonical apart. It is two draws each on all three, and none of 160000 floats
  of the kind plant_log keeps from them differ.

Tests:
- PlantLogScene.CityStartsAreTheSameOnEveryStandardLibrary pins seed 7's eight Humvee starts as
  city cells, read back from the rear axles after the settle (each within 0.02 of a cell's
  centre). It passes against the library before this change too: these are the cells Windows
  drew. libc++'s draw began (5, 55), (57, 5), (20, 75).
- No gate was loosened for macOS's plant-log draws. PlantLogPowerConvergence's collision filter
  (#875) is for Linux's and Windows' floating point, and stays.

The bundle scene's ring pick is not driven by a test: it needs a city bundle.
The two conflicts (sim_world.cpp's scatter_free picks, nav_sim_world_test.cpp's tests after
ScatterFreePicksTheSameCellsOnEveryStandardLibrary) are #1114's lines against #1118's rewrite of
them. #1114's squash is its commit 79e05d4 unchanged, so they keep #1118's side.
…ument

package-linux's writable-data scan failed on 8 symbols this branch never touched
(algo/distance_transform.cpp, ariadne/script_engine.cpp, nav/grid_nav.cpp) and 2 stale grid_nav
entries: master's allowlist has had them since. #1119 gave drive_plant_log_scene an app first;
PlantLogScene.CityStartsAreTheSameOnEveryStandardLibrary passes one, as its neighbours do.
…1133)

package-linux's writable-data scan failed on seven room_calls.cpp symbols from #1122, which this
branch never touched; #1133 put them on master's allowlist.
@transfix
transfix merged commit af7dd59 into master Nov 11, 2026
33 checks passed
@transfix
transfix deleted the fix/physics-portable-draws branch November 11, 2026 19:54
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