[libc-commits] [libc] fb69b92 - [libc][NFC] Add common template test class for sqrt, sqrtf and sqrtl.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Fri Apr 16 22:14:13 PDT 2021


Author: Siva Chandra Reddy
Date: 2021-04-17T05:13:27Z
New Revision: fb69b92c7b33b6a9b9be4278cd1e2d580f48ce8a

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

LOG: [libc][NFC] Add common template test class for sqrt, sqrtf and sqrtl.

Added: 
    libc/test/src/math/SqrtTest.h

Modified: 
    libc/test/src/math/sqrt_test.cpp
    libc/test/src/math/sqrtf_test.cpp
    libc/test/src/math/sqrtl_test.cpp

Removed: 
    


################################################################################
diff  --git a/libc/test/src/math/SqrtTest.h b/libc/test/src/math/SqrtTest.h
new file mode 100644
index 000000000000..56916a501329
--- /dev/null
+++ b/libc/test/src/math/SqrtTest.h
@@ -0,0 +1,73 @@
+//===-- Utility class to test fabs[f|l] -------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "utils/FPUtil/TestHelpers.h"
+#include "utils/MPFRWrapper/MPFRUtils.h"
+#include "utils/UnitTest/Test.h"
+
+#include <math.h>
+
+namespace mpfr = __llvm_libc::testing::mpfr;
+
+template <typename T> class SqrtTest : public __llvm_libc::testing::Test {
+
+  DECLARE_SPECIAL_CONSTANTS(T)
+
+  static constexpr UIntType HiddenBit =
+      UIntType(1) << __llvm_libc::fputil::MantissaWidth<T>::value;
+
+public:
+  typedef T (*SqrtFunc)(T);
+
+  void testSpecialNumbers(SqrtFunc func) {
+    ASSERT_FP_EQ(aNaN, func(aNaN));
+    ASSERT_FP_EQ(inf, func(inf));
+    ASSERT_FP_EQ(aNaN, func(negInf));
+    ASSERT_FP_EQ(0.0, func(0.0));
+    ASSERT_FP_EQ(-0.0, func(-0.0));
+    ASSERT_FP_EQ(aNaN, func(T(-1.0)));
+    ASSERT_FP_EQ(T(1.0), func(T(1.0)));
+    ASSERT_FP_EQ(T(2.0), func(T(4.0)));
+    ASSERT_FP_EQ(T(3.0), func(T(9.0)));
+  }
+
+  void testDenormalValues(SqrtFunc func) {
+    for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
+      FPBits denormal(T(0.0));
+      denormal.encoding.mantissa = mant;
+
+      ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, T(denormal),
+                        func(denormal), T(0.5));
+    }
+
+    constexpr UIntType count = 1'000'001;
+    constexpr UIntType step = HiddenBit / count;
+    for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+      T x = *reinterpret_cast<T *>(&v);
+      ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, func(x), 0.5);
+    }
+  }
+
+  void testNormalRange(SqrtFunc func) {
+    constexpr UIntType count = 10'000'001;
+    constexpr UIntType step = UIntType(-1) / count;
+    for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
+      T x = *reinterpret_cast<T *>(&v);
+      if (isnan(x) || (x < 0)) {
+        continue;
+      }
+      ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, func(x), 0.5);
+    }
+  }
+};
+
+#define LIST_SQRT_TESTS(T, func)                                               \
+  using LlvmLibcSqrtTest = SqrtTest<T>;                                        \
+  TEST_F(LlvmLibcSqrtTest, SpecialNumbers) { testSpecialNumbers(&func); }      \
+  TEST_F(LlvmLibcSqrtTest, DenormalValues) { testDenormalValues(&func); }      \
+  TEST_F(LlvmLibcSqrtTest, NormalRange) { testNormalRange(&func); }

diff  --git a/libc/test/src/math/sqrt_test.cpp b/libc/test/src/math/sqrt_test.cpp
index 0be62d4e41ec..237264895bbd 100644
--- a/libc/test/src/math/sqrt_test.cpp
+++ b/libc/test/src/math/sqrt_test.cpp
@@ -1,65 +1,13 @@
-//===-- Unittests for sqrt -----------------------------------------------===//
+//===-- Unittests for sqrt ------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-//===---------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
 
-#include "src/math/sqrt.h"
-#include "utils/FPUtil/FPBits.h"
-#include "utils/FPUtil/TestHelpers.h"
-#include "utils/MPFRWrapper/MPFRUtils.h"
-#include <math.h>
-
-using FPBits = __llvm_libc::fputil::FPBits<double>;
-using UIntType = typename FPBits::UIntType;
-
-namespace mpfr = __llvm_libc::testing::mpfr;
-
-constexpr UIntType HiddenBit =
-    UIntType(1) << __llvm_libc::fputil::MantissaWidth<double>::value;
-
-DECLARE_SPECIAL_CONSTANTS(double)
+#include "SqrtTest.h"
 
-TEST(LlvmLibcSqrtTest, SpecialValues) {
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(aNaN));
-  ASSERT_FP_EQ(inf, __llvm_libc::sqrt(inf));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(negInf));
-  ASSERT_FP_EQ(0.0, __llvm_libc::sqrt(0.0));
-  ASSERT_FP_EQ(-0.0, __llvm_libc::sqrt(-0.0));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrt(-1.0));
-  ASSERT_FP_EQ(1.0, __llvm_libc::sqrt(1.0));
-  ASSERT_FP_EQ(2.0, __llvm_libc::sqrt(4.0));
-  ASSERT_FP_EQ(3.0, __llvm_libc::sqrt(9.0));
-}
-
-TEST(LlvmLibcSqrtTest, DenormalValues) {
-  for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
-    FPBits denormal(0.0);
-    denormal.encoding.mantissa = mant;
-
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, double(denormal),
-                      __llvm_libc::sqrt(denormal), 0.5);
-  }
-
-  constexpr UIntType count = 1'000'001;
-  constexpr UIntType step = HiddenBit / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    double x = *reinterpret_cast<double *>(&v);
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
-  }
-}
-
-TEST(LlvmLibcSqrtTest, InDoubleRange) {
-  constexpr UIntType count = 10'000'001;
-  constexpr UIntType step = UIntType(-1) / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    double x = *reinterpret_cast<double *>(&v);
-    if (isnan(x) || (x < 0)) {
-      continue;
-    }
+#include "src/math/sqrt.h"
 
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrt(x), 0.5);
-  }
-}
+LIST_SQRT_TESTS(double, __llvm_libc::sqrt)

diff  --git a/libc/test/src/math/sqrtf_test.cpp b/libc/test/src/math/sqrtf_test.cpp
index 91f4a91633fc..c7681d01569a 100644
--- a/libc/test/src/math/sqrtf_test.cpp
+++ b/libc/test/src/math/sqrtf_test.cpp
@@ -1,65 +1,13 @@
-//===-- Unittests for sqrtf -----------------------------------------------===//
+//===-- Unittests for sqrtf------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-//===---------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
 
-#include "src/math/sqrtf.h"
-#include "utils/FPUtil/FPBits.h"
-#include "utils/FPUtil/TestHelpers.h"
-#include "utils/MPFRWrapper/MPFRUtils.h"
-#include <math.h>
-
-using FPBits = __llvm_libc::fputil::FPBits<float>;
-using UIntType = typename FPBits::UIntType;
-
-namespace mpfr = __llvm_libc::testing::mpfr;
-
-constexpr UIntType HiddenBit =
-    UIntType(1) << __llvm_libc::fputil::MantissaWidth<float>::value;
-
-DECLARE_SPECIAL_CONSTANTS(float)
+#include "SqrtTest.h"
 
-TEST(LlvmLibcSqrtfTest, SpecialValues) {
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(aNaN));
-  ASSERT_FP_EQ(inf, __llvm_libc::sqrtf(inf));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(negInf));
-  ASSERT_FP_EQ(0.0f, __llvm_libc::sqrtf(0.0f));
-  ASSERT_FP_EQ(-0.0f, __llvm_libc::sqrtf(-0.0f));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtf(-1.0f));
-  ASSERT_FP_EQ(1.0f, __llvm_libc::sqrtf(1.0f));
-  ASSERT_FP_EQ(2.0f, __llvm_libc::sqrtf(4.0f));
-  ASSERT_FP_EQ(3.0f, __llvm_libc::sqrtf(9.0f));
-}
-
-TEST(LlvmLibcSqrtfTest, DenormalValues) {
-  for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
-    FPBits denormal(0.0f);
-    denormal.encoding.mantissa = mant;
-
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, float(denormal),
-                      __llvm_libc::sqrtf(denormal), 0.5);
-  }
-
-  constexpr UIntType count = 1'000'001;
-  constexpr UIntType step = HiddenBit / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    float x = *reinterpret_cast<float *>(&v);
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
-  }
-}
-
-TEST(LlvmLibcSqrtfTest, InFloatRange) {
-  constexpr UIntType count = 10'000'001;
-  constexpr UIntType step = UIntType(-1) / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    float x = *reinterpret_cast<float *>(&v);
-    if (isnan(x) || (x < 0)) {
-      continue;
-    }
+#include "src/math/sqrtf.h"
 
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtf(x), 0.5);
-  }
-}
+LIST_SQRT_TESTS(float, __llvm_libc::sqrtf)

diff  --git a/libc/test/src/math/sqrtl_test.cpp b/libc/test/src/math/sqrtl_test.cpp
index 1701bfdbc67b..c48ebb08444d 100644
--- a/libc/test/src/math/sqrtl_test.cpp
+++ b/libc/test/src/math/sqrtl_test.cpp
@@ -1,65 +1,13 @@
-//===-- Unittests for sqrtl ----------------------------------------------===//
+//===-- Unittests for sqrtl------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-//===---------------------------------------------------------------------===//
+//===----------------------------------------------------------------------===//
 
-#include "src/math/sqrtl.h"
-#include "utils/FPUtil/FPBits.h"
-#include "utils/FPUtil/TestHelpers.h"
-#include "utils/MPFRWrapper/MPFRUtils.h"
-#include <math.h>
-
-using FPBits = __llvm_libc::fputil::FPBits<long double>;
-using UIntType = typename FPBits::UIntType;
-
-namespace mpfr = __llvm_libc::testing::mpfr;
-
-constexpr UIntType HiddenBit =
-    UIntType(1) << __llvm_libc::fputil::MantissaWidth<long double>::value;
-
-DECLARE_SPECIAL_CONSTANTS(long double)
+#include "SqrtTest.h"
 
-TEST(LlvmLibcSqrtlTest, SpecialValues) {
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtl(aNaN));
-  ASSERT_FP_EQ(inf, __llvm_libc::sqrtl(inf));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtl(negInf));
-  ASSERT_FP_EQ(0.0L, __llvm_libc::sqrtl(0.0L));
-  ASSERT_FP_EQ(-0.0L, __llvm_libc::sqrtl(-0.0L));
-  ASSERT_FP_EQ(aNaN, __llvm_libc::sqrtl(-1.0L));
-  ASSERT_FP_EQ(1.0L, __llvm_libc::sqrtl(1.0L));
-  ASSERT_FP_EQ(2.0L, __llvm_libc::sqrtl(4.0L));
-  ASSERT_FP_EQ(3.0L, __llvm_libc::sqrtl(9.0L));
-}
-
-TEST(LlvmLibcSqrtlTest, DenormalValues) {
-  for (UIntType mant = 1; mant < HiddenBit; mant <<= 1) {
-    FPBits denormal(0.0L);
-    denormal.encoding.mantissa = mant;
-
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, static_cast<long double>(denormal),
-                      __llvm_libc::sqrtl(denormal), 0.5);
-  }
-
-  constexpr UIntType count = 1'000'001;
-  constexpr UIntType step = HiddenBit / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    long double x = *reinterpret_cast<long double *>(&v);
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
-  }
-}
-
-TEST(LlvmLibcSqrtlTest, InLongDoubleRange) {
-  constexpr UIntType count = 10'000'001;
-  constexpr UIntType step = UIntType(-1) / count;
-  for (UIntType i = 0, v = 0; i <= count; ++i, v += step) {
-    long double x = *reinterpret_cast<long double *>(&v);
-    if (isnan(x) || (x < 0)) {
-      continue;
-    }
+#include "src/math/sqrtl.h"
 
-    ASSERT_MPFR_MATCH(mpfr::Operation::Sqrt, x, __llvm_libc::sqrtl(x), 0.5);
-  }
-}
+LIST_SQRT_TESTS(long double, __llvm_libc::sqrtl)


        


More information about the libc-commits mailing list