[libc] Add option to disable printf bit int (#180832)
Requested as a binary size optimization. Updates the parser, converter utils, config, tests, and docs.
This commit is contained in:
@@ -151,6 +151,10 @@ function(_get_compile_options_from_config output_var)
|
|||||||
list(APPEND config_options "-DLIBC_COPT_PRINTF_DISABLE_WIDE")
|
list(APPEND config_options "-DLIBC_COPT_PRINTF_DISABLE_WIDE")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
|
if(LIBC_COPT_PRINTF_DISABLE_BITINT)
|
||||||
|
list(APPEND config_options "-DLIBC_COPT_PRINTF_DISABLE_BITINT")
|
||||||
|
endif()
|
||||||
|
|
||||||
set(${output_var} ${config_options} PARENT_SCOPE)
|
set(${output_var} ${config_options} PARENT_SCOPE)
|
||||||
endfunction(_get_compile_options_from_config)
|
endfunction(_get_compile_options_from_config)
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,10 @@
|
|||||||
"LIBC_CONF_PRINTF_DISABLE_STRERROR": {
|
"LIBC_CONF_PRINTF_DISABLE_STRERROR": {
|
||||||
"value": true
|
"value": true
|
||||||
},
|
},
|
||||||
"LIBC_CONF_PRINTF_DISABLE_WIDE" : {
|
"LIBC_CONF_PRINTF_DISABLE_WIDE": {
|
||||||
|
"value": true
|
||||||
|
},
|
||||||
|
"LIBC_COPT_PRINTF_DISABLE_BITINT": {
|
||||||
"value": true
|
"value": true
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -40,7 +40,6 @@
|
|||||||
"value": false,
|
"value": false,
|
||||||
"doc": "Use an alternative printf float implementation based on 320-bit floats"
|
"doc": "Use an alternative printf float implementation based on 320-bit floats"
|
||||||
},
|
},
|
||||||
|
|
||||||
"LIBC_CONF_PRINTF_DISABLE_FIXED_POINT": {
|
"LIBC_CONF_PRINTF_DISABLE_FIXED_POINT": {
|
||||||
"value": false,
|
"value": false,
|
||||||
"doc": "Disable printing fixed point values in printf and friends."
|
"doc": "Disable printing fixed point values in printf and friends."
|
||||||
@@ -56,6 +55,10 @@
|
|||||||
"LIBC_CONF_PRINTF_DISABLE_WIDE": {
|
"LIBC_CONF_PRINTF_DISABLE_WIDE": {
|
||||||
"value": false,
|
"value": false,
|
||||||
"doc": "Disable handling wide characters for printf and friends."
|
"doc": "Disable handling wide characters for printf and friends."
|
||||||
|
},
|
||||||
|
"LIBC_COPT_PRINTF_DISABLE_BITINT": {
|
||||||
|
"value": false,
|
||||||
|
"doc": "Disable bitint length modifiers to reduce code size. Specifically the wNUM and wfNUM modifiers."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scanf": {
|
"scanf": {
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ to learn about the defaults for your platform and target.
|
|||||||
- ``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_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_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.
|
- ``LIBC_CONF_PRINTF_RUNTIME_DISPATCH``: Use dynamic dispatch for the output mechanism to reduce code size.
|
||||||
|
- ``LIBC_COPT_PRINTF_DISABLE_BITINT``: Disable bitint length modifiers to reduce code size. Specifically the wNUM and wfNUM modifiers.
|
||||||
* **"pthread" options**
|
* **"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_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).
|
- ``LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT``: Default number of spins before blocking if a rwlock is in contention (default to 100).
|
||||||
|
|||||||
@@ -72,12 +72,18 @@ invalid. This reduces code size. This has no effect if the current compiler does
|
|||||||
not support fixed point numbers.
|
not support fixed point numbers.
|
||||||
|
|
||||||
LIBC_COPT_PRINTF_DISABLE_WIDE
|
LIBC_COPT_PRINTF_DISABLE_WIDE
|
||||||
--------------------------------
|
-----------------------------
|
||||||
When set, this flag disables support for wide characters (%lc and %ls). Any
|
When set, this flag disables support for wide characters (%lc and %ls). Any
|
||||||
conversions will be ignored. This reduces code size. This will be set by default
|
conversions will be ignored. This reduces code size. This will be set by default
|
||||||
on windows platforms as current printf implementation does not support UTF-16 wide
|
on windows platforms as current printf implementation does not support UTF-16 wide
|
||||||
characters.
|
characters.
|
||||||
|
|
||||||
|
LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
-------------------------------
|
||||||
|
When set, this flag disables the bit int length modifiers wNUM and wfNUM. The
|
||||||
|
length modifiers will be treated as if they don't exist, so conversions using
|
||||||
|
them will be treated as invalid. This reduces code size.
|
||||||
|
|
||||||
.. _printf_no_nullptr_checks:
|
.. _printf_no_nullptr_checks:
|
||||||
|
|
||||||
LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
|
LIBC_COPT_PRINTF_NO_NULLPTR_CHECKS
|
||||||
|
|||||||
@@ -43,6 +43,7 @@ LIBC_INLINE uintmax_t apply_length_modifier(uintmax_t num,
|
|||||||
return num & cpp::numeric_limits<uintptr_t>::max();
|
return num & cpp::numeric_limits<uintptr_t>::max();
|
||||||
case LengthModifier::j:
|
case LengthModifier::j:
|
||||||
return num; // j is intmax, so no mask is necessary.
|
return num; // j is intmax, so no mask is necessary.
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case LengthModifier::w:
|
case LengthModifier::w:
|
||||||
case LengthModifier::wf: {
|
case LengthModifier::wf: {
|
||||||
uintmax_t mask;
|
uintmax_t mask;
|
||||||
@@ -55,6 +56,7 @@ LIBC_INLINE uintmax_t apply_length_modifier(uintmax_t num,
|
|||||||
}
|
}
|
||||||
return num & mask;
|
return num & mask;
|
||||||
}
|
}
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
}
|
}
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,21 @@ namespace printf_core {
|
|||||||
|
|
||||||
// These length modifiers match the length modifiers in the format string, which
|
// These length modifiers match the length modifiers in the format string, which
|
||||||
// is why they are formatted differently from the rest of the file.
|
// is why they are formatted differently from the rest of the file.
|
||||||
enum class LengthModifier { hh, h, l, ll, j, z, t, L, w, wf, none };
|
enum class LengthModifier {
|
||||||
|
hh,
|
||||||
|
h,
|
||||||
|
l,
|
||||||
|
ll,
|
||||||
|
j,
|
||||||
|
z,
|
||||||
|
t,
|
||||||
|
L,
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
w,
|
||||||
|
wf,
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
none
|
||||||
|
};
|
||||||
|
|
||||||
struct LengthSpec {
|
struct LengthSpec {
|
||||||
LengthModifier lm;
|
LengthModifier lm;
|
||||||
|
|||||||
@@ -224,6 +224,7 @@ public:
|
|||||||
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, ptrdiff_t, conv_index);
|
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, ptrdiff_t, conv_index);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case (LengthModifier::w):
|
case (LengthModifier::w):
|
||||||
case (LengthModifier::wf):
|
case (LengthModifier::wf):
|
||||||
if (bw == 0) {
|
if (bw == 0) {
|
||||||
@@ -238,6 +239,7 @@ public:
|
|||||||
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, intmax_t, conv_index);
|
WRITE_ARG_VAL_SIMPLEST(section.conv_val_raw, intmax_t, conv_index);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
|
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
|
||||||
@@ -360,6 +362,7 @@ private:
|
|||||||
++*local_pos;
|
++*local_pos;
|
||||||
return {LengthModifier::l, 0};
|
return {LengthModifier::l, 0};
|
||||||
}
|
}
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case ('w'): {
|
case ('w'): {
|
||||||
LengthModifier lm;
|
LengthModifier lm;
|
||||||
if (str[*local_pos + 1] == 'f') {
|
if (str[*local_pos + 1] == 'f') {
|
||||||
@@ -376,6 +379,7 @@ private:
|
|||||||
}
|
}
|
||||||
return {lm, 0};
|
return {lm, 0};
|
||||||
}
|
}
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case ('h'):
|
case ('h'):
|
||||||
if (str[*local_pos + 1] == 'h') {
|
if (str[*local_pos + 1] == 'h') {
|
||||||
*local_pos += 2;
|
*local_pos += 2;
|
||||||
@@ -629,6 +633,7 @@ private:
|
|||||||
case (LengthModifier::t):
|
case (LengthModifier::t):
|
||||||
conv_size = type_desc_from_type<ptrdiff_t>();
|
conv_size = type_desc_from_type<ptrdiff_t>();
|
||||||
break;
|
break;
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case (LengthModifier::w):
|
case (LengthModifier::w):
|
||||||
case (LengthModifier::wf):
|
case (LengthModifier::wf):
|
||||||
if (bw <= cpp::numeric_limits<unsigned int>::digits) {
|
if (bw <= cpp::numeric_limits<unsigned int>::digits) {
|
||||||
@@ -641,6 +646,7 @@ private:
|
|||||||
conv_size = type_desc_from_type<intmax_t>();
|
conv_size = type_desc_from_type<intmax_t>();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
|
#ifndef LIBC_COPT_PRINTF_DISABLE_FLOAT
|
||||||
|
|||||||
@@ -57,8 +57,10 @@ LIBC_INLINE int convert_write_int(Writer<write_mode> *writer,
|
|||||||
*reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written;
|
*reinterpret_cast<ptrdiff_t *>(to_conv.conv_val_ptr) = written;
|
||||||
break;
|
break;
|
||||||
case LengthModifier::j:
|
case LengthModifier::j:
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
case LengthModifier::w:
|
case LengthModifier::w:
|
||||||
case LengthModifier::wf:
|
case LengthModifier::wf:
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
*reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written;
|
*reinterpret_cast<uintmax_t *>(to_conv.conv_val_ptr) = written;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,8 +71,10 @@ static void display(FormatSection form) {
|
|||||||
CASE_LM(z);
|
CASE_LM(z);
|
||||||
CASE_LM(t);
|
CASE_LM(t);
|
||||||
CASE_LM(L);
|
CASE_LM(L);
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
CASE_LM_BIT_WIDTH(w, form.bit_width);
|
CASE_LM_BIT_WIDTH(w, form.bit_width);
|
||||||
CASE_LM_BIT_WIDTH(wf, form.bit_width);
|
CASE_LM_BIT_WIDTH(wf, form.bit_width);
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
}
|
}
|
||||||
tlog << "\n";
|
tlog << "\n";
|
||||||
tlog << "\tconversion name: " << form.conv_name << "\n";
|
tlog << "\tconversion name: " << form.conv_name << "\n";
|
||||||
|
|||||||
@@ -230,6 +230,7 @@ TEST(LlvmLibcPrintfParserTest, EvalOneArgWithLongLengthModifier) {
|
|||||||
ASSERT_PFORMAT_EQ(expected, format_arr[0]);
|
ASSERT_PFORMAT_EQ(expected, format_arr[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
TEST(LlvmLibcPrintfParserTest, EvalOneArgWithBitWidthLengthModifier) {
|
TEST(LlvmLibcPrintfParserTest, EvalOneArgWithBitWidthLengthModifier) {
|
||||||
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
|
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
|
||||||
const char *str = "%w32d";
|
const char *str = "%w32d";
|
||||||
@@ -267,6 +268,7 @@ TEST(LlvmLibcPrintfParserTest, EvalOneArgWithFastBitWidthLengthModifier) {
|
|||||||
|
|
||||||
ASSERT_PFORMAT_EQ(expected, format_arr[0]);
|
ASSERT_PFORMAT_EQ(expected, format_arr[0]);
|
||||||
}
|
}
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
|
||||||
TEST(LlvmLibcPrintfParserTest, EvalOneArgWithAllOptions) {
|
TEST(LlvmLibcPrintfParserTest, EvalOneArgWithAllOptions) {
|
||||||
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
|
LIBC_NAMESPACE::printf_core::FormatSection format_arr[10];
|
||||||
|
|||||||
@@ -151,6 +151,8 @@ TEST(LlvmLibcSPrintfTest, IntConv) {
|
|||||||
written = LIBC_NAMESPACE::sprintf(buff, "%lld", -9223372036854775807ll - 1ll);
|
written = LIBC_NAMESPACE::sprintf(buff, "%lld", -9223372036854775807ll - 1ll);
|
||||||
ASSERT_STREQ_LEN(written, buff, "-9223372036854775808"); // ll min
|
ASSERT_STREQ_LEN(written, buff, "-9223372036854775808"); // ll min
|
||||||
|
|
||||||
|
#ifndef LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
// Bit int width tests
|
||||||
written = LIBC_NAMESPACE::sprintf(buff, "%w3d", 5807);
|
written = LIBC_NAMESPACE::sprintf(buff, "%w3d", 5807);
|
||||||
ASSERT_STREQ_LEN(written, buff, "7");
|
ASSERT_STREQ_LEN(written, buff, "7");
|
||||||
|
|
||||||
@@ -218,6 +220,7 @@ TEST(LlvmLibcSPrintfTest, IntConv) {
|
|||||||
|
|
||||||
written = LIBC_NAMESPACE::sprintf(buff, "%wf999d", 9223372036854775807ll);
|
written = LIBC_NAMESPACE::sprintf(buff, "%wf999d", 9223372036854775807ll);
|
||||||
ASSERT_STREQ_LEN(written, buff, "9223372036854775807");
|
ASSERT_STREQ_LEN(written, buff, "9223372036854775807");
|
||||||
|
#endif // LIBC_COPT_PRINTF_DISABLE_BITINT
|
||||||
|
|
||||||
// Min Width Tests.
|
// Min Width Tests.
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user