@@ -26,21 +26,21 @@ using LIBC_NAMESPACE::Sign;
2626static constexpr int ROUNDING_MODES[4 ] = {FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO,
2727 FE_TONEAREST};
2828
29- template <typename F , typename I , bool TestModes = false >
29+ template <typename FloatType , typename IntType , bool TestModes = false >
3030class RoundToIntegerTestTemplate
3131 : public LIBC_NAMESPACE::testing::FEnvSafeTest {
3232public:
33- typedef I (*RoundToIntegerFunc)(F );
33+ typedef IntType (*RoundToIntegerFunc)(FloatType );
3434
3535private:
36- using FPBits = LIBC_NAMESPACE::fputil::FPBits<F >;
36+ using FPBits = LIBC_NAMESPACE::fputil::FPBits<FloatType >;
3737 using StorageType = typename FPBits::StorageType;
3838
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();
4444
4545 static constexpr StorageType MAX_NORMAL = FPBits::max_normal().uintval();
4646 static constexpr StorageType MIN_NORMAL = FPBits::min_normal().uintval();
@@ -49,11 +49,12 @@ class RoundToIntegerTestTemplate
4949 static constexpr StorageType MIN_SUBNORMAL =
5050 FPBits::min_subnormal ().uintval();
5151
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 );
5455
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) {
5758 LIBC_NAMESPACE::libc_errno = 0 ;
5859 LIBC_NAMESPACE::fputil::clear_except (FE_ALL_EXCEPT);
5960
@@ -120,35 +121,35 @@ class RoundToIntegerTestTemplate
120121 }
121122
122123 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 );
131132
132133 // The rest of this function compares with an equivalent MPFR function
133134 // which rounds floating point numbers to long values. There is no MPFR
134135 // 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 ))
138139 return ;
139140
140- constexpr int EXPONENT_LIMIT = sizeof (I ) * 8 - 1 ;
141+ constexpr int EXPONENT_LIMIT = sizeof (IntType ) * 8 - 1 ;
141142 constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
142143 if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
143144 return ;
144145 // We start with 1.0 so that the implicit bit for x86 long doubles
145146 // is set.
146- FPBits bits (F (1.0 ));
147+ FPBits bits (FloatType (1.0 ));
147148 bits.set_biased_exponent (BIASED_EXPONENT_LIMIT);
148149 bits.set_sign (Sign::NEG);
149150 bits.set_mantissa (0 );
150151
151- F x = bits.get_val ();
152+ FloatType x = bits.get_val ();
152153 long mpfr_result;
153154 bool erangeflag = mpfr::round_to_long (x, mpfr_result);
154155 ASSERT_FALSE (erangeflag);
@@ -167,10 +168,11 @@ class RoundToIntegerTestTemplate
167168 }
168169
169170 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 ),
172174 };
173- for (F x : FRACTIONS) {
175+ for (FloatType x : FRACTIONS) {
174176 long mpfr_long_result;
175177 bool erangeflag;
176178 if (TestModes)
@@ -179,7 +181,7 @@ class RoundToIntegerTestTemplate
179181 else
180182 erangeflag = mpfr::round_to_long (x, mpfr_long_result);
181183 ASSERT_FALSE (erangeflag);
182- I mpfr_result = mpfr_long_result;
184+ IntType mpfr_result = mpfr_long_result;
183185 test_one_input (func, x, mpfr_result, false );
184186 }
185187 }
@@ -201,23 +203,23 @@ class RoundToIntegerTestTemplate
201203 // This function compares with an equivalent MPFR function which rounds
202204 // floating point numbers to long values. There is no MPFR function to
203205 // 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 ))
207209 return ;
208210
209- constexpr int EXPONENT_LIMIT = sizeof (I ) * 8 - 1 ;
211+ constexpr int EXPONENT_LIMIT = sizeof (IntType ) * 8 - 1 ;
210212 constexpr int BIASED_EXPONENT_LIMIT = EXPONENT_LIMIT + FPBits::EXP_BIAS;
211213 if (BIASED_EXPONENT_LIMIT > FPBits::MAX_BIASED_EXPONENT)
212214 return ;
213215 // We start with 1.0 so that the implicit bit for x86 long doubles
214216 // is set.
215- FPBits bits (F (1.0 ));
217+ FPBits bits (FloatType (1.0 ));
216218 bits.set_biased_exponent (BIASED_EXPONENT_LIMIT);
217219 bits.set_sign (Sign::NEG);
218220 bits.set_mantissa (FPBits::FRACTION_MASK);
219221
220- F x = bits.get_val ();
222+ FloatType x = bits.get_val ();
221223 if (TestModes) {
222224 for (int m : ROUNDING_MODES) {
223225 LIBC_NAMESPACE::fputil::set_round (m);
@@ -241,29 +243,29 @@ class RoundToIntegerTestTemplate
241243 static_cast <StorageType>((MAX_SUBNORMAL - MIN_SUBNORMAL) / COUNT),
242244 StorageType (1 ));
243245 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 ))
246248 continue ;
247249 // All subnormal numbers should round to zero.
248250 if (TestModes) {
249251 if (x > 0 ) {
250252 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 );
252254 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 );
254256 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 );
256258 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 );
258260 } else {
259261 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 );
261263 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 );
263265 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 );
265267 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 );
267269 }
268270 } else {
269271 test_one_input (func, x, 0L , false );
@@ -275,9 +277,9 @@ class RoundToIntegerTestTemplate
275277 // This function compares with an equivalent MPFR function which rounds
276278 // floating point numbers to long values. There is no MPFR function to
277279 // 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 ))
281283 return ;
282284
283285 constexpr int COUNT = 1'000'001 ;
@@ -286,7 +288,7 @@ class RoundToIntegerTestTemplate
286288 StorageType (1 ));
287289 for (StorageType i = MIN_NORMAL; i <= MAX_NORMAL; i += STEP) {
288290 FPBits xbits (i);
289- F x = xbits.get_val ();
291+ FloatType x = xbits.get_val ();
290292 // In normal range on x86 platforms, the long double implicit 1 bit can be
291293 // zero making the numbers NaN. We will skip them.
292294 if (xbits.is_nan ())
@@ -297,7 +299,7 @@ class RoundToIntegerTestTemplate
297299 long mpfr_long_result;
298300 bool erangeflag = mpfr::round_to_long (x, to_mpfr_rounding_mode (m),
299301 mpfr_long_result);
300- I mpfr_result = mpfr_long_result;
302+ IntType mpfr_result = mpfr_long_result;
301303 LIBC_NAMESPACE::fputil::set_round (m);
302304 if (erangeflag)
303305 test_one_input (func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true );
@@ -307,7 +309,7 @@ class RoundToIntegerTestTemplate
307309 } else {
308310 long mpfr_long_result;
309311 bool erangeflag = mpfr::round_to_long (x, mpfr_long_result);
310- I mpfr_result = mpfr_long_result;
312+ IntType mpfr_result = mpfr_long_result;
311313 if (erangeflag)
312314 test_one_input (func, x, x > 0 ? INTEGER_MAX : INTEGER_MIN, true );
313315 else
@@ -317,9 +319,10 @@ class RoundToIntegerTestTemplate
317319 }
318320};
319321
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) \
321324 using LlvmLibcRoundToIntegerTest = \
322- RoundToIntegerTestTemplate<F, I , TestModes>; \
325+ RoundToIntegerTestTemplate<FloatType, IntType , TestModes>; \
323326 TEST_F (LlvmLibcRoundToIntegerTest, InfinityAndNaN) { \
324327 testInfinityAndNaN (&func); \
325328 } \
@@ -335,10 +338,10 @@ class RoundToIntegerTestTemplate
335338 } \
336339 TEST_F (LlvmLibcRoundToIntegerTest, NormalRange) { testNormalRange (&func); }
337340
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 )
340343
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 )
343346
344347#endif // LLVM_LIBC_TEST_SRC_MATH_ROUNDTOINTEGERTEST_H
0 commit comments