1//===-- Utility class to test logb[f|l] -------------------------*- 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#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"
14
15#include "hdr/math_macros.h"
16
17namespace mpfr = LIBC_NAMESPACE::testing::mpfr;
18
19template <typename T>
20class LogbTest : public LIBC_NAMESPACE::testing::FEnvSafeTest {
21
22 DECLARE_SPECIAL_CONSTANTS(T)
23
24 static constexpr StorageType HIDDEN_BIT =
25 StorageType(1) << LIBC_NAMESPACE::fputil::FPBits<T>::FRACTION_LEN;
26
27public:
28 typedef T (*LogbFunc)(T);
29
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));
36 }
37
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)));
41
42 EXPECT_FP_EQ(T(1.0), func(T(2.0)));
43 EXPECT_FP_EQ(T(1.0), func(T(-2.0)));
44
45 EXPECT_FP_EQ(T(2.0), func(T(4.0)));
46 EXPECT_FP_EQ(T(2.0), func(T(-4.0)));
47
48 EXPECT_FP_EQ(T(3.0), func(T(8.0)));
49 EXPECT_FP_EQ(T(3.0), func(T(-8.0)));
50
51 EXPECT_FP_EQ(T(4.0), func(T(16.0)));
52 EXPECT_FP_EQ(T(4.0), func(T(-16.0)));
53
54 EXPECT_FP_EQ(T(5.0), func(T(32.0)));
55 EXPECT_FP_EQ(T(5.0), func(T(-32.0)));
56 }
57
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)));
61
62 EXPECT_FP_EQ(T(2.0), func(T(7.0)));
63 EXPECT_FP_EQ(T(2.0), func(T(-7.0)));
64
65 EXPECT_FP_EQ(T(3.0), func(T(10.0)));
66 EXPECT_FP_EQ(T(3.0), func(T(-10.0)));
67
68 EXPECT_FP_EQ(T(4.0), func(T(31.0)));
69 EXPECT_FP_EQ(T(4.0), func(T(-31.0)));
70
71 EXPECT_FP_EQ(T(5.0), func(T(55.0)));
72 EXPECT_FP_EQ(T(5.0), func(T(-55.0)));
73 }
74
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 (isnan(x) || isinf(x) || x == 0.0l)
82 continue;
83
84 int exponent;
85 LIBC_NAMESPACE::fputil::frexp(x, exponent);
86 ASSERT_FP_EQ(T(exponent), func(x) + T(1.0));
87 }
88 }
89};
90
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); }
97

source code of libc/test/src/math/LogbTest.h