Skip to content

Commit

Permalink
[libc][NFC] clean up type warnings in printf
Browse files Browse the repository at this point in the history
In preparation for https://reviews.llvm.org/D156630 this patch cleans up
all integer size and sign conversion warnings in printf.

Reviewed By: lntue

Differential Revision: https://reviews.llvm.org/D156723
  • Loading branch information
michaelrj-google committed Jul 31, 2023
1 parent e444dce commit e9bdf4a
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 59 deletions.
18 changes: 11 additions & 7 deletions libc/src/__support/float_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,8 @@ LIBC_INLINE constexpr cpp::UInt<MID_INT_SIZE> get_table_positive(int exponent,
// ~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<int>(exponent + CALC_SHIFT_CONST - (BLOCK_SIZE * i));
if (shift_amount < 0) {
return 1;
}
Expand Down Expand Up @@ -221,7 +222,8 @@ LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_positive_df(int exponent,
// 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<int>(exponent + CALC_SHIFT_CONST - (9 * i));
if (shift_amount < 0) {
return 1;
}
Expand Down Expand Up @@ -288,7 +290,7 @@ LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_negative(int exponent, size_t i) {
} else {
ten_blocks = 0;
five_blocks = i;
shift_amount = shift_amount + (i * BLOCK_SIZE);
shift_amount = static_cast<int>(shift_amount + (i * BLOCK_SIZE));
}
}

Expand Down Expand Up @@ -375,8 +377,9 @@ LIBC_INLINE uint32_t fast_uint_mod_1e9(const cpp::UInt<MID_INT_SIZE> &val) {
{0x31680A88F8953031u, 0x89705F4136B4A597u, 0});
const auto middle = (mult_const * val);
const uint64_t result = static_cast<uint64_t>(middle[2]);
const uint32_t shifted = result >> 29;
return static_cast<uint32_t>(val) - (1000000000 * shifted);
const uint64_t shifted = result >> 29;
return static_cast<uint32_t>(static_cast<uint32_t>(val) -
(1000000000 * shifted));
}

LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
Expand All @@ -390,7 +393,8 @@ LIBC_INLINE uint32_t mul_shift_mod_1e9(const MantissaInt mantissa,
wide_mant[1] = static_cast<size_t>(mantissa >> 64);
val = (val * wide_mant) >> shift_amount;

return val.div_uint32_times_pow_2(1000000000, 0).value()[0];
return static_cast<uint32_t>(
val.div_uint32_times_pow_2(1000000000, 0).value());
// return fast_uint_mod_1e9(val);
}

Expand Down Expand Up @@ -596,7 +600,7 @@ class FloatToString {
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
Expand Down
88 changes: 48 additions & 40 deletions libc/src/stdio/printf_core/float_dec_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class PaddingWriter {
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<int>(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));
Expand All @@ -89,7 +90,8 @@ class PaddingWriter {
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<int>(min_width - total_digits - (sign_char > 0 ? 1 : 0));
if (left_justified && padding_amount > 0) {
RET_IF_RESULT_NEGATIVE(writer->write(' ', padding_amount));
}
Expand Down Expand Up @@ -288,7 +290,7 @@ class FloatWriter {

// 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)];
}

Expand All @@ -306,7 +308,8 @@ class FloatWriter {
(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<int>(block_digits) - 1;
count >= 0 && has_carry; --count) {
if (end_buff[count] == '9') {
end_buff[count] = '0';
} else {
Expand All @@ -315,7 +318,8 @@ class FloatWriter {
}
}
// handle the high block that's buffered
for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) {
for (int count = static_cast<int>(buffered_digits) - 1;
count >= 0 && has_carry; --count) {
if (block_buffer[count] == '9') {
block_buffer[count] = '0';
} else {
Expand Down Expand Up @@ -374,7 +378,7 @@ class FloatWriter {

// 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)];
}
}
Expand All @@ -393,7 +397,8 @@ class FloatWriter {
(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<int>(block_digits) - 1;
count >= 0 && has_carry; --count) {
if (end_buff[count] == '9') {
end_buff[count] = '0';
} else {
Expand All @@ -402,7 +407,8 @@ class FloatWriter {
}
}
// handle the high block that's buffered
for (int count = buffered_digits - 1; count >= 0 && has_carry; --count) {
for (int count = static_cast<int>(buffered_digits) - 1;
count >= 0 && has_carry; --count) {
if (block_buffer[count] == '9') {
block_buffer[count] = '0';
} else {
Expand Down Expand Up @@ -519,7 +525,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
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);

Expand All @@ -532,7 +538,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
FloatWriter float_writer(writer, has_decimal_point, padding_writer);
FloatToString<T> float_converter(static_cast<T>(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
Expand Down Expand Up @@ -571,7 +577,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
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<uint32_t>(float_converter.zero_blocks_after_point());
// write those blocks as zeroes.
RET_IF_RESULT_NEGATIVE(float_writer.write_zeroes(9 * i));
}
Expand Down Expand Up @@ -665,7 +671,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
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);

Expand All @@ -682,9 +688,9 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
int cur_block;

if (exponent < 0) {
cur_block = -float_converter.zero_blocks_after_point();
cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
} else {
cur_block = float_converter.get_positive_blocks();
cur_block = static_cast<int>(float_converter.get_positive_blocks());
}

BlockInt digits = 0;
Expand All @@ -707,7 +713,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
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<int>(block_width - 1);
int positive_exponent = final_exponent < 0 ? -final_exponent : final_exponent;

int_to_str = *IntegerToString::dec(positive_exponent, buf);
Expand Down Expand Up @@ -753,7 +759,7 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
}

// 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;
Expand Down Expand Up @@ -823,9 +829,9 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,

// 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):
Expand All @@ -835,7 +841,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,

// 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<T> float_converter(static_cast<T>(float_bits));

Expand All @@ -845,9 +851,9 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
int cur_block;

if (exponent < 0) {
cur_block = -float_converter.zero_blocks_after_point();
cur_block = -static_cast<int>(float_converter.zero_blocks_after_point());
} else {
cur_block = float_converter.get_positive_blocks();
cur_block = static_cast<int>(float_converter.get_positive_blocks());
}

BlockInt digits = 0;
Expand Down Expand Up @@ -890,7 +896,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
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<int>(block_width - 1);

// If the first block is not also the last block
if (block_width <= exp_precision + 1) {
Expand Down Expand Up @@ -959,17 +965,19 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
char buf[IntegerToString::dec_bufsize<intmax_t>()];
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) {
last_block_size = int_to_str.size();
implicit_leading_zeroes = 0;
}

int digits_requested = (exp_precision + 1) - digits_checked;
unsigned int digits_requested =
(exp_precision + 1) - static_cast<unsigned int>(digits_checked);

int digits_to_check = digits_requested - implicit_leading_zeroes;
int digits_to_check =
digits_requested - static_cast<int>(implicit_leading_zeroes);
if (digits_to_check < 0) {
digits_to_check = 0;
}
Expand Down Expand Up @@ -1003,7 +1011,8 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,

// 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<uint32_t>(digits_checked);
uint32_t last_digit = 0;
for (uint32_t k = 0; k < last_block_size - maximum; ++k) {
last_digit = digits % 10;
Expand Down Expand Up @@ -1100,7 +1109,7 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
// P - (X + 1).
if (static_cast<int>(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;
Expand All @@ -1125,36 +1134,35 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
| |
base_10_exp + 1 = 2 --+ +--- trailing_zeroes = 11
*/
int trimmed_precision =
digits_checked - (base_10_exp + 1) - trailing_zeroes;
int trimmed_precision = static_cast<int>(
digits_checked - (base_10_exp + 1) - trailing_zeroes);
if (trimmed_precision < 0) {
trimmed_precision = 0;
}
new_conv.precision =
(static_cast<size_t>(trimmed_precision) > conv_precision)
? conv_precision
: trimmed_precision;
new_conv.precision = (trimmed_precision > conv_precision)
? conv_precision
: trimmed_precision;
}

return convert_float_decimal_typed<T>(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<int>(digits_checked - 1 - trailing_zeroes);
if (trimmed_precision < 0) {
trimmed_precision = 0;
}
new_conv.precision =
(static_cast<size_t>(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<T>(writer, new_conv, float_bits);
}
Expand Down
9 changes: 5 additions & 4 deletions libc/src/stdio/printf_core/float_hex_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,

// 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.
Expand All @@ -214,9 +214,10 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
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<int>(to_conv.min_width - (sign_char > 0 ? 1 : 0) -
PREFIX_LEN - mant_digits -
static_cast<int>(has_hexadecimal_point) -
EXP_SEPERATOR_LEN - (EXP_LEN - exp_cur));
if (padding < 0)
padding = 0;

Expand Down
12 changes: 8 additions & 4 deletions libc/src/stdio/printf_core/int_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,15 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
// 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<int>(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<int>(to_conv.min_width - digits_written - prefix_len);
}
} else {
// If precision was specified, possibly write zeroes, and possibly write
Expand All @@ -127,10 +129,12 @@ LIBC_INLINE int convert_int(Writer *writer, const FormatSection &to_conv) {
// 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<int>(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<int>(to_conv.min_width - zeroes - digits_written -
prefix_len);
}

if ((to_conv.conv_name == 'o') &&
Expand Down
Loading

0 comments on commit e9bdf4a

Please sign in to comment.