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