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 "hdr/math_macros.h" 13 #include "src/__support/CPP/limits.h" // INT_MAX 14 #include "src/__support/FPUtil/FPBits.h" 15 #include "src/__support/FPUtil/ManipulationFunctions.h" 16 #include "test/UnitTest/FEnvSafeTest.h" 17 #include "test/UnitTest/Test.h" 18 19 using LIBC_NAMESPACE::Sign; 20 21 class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest { 22 public: 23 template <typename T> struct ILogbFunc { 24 typedef int (*Func)(T); 25 }; 26 27 template <typename T> test_special_numbers(typename ILogbFunc<T>::Func func)28 void test_special_numbers(typename ILogbFunc<T>::Func func) { 29 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; 30 31 EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::POS).get_val())); 32 EXPECT_EQ(FP_ILOGB0, func(FPBits::zero(Sign::NEG).get_val())); 33 EXPECT_EQ(FP_ILOGBNAN, func(FPBits::quiet_nan().get_val())); 34 EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::POS).get_val())); 35 EXPECT_EQ(INT_MAX, func(FPBits::inf(Sign::NEG).get_val())); 36 } 37 38 template <typename T> test_powers_of_two(typename ILogbFunc<T>::Func func)39 void test_powers_of_two(typename ILogbFunc<T>::Func func) { 40 EXPECT_EQ(0, func(T(1.0))); 41 EXPECT_EQ(0, func(T(-1.0))); 42 43 EXPECT_EQ(1, func(T(2.0))); 44 EXPECT_EQ(1, func(T(-2.0))); 45 46 EXPECT_EQ(2, func(T(4.0))); 47 EXPECT_EQ(2, func(T(-4.0))); 48 49 EXPECT_EQ(3, func(T(8.0))); 50 EXPECT_EQ(3, func(-8.0)); 51 52 EXPECT_EQ(4, func(16.0)); 53 EXPECT_EQ(4, func(-16.0)); 54 55 EXPECT_EQ(5, func(32.0)); 56 EXPECT_EQ(5, func(-32.0)); 57 } 58 59 template <typename T> test_some_integers(typename ILogbFunc<T>::Func func)60 void test_some_integers(typename ILogbFunc<T>::Func func) { 61 EXPECT_EQ(1, func(T(3.0))); 62 EXPECT_EQ(1, func(T(-3.0))); 63 64 EXPECT_EQ(2, func(T(7.0))); 65 EXPECT_EQ(2, func(T(-7.0))); 66 67 EXPECT_EQ(3, func(T(10.0))); 68 EXPECT_EQ(3, func(T(-10.0))); 69 70 EXPECT_EQ(4, func(T(31.0))); 71 EXPECT_EQ(4, func(-31.0)); 72 73 EXPECT_EQ(5, func(55.0)); 74 EXPECT_EQ(5, func(-55.0)); 75 } 76 77 template <typename T> test_subnormal_range(typename ILogbFunc<T>::Func func)78 void test_subnormal_range(typename ILogbFunc<T>::Func func) { 79 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; 80 using StorageType = typename FPBits::StorageType; 81 constexpr StorageType MIN_SUBNORMAL = FPBits::min_subnormal().uintval(); 82 constexpr StorageType MAX_SUBNORMAL = FPBits::max_subnormal().uintval(); 83 constexpr StorageType COUNT = 10'001; 84 constexpr StorageType STEP = (MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT; 85 for (StorageType v = MIN_SUBNORMAL; v <= MAX_SUBNORMAL; v += STEP) { 86 T x = FPBits(v).get_val(); 87 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0) 88 continue; 89 90 int exponent; 91 LIBC_NAMESPACE::fputil::frexp(x, exponent); 92 ASSERT_EQ(exponent, func(x) + 1); 93 } 94 } 95 96 template <typename T> 97 void test_normal_range(typename ILogbFunc<T>::Func func) { 98 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>; 99 using StorageType = typename FPBits::StorageType; 100 constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval(); 101 constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval(); 102 constexpr StorageType COUNT = 10'001; 103 constexpr StorageType STEP = (MAX_NORMAL - MIN_NORMAL) / COUNT; 104 for (StorageType v = MIN_NORMAL; v <= MAX_NORMAL; v += STEP) { 105 T x = FPBits(v).get_val(); 106 if (FPBits(v).is_nan() || FPBits(v).is_inf() || x == 0.0) 107 continue; 108 109 int exponent; 110 LIBC_NAMESPACE::fputil::frexp(x, exponent); 111 ASSERT_EQ(exponent, func(x) + 1); 112 } 113 } 114 }; 115 116 #endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H 117