numbers.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // Copyright 2017 The Abseil Authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // https://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // -----------------------------------------------------------------------------
  17. // File: numbers.h
  18. // -----------------------------------------------------------------------------
  19. //
  20. // This package contains functions for converting strings to numbers. For
  21. // converting numbers to strings, use `StrCat()` or `StrAppend()` in str_cat.h,
  22. // which automatically detect and convert most number values appropriately.
  23. #ifndef ABSL_STRINGS_NUMBERS_H_
  24. #define ABSL_STRINGS_NUMBERS_H_
  25. #ifdef __SSE4_2__
  26. #include <x86intrin.h>
  27. #endif
  28. #include <cstddef>
  29. #include <cstdlib>
  30. #include <cstring>
  31. #include <ctime>
  32. #include <limits>
  33. #include <string>
  34. #include <type_traits>
  35. #include "absl/base/config.h"
  36. #include "absl/base/internal/bits.h"
  37. #ifdef __SSE4_2__
  38. // TODO(jorg): Remove this when we figure out the right way
  39. // to swap bytes on SSE 4.2 that works with the compilers
  40. // we claim to support. Also, add tests for the compiler
  41. // that doesn't support the Intel _bswap64 intrinsic but
  42. // does support all the SSE 4.2 intrinsics
  43. #include "absl/base/internal/endian.h"
  44. #endif
  45. #include "absl/base/macros.h"
  46. #include "absl/base/port.h"
  47. #include "absl/numeric/int128.h"
  48. #include "absl/strings/string_view.h"
  49. namespace absl {
  50. ABSL_NAMESPACE_BEGIN
  51. // SimpleAtoi()
  52. //
  53. // Converts the given string (optionally followed or preceded by ASCII
  54. // whitespace) into an integer value, returning `true` if successful. The string
  55. // must reflect a base-10 integer whose value falls within the range of the
  56. // integer type (optionally preceded by a `+` or `-`). If any errors are
  57. // encountered, this function returns `false`, leaving `out` in an unspecified
  58. // state.
  59. template <typename int_type>
  60. ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out);
  61. // SimpleAtof()
  62. //
  63. // Converts the given string (optionally followed or preceded by ASCII
  64. // whitespace) into a float, which may be rounded on overflow or underflow,
  65. // returning `true` if successful.
  66. // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
  67. // allowed formats for `str`, except SimpleAtof() is locale-independent and will
  68. // always use the "C" locale. If any errors are encountered, this function
  69. // returns `false`, leaving `out` in an unspecified state.
  70. ABSL_MUST_USE_RESULT bool SimpleAtof(absl::string_view str, float* out);
  71. // SimpleAtod()
  72. //
  73. // Converts the given string (optionally followed or preceded by ASCII
  74. // whitespace) into a double, which may be rounded on overflow or underflow,
  75. // returning `true` if successful.
  76. // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
  77. // allowed formats for `str`, except SimpleAtod is locale-independent and will
  78. // always use the "C" locale. If any errors are encountered, this function
  79. // returns `false`, leaving `out` in an unspecified state.
  80. ABSL_MUST_USE_RESULT bool SimpleAtod(absl::string_view str, double* out);
  81. // SimpleAtob()
  82. //
  83. // Converts the given string into a boolean, returning `true` if successful.
  84. // The following case-insensitive strings are interpreted as boolean `true`:
  85. // "true", "t", "yes", "y", "1". The following case-insensitive strings
  86. // are interpreted as boolean `false`: "false", "f", "no", "n", "0". If any
  87. // errors are encountered, this function returns `false`, leaving `out` in an
  88. // unspecified state.
  89. ABSL_MUST_USE_RESULT bool SimpleAtob(absl::string_view str, bool* out);
  90. ABSL_NAMESPACE_END
  91. } // namespace absl
  92. // End of public API. Implementation details follow.
  93. namespace absl {
  94. ABSL_NAMESPACE_BEGIN
  95. namespace numbers_internal {
  96. // Digit conversion.
  97. ABSL_DLL extern const char kHexChar[17]; // 0123456789abcdef
  98. ABSL_DLL extern const char
  99. kHexTable[513]; // 000102030405060708090a0b0c0d0e0f1011...
  100. ABSL_DLL extern const char
  101. two_ASCII_digits[100][2]; // 00, 01, 02, 03...
  102. // Writes a two-character representation of 'i' to 'buf'. 'i' must be in the
  103. // range 0 <= i < 100, and buf must have space for two characters. Example:
  104. // char buf[2];
  105. // PutTwoDigits(42, buf);
  106. // // buf[0] == '4'
  107. // // buf[1] == '2'
  108. inline void PutTwoDigits(size_t i, char* buf) {
  109. assert(i < 100);
  110. memcpy(buf, two_ASCII_digits[i], 2);
  111. }
  112. // safe_strto?() functions for implementing SimpleAtoi()
  113. bool safe_strto32_base(absl::string_view text, int32_t* value, int base);
  114. bool safe_strto64_base(absl::string_view text, int64_t* value, int base);
  115. bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base);
  116. bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base);
  117. bool safe_strtou128_base(absl::string_view text, absl::uint128* value,
  118. int base);
  119. static const int kFastToBufferSize = 32;
  120. static const int kSixDigitsToBufferSize = 16;
  121. // Helper function for fast formatting of floating-point values.
  122. // The result is the same as printf's "%g", a.k.a. "%.6g"; that is, six
  123. // significant digits are returned, trailing zeros are removed, and numbers
  124. // outside the range 0.0001-999999 are output using scientific notation
  125. // (1.23456e+06). This routine is heavily optimized.
  126. // Required buffer size is `kSixDigitsToBufferSize`.
  127. size_t SixDigitsToBuffer(double d, char* buffer);
  128. // These functions are intended for speed. All functions take an output buffer
  129. // as an argument and return a pointer to the last byte they wrote, which is the
  130. // terminating '\0'. At most `kFastToBufferSize` bytes are written.
  131. char* FastIntToBuffer(int32_t, char*);
  132. char* FastIntToBuffer(uint32_t, char*);
  133. char* FastIntToBuffer(int64_t, char*);
  134. char* FastIntToBuffer(uint64_t, char*);
  135. // For enums and integer types that are not an exact match for the types above,
  136. // use templates to call the appropriate one of the four overloads above.
  137. template <typename int_type>
  138. char* FastIntToBuffer(int_type i, char* buffer) {
  139. static_assert(sizeof(i) <= 64 / 8,
  140. "FastIntToBuffer works only with 64-bit-or-less integers.");
  141. // TODO(jorg): This signed-ness check is used because it works correctly
  142. // with enums, and it also serves to check that int_type is not a pointer.
  143. // If one day something like std::is_signed<enum E> works, switch to it.
  144. if (static_cast<int_type>(1) - 2 < 0) { // Signed
  145. if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit
  146. return FastIntToBuffer(static_cast<int64_t>(i), buffer);
  147. } else { // 32-bit or less
  148. return FastIntToBuffer(static_cast<int32_t>(i), buffer);
  149. }
  150. } else { // Unsigned
  151. if (sizeof(i) > 32 / 8) { // 33-bit to 64-bit
  152. return FastIntToBuffer(static_cast<uint64_t>(i), buffer);
  153. } else { // 32-bit or less
  154. return FastIntToBuffer(static_cast<uint32_t>(i), buffer);
  155. }
  156. }
  157. }
  158. // Implementation of SimpleAtoi, generalized to support arbitrary base (used
  159. // with base different from 10 elsewhere in Abseil implementation).
  160. template <typename int_type>
  161. ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s, int_type* out,
  162. int base) {
  163. static_assert(sizeof(*out) == 4 || sizeof(*out) == 8,
  164. "SimpleAtoi works only with 32-bit or 64-bit integers.");
  165. static_assert(!std::is_floating_point<int_type>::value,
  166. "Use SimpleAtof or SimpleAtod instead.");
  167. bool parsed;
  168. // TODO(jorg): This signed-ness check is used because it works correctly
  169. // with enums, and it also serves to check that int_type is not a pointer.
  170. // If one day something like std::is_signed<enum E> works, switch to it.
  171. if (static_cast<int_type>(1) - 2 < 0) { // Signed
  172. if (sizeof(*out) == 64 / 8) { // 64-bit
  173. int64_t val;
  174. parsed = numbers_internal::safe_strto64_base(s, &val, base);
  175. *out = static_cast<int_type>(val);
  176. } else { // 32-bit
  177. int32_t val;
  178. parsed = numbers_internal::safe_strto32_base(s, &val, base);
  179. *out = static_cast<int_type>(val);
  180. }
  181. } else { // Unsigned
  182. if (sizeof(*out) == 64 / 8) { // 64-bit
  183. uint64_t val;
  184. parsed = numbers_internal::safe_strtou64_base(s, &val, base);
  185. *out = static_cast<int_type>(val);
  186. } else { // 32-bit
  187. uint32_t val;
  188. parsed = numbers_internal::safe_strtou32_base(s, &val, base);
  189. *out = static_cast<int_type>(val);
  190. }
  191. }
  192. return parsed;
  193. }
  194. // FastHexToBufferZeroPad16()
  195. //
  196. // Outputs `val` into `out` as if by `snprintf(out, 17, "%016x", val)` but
  197. // without the terminating null character. Thus `out` must be of length >= 16.
  198. // Returns the number of non-pad digits of the output (it can never be zero
  199. // since 0 has one digit).
  200. inline size_t FastHexToBufferZeroPad16(uint64_t val, char* out) {
  201. #ifdef __SSE4_2__
  202. uint64_t be = absl::big_endian::FromHost64(val);
  203. const auto kNibbleMask = _mm_set1_epi8(0xf);
  204. const auto kHexDigits = _mm_setr_epi8('0', '1', '2', '3', '4', '5', '6', '7',
  205. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
  206. auto v = _mm_loadl_epi64(reinterpret_cast<__m128i*>(&be)); // load lo dword
  207. auto v4 = _mm_srli_epi64(v, 4); // shift 4 right
  208. auto il = _mm_unpacklo_epi8(v4, v); // interleave bytes
  209. auto m = _mm_and_si128(il, kNibbleMask); // mask out nibbles
  210. auto hexchars = _mm_shuffle_epi8(kHexDigits, m); // hex chars
  211. _mm_storeu_si128(reinterpret_cast<__m128i*>(out), hexchars);
  212. #else
  213. for (int i = 0; i < 8; ++i) {
  214. auto byte = (val >> (56 - 8 * i)) & 0xFF;
  215. auto* hex = &absl::numbers_internal::kHexTable[byte * 2];
  216. std::memcpy(out + 2 * i, hex, 2);
  217. }
  218. #endif
  219. // | 0x1 so that even 0 has 1 digit.
  220. return 16 - absl::base_internal::CountLeadingZeros64(val | 0x1) / 4;
  221. }
  222. } // namespace numbers_internal
  223. // SimpleAtoi()
  224. //
  225. // Converts a string to an integer, using `safe_strto?()` functions for actual
  226. // parsing, returning `true` if successful. The `safe_strto?()` functions apply
  227. // strict checking; the string must be a base-10 integer, optionally followed or
  228. // preceded by ASCII whitespace, with a value in the range of the corresponding
  229. // integer type.
  230. template <typename int_type>
  231. ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type* out) {
  232. return numbers_internal::safe_strtoi_base(str, out, 10);
  233. }
  234. ABSL_MUST_USE_RESULT inline bool SimpleAtoi(absl::string_view str,
  235. absl::uint128* out) {
  236. return numbers_internal::safe_strtou128_base(str, out, 10);
  237. }
  238. ABSL_NAMESPACE_END
  239. } // namespace absl
  240. #endif // ABSL_STRINGS_NUMBERS_H_