Fix vectorized erf returning NaN at ±inf instead of ±1

libeigen/eigen!2306

Closes #3053
This commit is contained in:
Pavel Guzenfeld
2026-03-19 21:12:15 +00:00
committed by Rasmus Munk Larsen
parent 36ca36d0de
commit 3e5a2f9245

View File

@@ -506,7 +506,11 @@ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<float>::run(const T& x)
template <>
template <typename T>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<double>::run(const T& x) {
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T generic_fast_erf<double>::run(const T& x_in) {
// Clamp x to [-28:28] beyond which erf(x) is ±1 within double precision.
// This avoids NaN from twoprod and exp operations for infinite inputs.
constexpr double kClamp = 28.0;
const T x = pmin(pmax(x_in, pset1<T>(-kClamp)), pset1<T>(kClamp));
T x2 = pmul(x, x);
T erf_small = pmul(x, erf_over_x_double_small(x2));