Skip to content

Commit 6fea340

Browse files
authored
[libc] Fix non-templated uses of printf_core::Writer (llvm#131149)
Commit 598e882 turned `Writer` into a template, and updated most of the call sites that use it. But not all. The alternative FP printf system in `float_dec_converter_limited.h` wasn't updated, and neither was `baremetal/printf.cpp` or `baremetal/vprintf.cpp`. This patch updates `float_dec_converter_limited.h` in the same way that the previous commit updated `float_dec_converter.h`: propagate the templatedness through everything in the header, so that anything using a `Writer` at all has a `write_mode` template parameter to pass on to it. `printf.cpp` and `vprintf.cpp` are updated in the same way that the previous commit updated `printf_core/vfprintf_internal.h`: the `WriteBuffer` has parameter `WriteMode::FLUSH_TO_STREAM`, and `Writer` picks it up implicitly from that.
1 parent 02575f8 commit 6fea340

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

libc/src/stdio/baremetal/printf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ LLVM_LIBC_FUNCTION(int, printf, (const char *__restrict format, ...)) {
3838
constexpr size_t BUFF_SIZE = 1024;
3939
char buffer[BUFF_SIZE];
4040

41-
printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr);
41+
printf_core::WriteBuffer<Mode<WriteMode::FLUSH_TO_STREAM>::value> wb(
42+
buffer, BUFF_SIZE, &raw_write_hook, nullptr);
4243
printf_core::Writer writer(&wb);
4344

4445
int retval = printf_core::printf_main(&writer, format, args);

libc/src/stdio/baremetal/vprintf.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ LLVM_LIBC_FUNCTION(int, vprintf,
3636
constexpr size_t BUFF_SIZE = 1024;
3737
char buffer[BUFF_SIZE];
3838

39-
printf_core::WriteBuffer wb(buffer, BUFF_SIZE, &raw_write_hook, nullptr);
39+
printf_core::WriteBuffer<Mode<WriteMode::FLUSH_TO_STREAM>::value> wb(
40+
buffer, BUFF_SIZE, &raw_write_hook, nullptr);
4041
printf_core::Writer writer(&wb);
4142

4243
int retval = printf_core::printf_main(&writer, format, args);

libc/src/stdio/printf_core/float_dec_converter_limited.h

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,11 @@ DigitsOutput decimal_digits(DigitsInput input, int precision, bool e_mode) {
375375
return output;
376376
}
377377

378-
LIBC_INLINE int convert_float_inner(Writer *writer,
379-
const FormatSection &to_conv,
380-
int32_t fraction_len, int exponent,
381-
StorageType mantissa, Sign sign,
382-
ConversionType ctype) {
378+
template <WriteMode write_mode>
379+
LIBC_INLINE int
380+
convert_float_inner(Writer<write_mode> *writer, const FormatSection &to_conv,
381+
int32_t fraction_len, int exponent, StorageType mantissa,
382+
Sign sign, ConversionType ctype) {
383383
// If to_conv doesn't specify a precision, the precision defaults to 6.
384384
unsigned precision = to_conv.precision < 0 ? 6 : to_conv.precision;
385385

@@ -617,17 +617,19 @@ LIBC_INLINE int convert_float_inner(Writer *writer,
617617
return WRITE_OK;
618618
}
619619

620-
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
620+
template <typename T, WriteMode write_mode,
621+
cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
621622
LIBC_INLINE int
622-
convert_float_typed(Writer *writer, const FormatSection &to_conv,
623+
convert_float_typed(Writer<write_mode> *writer, const FormatSection &to_conv,
623624
fputil::FPBits<T> float_bits, ConversionType ctype) {
624625
return convert_float_inner(writer, to_conv, float_bits.FRACTION_LEN,
625626
float_bits.get_explicit_exponent(),
626627
float_bits.get_explicit_mantissa(),
627628
float_bits.sign(), ctype);
628629
}
629630

630-
LIBC_INLINE int convert_float_outer(Writer *writer,
631+
template <WriteMode write_mode>
632+
LIBC_INLINE int convert_float_outer(Writer<write_mode> *writer,
631633
const FormatSection &to_conv,
632634
ConversionType ctype) {
633635
if (to_conv.length_modifier == LengthModifier::L) {
@@ -649,38 +651,44 @@ LIBC_INLINE int convert_float_outer(Writer *writer,
649651
return convert_inf_nan(writer, to_conv);
650652
}
651653

652-
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
653-
LIBC_INLINE int convert_float_decimal_typed(Writer *writer,
654+
template <typename T, WriteMode write_mode,
655+
cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
656+
LIBC_INLINE int convert_float_decimal_typed(Writer<write_mode> *writer,
654657
const FormatSection &to_conv,
655658
fputil::FPBits<T> float_bits) {
656659
return convert_float_typed<T>(writer, to_conv, float_bits, ConversionType::F);
657660
}
658661

659-
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
660-
LIBC_INLINE int convert_float_dec_exp_typed(Writer *writer,
662+
template <typename T, WriteMode write_mode,
663+
cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
664+
LIBC_INLINE int convert_float_dec_exp_typed(Writer<write_mode> *writer,
661665
const FormatSection &to_conv,
662666
fputil::FPBits<T> float_bits) {
663667
return convert_float_typed<T>(writer, to_conv, float_bits, ConversionType::E);
664668
}
665669

666-
template <typename T, cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
667-
LIBC_INLINE int convert_float_dec_auto_typed(Writer *writer,
670+
template <typename T, WriteMode write_mode,
671+
cpp::enable_if_t<cpp::is_floating_point_v<T>, int> = 0>
672+
LIBC_INLINE int convert_float_dec_auto_typed(Writer<write_mode> *writer,
668673
const FormatSection &to_conv,
669674
fputil::FPBits<T> float_bits) {
670675
return convert_float_typed<T>(writer, to_conv, float_bits, ConversionType::G);
671676
}
672677

673-
LIBC_INLINE int convert_float_decimal(Writer *writer,
678+
template <WriteMode write_mode>
679+
LIBC_INLINE int convert_float_decimal(Writer<write_mode> *writer,
674680
const FormatSection &to_conv) {
675681
return convert_float_outer(writer, to_conv, ConversionType::F);
676682
}
677683

678-
LIBC_INLINE int convert_float_dec_exp(Writer *writer,
684+
template <WriteMode write_mode>
685+
LIBC_INLINE int convert_float_dec_exp(Writer<write_mode> *writer,
679686
const FormatSection &to_conv) {
680687
return convert_float_outer(writer, to_conv, ConversionType::E);
681688
}
682689

683-
LIBC_INLINE int convert_float_dec_auto(Writer *writer,
690+
template <WriteMode write_mode>
691+
LIBC_INLINE int convert_float_dec_auto(Writer<write_mode> *writer,
684692
const FormatSection &to_conv) {
685693
return convert_float_outer(writer, to_conv, ConversionType::G);
686694
}

0 commit comments

Comments
 (0)