Repository navigation
fix: make enhanced-determinism cover the math calls that go to the platform's libm under std - #1074
Open
iancarscadden wants to merge 1 commit into
Open
iancarscadden wants to merge 1 commit into
iancarscadden wants to merge 1 commit into
Conversation
With the `std` feature on, `x.sin()`, `x.acos()`, `x.exp()`... on a `Real` resolve to the inherent float methods, which call the platform's math library even when `enhanced-determinism` (`simba/libm_force`) is enabled. The results then differ across platforms. Call them through simba's `ComplexField`/`RealField` instead: - `AngularLimitParams::new` (`sin_cos`): every joint with an angular limit - `motor_angular_generic` (`sin`): position-target motors of impulse joints attached to a multibody link - `RevoluteJoint::angle` (`asin`, 3D) - soft bodies: deformation damping and tear smoothing (`exp`), cell size and plasticity (`powf`), dihedral angles (`acos`), 2D shape matching (`atan2`), the tube and disk builders (`sin`/`cos`) The new `platform_math_determinism` tests pin the joint paths to goldens that the native, aarch64 and wasm32 CI jobs all check. Refs dimforge#1054
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1054.
Problem
With
enhanced-determinism, transcendental functions are meant to go throughlibm(simba/libm_force). That only happens for calls made through simba's traits. A method call such asx.sin()on aRealresolves to the inherentf32::sinwhenever thestdfeature is on (the default, and whatserde-serializeneeds), and that calls the platform's math library. Platform libms round these functions differently, so results differ across platforms even withenhanced-determinismon.Fix
Call each of these sites through
simba::scalar::ComplexField/RealField, like the rest of the solver already does (simd_asin,ComplexField::cos, ...). Withoutenhanced-determinismthey still resolve to the standard library, so nothing changes for other users.joint_constraint_helper.rs,AngularLimitParams::newsin_cosgeneric_joint_constraint_builder.rs,motor_angular_genericsin×2revolute_joint.rs,RevoluteJoint::angleasin×2soft_element_constraint_assembly.rsexp,powfsoft_constraint_plasticity.rspowf×2soft_scalar_constraint.rsacossoft_body_shape_matching.rsatan2soft_body_material.rsexpsoft_body_builder_shapes.rssin/cos×2soft_body_builder_mesh_helpers.rsacosThe list comes from a grep for every transcendental method call on a concrete float, cross-checked with clippy's
disallowed-methodsoverf32::/f64::. What is left after this PR ispowi(2)(one multiplication),Rot2::sin()/cos()(accessors), and test code.Repro
The new
platform_math_determinismtests (2D and 3D) step the joint paths and compare the result bit for bit with a golden. Onmaster(cherry-pick only the two test files):mastermasterrevolute_joint_angle0xdd919012d0f49c140x384ef971c0fe5a210x384ef971c0fe5a21motor_position_on_multibody_link0x1c9bbfec0e740d880xec521711c406c8410xec521711c406c841motor_position_on_multibody_link0xc067a7ae165a74320x4260ecae0c6ed6bd0x4260ecae0c6ed6bdangular_limitshappens to agree between these two targets for the ranges I tried (Apple'ssinf/cosfrarely differ from libm for small arguments); it is there as a golden for the other platforms. #1054's repro repository shows that path diverging on macOS in 2D.Testing
tests-determinism,tests-determinism-arm64andwasm-determinismjobs and torun-ci-checks.sh.cargo fmt -- --check,cargo clippywith-D warnings,cargo test -p rapier2d -p rapier2d-f64 -p rapier3d -p rapier3d-f64(799 passed),cargo check --features enhanced-determinismfor both crates, theno_stdchecks on the host target. The existingsnapshot_portability(native and wasm32) andsimd_backend_determinismgoldens are unchanged.Not in this PR
Bvhoptimizationlog2,VectorExt::angle,ConvexPolyhedronsin_cos, mesh intersection): fix: makeenhanced-determinismcover the math calls that go to the platform's libm understdparry#468.SymmetricEigen3::eigenvalues(SymmetricEigen3::eigenvalues calls platform acos/cos in std builds, breaking enhanced-determinism glamx#9, fix in Route eigenvalue trigonometry through ComplexField under std glamx#10) andSvd2(fix(svd2): route trigonometry through simba so thelibmfeature applies understdglamx#12). The eigenvalue one is what makes 3D bodies with a non-diagonal inertia tensor (compound or rotated colliders) diverge between native and wasm32.For context: with these three fixes applied, 600-tick 2D and 3D scenes in our engine give identical state hashes and byte-identical snapshots on macOS arm64, Linux arm64 and x86_64, Android arm64, wasm32-wasip1 and the iOS Simulator, and the test binaries import no platform math symbols.