Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libc/config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
"LIBC_CONF_PRINTF_DISABLE_STRERROR": {
"value": false,
"doc": "Disable handling of %m to print strerror in printf and friends."
},
"LIBC_CONF_PRINTF_RUNTIME_DISPATCH": {
"value": true,
"doc": "Use dynamic dispatch for the output mechanism to reduce code size."
}
},
"scanf": {
Expand Down
3 changes: 3 additions & 0 deletions libc/config/gpu/amdgpu/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"LIBC_CONF_PRINTF_DISABLE_STRERROR": {
"value": true
},
"LIBC_CONF_PRINTF_RUNTIME_DISPATCH": {
"value": false
}
},
"scanf": {
Expand Down
3 changes: 3 additions & 0 deletions libc/config/gpu/nvptx/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
},
"LIBC_CONF_PRINTF_DISABLE_STRERROR": {
"value": true
},
"LIBC_CONF_PRINTF_RUNTIME_DISPATCH": {
"value": false
}
},
"scanf": {
Expand Down
1 change: 1 addition & 0 deletions libc/docs/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ to learn about the defaults for your platform and target.
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_DYADIC_FLOAT``: Use dyadic float for faster and smaller but less accurate printf doubles.
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_FLOAT320``: Use an alternative printf float implementation based on 320-bit floats
- ``LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE``: Use large table for better printf long double performance.
- ``LIBC_CONF_PRINTF_RUNTIME_DISPATCH``: Use dynamic dispatch for the output mechanism to reduce code size.
* **"pthread" options**
- ``LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a mutex is in contention (default to 100).
- ``LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a rwlock is in contention (default to 100).
Expand Down
15 changes: 6 additions & 9 deletions libc/src/stdio/printf_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ endif()
if(LIBC_CONF_PRINTF_DISABLE_STRERROR)
list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_STRERROR")
endif()
if(LIBC_CONF_PRINTF_RUNTIME_DISPATCH)
list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_RUNTIME_DISPATCH")
endif()
if(printf_config_copts)
list(PREPEND printf_config_copts "COMPILE_OPTIONS")
endif()
Expand Down Expand Up @@ -62,10 +65,8 @@ add_header_library(
libc.src.__support.common
)

add_object_library(
add_header_library(
writer
SRCS
writer.cpp
HDRS
writer.h
DEPENDS
Expand All @@ -76,10 +77,8 @@ add_object_library(
libc.src.string.memory_utils.inline_memset
)

add_object_library(
add_header_library(
converter
SRCS
converter.cpp
HDRS
converter.h
converter_atlas.h
Expand Down Expand Up @@ -113,10 +112,8 @@ add_object_library(
libc.src.__support.StringUtil.error_to_string
)

add_object_library(
add_header_library(
printf_main
SRCS
printf_main.cpp
HDRS
printf_main.h
DEPENDS
Expand Down
4 changes: 3 additions & 1 deletion libc/src/stdio/printf_core/char_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
namespace LIBC_NAMESPACE_DECL {
namespace printf_core {

LIBC_INLINE int convert_char(Writer *writer, const FormatSection &to_conv) {
template <WriteMode write_mode>
LIBC_INLINE int convert_char(Writer<write_mode> *writer,
const FormatSection &to_conv) {
char c = static_cast<char>(to_conv.conv_val_raw);

constexpr int STRING_LEN = 1;
Expand Down
105 changes: 0 additions & 105 deletions libc/src/stdio/printf_core/converter.cpp

This file was deleted.

85 changes: 84 additions & 1 deletion libc/src/stdio/printf_core/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@

#include "src/__support/macros/config.h"
#include "src/stdio/printf_core/core_structs.h"
#include "src/stdio/printf_core/printf_config.h"
#include "src/stdio/printf_core/strerror_converter.h"
#include "src/stdio/printf_core/writer.h"

// This option allows for replacing all of the conversion functions with custom
// replacements. This allows conversions to be replaced at compile time.
#ifndef LIBC_COPT_PRINTF_CONV_ATLAS
#include "src/stdio/printf_core/converter_atlas.h"
#else
#include LIBC_COPT_PRINTF_CONV_ATLAS
#endif

#include <stddef.h>

namespace LIBC_NAMESPACE_DECL {
Expand All @@ -21,7 +31,80 @@ namespace printf_core {
// convert will call a conversion function to convert the FormatSection into
// its string representation, and then that will write the result to the
// writer.
int convert(Writer *writer, const FormatSection &to_conv);
template <WriteMode write_mode>
int convert(Writer<write_mode> *writer, const FormatSection &to_conv) {
if (!to_conv.has_conv)
return writer->write(to_conv.raw_string);

#if !defined(LIBC_COPT_PRINTF_DISABLE_FLOAT) && \
defined(LIBC_COPT_PRINTF_HEX_LONG_DOUBLE)
if (to_conv.length_modifier == LengthModifier::L) {
switch (to_conv.conv_name) {
case 'f':
case 'F':
case 'e':
case 'E':
case 'g':
case 'G':
return convert_float_hex_exp(writer, to_conv);
default:
break;
}
}
#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT

switch (to_conv.conv_name) {
case '%':
return writer->write("%");
case 'c':
return convert_char(writer, to_conv);
case 's':
return convert_string(writer, to_conv);
case 'd':
case 'i':
case 'u':
case 'o':
case 'x':
case 'X':
case 'b':
case 'B':
return convert_int(writer, to_conv);
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
case 'f':
case 'F':
return convert_float_decimal(writer, to_conv);
case 'e':
case 'E':
return convert_float_dec_exp(writer, to_conv);
case 'a':
case 'A':
return convert_float_hex_exp(writer, to_conv);
case 'g':
case 'G':
return convert_float_dec_auto(writer, to_conv);
#endif // LIBC_COPT_PRINTF_DISABLE_FLOAT
#ifdef LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT
case 'r':
case 'R':
case 'k':
case 'K':
return convert_fixed(writer, to_conv);
#endif // LIBC_INTERNAL_PRINTF_HAS_FIXED_POINT
#ifndef LIBC_COPT_PRINTF_DISABLE_STRERROR
case 'm':
return convert_strerror(writer, to_conv);
#endif // LIBC_COPT_PRINTF_DISABLE_STRERROR
#ifndef LIBC_COPT_PRINTF_DISABLE_WRITE_INT
case 'n':
return convert_write_int(writer, to_conv);
#endif // LIBC_COPT_PRINTF_DISABLE_WRITE_INT
case 'p':
return convert_pointer(writer, to_conv);
default:
return writer->write(to_conv.raw_string);
}
return -1;
}

} // namespace printf_core
} // namespace LIBC_NAMESPACE_DECL
Expand Down
4 changes: 3 additions & 1 deletion libc/src/stdio/printf_core/fixed_converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ LIBC_INLINE constexpr uint32_t const_ten_exp(uint32_t exponent) {
} \
} while (false)

LIBC_INLINE int convert_fixed(Writer *writer, const FormatSection &to_conv) {
template <WriteMode write_mode>
LIBC_INLINE int convert_fixed(Writer<write_mode> *writer,
const FormatSection &to_conv) {
// Long accum should be the largest type, so we can store all the smaller
// numbers in things sized for it.
using LARep = fixed_point::FXRep<unsigned long accum>;
Expand Down
Loading