1//===-- Utility class to test different flavors of ufromfpx -----*- 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_UFROMFPXTEST_H
10#define LIBC_TEST_SRC_MATH_SMOKE_UFROMFPXTEST_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 UfromfpxTestTemplate : public LIBC_NAMESPACE::testing::FEnvSafeTest {
18
19 DECLARE_SPECIAL_CONSTANTS(T)
20
21public:
22 typedef T (*UfromfpxFunc)(T, int, unsigned int);
23
24 void testSpecialNumbersNonzeroWidth(UfromfpxFunc 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(UfromfpxFunc 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(UfromfpxFunc 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(UfromfpxFunc 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(UfromfpxFunc func) {
68 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(0.5), FP_INT_UPWARD, 1U),
69 FE_INEXACT);
70 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.5), FP_INT_UPWARD, 1U),
71 FE_INEXACT);
72 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(0.115), FP_INT_UPWARD, 1U),
73 FE_INEXACT);
74 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.115), FP_INT_UPWARD, 1U),
75 FE_INEXACT);
76 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(0.715), FP_INT_UPWARD, 1U),
77 FE_INEXACT);
78 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.715), FP_INT_UPWARD, 1U),
79 FE_INEXACT);
80 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(1.3), FP_INT_UPWARD, 2U),
81 FE_INEXACT);
82 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(1.5), FP_INT_UPWARD, 2U),
83 FE_INEXACT);
84 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(1.75), FP_INT_UPWARD, 2U),
85 FE_INEXACT);
86 EXPECT_FP_EQ_WITH_EXCEPTION(T(11.0), func(T(10.32), FP_INT_UPWARD, 4U),
87 FE_INEXACT);
88 EXPECT_FP_EQ_WITH_EXCEPTION(T(11.0), func(T(10.65), FP_INT_UPWARD, 4U),
89 FE_INEXACT);
90 EXPECT_FP_EQ_WITH_EXCEPTION(T(1235.0), func(T(1234.38), FP_INT_UPWARD, 11U),
91 FE_INEXACT);
92 EXPECT_FP_EQ_WITH_EXCEPTION(T(1235.0), func(T(1234.96), FP_INT_UPWARD, 11U),
93 FE_INEXACT);
94 }
95
96 void testFractionsUpwardOutsideRange(UfromfpxFunc func) {
97 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.3), FP_INT_UPWARD, 1U),
98 FE_INVALID);
99 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_UPWARD, 32U),
100 FE_INVALID);
101 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.5), FP_INT_UPWARD, 1U),
102 FE_INVALID);
103 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_UPWARD, 32U),
104 FE_INVALID);
105 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.75), FP_INT_UPWARD, 1U),
106 FE_INVALID);
107 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_UPWARD, 32U),
108 FE_INVALID);
109 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_UPWARD, 3U),
110 FE_INVALID);
111 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_UPWARD, 32U),
112 FE_INVALID);
113 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_UPWARD, 3U),
114 FE_INVALID);
115 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_UPWARD, 3U),
116 FE_INVALID);
117 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_UPWARD, 10U),
118 FE_INVALID);
119 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_UPWARD, 32U),
120 FE_INVALID);
121 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_UPWARD, 10U),
122 FE_INVALID);
123 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_UPWARD, 32U),
124 FE_INVALID);
125 }
126
127 void testFractionsDownwardWithinRange(UfromfpxFunc func) {
128 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.5), FP_INT_DOWNWARD, 1U),
129 FE_INEXACT);
130 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.115), FP_INT_DOWNWARD, 1U),
131 FE_INEXACT);
132 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.715), FP_INT_DOWNWARD, 1U),
133 FE_INEXACT);
134 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.3), FP_INT_DOWNWARD, 1U),
135 FE_INEXACT);
136 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.5), FP_INT_DOWNWARD, 1U),
137 FE_INEXACT);
138 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.75), FP_INT_DOWNWARD, 1U),
139 FE_INEXACT);
140 EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.32), FP_INT_DOWNWARD, 4U),
141 FE_INEXACT);
142 EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.65), FP_INT_DOWNWARD, 4U),
143 FE_INEXACT);
144 EXPECT_FP_EQ_WITH_EXCEPTION(
145 T(1234.0), func(T(1234.38), FP_INT_DOWNWARD, 11U), FE_INEXACT);
146 EXPECT_FP_EQ_WITH_EXCEPTION(
147 T(1234.0), func(T(1234.96), FP_INT_DOWNWARD, 11U), FE_INEXACT);
148 }
149
150 void testFractionsDownwardOutsideRange(UfromfpxFunc func) {
151 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.5), FP_INT_DOWNWARD, 32U),
152 FE_INVALID);
153 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.115), FP_INT_DOWNWARD, 32U),
154 FE_INVALID);
155 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.715), FP_INT_DOWNWARD, 32U),
156 FE_INVALID);
157 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_DOWNWARD, 32U),
158 FE_INVALID);
159 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_DOWNWARD, 32U),
160 FE_INVALID);
161 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_DOWNWARD, 32U),
162 FE_INVALID);
163 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_DOWNWARD, 3U),
164 FE_INVALID);
165 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_DOWNWARD, 32U),
166 FE_INVALID);
167 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_DOWNWARD, 3U),
168 FE_INVALID);
169 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_DOWNWARD, 32U),
170 FE_INVALID);
171 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_DOWNWARD, 10U),
172 FE_INVALID);
173 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_DOWNWARD, 32U),
174 FE_INVALID);
175 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_DOWNWARD, 10U),
176 FE_INVALID);
177 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_DOWNWARD, 32U),
178 FE_INVALID);
179 }
180
181 void testFractionsTowardZeroWithinRange(UfromfpxFunc func) {
182 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.5), FP_INT_TOWARDZERO, 1U),
183 FE_INEXACT);
184 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.5), FP_INT_TOWARDZERO, 1U),
185 FE_INEXACT);
186 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.115), FP_INT_TOWARDZERO, 1U),
187 FE_INEXACT);
188 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.115), FP_INT_TOWARDZERO, 1U),
189 FE_INEXACT);
190 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.715), FP_INT_TOWARDZERO, 1U),
191 FE_INEXACT);
192 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.715), FP_INT_TOWARDZERO, 1U),
193 FE_INEXACT);
194 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.3), FP_INT_TOWARDZERO, 1U),
195 FE_INEXACT);
196 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.5), FP_INT_TOWARDZERO, 1U),
197 FE_INEXACT);
198 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.75), FP_INT_TOWARDZERO, 1U),
199 FE_INEXACT);
200 EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.32), FP_INT_TOWARDZERO, 4U),
201 FE_INEXACT);
202 EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.65), FP_INT_TOWARDZERO, 4U),
203 FE_INEXACT);
204 EXPECT_FP_EQ_WITH_EXCEPTION(
205 T(1234.0), func(T(1234.38), FP_INT_TOWARDZERO, 11U), FE_INEXACT);
206 EXPECT_FP_EQ_WITH_EXCEPTION(
207 T(1234.0), func(T(1234.96), FP_INT_TOWARDZERO, 11U), FE_INEXACT);
208 }
209
210 void testFractionsTowardZeroOutsideRange(UfromfpxFunc func) {
211 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_TOWARDZERO, 32U),
212 FE_INVALID);
213 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_TOWARDZERO, 32U),
214 FE_INVALID);
215 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_TOWARDZERO, 32U),
216 FE_INVALID);
217 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_TOWARDZERO, 3U),
218 FE_INVALID);
219 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_TOWARDZERO, 32U),
220 FE_INVALID);
221 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_TOWARDZERO, 3U),
222 FE_INVALID);
223 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_TOWARDZERO, 32U),
224 FE_INVALID);
225 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_TOWARDZERO, 10U),
226 FE_INVALID);
227 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_TOWARDZERO, 32U),
228 FE_INVALID);
229 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_TOWARDZERO, 10U),
230 FE_INVALID);
231 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_TOWARDZERO, 32U),
232 FE_INVALID);
233 }
234
235 void testFractionsToNearestFromZeroWithinRange(UfromfpxFunc func) {
236 EXPECT_FP_EQ_WITH_EXCEPTION(
237 T(1.0), func(T(0.5), FP_INT_TONEARESTFROMZERO, 1U), FE_INEXACT);
238 EXPECT_FP_EQ_WITH_EXCEPTION(
239 T(0.0), func(T(0.115), FP_INT_TONEARESTFROMZERO, 1U), FE_INEXACT);
240 EXPECT_FP_EQ_WITH_EXCEPTION(
241 T(-0.0), func(T(-0.115), FP_INT_TONEARESTFROMZERO, 1U), FE_INEXACT);
242 EXPECT_FP_EQ_WITH_EXCEPTION(
243 T(1.0), func(T(0.715), FP_INT_TONEARESTFROMZERO, 1U), FE_INEXACT);
244 EXPECT_FP_EQ_WITH_EXCEPTION(
245 T(1.0), func(T(1.3), FP_INT_TONEARESTFROMZERO, 1U), FE_INEXACT);
246 EXPECT_FP_EQ_WITH_EXCEPTION(
247 T(2.0), func(T(1.5), FP_INT_TONEARESTFROMZERO, 2U), FE_INEXACT);
248 EXPECT_FP_EQ_WITH_EXCEPTION(
249 T(2.0), func(T(1.75), FP_INT_TONEARESTFROMZERO, 2U), FE_INEXACT);
250 EXPECT_FP_EQ_WITH_EXCEPTION(
251 T(10.0), func(T(10.32), FP_INT_TONEARESTFROMZERO, 4U), FE_INEXACT);
252 EXPECT_FP_EQ_WITH_EXCEPTION(
253 T(11.0), func(T(10.65), FP_INT_TONEARESTFROMZERO, 4U), FE_INEXACT);
254 EXPECT_FP_EQ_WITH_EXCEPTION(
255 T(1234.0), func(T(1234.38), FP_INT_TONEARESTFROMZERO, 11U), FE_INEXACT);
256 EXPECT_FP_EQ_WITH_EXCEPTION(
257 T(1235.0), func(T(1234.96), FP_INT_TONEARESTFROMZERO, 11U), FE_INEXACT);
258 }
259
260 void testFractionsToNearestFromZeroOutsideRange(UfromfpxFunc func) {
261 EXPECT_FP_EQ_WITH_EXCEPTION(
262 aNaN, func(T(-0.5), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
263 EXPECT_FP_EQ_WITH_EXCEPTION(
264 aNaN, func(T(-0.715), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
265 EXPECT_FP_EQ_WITH_EXCEPTION(
266 aNaN, func(T(-1.3), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
267 EXPECT_FP_EQ_WITH_EXCEPTION(
268 aNaN, func(T(1.5), FP_INT_TONEARESTFROMZERO, 1U), FE_INVALID);
269 EXPECT_FP_EQ_WITH_EXCEPTION(
270 aNaN, func(T(-1.5), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
271 EXPECT_FP_EQ_WITH_EXCEPTION(
272 aNaN, func(T(1.75), FP_INT_TONEARESTFROMZERO, 1U), FE_INVALID);
273 EXPECT_FP_EQ_WITH_EXCEPTION(
274 aNaN, func(T(-1.75), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
275 EXPECT_FP_EQ_WITH_EXCEPTION(
276 aNaN, func(T(10.32), FP_INT_TONEARESTFROMZERO, 3U), FE_INVALID);
277 EXPECT_FP_EQ_WITH_EXCEPTION(
278 aNaN, func(T(-10.32), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
279 EXPECT_FP_EQ_WITH_EXCEPTION(
280 aNaN, func(T(10.65), FP_INT_TONEARESTFROMZERO, 3U), FE_INVALID);
281 EXPECT_FP_EQ_WITH_EXCEPTION(
282 aNaN, func(T(-10.65), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
283 EXPECT_FP_EQ_WITH_EXCEPTION(
284 aNaN, func(T(1234.38), FP_INT_TONEARESTFROMZERO, 10U), FE_INVALID);
285 EXPECT_FP_EQ_WITH_EXCEPTION(
286 aNaN, func(T(-1234.38), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
287 EXPECT_FP_EQ_WITH_EXCEPTION(
288 aNaN, func(T(1234.96), FP_INT_TONEARESTFROMZERO, 10U), FE_INVALID);
289 EXPECT_FP_EQ_WITH_EXCEPTION(
290 aNaN, func(T(-1234.96), FP_INT_TONEARESTFROMZERO, 32U), FE_INVALID);
291 }
292
293 void testFractionsToNearestWithinRange(UfromfpxFunc func) {
294 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.5), FP_INT_TONEAREST, 1U),
295 FE_INEXACT);
296 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.5), FP_INT_TONEAREST, 1U),
297 FE_INEXACT);
298 EXPECT_FP_EQ_WITH_EXCEPTION(T(0.0), func(T(0.115), FP_INT_TONEAREST, 1U),
299 FE_INEXACT);
300 EXPECT_FP_EQ_WITH_EXCEPTION(T(-0.0), func(T(-0.115), FP_INT_TONEAREST, 1U),
301 FE_INEXACT);
302 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(0.715), FP_INT_TONEAREST, 1U),
303 FE_INEXACT);
304 EXPECT_FP_EQ_WITH_EXCEPTION(T(1.0), func(T(1.3), FP_INT_TONEAREST, 1U),
305 FE_INEXACT);
306 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(1.5), FP_INT_TONEAREST, 2U),
307 FE_INEXACT);
308 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(1.75), FP_INT_TONEAREST, 2U),
309 FE_INEXACT);
310 EXPECT_FP_EQ_WITH_EXCEPTION(T(10.0), func(T(10.32), FP_INT_TONEAREST, 4U),
311 FE_INEXACT);
312 EXPECT_FP_EQ_WITH_EXCEPTION(T(11.0), func(T(10.65), FP_INT_TONEAREST, 4U),
313 FE_INEXACT);
314 EXPECT_FP_EQ_WITH_EXCEPTION(
315 T(1234.0), func(T(1234.38), FP_INT_TONEAREST, 11U), FE_INEXACT);
316 EXPECT_FP_EQ_WITH_EXCEPTION(
317 T(1235.0), func(T(1234.96), FP_INT_TONEAREST, 11U), FE_INEXACT);
318
319 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(2.3), FP_INT_TONEAREST, 2U),
320 FE_INEXACT);
321 EXPECT_FP_EQ_WITH_EXCEPTION(T(2.0), func(T(2.5), FP_INT_TONEAREST, 2U),
322 FE_INEXACT);
323 EXPECT_FP_EQ_WITH_EXCEPTION(T(3.0), func(T(2.75), FP_INT_TONEAREST, 2U),
324 FE_INEXACT);
325 EXPECT_FP_EQ_WITH_EXCEPTION(T(5.0), func(T(5.3), FP_INT_TONEAREST, 3U),
326 FE_INEXACT);
327 EXPECT_FP_EQ_WITH_EXCEPTION(T(6.0), func(T(5.5), FP_INT_TONEAREST, 3U),
328 FE_INEXACT);
329 EXPECT_FP_EQ_WITH_EXCEPTION(T(6.0), func(T(5.75), FP_INT_TONEAREST, 3U),
330 FE_INEXACT);
331 }
332
333 void testFractionsToNearestOutsideRange(UfromfpxFunc func) {
334 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-0.715), FP_INT_TONEAREST, 32U),
335 FE_INVALID);
336 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.3), FP_INT_TONEAREST, 32U),
337 FE_INVALID);
338 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.5), FP_INT_TONEAREST, 1U),
339 FE_INVALID);
340 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.5), FP_INT_TONEAREST, 32U),
341 FE_INVALID);
342 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1.75), FP_INT_TONEAREST, 1U),
343 FE_INVALID);
344 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1.75), FP_INT_TONEAREST, 32U),
345 FE_INVALID);
346 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.32), FP_INT_TONEAREST, 3U),
347 FE_INVALID);
348 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.32), FP_INT_TONEAREST, 32U),
349 FE_INVALID);
350 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(10.65), FP_INT_TONEAREST, 3U),
351 FE_INVALID);
352 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-10.65), FP_INT_TONEAREST, 32U),
353 FE_INVALID);
354 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.38), FP_INT_TONEAREST, 10U),
355 FE_INVALID);
356 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.38), FP_INT_TONEAREST, 32U),
357 FE_INVALID);
358 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(1234.96), FP_INT_TONEAREST, 10U),
359 FE_INVALID);
360 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-1234.96), FP_INT_TONEAREST, 32U),
361 FE_INVALID);
362
363 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.3), FP_INT_TONEAREST, 1U),
364 FE_INVALID);
365 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.3), FP_INT_TONEAREST, 32U),
366 FE_INVALID);
367 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.5), FP_INT_TONEAREST, 1U),
368 FE_INVALID);
369 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.5), FP_INT_TONEAREST, 32U),
370 FE_INVALID);
371 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(2.75), FP_INT_TONEAREST, 1U),
372 FE_INVALID);
373 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-2.75), FP_INT_TONEAREST, 32U),
374 FE_INVALID);
375 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.3), FP_INT_TONEAREST, 2U),
376 FE_INVALID);
377 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.3), FP_INT_TONEAREST, 32U),
378 FE_INVALID);
379 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.5), FP_INT_TONEAREST, 2U),
380 FE_INVALID);
381 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.5), FP_INT_TONEAREST, 32U),
382 FE_INVALID);
383 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(5.75), FP_INT_TONEAREST, 2U),
384 FE_INVALID);
385 EXPECT_FP_EQ_WITH_EXCEPTION(aNaN, func(T(-5.75), FP_INT_TONEAREST, 32U),
386 FE_INVALID);
387 }
388
389 void testFractionsToNearestFallbackWithinRange(UfromfpxFunc func) {
390 EXPECT_FP_EQ_WITH_EXCEPTION(
391 T(0.0), func(T(0.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INEXACT);
392 EXPECT_FP_EQ_WITH_EXCEPTION(
393 T(-0.0), func(T(-0.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U),
394 FE_INEXACT);
395 EXPECT_FP_EQ_WITH_EXCEPTION(
396 T(0.0), func(T(0.115), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U),
397 FE_INEXACT);
398 EXPECT_FP_EQ_WITH_EXCEPTION(
399 T(-0.0), func(T(-0.115), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U),
400 FE_INEXACT);
401 EXPECT_FP_EQ_WITH_EXCEPTION(
402 T(1.0), func(T(0.715), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U),
403 FE_INEXACT);
404 EXPECT_FP_EQ_WITH_EXCEPTION(
405 T(1.0), func(T(1.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INEXACT);
406 EXPECT_FP_EQ_WITH_EXCEPTION(
407 T(2.0), func(T(1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INEXACT);
408 EXPECT_FP_EQ_WITH_EXCEPTION(
409 T(2.0), func(T(1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INEXACT);
410 EXPECT_FP_EQ_WITH_EXCEPTION(
411 T(10.0), func(T(10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U),
412 FE_INEXACT);
413 EXPECT_FP_EQ_WITH_EXCEPTION(
414 T(11.0), func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 4U),
415 FE_INEXACT);
416 EXPECT_FP_EQ_WITH_EXCEPTION(
417 T(1234.0), func(T(1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 11U),
418 FE_INEXACT);
419 EXPECT_FP_EQ_WITH_EXCEPTION(
420 T(1235.0), func(T(1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 11U),
421 FE_INEXACT);
422
423 EXPECT_FP_EQ_WITH_EXCEPTION(
424 T(2.0), func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INEXACT);
425 EXPECT_FP_EQ_WITH_EXCEPTION(
426 T(2.0), func(T(2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INEXACT);
427 EXPECT_FP_EQ_WITH_EXCEPTION(
428 T(3.0), func(T(2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INEXACT);
429 EXPECT_FP_EQ_WITH_EXCEPTION(
430 T(5.0), func(T(5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INEXACT);
431 EXPECT_FP_EQ_WITH_EXCEPTION(
432 T(6.0), func(T(5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INEXACT);
433 EXPECT_FP_EQ_WITH_EXCEPTION(
434 T(6.0), func(T(5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INEXACT);
435 }
436
437 void testFractionsToNearestFallbackOutsideRange(UfromfpxFunc func) {
438 EXPECT_FP_EQ_WITH_EXCEPTION(
439 aNaN, func(T(-0.715), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
440 FE_INVALID);
441 EXPECT_FP_EQ_WITH_EXCEPTION(
442 aNaN, func(T(-1.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
443 EXPECT_FP_EQ_WITH_EXCEPTION(
444 aNaN, func(T(1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
445 EXPECT_FP_EQ_WITH_EXCEPTION(
446 aNaN, func(T(-1.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
447 EXPECT_FP_EQ_WITH_EXCEPTION(
448 aNaN, func(T(1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
449 EXPECT_FP_EQ_WITH_EXCEPTION(
450 aNaN, func(T(-1.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
451 EXPECT_FP_EQ_WITH_EXCEPTION(
452 aNaN, func(T(10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INVALID);
453 EXPECT_FP_EQ_WITH_EXCEPTION(
454 aNaN, func(T(-10.32), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
455 FE_INVALID);
456 EXPECT_FP_EQ_WITH_EXCEPTION(
457 aNaN, func(T(10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 3U), FE_INVALID);
458 EXPECT_FP_EQ_WITH_EXCEPTION(
459 aNaN, func(T(-10.65), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
460 FE_INVALID);
461 EXPECT_FP_EQ_WITH_EXCEPTION(
462 aNaN, func(T(1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 10U),
463 FE_INVALID);
464 EXPECT_FP_EQ_WITH_EXCEPTION(
465 aNaN, func(T(-1234.38), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
466 FE_INVALID);
467 EXPECT_FP_EQ_WITH_EXCEPTION(
468 aNaN, func(T(1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 10U),
469 FE_INVALID);
470 EXPECT_FP_EQ_WITH_EXCEPTION(
471 aNaN, func(T(-1234.96), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U),
472 FE_INVALID);
473
474 EXPECT_FP_EQ_WITH_EXCEPTION(
475 aNaN, func(T(2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
476 EXPECT_FP_EQ_WITH_EXCEPTION(
477 aNaN, func(T(-2.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
478 EXPECT_FP_EQ_WITH_EXCEPTION(
479 aNaN, func(T(2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
480 EXPECT_FP_EQ_WITH_EXCEPTION(
481 aNaN, func(T(-2.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
482 EXPECT_FP_EQ_WITH_EXCEPTION(
483 aNaN, func(T(2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 1U), FE_INVALID);
484 EXPECT_FP_EQ_WITH_EXCEPTION(
485 aNaN, func(T(-2.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
486 EXPECT_FP_EQ_WITH_EXCEPTION(
487 aNaN, func(T(5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
488 EXPECT_FP_EQ_WITH_EXCEPTION(
489 aNaN, func(T(-5.3), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
490 EXPECT_FP_EQ_WITH_EXCEPTION(
491 aNaN, func(T(5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
492 EXPECT_FP_EQ_WITH_EXCEPTION(
493 aNaN, func(T(-5.5), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
494 EXPECT_FP_EQ_WITH_EXCEPTION(
495 aNaN, func(T(5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 2U), FE_INVALID);
496 EXPECT_FP_EQ_WITH_EXCEPTION(
497 aNaN, func(T(-5.75), UNKNOWN_MATH_ROUNDING_DIRECTION, 32U), FE_INVALID);
498 }
499};
500
501#define LIST_UFROMFPX_TESTS(T, func) \
502 using LlvmLibcUfromfpxTest = UfromfpxTestTemplate<T>; \
503 TEST_F(LlvmLibcUfromfpxTest, SpecialNumbersNonzeroWidth) { \
504 testSpecialNumbersNonzeroWidth(&func); \
505 } \
506 TEST_F(LlvmLibcUfromfpxTest, SpecialNumbersZeroWidth) { \
507 testSpecialNumbersZeroWidth(&func); \
508 } \
509 TEST_F(LlvmLibcUfromfpxTest, RoundedNumbersWithinRange) { \
510 testRoundedNumbersWithinRange(&func); \
511 } \
512 TEST_F(LlvmLibcUfromfpxTest, RoundedNumbersOutsideRange) { \
513 testRoundedNumbersOutsideRange(&func); \
514 } \
515 TEST_F(LlvmLibcUfromfpxTest, FractionsUpwardWithinRange) { \
516 testFractionsUpwardWithinRange(&func); \
517 } \
518 TEST_F(LlvmLibcUfromfpxTest, FractionsUpwardOutsideRange) { \
519 testFractionsUpwardOutsideRange(&func); \
520 } \
521 TEST_F(LlvmLibcUfromfpxTest, FractionsDownwardWithinRange) { \
522 testFractionsDownwardWithinRange(&func); \
523 } \
524 TEST_F(LlvmLibcUfromfpxTest, FractionsDownwardOutsideRange) { \
525 testFractionsDownwardOutsideRange(&func); \
526 } \
527 TEST_F(LlvmLibcUfromfpxTest, FractionsTowardZeroWithinRange) { \
528 testFractionsTowardZeroWithinRange(&func); \
529 } \
530 TEST_F(LlvmLibcUfromfpxTest, FractionsTowardZeroOutsideRange) { \
531 testFractionsTowardZeroOutsideRange(&func); \
532 } \
533 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestFromZeroWithinRange) { \
534 testFractionsToNearestFromZeroWithinRange(&func); \
535 } \
536 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestFromZeroOutsideRange) { \
537 testFractionsToNearestFromZeroOutsideRange(&func); \
538 } \
539 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestWithinRange) { \
540 testFractionsToNearestWithinRange(&func); \
541 } \
542 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestOutsideRange) { \
543 testFractionsToNearestOutsideRange(&func); \
544 } \
545 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestFallbackWithinRange) { \
546 testFractionsToNearestFallbackWithinRange(&func); \
547 } \
548 TEST_F(LlvmLibcUfromfpxTest, FractionsToNearestFallbackOutsideRange) { \
549 testFractionsToNearestFallbackOutsideRange(&func); \
550 }
551
552#endif // LIBC_TEST_SRC_MATH_SMOKE_UFROMFPXTEST_H
553

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