Skip to content

Commit

Permalink
[flang][runtime] Use std::fmod for most MOD/MODULO
Browse files Browse the repository at this point in the history
The new accurate algorithm for real MOD and MODULO in the
runtime is not as fast as std::fmod(), which is also
accurate.  So use std::fmod() for those floating-point
types that it supports.

Fixes llvm#78641.
  • Loading branch information
klausler committed Jan 19, 2024
1 parent 345c1ea commit 41f5c93
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions flang/runtime/numeric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ inline RT_API_ATTRS T RealMod(
return std::numeric_limits<T>::quiet_NaN();
} else if (std::isinf(p)) {
return a;
} else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double> ||
std::is_same_v<T, long double>) {
// std::fmod() semantics on signed operands seems to match
// the requirements of MOD(). MODULO() needs adjustment.
T result{std::fmod(a, p)};
if constexpr (IS_MODULO) {
if ((a < 0) != (p < 0)) {
if (result == 0.) {
result = -result;
} else {
result += p;
}
}
}
return result;
} else {
// The standard defines MOD(a,p)=a-AINT(a/p)*p and
// MODULO(a,p)=a-FLOOR(a/p)*p, but those definitions lose
Expand Down

0 comments on commit 41f5c93

Please sign in to comment.