1//===-- Utility class to test different flavors of nextafter ----*- 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_NEXTAFTERTEST_H
10#define LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H
11
12#include "hdr/math_macros.h"
13#include "src/__support/CPP/bit.h"
14#include "src/__support/CPP/type_traits.h"
15#include "src/__support/FPUtil/BasicOperations.h"
16#include "src/__support/FPUtil/FPBits.h"
17#include "test/UnitTest/FEnvSafeTest.h"
18#include "test/UnitTest/FPMatcher.h"
19#include "test/UnitTest/Test.h"
20
21template <typename T>
22class NextAfterTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
23 using FPBits = LIBC_NAMESPACE::fputil::FPBits<T>;
24 using StorageType = typename FPBits::StorageType;
25
26 const T inf = FPBits::inf(Sign::POS).get_val();
27 const T neg_inf = FPBits::inf(Sign::NEG).get_val();
28 const T zero = FPBits::zero(Sign::POS).get_val();
29 const T neg_zero = FPBits::zero(Sign::NEG).get_val();
30 const T nan = FPBits::quiet_nan().get_val();
31
32 const StorageType min_subnormal = FPBits::min_subnormal().uintval();
33 const StorageType max_subnormal = FPBits::max_subnormal().uintval();
34 const StorageType min_normal = FPBits::min_normal().uintval();
35 const StorageType max_normal = FPBits::max_normal().uintval();
36
37public:
38 typedef T (*NextAfterFunc)(T, T);
39
40 void testNaN(NextAfterFunc func) {
41 ASSERT_FP_EQ(func(nan, 0), nan);
42 ASSERT_FP_EQ(func(0, nan), nan);
43 }
44
45 void testBoundaries(NextAfterFunc func) {
46 ASSERT_FP_EQ(func(zero, neg_zero), neg_zero);
47 ASSERT_FP_EQ(func(neg_zero, zero), zero);
48
49 // 'from' is zero|neg_zero.
50 T x = zero;
51 T result = func(x, T(1));
52 StorageType expected_bits = 1;
53 T expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
54 ASSERT_FP_EQ(result, expected);
55
56 result = func(x, T(-1));
57 expected_bits = FPBits::SIGN_MASK + 1;
58 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
59 ASSERT_FP_EQ(result, expected);
60
61 x = neg_zero;
62 result = func(x, 1);
63 expected_bits = 1;
64 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
65 ASSERT_FP_EQ(result, expected);
66
67 result = func(x, -1);
68 expected_bits = FPBits::SIGN_MASK + 1;
69 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
70 ASSERT_FP_EQ(result, expected);
71
72 // 'from' is max subnormal value.
73 x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_subnormal);
74 result = func(x, 1);
75 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
76 ASSERT_FP_EQ(result, expected);
77
78 result = func(x, 0);
79 expected_bits = max_subnormal - 1;
80 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
81 ASSERT_FP_EQ(result, expected);
82
83 x = -x;
84
85 result = func(x, -1);
86 expected_bits = FPBits::SIGN_MASK + min_normal;
87 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
88 ASSERT_FP_EQ(result, expected);
89
90 result = func(x, 0);
91 expected_bits = FPBits::SIGN_MASK + max_subnormal - 1;
92 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
93 ASSERT_FP_EQ(result, expected);
94
95 // 'from' is min subnormal value.
96 x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_subnormal);
97 result = func(x, 1);
98 expected_bits = min_subnormal + 1;
99 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
100 ASSERT_FP_EQ(result, expected);
101 ASSERT_FP_EQ(func(x, 0), 0);
102
103 x = -x;
104 result = func(x, -1);
105 expected_bits = FPBits::SIGN_MASK + min_subnormal + 1;
106 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
107 ASSERT_FP_EQ(result, expected);
108 ASSERT_FP_EQ(func(x, 0), T(-0.0));
109
110 // 'from' is min normal.
111 x = LIBC_NAMESPACE::cpp::bit_cast<T>(min_normal);
112 result = func(x, 0);
113 expected_bits = max_subnormal;
114 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
115 ASSERT_FP_EQ(result, expected);
116
117 result = func(x, inf);
118 expected_bits = min_normal + 1;
119 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
120 ASSERT_FP_EQ(result, expected);
121
122 x = -x;
123 result = func(x, 0);
124 expected_bits = FPBits::SIGN_MASK + max_subnormal;
125 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
126 ASSERT_FP_EQ(result, expected);
127
128 result = func(x, -inf);
129 expected_bits = FPBits::SIGN_MASK + min_normal + 1;
130 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
131 ASSERT_FP_EQ(result, expected);
132
133 // 'from' is max normal and 'to' is infinity.
134 x = LIBC_NAMESPACE::cpp::bit_cast<T>(max_normal);
135 result = func(x, inf);
136 ASSERT_FP_EQ(result, inf);
137
138 result = func(-x, -inf);
139 ASSERT_FP_EQ(result, -inf);
140
141 // 'from' is infinity.
142 x = inf;
143 result = func(x, 0);
144 expected_bits = max_normal;
145 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
146 ASSERT_FP_EQ(result, expected);
147 ASSERT_FP_EQ(func(x, inf), inf);
148
149 x = neg_inf;
150 result = func(x, 0);
151 expected_bits = FPBits::SIGN_MASK + max_normal;
152 expected = LIBC_NAMESPACE::cpp::bit_cast<T>(expected_bits);
153 ASSERT_FP_EQ(result, expected);
154 ASSERT_FP_EQ(func(x, neg_inf), neg_inf);
155
156 // 'from' is a power of 2.
157 x = T(32.0);
158 result = func(x, 0);
159 FPBits x_bits = FPBits(x);
160 FPBits result_bits = FPBits(result);
161 ASSERT_EQ(result_bits.get_biased_exponent(),
162 uint16_t(x_bits.get_biased_exponent() - 1));
163 ASSERT_EQ(result_bits.get_mantissa(), FPBits::FRACTION_MASK);
164
165 result = func(x, T(33.0));
166 result_bits = FPBits(result);
167 ASSERT_EQ(result_bits.get_biased_exponent(), x_bits.get_biased_exponent());
168 ASSERT_EQ(result_bits.get_mantissa(),
169 x_bits.get_mantissa() + StorageType(1));
170
171 x = -x;
172
173 result = func(x, 0);
174 result_bits = FPBits(result);
175 ASSERT_EQ(result_bits.get_biased_exponent(),
176 uint16_t(x_bits.get_biased_exponent() - 1));
177 ASSERT_EQ(result_bits.get_mantissa(), FPBits::FRACTION_MASK);
178
179 result = func(x, T(-33.0));
180 result_bits = FPBits(result);
181 ASSERT_EQ(result_bits.get_biased_exponent(), x_bits.get_biased_exponent());
182 ASSERT_EQ(result_bits.get_mantissa(),
183 x_bits.get_mantissa() + StorageType(1));
184 }
185};
186
187#define LIST_NEXTAFTER_TESTS(T, func) \
188 using LlvmLibcNextAfterTest = NextAfterTestTemplate<T>; \
189 TEST_F(LlvmLibcNextAfterTest, TestNaN) { testNaN(&func); } \
190 TEST_F(LlvmLibcNextAfterTest, TestBoundaries) { testBoundaries(&func); }
191
192#endif // LLVM_LIBC_TEST_SRC_MATH_NEXTAFTERTEST_H
193

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