Skip to content

Commit 111b062

Browse files
authored
[libc] Fix for adding macro I (#111872)
We have two (EDIT: 4) files in which we are using `I`. This PR replaces them with alternatives like `i` and `IDX` etc.
1 parent 3ed8acf commit 111b062

File tree

4 files changed

+89
-81
lines changed

4 files changed

+89
-81
lines changed

Diff for: libc/src/__support/CPP/string_view.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class string_view {
3131

3232
LIBC_INLINE static int compareMemory(const char *Lhs, const char *Rhs,
3333
size_t Length) {
34-
for (size_t I = 0; I < Length; ++I)
35-
if (int Diff = (int)Lhs[I] - (int)Rhs[I])
34+
for (size_t i = 0; i < Length; ++i)
35+
if (int Diff = (int)Lhs[i] - (int)Rhs[i])
3636
return Diff;
3737
return 0;
3838
}

Diff for: libc/src/__support/CPP/utility/in_place.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ template <class T> struct in_place_type_t {
2727
};
2828
template <class T> LIBC_INLINE_VAR constexpr in_place_type_t<T> in_place_type{};
2929

30-
template <size_t I> struct in_place_index_t {
30+
template <size_t IDX> struct in_place_index_t {
3131
LIBC_INLINE explicit in_place_index_t() = default;
3232
};
33-
template <size_t I>
34-
LIBC_INLINE_VAR constexpr in_place_index_t<I> in_place_index{};
33+
template <size_t IDX>
34+
LIBC_INLINE_VAR constexpr in_place_index_t<IDX> in_place_index{};
3535

3636
} // namespace cpp
3737
} // namespace LIBC_NAMESPACE_DECL

Diff for: libc/src/__support/FPUtil/NearestIntegerOperations.h

+23-18
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ fromfpx(T x, int rnd, unsigned int width) {
346346

347347
namespace internal {
348348

349-
template <typename F, typename I,
350-
cpp::enable_if_t<cpp::is_floating_point_v<F> && cpp::is_integral_v<I>,
349+
template <typename FloatType, typename IntType,
350+
cpp::enable_if_t<cpp::is_floating_point_v<FloatType> &&
351+
cpp::is_integral_v<IntType>,
351352
int> = 0>
352-
LIBC_INLINE I rounded_float_to_signed_integer(F x) {
353-
constexpr I INTEGER_MIN = (I(1) << (sizeof(I) * 8 - 1));
354-
constexpr I INTEGER_MAX = -(INTEGER_MIN + 1);
355-
FPBits<F> bits(x);
353+
LIBC_INLINE IntType rounded_float_to_signed_integer(FloatType x) {
354+
constexpr IntType INTEGER_MIN = (IntType(1) << (sizeof(IntType) * 8 - 1));
355+
constexpr IntType INTEGER_MAX = -(INTEGER_MIN + 1);
356+
FPBits<FloatType> bits(x);
356357
auto set_domain_error_and_raise_invalid = []() {
357358
set_errno_if_required(EDOM);
358359
raise_except_if_required(FE_INVALID);
@@ -364,7 +365,7 @@ LIBC_INLINE I rounded_float_to_signed_integer(F x) {
364365
}
365366

366367
int exponent = bits.get_exponent();
367-
constexpr int EXPONENT_LIMIT = sizeof(I) * 8 - 1;
368+
constexpr int EXPONENT_LIMIT = sizeof(IntType) * 8 - 1;
368369
if (exponent > EXPONENT_LIMIT) {
369370
set_domain_error_and_raise_invalid();
370371
return bits.is_neg() ? INTEGER_MIN : INTEGER_MAX;
@@ -374,29 +375,33 @@ LIBC_INLINE I rounded_float_to_signed_integer(F x) {
374375
return bits.is_neg() ? INTEGER_MIN : INTEGER_MAX;
375376
}
376377
// If the control reaches here, then it means that the rounded
377-
// value is the most negative number for the signed integer type I.
378+
// value is the most negative number for the signed integer type IntType.
378379
}
379380

380-
// For all other cases, if `x` can fit in the integer type `I`,
381+
// For all other cases, if `x` can fit in the integer type `IntType`,
381382
// we just return `x`. static_cast will convert the floating
382383
// point value to the exact integer value.
383-
return static_cast<I>(x);
384+
return static_cast<IntType>(x);
384385
}
385386

386387
} // namespace internal
387388

388-
template <typename F, typename I,
389-
cpp::enable_if_t<cpp::is_floating_point_v<F> && cpp::is_integral_v<I>,
389+
template <typename FloatType, typename IntType,
390+
cpp::enable_if_t<cpp::is_floating_point_v<FloatType> &&
391+
cpp::is_integral_v<IntType>,
390392
int> = 0>
391-
LIBC_INLINE I round_to_signed_integer(F x) {
392-
return internal::rounded_float_to_signed_integer<F, I>(round(x));
393+
LIBC_INLINE IntType round_to_signed_integer(FloatType x) {
394+
return internal::rounded_float_to_signed_integer<FloatType, IntType>(
395+
round(x));
393396
}
394397

395-
template <typename F, typename I,
396-
cpp::enable_if_t<cpp::is_floating_point_v<F> && cpp::is_integral_v<I>,
398+
template <typename FloatType, typename IntType,
399+
cpp::enable_if_t<cpp::is_floating_point_v<FloatType> &&
400+
cpp::is_integral_v<IntType>,
397401
int> = 0>
398-
LIBC_INLINE I round_to_signed_integer_using_current_rounding_mode(F x) {
399-
return internal::rounded_float_to_signed_integer<F, I>(
402+
LIBC_INLINE IntType
403+
round_to_signed_integer_using_current_rounding_mode(FloatType x) {
404+
return internal::rounded_float_to_signed_integer<FloatType, IntType>(
400405
round_using_current_rounding_mode(x));
401406
}
402407

Diff for: libc/test/src/math/RoundToIntegerTest.h

+61-58
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ using LIBC_NAMESPACE::Sign;
2626
static 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>
3030
class RoundToIntegerTestTemplate
3131
: public LIBC_NAMESPACE::testing::FEnvSafeTest {
3232
public:
33-
typedef I (*RoundToIntegerFunc)(F);
33+
typedef IntType (*RoundToIntegerFunc)(FloatType);
3434

3535
private:
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

Comments
 (0)