diff --git a/libcxx/include/__charconv/chars_format.h b/libcxx/include/__charconv/chars_format.h --- a/libcxx/include/__charconv/chars_format.h +++ b/libcxx/include/__charconv/chars_format.h @@ -21,35 +21,22 @@ #if _LIBCPP_STD_VER >= 17 -enum class _LIBCPP_ENUM_VIS chars_format -{ - scientific = 0x1, - fixed = 0x2, - hex = 0x4, - general = fixed | scientific -}; - -inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format -operator~(chars_format __x) { +enum class _LIBCPP_ENUM_VIS chars_format { scientific = 0x1, fixed = 0x2, hex = 0x4, general = fixed | scientific }; + +inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator~(chars_format __x) { return chars_format(~std::__to_underlying(__x)); } -inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format -operator&(chars_format __x, chars_format __y) { - return chars_format(std::__to_underlying(__x) & - std::__to_underlying(__y)); +inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator&(chars_format __x, chars_format __y) { + return chars_format(std::__to_underlying(__x) & std::__to_underlying(__y)); } -inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format -operator|(chars_format __x, chars_format __y) { - return chars_format(std::__to_underlying(__x) | - std::__to_underlying(__y)); +inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator|(chars_format __x, chars_format __y) { + return chars_format(std::__to_underlying(__x) | std::__to_underlying(__y)); } -inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format -operator^(chars_format __x, chars_format __y) { - return chars_format(std::__to_underlying(__x) ^ - std::__to_underlying(__y)); +inline _LIBCPP_HIDE_FROM_ABI constexpr chars_format operator^(chars_format __x, chars_format __y) { + return chars_format(std::__to_underlying(__x) ^ std::__to_underlying(__y)); } inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 chars_format& diff --git a/libcxx/include/__charconv/from_chars_integral.h b/libcxx/include/__charconv/from_chars_integral.h --- a/libcxx/include/__charconv/from_chars_integral.h +++ b/libcxx/include/__charconv/from_chars_integral.h @@ -38,150 +38,125 @@ template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) -{ - using __tl = numeric_limits<_Tp>; - decltype(std::__to_unsigned_like(__value)) __x; - - bool __neg = (__first != __last && *__first == '-'); - auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); - switch (__r.ec) - { - case errc::invalid_argument: - return {__first, __r.ec}; - case errc::result_out_of_range: - return __r; - default: - break; - } - - if (__neg) - { - if (__x <= std::__complement(std::__to_unsigned_like(__tl::min()))) - { - __x = std::__complement(__x); - std::copy_n(std::addressof(__x), 1, std::addressof(__value)); - return __r; - } +__sign_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) { + using __tl = numeric_limits<_Tp>; + decltype(std::__to_unsigned_like(__value)) __x; + + bool __neg = (__first != __last && *__first == '-'); + auto __r = __f(__neg ? __first + 1 : __first, __last, __x, __args...); + switch (__r.ec) { + case errc::invalid_argument: + return {__first, __r.ec}; + case errc::result_out_of_range: + return __r; + default: + break; + } + + if (__neg) { + if (__x <= std::__complement(std::__to_unsigned_like(__tl::min()))) { + __x = std::__complement(__x); + std::copy_n(std::addressof(__x), 1, std::addressof(__value)); + return __r; } - else - { - if (__x <= std::__to_unsigned_like(__tl::max())) - { - __value = __x; - return __r; - } + } else { + if (__x <= std::__to_unsigned_like(__tl::max())) { + __value = __x; + return __r; } + } - return {__r.ptr, errc::result_out_of_range}; + return {__r.ptr, errc::result_out_of_range}; } template -inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool -__in_pattern(_Tp __c) -{ - return '0' <= __c && __c <= '9'; +inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __in_pattern(_Tp __c) { + return '0' <= __c && __c <= '9'; } -struct _LIBCPP_HIDDEN __in_pattern_result -{ - bool __ok; - int __val; +struct _LIBCPP_HIDDEN __in_pattern_result { + bool __ok; + int __val; - explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; } + explicit _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; } }; template -inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result -__in_pattern(_Tp __c, int __base) -{ - if (__base <= 10) - return {'0' <= __c && __c < '0' + __base, __c - '0'}; - else if (std::__in_pattern(__c)) - return {true, __c - '0'}; - else if ('a' <= __c && __c < 'a' + __base - 10) - return {true, __c - 'a' + 10}; - else - return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; +inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI __in_pattern_result __in_pattern(_Tp __c, int __base) { + if (__base <= 10) + return {'0' <= __c && __c < '0' + __base, __c - '0'}; + else if (std::__in_pattern(__c)) + return {true, __c - '0'}; + else if ('a' <= __c && __c < 'a' + __base - 10) + return {true, __c - 'a' + 10}; + else + return {'A' <= __c && __c < 'A' + __base - 10, __c - 'A' + 10}; } template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, - _Ts... __args) -{ - auto __find_non_zero = [](_It __firstit, _It __lastit) { - for (; __firstit != __lastit; ++__firstit) - if (*__firstit != '0') - break; - return __firstit; - }; - - auto __p = __find_non_zero(__first, __last); - if (__p == __last || !std::__in_pattern(*__p, __args...)) - { - if (__p == __first) - return {__first, errc::invalid_argument}; - else - { - __value = 0; - return {__p, {}}; - } +__subject_seq_combinator(_It __first, _It __last, _Tp& __value, _Fn __f, _Ts... __args) { + auto __find_non_zero = [](_It __firstit, _It __lastit) { + for (; __firstit != __lastit; ++__firstit) + if (*__firstit != '0') + break; + return __firstit; + }; + + auto __p = __find_non_zero(__first, __last); + if (__p == __last || !std::__in_pattern(*__p, __args...)) { + if (__p == __first) + return {__first, errc::invalid_argument}; + else { + __value = 0; + return {__p, {}}; } + } - auto __r = __f(__p, __last, __value, __args...); - if (__r.ec == errc::result_out_of_range) - { - for (; __r.ptr != __last; ++__r.ptr) - { - if (!std::__in_pattern(*__r.ptr, __args...)) - break; - } + auto __r = __f(__p, __last, __value, __args...); + if (__r.ec == errc::result_out_of_range) { + for (; __r.ptr != __last; ++__r.ptr) { + if (!std::__in_pattern(*__r.ptr, __args...)) + break; } + } - return __r; + return __r; } template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) -{ - using __tx = __itoa::__traits<_Tp>; - using __output_type = typename __tx::type; - - return std::__subject_seq_combinator( - __first, __last, __value, - [](const char* __f, const char* __l, - _Tp& __val) -> from_chars_result { - __output_type __a, __b; - auto __p = __tx::__read(__f, __l, __a, __b); - if (__p == __l || !std::__in_pattern(*__p)) - { - __output_type __m = numeric_limits<_Tp>::max(); - if (__m >= __a && __m - __a >= __b) - { - __val = __a + __b; - return {__p, {}}; - } - } - return {__p, errc::result_out_of_range}; - }); +__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) { + using __tx = __itoa::__traits<_Tp>; + using __output_type = typename __tx::type; + + return std::__subject_seq_combinator( + __first, __last, __value, [](const char* __f, const char* __l, _Tp& __val) -> from_chars_result { + __output_type __a, __b; + auto __p = __tx::__read(__f, __l, __a, __b); + if (__p == __l || !std::__in_pattern(*__p)) { + __output_type __m = numeric_limits<_Tp>::max(); + if (__m >= __a && __m - __a >= __b) { + __val = __a + __b; + return {__p, {}}; + } + } + return {__p, errc::result_out_of_range}; + }); } template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) -{ - using __t = decltype(std::__to_unsigned_like(__value)); - return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); +__from_chars_atoi(const char* __first, const char* __last, _Tp& __value) { + using __t = decltype(std::__to_unsigned_like(__value)); + return std::__sign_combinator(__first, __last, __value, __from_chars_atoi<__t>); } - /* // Code used to generate __from_chars_log2f_lut. #include -#include #include +#include int main() { for (int i = 2; i <= 36; ++i) @@ -197,75 +172,63 @@ template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__from_chars_integral(const char* __first, const char* __last, _Tp& __value, - int __base) -{ - if (__base == 10) - return std::__from_chars_atoi(__first, __last, __value); - - return std::__subject_seq_combinator( - __first, __last, __value, - [](const char* __p, const char* __lastp, _Tp& __val, - int __b) -> from_chars_result { - using __tl = numeric_limits<_Tp>; - // __base is always between 2 and 36 inclusive. - auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2]; - _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0; - - for (int __i = 1; __p != __lastp; ++__i, ++__p) - { - if (auto __c = __in_pattern(*__p, __b)) - { - if (__i < __digits - 1) - __x = __x * __b + __c.__val; - else - { - if (!__itoa::__mul_overflowed(__x, __b, __x)) - ++__p; - __y = __c.__val; - break; - } - } - else - break; - } +__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) { + if (__base == 10) + return std::__from_chars_atoi(__first, __last, __value); - if (__p == __lastp || !__in_pattern(*__p, __b)) - { - if (__tl::max() - __x >= __y) - { - __val = __x + __y; - return {__p, {}}; - } + return std::__subject_seq_combinator( + __first, + __last, + __value, + [](const char* __p, const char* __lastp, _Tp& __val, int __b) -> from_chars_result { + using __tl = numeric_limits<_Tp>; + // __base is always between 2 and 36 inclusive. + auto __digits = __tl::digits / __from_chars_log2f_lut[__b - 2]; + _Tp __x = __in_pattern(*__p++, __b).__val, __y = 0; + + for (int __i = 1; __p != __lastp; ++__i, ++__p) { + if (auto __c = __in_pattern(*__p, __b)) { + if (__i < __digits - 1) + __x = __x * __b + __c.__val; + else { + if (!__itoa::__mul_overflowed(__x, __b, __x)) + ++__p; + __y = __c.__val; + break; } - return {__p, errc::result_out_of_range}; - }, - __base); + } else + break; + } + + if (__p == __lastp || !__in_pattern(*__p, __b)) { + if (__tl::max() - __x >= __y) { + __val = __x + __y; + return {__p, {}}; + } + } + return {__p, errc::result_out_of_range}; + }, + __base); } template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -__from_chars_integral(const char* __first, const char* __last, _Tp& __value, - int __base) -{ - using __t = decltype(std::__to_unsigned_like(__value)); - return std::__sign_combinator(__first, __last, __value, - __from_chars_integral<__t>, __base); +__from_chars_integral(const char* __first, const char* __last, _Tp& __value, int __base) { + using __t = decltype(std::__to_unsigned_like(__value)); + return std::__sign_combinator(__first, __last, __value, __from_chars_integral<__t>, __base); } template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -from_chars(const char* __first, const char* __last, _Tp& __value) -{ - return std::__from_chars_atoi(__first, __last, __value); +from_chars(const char* __first, const char* __last, _Tp& __value) { + return std::__from_chars_atoi(__first, __last, __value); } template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI from_chars_result -from_chars(const char* __first, const char* __last, _Tp& __value, int __base) -{ - _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); - return std::__from_chars_integral(__first, __last, __value, __base); +from_chars(const char* __first, const char* __last, _Tp& __value, int __base) { + _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); + return std::__from_chars_integral(__first, __last, __value, __base); } #endif // _LIBCPP_STD_VER >= 17 diff --git a/libcxx/include/__charconv/from_chars_result.h b/libcxx/include/__charconv/from_chars_result.h --- a/libcxx/include/__charconv/from_chars_result.h +++ b/libcxx/include/__charconv/from_chars_result.h @@ -21,12 +21,11 @@ #if _LIBCPP_STD_VER >= 17 -struct _LIBCPP_TYPE_VIS from_chars_result -{ - const char* ptr; - errc ec; +struct _LIBCPP_TYPE_VIS from_chars_result { + const char* ptr; + errc ec; # if _LIBCPP_STD_VER >= 20 - _LIBCPP_HIDE_FROM_ABI friend bool operator==(const from_chars_result&, const from_chars_result&) = default; + _LIBCPP_HIDE_FROM_ABI friend bool operator==(const from_chars_result&, const from_chars_result&) = default; # endif }; diff --git a/libcxx/include/__charconv/tables.h b/libcxx/include/__charconv/tables.h --- a/libcxx/include/__charconv/tables.h +++ b/libcxx/include/__charconv/tables.h @@ -62,32 +62,41 @@ 'f', 'd', 'f', 'e', 'f', 'f'}; inline constexpr uint32_t __pow10_32[10] = { - UINT32_C(0), UINT32_C(10), UINT32_C(100), UINT32_C(1000), UINT32_C(10000), - UINT32_C(100000), UINT32_C(1000000), UINT32_C(10000000), UINT32_C(100000000), UINT32_C(1000000000)}; - -inline constexpr uint64_t __pow10_64[20] = {UINT64_C(0), - UINT64_C(10), - UINT64_C(100), - UINT64_C(1000), - UINT64_C(10000), - UINT64_C(100000), - UINT64_C(1000000), - UINT64_C(10000000), - UINT64_C(100000000), - UINT64_C(1000000000), - UINT64_C(10000000000), - UINT64_C(100000000000), - UINT64_C(1000000000000), - UINT64_C(10000000000000), - UINT64_C(100000000000000), - UINT64_C(1000000000000000), - UINT64_C(10000000000000000), - UINT64_C(100000000000000000), - UINT64_C(1000000000000000000), - UINT64_C(10000000000000000000)}; + UINT32_C(0), + UINT32_C(10), + UINT32_C(100), + UINT32_C(1000), + UINT32_C(10000), + UINT32_C(100000), + UINT32_C(1000000), + UINT32_C(10000000), + UINT32_C(100000000), + UINT32_C(1000000000)}; + +inline constexpr uint64_t __pow10_64[20] = { + UINT64_C(0), + UINT64_C(10), + UINT64_C(100), + UINT64_C(1000), + UINT64_C(10000), + UINT64_C(100000), + UINT64_C(1000000), + UINT64_C(10000000), + UINT64_C(100000000), + UINT64_C(1000000000), + UINT64_C(10000000000), + UINT64_C(100000000000), + UINT64_C(1000000000000), + UINT64_C(10000000000000), + UINT64_C(100000000000000), + UINT64_C(1000000000000000), + UINT64_C(10000000000000000), + UINT64_C(100000000000000000), + UINT64_C(1000000000000000000), + UINT64_C(10000000000000000000)}; # ifndef _LIBCPP_HAS_NO_INT128 -inline constexpr int __pow10_128_offset = 0; +inline constexpr int __pow10_128_offset = 0; inline constexpr __uint128_t __pow10_128[40] = { UINT64_C(0), UINT64_C(10), diff --git a/libcxx/include/__charconv/to_chars_base_10.h b/libcxx/include/__charconv/to_chars_base_10.h --- a/libcxx/include/__charconv/to_chars_base_10.h +++ b/libcxx/include/__charconv/to_chars_base_10.h @@ -72,7 +72,8 @@ static_cast(__value % 100000000)); } -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u32(char* __first, uint32_t __value) noexcept { +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* +__base_10_u32(char* __first, uint32_t __value) noexcept { if (__value < 1000000) { if (__value < 10000) { if (__value < 100) { @@ -107,7 +108,8 @@ return __itoa::__append10(__first, __value); } -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u64(char* __buffer, uint64_t __value) noexcept { +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* +__base_10_u64(char* __buffer, uint64_t __value) noexcept { if (__value <= UINT32_MAX) return __itoa::__base_10_u32(__buffer, static_cast(__value)); @@ -134,7 +136,8 @@ return __pow10_128[__exp - __pow10_128_offset]; } -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* __base_10_u128(char* __buffer, __uint128_t __value) noexcept { +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI inline char* +__base_10_u128(char* __buffer, __uint128_t __value) noexcept { _LIBCPP_ASSERT( __value > numeric_limits::max(), "The optimizations for this algorithm fail when this isn't true."); @@ -159,8 +162,7 @@ __value %= __itoa::__pow_10(29); __buffer = __itoa::__append10(__buffer, static_cast(__value / __itoa::__pow_10(19))); __value %= __itoa::__pow_10(19); - } - else { + } else { // step 2 // This version needs to determine the position of the leading non-zero digit. __buffer = __base_10_u64(__buffer, static_cast(__value / __itoa::__pow_10(19))); diff --git a/libcxx/include/__charconv/to_chars_floating_point.h b/libcxx/include/__charconv/to_chars_floating_point.h --- a/libcxx/include/__charconv/to_chars_floating_point.h +++ b/libcxx/include/__charconv/to_chars_floating_point.h @@ -23,32 +23,32 @@ #if _LIBCPP_STD_VER >= 17 -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, float __value); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, float __value); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, double __value); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, double __value); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, long double __value); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, long double __value); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, float __value, chars_format __fmt); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, double __value, chars_format __fmt); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, long double __value, chars_format __fmt); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, float __value, chars_format __fmt, int __precision); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, double __value, chars_format __fmt, int __precision); -_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS -to_chars_result to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision); +_LIBCPP_AVAILABILITY_TO_CHARS_FLOATING_POINT _LIBCPP_FUNC_VIS to_chars_result +to_chars(char* __first, char* __last, long double __value, chars_format __fmt, int __precision); #endif // _LIBCPP_STD_VER >= 17 _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/include/__charconv/to_chars_integral.h b/libcxx/include/__charconv/to_chars_integral.h --- a/libcxx/include/__charconv/to_chars_integral.h +++ b/libcxx/include/__charconv/to_chars_integral.h @@ -47,52 +47,48 @@ template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) -{ - auto __x = std::__to_unsigned_like(__value); - if (__value < 0 && __first != __last) - { - *__first++ = '-'; - __x = std::__complement(__x); - } +__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type) { + auto __x = std::__to_unsigned_like(__value); + if (__value < 0 && __first != __last) { + *__first++ = '-'; + __x = std::__complement(__x); + } - return std::__to_chars_itoa(__first, __last, __x, false_type()); + return std::__to_chars_itoa(__first, __last, __x, false_type()); } template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) -{ - using __tx = __itoa::__traits<_Tp>; - auto __diff = __last - __first; - - if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) - return {__tx::__convert(__first, __value), errc(0)}; - else - return {__last, errc::value_too_large}; +__to_chars_itoa(char* __first, char* __last, _Tp __value, false_type) { + using __tx = __itoa::__traits<_Tp>; + auto __diff = __last - __first; + + if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) + return {__tx::__convert(__first, __value), errc(0)}; + else + return {__last, errc::value_too_large}; } # ifndef _LIBCPP_HAS_NO_INT128 template <> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -__to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type) -{ - // When the value fits in 64-bits use the 64-bit code path. This reduces - // the number of expensive calculations on 128-bit values. - // - // NOTE the 128-bit code path requires this optimization. - if(__value <= numeric_limits::max()) - return __to_chars_itoa(__first, __last, static_cast(__value), false_type()); - - using __tx = __itoa::__traits<__uint128_t>; - auto __diff = __last - __first; - - if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) - return {__tx::__convert(__first, __value), errc(0)}; - else - return {__last, errc::value_too_large}; +__to_chars_itoa(char* __first, char* __last, __uint128_t __value, false_type) { + // When the value fits in 64-bits use the 64-bit code path. This reduces + // the number of expensive calculations on 128-bit values. + // + // NOTE the 128-bit code path requires this optimization. + if (__value <= numeric_limits::max()) + return __to_chars_itoa(__first, __last, static_cast(__value), false_type()); + + using __tx = __itoa::__traits<__uint128_t>; + auto __diff = __last - __first; + + if (__tx::digits <= __diff || __tx::__width(__value) <= __diff) + return {__tx::__convert(__first, __value), errc(0)}; + else + return {__last, errc::value_too_large}; } -#endif +# endif template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result @@ -100,17 +96,14 @@ template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, - true_type) -{ - auto __x = std::__to_unsigned_like(__value); - if (__value < 0 && __first != __last) - { - *__first++ = '-'; - __x = std::__complement(__x); - } +__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, true_type) { + auto __x = std::__to_unsigned_like(__value); + if (__value < 0 && __first != __last) { + *__first++ = '-'; + __x = std::__complement(__x); + } - return std::__to_chars_integral(__first, __last, __x, __base, false_type()); + return std::__to_chars_integral(__first, __last, __x, __base, false_type()); } namespace __itoa { @@ -129,14 +122,15 @@ } template - _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { + _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result + __to_chars(char* __first, char* __last, _Tp __value) { ptrdiff_t __cap = __last - __first; - int __n = __width(__value); + int __n = __width(__value); if (__n > __cap) return {__last, errc::value_too_large}; - __last = __first + __n; - char* __p = __last; + __last = __first + __n; + char* __p = __last; const unsigned __divisor = 16; while (__value > __divisor) { unsigned __c = __value % __divisor; @@ -164,14 +158,15 @@ } template - _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { + _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result + __to_chars(char* __first, char* __last, _Tp __value) { ptrdiff_t __cap = __last - __first; - int __n = __width(__value); + int __n = __width(__value); if (__n > __cap) return {__last, errc::value_too_large}; - __last = __first + __n; - char* __p = __last; + __last = __first + __n; + char* __p = __last; unsigned __divisor = 64; while (__value > __divisor) { unsigned __c = __value % __divisor; @@ -186,7 +181,6 @@ } while (__value != 0); return {__last, errc(0)}; } - }; template <> @@ -200,14 +194,15 @@ } template - _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result __to_chars(char* __first, char* __last, _Tp __value) { + _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI static to_chars_result + __to_chars(char* __first, char* __last, _Tp __value) { ptrdiff_t __cap = __last - __first; - int __n = __width(__value); + int __n = __width(__value); if (__n > __cap) return {__last, errc::value_too_large}; - __last = __first + __n; - char* __p = __last; + __last = __first + __n; + char* __p = __last; unsigned __divisor = 256; while (__value > __divisor) { unsigned __c = __value % __divisor; @@ -227,37 +222,30 @@ } // namespace __itoa -template = sizeof(unsigned)), int>::type = 0> -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int -__to_chars_integral_width(_Tp __value) { +template = sizeof(unsigned)), int>::type = 0> +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value) { return __itoa::__integral<_Base>::__width(__value); } -template ::type = 0> -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int -__to_chars_integral_width(_Tp __value) { +template ::type = 0> +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value) { return std::__to_chars_integral_width<_Base>(static_cast(__value)); } -template = sizeof(unsigned)), int>::type = 0> +template = sizeof(unsigned)), int>::type = 0> _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result __to_chars_integral(char* __first, char* __last, _Tp __value) { return __itoa::__integral<_Base>::__to_chars(__first, __last, __value); } -template ::type = 0> +template ::type = 0> _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result __to_chars_integral(char* __first, char* __last, _Tp __value) { return std::__to_chars_integral<_Base>(__first, __last, static_cast(__value)); } template -_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int -__to_chars_integral_width(_Tp __value, unsigned __base) { +_LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __to_chars_integral_width(_Tp __value, unsigned __base) { _LIBCPP_ASSERT(__value >= 0, "The function requires a non-negative value."); unsigned __base_2 = __base * __base; @@ -284,9 +272,7 @@ template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, - false_type) -{ +__to_chars_integral(char* __first, char* __last, _Tp __value, int __base, false_type) { if (__base == 10) [[likely]] return std::__to_chars_itoa(__first, __last, __value, false_type()); @@ -300,11 +286,11 @@ } ptrdiff_t __cap = __last - __first; - int __n = std::__to_chars_integral_width(__value, __base); + int __n = std::__to_chars_integral_width(__value, __base); if (__n > __cap) return {__last, errc::value_too_large}; - __last = __first + __n; + __last = __first + __n; char* __p = __last; do { unsigned __c = __value % __base; @@ -316,8 +302,7 @@ template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -to_chars(char* __first, char* __last, _Tp __value) -{ +to_chars(char* __first, char* __last, _Tp __value) { using _Type = __make_32_64_or_128_bit_t<_Tp>; static_assert(!is_same<_Type, void>::value, "unsupported integral type used in to_chars"); return std::__to_chars_itoa(__first, __last, static_cast<_Type>(__value), is_signed<_Tp>()); @@ -325,8 +310,7 @@ template ::value, int>::type = 0> inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI to_chars_result -to_chars(char* __first, char* __last, _Tp __value, int __base) -{ +to_chars(char* __first, char* __last, _Tp __value, int __base) { _LIBCPP_ASSERT(2 <= __base && __base <= 36, "base not in [2, 36]"); using _Type = __make_32_64_or_128_bit_t<_Tp>; diff --git a/libcxx/include/__charconv/to_chars_result.h b/libcxx/include/__charconv/to_chars_result.h --- a/libcxx/include/__charconv/to_chars_result.h +++ b/libcxx/include/__charconv/to_chars_result.h @@ -21,12 +21,11 @@ #if _LIBCPP_STD_VER >= 17 -struct _LIBCPP_TYPE_VIS to_chars_result -{ - char* ptr; - errc ec; +struct _LIBCPP_TYPE_VIS to_chars_result { + char* ptr; + errc ec; # if _LIBCPP_STD_VER >= 20 - _LIBCPP_HIDE_FROM_ABI friend bool operator==(const to_chars_result&, const to_chars_result&) = default; + _LIBCPP_HIDE_FROM_ABI friend bool operator==(const to_chars_result&, const to_chars_result&) = default; # endif }; diff --git a/libcxx/include/__charconv/traits.h b/libcxx/include/__charconv/traits.h --- a/libcxx/include/__charconv/traits.h +++ b/libcxx/include/__charconv/traits.h @@ -30,42 +30,39 @@ #if _LIBCPP_STD_VER >= 17 -namespace __itoa -{ +namespace __itoa { template struct _LIBCPP_HIDDEN __traits_base; template -struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t> -{ - using type = uint32_t; - - /// The width estimation using a log10 algorithm. - /// - /// The algorithm is based on - /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 - /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that - /// function requires its input to have at least one bit set the value of - /// zero is set to one. This means the first element of the lookup table is - /// zero. - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) - { - auto __t = (32 - std::__libcpp_clz(static_cast(__v | 1))) * 1233 >> 12; - return __t - (__v < __itoa::__pow10_32[__t]) + 1; - } - - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) - { - return __itoa::__base_10_u32(__p, __v); - } - - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() { return __itoa::__pow10_32; } +struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t> { + using type = uint32_t; + + /// The width estimation using a log10 algorithm. + /// + /// The algorithm is based on + /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 + /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that + /// function requires its input to have at least one bit set the value of + /// zero is set to one. This means the first element of the lookup table is + /// zero. + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { + auto __t = (32 - std::__libcpp_clz(static_cast(__v | 1))) * 1233 >> 12; + return __t - (__v < __itoa::__pow10_32[__t]) + 1; + } + + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { + return __itoa::__base_10_u32(__p, __v); + } + + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() { + return __itoa::__pow10_32; + } }; template -struct _LIBCPP_HIDDEN - __traits_base<_Tp, __enable_if_t> { +struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t> { using type = uint64_t; /// The width estimation using a log10 algorithm. @@ -81,16 +78,18 @@ return __t - (__v < __itoa::__pow10_64[__t]) + 1; } - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(__p, __v); } + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { + return __itoa::__base_10_u64(__p, __v); + } - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() { return __itoa::__pow10_64; } + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() { + return __itoa::__pow10_64; + } }; - # ifndef _LIBCPP_HAS_NO_INT128 template -struct _LIBCPP_HIDDEN - __traits_base<_Tp, __enable_if_t > { +struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t > { using type = __uint128_t; /// The width estimation using a log10 algorithm. @@ -102,7 +101,8 @@ /// zero is set to one. This means the first element of the lookup table is /// zero. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) { - _LIBCPP_ASSERT(__v > numeric_limits::max(), "The optimizations for this algorithm fail when this isn't true."); + _LIBCPP_ASSERT( + __v > numeric_limits::max(), "The optimizations for this algorithm fail when this isn't true."); // There's always a bit set in the upper 64-bits. auto __t = (128 - std::__libcpp_clz(static_cast(__v >> 64))) * 1233 >> 12; _LIBCPP_ASSERT(__t >= __itoa::__pow10_128_offset, "Index out of bounds"); @@ -110,96 +110,86 @@ return __t - (__v < __itoa::__pow10_128[__t - __itoa::__pow10_128_offset]) + 1; } - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u128(__p, __v); } + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { + return __itoa::__base_10_u128(__p, __v); + } // TODO FMT This pow function should get an index. // By moving this to its own header it can be reused by the pow function in to_chars_base_10. - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() { return __itoa::__pow10_128; } + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() { + return __itoa::__pow10_128; + } }; -#endif +# endif template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool -__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) -{ - auto __c = __a * __b; - __r = __c; - return __c > numeric_limits::max(); +__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) { + auto __c = __a * __b; + __r = __c; + return __c > numeric_limits::max(); } template inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool -__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) -{ - auto __c = __a * __b; - __r = __c; - return __c > numeric_limits::max(); +__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) { + auto __c = __a * __b; + __r = __c; + return __c > numeric_limits::max(); } template -inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool -__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) -{ - static_assert(is_unsigned<_Tp>::value, ""); - return __builtin_mul_overflow(__a, __b, &__r); +inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) { + static_assert(is_unsigned<_Tp>::value, ""); + return __builtin_mul_overflow(__a, __b, &__r); } template -inline _LIBCPP_HIDE_FROM_ABI bool -_LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) -{ - return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r); +inline _LIBCPP_HIDE_FROM_ABI bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) { + return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r); } template -struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> -{ - static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; - using __traits_base<_Tp>::__pow; - using typename __traits_base<_Tp>::type; - - // precondition: at least one non-zero character available - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const* - __read(char const* __p, char const* __ep, type& __a, type& __b) - { - type __cprod[digits]; - int __j = digits - 1; - int __i = digits; - do - { - if (*__p < '0' || *__p > '9') - break; - __cprod[--__i] = *__p++ - '0'; - } while (__p != __ep && __i != 0); - - __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, - __cprod[__i]); - if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) - --__p; - return __p; - } - - template - static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up - __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) - { - for (; __first1 < __last1; ++__first1, ++__first2) - __init = __init + *__first1 * *__first2; - return __init; - } +struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> { + static constexpr int digits = numeric_limits<_Tp>::digits10 + 1; + using __traits_base<_Tp>::__pow; + using typename __traits_base<_Tp>::type; + + // precondition: at least one non-zero character available + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const* + __read(char const* __p, char const* __ep, type& __a, type& __b) { + type __cprod[digits]; + int __j = digits - 1; + int __i = digits; + do { + if (*__p < '0' || *__p > '9') + break; + __cprod[--__i] = *__p++ - '0'; + } while (__p != __ep && __i != 0); + + __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, __cprod[__i]); + if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b)) + --__p; + return __p; + } + + template + static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up + __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) { + for (; __first1 < __last1; ++__first1, ++__first2) + __init = __init + *__first1 * *__first2; + return __init; + } }; -} // namespace __itoa +} // namespace __itoa template -inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp -__complement(_Tp __x) -{ - static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); - return _Tp(~__x + 1); +inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp __complement(_Tp __x) { + static_assert(is_unsigned<_Tp>::value, "cast to unsigned first"); + return _Tp(~__x + 1); } - #endif // _LIBCPP_STD_VER >= 17 _LIBCPP_END_NAMESPACE_STD diff --git a/libcxx/utils/data/ignore_format.txt b/libcxx/utils/data/ignore_format.txt --- a/libcxx/utils/data/ignore_format.txt +++ b/libcxx/utils/data/ignore_format.txt @@ -239,15 +239,6 @@ libcxx/include/__bsd_locale_defaults.h libcxx/include/__bsd_locale_fallbacks.h libcxx/include/cctype -libcxx/include/__charconv/chars_format.h -libcxx/include/__charconv/from_chars_integral.h -libcxx/include/__charconv/from_chars_result.h -libcxx/include/__charconv/tables.h -libcxx/include/__charconv/to_chars_base_10.h -libcxx/include/__charconv/to_chars_floating_point.h -libcxx/include/__charconv/to_chars_integral.h -libcxx/include/__charconv/to_chars_result.h -libcxx/include/__charconv/traits.h libcxx/include/chrono libcxx/include/__chrono/calendar.h libcxx/include/__chrono/convert_to_timespec.h