diff options
| author | Augie Fackler <augie@google.com> | 2025-03-05 13:52:31 -0500 |
|---|---|---|
| committer | Augie Fackler <augie@google.com> | 2025-03-05 16:42:40 -0500 |
| commit | da61b0ddc5dcc8f1ac64eaddabdbfec5aa23f22b (patch) | |
| tree | 8750d4c36c31e7d3babb229377b1c521dc9d21d7 /libc/src | |
| parent | 46236f4c3dbe11e14fe7ac1f4b903637efedfecf (diff) | |
Revert "[libc] Enable -Wconversion for tests. (#127523)"
This reverts commit 1e6e845d49a336e9da7ca6c576ec45c0b419b5f6 because it
changed the 1st parameter of adjust() to be unsigned, but libc itself
calls adjust() with a negative argument in align_backward() in
op_generic.h.
Diffstat (limited to 'libc/src')
23 files changed, 100 insertions, 128 deletions
diff --git a/libc/src/__support/CPP/bit.h b/libc/src/__support/CPP/bit.h index 7d138201783b..82b9eb512826 100644 --- a/libc/src/__support/CPP/bit.h +++ b/libc/src/__support/CPP/bit.h @@ -101,7 +101,7 @@ countr_zero(T value) { shift >>= 1; mask >>= shift; } - return static_cast<int>(zero_bits); + return zero_bits; } #if __has_builtin(__builtin_ctzs) ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs) @@ -140,7 +140,7 @@ countl_zero(T value) { else zero_bits |= shift; } - return static_cast<int>(zero_bits); + return zero_bits; } #if __has_builtin(__builtin_clzs) ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs) @@ -226,7 +226,7 @@ rotr(T value, int rotate); template <typename T> [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T> rotl(T value, int rotate) { - constexpr int N = cpp::numeric_limits<T>::digits; + constexpr unsigned N = cpp::numeric_limits<T>::digits; rotate = rotate % N; if (!rotate) return value; @@ -238,7 +238,7 @@ rotl(T value, int rotate) { template <typename T> [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, T> rotr(T value, int rotate) { - constexpr int N = cpp::numeric_limits<T>::digits; + constexpr unsigned N = cpp::numeric_limits<T>::digits; rotate = rotate % N; if (!rotate) return value; diff --git a/libc/src/__support/CPP/span.h b/libc/src/__support/CPP/span.h index 9234a26d201c..a41c9b744e37 100644 --- a/libc/src/__support/CPP/span.h +++ b/libc/src/__support/CPP/span.h @@ -11,7 +11,6 @@ #include <stddef.h> // For size_t #include "array.h" // For array -#include "limits.h" #include "src/__support/macros/config.h" #include "type_traits.h" // For remove_cv_t, enable_if_t, is_same_v, is_const_v @@ -49,8 +48,7 @@ public: using const_reference = const T &; using iterator = T *; - LIBC_INLINE_VAR static constexpr size_type dynamic_extent = - cpp::numeric_limits<size_type>::max(); + LIBC_INLINE_VAR static constexpr size_type dynamic_extent = -1; LIBC_INLINE constexpr span() : span_data(nullptr), span_size(0) {} @@ -60,7 +58,7 @@ public: : span_data(first), span_size(count) {} LIBC_INLINE constexpr span(pointer first, pointer end) - : span_data(first), span_size(static_cast<size_t>(end - first)) {} + : span_data(first), span_size(end - first) {} template <typename U, size_t N, cpp::enable_if_t<is_compatible_v<U>, bool> = true> diff --git a/libc/src/__support/CPP/string.h b/libc/src/__support/CPP/string.h index 1ac04c7f1f9d..dbc0ae04e5e6 100644 --- a/libc/src/__support/CPP/string.h +++ b/libc/src/__support/CPP/string.h @@ -67,8 +67,7 @@ public: : string(cstr, ::LIBC_NAMESPACE::internal::string_length(cstr)) {} LIBC_INLINE string(size_t size_, char value) { resize(size_); - static_assert(sizeof(char) == sizeof(uint8_t)); - inline_memset((void *)buffer_, static_cast<uint8_t>(value), size_); + inline_memset((void *)buffer_, value, size_); } LIBC_INLINE string &operator=(const string &other) { diff --git a/libc/src/__support/CPP/string_view.h b/libc/src/__support/CPP/string_view.h index aa15814b2e14..745c62c35f0a 100644 --- a/libc/src/__support/CPP/string_view.h +++ b/libc/src/__support/CPP/string_view.h @@ -9,7 +9,6 @@ #ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRING_VIEW_H #define LLVM_LIBC_SRC___SUPPORT_CPP_STRING_VIEW_H -#include "limits.h" #include "src/__support/common.h" #include "src/__support/macros/config.h" @@ -41,7 +40,7 @@ private: LIBC_INLINE static constexpr size_t length(const char *Str) { for (const char *End = Str;; ++End) if (*End == '\0') - return static_cast<size_t>(End - Str); + return End - Str; } LIBC_INLINE bool equals(string_view Other) const { @@ -62,8 +61,7 @@ public: // special value equal to the maximum value representable by the type // size_type. - LIBC_INLINE_VAR static constexpr size_t npos = - cpp::numeric_limits<size_t>::max(); + LIBC_INLINE_VAR static constexpr size_t npos = -1; LIBC_INLINE constexpr string_view() : Data(nullptr), Len(0) {} diff --git a/libc/src/__support/FPUtil/FPBits.h b/libc/src/__support/FPUtil/FPBits.h index bee8d0a8dc47..90b6e406e0f3 100644 --- a/libc/src/__support/FPUtil/FPBits.h +++ b/libc/src/__support/FPUtil/FPBits.h @@ -247,11 +247,11 @@ protected: using UP::UP; LIBC_INLINE constexpr BiasedExponent(Exponent exp) - : UP(static_cast<uint32_t>(static_cast<int32_t>(exp) + EXP_BIAS)) {} + : UP(static_cast<int32_t>(exp) + EXP_BIAS) {} // Cast operator to get convert from BiasedExponent to Exponent. LIBC_INLINE constexpr operator Exponent() const { - return Exponent(static_cast<int32_t>(UP::value - EXP_BIAS)); + return Exponent(UP::value - EXP_BIAS); } LIBC_INLINE constexpr BiasedExponent &operator++() { @@ -686,7 +686,7 @@ public: } LIBC_INLINE constexpr void set_biased_exponent(StorageType biased) { - UP::set_biased_exponent(BiasedExponent(static_cast<uint32_t>(biased))); + UP::set_biased_exponent(BiasedExponent((int32_t)biased)); } LIBC_INLINE constexpr int get_exponent() const { diff --git a/libc/src/__support/FPUtil/NormalFloat.h b/libc/src/__support/FPUtil/NormalFloat.h index a2f285fc6fb9..b4cbb5042a68 100644 --- a/libc/src/__support/FPUtil/NormalFloat.h +++ b/libc/src/__support/FPUtil/NormalFloat.h @@ -105,7 +105,7 @@ template <typename T> struct NormalFloat { constexpr int SUBNORMAL_EXPONENT = -FPBits<T>::EXP_BIAS + 1; if (exponent < SUBNORMAL_EXPONENT) { - unsigned shift = static_cast<unsigned>(SUBNORMAL_EXPONENT - exponent); + unsigned shift = SUBNORMAL_EXPONENT - exponent; // Since exponent > subnormalExponent, shift is strictly greater than // zero. if (shift <= FPBits<T>::FRACTION_LEN + 1) { @@ -160,7 +160,7 @@ private: if (bits.is_subnormal()) { unsigned shift = evaluate_normalization_shift(bits.get_mantissa()); mantissa = static_cast<StorageType>(bits.get_mantissa() << shift); - exponent = 1 - FPBits<T>::EXP_BIAS - static_cast<int32_t>(shift); + exponent = 1 - FPBits<T>::EXP_BIAS - shift; } else { exponent = bits.get_biased_exponent() - FPBits<T>::EXP_BIAS; mantissa = ONE | bits.get_mantissa(); diff --git a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h index 914155a01631..18b0631324f8 100644 --- a/libc/src/__support/FPUtil/aarch64/FEnvImpl.h +++ b/libc/src/__support/FPUtil/aarch64/FEnvImpl.h @@ -110,7 +110,7 @@ LIBC_INLINE int enable_except(int excepts) { (controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F; controlWord |= (newExcepts << FEnv::ExceptionControlFlagsBitPosition); FEnv::writeControlWord(controlWord); - return FEnv::exceptionStatusToMacro(static_cast<uint32_t>(oldExcepts)); + return FEnv::exceptionStatusToMacro(oldExcepts); } LIBC_INLINE int disable_except(int excepts) { @@ -120,12 +120,12 @@ LIBC_INLINE int disable_except(int excepts) { (controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F; controlWord &= ~(disabledExcepts << FEnv::ExceptionControlFlagsBitPosition); FEnv::writeControlWord(controlWord); - return FEnv::exceptionStatusToMacro(static_cast<uint32_t>(oldExcepts)); + return FEnv::exceptionStatusToMacro(oldExcepts); } LIBC_INLINE int get_except() { uint32_t controlWord = FEnv::getControlWord(); - uint32_t enabledExcepts = + int enabledExcepts = (controlWord >> FEnv::ExceptionControlFlagsBitPosition) & 0x1F; return FEnv::exceptionStatusToMacro(enabledExcepts); } @@ -250,10 +250,8 @@ LIBC_INLINE int set_round(int mode) { } uint32_t controlWord = FEnv::getControlWord(); - controlWord &= - static_cast<uint32_t>(~(0x3 << FEnv::RoundingControlBitPosition)); - controlWord |= - static_cast<uint32_t>(bitValue << FEnv::RoundingControlBitPosition); + controlWord &= ~(0x3 << FEnv::RoundingControlBitPosition); + controlWord |= (bitValue << FEnv::RoundingControlBitPosition); FEnv::writeControlWord(controlWord); return 0; diff --git a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h index dcce76b6116b..969e70796d1f 100644 --- a/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h +++ b/libc/src/__support/FPUtil/aarch64/fenv_darwin_impl.h @@ -63,7 +63,7 @@ struct FEnv { // __fpcr_flush_to_zero bit in the FPCR register. This control bit is // located in a different place from FE_FLUSHTOZERO status bit relative to // the other exceptions. - LIBC_INLINE static uint32_t exception_value_from_status(uint32_t status) { + LIBC_INLINE static uint32_t exception_value_from_status(int status) { return ((status & FE_INVALID) ? EX_INVALID : 0) | ((status & FE_DIVBYZERO) ? EX_DIVBYZERO : 0) | ((status & FE_OVERFLOW) ? EX_OVERFLOW : 0) | @@ -72,7 +72,7 @@ struct FEnv { ((status & FE_FLUSHTOZERO) ? EX_FLUSHTOZERO : 0); } - LIBC_INLINE static uint32_t exception_value_from_control(uint32_t control) { + LIBC_INLINE static uint32_t exception_value_from_control(int control) { return ((control & __fpcr_trap_invalid) ? EX_INVALID : 0) | ((control & __fpcr_trap_divbyzero) ? EX_DIVBYZERO : 0) | ((control & __fpcr_trap_overflow) ? EX_OVERFLOW : 0) | @@ -81,7 +81,7 @@ struct FEnv { ((control & __fpcr_flush_to_zero) ? EX_FLUSHTOZERO : 0); } - LIBC_INLINE static uint32_t exception_value_to_status(uint32_t excepts) { + LIBC_INLINE static int exception_value_to_status(uint32_t excepts) { return ((excepts & EX_INVALID) ? FE_INVALID : 0) | ((excepts & EX_DIVBYZERO) ? FE_DIVBYZERO : 0) | ((excepts & EX_OVERFLOW) ? FE_OVERFLOW : 0) | @@ -90,7 +90,7 @@ struct FEnv { ((excepts & EX_FLUSHTOZERO) ? FE_FLUSHTOZERO : 0); } - LIBC_INLINE static uint32_t exception_value_to_control(uint32_t excepts) { + LIBC_INLINE static int exception_value_to_control(uint32_t excepts) { return ((excepts & EX_INVALID) ? __fpcr_trap_invalid : 0) | ((excepts & EX_DIVBYZERO) ? __fpcr_trap_divbyzero : 0) | ((excepts & EX_OVERFLOW) ? __fpcr_trap_overflow : 0) | @@ -113,37 +113,34 @@ struct FEnv { }; LIBC_INLINE int enable_except(int excepts) { - uint32_t new_excepts = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); + uint32_t new_excepts = FEnv::exception_value_from_status(excepts); uint32_t control_word = FEnv::get_control_word(); uint32_t old_excepts = FEnv::exception_value_from_control(control_word); if (new_excepts != old_excepts) { control_word |= FEnv::exception_value_to_control(new_excepts); FEnv::set_control_word(control_word); } - return static_cast<int>(FEnv::exception_value_to_status(old_excepts)); + return FEnv::exception_value_to_status(old_excepts); } LIBC_INLINE int disable_except(int excepts) { - uint32_t disabled_excepts = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); + uint32_t disabled_excepts = FEnv::exception_value_from_status(excepts); uint32_t control_word = FEnv::get_control_word(); uint32_t old_excepts = FEnv::exception_value_from_control(control_word); control_word &= ~FEnv::exception_value_to_control(disabled_excepts); FEnv::set_control_word(control_word); - return static_cast<int>(FEnv::exception_value_to_status(old_excepts)); + return FEnv::exception_value_to_status(old_excepts); } LIBC_INLINE int get_except() { uint32_t control_word = FEnv::get_control_word(); uint32_t enabled_excepts = FEnv::exception_value_from_control(control_word); - return static_cast<int>(FEnv::exception_value_to_status(enabled_excepts)); + return FEnv::exception_value_to_status(enabled_excepts); } LIBC_INLINE int clear_except(int excepts) { uint32_t status_word = FEnv::get_status_word(); - uint32_t except_value = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); + uint32_t except_value = FEnv::exception_value_from_status(excepts); status_word &= ~FEnv::exception_value_to_status(except_value); FEnv::set_status_word(status_word); return 0; @@ -151,16 +148,13 @@ LIBC_INLINE int clear_except(int excepts) { LIBC_INLINE int test_except(int excepts) { uint32_t statusWord = FEnv::get_status_word(); - uint32_t ex_value = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); - return static_cast<int>(statusWord & - FEnv::exception_value_to_status(ex_value)); + uint32_t ex_value = FEnv::exception_value_from_status(excepts); + return statusWord & FEnv::exception_value_to_status(ex_value); } LIBC_INLINE int set_except(int excepts) { uint32_t status_word = FEnv::get_status_word(); - uint32_t new_exceptions = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); + uint32_t new_exceptions = FEnv::exception_value_from_status(excepts); status_word |= FEnv::exception_value_to_status(new_exceptions); FEnv::set_status_word(status_word); return 0; @@ -180,8 +174,7 @@ LIBC_INLINE int raise_except(int excepts) { : "s0", "s1" /* s0 and s1 are clobbered */); }; - uint32_t to_raise = - FEnv::exception_value_from_status(static_cast<uint32_t>(excepts)); + uint32_t to_raise = FEnv::exception_value_from_status(excepts); int result = 0; if (to_raise & FEnv::EX_INVALID) { @@ -244,7 +237,7 @@ LIBC_INLINE int get_round() { } LIBC_INLINE int set_round(int mode) { - uint32_t bit_value; + uint16_t bit_value; switch (mode) { case FE_TONEAREST: bit_value = FEnv::TONEAREST; @@ -263,7 +256,7 @@ LIBC_INLINE int set_round(int mode) { } uint32_t control_word = FEnv::get_control_word(); - control_word &= ~(0x3u << FEnv::ROUNDING_CONTROL_BIT_POSITION); + control_word &= ~(0x3 << FEnv::ROUNDING_CONTROL_BIT_POSITION); control_word |= (bit_value << FEnv::ROUNDING_CONTROL_BIT_POSITION); FEnv::set_control_word(control_word); diff --git a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h index 0ba836d17a08..9492d52da045 100644 --- a/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h +++ b/libc/src/__support/FPUtil/generic/sqrt_80_bit_long_double.h @@ -24,7 +24,7 @@ namespace x86 { LIBC_INLINE void normalize(int &exponent, FPBits<long double>::StorageType &mantissa) { const unsigned int shift = static_cast<unsigned int>( - static_cast<size_t>(cpp::countl_zero(static_cast<uint64_t>(mantissa))) - + cpp::countl_zero(static_cast<uint64_t>(mantissa)) - (8 * sizeof(uint64_t) - 1 - FPBits<long double>::FRACTION_LEN)); exponent -= shift; mantissa <<= shift; diff --git a/libc/src/__support/OSUtil/darwin/io.h b/libc/src/__support/OSUtil/darwin/io.h index 69df99da522f..a5f7ecbd7036 100644 --- a/libc/src/__support/OSUtil/darwin/io.h +++ b/libc/src/__support/OSUtil/darwin/io.h @@ -17,8 +17,7 @@ namespace LIBC_NAMESPACE_DECL { LIBC_INLINE void write_to_stderr(cpp::string_view msg) { LIBC_NAMESPACE::syscall_impl(4 /*SYS_write*/, 2 /* stderr */, - reinterpret_cast<long>(msg.data()), - static_cast<long>(msg.size())); + reinterpret_cast<long>(msg.data()), msg.size()); } } // namespace LIBC_NAMESPACE_DECL diff --git a/libc/src/__support/big_int.h b/libc/src/__support/big_int.h index f44624a7eafc..e726a094b5da 100644 --- a/libc/src/__support/big_int.h +++ b/libc/src/__support/big_int.h @@ -284,7 +284,7 @@ LIBC_INLINE constexpr cpp::array<word, N> shift(cpp::array<word, N> array, if (i < 0) return 0; if (i >= int(N)) - return is_neg ? cpp::numeric_limits<word>::max() : 0; + return is_neg ? -1 : 0; return array[i]; }; const size_t index_offset = offset / WORD_BITS; @@ -696,8 +696,7 @@ public: } BigInt quotient; WordType x_word = static_cast<WordType>(x); - constexpr size_t LOG2_WORD_SIZE = - static_cast<size_t>(cpp::bit_width(WORD_SIZE) - 1); + constexpr size_t LOG2_WORD_SIZE = cpp::bit_width(WORD_SIZE) - 1; constexpr size_t HALF_WORD_SIZE = WORD_SIZE >> 1; constexpr WordType HALF_MASK = ((WordType(1) << HALF_WORD_SIZE) - 1); // lower = smallest multiple of WORD_SIZE that is >= e. @@ -1009,12 +1008,12 @@ private: BigInt subtractor = divider; int cur_bit = multiword::countl_zero(subtractor.val) - multiword::countl_zero(remainder.val); - subtractor <<= static_cast<size_t>(cur_bit); + subtractor <<= cur_bit; for (; cur_bit >= 0 && remainder > 0; --cur_bit, subtractor >>= 1) { if (remainder < subtractor) continue; remainder -= subtractor; - quotient.set_bit(static_cast<size_t>(cur_bit)); + quotient.set_bit(cur_bit); } } return Division{quotient, remainder}; @@ -1276,28 +1275,26 @@ rotr(T value, int rotate); template <typename T> [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> rotl(T value, int rotate) { - constexpr int N = cpp::numeric_limits<T>::digits; + constexpr unsigned N = cpp::numeric_limits<T>::digits; rotate = rotate % N; if (!rotate) return value; if (rotate < 0) return cpp::rotr<T>(value, -rotate); - return (value << static_cast<size_t>(rotate)) | - (value >> (N - static_cast<size_t>(rotate))); + return (value << rotate) | (value >> (N - rotate)); } // Specialization of cpp::rotr ('bit.h') for BigInt. template <typename T> [[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> rotr(T value, int rotate) { - constexpr int N = cpp::numeric_limits<T>::digits; + constexpr unsigned N = cpp::numeric_limits<T>::digits; rotate = rotate % N; if (!rotate) return value; if (rotate < 0) return cpp::rotl<T>(value, -rotate); - return (value >> static_cast<size_t>(rotate)) | - (value << (N - static_cast<size_t>(rotate))); + return (value >> rotate) | (value << (N - rotate)); } } // namespace cpp @@ -1314,7 +1311,7 @@ mask_trailing_ones() { T out; // zero initialized for (size_t i = 0; i <= QUOTIENT; ++i) out[i] = i < QUOTIENT - ? cpp::numeric_limits<typename T::word_type>::max() + ? -1 : mask_trailing_ones<typename T::word_type, REMAINDER>(); return out; } @@ -1330,7 +1327,7 @@ LIBC_INLINE constexpr cpp::enable_if_t<is_big_int_v<T>, T> mask_leading_ones() { T out; // zero initialized for (size_t i = QUOTIENT; i < T::WORD_COUNT; ++i) out[i] = i > QUOTIENT - ? cpp::numeric_limits<typename T::word_type>::max() + ? -1 : mask_leading_ones<typename T::word_type, REMAINDER>(); return out; } diff --git a/libc/src/__support/high_precision_decimal.h b/libc/src/__support/high_precision_decimal.h index cb4b50c31544..922dce484aa6 100644 --- a/libc/src/__support/high_precision_decimal.h +++ b/libc/src/__support/high_precision_decimal.h @@ -264,7 +264,7 @@ private: LIBC_INLINE void left_shift(uint32_t shift_amount) { uint32_t new_digits = this->get_num_new_digits(shift_amount); - int32_t read_index = static_cast<int32_t>(this->num_digits - 1); + int32_t read_index = this->num_digits - 1; uint32_t write_index = this->num_digits + new_digits; uint64_t accumulator = 0; @@ -329,7 +329,7 @@ public: if (saw_dot) { break; } - this->decimal_point = static_cast<int32_t>(total_digits); + this->decimal_point = total_digits; saw_dot = true; } else { if (num_string[num_cur] == '0' && this->num_digits == 0) { @@ -350,7 +350,7 @@ public: } if (!saw_dot) - this->decimal_point = static_cast<int32_t>(total_digits); + this->decimal_point = total_digits; if (num_cur < num_len && (num_string[num_cur] == 'e' || num_string[num_cur] == 'E')) { @@ -393,7 +393,7 @@ public: this->left_shift(MAX_SHIFT_AMOUNT); shift_amount -= MAX_SHIFT_AMOUNT; } - this->left_shift(static_cast<uint32_t>(shift_amount)); + this->left_shift(shift_amount); } // Right else { @@ -401,7 +401,7 @@ public: this->right_shift(MAX_SHIFT_AMOUNT); shift_amount += MAX_SHIFT_AMOUNT; } - this->right_shift(static_cast<uint32_t>(-shift_amount)); + this->right_shift(-shift_amount); } } @@ -424,8 +424,8 @@ public: result *= 10; ++cur_digit; } - return result + - static_cast<T>(this->should_round_up(this->decimal_point, round)); + return result + static_cast<unsigned int>( + this->should_round_up(this->decimal_point, round)); } // Extra functions for testing. diff --git a/libc/src/__support/integer_literals.h b/libc/src/__support/integer_literals.h index f68b7ef12c87..0298ec7d088d 100644 --- a/libc/src/__support/integer_literals.h +++ b/libc/src/__support/integer_literals.h @@ -47,7 +47,7 @@ LIBC_INLINE constexpr T accumulate(int base, const uint8_t *digits, size_t size) { T value{}; for (; size; ++digits, --size) { - value *= static_cast<unsigned int>(base); + value *= base; value += *digits; } return value; diff --git a/libc/src/__support/integer_to_string.h b/libc/src/__support/integer_to_string.h index 65bdcf16b386..13dd83e9ed2b 100644 --- a/libc/src/__support/integer_to_string.h +++ b/libc/src/__support/integer_to_string.h @@ -405,7 +405,7 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString { // Returns the absolute value of 'value' as 'UNSIGNED_T'. LIBC_INLINE static UNSIGNED_T abs(T value) { if (cpp::is_unsigned_v<T> || value >= 0) - return static_cast<UNSIGNED_T>(value); // already of the right sign. + return value; // already of the right sign. // Signed integers are asymmetric (e.g., int8_t ∈ [-128, 127]). // Thus negating the type's minimum value would overflow. @@ -422,8 +422,7 @@ template <typename T, typename Fmt = radix::Dec> class IntegerToString { if (value == cpp::numeric_limits<T>::min()) { return cpp::bit_cast<UNSIGNED_T>(value); } else { - return static_cast<UNSIGNED_T>( - -value); // legal and representable both as T and UNSIGNED_T.` + return -value; // legal and representable both as T and UNSIGNED_T.` } } diff --git a/libc/src/__support/memory_size.h b/libc/src/__support/memory_size.h index 3d40b113bcb6..cdd6a10222de 100644 --- a/libc/src/__support/memory_size.h +++ b/libc/src/__support/memory_size.h @@ -77,8 +77,7 @@ public: if (!cpp::has_single_bit(alignment) || alignment > MAX_MEM_SIZE || !valid()) return SafeMemSize{type{-1}}; - type offset = - static_cast<type>(offset_to(static_cast<size_t>(value), alignment)); + type offset = offset_to(value, alignment); if (LIBC_UNLIKELY(offset > static_cast<type>(MAX_MEM_SIZE) - value)) return SafeMemSize{type{-1}}; diff --git a/libc/src/__support/str_to_float.h b/libc/src/__support/str_to_float.h index 48c88309c58e..b4d5646822df 100644 --- a/libc/src/__support/str_to_float.h +++ b/libc/src/__support/str_to_float.h @@ -108,11 +108,11 @@ eisel_lemire(ExpandedFloat<T> init_num, } // Normalization - uint32_t clz = static_cast<uint32_t>(cpp::countl_zero<StorageType>(mantissa)); + uint32_t clz = cpp::countl_zero<StorageType>(mantissa); mantissa <<= clz; - int32_t exp2 = exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - - static_cast<int32_t>(clz); + int32_t exp2 = + exp10_to_exp2(exp10) + FPBits::STORAGE_LEN + FPBits::EXP_BIAS - clz; // Multiplication const uint64_t *power_of_ten = @@ -225,8 +225,8 @@ eisel_lemire<long double>(ExpandedFloat<long double> init_num, } // Normalization - int32_t clz = static_cast<int32_t>(cpp::countl_zero(mantissa)) - - ((sizeof(UInt128) - sizeof(StorageType)) * CHAR_BIT); + uint32_t clz = cpp::countl_zero(mantissa) - + ((sizeof(UInt128) - sizeof(StorageType)) * CHAR_BIT); mantissa <<= clz; int32_t exp2 = @@ -802,7 +802,7 @@ LIBC_INLINE FloatConvertReturn<T> binary_exp_to_float(ExpandedFloat<T> init_num, // Handle subnormals. if (biased_exponent <= 0) { - amount_to_shift_right += static_cast<uint32_t>(1 - biased_exponent); + amount_to_shift_right += 1 - biased_exponent; biased_exponent = 0; if (amount_to_shift_right > FPBits::STORAGE_LEN) { @@ -909,7 +909,7 @@ decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT, cpp::numeric_limits<StorageType>::max() / BASE; while (true) { if (isdigit(src[index])) { - uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index])); + uint32_t digit = b36_char_to_int(src[index]); seen_digit = true; if (mantissa < bitstype_max_div_by_base) { @@ -956,7 +956,7 @@ decimal_string_to_float(const char *__restrict src, const char DECIMAL_POINT, if (result.has_error()) output.error = result.error; int32_t add_to_exponent = result.value; - index += static_cast<size_t>(result.parsed_len); + index += result.parsed_len; // Here we do this operation as int64 to avoid overflow. int64_t temp_exponent = static_cast<int64_t>(exponent) + @@ -1020,7 +1020,7 @@ hexadecimal_string_to_float(const char *__restrict src, cpp::numeric_limits<StorageType>::max() / BASE; while (true) { if (isalnum(src[index])) { - uint32_t digit = static_cast<uint32_t>(b36_char_to_int(src[index])); + uint32_t digit = b36_char_to_int(src[index]); if (digit < BASE) seen_digit = true; else @@ -1070,7 +1070,7 @@ hexadecimal_string_to_float(const char *__restrict src, output.error = result.error; int32_t add_to_exponent = result.value; - index += static_cast<size_t>(result.parsed_len); + index += result.parsed_len; // Here we do this operation as int64 to avoid overflow. int64_t temp_exponent = static_cast<int64_t>(exponent) + @@ -1135,7 +1135,7 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) { int error = 0; - size_t index = static_cast<size_t>(first_non_whitespace(src) - src); + ptrdiff_t index = first_non_whitespace(src) - src; if (src[index] == '+' || src[index] == '-') { sign = src[index]; @@ -1245,7 +1245,7 @@ LIBC_INLINE StrToNumResult<T> strtofloatingpoint(const char *__restrict src) { // special 80 bit long doubles. Otherwise it should be inlined out. set_implicit_bit<T>(result); - return {result.get_val(), static_cast<ptrdiff_t>(index), error}; + return {result.get_val(), index, error}; } template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) { diff --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h index 9212ad25d082..8e569e8a7feb 100644 --- a/libc/src/__support/str_to_integer.h +++ b/libc/src/__support/str_to_integer.h @@ -96,7 +96,7 @@ strtointeger(const char *__restrict src, int base, if (base < 0 || base == 1 || base > 36) return {0, 0, EINVAL}; - src_cur = static_cast<size_t>(first_non_whitespace(src, src_len) - src); + src_cur = first_non_whitespace(src, src_len) - src; char result_sign = '+'; if (src[src_cur] == '+' || src[src_cur] == '-') { @@ -119,7 +119,7 @@ strtointeger(const char *__restrict src, int base, ResultType const abs_max = (is_positive ? cpp::numeric_limits<T>::max() : NEGATIVE_MAX); ResultType const abs_max_div_by_base = - abs_max / static_cast<ResultType>(base); + static_cast<ResultType>(abs_max / base); while (src_cur < src_len && isalnum(src[src_cur])) { int cur_digit = b36_char_to_int(src[src_cur]); @@ -141,17 +141,17 @@ strtointeger(const char *__restrict src, int base, result = abs_max; error_val = ERANGE; } else { - result = result * static_cast<ResultType>(base); + result = static_cast<ResultType>(result * base); } - if (result > abs_max - static_cast<ResultType>(cur_digit)) { + if (result > abs_max - cur_digit) { result = abs_max; error_val = ERANGE; } else { - result = result + static_cast<ResultType>(cur_digit); + result = static_cast<ResultType>(result + cur_digit); } } - ptrdiff_t str_len = is_number ? static_cast<ptrdiff_t>(src_cur) : 0; + ptrdiff_t str_len = is_number ? (src_cur) : 0; if (error_val == ERANGE) { if (is_positive || IS_UNSIGNED) diff --git a/libc/src/stdio/printf_core/parser.h b/libc/src/stdio/printf_core/parser.h index 89556f1a9e5f..acbbaa25b1c9 100644 --- a/libc/src/stdio/printf_core/parser.h +++ b/libc/src/stdio/printf_core/parser.h @@ -57,8 +57,7 @@ template <typename T> using int_type_of_v = typename int_type_of<T>::type; if (!temp.has_value()) { \ section.has_conv = false; \ } else { \ - dst = static_cast<decltype(dst)>( \ - cpp::bit_cast<int_type_of_v<arg_type>>(temp.value())); \ + dst = cpp::bit_cast<int_type_of_v<arg_type>>(temp.value()); \ } \ } #else @@ -131,7 +130,7 @@ public: } else if (internal::isdigit(str[cur_pos])) { auto result = internal::strtointeger<int>(str + cur_pos, 10); section.min_width = result.value; - cur_pos = cur_pos + static_cast<size_t>(result.parsed_len); + cur_pos = cur_pos + result.parsed_len; } if (section.min_width < 0) { section.min_width = @@ -154,7 +153,7 @@ public: } else if (internal::isdigit(str[cur_pos])) { auto result = internal::strtointeger<int>(str + cur_pos, 10); section.precision = result.value; - cur_pos = cur_pos + static_cast<size_t>(result.parsed_len); + cur_pos = cur_pos + result.parsed_len; } } @@ -266,8 +265,7 @@ public: case ('m'): // %m is an odd conversion in that it doesn't consume an argument, it // just takes the current value of errno as its argument. - section.conv_val_raw = - static_cast<fputil::FPBits<double>::StorageType>(libc_errno); + section.conv_val_raw = static_cast<int>(libc_errno); break; #endif // LIBC_COPT_PRINTF_DISABLE_STRERROR #ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT @@ -358,7 +356,7 @@ private: } if (internal::isdigit(str[*local_pos])) { const auto result = internal::strtointeger<int>(str + *local_pos, 10); - *local_pos += static_cast<size_t>(result.parsed_len); + *local_pos += result.parsed_len; return {lm, static_cast<size_t>(cpp::max(0, result.value))}; } return {lm, 0}; @@ -407,10 +405,10 @@ private: LIBC_INLINE size_t parse_index(size_t *local_pos) { if (internal::isdigit(str[*local_pos])) { auto result = internal::strtointeger<int>(str + *local_pos, 10); - size_t index = static_cast<size_t>(result.value); - if (str[*local_pos + static_cast<size_t>(result.parsed_len)] != '$') + size_t index = result.value; + if (str[*local_pos + result.parsed_len] != '$') return 0; - *local_pos = static_cast<size_t>(1 + result.parsed_len) + *local_pos; + *local_pos = 1 + result.parsed_len + *local_pos; return index; } return 0; diff --git a/libc/src/stdio/printf_core/writer.h b/libc/src/stdio/printf_core/writer.h index 28c9a2827087..5526a478ea62 100644 --- a/libc/src/stdio/printf_core/writer.h +++ b/libc/src/stdio/printf_core/writer.h @@ -135,8 +135,7 @@ public: chars_written += static_cast<int>(length); if (LIBC_LIKELY(wb->buff_cur + length <= wb->buff_len)) { - inline_memset(wb->buff + wb->buff_cur, - static_cast<unsigned char>(new_char), length); + inline_memset(wb->buff + wb->buff_cur, new_char, length); wb->buff_cur += length; return WRITE_OK; } diff --git a/libc/src/stdio/scanf_core/parser.h b/libc/src/stdio/scanf_core/parser.h index 1e2f26e0d3fd..6cc5b30eb0db 100644 --- a/libc/src/stdio/scanf_core/parser.h +++ b/libc/src/stdio/scanf_core/parser.h @@ -78,7 +78,7 @@ public: if (internal::isdigit(str[cur_pos])) { auto result = internal::strtointeger<int>(str + cur_pos, 10); section.max_width = result.value; - cur_pos = cur_pos + static_cast<size_t>(result.parsed_len); + cur_pos = cur_pos + result.parsed_len; } // TODO(michaelrj): add posix allocate flag support. @@ -150,11 +150,10 @@ public: char b = str[cur_pos + 1]; char start = (a < b ? a : b); char end = (a < b ? b : a); - scan_set.set_range(static_cast<size_t>(start), - static_cast<size_t>(end)); + scan_set.set_range(start, end); cur_pos += 2; } else { - scan_set.set(static_cast<size_t>(str[cur_pos])); + scan_set.set(str[cur_pos]); ++cur_pos; } } @@ -238,10 +237,10 @@ private: LIBC_INLINE size_t parse_index(size_t *local_pos) { if (internal::isdigit(str[*local_pos])) { auto result = internal::strtointeger<int>(str + *local_pos, 10); - size_t index = static_cast<size_t>(result.value); - if (str[*local_pos + static_cast<size_t>(result.parsed_len)] != '$') + size_t index = result.value; + if (str[*local_pos + result.parsed_len] != '$') return 0; - *local_pos = static_cast<size_t>(1 + result.parsed_len) + *local_pos; + *local_pos = 1 + result.parsed_len + *local_pos; return index; } return 0; diff --git a/libc/src/stdlib/quick_sort.h b/libc/src/stdlib/quick_sort.h index 8ba0098854d1..9ab283025001 100644 --- a/libc/src/stdlib/quick_sort.h +++ b/libc/src/stdlib/quick_sort.h @@ -167,9 +167,7 @@ LIBC_INLINE void quick_sort_impl(A &array, const void *ancestor_pivot, } } -constexpr size_t ilog2(size_t n) { - return static_cast<size_t>(cpp::bit_width(n)) - 1; -} +constexpr size_t ilog2(size_t n) { return cpp::bit_width(n) - 1; } template <typename A, typename F> LIBC_INLINE void quick_sort(A &array, const F &is_less) { diff --git a/libc/src/string/memory_utils/utils.h b/libc/src/string/memory_utils/utils.h index 508a70794211..5c9bc72208f8 100644 --- a/libc/src/string/memory_utils/utils.h +++ b/libc/src/string/memory_utils/utils.h @@ -263,7 +263,7 @@ LIBC_INLINE void store_aligned(ValueType value, Ptr dst) { static_assert(sizeof(ValueType) >= (sizeof(T) + ... + sizeof(TS))); constexpr size_t SHIFT = sizeof(T) * 8; if constexpr (Endian::IS_LITTLE) { - store<T>(assume_aligned<sizeof(T)>(dst), T(value & T(~0))); + store<T>(assume_aligned<sizeof(T)>(dst), value & ~T(0)); if constexpr (sizeof...(TS) > 0) store_aligned<ValueType, TS...>(value >> SHIFT, dst + sizeof(T)); } else if constexpr (Endian::IS_BIG) { @@ -293,7 +293,7 @@ LIBC_INLINE void store64_aligned(uint64_t value, Ptr dst, size_t offset) { // Advances the pointers p1 and p2 by offset bytes and decrease count by the // same amount. template <typename T1, typename T2> -LIBC_INLINE void adjust(uintptr_t offset, T1 *__restrict &p1, +LIBC_INLINE void adjust(ptrdiff_t offset, T1 *__restrict &p1, T2 *__restrict &p2, size_t &count) { p1 += offset; p2 += offset; diff --git a/libc/src/string/string_utils.h b/libc/src/string/string_utils.h index e4659f65c93e..583d35014d39 100644 --- a/libc/src/string/string_utils.h +++ b/libc/src/string/string_utils.h @@ -66,7 +66,7 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) { for (; reinterpret_cast<uintptr_t>(char_ptr) % sizeof(Word) != 0; ++char_ptr) { if (*char_ptr == '\0') - return static_cast<size_t>(char_ptr - src); + return char_ptr - src; } // Step 2: read blocks for (const Word *block_ptr = reinterpret_cast<const Word *>(char_ptr); @@ -77,7 +77,7 @@ LIBC_INLINE size_t string_length_wide_read(const char *src) { for (; *char_ptr != '\0'; ++char_ptr) { ; } - return static_cast<size_t>(char_ptr - src); + return char_ptr - src; } // Returns the length of a string, denoted by the first occurrence @@ -169,7 +169,7 @@ LIBC_INLINE size_t complementary_span(const char *src, const char *segment) { for (; *src && !bitset.test(*reinterpret_cast<const unsigned char *>(src)); ++src) ; - return static_cast<size_t>(src - initial); + return src - initial; } // Given the similarities between strtok and strtok_r, we can implement both @@ -189,14 +189,12 @@ LIBC_INLINE char *string_token(char *__restrict src, if (LIBC_UNLIKELY(src == nullptr && ((src = *saveptr) == nullptr))) return nullptr; - static_assert(sizeof(char) == sizeof(cpp::byte), - "bitset of 256 assumes char is 8 bits"); cpp::bitset<256> delimiter_set; for (; *delimiter_string != '\0'; ++delimiter_string) - delimiter_set.set(static_cast<size_t>(*delimiter_string)); + delimiter_set.set(*delimiter_string); if constexpr (SkipDelim) - for (; *src != '\0' && delimiter_set.test(static_cast<size_t>(*src)); ++src) + for (; *src != '\0' && delimiter_set.test(*src); ++src) ; if (*src == '\0') { *saveptr = src; @@ -204,7 +202,7 @@ LIBC_INLINE char *string_token(char *__restrict src, } char *token = src; for (; *src != '\0'; ++src) { - if (delimiter_set.test(static_cast<size_t>(*src))) { + if (delimiter_set.test(*src)) { *src = '\0'; ++src; break; |
