[libc-commits] [PATCH] D119843: [libc] Improve performance of generic hypot when the exponent difference is sufficiently large.

Tue Ly via Phabricator via libc-commits libc-commits at lists.llvm.org
Tue Feb 15 07:08:12 PST 2022


lntue created this revision.
lntue added reviewers: michaelrj, sivachandra.
Herald added subscribers: ecnelises, tschuett.
Herald added a project: libc-project.
lntue requested review of this revision.

Simplify the logic when the exponent difference is at least MantissaLength + 2, while still maintaining correct rounding for all rounding modes.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D119843

Files:
  libc/src/__support/FPUtil/Hypot.h


Index: libc/src/__support/FPUtil/Hypot.h
===================================================================
--- libc/src/__support/FPUtil/Hypot.h
+++ libc/src/__support/FPUtil/Hypot.h
@@ -143,38 +143,28 @@
     return y;
   }
 
+  uint16_t x_exp = x_bits.get_unbiased_exponent();
+  uint16_t y_exp = y_bits.get_unbiased_exponent();
+  uint16_t exp_diff = (x_exp > y_exp) ? (x_exp - y_exp) : (y_exp - x_exp);
+
+  if ((exp_diff >= MantissaWidth<T>::VALUE + 2) || (x == 0) || (y == 0)) {
+    return abs(x) + abs(y);
+  }
+
   uint16_t a_exp, b_exp, out_exp;
   UIntType a_mant, b_mant;
   DUIntType a_mant_sq, b_mant_sq;
   bool sticky_bits;
 
-  if ((x_bits.get_unbiased_exponent() >=
-       y_bits.get_unbiased_exponent() + MantissaWidth<T>::VALUE + 2) ||
-      (y == 0)) {
-    if ((y != 0) && (get_round() == FE_UPWARD)) {
-      UIntType out_bits = FPBits_t(abs(x)).uintval();
-      return T(FPBits_t(++out_bits));
-    }
-    return abs(x);
-  } else if ((y_bits.get_unbiased_exponent() >=
-              x_bits.get_unbiased_exponent() + MantissaWidth<T>::VALUE + 2) ||
-             (x == 0)) {
-    if ((x != 0) && (get_round() == FE_UPWARD)) {
-      UIntType out_bits = FPBits_t(abs(y)).uintval();
-      return T(FPBits_t(++out_bits));
-    }
-    return abs(y);
-  }
-
   if (abs(x) >= abs(y)) {
-    a_exp = x_bits.get_unbiased_exponent();
+    a_exp = x_exp;
     a_mant = x_bits.get_mantissa();
-    b_exp = y_bits.get_unbiased_exponent();
+    b_exp = y_exp;
     b_mant = y_bits.get_mantissa();
   } else {
-    a_exp = y_bits.get_unbiased_exponent();
+    a_exp = y_exp;
     a_mant = y_bits.get_mantissa();
-    b_exp = x_bits.get_unbiased_exponent();
+    b_exp = x_exp;
     b_mant = x_bits.get_mantissa();
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119843.408863.patch
Type: text/x-patch
Size: 1738 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220215/8c1be239/attachment.bin>


More information about the libc-commits mailing list