Skip to content

Commit ecffd18

Browse files
committed
Avoid error in acos and asin with fast-math
by returning hardcoded values on boundaries -1 and 1
1 parent b207121 commit ecffd18

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

src/colvarmodule.h

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define COLVARS_DEBUG false
1919
#endif
2020

21-
#if defined(__FAST_MATH__) && defined(__aarch64__)
21+
#if defined(__FAST_MATH__)
2222
// NOTE: This is used for fixing https://github.com/Colvars/colvars/issues/767
2323
#define COLVARS_BOUNDED_INV_TRIGONOMETRIC_FUNC
2424
#endif
@@ -152,27 +152,42 @@ class colvarmodule {
152152
return ::cos(static_cast<double>(x));
153153
}
154154

155-
/// Reimplemented to work around MS compiler issues
156-
static inline real asin(real const &x)
157-
{
155+
#ifndef PI
156+
#define PI 3.14159265358979323846
157+
#define PI_2 1.57079632679489661923
158+
#endif
159+
160+
/// Reimplemented to work around compiler issues; return hard-coded values for boundary conditions
161+
static inline real asin(real const &x)
162+
{
158163
#ifdef COLVARS_BOUNDED_INV_TRIGONOMETRIC_FUNC
159-
const double tmp = static_cast<double>(x);
160-
return ::asin(tmp > 1.0 ? 1.0 : (tmp < -1.0 ? -1.0 : tmp));
164+
if (x <= -1.0) {
165+
return -PI_2;
166+
} else if (x >= 1.0) {
167+
return PI_2;
168+
} else {
169+
return ::asin(static_cast<double>(x));
170+
}
161171
#else
162172
return ::asin(static_cast<double>(x));
163173
#endif
164-
}
174+
}
165175

166-
/// Reimplemented to work around MS compiler issues
167-
static inline real acos(real const &x)
168-
{
176+
/// Reimplemented to work around compiler issues; return hard-coded values for boundary conditions
177+
static inline real acos(real const &x)
178+
{
169179
#ifdef COLVARS_BOUNDED_INV_TRIGONOMETRIC_FUNC
170-
const double tmp = static_cast<double>(x);
171-
return ::acos(tmp > 1.0 ? 1.0 : (tmp < -1.0 ? -1.0 : tmp));
180+
if (x <= -1.0) {
181+
return PI;
182+
} else if (x >= 1.0) {
183+
return 0.0;
184+
} else {
185+
return ::acos(static_cast<double>(x));
186+
}
172187
#else
173188
return ::acos(static_cast<double>(x));
174189
#endif
175-
}
190+
}
176191

177192
/// Reimplemented to work around MS compiler issues
178193
static inline real atan2(real const &x, real const &y)

src/colvartypes.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@
2020

2121
#include "colvarmodule.h"
2222

23-
#ifndef PI
24-
#define PI 3.14159265358979323846
25-
#endif
26-
2723
// ----------------------------------------------------------------------
2824
/// Linear algebra functions and data types used in the collective
2925
/// variables implemented so far

0 commit comments

Comments
 (0)