[flang-commits] [flang] e6fdbd1 - [flang][runtime] Add special-case faster path to real MOD/MODULO (#79625)

via flang-commits flang-commits at lists.llvm.org
Mon Jan 29 14:13:32 PST 2024


Author: Peter Klausler
Date: 2024-01-29T14:13:27-08:00
New Revision: e6fdbd17769d626c66e087b0fd9bbda0ea69194d

URL: https://github.com/llvm/llvm-project/commit/e6fdbd17769d626c66e087b0fd9bbda0ea69194d
DIFF: https://github.com/llvm/llvm-project/commit/e6fdbd17769d626c66e087b0fd9bbda0ea69194d.diff

LOG: [flang][runtime] Add special-case faster path to real MOD/MODULO (#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.

Added: 
    

Modified: 
    flang/runtime/numeric.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/numeric.cpp b/flang/runtime/numeric.cpp
index df73502bee5fed..8e512aff1ea1d3 100644
--- a/flang/runtime/numeric.cpp
+++ b/flang/runtime/numeric.cpp
@@ -144,7 +144,18 @@ 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> ||
+  }
+  if (auto aInt{static_cast<std::int64_t>(a)}; a == aInt) {
+    if (auto pInt{static_cast<std::int64_t>(p)}; p == pInt) {
+      // Fast exact case for integer operands
+      auto mod{aInt - (aInt / pInt) * pInt};
+      if (IS_MODULO && (aInt > 0) != (pInt > 0)) {
+        mod += pInt;
+      }
+      return static_cast<T>(mod);
+    }
+  }
+  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.


        


More information about the flang-commits mailing list