Skip to content

Commit ab65c9c

Browse files
[libc][NFC] fix int warnings in float conversion (llvm#74379)
The printf float to string conversion functions had some implicit integer conversion warnings on gcc. This patch adds explicit casts to these places.
1 parent 924f6ca commit ab65c9c

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

libc/src/__support/UInt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ template <size_t Bits, bool Signed> struct BigInt {
121121
return lo;
122122
}
123123
} else {
124+
// TODO: silence shift warning
124125
return static_cast<T>((static_cast<T>(val[1]) << 64) + lo);
125126
}
126127
}

libc/src/__support/float_to_string.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ constexpr size_t MID_INT_SIZE = 192;
103103
namespace LIBC_NAMESPACE {
104104

105105
using BlockInt = uint32_t;
106-
constexpr size_t BLOCK_SIZE = 9;
106+
constexpr uint32_t BLOCK_SIZE = 9;
107107

108108
using MantissaInt = fputil::FPBits<long double>::UIntType;
109109

@@ -136,12 +136,12 @@ LIBC_INLINE constexpr uint32_t log10_pow2(const uint64_t e) {
136136
// us the floor, whereas counting the digits of the power of 2 gives us the
137137
// ceiling. With a similar loop I checked the maximum valid value and found
138138
// 42039.
139-
return (e * 0x13441350fbdll) >> 42;
139+
return static_cast<uint32_t>((e * 0x13441350fbdll) >> 42);
140140
}
141141

142142
// Same as above, but with different constants.
143143
LIBC_INLINE constexpr uint32_t log2_pow5(const uint64_t e) {
144-
return (e * 0x12934f0979bll) >> 39;
144+
return static_cast<uint32_t>((e * 0x12934f0979bll) >> 39);
145145
}
146146

147147
// Returns 1 + floor(log_10(2^e). This could technically be off by 1 if any
@@ -290,7 +290,7 @@ LIBC_INLINE cpp::UInt<MID_INT_SIZE> get_table_negative(int exponent, size_t i) {
290290
} else {
291291
ten_blocks = 0;
292292
five_blocks = i;
293-
shift_amount = static_cast<int>(shift_amount + (i * BLOCK_SIZE));
293+
shift_amount = shift_amount + (static_cast<int>(i) * BLOCK_SIZE);
294294
}
295295
}
296296

@@ -488,7 +488,8 @@ class FloatToString {
488488

489489
val = POW10_SPLIT[POW10_OFFSET[idx] + block_index];
490490
#endif
491-
const uint32_t shift_amount = SHIFT_CONST + (IDX_SIZE * idx) - exponent;
491+
const uint32_t shift_amount =
492+
SHIFT_CONST + (static_cast<uint32_t>(IDX_SIZE) * idx) - exponent;
492493
const uint32_t digits =
493494
internal::mul_shift_mod_1e9(mantissa, val, (int32_t)(shift_amount));
494495
return digits;
@@ -549,7 +550,7 @@ class FloatToString {
549550
val = POW10_SPLIT_2[p];
550551
#endif
551552
const int32_t shift_amount =
552-
SHIFT_CONST + (-exponent - static_cast<int>(IDX_SIZE) * idx);
553+
SHIFT_CONST + (-exponent - (static_cast<int32_t>(IDX_SIZE) * idx));
553554
uint32_t digits =
554555
internal::mul_shift_mod_1e9(mantissa, val, shift_amount);
555556
return digits;
@@ -747,7 +748,8 @@ FloatToString<long double>::get_negative_block(int block_index) {
747748
block_index + 1);
748749
}
749750
#endif
750-
const int32_t shift_amount = SHIFT_CONST + (-exponent - IDX_SIZE * idx);
751+
const int32_t shift_amount =
752+
SHIFT_CONST + (-exponent - static_cast<int>(IDX_SIZE * idx));
751753
BlockInt digits = internal::mul_shift_mod_1e9(mantissa, val, shift_amount);
752754
return digits;
753755
} else {

libc/src/math/generic/math_utils.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,13 @@
1818

1919
#include <stdint.h>
2020

21-
namespace LIBC_NAMESPACE {
22-
23-
LIBC_INLINE uint32_t as_uint32_bits(float x) {
24-
return cpp::bit_cast<uint32_t>(x);
25-
}
26-
27-
LIBC_INLINE uint64_t as_uint64_bits(double x) {
28-
return cpp::bit_cast<uint64_t>(x);
29-
}
21+
// TODO: evaluate which functions from this file are actually used.
3022

31-
LIBC_INLINE float as_float(uint32_t x) { return cpp::bit_cast<float>(x); }
23+
namespace LIBC_NAMESPACE {
3224

25+
// TODO: Remove this, or move it to exp_utils.cpp which is its only user.
3326
LIBC_INLINE double as_double(uint64_t x) { return cpp::bit_cast<double>(x); }
3427

35-
LIBC_INLINE uint32_t top12_bits(float x) { return as_uint32_bits(x) >> 20; }
36-
37-
LIBC_INLINE uint32_t top12_bits(double x) { return as_uint64_bits(x) >> 52; }
38-
3928
// Values to trigger underflow and overflow.
4029
template <typename T> struct XFlowValues;
4130

libc/src/stdio/printf_core/float_dec_converter.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
537537
}
538538

539539
if (exponent < MANT_WIDTH) {
540-
const uint32_t blocks = (precision / BLOCK_SIZE) + 1;
540+
const uint32_t blocks = (precision / static_cast<uint32_t>(BLOCK_SIZE)) + 1;
541541
uint32_t i = 0;
542542
// if all the blocks we should write are zero
543543
if (blocks <= float_converter.zero_blocks_after_point()) {
@@ -561,7 +561,8 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
561561
RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
562562
} else {
563563

564-
const uint32_t maximum = precision - BLOCK_SIZE * i;
564+
const uint32_t maximum =
565+
static_cast<uint32_t>(precision - BLOCK_SIZE * i);
565566
uint32_t last_digit = 0;
566567
for (uint32_t k = 0; k < BLOCK_SIZE - maximum; ++k) {
567568
last_digit = digits % 10;
@@ -646,7 +647,8 @@ LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
646647

647648
const size_t block_width = IntegerToString<intmax_t>(digits).size();
648649

649-
final_exponent = (cur_block * BLOCK_SIZE) + static_cast<int>(block_width - 1);
650+
final_exponent = static_cast<int>(cur_block * BLOCK_SIZE) +
651+
static_cast<int>(block_width - 1);
650652
int positive_exponent = final_exponent < 0 ? -final_exponent : final_exponent;
651653

652654
size_t exponent_width = IntegerToString<intmax_t>(positive_exponent).size();
@@ -819,7 +821,8 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
819821
size_t trailing_zeroes = 0;
820822
size_t trailing_nines = 0;
821823

822-
base_10_exp = (cur_block * BLOCK_SIZE) + static_cast<int>(block_width - 1);
824+
base_10_exp = static_cast<int>(cur_block * BLOCK_SIZE) +
825+
static_cast<int>(block_width - 1);
823826

824827
// If the first block is not also the last block
825828
if (block_width <= exp_precision + 1) {
@@ -858,13 +861,13 @@ LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
858861
trailing_nines = 0;
859862
trailing_zeroes = 0;
860863
BlockInt copy_of_digits = digits;
861-
int cur_last_digit = copy_of_digits % 10;
864+
BlockInt cur_last_digit = copy_of_digits % 10;
862865
// We only care if it ends in nines or zeroes.
863866
while (copy_of_digits > 0 &&
864867
(cur_last_digit == 9 || cur_last_digit == 0)) {
865868
// If the next digit is not the same as the previous one, then there are
866869
// no more contiguous trailing digits.
867-
if ((copy_of_digits % 10) != cur_last_digit) {
870+
if (copy_of_digits % 10 != cur_last_digit) {
868871
break;
869872
}
870873
if (cur_last_digit == 9) {

libc/src/stdio/printf_core/float_hex_converter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
139139
size_t first_non_zero = 1;
140140
for (; mant_cur > 0; --mant_cur, mantissa >>= 4) {
141141
char mant_mod_16 = static_cast<char>(mantissa) & 15;
142-
char new_digit =
143-
(mant_mod_16 > 9) ? (mant_mod_16 - 10 + a) : (mant_mod_16 + '0');
142+
char new_digit = static_cast<char>(
143+
(mant_mod_16 > 9) ? (mant_mod_16 - 10 + a) : (mant_mod_16 + '0'));
144144
mant_buffer[mant_cur - 1] = new_digit;
145145
if (new_digit != '0' && first_non_zero < mant_cur)
146146
first_non_zero = mant_cur;
@@ -169,7 +169,7 @@ LIBC_INLINE int convert_float_hex_exp(Writer *writer,
169169

170170
size_t exp_cur = EXP_LEN;
171171
for (; exponent > 0; --exp_cur, exponent /= 10) {
172-
exp_buffer[exp_cur - 1] = (exponent % 10) + '0';
172+
exp_buffer[exp_cur - 1] = static_cast<char>((exponent % 10) + '0');
173173
}
174174
if (exp_cur == EXP_LEN) { // if nothing else was written, write a 0.
175175
exp_buffer[EXP_LEN - 1] = '0';

0 commit comments

Comments
 (0)