Skip to content

Commit

Permalink
[libc] add printf auto float conversion
Browse files Browse the repository at this point in the history
This patch adds the final conversion to printf, %g. This is a floating
point conversion that selects automatically between the %e and %f
formats based on the precision requested and resulting exponent.
Additionally it trims trailing zeroes. With this done all that's left
for finishing printf is adding long double support to the decimal float
conversions.

Reviewed By: sivachandra

Differential Revision: https://reviews.llvm.org/D143006
  • Loading branch information
michaelrj-google committed Feb 3, 2023
1 parent 223e99f commit 173d502
Show file tree
Hide file tree
Showing 5 changed files with 1,018 additions and 22 deletions.
2 changes: 2 additions & 0 deletions libc/src/__support/float_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ LIBC_INLINE constexpr uint32_t length_for_num(const uint32_t idx,
// Rewritten slightly we get:
// floor(5^(-9i) * 2^(e + c_1 - 9i) + 1) % (10^9 * 2^c_1)

// TODO: Fix long doubles (needs bigger table or alternate algorithm.)
// Currently the table values are generated, which is very slow.
template <size_t INT_SIZE>
LIBC_INLINE constexpr cpp::UInt<MID_INT_SIZE>
get_table_positive(int exponent, size_t i, const size_t constant) {
Expand Down
6 changes: 3 additions & 3 deletions libc/src/stdio/printf_core/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ int convert(Writer *writer, const FormatSection &to_conv) {
case 'a':
case 'A':
return convert_float_hex_exp(writer, to_conv);
// case 'g':
// case 'G':
// return convert_float_mixed(writer, to_conv);
case 'g':
case 'G':
return convert_float_dec_auto(writer, to_conv);
#endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT
#ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
case 'n':
Expand Down
4 changes: 2 additions & 2 deletions libc/src/stdio/printf_core/converter_atlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@

#ifndef LLVM_LIBC_PRINTF_DISABLE_FLOAT
// defines convert_float_decimal
#include "src/stdio/printf_core/float_dec_converter.h"
// defines convert_float_dec_exp
// defines convert_float_dec_auto
#include "src/stdio/printf_core/float_dec_converter.h"
// defines convert_float_hex_exp
#include "src/stdio/printf_core/float_hex_converter.h"
// defines convert_float_mixed
#endif // LLVM_LIBC_PRINTF_DISABLE_FLOAT

#ifndef LLVM_LIBC_PRINTF_DISABLE_WRITE_INT
Expand Down
Loading

0 comments on commit 173d502

Please sign in to comment.