diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamics/solver/velocity_constraint.rs | 8 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_constraint_element.rs | 8 | ||||
| -rw-r--r-- | src/dynamics/solver/velocity_ground_constraint_element.rs | 7 | ||||
| -rw-r--r-- | src/utils.rs | 46 |
4 files changed, 66 insertions, 3 deletions
diff --git a/src/dynamics/solver/velocity_constraint.rs b/src/dynamics/solver/velocity_constraint.rs index e15f2d8..90c574c 100644 --- a/src/dynamics/solver/velocity_constraint.rs +++ b/src/dynamics/solver/velocity_constraint.rs @@ -385,7 +385,13 @@ where let relative_linvel = linvel1 - linvel2; let mut tangent_relative_linvel = relative_linvel - force_dir1 * (force_dir1.dot(&relative_linvel)); - let tangent_linvel_norm = tangent_relative_linvel.normalize_mut(); + + let tangent_linvel_norm = { + let _disable_fe_except = + crate::utils::DisableFloatingPointExceptionsFlags::disable_floating_point_exceptions(); + tangent_relative_linvel.normalize_mut() + }; + let threshold: N::Element = na::convert(1.0e-4); let use_fallback = tangent_linvel_norm.simd_lt(N::splat(threshold)); let tangent_fallback = force_dir1.orthonormal_vector(); diff --git a/src/dynamics/solver/velocity_constraint_element.rs b/src/dynamics/solver/velocity_constraint_element.rs index 1324c35..d1ad6ac 100644 --- a/src/dynamics/solver/velocity_constraint_element.rs +++ b/src/dynamics/solver/velocity_constraint_element.rs @@ -99,7 +99,13 @@ impl<N: SimdRealField> VelocityConstraintTangentPart<N> { self.impulse[0] - self.r[0] * dimpulse_0, self.impulse[1] - self.r[1] * dimpulse_1, ); - let new_impulse = new_impulse.simd_cap_magnitude(limit); + let new_impulse = { + let _disable_fe_except = + crate::utils::DisableFloatingPointExceptionsFlags:: + disable_floating_point_exceptions(); + new_impulse.simd_cap_magnitude(limit) + }; + let dlambda = new_impulse - self.impulse; self.impulse = new_impulse; diff --git a/src/dynamics/solver/velocity_ground_constraint_element.rs b/src/dynamics/solver/velocity_ground_constraint_element.rs index 8bcdc82..c4c6393 100644 --- a/src/dynamics/solver/velocity_ground_constraint_element.rs +++ b/src/dynamics/solver/velocity_ground_constraint_element.rs @@ -78,7 +78,12 @@ impl<N: SimdRealField> VelocityGroundConstraintTangentPart<N> { self.impulse[0] - self.r[0] * dimpulse_0, self.impulse[1] - self.r[1] * dimpulse_1, ); - let new_impulse = new_impulse.simd_cap_magnitude(limit); + let new_impulse = { + let _disable_fe_except = + crate::utils::DisableFloatingPointExceptionsFlags:: + disable_floating_point_exceptions(); + new_impulse.simd_cap_magnitude(limit) + }; let dlambda = new_impulse - self.impulse; self.impulse = new_impulse; diff --git a/src/utils.rs b/src/utils.rs index c1eb31d..84ed250 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -670,6 +670,52 @@ impl Drop for FlushToZeroDenormalsAreZeroFlags { } } +// This is an RAII structure that disables floating point exceptions while +// it is alive, so that operations which generate NaNs and infinite values +// intentionally will not trip an exception when debugging problematic +// code that is generating NaNs and infinite values erroneously. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct DisableFloatingPointExceptionsFlags { + #[cfg(feature = "avoid-fe-exceptions")] + // We can't get a precise size for this, because it's of type + // `fenv_t`, which is a definition that doesn't exist in rust + // (not even in the libc crate, as of the time of writing.) + // But since the state is intended to be stored on the stack, + // 256 bytes should be more than enough. + original_flags: [u8; 256], +} + +#[cfg(feature = "avoid-fe-exceptions")] +extern "C" { + fn feholdexcept(env: *mut std::ffi::c_void); + fn fesetenv(env: *const std::ffi::c_void); +} + +impl DisableFloatingPointExceptionsFlags { + #[cfg(not(feature = "avoid-fe-exceptions"))] + pub fn disable_floating_point_exceptions() -> Self { + Self { } + } + + #[cfg(feature = "avoid-fe-exceptions")] + pub fn disable_floating_point_exceptions() -> Self { + unsafe { + let mut original_flags = [0; 256]; + feholdexcept(original_flags.as_mut_ptr() as *mut _); + Self { original_flags } + } + } +} + +#[cfg(feature = "avoid-fe-exceptions")] +impl Drop for DisableFloatingPointExceptionsFlags { + fn drop(&mut self) { + unsafe { + fesetenv(self.original_flags.as_ptr() as *const _); + } + } +} + pub(crate) fn select_other<T: PartialEq>(pair: (T, T), elt: T) -> T { if pair.0 == elt { pair.1 |
