Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][runtime] Use std::fmod for most MOD/MODULO #78745

Merged
merged 1 commit into from
Jan 25, 2024

Conversation

klausler
Copy link
Contributor

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 #78641.

@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Jan 19, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 19, 2024

@llvm/pr-subscribers-flang-runtime

Author: Peter Klausler (klausler)

Changes

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 #78641.


Full diff: https://github.com/llvm/llvm-project/pull/78745.diff

1 Files Affected:

  • (modified) flang/runtime/numeric.cpp (+25-17)
diff --git a/flang/runtime/numeric.cpp b/flang/runtime/numeric.cpp
index 3f6f553e7bb554d..ad6b0e854522496 100644
--- a/flang/runtime/numeric.cpp
+++ b/flang/runtime/numeric.cpp
@@ -145,25 +145,33 @@ inline RT_API_ATTRS T RealMod(
   } else if (std::isinf(p)) {
     return a;
   } 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
-    // precision badly due to cancellation when ABS(a) is
-    // much larger than ABS(p).
-    // Insights:
-    //  - MOD(a,p)=MOD(a-n*p,p) when a>0, p>0, integer n>0, and a>=n*p
-    //  - when n is a power of two, n*p is exact
-    //  - as a>=n*p, a-n*p does not round.
-    // So repeatedly reduce a by all n*p in decreasing order of n;
-    // what's left is the desired remainder.  This is basically
-    // the same algorithm as arbitrary precision binary long division,
-    // discarding the quotient.
     T tmp{std::abs(a)};
     T pAbs{std::abs(p)};
-    for (T adj{SetExponent(pAbs, Exponent<int>(tmp))}; tmp >= pAbs; adj /= 2) {
-      if (tmp >= adj) {
-        tmp -= adj;
-        if (tmp == 0) {
-          break;
+    if (tmp < pAbs) {
+    } else if constexpr (std::is_same_v<T, float> ||
+        std::is_same_v<T, double> || std::is_same_v<T, long double>) {
+      tmp = std::fmod(tmp, pAbs);
+    } 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
+      // precision badly due to cancellation when ABS(a) is
+      // much larger than ABS(p) and the values are not
+      // integers
+      // Insights:
+      //  - MOD(a,p)=MOD(a-n*p,p) when a>0, p>0, integer n>0, and a>=n*p
+      //  - when n is a power of two, n*p is exact
+      //  - as a>=n*p, a-n*p does not round.
+      // So repeatedly reduce a by all n*p in decreasing order of n;
+      // what's left is the desired remainder.  This is basically
+      // the same algorithm as arbitrary precision binary long division,
+      // discarding the quotient.
+      for (T adj{SetExponent(pAbs, Exponent<int>(tmp))}; tmp >= pAbs;
+           adj /= 2) {
+        if (tmp >= adj) {
+          tmp -= adj;
+          if (tmp == 0) {
+            break;
+          }
         }
       }
     }

if (tmp < pAbs) {
} else if constexpr (std::is_same_v<T, float> ||
std::is_same_v<T, double> || std::is_same_v<T, long double>) {
tmp = std::fmod(tmp, pAbs);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should just use fmod(a, p) for these types, and avoid the abs computations, tmp < pAbs evaluation, and the tmp = -tmp fixup below. I think this may remove some overhead for the likely cases (e.g. when a >= p). What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting the result right for both MOD and MODULO when either a or p or both are negative is tricky. Will try.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Peter! It looks good to me, though, there might be some unneeded overhead (see my comment).
I suggest merging this, and I will check out performance after. It looks like using fmod is the standard way in other compilers as well.

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.
Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@klausler klausler merged commit e8a5010 into llvm:main Jan 25, 2024
4 checks passed
@klausler klausler deleted the slowmod branch January 25, 2024 23:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] MOD 7x performance regression
3 participants