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

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