Skip to content

Commit f2994c3

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 f2994c3

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

src/colvarmodule.h

Lines changed: 30 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,44 @@ 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+
#endif
158+
#ifndef PI_2
159+
#define PI_2 1.57079632679489661923
160+
#endif
161+
162+
/// Reimplemented to work around compiler issues; return hard-coded values for boundary conditions
163+
static inline real asin(real const &x)
164+
{
158165
#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));
166+
if (x <= -1.0) {
167+
return -PI_2;
168+
} else if (x >= 1.0) {
169+
return PI_2;
170+
} else {
171+
return ::asin(static_cast<double>(x));
172+
}
161173
#else
162174
return ::asin(static_cast<double>(x));
163175
#endif
164-
}
176+
}
165177

166-
/// Reimplemented to work around MS compiler issues
167-
static inline real acos(real const &x)
168-
{
178+
/// Reimplemented to work around compiler issues; return hard-coded values for boundary conditions
179+
static inline real acos(real const &x)
180+
{
169181
#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));
182+
if (x <= -1.0) {
183+
return PI;
184+
} else if (x >= 1.0) {
185+
return 0.0;
186+
} else {
187+
return ::acos(static_cast<double>(x));
188+
}
172189
#else
173190
return ::acos(static_cast<double>(x));
174191
#endif
175-
}
192+
}
176193

177194
/// Reimplemented to work around MS compiler issues
178195
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)