1 //===-- Utility class to test different flavors of hypot ------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H 10 #define LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H 11 12 #include "include/math.h" 13 #include "utils/FPUtil/FPBits.h" 14 #include "utils/FPUtil/Hypot.h" 15 #include "utils/FPUtil/TestHelpers.h" 16 #include "utils/MPFRWrapper/MPFRUtils.h" 17 #include "utils/UnitTest/Test.h" 18 19 namespace mpfr = __llvm_libc::testing::mpfr; 20 21 template <typename T> 22 class HypotTestTemplate : public __llvm_libc::testing::Test { 23 private: 24 using Func = T (*)(T, T); 25 using FPBits = __llvm_libc::fputil::FPBits<T>; 26 using UIntType = typename FPBits::UIntType; 27 const T nan = __llvm_libc::fputil::FPBits<T>::buildNaN(1); 28 const T inf = __llvm_libc::fputil::FPBits<T>::inf(); 29 const T negInf = __llvm_libc::fputil::FPBits<T>::negInf(); 30 const T zero = __llvm_libc::fputil::FPBits<T>::zero(); 31 const T negZero = __llvm_libc::fputil::FPBits<T>::negZero(); 32 33 public: testSpecialNumbers(Func func)34 void testSpecialNumbers(Func func) { 35 EXPECT_FP_EQ(func(inf, nan), inf); 36 EXPECT_FP_EQ(func(nan, negInf), inf); 37 EXPECT_FP_EQ(func(zero, inf), inf); 38 EXPECT_FP_EQ(func(negInf, negZero), inf); 39 40 EXPECT_FP_EQ(func(nan, nan), nan); 41 EXPECT_FP_EQ(func(nan, zero), nan); 42 EXPECT_FP_EQ(func(negZero, nan), nan); 43 44 EXPECT_FP_EQ(func(negZero, zero), zero); 45 } 46 testSubnormalRange(Func func)47 void testSubnormalRange(Func func) { 48 constexpr UIntType count = 1000001; 49 constexpr UIntType step = 50 (FPBits::maxSubnormal - FPBits::minSubnormal) / count; 51 for (UIntType v = FPBits::minSubnormal, w = FPBits::maxSubnormal; 52 v <= FPBits::maxSubnormal && w >= FPBits::minSubnormal; 53 v += step, w -= step) { 54 T x = FPBits(v), y = FPBits(w); 55 T result = func(x, y); 56 mpfr::BinaryInput<T> input{x, y}; 57 ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); 58 } 59 } 60 testNormalRange(Func func)61 void testNormalRange(Func func) { 62 constexpr UIntType count = 1000001; 63 constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; 64 for (UIntType v = FPBits::minNormal, w = FPBits::maxNormal; 65 v <= FPBits::maxNormal && w >= FPBits::minNormal; 66 v += step, w -= step) { 67 T x = FPBits(v), y = FPBits(w); 68 T result = func(x, y); 69 mpfr::BinaryInput<T> input{x, y}; 70 ASSERT_MPFR_MATCH(mpfr::Operation::Hypot, input, result, 0.5); 71 } 72 } 73 }; 74 75 #endif // LLVM_LIBC_TEST_SRC_MATH_HYPOTTEST_H 76