Path: blob/main/contrib/llvm-project/libcxx/include/__charconv/traits.h
35262 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___CHARCONV_TRAITS10#define _LIBCPP___CHARCONV_TRAITS1112#include <__assert>13#include <__bit/countl.h>14#include <__charconv/tables.h>15#include <__charconv/to_chars_base_10.h>16#include <__config>17#include <__type_traits/enable_if.h>18#include <__type_traits/is_unsigned.h>19#include <cstdint>20#include <limits>2122#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)23# pragma GCC system_header24#endif2526_LIBCPP_PUSH_MACROS27#include <__undef_macros>2829_LIBCPP_BEGIN_NAMESPACE_STD3031#if _LIBCPP_STD_VER >= 173233namespace __itoa {3435template <typename _Tp, typename = void>36struct _LIBCPP_HIDDEN __traits_base;3738template <typename _Tp>39struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)>> {40using type = uint32_t;4142/// The width estimation using a log10 algorithm.43///44/// The algorithm is based on45/// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog1046/// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that47/// function requires its input to have at least one bit set the value of48/// zero is set to one. This means the first element of the lookup table is49/// zero.50static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {51auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;52return __t - (__v < __itoa::__pow10_32[__t]) + 1;53}5455static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {56return __itoa::__base_10_u32(__p, __v);57}5859static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() {60return __itoa::__pow10_32;61}62};6364template <typename _Tp>65struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> {66using type = uint64_t;6768/// The width estimation using a log10 algorithm.69///70/// The algorithm is based on71/// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog1072/// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that73/// function requires its input to have at least one bit set the value of74/// zero is set to one. This means the first element of the lookup table is75/// zero.76static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {77auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;78return __t - (__v < __itoa::__pow10_64[__t]) + 1;79}8081static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {82return __itoa::__base_10_u64(__p, __v);83}8485static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() {86return __itoa::__pow10_64;87}88};8990# ifndef _LIBCPP_HAS_NO_INT12891template <typename _Tp>92struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > {93using type = __uint128_t;9495/// The width estimation using a log10 algorithm.96///97/// The algorithm is based on98/// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog1099/// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that100/// function requires its input to have at least one bit set the value of101/// zero is set to one. This means the first element of the lookup table is102/// zero.103static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {104_LIBCPP_ASSERT_INTERNAL(105__v > numeric_limits<uint64_t>::max(), "The optimizations for this algorithm fail when this isn't true.");106// There's always a bit set in the upper 64-bits.107auto __t = (128 - std::__libcpp_clz(static_cast<uint64_t>(__v >> 64))) * 1233 >> 12;108_LIBCPP_ASSERT_INTERNAL(__t >= __itoa::__pow10_128_offset, "Index out of bounds");109// __t is adjusted since the lookup table misses the lower entries.110return __t - (__v < __itoa::__pow10_128[__t - __itoa::__pow10_128_offset]) + 1;111}112113static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {114return __itoa::__base_10_u128(__p, __v);115}116117// TODO FMT This pow function should get an index.118// By moving this to its own header it can be reused by the pow function in to_chars_base_10.119static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() {120return __itoa::__pow10_128;121}122};123# endif124125template <typename _Tp>126inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool127__mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) {128auto __c = __a * __b;129__r = __c;130return __c > numeric_limits<unsigned char>::max();131}132133template <typename _Tp>134inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool135__mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) {136auto __c = __a * __b;137__r = __c;138return __c > numeric_limits<unsigned short>::max();139}140141template <typename _Tp>142inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) {143static_assert(is_unsigned<_Tp>::value, "");144return __builtin_mul_overflow(__a, __b, &__r);145}146147template <typename _Tp, typename _Up>148inline _LIBCPP_HIDE_FROM_ABI bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) {149return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r);150}151152template <typename _Tp>153struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> {154static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;155using __traits_base<_Tp>::__pow;156using typename __traits_base<_Tp>::type;157158// precondition: at least one non-zero character available159static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const*160__read(char const* __p, char const* __ep, type& __a, type& __b) {161type __cprod[digits];162int __j = digits - 1;163int __i = digits;164do {165if (*__p < '0' || *__p > '9')166break;167__cprod[--__i] = *__p++ - '0';168} while (__p != __ep && __i != 0);169170__a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, __cprod[__i]);171if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))172--__p;173return __p;174}175176template <typename _It1, typename _It2, class _Up>177static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up178__inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) {179for (; __first1 < __last1; ++__first1, ++__first2)180__init = __init + *__first1 * *__first2;181return __init;182}183};184185} // namespace __itoa186187template <typename _Tp>188inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp __complement(_Tp __x) {189static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");190return _Tp(~__x + 1);191}192193#endif // _LIBCPP_STD_VER >= 17194195_LIBCPP_END_NAMESPACE_STD196197_LIBCPP_POP_MACROS198199#endif // _LIBCPP___CHARCONV_TRAITS200201202