1 //===-- Utility class to test logb[f|l] -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "src/__support/FPUtil/ManipulationFunctions.h"
10 #include "test/UnitTest/FEnvSafeTest.h"
11 #include "test/UnitTest/FPMatcher.h"
12 #include "test/UnitTest/Test.h"
13 #include "utils/MPFRWrapper/MPFRUtils.h"
15 #include "hdr/math_macros.h"
17 namespace mpfr
= LIBC_NAMESPACE::testing::mpfr
;
20 class LogbTest
: public LIBC_NAMESPACE::testing::FEnvSafeTest
{
22 DECLARE_SPECIAL_CONSTANTS(T
)
24 static constexpr StorageType HIDDEN_BIT
=
25 StorageType(1) << LIBC_NAMESPACE::fputil::FPBits
<T
>::FRACTION_LEN
;
28 typedef T (*LogbFunc
)(T
);
30 void testSpecialNumbers(LogbFunc func
) {
31 ASSERT_FP_EQ(aNaN
, func(aNaN
));
32 ASSERT_FP_EQ(inf
, func(inf
));
33 ASSERT_FP_EQ(inf
, func(neg_inf
));
34 ASSERT_FP_EQ(neg_inf
, func(0.0));
35 ASSERT_FP_EQ(neg_inf
, func(-0.0));
38 void testPowersOfTwo(LogbFunc func
) {
39 EXPECT_FP_EQ(T(0.0), func(T(1.0)));
40 EXPECT_FP_EQ(T(0.0), func(T(-1.0)));
42 EXPECT_FP_EQ(T(1.0), func(T(2.0)));
43 EXPECT_FP_EQ(T(1.0), func(T(-2.0)));
45 EXPECT_FP_EQ(T(2.0), func(T(4.0)));
46 EXPECT_FP_EQ(T(2.0), func(T(-4.0)));
48 EXPECT_FP_EQ(T(3.0), func(T(8.0)));
49 EXPECT_FP_EQ(T(3.0), func(T(-8.0)));
51 EXPECT_FP_EQ(T(4.0), func(T(16.0)));
52 EXPECT_FP_EQ(T(4.0), func(T(-16.0)));
54 EXPECT_FP_EQ(T(5.0), func(T(32.0)));
55 EXPECT_FP_EQ(T(5.0), func(T(-32.0)));
58 void testSomeIntegers(LogbFunc func
) {
59 EXPECT_FP_EQ(T(1.0), func(T(3.0)));
60 EXPECT_FP_EQ(T(1.0), func(T(-3.0)));
62 EXPECT_FP_EQ(T(2.0), func(T(7.0)));
63 EXPECT_FP_EQ(T(2.0), func(T(-7.0)));
65 EXPECT_FP_EQ(T(3.0), func(T(10.0)));
66 EXPECT_FP_EQ(T(3.0), func(T(-10.0)));
68 EXPECT_FP_EQ(T(4.0), func(T(31.0)));
69 EXPECT_FP_EQ(T(4.0), func(T(-31.0)));
71 EXPECT_FP_EQ(T(5.0), func(T(55.0)));
72 EXPECT_FP_EQ(T(5.0), func(T(-55.0)));
75 void testRange(LogbFunc func
) {
76 using StorageType
= typename
FPBits::StorageType
;
77 constexpr StorageType COUNT
= 100'000;
78 constexpr StorageType STEP
= STORAGE_MAX
/ COUNT
;
79 for (StorageType i
= 0, v
= 0; i
<= COUNT
; ++i
, v
+= STEP
) {
80 T x
= FPBits(v
).get_val();
81 if (FPBits(v
).is_nan() || FPBits(v
).is_inf() || x
== 0.0l)
85 LIBC_NAMESPACE::fputil::frexp(x
, exponent
);
86 ASSERT_FP_EQ(T(exponent
), func(x
) + T(1.0));
91 #define LIST_LOGB_TESTS(T, func) \
92 using LlvmLibcLogbTest = LogbTest<T>; \
93 TEST_F(LlvmLibcLogbTest, SpecialNumbers) { testSpecialNumbers(&func); } \
94 TEST_F(LlvmLibcLogbTest, PowersOfTwo) { testPowersOfTwo(&func); } \
95 TEST_F(LlvmLibcLogbTest, SomeIntegers) { testSomeIntegers(&func); } \
96 TEST_F(LlvmLibcLogbTest, InRange) { testRange(&func); }