Skip to content

Commit 7b33c5c

Browse files
[libc] Remove unnecessary check in printf floats (#95841)
Fixes #95638 The check was `if(unsigned_num >= 0)` which will always be true. The intent was to check for zero, but the `for` loop inside the `if` was already doing that.
1 parent 295d574 commit 7b33c5c

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

libc/src/stdio/printf_core/float_dec_converter.h

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -502,25 +502,22 @@ LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
502502

503503
const size_t positive_blocks = float_converter.get_positive_blocks();
504504

505-
if (positive_blocks >= 0) {
506-
// This loop iterates through the number a block at a time until it finds a
507-
// block that is not zero or it hits the decimal point. This is because all
508-
// zero blocks before the first nonzero digit or the decimal point are
509-
// ignored (no leading zeroes, at least at this stage).
510-
int32_t i = static_cast<int32_t>(positive_blocks) - 1;
511-
for (; i >= 0; --i) {
512-
BlockInt digits = float_converter.get_positive_block(i);
513-
if (nonzero) {
514-
RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
515-
} else if (digits != 0) {
516-
size_t blocks_before_decimal = i;
517-
float_writer.init((blocks_before_decimal * BLOCK_SIZE) +
518-
(has_decimal_point ? 1 : 0) + precision,
519-
blocks_before_decimal * BLOCK_SIZE);
520-
float_writer.write_first_block(digits);
521-
522-
nonzero = true;
523-
}
505+
// This loop iterates through the number a block at a time until it finds a
506+
// block that is not zero or it hits the decimal point. This is because all
507+
// zero blocks before the first nonzero digit or the decimal point are
508+
// ignored (no leading zeroes, at least at this stage).
509+
for (int32_t i = static_cast<int32_t>(positive_blocks) - 1; i >= 0; --i) {
510+
BlockInt digits = float_converter.get_positive_block(i);
511+
if (nonzero) {
512+
RET_IF_RESULT_NEGATIVE(float_writer.write_middle_block(digits));
513+
} else if (digits != 0) {
514+
size_t blocks_before_decimal = i;
515+
float_writer.init((blocks_before_decimal * BLOCK_SIZE) +
516+
(has_decimal_point ? 1 : 0) + precision,
517+
blocks_before_decimal * BLOCK_SIZE);
518+
float_writer.write_first_block(digits);
519+
520+
nonzero = true;
524521
}
525522
}
526523

0 commit comments

Comments
 (0)