diff --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h --- a/libc/src/__support/float_to_string.h +++ b/libc/src/__support/float_to_string.h @@ -178,7 +178,8 @@ // ~1000 for double and ~16000 for long double. Be warned that the time // complexity of exponentiation is O(n^2 * log_2(m)) where n is the number of // bits in the number being exponentiated and m is the exponent. - const int shift_amount = exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * i); + const int shift_amount = + static_cast(exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * i)); if (shift_amount < 0) { return 1; } @@ -221,7 +222,8 @@ // doubles are only accurate to ~35 digits, the 50 digits of accuracy are // enough for these floats to be converted back and forth safely. This is // ideal for avoiding the size of the long double table. - const int shift_amount = exponent + CALC_SHIFT_CONST - (9 * i); + const int shift_amount = + static_cast(exponent + CALC_SHIFT_CONST - (9 * i)); if (shift_amount < 0) { return 1; } @@ -288,7 +290,7 @@ } else { ten_blocks = 0; five_blocks = i; - shift_amount = shift_amount + (i * BLOCK_SIZE); + shift_amount = static_cast(shift_amount + (i * BLOCK_SIZE)); } } @@ -375,8 +377,9 @@ {0x31680A88F8953031u, 0x89705F4136B4A597u, 0}); const auto middle = (mult_const * val); const uint64_t result = static_cast(middle[2]); - const uint32_t shifted = result >> 29; - return static_cast(val) - (1000000000 * shifted); + const uint64_t shifted = result >> 29; + return static_cast(static_cast(val) - + (1000000000 * shifted)); } LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa, @@ -390,7 +393,8 @@ wide_mant[1] = static_cast(mantissa >> 64); val = (val * wide_mant) >> shift_amount; - return val.div_uint32_times_pow_2(1000000000, 0).value()[0]; + return static_cast( + val.div_uint32_times_pow_2(1000000000, 0).value()); // return fast_uint_mod_1e9(val); } @@ -596,7 +600,7 @@ return false; #else const int32_t idx = -exponent / IDX_SIZE; - const uint32_t p = POW10_OFFSET_2[idx] + block_index - MIN_BLOCK_2[idx]; + const size_t p = POW10_OFFSET_2[idx] + block_index - MIN_BLOCK_2[idx]; // If the remaining digits are all 0, then this is the lowest block. return p >= POW10_OFFSET_2[idx + 1]; #endif diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h --- a/libc/src/stdio/printf_core/float_dec_converter.h +++ b/libc/src/stdio/printf_core/float_dec_converter.h @@ -67,7 +67,8 @@ int write_left_padding(Writer *writer, size_t total_digits) { // The pattern is (spaces) (sign) (zeroes), but only one of spaces and // zeroes can be written, and only if the padding amount is positive. - int padding_amount = min_width - total_digits - (sign_char > 0 ? 1 : 0); + int padding_amount = + static_cast(min_width - total_digits - (sign_char > 0 ? 1 : 0)); if (left_justified || padding_amount < 0) { if (sign_char > 0) { RET_IF_RESULT_NEGATIVE(writer->write(sign_char)); @@ -89,7 +90,8 @@ int write_right_padding(Writer *writer, size_t total_digits) { // If and only if the conversion is left justified, there may be trailing // spaces. - int padding_amount = min_width - total_digits - (sign_char > 0 ? 1 : 0); + int padding_amount = + static_cast(min_width - total_digits - (sign_char > 0 ? 1 : 0)); if (left_justified && padding_amount > 0) { RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_amount)); } @@ -288,7 +290,7 @@ // copy the last block_digits characters into the start of end_buff. // TODO: Replace with memcpy - for (int count = block_digits - 1; count >= 0; --count) { + for (size_t count = 0; count < block_digits; ++count) { end_buff[count] = int_to_str[count + 1 + (BLOCK_SIZE - block_digits)]; } @@ -306,7 +308,8 @@ (round == RoundDirection::Even && low_digit % 2 != 0)) { bool has_carry = true; // handle the low block that we're adding - for (int count = block_digits - 1; count >= 0 && has_carry; --count) { + for (int count = static_cast(block_digits) - 1; + count >= 0 && has_carry; --count) { if (end_buff[count] == '9') { end_buff[count] = '0'; } else { @@ -315,7 +318,8 @@ } } // handle the high block that's buffered - for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) { + for (int count = static_cast(buffered_digits) - 1; + count >= 0 && has_carry; --count) { if (block_buffer[count] == '9') { block_buffer[count] = '0'; } else { @@ -374,7 +378,7 @@ // copy the last block_digits characters into the start of end_buff. // TODO: Replace with memcpy - for (int count = block_digits - 1; count >= 0; --count) { + for (size_t count = 0; count < block_digits; ++count) { end_buff[count] = int_to_str[count + 1 + (BLOCK_SIZE - block_digits)]; } } @@ -393,7 +397,8 @@ (round == RoundDirection::Even && low_digit % 2 != 0)) { bool has_carry = true; // handle the low block that we're adding - for (int count = block_digits - 1; count >= 0 && has_carry; --count) { + for (int count = static_cast(block_digits) - 1; + count >= 0 && has_carry; --count) { if (end_buff[count] == '9') { end_buff[count] = '0'; } else { @@ -402,7 +407,8 @@ } } // handle the high block that's buffered - for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) { + for (int count = static_cast(buffered_digits) - 1; + count >= 0 && has_carry; --count) { if (block_buffer[count] == '9') { block_buffer[count] = '0'; } else { @@ -519,7 +525,7 @@ sign_char = ' '; // If to_conv doesn't specify a precision, the precision defaults to 6. - const size_t precision = to_conv.precision < 0 ? 6 : to_conv.precision; + const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision; bool has_decimal_point = (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0); @@ -532,7 +538,7 @@ FloatWriter float_writer(writer, has_decimal_point, padding_writer); FloatToString float_converter(static_cast(float_bits)); - const uint32_t positive_blocks = float_converter.get_positive_blocks(); + const size_t positive_blocks = float_converter.get_positive_blocks(); if (positive_blocks >= 0) { // This loop iterates through the number a block at a time until it finds a @@ -571,7 +577,7 @@ RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(precision)); } else if (i < float_converter.zero_blocks_after_point()) { // else if there are some blocks that are zeroes - i = float_converter.zero_blocks_after_point(); + i = static_cast(float_converter.zero_blocks_after_point()); // write those blocks as zeroes. RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(9 * i)); } @@ -665,7 +671,7 @@ sign_char = ' '; // If to_conv doesn't specify a precision, the precision defaults to 6. - const size_t precision = to_conv.precision < 0 ? 6 : to_conv.precision; + const unsigned int precision = to_conv.precision < 0 ? 6 : to_conv.precision; bool has_decimal_point = (precision > 0) || ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0); @@ -682,9 +688,9 @@ int cur_block; if (exponent < 0) { - cur_block = -float_converter.zero_blocks_after_point(); + cur_block = -static_cast(float_converter.zero_blocks_after_point()); } else { - cur_block = float_converter.get_positive_blocks(); + cur_block = static_cast(float_converter.get_positive_blocks()); } BlockInt digits = 0; @@ -707,7 +713,7 @@ auto int_to_str = *IntegerToString::dec(digits, buf); size_t block_width = int_to_str.size(); - final_exponent = (cur_block * BLOCK_SIZE) + (block_width - 1); + final_exponent = (cur_block * BLOCK_SIZE) + static_cast(block_width - 1); int positive_exponent = final_exponent < 0 ? -final_exponent : final_exponent; int_to_str = *IntegerToString::dec(positive_exponent, buf); @@ -753,7 +759,7 @@ } // This is the last block. - const uint32_t maximum = precision + 1 - digits_written; + const size_t maximum = precision + 1 - digits_written; uint32_t last_digit = 0; for (uint32_t k = 0; k < last_block_size - maximum; ++k) { last_digit = digits % 10; @@ -823,9 +829,9 @@ // From the standard: Let P (init_precision) equal the precision if nonzero, 6 // if the precision is omitted, or 1 if the precision is zero. - const size_t init_precision = to_conv.precision <= 0 - ? (to_conv.precision == 0 ? 1 : 6) - : to_conv.precision; + const unsigned int init_precision = to_conv.precision <= 0 + ? (to_conv.precision == 0 ? 1 : 6) + : to_conv.precision; // Then, if a conversion with style E would have an exponent of X // (base_10_exp): @@ -835,7 +841,7 @@ // For calculating the base 10 exponent, we need to process the number as if // it has style E, so here we calculate the precision we'll use in that case. - const size_t exp_precision = init_precision - 1; + const unsigned int exp_precision = init_precision - 1; FloatToString float_converter(static_cast(float_bits)); @@ -845,9 +851,9 @@ int cur_block; if (exponent < 0) { - cur_block = -float_converter.zero_blocks_after_point(); + cur_block = -static_cast(float_converter.zero_blocks_after_point()); } else { - cur_block = float_converter.get_positive_blocks(); + cur_block = static_cast(float_converter.get_positive_blocks()); } BlockInt digits = 0; @@ -890,7 +896,7 @@ size_t trailing_zeroes = 0; size_t trailing_nines = 0; - base_10_exp = (cur_block * BLOCK_SIZE) + (block_width - 1); + base_10_exp = (cur_block * BLOCK_SIZE) + static_cast(block_width - 1); // If the first block is not also the last block if (block_width <= exp_precision + 1) { @@ -959,7 +965,7 @@ char buf[IntegerToString::dec_bufsize()]; auto int_to_str = *IntegerToString::dec(digits, buf); - int implicit_leading_zeroes = BLOCK_SIZE - int_to_str.size(); + size_t implicit_leading_zeroes = BLOCK_SIZE - int_to_str.size(); // if the last block is also the first block, then ignore leading zeroes. if (digits_checked == 0) { @@ -967,9 +973,11 @@ implicit_leading_zeroes = 0; } - int digits_requested = (exp_precision + 1) - digits_checked; + unsigned int digits_requested = + (exp_precision + 1) - static_cast(digits_checked); - int digits_to_check = digits_requested - implicit_leading_zeroes; + int digits_to_check = + digits_requested - static_cast(implicit_leading_zeroes); if (digits_to_check < 0) { digits_to_check = 0; } @@ -1003,7 +1011,8 @@ // Find the digit after the lowest digit that we'll actually print to // determine the rounding. - const uint32_t maximum = exp_precision + 1 - digits_checked; + const uint32_t maximum = + exp_precision + 1 - static_cast(digits_checked); uint32_t last_digit = 0; for (uint32_t k = 0; k < last_block_size - maximum; ++k) { last_digit = digits % 10; @@ -1100,7 +1109,7 @@ // P - (X + 1). if (static_cast(init_precision) > base_10_exp && base_10_exp >= -4) { FormatSection new_conv = to_conv; - const size_t conv_precision = init_precision - (base_10_exp + 1); + const int conv_precision = init_precision - (base_10_exp + 1); if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) { new_conv.precision = conv_precision; @@ -1125,36 +1134,35 @@ | | base_10_exp + 1 = 2 --+ +--- trailing_zeroes = 11 */ - int trimmed_precision = - digits_checked - (base_10_exp + 1) - trailing_zeroes; + int trimmed_precision = static_cast( + digits_checked - (base_10_exp + 1) - trailing_zeroes); if (trimmed_precision < 0) { trimmed_precision = 0; } - new_conv.precision = - (static_cast(trimmed_precision) > conv_precision) - ? conv_precision - : trimmed_precision; + new_conv.precision = (trimmed_precision > conv_precision) + ? conv_precision + : trimmed_precision; } return convert_float_decimal_typed(writer, new_conv, float_bits); } else { // otherwise, the conversion is with style e (or E) and precision equals // P - 1 - const size_t conv_precision = init_precision - 1; + const int conv_precision = init_precision - 1; FormatSection new_conv = to_conv; if ((to_conv.flags & FormatFlags::ALTERNATE_FORM) != 0) { new_conv.precision = conv_precision; } else { // If alt form isn't set, then we need to determine the number of trailing // zeroes and set the precision such that they are removed. - int trimmed_precision = digits_checked - 1 - trailing_zeroes; + int trimmed_precision = + static_cast(digits_checked - 1 - trailing_zeroes); if (trimmed_precision < 0) { trimmed_precision = 0; } - new_conv.precision = - (static_cast(trimmed_precision) > conv_precision) - ? conv_precision - : trimmed_precision; + new_conv.precision = (trimmed_precision > conv_precision) + ? conv_precision + : trimmed_precision; } return convert_float_dec_exp_typed(writer, new_conv, float_bits); } diff --git a/libc/src/stdio/printf_core/float_hex_converter.h b/libc/src/stdio/printf_core/float_hex_converter.h --- a/libc/src/stdio/printf_core/float_hex_converter.h +++ b/libc/src/stdio/printf_core/float_hex_converter.h @@ -191,7 +191,7 @@ // these are signed to prevent underflow due to negative values. The eventual // values will always be non-negative. - int trailing_zeroes = 0; + size_t trailing_zeroes = 0; int padding; // prefix is "0x", and always appears. @@ -214,9 +214,10 @@ const char exp_seperator = a + ('p' - 'a'); constexpr int EXP_SEPERATOR_LEN = 1; - padding = to_conv.min_width - (sign_char > 0 ? 1 : 0) - PREFIX_LEN - - mant_digits - (has_hexadecimal_point ? 1 : 0) - EXP_SEPERATOR_LEN - - (EXP_LEN - exp_cur); + padding = static_cast(to_conv.min_width - (sign_char > 0 ? 1 : 0) - + PREFIX_LEN - mant_digits - + static_cast(has_hexadecimal_point) - + EXP_SEPERATOR_LEN - (EXP_LEN - exp_cur)); if (padding < 0) padding = 0; diff --git a/libc/src/stdio/printf_core/int_converter.h b/libc/src/stdio/printf_core/int_converter.h --- a/libc/src/stdio/printf_core/int_converter.h +++ b/libc/src/stdio/printf_core/int_converter.h @@ -108,13 +108,15 @@ // If this conv has flag 0 but not - and no specified precision, it's // padded with 0's instead of spaces identically to if precision = // min_width - (1 if sign_char). For example: ("%+04d", 1) -> "+001" - zeroes = to_conv.min_width - digits_written - prefix_len; + zeroes = + static_cast(to_conv.min_width - digits_written - prefix_len); spaces = 0; } else { // If there are enough digits to pass over the precision, just write the // number, padded by spaces. zeroes = 0; - spaces = to_conv.min_width - digits_written - prefix_len; + spaces = + static_cast(to_conv.min_width - digits_written - prefix_len); } } else { // If precision was specified, possibly write zeroes, and possibly write @@ -127,10 +129,12 @@ // that special case first. if (num == 0 && to_conv.precision == 0) digits_written = 0; - zeroes = to_conv.precision - digits_written; // a negative value means 0 + zeroes = static_cast(to_conv.precision - + digits_written); // a negative value means 0 if (zeroes < 0) zeroes = 0; - spaces = to_conv.min_width - zeroes - digits_written - prefix_len; + spaces = static_cast(to_conv.min_width - zeroes - digits_written - + prefix_len); } if ((to_conv.conv_name == 'o') && diff --git a/libc/src/stdio/printf_core/vfprintf_internal.h b/libc/src/stdio/printf_core/vfprintf_internal.h --- a/libc/src/stdio/printf_core/vfprintf_internal.h +++ b/libc/src/stdio/printf_core/vfprintf_internal.h @@ -36,8 +36,9 @@ LIBC_INLINE int fwrite_unlocked(const void *ptr, size_t size, size_t nmemb, FILE *f) { - return reinterpret_cast<__llvm_libc::File *>(f)->write_unlocked(ptr, - size * nmemb); + return static_cast( + reinterpret_cast<__llvm_libc::File *>(f)->write_unlocked(ptr, + size * nmemb)); } #else // defined(LIBC_COPT_PRINTF_USE_SYSTEM_FILE) LIBC_INLINE int ferror_unlocked(::FILE *f) { return ::ferror_unlocked(f); } diff --git a/libc/src/stdio/printf_core/write_int_converter.h b/libc/src/stdio/printf_core/write_int_converter.h --- a/libc/src/stdio/printf_core/write_int_converter.h +++ b/libc/src/stdio/printf_core/write_int_converter.h @@ -25,6 +25,8 @@ // This is an additional check added by LLVM-libc. The reason it returns -3 is // because printf uses negative return values for errors, and -1 and -2 are // already in use by the file_writer class for file errors. + // TODO: Remove this. It's better to crash than to provide an incorrect + // result. if (to_conv.conv_val_ptr == nullptr) return NULLPTR_WRITE_ERROR; @@ -42,10 +44,12 @@ *reinterpret_cast(to_conv.conv_val_ptr) = written; break; case LengthModifier::h: - *reinterpret_cast(to_conv.conv_val_ptr) = written; + *reinterpret_cast(to_conv.conv_val_ptr) = + static_cast(written); break; case LengthModifier::hh: - *reinterpret_cast(to_conv.conv_val_ptr) = written; + *reinterpret_cast(to_conv.conv_val_ptr) = + static_cast(written); break; case LengthModifier::z: *reinterpret_cast(to_conv.conv_val_ptr) = written;