[libc-commits] [PATCH] D114726: [libc] Fix a bug with negative inputs in hypot implementation.

Tue Ly via Phabricator via libc-commits libc-commits at lists.llvm.org
Mon Nov 29 10:36:44 PST 2021


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

Fix a bug with negative inputs in hypot implementation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D114726

Files:
  libc/src/__support/FPUtil/Hypot.h
  libc/test/src/math/HypotTest.h


Index: libc/test/src/math/HypotTest.h
===================================================================
--- libc/test/src/math/HypotTest.h
+++ libc/test/src/math/HypotTest.h
@@ -49,26 +49,44 @@
     constexpr UIntType count = 1000001;
     constexpr UIntType step =
         (FPBits::maxSubnormal - FPBits::minSubnormal) / count;
-    for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal;
-         v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal;
-         v += step, w -= step) {
-      T x = T(FPBits(v)), y = T(FPBits(w));
-      T result = func(x, y);
-      mpfr::BinaryInput<T> input{x, y};
-      ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
+    for (int signs = 0; signs < 4; ++signs) {
+      for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal;
+           v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal;
+           v += step, w -= step) {
+        T x = T(FPBits(v)), y = T(FPBits(w));
+        if (signs % 2 == 1) {
+          x = -x;
+        }
+        if (signs >= 2) {
+          y = -y;
+        }
+
+        T result = func(x, y);
+        mpfr::BinaryInput<T> input{x, y};
+        ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
+      }
     }
   }
 
   void testNormalRange(Func func) {
     constexpr UIntType count = 1000001;
     constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count;
-    for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal;
-         v <= FPBits::maxNormal && w >= FPBits::minNormal;
-         v += step, w -= step) {
-      T x = T(FPBits(v)), y = T(FPBits(w));
-      T result = func(x, y);
-      mpfr::BinaryInput<T> input{x, y};
-      ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
+    for (int signs = 0; signs < 4; ++signs) {
+      for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal;
+           v <= FPBits::maxNormal && w >= FPBits::minNormal;
+           v += step, w -= step) {
+        T x = T(FPBits(v)), y = T(FPBits(w));
+        if (signs % 2 == 1) {
+          x = -x;
+        }
+        if (signs >= 2) {
+          y = -y;
+        }
+
+        T result = func(x, y);
+        mpfr::BinaryInput<T> input{x, y};
+        ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5);
+      }
     }
   }
 };
Index: libc/src/__support/FPUtil/Hypot.h
===================================================================
--- libc/src/__support/FPUtil/Hypot.h
+++ libc/src/__support/FPUtil/Hypot.h
@@ -150,7 +150,7 @@
     return abs(y);
   }
 
-  if (x >= y) {
+  if (abs(x) >= abs(y)) {
     a_exp = x_bits.getUnbiasedExponent();
     a_mant = x_bits.getMantissa();
     b_exp = y_bits.getUnbiasedExponent();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114726.390410.patch
Type: text/x-patch
Size: 2702 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20211129/189f3688/attachment.bin>


More information about the libc-commits mailing list