Skip to content

Commit e6fdbd1

Browse files
authored
[flang][runtime] Add special-case faster path to real MOD/MODULO (llvm#79625)
When a real-valued reference to the MOD/MODULO intrinsic functions has operands that are exact integers, use the fast exact integer algorithm rather than calling std::fmod.
1 parent 8dbedf4 commit e6fdbd1

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

flang/runtime/numeric.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,18 @@ inline RT_API_ATTRS T RealMod(
144144
return std::numeric_limits<T>::quiet_NaN();
145145
} else if (std::isinf(p)) {
146146
return a;
147-
} else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double> ||
147+
}
148+
if (auto aInt{static_cast<std::int64_t>(a)}; a == aInt) {
149+
if (auto pInt{static_cast<std::int64_t>(p)}; p == pInt) {
150+
// Fast exact case for integer operands
151+
auto mod{aInt - (aInt / pInt) * pInt};
152+
if (IS_MODULO && (aInt > 0) != (pInt > 0)) {
153+
mod += pInt;
154+
}
155+
return static_cast<T>(mod);
156+
}
157+
}
158+
if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double> ||
148159
std::is_same_v<T, long double>) {
149160
// std::fmod() semantics on signed operands seems to match
150161
// the requirements of MOD(). MODULO() needs adjustment.

0 commit comments

Comments
 (0)