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 "src/__support/FPUtil/FPBits.h"
13 #include "src/__support/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>
test_special_numbers(typename ILogbFunc<T>::Func func)24   void test_special_numbers(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>::neg_zero())));
27 
28     EXPECT_EQ(FP_ILOGBNAN,
29               func(T(__llvm_libc::fputil::FPBits<T>::build_nan(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>::neg_inf())));
33   }
34 
35   template <typename T>
test_powers_of_two(typename ILogbFunc<T>::Func func)36   void test_powers_of_two(typename ILogbFunc<T>::Func func) {
37     EXPECT_EQ(0, func(T(1.0)));
38     EXPECT_EQ(0, func(T(-1.0)));
39 
40     EXPECT_EQ(1, func(T(2.0)));
41     EXPECT_EQ(1, func(T(-2.0)));
42 
43     EXPECT_EQ(2, func(T(4.0)));
44     EXPECT_EQ(2, func(T(-4.0)));
45 
46     EXPECT_EQ(3, func(T(8.0)));
47     EXPECT_EQ(3, func(-8.0));
48 
49     EXPECT_EQ(4, func(16.0));
50     EXPECT_EQ(4, func(-16.0));
51 
52     EXPECT_EQ(5, func(32.0));
53     EXPECT_EQ(5, func(-32.0));
54   }
55 
56   template <typename T>
test_some_integers(typename ILogbFunc<T>::Func func)57   void test_some_integers(typename ILogbFunc<T>::Func func) {
58     EXPECT_EQ(1, func(T(3.0)));
59     EXPECT_EQ(1, func(T(-3.0)));
60 
61     EXPECT_EQ(2, func(T(7.0)));
62     EXPECT_EQ(2, func(T(-7.0)));
63 
64     EXPECT_EQ(3, func(T(10.0)));
65     EXPECT_EQ(3, func(T(-10.0)));
66 
67     EXPECT_EQ(4, func(T(31.0)));
68     EXPECT_EQ(4, func(-31.0));
69 
70     EXPECT_EQ(5, func(55.0));
71     EXPECT_EQ(5, func(-55.0));
72   }
73 
74   template <typename T>
test_subnormal_range(typename ILogbFunc<T>::Func func)75   void test_subnormal_range(typename ILogbFunc<T>::Func func) {
76     using FPBits = __llvm_libc::fputil::FPBits<T>;
77     using UIntType = typename FPBits::UIntType;
78     constexpr UIntType COUNT = 1000001;
79     constexpr UIntType STEP =
80         (FPBits::MAX_SUBNORMAL - FPBits::MIN_SUBNORMAL) / COUNT;
81     for (UIntType v = FPBits::MIN_SUBNORMAL; v <= FPBits::MAX_SUBNORMAL;
82          v += STEP) {
83       T x = T(FPBits(v));
84       if (isnan(x) || isinf(x) || x == 0.0)
85         continue;
86 
87       int exponent;
88       __llvm_libc::fputil::frexp(x, exponent);
89       ASSERT_EQ(exponent, func(x) + 1);
90     }
91   }
92 
93   template <typename T>
test_normal_range(typename ILogbFunc<T>::Func func)94   void test_normal_range(typename ILogbFunc<T>::Func func) {
95     using FPBits = __llvm_libc::fputil::FPBits<T>;
96     using UIntType = typename FPBits::UIntType;
97     constexpr UIntType COUNT = 1000001;
98     constexpr UIntType STEP = (FPBits::MAX_NORMAL - FPBits::MIN_NORMAL) / COUNT;
99     for (UIntType v = FPBits::MIN_NORMAL; v <= FPBits::MAX_NORMAL; v += STEP) {
100       T x = T(FPBits(v));
101       if (isnan(x) || isinf(x) || x == 0.0)
102         continue;
103 
104       int exponent;
105       __llvm_libc::fputil::frexp(x, exponent);
106       ASSERT_EQ(exponent, func(x) + 1);
107     }
108   }
109 };
110 
111 #endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
112