traits.h 7.0 KB

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