@@ -26,21 +26,21 @@ using LIBC_NAMESPACE::Sign;
26
26
static constexpr int ROUNDING_MODES[4 ] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
27
27
FE_TONEAREST};
28
28
29
- template <typename F , typename I , bool TestModes = false >
29
+ template <typename FloatType , typename IntType , bool TestModes = false >
30
30
class RoundToIntegerTestTemplate
31
31
: public LIBC_NAMESPACE::testing::FEnvSafeTest {
32
32
public:
33
- typedef I (*RoundToIntegerFunc)(F );
33
+ typedef IntType (*RoundToIntegerFunc)(FloatType );
34
34
35
35
private:
36
- using FPBits = LIBC_NAMESPACE::fputil::FPBits<F >;
36
+ using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType >;
37
37
using StorageType = typename FPBits::StorageType;
38
38
39
- const F zero = FPBits::zero().get_val();
40
- const F neg_zero = FPBits::zero(Sign::NEG).get_val();
41
- const F inf = FPBits::inf().get_val();
42
- const F neg_inf = FPBits::inf(Sign::NEG).get_val();
43
- const F nan = FPBits::quiet_nan().get_val();
39
+ const FloatType zero = FPBits::zero().get_val();
40
+ const FloatType neg_zero = FPBits::zero(Sign::NEG).get_val();
41
+ const FloatType inf = FPBits::inf().get_val();
42
+ const FloatType neg_inf = FPBits::inf(Sign::NEG).get_val();
43
+ const FloatType nan = FPBits::quiet_nan().get_val();
44
44
45
45
static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
46
46
static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
@@ -49,11 +49,12 @@ class RoundToIntegerTestTemplate
49
49
static constexpr StorageType MIN_SUBNORMAL =
50
50
FPBits::min_subnormal ().uintval();
51
51
52
- static constexpr I INTEGER_MIN = I(1 ) << (sizeof (I) * 8 - 1 );
53
- static constexpr I INTEGER_MAX = -(INTEGER_MIN + 1 );
52
+ static constexpr IntType INTEGER_MIN = IntType(1 )
53
+ << (sizeof (IntType) * 8 - 1 );
54
+ static constexpr IntType INTEGER_MAX = -(INTEGER_MIN + 1 );
54
55
55
- void test_one_input (RoundToIntegerFunc func, F input, I expected ,
56
- bool expectError) {
56
+ void test_one_input (RoundToIntegerFunc func, FloatType input,
57
+ IntType expected, bool expectError) {
57
58
LIBC_NAMESPACE::libc_errno = 0 ;
58
59
LIBC_NAMESPACE::fputil::clear_except (FE_ALL_EXCEPT);
59
60
@@ -120,35 +121,35 @@ class RoundToIntegerTestTemplate
120
121
}
121
122
122
123
void do_round_numbers_test (RoundToIntegerFunc func) {
123
- test_one_input (func, zero, I (0 ), false );
124
- test_one_input (func, neg_zero, I (0 ), false );
125
- test_one_input (func, F (1.0 ), I (1 ), false );
126
- test_one_input (func, F (-1.0 ), I (-1 ), false );
127
- test_one_input (func, F (10.0 ), I (10 ), false );
128
- test_one_input (func, F (-10.0 ), I (-10 ), false );
129
- test_one_input (func, F (1234.0 ), I (1234 ), false );
130
- test_one_input (func, F (-1234.0 ), I (-1234 ), false );
124
+ test_one_input (func, zero, IntType (0 ), false );
125
+ test_one_input (func, neg_zero, IntType (0 ), false );
126
+ test_one_input (func, FloatType (1.0 ), IntType (1 ), false );
127
+ test_one_input (func, FloatType (-1.0 ), IntType (-1 ), false );
128
+ test_one_input (func, FloatType (10.0 ), IntType (10 ), false );
129
+ test_one_input (func, FloatType (-10.0 ), IntType (-10 ), false );
130
+ test_one_input (func, FloatType (1234.0 ), IntType (1234 ), false );
131
+ test_one_input (func, FloatType (-1234.0 ), IntType (-1234 ), false );
131
132
132
133
// The rest of this function compares with an equivalent MPFR function
133
134
// which rounds floating point numbers to long values. There is no MPFR
134
135
// function to round to long long or wider integer values. So, we will
135
- // the remaining tests only if the width of I less than equal to that of
136
- // long.
137
- if (sizeof (I ) > sizeof (long ))
136
+ // the remaining tests only if the width of IntType less than equal to that
137
+ // of long.
138
+ if (sizeof (IntType ) > sizeof (long ))
138
139
return ;
139
140
140
- constexpr int EXPONENT_LIMIT = sizeof (I ) * 8 - 1 ;
141
+ constexpr int EXPONENT_LIMIT = sizeof (IntType ) * 8 - 1 ;
141
142
constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
142
143
if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
143
144
return ;
144
145
// We start with 1.0 so that the implicit bit for x86 long doubles
145
146
// is set.
146
- FPBits bits (F (1.0 ));
147
+ FPBits bits (FloatType (1.0 ));
147
148
bits.set_biased_exponent (BIASED_EXPONENT_LIMIT);
148
149
bits.set_sign (Sign::NEG);
149
150
bits.set_mantissa (0 );
150
151
151
- F x = bits.get_val ();
152
+ FloatType x = bits.get_val ();
152
153
long mpfr_result;
153
154
bool erangeflag = mpfr::round_to_long (x, mpfr_result);
154
155
ASSERT_FALSE (erangeflag);
@@ -167,10 +168,11 @@ class RoundToIntegerTestTemplate
167
168
}
168
169
169
170
void do_fractions_test (RoundToIntegerFunc func, int mode) {
170
- constexpr F FRACTIONS[] = {
171
- F (0.5 ), F (-0.5 ), F (0.115 ), F (-0.115 ), F (0.715 ), F (-0.715 ),
171
+ constexpr FloatType FRACTIONS[] = {
172
+ FloatType (0.5 ), FloatType (-0.5 ), FloatType (0.115 ),
173
+ FloatType (-0.115 ), FloatType (0.715 ), FloatType (-0.715 ),
172
174
};
173
- for (F x : FRACTIONS) {
175
+ for (FloatType x : FRACTIONS) {
174
176
long mpfr_long_result;
175
177
bool erangeflag;
176
178
if (TestModes)
@@ -179,7 +181,7 @@ class RoundToIntegerTestTemplate
179
181
else
180
182
erangeflag = mpfr::round_to_long (x, mpfr_long_result);
181
183
ASSERT_FALSE (erangeflag);
182
- I mpfr_result = mpfr_long_result;
184
+ IntType mpfr_result = mpfr_long_result;
183
185
test_one_input (func, x, mpfr_result, false );
184
186
}
185
187
}
@@ -201,23 +203,23 @@ class RoundToIntegerTestTemplate
201
203
// This function compares with an equivalent MPFR function which rounds
202
204
// floating point numbers to long values. There is no MPFR function to
203
205
// round to long long or wider integer values. So, we will peform the
204
- // comparisons in this function only if the width of I less than equal to
205
- // that of long.
206
- if (sizeof (I ) > sizeof (long ))
206
+ // comparisons in this function only if the width of IntType less than equal
207
+ // to that of long.
208
+ if (sizeof (IntType ) > sizeof (long ))
207
209
return ;
208
210
209
- constexpr int EXPONENT_LIMIT = sizeof (I ) * 8 - 1 ;
211
+ constexpr int EXPONENT_LIMIT = sizeof (IntType ) * 8 - 1 ;
210
212
constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
211
213
if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
212
214
return ;
213
215
// We start with 1.0 so that the implicit bit for x86 long doubles
214
216
// is set.
215
- FPBits bits (F (1.0 ));
217
+ FPBits bits (FloatType (1.0 ));
216
218
bits.set_biased_exponent (BIASED_EXPONENT_LIMIT);
217
219
bits.set_sign (Sign::NEG);
218
220
bits.set_mantissa (FPBits::FRACTION_MASK);
219
221
220
- F x = bits.get_val ();
222
+ FloatType x = bits.get_val ();
221
223
if (TestModes) {
222
224
for (int m : ROUNDING_MODES) {
223
225
LIBC_NAMESPACE::fputil::set_round (m);
@@ -241,29 +243,29 @@ class RoundToIntegerTestTemplate
241
243
static_cast <StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
242
244
StorageType (1 ));
243
245
for (StorageType i = MIN_SUBNORMAL; i <= MAX_SUBNORMAL; i += STEP) {
244
- F x = FPBits (i).get_val ();
245
- if (x == F (0.0 ))
246
+ FloatType x = FPBits (i).get_val ();
247
+ if (x == FloatType (0.0 ))
246
248
continue ;
247
249
// All subnormal numbers should round to zero.
248
250
if (TestModes) {
249
251
if (x > 0 ) {
250
252
LIBC_NAMESPACE::fputil::set_round (FE_UPWARD);
251
- test_one_input (func, x, I (1 ), false );
253
+ test_one_input (func, x, IntType (1 ), false );
252
254
LIBC_NAMESPACE::fputil::set_round (FE_DOWNWARD);
253
- test_one_input (func, x, I (0 ), false );
255
+ test_one_input (func, x, IntType (0 ), false );
254
256
LIBC_NAMESPACE::fputil::set_round (FE_TOWARDZERO);
255
- test_one_input (func, x, I (0 ), false );
257
+ test_one_input (func, x, IntType (0 ), false );
256
258
LIBC_NAMESPACE::fputil::set_round (FE_TONEAREST);
257
- test_one_input (func, x, I (0 ), false );
259
+ test_one_input (func, x, IntType (0 ), false );
258
260
} else {
259
261
LIBC_NAMESPACE::fputil::set_round (FE_UPWARD);
260
- test_one_input (func, x, I (0 ), false );
262
+ test_one_input (func, x, IntType (0 ), false );
261
263
LIBC_NAMESPACE::fputil::set_round (FE_DOWNWARD);
262
- test_one_input (func, x, I (-1 ), false );
264
+ test_one_input (func, x, IntType (-1 ), false );
263
265
LIBC_NAMESPACE::fputil::set_round (FE_TOWARDZERO);
264
- test_one_input (func, x, I (0 ), false );
266
+ test_one_input (func, x, IntType (0 ), false );
265
267
LIBC_NAMESPACE::fputil::set_round (FE_TONEAREST);
266
- test_one_input (func, x, I (0 ), false );
268
+ test_one_input (func, x, IntType (0 ), false );
267
269
}
268
270
} else {
269
271
test_one_input (func, x, 0L , false );
@@ -275,9 +277,9 @@ class RoundToIntegerTestTemplate
275
277
// This function compares with an equivalent MPFR function which rounds
276
278
// floating point numbers to long values. There is no MPFR function to
277
279
// round to long long or wider integer values. So, we will peform the
278
- // comparisons in this function only if the width of I less than equal to
279
- // that of long.
280
- if (sizeof (I ) > sizeof (long ))
280
+ // comparisons in this function only if the width of IntType less than equal
281
+ // to that of long.
282
+ if (sizeof (IntType ) > sizeof (long ))
281
283
return ;
282
284
283
285
constexpr int COUNT = 1'000'001 ;
@@ -286,7 +288,7 @@ class RoundToIntegerTestTemplate
286
288
StorageType (1 ));
287
289
for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
288
290
FPBits xbits (i);
289
- F x = xbits.get_val ();
291
+ FloatType x = xbits.get_val ();
290
292
// In normal range on x86 platforms, the long double implicit 1 bit can be
291
293
// zero making the numbers NaN. We will skip them.
292
294
if (xbits.is_nan ())
@@ -297,7 +299,7 @@ class RoundToIntegerTestTemplate
297
299
long mpfr_long_result;
298
300
bool erangeflag = mpfr::round_to_long (x, to_mpfr_rounding_mode (m),
299
301
mpfr_long_result);
300
- I mpfr_result = mpfr_long_result;
302
+ IntType mpfr_result = mpfr_long_result;
301
303
LIBC_NAMESPACE::fputil::set_round (m);
302
304
if (erangeflag)
303
305
test_one_input (func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true );
@@ -307,7 +309,7 @@ class RoundToIntegerTestTemplate
307
309
} else {
308
310
long mpfr_long_result;
309
311
bool erangeflag = mpfr::round_to_long (x, mpfr_long_result);
310
- I mpfr_result = mpfr_long_result;
312
+ IntType mpfr_result = mpfr_long_result;
311
313
if (erangeflag)
312
314
test_one_input (func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true );
313
315
else
@@ -317,9 +319,10 @@ class RoundToIntegerTestTemplate
317
319
}
318
320
};
319
321
320
- #define LIST_ROUND_TO_INTEGER_TESTS_HELPER (F, I, func, TestModes ) \
322
+ #define LIST_ROUND_TO_INTEGER_TESTS_HELPER (FloatType, IntType, func, \
323
+ TestModes) \
321
324
using LlvmLibcRoundToIntegerTest = \
322
- RoundToIntegerTestTemplate<F, I , TestModes>; \
325
+ RoundToIntegerTestTemplate<FloatType, IntType , TestModes>; \
323
326
TEST_F (LlvmLibcRoundToIntegerTest, InfinityAndNaN) { \
324
327
testInfinityAndNaN (&func); \
325
328
} \
@@ -335,10 +338,10 @@ class RoundToIntegerTestTemplate
335
338
} \
336
339
TEST_F (LlvmLibcRoundToIntegerTest, NormalRange) { testNormalRange (&func); }
337
340
338
- #define LIST_ROUND_TO_INTEGER_TESTS (F, I , func ) \
339
- LIST_ROUND_TO_INTEGER_TESTS_HELPER (F, I , func, false )
341
+ #define LIST_ROUND_TO_INTEGER_TESTS (FloatType, IntType , func ) \
342
+ LIST_ROUND_TO_INTEGER_TESTS_HELPER (FloatType, IntType , func, false )
340
343
341
- #define LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES (F, I , func ) \
342
- LIST_ROUND_TO_INTEGER_TESTS_HELPER (F, I , func, true )
344
+ #define LIST_ROUND_TO_INTEGER_TESTS_WITH_MODES (FloatType, IntType , func ) \
345
+ LIST_ROUND_TO_INTEGER_TESTS_HELPER (FloatType, IntType , func, true )
343
346
344
347
#endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
0 commit comments