1// -*- C++ -*-
2//===----------------------------------------------------------------------===//
3//
4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP___CHARCONV_TRAITS
11#define _LIBCPP___CHARCONV_TRAITS
12
13#include <__assert>
14#include <__bit/countl.h>
15#include <__charconv/tables.h>
16#include <__charconv/to_chars_base_10.h>
17#include <__config>
18#include <__memory/addressof.h>
19#include <__type_traits/enable_if.h>
20#include <__type_traits/is_unsigned.h>
21#include <cstdint>
22#include <limits>
23
24#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25# pragma GCC system_header
26#endif
27
28_LIBCPP_PUSH_MACROS
29#include <__undef_macros>
30
31_LIBCPP_BEGIN_NAMESPACE_STD
32
33namespace __itoa {
34
35template <typename _Tp, typename = void>
36struct _LIBCPP_HIDDEN __traits_base;
37
38template <typename _Tp>
39struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)> > {
40 using type = uint32_t;
41
42 /// The width estimation using a log10 algorithm.
43 ///
44 /// The algorithm is based on
45 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
46 /// Instead of using IntegerLogBase2 it uses __countl_zero.
47 static _LIBCPP_CONSTEXPR_SINCE_CXX23 int __width(_Tp __v) {
48 auto __t = (32 - std::__countl_zero(t: static_cast<type>(__v | 1))) * 1233 >> 12;
49 return __t - (__v < __itoa::__pow10_32[__t]) + 1;
50 }
51
52 static _LIBCPP_CONSTEXPR_SINCE_CXX23 char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u32(first: __p, value: __v); }
53
54 static _LIBCPP_CONSTEXPR_SINCE_CXX23 decltype(__pow10_32)& __pow() { return __itoa::__pow10_32; }
55};
56
57template <typename _Tp>
58struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)> > {
59 using type = uint64_t;
60
61 /// The width estimation using a log10 algorithm.
62 ///
63 /// The algorithm is based on
64 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
65 /// Instead of using IntegerLogBase2 it uses __countl_zero.
66 static _LIBCPP_CONSTEXPR_SINCE_CXX23 int __width(_Tp __v) {
67 auto __t = (64 - std::__countl_zero(t: static_cast<type>(__v | 1))) * 1233 >> 12;
68 return __t - (__v < __itoa::__pow10_64[__t]) + 1;
69 }
70
71 static _LIBCPP_CONSTEXPR_SINCE_CXX23 char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u64(buffer: __p, value: __v); }
72
73 static _LIBCPP_CONSTEXPR_SINCE_CXX23 decltype(__pow10_64)& __pow() { return __itoa::__pow10_64; }
74};
75
76#if _LIBCPP_HAS_INT128
77template <typename _Tp>
78struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > {
79 using type = __uint128_t;
80
81 /// The width estimation using a log10 algorithm.
82 ///
83 /// The algorithm is based on
84 /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
85 /// Instead of using IntegerLogBase2 it uses __countl_zero.
86 static _LIBCPP_CONSTEXPR_SINCE_CXX23 int __width(_Tp __v) {
87 _LIBCPP_ASSERT_INTERNAL(
88 __v > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true.");
89 // There's always a bit set in the upper 64-bits.
90 auto __t = (128 - std::__countl_zero(t: static_cast<uint64_t>(__v >> 64))) * 1233 >> 12;
91 _LIBCPP_ASSERT_INTERNAL(__t >= __itoa::__pow10_128_offset, "Index out of bounds");
92 // __t is adjusted since the lookup table misses the lower entries.
93 return __t - (__v < __itoa::__pow10_128[__t - __itoa::__pow10_128_offset]) + 1;
94 }
95
96 static _LIBCPP_CONSTEXPR_SINCE_CXX23 char* __convert(char* __p, _Tp __v) { return __itoa::__base_10_u128(buffer: __p, value: __v); }
97
98 // TODO FMT This pow function should get an index.
99 // By moving this to its own header it can be reused by the pow function in to_chars_base_10.
100 static _LIBCPP_CONSTEXPR_SINCE_CXX23 decltype(__pow10_128)& __pow() { return __itoa::__pow10_128; }
101};
102#endif
103
104template <typename _Tp, typename _Up>
105bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) {
106 static_assert(is_unsigned<_Tp>::value);
107 return __builtin_mul_overflow(__a, static_cast<_Tp>(__b), std::addressof(__r));
108}
109
110template <typename _Tp>
111struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> {
112 static _LIBCPP_CONSTEXPR const int digits = numeric_limits<_Tp>::digits10 + 1;
113 using __traits_base<_Tp>::__pow;
114 using typename __traits_base<_Tp>::type;
115
116 // precondition: at least one non-zero character available
117 static _LIBCPP_CONSTEXPR_SINCE_CXX23 char const* __read(char const* __p, char const* __ep, type& __a, type& __b) {
118 type __cprod[digits];
119 int __j = digits - 1;
120 int __i = digits;
121 do {
122 if (*__p < '0' || *__p > '9')
123 break;
124 __cprod[--__i] = *__p++ - '0';
125 } while (__p != __ep && __i != 0);
126
127 __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, __cprod[__i]);
128 if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
129 --__p;
130 return __p;
131 }
132
133 template <typename _It1, typename _It2, class _Up>
134 static _LIBCPP_CONSTEXPR_SINCE_CXX23 _Up __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) {
135 for (; __first1 < __last1; ++__first1, ++__first2)
136 __init = __init + *__first1 * *__first2;
137 return __init;
138 }
139};
140
141} // namespace __itoa
142
143template <typename _Tp>
144inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_DISABLE_UBSAN_UNSIGNED_INTEGER_CHECK _Tp __complement(_Tp __x) {
145 static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
146 return _Tp(~__x + 1);
147}
148
149_LIBCPP_END_NAMESPACE_STD
150
151_LIBCPP_POP_MACROS
152
153#endif // _LIBCPP___CHARCONV_TRAITS
154