1//===-- Utility class to test different flavors of ufromfp ------*- 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 LIBC_TEST_SRC_MATH_SMOKE_UFROMFPTEST_H
10#define LIBC_TEST_SRC_MATH_SMOKE_UFROMFPTEST_H
11
12#include "test/UnitTest/FEnvSafeTest.h"
13#include "test/UnitTest/FPMatcher.h"
14#include "test/UnitTest/Test.h"
15
16template <typename T>
17class UfromfpTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
18
19 DECLARE_SPECIAL_CONSTANTS(T)
20
21public:
22 typedef T (*UfromfpFunc)(T, int, unsigned int);
23
24 void testSpecialNumbersNonzeroWidth(UfromfpFunc func) {
25 for (int rnd : MATH_ROUNDING_DIRECTIONS_INCLUDING_UNKNOWN) {
26 EXPECT_FP_EQ(zero, func(zero, rnd, 32U));
27 EXPECT_FP_EQ(neg_zero, func(neg_zero, rnd, 32U));
28
29 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(inf, rnd, 32U), FE_INVALID);
30 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(neg_inf, rnd, 32U), FE_INVALID);
31
32 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(aNaN, rnd, 32U), FE_INVALID);
33 }
34 }
35
36 void testSpecialNumbersZeroWidth(UfromfpFunc func) {
37 for (int rnd : MATH_ROUNDING_DIRECTIONS_INCLUDING_UNKNOWN) {
38 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(zero, rnd, 0U), FE_INVALID);
39 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(neg_zero, rnd, 0U), FE_INVALID);
40
41 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(inf, rnd, 0U), FE_INVALID);
42 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(neg_inf, rnd, 0U), FE_INVALID);
43
44 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(aNaN, rnd, 0U), FE_INVALID);
45 }
46 }
47
48 void testRoundedNumbersWithinRange(UfromfpFunc func) {
49 for (int rnd : MATH_ROUNDING_DIRECTIONS_INCLUDING_UNKNOWN) {
50 EXPECT_FP_EQ(T(1.0), func(T(1.0), rnd, 1U));
51 EXPECT_FP_EQ(T(10.0), func(T(10.0), rnd, 4U));
52 EXPECT_FP_EQ(T(1234.0), func(T(1234.0), rnd, 11U));
53 EXPECT_FP_EQ(T(1234.0), func(T(1234.0), rnd, 64U));
54 }
55 }
56
57 void testRoundedNumbersOutsideRange(UfromfpFunc func) {
58 for (int rnd : MATH_ROUNDING_DIRECTIONS_INCLUDING_UNKNOWN) {
59 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.0), rnd, 32U), FE_INVALID);
60 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.0), rnd, 3U), FE_INVALID);
61 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.0), rnd, 32U), FE_INVALID);
62 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.0), rnd, 10U), FE_INVALID);
63 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.0), rnd, 32U), FE_INVALID);
64 }
65 }
66
67 void testFractionsUpwardWithinRange(UfromfpFunc func) {
68 EXPECT_FP_EQ(T(1.0), func(T(0.5), FP_INT_UPWARD, 1U));
69 EXPECT_FP_EQ(T(-0.0), func(T(-0.5), FP_INT_UPWARD, 1U));
70 EXPECT_FP_EQ(T(1.0), func(T(0.115), FP_INT_UPWARD, 1U));
71 EXPECT_FP_EQ(T(-0.0), func(T(-0.115), FP_INT_UPWARD, 1U));
72 EXPECT_FP_EQ(T(1.0), func(T(0.715), FP_INT_UPWARD, 1U));
73 EXPECT_FP_EQ(T(-0.0), func(T(-0.715), FP_INT_UPWARD, 1U));
74 EXPECT_FP_EQ(T(2.0), func(T(1.3), FP_INT_UPWARD, 2U));
75 EXPECT_FP_EQ(T(2.0), func(T(1.5), FP_INT_UPWARD, 2U));
76 EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_UPWARD, 2U));
77 EXPECT_FP_EQ(T(11.0), func(T(10.32), FP_INT_UPWARD, 4U));
78 EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_UPWARD, 4U));
79 EXPECT_FP_EQ(T(1235.0), func(T(1234.38), FP_INT_UPWARD, 11U));
80 EXPECT_FP_EQ(T(1235.0), func(T(1234.96), FP_INT_UPWARD, 11U));
81 }
82
83 void testFractionsUpwardOutsideRange(UfromfpFunc func) {
84 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.3), FP_INT_UPWARD, 1U),
85 FE_INVALID);
86 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_UPWARD, 32U),
87 FE_INVALID);
88 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.5), FP_INT_UPWARD, 1U),
89 FE_INVALID);
90 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_UPWARD, 32U),
91 FE_INVALID);
92 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.75), FP_INT_UPWARD, 1U),
93 FE_INVALID);
94 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_UPWARD, 32U),
95 FE_INVALID);
96 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_UPWARD, 3U),
97 FE_INVALID);
98 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_UPWARD, 32U),
99 FE_INVALID);
100 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_UPWARD, 3U),
101 FE_INVALID);
102 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_UPWARD, 3U),
103 FE_INVALID);
104 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_UPWARD, 10U),
105 FE_INVALID);
106 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_UPWARD, 32U),
107 FE_INVALID);
108 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_UPWARD, 10U),
109 FE_INVALID);
110 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_UPWARD, 32U),
111 FE_INVALID);
112 }
113
114 void testFractionsDownwardWithinRange(UfromfpFunc func) {
115 EXPECT_FP_EQ(T(0.0), func(T(0.5), FP_INT_DOWNWARD, 1U));
116 EXPECT_FP_EQ(T(0.0), func(T(0.115), FP_INT_DOWNWARD, 1U));
117 EXPECT_FP_EQ(T(0.0), func(T(0.715), FP_INT_DOWNWARD, 1U));
118 EXPECT_FP_EQ(T(1.0), func(T(1.3), FP_INT_DOWNWARD, 1U));
119 EXPECT_FP_EQ(T(1.0), func(T(1.5), FP_INT_DOWNWARD, 1U));
120 EXPECT_FP_EQ(T(1.0), func(T(1.75), FP_INT_DOWNWARD, 1U));
121 EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_DOWNWARD, 4U));
122 EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_DOWNWARD, 4U));
123 EXPECT_FP_EQ(T(1234.0), func(T(1234.38), FP_INT_DOWNWARD, 11U));
124 EXPECT_FP_EQ(T(1234.0), func(T(1234.96), FP_INT_DOWNWARD, 11U));
125 }
126
127 void testFractionsDownwardOutsideRange(UfromfpFunc func) {
128 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.5), FP_INT_DOWNWARD, 32U),
129 FE_INVALID);
130 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.115), FP_INT_DOWNWARD, 32U),
131 FE_INVALID);
132 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.715), FP_INT_DOWNWARD, 32U),
133 FE_INVALID);
134 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_DOWNWARD, 32U),
135 FE_INVALID);
136 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_DOWNWARD, 32U),
137 FE_INVALID);
138 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_DOWNWARD, 32U),
139 FE_INVALID);
140 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_DOWNWARD, 3U),
141 FE_INVALID);
142 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_DOWNWARD, 32U),
143 FE_INVALID);
144 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_DOWNWARD, 3U),
145 FE_INVALID);
146 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_DOWNWARD, 32U),
147 FE_INVALID);
148 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_DOWNWARD, 10U),
149 FE_INVALID);
150 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_DOWNWARD, 32U),
151 FE_INVALID);
152 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_DOWNWARD, 10U),
153 FE_INVALID);
154 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_DOWNWARD, 32U),
155 FE_INVALID);
156 }
157
158 void testFractionsTowardZeroWithinRange(UfromfpFunc func) {
159 EXPECT_FP_EQ(T(0.0), func(T(0.5), FP_INT_TOWARDZERO, 1U));
160 EXPECT_FP_EQ(T(-0.0), func(T(-0.5), FP_INT_TOWARDZERO, 1U));
161 EXPECT_FP_EQ(T(0.0), func(T(0.115), FP_INT_TOWARDZERO, 1U));
162 EXPECT_FP_EQ(T(-0.0), func(T(-0.115), FP_INT_TOWARDZERO, 1U));
163 EXPECT_FP_EQ(T(0.0), func(T(0.715), FP_INT_TOWARDZERO, 1U));
164 EXPECT_FP_EQ(T(-0.0), func(T(-0.715), FP_INT_TOWARDZERO, 1U));
165 EXPECT_FP_EQ(T(1.0), func(T(1.3), FP_INT_TOWARDZERO, 1U));
166 EXPECT_FP_EQ(T(1.0), func(T(1.5), FP_INT_TOWARDZERO, 1U));
167 EXPECT_FP_EQ(T(1.0), func(T(1.75), FP_INT_TOWARDZERO, 1U));
168 EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TOWARDZERO, 4U));
169 EXPECT_FP_EQ(T(10.0), func(T(10.65), FP_INT_TOWARDZERO, 4U));
170 EXPECT_FP_EQ(T(1234.0), func(T(1234.38), FP_INT_TOWARDZERO, 11U));
171 EXPECT_FP_EQ(T(1234.0), func(T(1234.96), FP_INT_TOWARDZERO, 11U));
172 }
173
174 void testFractionsTowardZeroOutsideRange(UfromfpFunc func) {
175 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_TOWARDZERO, 32U),
176 FE_INVALID);
177 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_TOWARDZERO, 32U),
178 FE_INVALID);
179 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_TOWARDZERO, 32U),
180 FE_INVALID);
181 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_TOWARDZERO, 3U),
182 FE_INVALID);
183 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_TOWARDZERO, 32U),
184 FE_INVALID);
185 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_TOWARDZERO, 3U),
186 FE_INVALID);
187 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_TOWARDZERO, 32U),
188 FE_INVALID);
189 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_TOWARDZERO, 10U),
190 FE_INVALID);
191 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_TOWARDZERO, 32U),
192 FE_INVALID);
193 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_TOWARDZERO, 10U),
194 FE_INVALID);
195 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_TOWARDZERO, 32U),
196 FE_INVALID);
197 }
198
199 void testFractionsToNearestFromZeroWithinRange(UfromfpFunc func) {
200 EXPECT_FP_EQ(T(1.0), func(T(0.5), FP_INT_TONEARESTFROMZERO, 1U));
201 EXPECT_FP_EQ(T(0.0), func(T(0.115), FP_INT_TONEARESTFROMZERO, 1U));
202 EXPECT_FP_EQ(T(-0.0), func(T(-0.115), FP_INT_TONEARESTFROMZERO, 1U));
203 EXPECT_FP_EQ(T(1.0), func(T(0.715), FP_INT_TONEARESTFROMZERO, 1U));
204 EXPECT_FP_EQ(T(1.0), func(T(1.3), FP_INT_TONEARESTFROMZERO, 1U));
205 EXPECT_FP_EQ(T(2.0), func(T(1.5), FP_INT_TONEARESTFROMZERO, 2U));
206 EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_TONEARESTFROMZERO, 2U));
207 EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TONEARESTFROMZERO, 4U));
208 EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEARESTFROMZERO, 4U));
209 EXPECT_FP_EQ(T(1234.0), func(T(1234.38), FP_INT_TONEARESTFROMZERO, 11U));
210 EXPECT_FP_EQ(T(1235.0), func(T(1234.96), FP_INT_TONEARESTFROMZERO, 11U));
211 }
212
213 void testFractionsToNearestFromZeroOutsideRange(UfromfpFunc func) {
214 EXPECT_FP_EQ_WITH_EXCEPTION(
215 aNaN, func(T(-0.5), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
216 EXPECT_FP_EQ_WITH_EXCEPTION(
217 aNaN, func(T(-0.715), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
218 EXPECT_FP_EQ_WITH_EXCEPTION(
219 aNaN, func(T(-1.3), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
220 EXPECT_FP_EQ_WITH_EXCEPTION(
221 aNaN, func(T(1.5), FP_INT_TONEARESTFROMZERO, 1U), FE_INVALID);
222 EXPECT_FP_EQ_WITH_EXCEPTION(
223 aNaN, func(T(-1.5), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
224 EXPECT_FP_EQ_WITH_EXCEPTION(
225 aNaN, func(T(1.75), FP_INT_TONEARESTFROMZERO, 1U), FE_INVALID);
226 EXPECT_FP_EQ_WITH_EXCEPTION(
227 aNaN, func(T(-1.75), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
228 EXPECT_FP_EQ_WITH_EXCEPTION(
229 aNaN, func(T(10.32), FP_INT_TONEARESTFROMZERO, 3U), FE_INVALID);
230 EXPECT_FP_EQ_WITH_EXCEPTION(
231 aNaN, func(T(-10.32), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
232 EXPECT_FP_EQ_WITH_EXCEPTION(
233 aNaN, func(T(10.65), FP_INT_TONEARESTFROMZERO, 3U), FE_INVALID);
234 EXPECT_FP_EQ_WITH_EXCEPTION(
235 aNaN, func(T(-10.65), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
236 EXPECT_FP_EQ_WITH_EXCEPTION(
237 aNaN, func(T(1234.38), FP_INT_TONEARESTFROMZERO, 10U), FE_INVALID);
238 EXPECT_FP_EQ_WITH_EXCEPTION(
239 aNaN, func(T(-1234.38), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
240 EXPECT_FP_EQ_WITH_EXCEPTION(
241 aNaN, func(T(1234.96), FP_INT_TONEARESTFROMZERO, 10U), FE_INVALID);
242 EXPECT_FP_EQ_WITH_EXCEPTION(
243 aNaN, func(T(-1234.96), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
244 }
245
246 void testFractionsToNearestWithinRange(UfromfpFunc func) {
247 EXPECT_FP_EQ(T(0.0), func(T(0.5), FP_INT_TONEAREST, 1U));
248 EXPECT_FP_EQ(T(-0.0), func(T(-0.5), FP_INT_TONEAREST, 1U));
249 EXPECT_FP_EQ(T(0.0), func(T(0.115), FP_INT_TONEAREST, 1U));
250 EXPECT_FP_EQ(T(-0.0), func(T(-0.115), FP_INT_TONEAREST, 1U));
251 EXPECT_FP_EQ(T(1.0), func(T(0.715), FP_INT_TONEAREST, 1U));
252 EXPECT_FP_EQ(T(1.0), func(T(1.3), FP_INT_TONEAREST, 1U));
253 EXPECT_FP_EQ(T(2.0), func(T(1.5), FP_INT_TONEAREST, 2U));
254 EXPECT_FP_EQ(T(2.0), func(T(1.75), FP_INT_TONEAREST, 2U));
255 EXPECT_FP_EQ(T(10.0), func(T(10.32), FP_INT_TONEAREST, 4U));
256 EXPECT_FP_EQ(T(11.0), func(T(10.65), FP_INT_TONEAREST, 4U));
257 EXPECT_FP_EQ(T(1234.0), func(T(1234.38), FP_INT_TONEAREST, 11U));
258 EXPECT_FP_EQ(T(1235.0), func(T(1234.96), FP_INT_TONEAREST, 11U));
259
260 EXPECT_FP_EQ(T(2.0), func(T(2.3), FP_INT_TONEAREST, 2U));
261 EXPECT_FP_EQ(T(2.0), func(T(2.5), FP_INT_TONEAREST, 2U));
262 EXPECT_FP_EQ(T(3.0), func(T(2.75), FP_INT_TONEAREST, 2U));
263 EXPECT_FP_EQ(T(5.0), func(T(5.3), FP_INT_TONEAREST, 3U));
264 EXPECT_FP_EQ(T(6.0), func(T(5.5), FP_INT_TONEAREST, 3U));
265 EXPECT_FP_EQ(T(6.0), func(T(5.75), FP_INT_TONEAREST, 3U));
266 }
267
268 void testFractionsToNearestOutsideRange(UfromfpFunc func) {
269 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.715), FP_INT_TONEAREST, 32U),
270 FE_INVALID);
271 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_TONEAREST, 32U),
272 FE_INVALID);
273 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.5), FP_INT_TONEAREST, 1U),
274 FE_INVALID);
275 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_TONEAREST, 32U),
276 FE_INVALID);
277 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.75), FP_INT_TONEAREST, 1U),
278 FE_INVALID);
279 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_TONEAREST, 32U),
280 FE_INVALID);
281 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_TONEAREST, 3U),
282 FE_INVALID);
283 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_TONEAREST, 32U),
284 FE_INVALID);
285 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_TONEAREST, 3U),
286 FE_INVALID);
287 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_TONEAREST, 32U),
288 FE_INVALID);
289 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_TONEAREST, 10U),
290 FE_INVALID);
291 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_TONEAREST, 32U),
292 FE_INVALID);
293 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_TONEAREST, 10U),
294 FE_INVALID);
295 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_TONEAREST, 32U),
296 FE_INVALID);
297
298 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.3), FP_INT_TONEAREST, 1U),
299 FE_INVALID);
300 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.3), FP_INT_TONEAREST, 32U),
301 FE_INVALID);
302 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.5), FP_INT_TONEAREST, 1U),
303 FE_INVALID);
304 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.5), FP_INT_TONEAREST, 32U),
305 FE_INVALID);
306 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.75), FP_INT_TONEAREST, 1U),
307 FE_INVALID);
308 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.75), FP_INT_TONEAREST, 32U),
309 FE_INVALID);
310 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.3), FP_INT_TONEAREST, 2U),
311 FE_INVALID);
312 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.3), FP_INT_TONEAREST, 32U),
313 FE_INVALID);
314 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.5), FP_INT_TONEAREST, 2U),
315 FE_INVALID);
316 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.5), FP_INT_TONEAREST, 32U),
317 FE_INVALID);
318 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.75), FP_INT_TONEAREST, 2U),
319 FE_INVALID);
320 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.75), FP_INT_TONEAREST, 32U),
321 FE_INVALID);
322 }
323
324 void testFractionsToNearestFallbackWithinRange(UfromfpFunc func) {
325 EXPECT_FP_EQ(T(0.0), func(T(0.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
326 EXPECT_FP_EQ(T(-0.0), func(T(-0.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
327 EXPECT_FP_EQ(T(0.0), func(T(0.115), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
328 EXPECT_FP_EQ(T(-0.0), func(T(-0.115), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
329 EXPECT_FP_EQ(T(1.0), func(T(0.715), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
330 EXPECT_FP_EQ(T(1.0), func(T(1.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U));
331 EXPECT_FP_EQ(T(2.0), func(T(1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
332 EXPECT_FP_EQ(T(2.0), func(T(1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
333 EXPECT_FP_EQ(T(10.0), func(T(10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U));
334 EXPECT_FP_EQ(T(11.0), func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U));
335 EXPECT_FP_EQ(T(1234.0),
336 func(T(1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 11U));
337 EXPECT_FP_EQ(T(1235.0),
338 func(T(1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 11U));
339
340 EXPECT_FP_EQ(T(2.0), func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
341 EXPECT_FP_EQ(T(2.0), func(T(2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
342 EXPECT_FP_EQ(T(3.0), func(T(2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U));
343 EXPECT_FP_EQ(T(5.0), func(T(5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U));
344 EXPECT_FP_EQ(T(6.0), func(T(5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U));
345 EXPECT_FP_EQ(T(6.0), func(T(5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U));
346 }
347
348 void testFractionsToNearestFallbackOutsideRange(UfromfpFunc func) {
349 EXPECT_FP_EQ_WITH_EXCEPTION(
350 aNaN, func(T(-0.715), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
351 FE_INVALID);
352 EXPECT_FP_EQ_WITH_EXCEPTION(
353 aNaN, func(T(-1.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
354 EXPECT_FP_EQ_WITH_EXCEPTION(
355 aNaN, func(T(1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
356 EXPECT_FP_EQ_WITH_EXCEPTION(
357 aNaN, func(T(-1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
358 EXPECT_FP_EQ_WITH_EXCEPTION(
359 aNaN, func(T(1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
360 EXPECT_FP_EQ_WITH_EXCEPTION(
361 aNaN, func(T(-1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
362 EXPECT_FP_EQ_WITH_EXCEPTION(
363 aNaN, func(T(10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INVALID);
364 EXPECT_FP_EQ_WITH_EXCEPTION(
365 aNaN, func(T(-10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
366 FE_INVALID);
367 EXPECT_FP_EQ_WITH_EXCEPTION(
368 aNaN, func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INVALID);
369 EXPECT_FP_EQ_WITH_EXCEPTION(
370 aNaN, func(T(-10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
371 FE_INVALID);
372 EXPECT_FP_EQ_WITH_EXCEPTION(
373 aNaN, func(T(1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 10U),
374 FE_INVALID);
375 EXPECT_FP_EQ_WITH_EXCEPTION(
376 aNaN, func(T(-1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
377 FE_INVALID);
378 EXPECT_FP_EQ_WITH_EXCEPTION(
379 aNaN, func(T(1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 10U),
380 FE_INVALID);
381 EXPECT_FP_EQ_WITH_EXCEPTION(
382 aNaN, func(T(-1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
383 FE_INVALID);
384
385 EXPECT_FP_EQ_WITH_EXCEPTION(
386 aNaN, func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
387 EXPECT_FP_EQ_WITH_EXCEPTION(
388 aNaN, func(T(-2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
389 EXPECT_FP_EQ_WITH_EXCEPTION(
390 aNaN, func(T(2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
391 EXPECT_FP_EQ_WITH_EXCEPTION(
392 aNaN, func(T(-2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
393 EXPECT_FP_EQ_WITH_EXCEPTION(
394 aNaN, func(T(2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
395 EXPECT_FP_EQ_WITH_EXCEPTION(
396 aNaN, func(T(-2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
397 EXPECT_FP_EQ_WITH_EXCEPTION(
398 aNaN, func(T(5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
399 EXPECT_FP_EQ_WITH_EXCEPTION(
400 aNaN, func(T(-5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
401 EXPECT_FP_EQ_WITH_EXCEPTION(
402 aNaN, func(T(5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
403 EXPECT_FP_EQ_WITH_EXCEPTION(
404 aNaN, func(T(-5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
405 EXPECT_FP_EQ_WITH_EXCEPTION(
406 aNaN, func(T(5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
407 EXPECT_FP_EQ_WITH_EXCEPTION(
408 aNaN, func(T(-5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
409 }
410};
411
412#define LIST_UFROMFP_TESTS(T, func) \
413 using LlvmLibcUfromfpTest = UfromfpTestTemplate<T>; \
414 TEST_F(LlvmLibcUfromfpTest, SpecialNumbersNonzeroWidth) { \
415 testSpecialNumbersNonzeroWidth(&func); \
416 } \
417 TEST_F(LlvmLibcUfromfpTest, SpecialNumbersZeroWidth) { \
418 testSpecialNumbersZeroWidth(&func); \
419 } \
420 TEST_F(LlvmLibcUfromfpTest, RoundedNumbersWithinRange) { \
421 testRoundedNumbersWithinRange(&func); \
422 } \
423 TEST_F(LlvmLibcUfromfpTest, RoundedNumbersOutsideRange) { \
424 testRoundedNumbersOutsideRange(&func); \
425 } \
426 TEST_F(LlvmLibcUfromfpTest, FractionsUpwardWithinRange) { \
427 testFractionsUpwardWithinRange(&func); \
428 } \
429 TEST_F(LlvmLibcUfromfpTest, FractionsUpwardOutsideRange) { \
430 testFractionsUpwardOutsideRange(&func); \
431 } \
432 TEST_F(LlvmLibcUfromfpTest, FractionsDownwardWithinRange) { \
433 testFractionsDownwardWithinRange(&func); \
434 } \
435 TEST_F(LlvmLibcUfromfpTest, FractionsDownwardOutsideRange) { \
436 testFractionsDownwardOutsideRange(&func); \
437 } \
438 TEST_F(LlvmLibcUfromfpTest, FractionsTowardZeroWithinRange) { \
439 testFractionsTowardZeroWithinRange(&func); \
440 } \
441 TEST_F(LlvmLibcUfromfpTest, FractionsTowardZeroOutsideRange) { \
442 testFractionsTowardZeroOutsideRange(&func); \
443 } \
444 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestFromZeroWithinRange) { \
445 testFractionsToNearestFromZeroWithinRange(&func); \
446 } \
447 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestFromZeroOutsideRange) { \
448 testFractionsToNearestFromZeroOutsideRange(&func); \
449 } \
450 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestWithinRange) { \
451 testFractionsToNearestWithinRange(&func); \
452 } \
453 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestOutsideRange) { \
454 testFractionsToNearestOutsideRange(&func); \
455 } \
456 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestFallbackWithinRange) { \
457 testFractionsToNearestFallbackWithinRange(&func); \
458 } \
459 TEST_F(LlvmLibcUfromfpTest, FractionsToNearestFallbackOutsideRange) { \
460 testFractionsToNearestFallbackOutsideRange(&func); \
461 }
462
463#endif // LIBC_TEST_SRC_MATH_SMOKE_UFROMFPTEST_H
464

source code of libc/test/src/math/smoke/UfromfpTest.h