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