traits.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #ifndef _LIBCPP___CHARCONV_TRAITS
  10. #define _LIBCPP___CHARCONV_TRAITS
  11. #include <__assert>
  12. #include <__bit/countl.h>
  13. #include <__charconv/tables.h>
  14. #include <__charconv/to_chars_base_10.h>
  15. #include <__config>
  16. #include <__type_traits/enable_if.h>
  17. #include <__type_traits/is_unsigned.h>
  18. #include <cstdint>
  19. #include <limits>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_PUSH_MACROS
  24. #include <__undef_macros>
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. #if _LIBCPP_STD_VER >= 17
  27. namespace __itoa {
  28. template <typename _Tp, typename = void>
  29. struct _LIBCPP_HIDDEN __traits_base;
  30. template <typename _Tp>
  31. struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) <= sizeof(uint32_t)>> {
  32. using type = uint32_t;
  33. /// The width estimation using a log10 algorithm.
  34. ///
  35. /// The algorithm is based on
  36. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  37. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  38. /// function requires its input to have at least one bit set the value of
  39. /// zero is set to one. This means the first element of the lookup table is
  40. /// zero.
  41. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {
  42. auto __t = (32 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
  43. return __t - (__v < __itoa::__pow10_32[__t]) + 1;
  44. }
  45. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {
  46. return __itoa::__base_10_u32(__p, __v);
  47. }
  48. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_32)& __pow() {
  49. return __itoa::__pow10_32;
  50. }
  51. };
  52. template <typename _Tp>
  53. struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(uint64_t)>> {
  54. using type = uint64_t;
  55. /// The width estimation using a log10 algorithm.
  56. ///
  57. /// The algorithm is based on
  58. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  59. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  60. /// function requires its input to have at least one bit set the value of
  61. /// zero is set to one. This means the first element of the lookup table is
  62. /// zero.
  63. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI int __width(_Tp __v) {
  64. auto __t = (64 - std::__libcpp_clz(static_cast<type>(__v | 1))) * 1233 >> 12;
  65. return __t - (__v < __itoa::__pow10_64[__t]) + 1;
  66. }
  67. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {
  68. return __itoa::__base_10_u64(__p, __v);
  69. }
  70. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_64)& __pow() {
  71. return __itoa::__pow10_64;
  72. }
  73. };
  74. # ifndef _LIBCPP_HAS_NO_INT128
  75. template <typename _Tp>
  76. struct _LIBCPP_HIDDEN __traits_base<_Tp, __enable_if_t<sizeof(_Tp) == sizeof(__uint128_t)> > {
  77. using type = __uint128_t;
  78. /// The width estimation using a log10 algorithm.
  79. ///
  80. /// The algorithm is based on
  81. /// http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10
  82. /// Instead of using IntegerLogBase2 it uses __libcpp_clz. Since that
  83. /// function requires its input to have at least one bit set the value of
  84. /// zero is set to one. This means the first element of the lookup table is
  85. /// zero.
  86. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI 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::__libcpp_clz(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. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char* __convert(char* __p, _Tp __v) {
  96. return __itoa::__base_10_u128(__p, __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 _LIBCPP_HIDE_FROM_ABI decltype(__pow10_128)& __pow() {
  101. return __itoa::__pow10_128;
  102. }
  103. };
  104. # endif
  105. template <typename _Tp>
  106. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  107. __mul_overflowed(unsigned char __a, _Tp __b, unsigned char& __r) {
  108. auto __c = __a * __b;
  109. __r = __c;
  110. return __c > numeric_limits<unsigned char>::max();
  111. }
  112. template <typename _Tp>
  113. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool
  114. __mul_overflowed(unsigned short __a, _Tp __b, unsigned short& __r) {
  115. auto __c = __a * __b;
  116. __r = __c;
  117. return __c > numeric_limits<unsigned short>::max();
  118. }
  119. template <typename _Tp>
  120. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI bool __mul_overflowed(_Tp __a, _Tp __b, _Tp& __r) {
  121. static_assert(is_unsigned<_Tp>::value, "");
  122. return __builtin_mul_overflow(__a, __b, &__r);
  123. }
  124. template <typename _Tp, typename _Up>
  125. inline _LIBCPP_HIDE_FROM_ABI bool _LIBCPP_CONSTEXPR_SINCE_CXX23 __mul_overflowed(_Tp __a, _Up __b, _Tp& __r) {
  126. return __itoa::__mul_overflowed(__a, static_cast<_Tp>(__b), __r);
  127. }
  128. template <typename _Tp>
  129. struct _LIBCPP_HIDDEN __traits : __traits_base<_Tp> {
  130. static constexpr int digits = numeric_limits<_Tp>::digits10 + 1;
  131. using __traits_base<_Tp>::__pow;
  132. using typename __traits_base<_Tp>::type;
  133. // precondition: at least one non-zero character available
  134. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI char const*
  135. __read(char const* __p, char const* __ep, type& __a, type& __b) {
  136. type __cprod[digits];
  137. int __j = digits - 1;
  138. int __i = digits;
  139. do {
  140. if (*__p < '0' || *__p > '9')
  141. break;
  142. __cprod[--__i] = *__p++ - '0';
  143. } while (__p != __ep && __i != 0);
  144. __a = __inner_product(__cprod + __i + 1, __cprod + __j, __pow() + 1, __cprod[__i]);
  145. if (__itoa::__mul_overflowed(__cprod[__j], __pow()[__j - __i], __b))
  146. --__p;
  147. return __p;
  148. }
  149. template <typename _It1, typename _It2, class _Up>
  150. static _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Up
  151. __inner_product(_It1 __first1, _It1 __last1, _It2 __first2, _Up __init) {
  152. for (; __first1 < __last1; ++__first1, ++__first2)
  153. __init = __init + *__first1 * *__first2;
  154. return __init;
  155. }
  156. };
  157. } // namespace __itoa
  158. template <typename _Tp>
  159. inline _LIBCPP_CONSTEXPR_SINCE_CXX23 _LIBCPP_HIDE_FROM_ABI _Tp __complement(_Tp __x) {
  160. static_assert(is_unsigned<_Tp>::value, "cast to unsigned first");
  161. return _Tp(~__x + 1);
  162. }
  163. #endif // _LIBCPP_STD_VER >= 17
  164. _LIBCPP_END_NAMESPACE_STD
  165. _LIBCPP_POP_MACROS
  166. #endif // _LIBCPP___CHARCONV_TRAITS