llvm-project/libcxx/include/charconv

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

828 lines
27 KiB
Plaintext
Raw Normal View History

// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_CHARCONV
#define _LIBCPP_CHARCONV
/*
charconv synopsis
namespace std {
// floating-point format for primitive numerical conversion
enum class chars_format {
scientific = unspecified,
fixed = unspecified,
hex = unspecified,
general = fixed | scientific
};
// 23.20.2, primitive numerical output conversion
struct to_chars_result {
char* ptr;
errc ec;
friend bool operator==(const to_chars_result&, const to_chars_result&) = default; // since C++20
};
to_chars_result to_chars(char* first, char* last, see below value,
int base = 10);
to_chars_result to_chars(char* first, char* last, bool value,
int base = 10) = delete;
to_chars_result to_chars(char* first, char* last, float value);
to_chars_result to_chars(char* first, char* last, double value);
to_chars_result to_chars(char* first, char* last, long double value);
to_chars_result to_chars(char* first, char* last, float value,
chars_format fmt);
to_chars_result to_chars(char* first, char* last, double value,
chars_format fmt);
to_chars_result to_chars(char* first, char* last, long double value,
chars_format fmt);
to_chars_result to_chars(char* first, char* last, float value,
chars_format fmt, int precision);
to_chars_result to_chars(char* first, char* last, double value,
chars_format fmt, int precision);
to_chars_result to_chars(char* first, char* last, long double value,
chars_format fmt, int precision);
// 23.20.3, primitive numerical input conversion
struct from_chars_result {
const char* ptr;
errc ec;
friend bool operator==(const from_chars_result&, const from_chars_result&) = default; // since C++20
};
from_chars_result from_chars(const char* first, const char* last,
see below& value, int base = 10);
from_chars_result from_chars(const char* first, const char* last,
float& value,
chars_format fmt = chars_format::general);
from_chars_result from_chars(const char* first, const char* last,
double& value,
chars_format fmt = chars_format::general);
from_chars_result from_chars(const char* first, const char* last,
long double& value,
chars_format fmt = chars_format::general);
} // namespace std
*/
#include <__assert> // all public C++ headers provide the assertion handler
#include <__availability>
#include <__bits>
#include <__charconv/chars_format.h>
#include <__charconv/from_chars_result.h>
#include <__charconv/tables.h>
#include <__charconv/to_chars_base_10.h>
#include <__charconv/to_chars_result.h>
#include <__config>
#include <__debug>
#include <__errc>
#include <__type_traits/make_32_64_or_128_bit.h>
#include <__utility/unreachable.h>
#include <cmath> // for log2f
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <type_traits>
[libc++] Re-add transitive includes that had been removed since LLVM 14 This commit re-adds transitive includes that had been removed by 4cd04d1687f1, c36870c8e79c, a83f4b9cda57, 1458458b558d, 2e2f3158c604, and 489637e66dd3. This should cover almost all the includes that had been removed since LLVM 14 and that would contribute to breaking user code when releasing LLVM 15. It is possible to disable the inclusion of these headers by defining _LIBCPP_REMOVE_TRANSITIVE_INCLUDES. The intent is that vendors will enable that macro and start fixing downstream issues immediately. We can then remove the macro (and the transitive includes) by default in a future release. That way, we will break users only once by removing transitive includes in bulk instead of doing it bit by bit a every release, which is more disruptive for users. Note 1: The set of headers to re-add was found by re-generating the transitive include test on a checkout of release/14.x, which provided the list of all transitive includes we used to provide. Note 2: Several includes of <vector>, <optional>, <array> and <unordered_map> have been added in this commit. These transitive inclusions were added when we implemented boyer_moore_searcher in <functional>. Note 3: This is a best effort patch to try and resolve downstream breakage caused since branching LLVM 14. I wasn't able to perfectly mirror transitive includes in LLVM 14 for a few headers, so I added a release note explaining it. To summarize, adding boyer_moore_searcher created a bunch of circular dependencies, so we have to break backwards compatibility in a few cases. Differential Revision: https://reviews.llvm.org/D128661
2022-06-28 03:53:41 +08:00
#ifndef _LIBCPP_REMOVE_TRANSITIVE_INCLUDES
# include <iosfwd>
#endif
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
#ifndef _LIBCPP_CXX03_LANG
to_chars_result to_chars(char*, char*, bool, int = 10) = delete;
from_chars_result from_chars(const char*, const char*, bool, int = 10) = delete;
namespace __itoa
{
template <typename _Tp, typename = void>
struct _LIBCPP_HIDDEN __traits_base;
template <typename _Tp>
struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_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_HIDE_FROM_ABI int __width(_Tp __v)
{
auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
return __t - (__v < __table<>::__pow10_32[__t]) + 1;
}
[libc++] Improve charconv base10 algorithm. This change is a preparation to add the 128-bit integral output. Before ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 20.1 ns 20.1 ns 35045000 BM_to_chars_good/3 117 ns 117 ns 5916000 BM_to_chars_good/4 83.7 ns 83.7 ns 8401000 BM_to_chars_good/5 70.6 ns 70.6 ns 9915000 BM_to_chars_good/6 59.9 ns 59.9 ns 11678000 BM_to_chars_good/7 53.9 ns 53.8 ns 12995000 BM_to_chars_good/8 19.0 ns 19.0 ns 37110000 BM_to_chars_good/9 45.9 ns 45.8 ns 15278000 BM_to_chars_good/10 9.24 ns 9.24 ns 75343000 BM_to_chars_good/11 42.6 ns 42.6 ns 16449000 BM_to_chars_good/12 38.8 ns 38.8 ns 18101000 BM_to_chars_good/13 38.8 ns 38.8 ns 17999000 BM_to_chars_good/14 37.7 ns 37.6 ns 18571000 BM_to_chars_good/15 35.8 ns 35.8 ns 19660000 BM_to_chars_good/16 15.4 ns 15.4 ns 46129000 BM_to_chars_good/17 32.3 ns 32.3 ns 21763000 BM_to_chars_good/18 32.8 ns 32.8 ns 21396000 BM_to_chars_good/19 33.4 ns 33.4 ns 21078000 BM_to_chars_good/20 33.3 ns 33.3 ns 21020000 BM_to_chars_good/21 32.3 ns 32.3 ns 21807000 BM_to_chars_good/22 31.6 ns 31.6 ns 22057000 BM_to_chars_good/23 30.7 ns 30.7 ns 22938000 BM_to_chars_good/24 28.3 ns 28.3 ns 24659000 BM_to_chars_good/25 28.2 ns 28.2 ns 24790000 BM_to_chars_good/26 28.4 ns 28.4 ns 24410000 BM_to_chars_good/27 28.7 ns 28.7 ns 24423000 BM_to_chars_good/28 28.9 ns 28.9 ns 24139000 BM_to_chars_good/29 28.9 ns 28.9 ns 24347000 BM_to_chars_good/30 29.2 ns 29.2 ns 24141000 BM_to_chars_good/31 29.6 ns 29.6 ns 23699000 BM_to_chars_good/32 29.5 ns 29.5 ns 23933000 BM_to_chars_good/33 28.9 ns 28.9 ns 24042000 BM_to_chars_good/34 28.7 ns 28.7 ns 24361000 BM_to_chars_good/35 28.3 ns 28.3 ns 24703000 BM_to_chars_good/36 28.1 ns 28.1 ns 24924000 BM_to_chars_bad/2 6.16 ns 6.15 ns 114101000 BM_to_chars_bad/3 14.5 ns 14.5 ns 48244000 BM_to_chars_bad/4 16.9 ns 16.9 ns 41974000 BM_to_chars_bad/5 12.5 ns 12.5 ns 56080000 BM_to_chars_bad/6 10.9 ns 10.9 ns 64036000 BM_to_chars_bad/7 14.5 ns 14.5 ns 47294000 BM_to_chars_bad/8 6.36 ns 6.35 ns 110430000 BM_to_chars_bad/9 12.4 ns 12.4 ns 56448000 BM_to_chars_bad/10 5.13 ns 5.13 ns 137596000 BM_to_chars_bad/11 9.88 ns 9.88 ns 69015000 BM_to_chars_bad/12 10.8 ns 10.8 ns 63990000 BM_to_chars_bad/13 10.7 ns 10.7 ns 65066000 BM_to_chars_bad/14 9.71 ns 9.71 ns 71775000 BM_to_chars_bad/15 9.18 ns 9.18 ns 75267000 BM_to_chars_bad/16 6.12 ns 6.12 ns 115000000 BM_to_chars_bad/17 10.7 ns 10.7 ns 65504000 BM_to_chars_bad/18 10.6 ns 10.6 ns 65685000 BM_to_chars_bad/19 9.98 ns 9.98 ns 69894000 BM_to_chars_bad/20 9.74 ns 9.74 ns 72098000 BM_to_chars_bad/21 9.25 ns 9.25 ns 75184000 BM_to_chars_bad/22 9.10 ns 9.10 ns 75602000 BM_to_chars_bad/23 9.48 ns 9.48 ns 72824000 BM_to_chars_bad/24 9.27 ns 9.27 ns 75112000 BM_to_chars_bad/25 9.61 ns 9.61 ns 72080000 BM_to_chars_bad/26 9.72 ns 9.72 ns 72178000 BM_to_chars_bad/27 10.0 ns 10.0 ns 69733000 BM_to_chars_bad/28 10.3 ns 10.3 ns 67409000 BM_to_chars_bad/29 9.97 ns 9.97 ns 69193000 BM_to_chars_bad/30 10.1 ns 10.1 ns 69007000 BM_to_chars_bad/31 9.68 ns 9.68 ns 72232000 BM_to_chars_bad/32 8.99 ns 8.99 ns 76825000 BM_to_chars_bad/33 8.82 ns 8.82 ns 79293000 BM_to_chars_bad/34 8.64 ns 8.64 ns 80441000 BM_to_chars_bad/35 8.96 ns 8.96 ns 75320000 BM_to_chars_bad/36 8.87 ns 8.87 ns 77293000 ``` After ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 14.7 ns 14.7 ns 47583000 BM_to_chars_good/3 101 ns 101 ns 6901000 BM_to_chars_good/4 68.4 ns 68.4 ns 10088000 BM_to_chars_good/5 58.2 ns 58.2 ns 12007000 BM_to_chars_good/6 51.1 ns 51.1 ns 13687000 BM_to_chars_good/7 45.6 ns 45.6 ns 15323000 BM_to_chars_good/8 14.6 ns 14.6 ns 47795000 BM_to_chars_good/9 40.7 ns 40.7 ns 17371000 BM_to_chars_good/10 7.48 ns 7.48 ns 90931000 BM_to_chars_good/11 37.6 ns 37.6 ns 18542000 BM_to_chars_good/12 35.2 ns 35.2 ns 19922000 BM_to_chars_good/13 34.9 ns 34.9 ns 20105000 BM_to_chars_good/14 33.5 ns 33.5 ns 20863000 BM_to_chars_good/15 31.9 ns 31.9 ns 22014000 BM_to_chars_good/16 11.7 ns 11.7 ns 60012000 BM_to_chars_good/17 28.9 ns 28.9 ns 24148000 BM_to_chars_good/18 29.0 ns 29.0 ns 24317000 BM_to_chars_good/19 28.7 ns 28.7 ns 24363000 BM_to_chars_good/20 28.1 ns 28.1 ns 24899000 BM_to_chars_good/21 27.5 ns 27.5 ns 25499000 BM_to_chars_good/22 26.9 ns 26.9 ns 25929000 BM_to_chars_good/23 26.2 ns 26.2 ns 26828000 BM_to_chars_good/24 25.1 ns 25.1 ns 27742000 BM_to_chars_good/25 25.3 ns 25.3 ns 27720000 BM_to_chars_good/26 25.2 ns 25.2 ns 27789000 BM_to_chars_good/27 25.3 ns 25.3 ns 27777000 BM_to_chars_good/28 25.3 ns 25.3 ns 27643000 BM_to_chars_good/29 25.3 ns 25.3 ns 27750000 BM_to_chars_good/30 25.4 ns 25.4 ns 27566000 BM_to_chars_good/31 25.4 ns 25.4 ns 27611000 BM_to_chars_good/32 25.8 ns 25.8 ns 27218000 BM_to_chars_good/33 25.7 ns 25.7 ns 27070000 BM_to_chars_good/34 26.1 ns 26.1 ns 26693000 BM_to_chars_good/35 26.4 ns 26.4 ns 26486000 BM_to_chars_good/36 26.3 ns 26.3 ns 26619000 BM_to_chars_bad/2 5.99 ns 5.99 ns 118787000 BM_to_chars_bad/3 14.3 ns 14.3 ns 48567000 BM_to_chars_bad/4 16.0 ns 16.0 ns 43239000 BM_to_chars_bad/5 12.6 ns 12.6 ns 55354000 BM_to_chars_bad/6 10.7 ns 10.7 ns 65491000 BM_to_chars_bad/7 14.4 ns 14.4 ns 48723000 BM_to_chars_bad/8 6.50 ns 6.50 ns 104967000 BM_to_chars_bad/9 12.0 ns 12.0 ns 56552000 BM_to_chars_bad/10 5.16 ns 5.16 ns 136380000 BM_to_chars_bad/11 10.5 ns 10.5 ns 66764000 BM_to_chars_bad/12 10.7 ns 10.7 ns 65534000 BM_to_chars_bad/13 11.0 ns 11.0 ns 63426000 BM_to_chars_bad/14 9.90 ns 9.90 ns 68575000 BM_to_chars_bad/15 9.52 ns 9.52 ns 70932000 BM_to_chars_bad/16 6.14 ns 6.14 ns 111762000 BM_to_chars_bad/17 10.6 ns 10.6 ns 65883000 BM_to_chars_bad/18 10.5 ns 10.5 ns 67606000 BM_to_chars_bad/19 9.96 ns 9.96 ns 68898000 BM_to_chars_bad/20 9.40 ns 9.41 ns 73116000 BM_to_chars_bad/21 9.12 ns 9.12 ns 78647000 BM_to_chars_bad/22 8.95 ns 8.95 ns 80211000 BM_to_chars_bad/23 9.50 ns 9.49 ns 73571000 BM_to_chars_bad/24 9.29 ns 9.29 ns 74690000 BM_to_chars_bad/25 9.65 ns 9.65 ns 72877000 BM_to_chars_bad/26 9.78 ns 9.78 ns 70171000 BM_to_chars_bad/27 10.1 ns 10.1 ns 69543000 BM_to_chars_bad/28 10.4 ns 10.4 ns 67582000 BM_to_chars_bad/29 10.00 ns 10.00 ns 70806000 BM_to_chars_bad/30 9.99 ns 9.99 ns 70340000 BM_to_chars_bad/31 9.56 ns 9.56 ns 74159000 BM_to_chars_bad/32 8.97 ns 8.97 ns 78052000 BM_to_chars_bad/33 8.86 ns 8.86 ns 78586000 BM_to_chars_bad/34 8.81 ns 8.81 ns 78562000 BM_to_chars_bad/35 8.90 ns 8.90 ns 77384000 BM_to_chars_bad/36 9.04 ns 9.04 ns 77263000 ``` Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D127764
2022-05-16 13:42:40 +08:00
static _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v)
{
[libc++] Improve charconv base10 algorithm. This change is a preparation to add the 128-bit integral output. Before ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 20.1 ns 20.1 ns 35045000 BM_to_chars_good/3 117 ns 117 ns 5916000 BM_to_chars_good/4 83.7 ns 83.7 ns 8401000 BM_to_chars_good/5 70.6 ns 70.6 ns 9915000 BM_to_chars_good/6 59.9 ns 59.9 ns 11678000 BM_to_chars_good/7 53.9 ns 53.8 ns 12995000 BM_to_chars_good/8 19.0 ns 19.0 ns 37110000 BM_to_chars_good/9 45.9 ns 45.8 ns 15278000 BM_to_chars_good/10 9.24 ns 9.24 ns 75343000 BM_to_chars_good/11 42.6 ns 42.6 ns 16449000 BM_to_chars_good/12 38.8 ns 38.8 ns 18101000 BM_to_chars_good/13 38.8 ns 38.8 ns 17999000 BM_to_chars_good/14 37.7 ns 37.6 ns 18571000 BM_to_chars_good/15 35.8 ns 35.8 ns 19660000 BM_to_chars_good/16 15.4 ns 15.4 ns 46129000 BM_to_chars_good/17 32.3 ns 32.3 ns 21763000 BM_to_chars_good/18 32.8 ns 32.8 ns 21396000 BM_to_chars_good/19 33.4 ns 33.4 ns 21078000 BM_to_chars_good/20 33.3 ns 33.3 ns 21020000 BM_to_chars_good/21 32.3 ns 32.3 ns 21807000 BM_to_chars_good/22 31.6 ns 31.6 ns 22057000 BM_to_chars_good/23 30.7 ns 30.7 ns 22938000 BM_to_chars_good/24 28.3 ns 28.3 ns 24659000 BM_to_chars_good/25 28.2 ns 28.2 ns 24790000 BM_to_chars_good/26 28.4 ns 28.4 ns 24410000 BM_to_chars_good/27 28.7 ns 28.7 ns 24423000 BM_to_chars_good/28 28.9 ns 28.9 ns 24139000 BM_to_chars_good/29 28.9 ns 28.9 ns 24347000 BM_to_chars_good/30 29.2 ns 29.2 ns 24141000 BM_to_chars_good/31 29.6 ns 29.6 ns 23699000 BM_to_chars_good/32 29.5 ns 29.5 ns 23933000 BM_to_chars_good/33 28.9 ns 28.9 ns 24042000 BM_to_chars_good/34 28.7 ns 28.7 ns 24361000 BM_to_chars_good/35 28.3 ns 28.3 ns 24703000 BM_to_chars_good/36 28.1 ns 28.1 ns 24924000 BM_to_chars_bad/2 6.16 ns 6.15 ns 114101000 BM_to_chars_bad/3 14.5 ns 14.5 ns 48244000 BM_to_chars_bad/4 16.9 ns 16.9 ns 41974000 BM_to_chars_bad/5 12.5 ns 12.5 ns 56080000 BM_to_chars_bad/6 10.9 ns 10.9 ns 64036000 BM_to_chars_bad/7 14.5 ns 14.5 ns 47294000 BM_to_chars_bad/8 6.36 ns 6.35 ns 110430000 BM_to_chars_bad/9 12.4 ns 12.4 ns 56448000 BM_to_chars_bad/10 5.13 ns 5.13 ns 137596000 BM_to_chars_bad/11 9.88 ns 9.88 ns 69015000 BM_to_chars_bad/12 10.8 ns 10.8 ns 63990000 BM_to_chars_bad/13 10.7 ns 10.7 ns 65066000 BM_to_chars_bad/14 9.71 ns 9.71 ns 71775000 BM_to_chars_bad/15 9.18 ns 9.18 ns 75267000 BM_to_chars_bad/16 6.12 ns 6.12 ns 115000000 BM_to_chars_bad/17 10.7 ns 10.7 ns 65504000 BM_to_chars_bad/18 10.6 ns 10.6 ns 65685000 BM_to_chars_bad/19 9.98 ns 9.98 ns 69894000 BM_to_chars_bad/20 9.74 ns 9.74 ns 72098000 BM_to_chars_bad/21 9.25 ns 9.25 ns 75184000 BM_to_chars_bad/22 9.10 ns 9.10 ns 75602000 BM_to_chars_bad/23 9.48 ns 9.48 ns 72824000 BM_to_chars_bad/24 9.27 ns 9.27 ns 75112000 BM_to_chars_bad/25 9.61 ns 9.61 ns 72080000 BM_to_chars_bad/26 9.72 ns 9.72 ns 72178000 BM_to_chars_bad/27 10.0 ns 10.0 ns 69733000 BM_to_chars_bad/28 10.3 ns 10.3 ns 67409000 BM_to_chars_bad/29 9.97 ns 9.97 ns 69193000 BM_to_chars_bad/30 10.1 ns 10.1 ns 69007000 BM_to_chars_bad/31 9.68 ns 9.68 ns 72232000 BM_to_chars_bad/32 8.99 ns 8.99 ns 76825000 BM_to_chars_bad/33 8.82 ns 8.82 ns 79293000 BM_to_chars_bad/34 8.64 ns 8.64 ns 80441000 BM_to_chars_bad/35 8.96 ns 8.96 ns 75320000 BM_to_chars_bad/36 8.87 ns 8.87 ns 77293000 ``` After ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 14.7 ns 14.7 ns 47583000 BM_to_chars_good/3 101 ns 101 ns 6901000 BM_to_chars_good/4 68.4 ns 68.4 ns 10088000 BM_to_chars_good/5 58.2 ns 58.2 ns 12007000 BM_to_chars_good/6 51.1 ns 51.1 ns 13687000 BM_to_chars_good/7 45.6 ns 45.6 ns 15323000 BM_to_chars_good/8 14.6 ns 14.6 ns 47795000 BM_to_chars_good/9 40.7 ns 40.7 ns 17371000 BM_to_chars_good/10 7.48 ns 7.48 ns 90931000 BM_to_chars_good/11 37.6 ns 37.6 ns 18542000 BM_to_chars_good/12 35.2 ns 35.2 ns 19922000 BM_to_chars_good/13 34.9 ns 34.9 ns 20105000 BM_to_chars_good/14 33.5 ns 33.5 ns 20863000 BM_to_chars_good/15 31.9 ns 31.9 ns 22014000 BM_to_chars_good/16 11.7 ns 11.7 ns 60012000 BM_to_chars_good/17 28.9 ns 28.9 ns 24148000 BM_to_chars_good/18 29.0 ns 29.0 ns 24317000 BM_to_chars_good/19 28.7 ns 28.7 ns 24363000 BM_to_chars_good/20 28.1 ns 28.1 ns 24899000 BM_to_chars_good/21 27.5 ns 27.5 ns 25499000 BM_to_chars_good/22 26.9 ns 26.9 ns 25929000 BM_to_chars_good/23 26.2 ns 26.2 ns 26828000 BM_to_chars_good/24 25.1 ns 25.1 ns 27742000 BM_to_chars_good/25 25.3 ns 25.3 ns 27720000 BM_to_chars_good/26 25.2 ns 25.2 ns 27789000 BM_to_chars_good/27 25.3 ns 25.3 ns 27777000 BM_to_chars_good/28 25.3 ns 25.3 ns 27643000 BM_to_chars_good/29 25.3 ns 25.3 ns 27750000 BM_to_chars_good/30 25.4 ns 25.4 ns 27566000 BM_to_chars_good/31 25.4 ns 25.4 ns 27611000 BM_to_chars_good/32 25.8 ns 25.8 ns 27218000 BM_to_chars_good/33 25.7 ns 25.7 ns 27070000 BM_to_chars_good/34 26.1 ns 26.1 ns 26693000 BM_to_chars_good/35 26.4 ns 26.4 ns 26486000 BM_to_chars_good/36 26.3 ns 26.3 ns 26619000 BM_to_chars_bad/2 5.99 ns 5.99 ns 118787000 BM_to_chars_bad/3 14.3 ns 14.3 ns 48567000 BM_to_chars_bad/4 16.0 ns 16.0 ns 43239000 BM_to_chars_bad/5 12.6 ns 12.6 ns 55354000 BM_to_chars_bad/6 10.7 ns 10.7 ns 65491000 BM_to_chars_bad/7 14.4 ns 14.4 ns 48723000 BM_to_chars_bad/8 6.50 ns 6.50 ns 104967000 BM_to_chars_bad/9 12.0 ns 12.0 ns 56552000 BM_to_chars_bad/10 5.16 ns 5.16 ns 136380000 BM_to_chars_bad/11 10.5 ns 10.5 ns 66764000 BM_to_chars_bad/12 10.7 ns 10.7 ns 65534000 BM_to_chars_bad/13 11.0 ns 11.0 ns 63426000 BM_to_chars_bad/14 9.90 ns 9.90 ns 68575000 BM_to_chars_bad/15 9.52 ns 9.52 ns 70932000 BM_to_chars_bad/16 6.14 ns 6.14 ns 111762000 BM_to_chars_bad/17 10.6 ns 10.6 ns 65883000 BM_to_chars_bad/18 10.5 ns 10.5 ns 67606000 BM_to_chars_bad/19 9.96 ns 9.96 ns 68898000 BM_to_chars_bad/20 9.40 ns 9.41 ns 73116000 BM_to_chars_bad/21 9.12 ns 9.12 ns 78647000 BM_to_chars_bad/22 8.95 ns 8.95 ns 80211000 BM_to_chars_bad/23 9.50 ns 9.49 ns 73571000 BM_to_chars_bad/24 9.29 ns 9.29 ns 74690000 BM_to_chars_bad/25 9.65 ns 9.65 ns 72877000 BM_to_chars_bad/26 9.78 ns 9.78 ns 70171000 BM_to_chars_bad/27 10.1 ns 10.1 ns 69543000 BM_to_chars_bad/28 10.4 ns 10.4 ns 67582000 BM_to_chars_bad/29 10.00 ns 10.00 ns 70806000 BM_to_chars_bad/30 9.99 ns 9.99 ns 70340000 BM_to_chars_bad/31 9.56 ns 9.56 ns 74159000 BM_to_chars_bad/32 8.97 ns 8.97 ns 78052000 BM_to_chars_bad/33 8.86 ns 8.86 ns 78586000 BM_to_chars_bad/34 8.81 ns 8.81 ns 78562000 BM_to_chars_bad/35 8.90 ns 8.90 ns 77384000 BM_to_chars_bad/36 9.04 ns 9.04 ns 77263000 ``` Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D127764
2022-05-16 13:42:40 +08:00
return __itoa::__base_10_u32(__p, __v);
}
static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_32)& __pow() { return __table<>::__pow10_32; }
};
template <typename _Tp>
struct _LIBCPP_HIDDEN
__traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> {
using type = uint64_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_HIDE_FROM_ABI int __width(_Tp __v) {
auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
return __t - (__v < __table<>::__pow10_64[__t]) + 1;
}
static _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(__p, __v); }
static _LIBCPP_HIDE_FROM_ABI decltype(__table<>::__pow10_64)& __pow() { return __table<>::__pow10_64; }
};
# ifndef _LIBCPP_HAS_NO_INT128
template <typename _Tp>
struct _LIBCPP_HIDDEN
__traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > {
using type = __uint128_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_HIDE_FROM_ABI int __width(_Tp __v) {
_LIBCPP_ASSERT(__v > numeric_limits<uint64_t>::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<uint64_t>(__v >> 64))) * 1233 >> 12;
_LIBCPP_ASSERT(__t >= __table<>::__pow10_128_offset, "Index out of bounds");
// __t is adjusted since the lookup table misses the lower entries.
return __t - (__v < __table<>::__pow10_128[__t - __table<>::__pow10_128_offset]) + 1;
}
static _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_HIDE_FROM_ABI decltype(__table<>::__pow10_128)& __pow() { return __table<>::__pow10_128; }
};
#endif
template <typename _Tp>
inline _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<unsigned char>::max();
}
template <typename _Tp>
inline _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<unsigned short>::max();
}
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI bool
__mul_overflowed(_Tp __a, _Tp __b, _Tp& __r)
{
static_assert(is_unsigned<_Tp>::value, "");
#if !defined(_LIBCPP_COMPILER_MSVC)
return __builtin_mul_overflow(__a, __b, &__r);
#else
bool __did = __b && (numeric_limits<_Tp>::max() / __b) < __a;
__r = __a * __b;
return __did;
#endif
}
template <typename _Tp, typename _Up>
inline _LIBCPP_HIDE_FROM_ABI bool
__mul_overflowed(_Tp __a, _Up __b, _Tp& __r)
{
return __mul_overflowed(__a, static_cast<_Tp>(__b), __r);
}
template <typename _Tp>
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_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 (!('0' <= *__p && *__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 (__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
--__p;
return __p;
}
template <typename _It1, typename _It2, class _Up>
static _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
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI _Tp
__complement(_Tp __x)
{
static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
return _Tp(~__x + 1);
}
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI to_chars_result
__to_chars_itoa(char* __first, char* __last, _Tp __value, true_type)
{
auto __x = __to_unsigned_like(__value);
if (__value < 0 && __first != __last)
{
*__first++ = '-';
__x = __complement(__x);
}
return __to_chars_itoa(__first, __last, __x, false_type());
}
template <typename _Tp>
inline _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)
[libc++] Improve charconv base10 algorithm. This change is a preparation to add the 128-bit integral output. Before ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 20.1 ns 20.1 ns 35045000 BM_to_chars_good/3 117 ns 117 ns 5916000 BM_to_chars_good/4 83.7 ns 83.7 ns 8401000 BM_to_chars_good/5 70.6 ns 70.6 ns 9915000 BM_to_chars_good/6 59.9 ns 59.9 ns 11678000 BM_to_chars_good/7 53.9 ns 53.8 ns 12995000 BM_to_chars_good/8 19.0 ns 19.0 ns 37110000 BM_to_chars_good/9 45.9 ns 45.8 ns 15278000 BM_to_chars_good/10 9.24 ns 9.24 ns 75343000 BM_to_chars_good/11 42.6 ns 42.6 ns 16449000 BM_to_chars_good/12 38.8 ns 38.8 ns 18101000 BM_to_chars_good/13 38.8 ns 38.8 ns 17999000 BM_to_chars_good/14 37.7 ns 37.6 ns 18571000 BM_to_chars_good/15 35.8 ns 35.8 ns 19660000 BM_to_chars_good/16 15.4 ns 15.4 ns 46129000 BM_to_chars_good/17 32.3 ns 32.3 ns 21763000 BM_to_chars_good/18 32.8 ns 32.8 ns 21396000 BM_to_chars_good/19 33.4 ns 33.4 ns 21078000 BM_to_chars_good/20 33.3 ns 33.3 ns 21020000 BM_to_chars_good/21 32.3 ns 32.3 ns 21807000 BM_to_chars_good/22 31.6 ns 31.6 ns 22057000 BM_to_chars_good/23 30.7 ns 30.7 ns 22938000 BM_to_chars_good/24 28.3 ns 28.3 ns 24659000 BM_to_chars_good/25 28.2 ns 28.2 ns 24790000 BM_to_chars_good/26 28.4 ns 28.4 ns 24410000 BM_to_chars_good/27 28.7 ns 28.7 ns 24423000 BM_to_chars_good/28 28.9 ns 28.9 ns 24139000 BM_to_chars_good/29 28.9 ns 28.9 ns 24347000 BM_to_chars_good/30 29.2 ns 29.2 ns 24141000 BM_to_chars_good/31 29.6 ns 29.6 ns 23699000 BM_to_chars_good/32 29.5 ns 29.5 ns 23933000 BM_to_chars_good/33 28.9 ns 28.9 ns 24042000 BM_to_chars_good/34 28.7 ns 28.7 ns 24361000 BM_to_chars_good/35 28.3 ns 28.3 ns 24703000 BM_to_chars_good/36 28.1 ns 28.1 ns 24924000 BM_to_chars_bad/2 6.16 ns 6.15 ns 114101000 BM_to_chars_bad/3 14.5 ns 14.5 ns 48244000 BM_to_chars_bad/4 16.9 ns 16.9 ns 41974000 BM_to_chars_bad/5 12.5 ns 12.5 ns 56080000 BM_to_chars_bad/6 10.9 ns 10.9 ns 64036000 BM_to_chars_bad/7 14.5 ns 14.5 ns 47294000 BM_to_chars_bad/8 6.36 ns 6.35 ns 110430000 BM_to_chars_bad/9 12.4 ns 12.4 ns 56448000 BM_to_chars_bad/10 5.13 ns 5.13 ns 137596000 BM_to_chars_bad/11 9.88 ns 9.88 ns 69015000 BM_to_chars_bad/12 10.8 ns 10.8 ns 63990000 BM_to_chars_bad/13 10.7 ns 10.7 ns 65066000 BM_to_chars_bad/14 9.71 ns 9.71 ns 71775000 BM_to_chars_bad/15 9.18 ns 9.18 ns 75267000 BM_to_chars_bad/16 6.12 ns 6.12 ns 115000000 BM_to_chars_bad/17 10.7 ns 10.7 ns 65504000 BM_to_chars_bad/18 10.6 ns 10.6 ns 65685000 BM_to_chars_bad/19 9.98 ns 9.98 ns 69894000 BM_to_chars_bad/20 9.74 ns 9.74 ns 72098000 BM_to_chars_bad/21 9.25 ns 9.25 ns 75184000 BM_to_chars_bad/22 9.10 ns 9.10 ns 75602000 BM_to_chars_bad/23 9.48 ns 9.48 ns 72824000 BM_to_chars_bad/24 9.27 ns 9.27 ns 75112000 BM_to_chars_bad/25 9.61 ns 9.61 ns 72080000 BM_to_chars_bad/26 9.72 ns 9.72 ns 72178000 BM_to_chars_bad/27 10.0 ns 10.0 ns 69733000 BM_to_chars_bad/28 10.3 ns 10.3 ns 67409000 BM_to_chars_bad/29 9.97 ns 9.97 ns 69193000 BM_to_chars_bad/30 10.1 ns 10.1 ns 69007000 BM_to_chars_bad/31 9.68 ns 9.68 ns 72232000 BM_to_chars_bad/32 8.99 ns 8.99 ns 76825000 BM_to_chars_bad/33 8.82 ns 8.82 ns 79293000 BM_to_chars_bad/34 8.64 ns 8.64 ns 80441000 BM_to_chars_bad/35 8.96 ns 8.96 ns 75320000 BM_to_chars_bad/36 8.87 ns 8.87 ns 77293000 ``` After ``` -------------------------------------------------------------- Benchmark Time CPU Iterations -------------------------------------------------------------- BM_to_chars_good/2 14.7 ns 14.7 ns 47583000 BM_to_chars_good/3 101 ns 101 ns 6901000 BM_to_chars_good/4 68.4 ns 68.4 ns 10088000 BM_to_chars_good/5 58.2 ns 58.2 ns 12007000 BM_to_chars_good/6 51.1 ns 51.1 ns 13687000 BM_to_chars_good/7 45.6 ns 45.6 ns 15323000 BM_to_chars_good/8 14.6 ns 14.6 ns 47795000 BM_to_chars_good/9 40.7 ns 40.7 ns 17371000 BM_to_chars_good/10 7.48 ns 7.48 ns 90931000 BM_to_chars_good/11 37.6 ns 37.6 ns 18542000 BM_to_chars_good/12 35.2 ns 35.2 ns 19922000 BM_to_chars_good/13 34.9 ns 34.9 ns 20105000 BM_to_chars_good/14 33.5 ns 33.5 ns 20863000 BM_to_chars_good/15 31.9 ns 31.9 ns 22014000 BM_to_chars_good/16 11.7 ns 11.7 ns 60012000 BM_to_chars_good/17 28.9 ns 28.9 ns 24148000 BM_to_chars_good/18 29.0 ns 29.0 ns 24317000 BM_to_chars_good/19 28.7 ns 28.7 ns 24363000 BM_to_chars_good/20 28.1 ns 28.1 ns 24899000 BM_to_chars_good/21 27.5 ns 27.5 ns 25499000 BM_to_chars_good/22 26.9 ns 26.9 ns 25929000 BM_to_chars_good/23 26.2 ns 26.2 ns 26828000 BM_to_chars_good/24 25.1 ns 25.1 ns 27742000 BM_to_chars_good/25 25.3 ns 25.3 ns 27720000 BM_to_chars_good/26 25.2 ns 25.2 ns 27789000 BM_to_chars_good/27 25.3 ns 25.3 ns 27777000 BM_to_chars_good/28 25.3 ns 25.3 ns 27643000 BM_to_chars_good/29 25.3 ns 25.3 ns 27750000 BM_to_chars_good/30 25.4 ns 25.4 ns 27566000 BM_to_chars_good/31 25.4 ns 25.4 ns 27611000 BM_to_chars_good/32 25.8 ns 25.8 ns 27218000 BM_to_chars_good/33 25.7 ns 25.7 ns 27070000 BM_to_chars_good/34 26.1 ns 26.1 ns 26693000 BM_to_chars_good/35 26.4 ns 26.4 ns 26486000 BM_to_chars_good/36 26.3 ns 26.3 ns 26619000 BM_to_chars_bad/2 5.99 ns 5.99 ns 118787000 BM_to_chars_bad/3 14.3 ns 14.3 ns 48567000 BM_to_chars_bad/4 16.0 ns 16.0 ns 43239000 BM_to_chars_bad/5 12.6 ns 12.6 ns 55354000 BM_to_chars_bad/6 10.7 ns 10.7 ns 65491000 BM_to_chars_bad/7 14.4 ns 14.4 ns 48723000 BM_to_chars_bad/8 6.50 ns 6.50 ns 104967000 BM_to_chars_bad/9 12.0 ns 12.0 ns 56552000 BM_to_chars_bad/10 5.16 ns 5.16 ns 136380000 BM_to_chars_bad/11 10.5 ns 10.5 ns 66764000 BM_to_chars_bad/12 10.7 ns 10.7 ns 65534000 BM_to_chars_bad/13 11.0 ns 11.0 ns 63426000 BM_to_chars_bad/14 9.90 ns 9.90 ns 68575000 BM_to_chars_bad/15 9.52 ns 9.52 ns 70932000 BM_to_chars_bad/16 6.14 ns 6.14 ns 111762000 BM_to_chars_bad/17 10.6 ns 10.6 ns 65883000 BM_to_chars_bad/18 10.5 ns 10.5 ns 67606000 BM_to_chars_bad/19 9.96 ns 9.96 ns 68898000 BM_to_chars_bad/20 9.40 ns 9.41 ns 73116000 BM_to_chars_bad/21 9.12 ns 9.12 ns 78647000 BM_to_chars_bad/22 8.95 ns 8.95 ns 80211000 BM_to_chars_bad/23 9.50 ns 9.49 ns 73571000 BM_to_chars_bad/24 9.29 ns 9.29 ns 74690000 BM_to_chars_bad/25 9.65 ns 9.65 ns 72877000 BM_to_chars_bad/26 9.78 ns 9.78 ns 70171000 BM_to_chars_bad/27 10.1 ns 10.1 ns 69543000 BM_to_chars_bad/28 10.4 ns 10.4 ns 67582000 BM_to_chars_bad/29 10.00 ns 10.00 ns 70806000 BM_to_chars_bad/30 9.99 ns 9.99 ns 70340000 BM_to_chars_bad/31 9.56 ns 9.56 ns 74159000 BM_to_chars_bad/32 8.97 ns 8.97 ns 78052000 BM_to_chars_bad/33 8.86 ns 8.86 ns 78586000 BM_to_chars_bad/34 8.81 ns 8.81 ns 78562000 BM_to_chars_bad/35 8.90 ns 8.90 ns 77384000 BM_to_chars_bad/36 9.04 ns 9.04 ns 77263000 ``` Reviewed By: #libc, ldionne Differential Revision: https://reviews.llvm.org/D127764
2022-05-16 13:42:40 +08:00
return {__tx::__convert(__first, __value), errc(0)};
else
return {__last, errc::value_too_large};
}
# ifndef _LIBCPP_HAS_NO_INT128
template <>
inline _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<uint64_t>::max())
return __to_chars_itoa(__first, __last, static_cast<uint64_t>(__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
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI to_chars_result
__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
true_type)
{
auto __x = __to_unsigned_like(__value);
if (__value < 0 && __first != __last)
{
*__first++ = '-';
__x = __complement(__x);
}
return __to_chars_integral(__first, __last, __x, __base, false_type());
}
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
namespace __itoa {
template <unsigned _Base>
struct _LIBCPP_HIDDEN __integral;
template <>
struct _LIBCPP_HIDDEN __integral<2> {
template <typename _Tp>
_LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
// If value == 0 still need one digit. If the value != this has no
// effect since the code scans for the most significant bit set. (Note
// that __libcpp_clz doesn't work for 0.)
return numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1);
}
template <typename _Tp>
_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);
if (__n > __cap)
return {__last, errc::value_too_large};
__last = __first + __n;
char* __p = __last;
const unsigned __divisor = 16;
while (__value > __divisor) {
unsigned __c = __value % __divisor;
__value /= __divisor;
__p -= 4;
std::memcpy(__p, &__table<>::__base_2_lut[4 * __c], 4);
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
}
do {
unsigned __c = __value % 2;
__value /= 2;
*--__p = "01"[__c];
} while (__value != 0);
return {__last, errc(0)};
}
};
template <>
struct _LIBCPP_HIDDEN __integral<8> {
template <typename _Tp>
_LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
// If value == 0 still need one digit. If the value != this has no
// effect since the code scans for the most significat bit set. (Note
// that __libcpp_clz doesn't work for 0.)
return ((numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1)) + 2) / 3;
}
template <typename _Tp>
_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);
if (__n > __cap)
return {__last, errc::value_too_large};
__last = __first + __n;
char* __p = __last;
unsigned __divisor = 64;
while (__value > __divisor) {
unsigned __c = __value % __divisor;
__value /= __divisor;
__p -= 2;
std::memcpy(__p, &__table<>::__base_8_lut[2 * __c], 2);
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
}
do {
unsigned __c = __value % 8;
__value /= 8;
*--__p = "01234567"[__c];
} while (__value != 0);
return {__last, errc(0)};
}
};
template <>
struct _LIBCPP_HIDDEN __integral<16> {
template <typename _Tp>
_LIBCPP_HIDE_FROM_ABI static constexpr int __width(_Tp __value) noexcept {
// If value == 0 still need one digit. If the value != this has no
// effect since the code scans for the most significat bit set. (Note
// that __libcpp_clz doesn't work for 0.)
return (numeric_limits<_Tp>::digits - std::__libcpp_clz(__value | 1) + 3) / 4;
}
template <typename _Tp>
_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);
if (__n > __cap)
return {__last, errc::value_too_large};
__last = __first + __n;
char* __p = __last;
unsigned __divisor = 256;
while (__value > __divisor) {
unsigned __c = __value % __divisor;
__value /= __divisor;
__p -= 2;
std::memcpy(__p, &__table<>::__base_16_lut[2 * __c], 2);
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
}
if (__first != __last)
do {
unsigned __c = __value % 16;
__value /= 16;
*--__p = "0123456789abcdef"[__c];
} while (__value != 0);
return {__last, errc(0)};
}
};
} // namespace __itoa
template <unsigned _Base, typename _Tp,
typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
_LIBCPP_HIDE_FROM_ABI int
__to_chars_integral_width(_Tp __value) {
return __itoa::__integral<_Base>::__width(__value);
}
template <unsigned _Base, typename _Tp,
typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
_LIBCPP_HIDE_FROM_ABI int
__to_chars_integral_width(_Tp __value) {
return std::__to_chars_integral_width<_Base>(static_cast<unsigned>(__value));
}
template <unsigned _Base, typename _Tp,
typename enable_if<(sizeof(_Tp) >= sizeof(unsigned)), int>::type = 0>
_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 <unsigned _Base, typename _Tp,
typename enable_if<(sizeof(_Tp) < sizeof(unsigned)), int>::type = 0>
_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<unsigned>(__value));
}
template <typename _Tp>
_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;
unsigned __base_3 = __base_2 * __base;
unsigned __base_4 = __base_2 * __base_2;
int __r = 0;
while (true) {
if (__value < __base)
return __r + 1;
if (__value < __base_2)
return __r + 2;
if (__value < __base_3)
return __r + 3;
if (__value < __base_4)
return __r + 4;
__value /= __base_4;
__r += 4;
}
__libcpp_unreachable();
}
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI to_chars_result
__to_chars_integral(char* __first, char* __last, _Tp __value, int __base,
false_type)
{
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
if (__base == 10) [[likely]]
return __to_chars_itoa(__first, __last, __value, false_type());
[libc++] Improve std::to_chars for base != 10. This improves the speed of `to_chars` for bases 2, 8, and 16. These bases are common and used in `<format>`. This change uses a lookup table, like done in base 10 and causes an increase in code size. The change has a small overhead for the other bases. ``` Benchmark Time CPU Time Old Time New CPU Old CPU New ------------------------------------------------------------------------------------------------------------------ BM_to_chars_good/2 -0.9476 -0.9476 252 13 252 13 BM_to_chars_good/3 +0.0018 +0.0018 145 145 145 145 BM_to_chars_good/4 +0.0108 +0.0108 104 105 104 105 BM_to_chars_good/5 +0.0159 +0.0160 89 91 89 91 BM_to_chars_good/6 +0.0162 +0.0162 80 81 80 81 BM_to_chars_good/7 +0.0117 +0.0117 72 73 72 73 BM_to_chars_good/8 -0.8643 -0.8643 64 9 64 9 BM_to_chars_good/9 +0.0095 +0.0095 60 60 60 60 BM_to_chars_good/10 +0.0540 +0.0540 6 6 6 6 BM_to_chars_good/11 +0.0299 +0.0299 55 57 55 57 BM_to_chars_good/12 +0.0060 +0.0060 48 49 49 49 BM_to_chars_good/13 +0.0102 +0.0102 48 48 48 48 BM_to_chars_good/14 +0.0184 +0.0185 47 48 47 48 BM_to_chars_good/15 +0.0269 +0.0269 44 45 44 45 BM_to_chars_good/16 -0.8207 -0.8207 37 7 37 7 BM_to_chars_good/17 +0.0241 +0.0241 37 38 37 38 BM_to_chars_good/18 +0.0221 +0.0221 37 38 37 38 BM_to_chars_good/19 +0.0222 +0.0223 37 38 37 38 BM_to_chars_good/20 +0.0317 +0.0317 38 39 38 39 BM_to_chars_good/21 +0.0342 +0.0341 38 39 38 39 BM_to_chars_good/22 +0.0336 +0.0336 36 38 36 38 BM_to_chars_good/23 +0.0222 +0.0222 34 35 34 35 BM_to_chars_good/24 +0.0185 +0.0185 31 32 31 32 BM_to_chars_good/25 +0.0157 +0.0157 32 32 32 32 BM_to_chars_good/26 +0.0181 +0.0181 32 32 32 32 BM_to_chars_good/27 +0.0153 +0.0153 32 32 32 32 BM_to_chars_good/28 +0.0179 +0.0179 32 32 32 32 BM_to_chars_good/29 +0.0189 +0.0189 32 33 32 33 BM_to_chars_good/30 +0.0212 +0.0212 32 33 32 33 BM_to_chars_good/31 +0.0221 +0.0221 32 33 32 33 BM_to_chars_good/32 +0.0292 +0.0292 32 33 32 33 BM_to_chars_good/33 +0.0319 +0.0319 32 33 32 33 BM_to_chars_good/34 +0.0411 +0.0410 33 34 33 34 BM_to_chars_good/35 +0.0515 +0.0515 33 34 33 34 BM_to_chars_good/36 +0.0502 +0.0502 32 34 32 34 BM_to_chars_bad/2 -0.8752 -0.8752 40 5 40 5 BM_to_chars_bad/3 +0.1952 +0.1952 21 26 21 26 BM_to_chars_bad/4 +0.3626 +0.3626 16 22 16 22 BM_to_chars_bad/5 +0.2267 +0.2268 17 21 17 21 BM_to_chars_bad/6 +0.3560 +0.3559 14 19 14 19 BM_to_chars_bad/7 +0.4599 +0.4600 12 18 12 18 BM_to_chars_bad/8 -0.5074 -0.5074 11 5 11 5 BM_to_chars_bad/9 +0.4814 +0.4814 10 15 10 15 BM_to_chars_bad/10 +0.7761 +0.7761 2 4 2 4 BM_to_chars_bad/11 +0.3948 +0.3948 12 16 12 16 BM_to_chars_bad/12 +0.3203 +0.3203 10 13 10 13 BM_to_chars_bad/13 +0.3067 +0.3067 11 14 11 14 BM_to_chars_bad/14 +0.2235 +0.2235 12 14 12 14 BM_to_chars_bad/15 +0.2675 +0.2675 11 14 11 14 BM_to_chars_bad/16 -0.1801 -0.1801 7 5 7 5 BM_to_chars_bad/17 +0.5651 +0.5651 7 11 7 11 BM_to_chars_bad/18 +0.5407 +0.5406 7 11 7 11 BM_to_chars_bad/19 +0.5593 +0.5593 8 12 8 12 BM_to_chars_bad/20 +0.5823 +0.5823 8 13 8 13 BM_to_chars_bad/21 +0.6032 +0.6032 9 15 9 15 BM_to_chars_bad/22 +0.6407 +0.6408 9 14 9 14 BM_to_chars_bad/23 +0.6292 +0.6292 7 12 7 12 BM_to_chars_bad/24 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/25 +0.5784 +0.5784 6 10 6 10 BM_to_chars_bad/26 +0.5713 +0.5713 7 10 7 10 BM_to_chars_bad/27 +0.5969 +0.5969 7 11 7 11 BM_to_chars_bad/28 +0.6131 +0.6131 7 11 7 11 BM_to_chars_bad/29 +0.6937 +0.6937 7 11 7 11 BM_to_chars_bad/30 +0.7655 +0.7656 7 12 7 12 BM_to_chars_bad/31 +0.8939 +0.8939 6 12 6 12 BM_to_chars_bad/32 +1.0157 +1.0157 6 13 6 13 BM_to_chars_bad/33 +1.0279 +1.0279 7 14 7 14 BM_to_chars_bad/34 +1.0388 +1.0388 7 14 7 14 BM_to_chars_bad/35 +1.0990 +1.0990 7 15 7 15 BM_to_chars_bad/36 +1.1503 +1.1503 7 15 7 15 ``` Reviewed By: ldionne, #libc Differential Revision: https://reviews.llvm.org/D97705
2021-02-27 23:52:39 +08:00
switch (__base) {
case 2:
return __to_chars_integral<2>(__first, __last, __value);
case 8:
return __to_chars_integral<8>(__first, __last, __value);
case 16:
return __to_chars_integral<16>(__first, __last, __value);
}
ptrdiff_t __cap = __last - __first;
int __n = __to_chars_integral_width(__value, __base);
if (__n > __cap)
return {__last, errc::value_too_large};
__last = __first + __n;
char* __p = __last;
do {
unsigned __c = __value % __base;
__value /= __base;
*--__p = "0123456789abcdefghijklmnopqrstuvwxyz"[__c];
} while (__value != 0);
return {__last, errc(0)};
}
template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI to_chars_result
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>());
}
template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI to_chars_result
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>;
return std::__to_chars_integral(__first, __last, static_cast<_Type>(__value), __base, is_signed<_Tp>());
}
template <typename _It, typename _Tp, typename _Fn, typename... _Ts>
inline _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(__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 <= __complement(__to_unsigned_like(__tl::min())))
{
__x = __complement(__x);
std::memcpy(&__value, &__x, sizeof(__x));
return __r;
}
}
else
{
if (__x <= __to_unsigned_like(__tl::max()))
{
__value = __x;
return __r;
}
}
return {__r.ptr, errc::result_out_of_range};
}
template <typename _Tp>
inline _LIBCPP_HIDE_FROM_ABI bool
__in_pattern(_Tp __c)
{
return '0' <= __c && __c <= '9';
}
struct _LIBCPP_HIDDEN __in_pattern_result
{
bool __ok;
int __val;
explicit _LIBCPP_HIDE_FROM_ABI operator bool() const { return __ok; }
};
template <typename _Tp>
inline _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 (__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 <typename _It, typename _Tp, typename _Fn, typename... _Ts>
inline _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 || !__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 (!__in_pattern(*__r.ptr, __args...))
break;
}
}
return __r;
}
template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
inline _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 __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 || !__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 <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_atoi(const char* __first, const char* __last, _Tp& __value)
{
using __t = decltype(__to_unsigned_like(__value));
return __sign_combinator(__first, __last, __value, __from_chars_atoi<__t>);
}
template <typename _Tp, typename enable_if<is_unsigned<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
int __base)
{
if (__base == 10)
return __from_chars_atoi(__first, __last, __value);
return __subject_seq_combinator(
__first, __last, __value,
[](const char* __p, const char* __lastp, _Tp& __val,
int __b) -> from_chars_result {
using __tl = numeric_limits<_Tp>;
auto __digits = __tl::digits / log2f(float(__b));
_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;
}
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 <typename _Tp, typename enable_if<is_signed<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI from_chars_result
__from_chars_integral(const char* __first, const char* __last, _Tp& __value,
int __base)
{
using __t = decltype(__to_unsigned_like(__value));
return __sign_combinator(__first, __last, __value,
__from_chars_integral<__t>, __base);
}
template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _LIBCPP_HIDE_FROM_ABI from_chars_result
from_chars(const char* __first, const char* __last, _Tp& __value)
{
return __from_chars_atoi(__first, __last, __value);
}
template <typename _Tp, typename enable_if<is_integral<_Tp>::value, int>::type = 0>
inline _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 __from_chars_integral(__first, __last, __value, __base);
}
Microsoft's floating-point to_chars powered by Ryu and Ryu Printf Microsoft would like to contribute its implementation of floating-point to_chars to libc++. This uses the impossibly fast Ryu and Ryu Printf algorithms invented by Ulf Adams at Google. Upstream repos: https://github.com/microsoft/STL and https://github.com/ulfjack/ryu . Licensing notes: MSVC's STL is available under the Apache License v2.0 with LLVM Exception, intentionally chosen to match libc++. We've used Ryu under the Boost Software License. This patch contains minor changes from Jorg Brown at Google, to adapt the code to libc++. He verified that it works in Google's Linux-based environment, but then I applied more changes on top of his, so any compiler errors are my fault. (I haven't tried to build and test libc++ yet.) Please tell me if we need to do anything else in order to follow https://llvm.org/docs/DeveloperPolicy.html#attribution-of-changes . Notes: * libc++'s integer charconv is unchanged (except for a small refactoring). MSVC's integer charconv hasn't been tuned for performance yet, so you're not missing anything. * Floating-point from_chars isn't part of this patch because Jorg found that MSVC's implementation (derived from our CRT's strtod) was slower than Abseil's. If you're unable to use Abseil or another implementation due to licensing or technical considerations, Microsoft would be delighted if you used MSVC's from_chars (and you can just take it, or ask us to provide a patch like this). Ulf is also working on a novel algorithm for from_chars. * This assumes that float is IEEE 32-bit, double is IEEE 64-bit, and long double is also IEEE 64-bit. * I have added MSVC's charconv tests (the whole thing: integer/floating from_chars/to_chars), but haven't adapted them to libcxx's harness at all. (These tests will be available in the microsoft/STL repo soon.) * Jorg added int128 codepaths. These were originally present in upstream Ryu, and I removed them from microsoft/STL purely for performance reasons (MSVC doesn't support int128; Clang on Windows does, but I found that x64 intrinsics were slightly faster). * The implementation is split into 3 headers. In MSVC's STL, charconv contains only Microsoft-written code. xcharconv_ryu.h contains code derived from Ryu (with significant modifications and additions). xcharconv_ryu_tables.h contains Ryu's large lookup tables (they were sufficiently large to make editing inconvenient, hence the separate file). The xmeow.h convention is MSVC's for internal headers; you may wish to rename them. * You should consider separately compiling the lookup tables (see https://github.com/microsoft/STL/issues/172 ) for compiler throughput and reduced object file size. * See https://github.com/StephanTLavavej/llvm-project/commits/charconv for fine-grained history. (If necessary, I can perform some rebase surgery to show you what Jorg changed relative to the microsoft/STL repo; currently that's all fused into the first commit.) Differential Revision: https://reviews.llvm.org/D70631
2021-02-10 00:52:41 +08:00
// Floating-point implementation starts here.
// Unlike the other parts of charconv this is only available in C++17 and newer.
#if _LIBCPP_STD_VER > 14
_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, 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, 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, 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 > 14
#endif // _LIBCPP_CXX03_LANG
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP_CHARCONV