Skip to content

Commit d8f6fb4

Browse files
fix warnings
1 parent b248760 commit d8f6fb4

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

libc/src/__support/big_int.h

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,19 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array,
272272
if (LIBC_UNLIKELY(offset == 0))
273273
return array;
274274
const bool is_neg = is_signed && is_negative(array);
275-
constexpr auto at = [](size_t index) -> int {
275+
constexpr auto at = [](size_t index) -> size_t {
276276
// reverse iteration when direction == LEFT.
277277
if constexpr (direction == LEFT)
278-
return int(N) - int(index) - 1;
279-
return int(index);
278+
return N - index - 1;
279+
return index;
280280
};
281281
const auto safe_get_at = [&](size_t index) -> word {
282282
// return appropriate value when accessing out of bound elements.
283-
const int i = at(index);
283+
const size_t i = at(index);
284284
if (i < 0)
285285
return 0;
286286
if (i >= int(N))
287-
return is_neg ? -1 : 0;
287+
return is_neg ? cpp::numeric_limits<word>::max() : 0;
288288
return array[i];
289289
};
290290
const size_t index_offset = offset / WORD_BITS;
@@ -697,7 +697,8 @@ struct BigInt {
697697
}
698698
BigInt quotient;
699699
WordType x_word = static_cast<WordType>(x);
700-
constexpr size_t LOG2_WORD_SIZE = cpp::bit_width(WORD_SIZE) - 1;
700+
constexpr size_t LOG2_WORD_SIZE =
701+
static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1);
701702
constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1;
702703
constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1);
703704
// lower = smallest multiple of WORD_SIZE that is >= e.
@@ -1007,14 +1008,16 @@ struct BigInt {
10071008
BigInt quotient;
10081009
if (remainder >= divider) {
10091010
BigInt subtractor = divider;
1010-
int cur_bit = multiword::countl_zero(subtractor.val) -
1011-
multiword::countl_zero(remainder.val);
1012-
subtractor <<= cur_bit;
1011+
// TODO: failure here
1012+
int cur_bit =
1013+
(multiword::countl_zero(subtractor.val) -
1014+
multiword::countl_zero(remainder.val));
1015+
subtractor <<= static_cast<size_t>(cur_bit);
10131016
for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) {
10141017
if (remainder < subtractor)
10151018
continue;
10161019
remainder -= subtractor;
1017-
quotient.set_bit(cur_bit);
1020+
quotient.set_bit(static_cast<size_t>(cur_bit));
10181021
}
10191022
}
10201023
return Division{quotient, remainder};
@@ -1276,26 +1279,28 @@ rotr(T value, int rotate);
12761279
template <typename T>
12771280
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
12781281
rotl(T value, int rotate) {
1279-
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1282+
constexpr int N = cpp::numeric_limits<T>::digits;
12801283
rotate = rotate % N;
12811284
if (!rotate)
12821285
return value;
12831286
if (rotate < 0)
12841287
return cpp::rotr<T>(value, -rotate);
1285-
return (value << rotate) | (value >> (N - rotate));
1288+
return (value << static_cast<size_t>(rotate)) |
1289+
(value >> (N - static_cast<size_t>(rotate)));
12861290
}
12871291

12881292
// Specialization of cpp::rotr ('bit.h') for BigInt.
12891293
template <typename T>
12901294
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T>
12911295
rotr(T value, int rotate) {
1292-
constexpr unsigned N = cpp::numeric_limits<T>::digits;
1296+
constexpr int N = cpp::numeric_limits<T>::digits;
12931297
rotate = rotate % N;
12941298
if (!rotate)
12951299
return value;
12961300
if (rotate < 0)
12971301
return cpp::rotl<T>(value, -rotate);
1298-
return (value >> rotate) | (value << (N - rotate));
1302+
return (value >> static_cast<size_t>(rotate)) |
1303+
(value << (N - static_cast<size_t>(rotate)));
12991304
}
13001305

13011306
} // namespace cpp
@@ -1312,7 +1317,7 @@ mask_trailing_ones() {
13121317
T out; // zero initialized
13131318
for (size_t i = 0; i <= QUOTIENT; ++i)
13141319
out[i] = i < QUOTIENT
1315-
? -1
1320+
? cpp::numeric_limits<typename T::word_type>::max()
13161321
: mask_trailing_ones<typename T::word_type, REMAINDER>();
13171322
return out;
13181323
}
@@ -1328,7 +1333,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() {
13281333
T out; // zero initialized
13291334
for (size_t i = QUOTIENT; i < T::WORD_COUNT; ++i)
13301335
out[i] = i > QUOTIENT
1331-
? -1
1336+
? cpp::numeric_limits<typename T::word_type>::max()
13321337
: mask_leading_ones<typename T::word_type, REMAINDER>();
13331338
return out;
13341339
}

0 commit comments

Comments
 (0)