1 //===-- Utility class to test different flavors of ilogb --------*- C++ -*-===// 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_ILOGBTEST_H 10 #define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H 11 12 #include "utils/FPUtil/FPBits.h" 13 #include "utils/FPUtil/ManipulationFunctions.h" 14 #include "utils/UnitTest/Test.h" 15 #include <math.h> 16 17 #include <limits.h> 18 19 class LlvmLibcILogbTest : public __llvm_libc::testing::Test { 20 public: 21 template <typename T> struct ILogbFunc { typedef int (*Func)(T); }; 22 23 template <typename T> 24 void testSpecialNumbers(typename ILogbFunc<T>::Func func) { 25 EXPECT_EQ(FP_ILOGB0, func(T(__llvm_libc::fputil::FPBits<T>::zero()))); 26 EXPECT_EQ(FP_ILOGB0, func(T(__llvm_libc::fputil::FPBits<T>::negZero()))); 27 28 EXPECT_EQ(FP_ILOGBNAN, 29 func(T(__llvm_libc::fputil::FPBits<T>::buildNaN(1)))); 30 31 EXPECT_EQ(INT_MAX, func(T(__llvm_libc::fputil::FPBits<T>::inf()))); 32 EXPECT_EQ(INT_MAX, func(T(__llvm_libc::fputil::FPBits<T>::negInf()))); 33 } 34 35 template <typename T> void testPowersOfTwo(typename ILogbFunc<T>::Func func) { 36 EXPECT_EQ(0, func(T(1.0))); 37 EXPECT_EQ(0, func(T(-1.0))); 38 39 EXPECT_EQ(1, func(T(2.0))); 40 EXPECT_EQ(1, func(T(-2.0))); 41 42 EXPECT_EQ(2, func(T(4.0))); 43 EXPECT_EQ(2, func(T(-4.0))); 44 45 EXPECT_EQ(3, func(T(8.0))); 46 EXPECT_EQ(3, func(-8.0)); 47 48 EXPECT_EQ(4, func(16.0)); 49 EXPECT_EQ(4, func(-16.0)); 50 51 EXPECT_EQ(5, func(32.0)); 52 EXPECT_EQ(5, func(-32.0)); 53 } 54 55 template <typename T> 56 void testSomeIntegers(typename ILogbFunc<T>::Func func) { 57 EXPECT_EQ(1, func(T(3.0))); 58 EXPECT_EQ(1, func(T(-3.0))); 59 60 EXPECT_EQ(2, func(T(7.0))); 61 EXPECT_EQ(2, func(T(-7.0))); 62 63 EXPECT_EQ(3, func(T(10.0))); 64 EXPECT_EQ(3, func(T(-10.0))); 65 66 EXPECT_EQ(4, func(T(31.0))); 67 EXPECT_EQ(4, func(-31.0)); 68 69 EXPECT_EQ(5, func(55.0)); 70 EXPECT_EQ(5, func(-55.0)); 71 } 72 73 template <typename T> 74 void testSubnormalRange(typename ILogbFunc<T>::Func func) { 75 using FPBits = __llvm_libc::fputil::FPBits<T>; 76 using UIntType = typename FPBits::UIntType; 77 constexpr UIntType count = 1000001; 78 constexpr UIntType step = 79 (FPBits::maxSubnormal - FPBits::minSubnormal) / count; 80 for (UIntType v = FPBits::minSubnormal; v <= FPBits::maxSubnormal; 81 v += step) { 82 T x = T(FPBits(v)); 83 if (isnan(x) || isinf(x) || x == 0.0) 84 continue; 85 86 int exponent; 87 __llvm_libc::fputil::frexp(x, exponent); 88 ASSERT_EQ(exponent, func(x) + 1); 89 } 90 } 91 92 template <typename T> void testNormalRange(typename ILogbFunc<T>::Func func) { 93 using FPBits = __llvm_libc::fputil::FPBits<T>; 94 using UIntType = typename FPBits::UIntType; 95 constexpr UIntType count = 1000001; 96 constexpr UIntType step = (FPBits::maxNormal - FPBits::minNormal) / count; 97 for (UIntType v = FPBits::minNormal; v <= FPBits::maxNormal; v += step) { 98 T x = T(FPBits(v)); 99 if (isnan(x) || isinf(x) || x == 0.0) 100 continue; 101 102 int exponent; 103 __llvm_libc::fputil::frexp(x, exponent); 104 ASSERT_EQ(exponent, func(x) + 1); 105 } 106 } 107 }; 108 109 #endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H 110