Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libc / test / src / math / ILogbTest.h
blobe51a5d7a2544cdc3877c8a93cb7c49d10563ddfd
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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
10 #define LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H
12 #include "src/__support/FPUtil/FPBits.h"
13 #include "src/__support/FPUtil/ManipulationFunctions.h"
14 #include "test/UnitTest/Test.h"
15 #include <math.h>
17 #include <limits.h>
19 class LlvmLibcILogbTest : public LIBC_NAMESPACE::testing::Test {
20 public:
21 template <typename T> struct ILogbFunc {
22 typedef int (*Func)(T);
25 template <typename T>
26 void test_special_numbers(typename ILogbFunc<T>::Func func) {
27 EXPECT_EQ(FP_ILOGB0, func(T(LIBC_NAMESPACE::fputil::FPBits<T>::zero())));
28 EXPECT_EQ(FP_ILOGB0,
29 func(T(LIBC_NAMESPACE::fputil::FPBits<T>::neg_zero())));
31 EXPECT_EQ(FP_ILOGBNAN,
32 func(T(LIBC_NAMESPACE::fputil::FPBits<T>::build_quiet_nan(1))));
34 EXPECT_EQ(INT_MAX, func(T(LIBC_NAMESPACE::fputil::FPBits<T>::inf())));
35 EXPECT_EQ(INT_MAX, func(T(LIBC_NAMESPACE::fputil::FPBits<T>::neg_inf())));
38 template <typename T>
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)));
43 EXPECT_EQ(1, func(T(2.0)));
44 EXPECT_EQ(1, func(T(-2.0)));
46 EXPECT_EQ(2, func(T(4.0)));
47 EXPECT_EQ(2, func(T(-4.0)));
49 EXPECT_EQ(3, func(T(8.0)));
50 EXPECT_EQ(3, func(-8.0));
52 EXPECT_EQ(4, func(16.0));
53 EXPECT_EQ(4, func(-16.0));
55 EXPECT_EQ(5, func(32.0));
56 EXPECT_EQ(5, func(-32.0));
59 template <typename T>
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)));
64 EXPECT_EQ(2, func(T(7.0)));
65 EXPECT_EQ(2, func(T(-7.0)));
67 EXPECT_EQ(3, func(T(10.0)));
68 EXPECT_EQ(3, func(T(-10.0)));
70 EXPECT_EQ(4, func(T(31.0)));
71 EXPECT_EQ(4, func(-31.0));
73 EXPECT_EQ(5, func(55.0));
74 EXPECT_EQ(5, func(-55.0));
77 template <typename T>
78 void test_subnormal_range(typename ILogbFunc<T>::Func func) {
79 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
80 using UIntType = typename FPBits::UIntType;
81 constexpr UIntType COUNT = 10'001;
82 constexpr UIntType STEP =
83 (UIntType(FPBits::MAX_SUBNORMAL) - UIntType(FPBits::MIN_SUBNORMAL)) /
84 COUNT;
85 for (UIntType v = FPBits::MIN_SUBNORMAL; v <= FPBits::MAX_SUBNORMAL;
86 v += STEP) {
87 T x = T(FPBits(v));
88 if (isnan(x) || isinf(x) || x == 0.0)
89 continue;
91 int exponent;
92 LIBC_NAMESPACE::fputil::frexp(x, exponent);
93 ASSERT_EQ(exponent, func(x) + 1);
97 template <typename T>
98 void test_normal_range(typename ILogbFunc<T>::Func func) {
99 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
100 using UIntType = typename FPBits::UIntType;
101 constexpr UIntType COUNT = 10'001;
102 constexpr UIntType STEP =
103 (UIntType(FPBits::MAX_NORMAL) - UIntType(FPBits::MIN_NORMAL)) / COUNT;
104 for (UIntType v = FPBits::MIN_NORMAL; v <= FPBits::MAX_NORMAL; v += STEP) {
105 T x = T(FPBits(v));
106 if (isnan(x) || isinf(x) || x == 0.0)
107 continue;
109 int exponent;
110 LIBC_NAMESPACE::fputil::frexp(x, exponent);
111 ASSERT_EQ(exponent, func(x) + 1);
116 #endif // LLVM_LIBC_TEST_SRC_MATH_ILOGBTEST_H