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

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