Skip to content

Commit 2c5e9a0

Browse files
HanatoKjhenin
authored andcommitted
fix: clamp the input values of asin and acos in case of fast math on
aarch64
1 parent 1ee3ec1 commit 2c5e9a0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/colvarmodule.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
#define COLVARS_DEBUG false
1919
#endif
2020

21+
#if defined(__FAST_MATH__) && defined(__aarch64__)
22+
// NOTE: This is used for fixing https://github.com/Colvars/colvars/issues/767
23+
#define COLVARS_BOUNDED_INV_TRIGONOMETRIC_FUNC
24+
#endif
25+
2126
/*! \mainpage Main page
2227
This is the Developer's documentation for the Collective Variables module (Colvars).
2328
@@ -150,13 +155,23 @@ class colvarmodule {
150155
/// Reimplemented to work around MS compiler issues
151156
static inline real asin(real const &x)
152157
{
158+
#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));
161+
#else
153162
return ::asin(static_cast<double>(x));
163+
#endif
154164
}
155165

156166
/// Reimplemented to work around MS compiler issues
157167
static inline real acos(real const &x)
158168
{
169+
#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));
172+
#else
159173
return ::acos(static_cast<double>(x));
174+
#endif
160175
}
161176

162177
/// Reimplemented to work around MS compiler issues

src/colvarvalue.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ colvarvalue colvarvalue::dist2_grad(colvarvalue const &x2) const
674674
cvm::rvector const &v1 = this->rvector_value;
675675
cvm::rvector const &v2 = x2.rvector_value;
676676
cvm::real const cos_t = v1 * v2;
677-
return colvarvalue(2.0 * std::acos(cos_t) * -1.0 / cvm::sqrt(1.0 - cos_t * cos_t) * v2,
677+
return colvarvalue(2.0 * cvm::acos(cos_t) * -1.0 / cvm::sqrt(1.0 - cos_t * cos_t) * v2,
678678
colvarvalue::type_unit3vectorderiv);
679679
}
680680
case colvarvalue::type_quaternion:

0 commit comments

Comments
 (0)