| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | // This file contains string processing functions related to |
| 16 | // numeric values. |
| 17 |
| 18 | #include "absl/strings/numbers.h" |
| 19 |
| 20 | #include <algorithm> |
| 21 | #include <array> |
| 22 | #include <cassert> |
| 23 | #include <cfloat> // for DBL_DIG and FLT_DIG |
| 24 | #include <cmath> // for HUGE_VAL |
| 25 | #include <cstdint> |
| 26 | #include <cstdio> |
| 27 | #include <cstdlib> |
| 28 | #include <cstring> |
| 29 | #include <iterator> |
| 30 | #include <limits> |
| 31 | #include <system_error> // NOLINT(build/c++11) |
| 32 | #include <utility> |
| 33 |
| 34 | #include "absl/base/attributes.h" |
| 35 | #include "absl/base/config.h" |
| 36 | #include "absl/base/internal/endian.h" |
| 37 | #include "absl/base/internal/raw_logging.h" |
| 38 | #include "absl/base/nullability.h" |
| 39 | #include "absl/base/optimization.h" |
| 40 | #include "absl/numeric/bits.h" |
| 41 | #include "absl/numeric/int128.h" |
| 42 | #include "absl/strings/ascii.h" |
| 43 | #include "absl/strings/charconv.h" |
| 44 | #include "absl/strings/match.h" |
| 45 | #include "absl/strings/string_view.h" |
| 46 |
| 47 | namespace absl { |
| 48 | ABSL_NAMESPACE_BEGIN |
| 49 |
| 50 | bool SimpleAtof(absl::string_view str, absl::Nonnull<float*> out) { |
| 51 | *out = 0.0; |
| 52 | str = StripAsciiWhitespace(str); |
| 53 | // std::from_chars doesn't accept an initial +, but SimpleAtof does, so if one |
| 54 | // is present, skip it, while avoiding accepting "+-0" as valid. |
| 55 | if (!str.empty() && str[0] == '+') { |
| 56 | str.remove_prefix(1); |
| 57 | if (!str.empty() && str[0] == '-') { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | auto result = absl::from_chars(str.data(), str.data() + str.size(), *out); |
| 62 | if (result.ec == std::errc::invalid_argument) { |
| 63 | return false; |
| 64 | } |
| 65 | if (result.ptr != str.data() + str.size()) { |
| 66 | // not all non-whitespace characters consumed |
| 67 | return false; |
| 68 | } |
| 69 | // from_chars() with DR 3081's current wording will return max() on |
| 70 | // overflow. SimpleAtof returns infinity instead. |
| 71 | if (result.ec == std::errc::result_out_of_range) { |
| 72 | if (*out > 1.0) { |
| 73 | *out = std::numeric_limits<float>::infinity(); |
| 74 | } else if (*out < -1.0) { |
| 75 | *out = -std::numeric_limits<float>::infinity(); |
| 76 | } |
| 77 | } |
| 78 | return true; |
| 79 | } |
| 80 |
| 81 | bool SimpleAtod(absl::string_view str, absl::Nonnull<double*> out) { |
| 82 | *out = 0.0; |
| 83 | str = StripAsciiWhitespace(str); |
| 84 | // std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one |
| 85 | // is present, skip it, while avoiding accepting "+-0" as valid. |
| 86 | if (!str.empty() && str[0] == '+') { |
| 87 | str.remove_prefix(1); |
| 88 | if (!str.empty() && str[0] == '-') { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | auto result = absl::from_chars(str.data(), str.data() + str.size(), *out); |
| 93 | if (result.ec == std::errc::invalid_argument) { |
| 94 | return false; |
| 95 | } |
| 96 | if (result.ptr != str.data() + str.size()) { |
| 97 | // not all non-whitespace characters consumed |
| 98 | return false; |
| 99 | } |
| 100 | // from_chars() with DR 3081's current wording will return max() on |
| 101 | // overflow. SimpleAtod returns infinity instead. |
| 102 | if (result.ec == std::errc::result_out_of_range) { |
| 103 | if (*out > 1.0) { |
| 104 | *out = std::numeric_limits<double>::infinity(); |
| 105 | } else if (*out < -1.0) { |
| 106 | *out = -std::numeric_limits<double>::infinity(); |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 |
| 112 | bool SimpleAtob(absl::string_view str, absl::Nonnull<bool*> out) { |
| 113 | ABSL_RAW_CHECK(out != nullptr, "Output pointer must not be nullptr."); |
| 114 | if (EqualsIgnoreCase(str, "true") || EqualsIgnoreCase(str, "t") || |
| 115 | EqualsIgnoreCase(str, "yes") || EqualsIgnoreCase(str, "y") || |
| 116 | EqualsIgnoreCase(str, "1")) { |
| 117 | *out = true; |
| 118 | return true; |
| 119 | } |
| 120 | if (EqualsIgnoreCase(str, "false") || EqualsIgnoreCase(str, "f") || |
| 121 | EqualsIgnoreCase(str, "no") || EqualsIgnoreCase(str, "n") || |
| 122 | EqualsIgnoreCase(str, "0")) { |
| 123 | *out = false; |
| 124 | return true; |
| 125 | } |
| 126 | return false; |
| 127 | } |
| 128 |
| 129 | // ---------------------------------------------------------------------- |
| 130 | // FastIntToBuffer() overloads |
| 131 | // |
| 132 | // Like the Fast*ToBuffer() functions above, these are intended for speed. |
| 133 | // Unlike the Fast*ToBuffer() functions, however, these functions write |
| 134 | // their output to the beginning of the buffer. The caller is responsible |
| 135 | // for ensuring that the buffer has enough space to hold the output. |
| 136 | // |
| 137 | // Returns a pointer to the end of the string (i.e. the null character |
| 138 | // terminating the string). |
| 139 | // ---------------------------------------------------------------------- |
| 140 |
| 141 | namespace { |
| 142 |
| 143 | // Various routines to encode integers to strings. |
| 144 |
| 145 | // We split data encodings into a group of 2 digits, 4 digits, 8 digits as |
| 146 | // it's easier to combine powers of two into scalar arithmetic. |
| 147 |
| 148 | // Previous implementation used a lookup table of 200 bytes for every 2 bytes |
| 149 | // and it was memory bound, any L1 cache miss would result in a much slower |
| 150 | // result. When benchmarking with a cache eviction rate of several percent, |
| 151 | // this implementation proved to be better. |
| 152 |
| 153 | // These constants represent '00', '0000' and '00000000' as ascii strings in |
| 154 | // integers. We can add these numbers if we encode to bytes from 0 to 9. as |
| 155 | // 'i' = '0' + i for 0 <= i <= 9. |
| 156 | constexpr uint32_t kTwoZeroBytes = 0x0101 * '0'; |
| 157 | constexpr uint64_t kFourZeroBytes = 0x01010101 * '0'; |
| 158 | constexpr uint64_t kEightZeroBytes = 0x0101010101010101ull * '0'; |
| 159 |
| 160 | // * 103 / 1024 is a division by 10 for values from 0 to 99. It's also a |
| 161 | // division of a structure [k takes 2 bytes][m takes 2 bytes], then * 103 / 1024 |
| 162 | // will be [k / 10][m / 10]. It allows parallel division. |
| 163 | constexpr uint64_t kDivisionBy10Mul = 103u; |
| 164 | constexpr uint64_t kDivisionBy10Div = 1 << 10; |
| 165 |
| 166 | // * 10486 / 1048576 is a division by 100 for values from 0 to 9999. |
| 167 | constexpr uint64_t kDivisionBy100Mul = 10486u; |
| 168 | constexpr uint64_t kDivisionBy100Div = 1 << 20; |
| 169 |
| 170 | // Encode functions write the ASCII output of input `n` to `out_str`. |
| 171 | inline char* EncodeHundred(uint32_t n, absl::Nonnull<char*> out_str) { |
| 172 | int num_digits = static_cast<int>(n - 10) >> 8; |
| 173 | uint32_t div10 = (n * kDivisionBy10Mul) / kDivisionBy10Div; |
| 174 | uint32_t mod10 = n - 10u * div10; |
| 175 | uint32_t base = kTwoZeroBytes + div10 + (mod10 << 8); |
| 176 | base >>= num_digits & 8; |
| 177 | little_endian::Store16(out_str, static_cast<uint16_t>(base)); |
| 178 | return out_str + 2 + num_digits; |
| 179 | } |
| 180 |
| 181 | inline char* EncodeTenThousand(uint32_t n, absl::Nonnull<char*> out_str) { |
| 182 | // We split lower 2 digits and upper 2 digits of n into 2 byte consecutive |
| 183 | // blocks. 123 -> [\0\1][\0\23]. We divide by 10 both blocks |
| 184 | // (it's 1 division + zeroing upper bits), and compute modulo 10 as well "in |
| 185 | // parallel". Then we combine both results to have both ASCII digits, |
| 186 | // strip trailing zeros, add ASCII '0000' and return. |
| 187 | uint32_t div100 = (n * kDivisionBy100Mul) / kDivisionBy100Div; |
| 188 | uint32_t mod100 = n - 100ull * div100; |
| 189 | uint32_t hundreds = (mod100 << 16) + div100; |
| 190 | uint32_t tens = (hundreds * kDivisionBy10Mul) / kDivisionBy10Div; |
| 191 | tens &= (0xFull << 16) | 0xFull; |
| 192 | tens += (hundreds - 10ull * tens) << 8; |
| 193 | ABSL_ASSUME(tens != 0); |
| 194 | // The result can contain trailing zero bits, we need to strip them to a first |
| 195 | // significant byte in a final representation. For example, for n = 123, we |
| 196 | // have tens to have representation \0\1\2\3. We do `& -8` to round |
| 197 | // to a multiple to 8 to strip zero bytes, not all zero bits. |
| 198 | // countr_zero to help. |
| 199 | // 0 minus 8 to make MSVC happy. |
| 200 | uint32_t zeroes = static_cast<uint32_t>(absl::countr_zero(tens)) & (0 - 8u); |
| 201 | tens += kFourZeroBytes; |
| 202 | tens >>= zeroes; |
| 203 | little_endian::Store32(out_str, tens); |
| 204 | return out_str + sizeof(tens) - zeroes / 8; |
| 205 | } |
| 206 |
| 207 | // Helper function to produce an ASCII representation of `i`. |
| 208 | // |
| 209 | // Function returns an 8-byte integer which when summed with `kEightZeroBytes`, |
| 210 | // can be treated as a printable buffer with ascii representation of `i`, |
| 211 | // possibly with leading zeros. |
| 212 | // |
| 213 | // Example: |
| 214 | // |
| 215 | // uint64_t buffer = PrepareEightDigits(102030) + kEightZeroBytes; |
| 216 | // char* ascii = reinterpret_cast<char*>(&buffer); |
| 217 | // // Note two leading zeros: |
| 218 | // EXPECT_EQ(absl::string_view(ascii, 8), "00102030"); |
| 219 | // |
| 220 | // Pre-condition: `i` must be less than 100000000. |
| 221 | inline uint64_t PrepareEightDigits(uint32_t i) { |
| 222 | ABSL_ASSUME(i < 10000'0000); |
| 223 | // Prepare 2 blocks of 4 digits "in parallel". |
| 224 | uint32_t hi = i / 10000; |
| 225 | uint32_t lo = i % 10000; |
| 226 | uint64_t merged = hi | (uint64_t{lo} << 32); |
| 227 | uint64_t div100 = ((merged * kDivisionBy100Mul) / kDivisionBy100Div) & |
| 228 | ((0x7Full << 32) | 0x7Full); |
| 229 | uint64_t mod100 = merged - 100ull * div100; |
| 230 | uint64_t hundreds = (mod100 << 16) + div100; |
| 231 | uint64_t tens = (hundreds * kDivisionBy10Mul) / kDivisionBy10Div; |
| 232 | tens &= (0xFull << 48) | (0xFull << 32) | (0xFull << 16) | 0xFull; |
| 233 | tens += (hundreds - 10ull * tens) << 8; |
| 234 | return tens; |
| 235 | } |
| 236 |
| 237 | inline ABSL_ATTRIBUTE_ALWAYS_INLINE absl::Nonnull<char*> EncodeFullU32( |
| 238 | uint32_t n, absl::Nonnull<char*> out_str) { |
| 239 | if (n < 10) { |
| 240 | *out_str = static_cast<char>('0' + n); |
| 241 | return out_str + 1; |
| 242 | } |
| 243 | if (n < 100'000'000) { |
| 244 | uint64_t bottom = PrepareEightDigits(n); |
| 245 | ABSL_ASSUME(bottom != 0); |
| 246 | // 0 minus 8 to make MSVC happy. |
| 247 | uint32_t zeroes = |
| 248 | static_cast<uint32_t>(absl::countr_zero(bottom)) & (0 - 8u); |
| 249 | little_endian::Store64(out_str, (bottom + kEightZeroBytes) >> zeroes); |
| 250 | return out_str + sizeof(bottom) - zeroes / 8; |
| 251 | } |
| 252 | uint32_t div08 = n / 100'000'000; |
| 253 | uint32_t mod08 = n % 100'000'000; |
| 254 | uint64_t bottom = PrepareEightDigits(mod08) + kEightZeroBytes; |
| 255 | out_str = EncodeHundred(div08, out_str); |
| 256 | little_endian::Store64(out_str, bottom); |
| 257 | return out_str + sizeof(bottom); |
| 258 | } |
| 259 |
| 260 | inline ABSL_ATTRIBUTE_ALWAYS_INLINE char* EncodeFullU64(uint64_t i, |
| 261 | char* buffer) { |
| 262 | if (i <= std::numeric_limits<uint32_t>::max()) { |
| 263 | return EncodeFullU32(static_cast<uint32_t>(i), buffer); |
| 264 | } |
| 265 | uint32_t mod08; |
| 266 | if (i < 1'0000'0000'0000'0000ull) { |
| 267 | uint32_t div08 = static_cast<uint32_t>(i / 100'000'000ull); |
| 268 | mod08 = static_cast<uint32_t>(i % 100'000'000ull); |
| 269 | buffer = EncodeFullU32(div08, buffer); |
| 270 | } else { |
| 271 | uint64_t div08 = i / 100'000'000ull; |
| 272 | mod08 = static_cast<uint32_t>(i % 100'000'000ull); |
| 273 | uint32_t div016 = static_cast<uint32_t>(div08 / 100'000'000ull); |
| 274 | uint32_t div08mod08 = static_cast<uint32_t>(div08 % 100'000'000ull); |
| 275 | uint64_t mid_result = PrepareEightDigits(div08mod08) + kEightZeroBytes; |
| 276 | buffer = EncodeTenThousand(div016, buffer); |
| 277 | little_endian::Store64(buffer, mid_result); |
| 278 | buffer += sizeof(mid_result); |
| 279 | } |
| 280 | uint64_t mod_result = PrepareEightDigits(mod08) + kEightZeroBytes; |
| 281 | little_endian::Store64(buffer, mod_result); |
| 282 | return buffer + sizeof(mod_result); |
| 283 | } |
| 284 |
| 285 | } // namespace |
| 286 |
| 287 | void numbers_internal::PutTwoDigits(uint32_t i, absl::Nonnull<char*> buf) { |
| 288 | assert(i < 100); |
| 289 | uint32_t base = kTwoZeroBytes; |
| 290 | uint32_t div10 = (i * kDivisionBy10Mul) / kDivisionBy10Div; |
| 291 | uint32_t mod10 = i - 10u * div10; |
| 292 | base += div10 + (mod10 << 8); |
| 293 | little_endian::Store16(buf, static_cast<uint16_t>(base)); |
| 294 | } |
| 295 |
| 296 | absl::Nonnull<char*> numbers_internal::FastIntToBuffer( |
| 297 | uint32_t n, absl::Nonnull<char*> out_str) { |
| 298 | out_str = EncodeFullU32(n, out_str); |
| 299 | *out_str = '\0'; |
| 300 | return out_str; |
| 301 | } |
| 302 |
| 303 | absl::Nonnull<char*> numbers_internal::FastIntToBuffer( |
| 304 | int32_t i, absl::Nonnull<char*> buffer) { |
| 305 | uint32_t u = static_cast<uint32_t>(i); |
| 306 | if (i < 0) { |
| 307 | *buffer++ = '-'; |
| 308 | // We need to do the negation in modular (i.e., "unsigned") |
| 309 | // arithmetic; MSVC++ apparently warns for plain "-u", so |
| 310 | // we write the equivalent expression "0 - u" instead. |
| 311 | u = 0 - u; |
| 312 | } |
| 313 | buffer = EncodeFullU32(u, buffer); |
| 314 | *buffer = '\0'; |
| 315 | return buffer; |
| 316 | } |
| 317 |
| 318 | absl::Nonnull<char*> numbers_internal::FastIntToBuffer( |
| 319 | uint64_t i, absl::Nonnull<char*> buffer) { |
| 320 | buffer = EncodeFullU64(i, buffer); |
| 321 | *buffer = '\0'; |
| 322 | return buffer; |
| 323 | } |
| 324 |
| 325 | absl::Nonnull<char*> numbers_internal::FastIntToBuffer( |
| 326 | int64_t i, absl::Nonnull<char*> buffer) { |
| 327 | uint64_t u = static_cast<uint64_t>(i); |
| 328 | if (i < 0) { |
| 329 | *buffer++ = '-'; |
| 330 | // We need to do the negation in modular (i.e., "unsigned") |
| 331 | // arithmetic; MSVC++ apparently warns for plain "-u", so |
| 332 | // we write the equivalent expression "0 - u" instead. |
| 333 | u = 0 - u; |
| 334 | } |
| 335 | buffer = EncodeFullU64(u, buffer); |
| 336 | *buffer = '\0'; |
| 337 | return buffer; |
| 338 | } |
| 339 |
| 340 | // Given a 128-bit number expressed as a pair of uint64_t, high half first, |
| 341 | // return that number multiplied by the given 32-bit value. If the result is |
| 342 | // too large to fit in a 128-bit number, divide it by 2 until it fits. |
| 343 | static std::pair<uint64_t, uint64_t> Mul32(std::pair<uint64_t, uint64_t> num, |
| 344 | uint32_t mul) { |
| 345 | uint64_t bits0_31 = num.second & 0xFFFFFFFF; |
| 346 | uint64_t bits32_63 = num.second >> 32; |
| 347 | uint64_t bits64_95 = num.first & 0xFFFFFFFF; |
| 348 | uint64_t bits96_127 = num.first >> 32; |
| 349 |
| 350 | // The picture so far: each of these 64-bit values has only the lower 32 bits |
| 351 | // filled in. |
| 352 | // bits96_127: [ 00000000 xxxxxxxx ] |
| 353 | // bits64_95: [ 00000000 xxxxxxxx ] |
| 354 | // bits32_63: [ 00000000 xxxxxxxx ] |
| 355 | // bits0_31: [ 00000000 xxxxxxxx ] |
| 356 |
| 357 | bits0_31 *= mul; |
| 358 | bits32_63 *= mul; |
| 359 | bits64_95 *= mul; |
| 360 | bits96_127 *= mul; |
| 361 |
| 362 | // Now the top halves may also have value, though all 64 of their bits will |
| 363 | // never be set at the same time, since they are a result of a 32x32 bit |
| 364 | // multiply. This makes the carry calculation slightly easier. |
| 365 | // bits96_127: [ mmmmmmmm | mmmmmmmm ] |
| 366 | // bits64_95: [ | mmmmmmmm mmmmmmmm | ] |
| 367 | // bits32_63: | [ mmmmmmmm | mmmmmmmm ] |
| 368 | // bits0_31: | [ | mmmmmmmm mmmmmmmm ] |
| 369 | // eventually: [ bits128_up | ...bits64_127.... | ..bits0_63... ] |
| 370 |
| 371 | uint64_t bits0_63 = bits0_31 + (bits32_63 << 32); |
| 372 | uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) + |
| 373 | (bits0_63 < bits0_31); |
| 374 | uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95); |
| 375 | if (bits128_up == 0) return {bits64_127, bits0_63}; |
| 376 |
| 377 | auto shift = static_cast<unsigned>(bit_width(bits128_up)); |
| 378 | uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift)); |
| 379 | uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift)); |
| 380 | return {hi, lo}; |
| 381 | } |
| 382 |
| 383 | // Compute num * 5 ^ expfive, and return the first 128 bits of the result, |
| 384 | // where the first bit is always a one. So PowFive(1, 0) starts 0b100000, |
| 385 | // PowFive(1, 1) starts 0b101000, PowFive(1, 2) starts 0b110010, etc. |
| 386 | static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) { |
| 387 | std::pair<uint64_t, uint64_t> result = {num, 0}; |
| 388 | while (expfive >= 13) { |
| 389 | // 5^13 is the highest power of five that will fit in a 32-bit integer. |
| 390 | result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5); |
| 391 | expfive -= 13; |
| 392 | } |
| 393 | constexpr uint32_t powers_of_five[13] = { |
| 394 | 1, |
| 395 | 5, |
| 396 | 5 * 5, |
| 397 | 5 * 5 * 5, |
| 398 | 5 * 5 * 5 * 5, |
| 399 | 5 * 5 * 5 * 5 * 5, |
| 400 | 5 * 5 * 5 * 5 * 5 * 5, |
| 401 | 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 402 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 403 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 404 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 405 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5, |
| 406 | 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5}; |
| 407 | result = Mul32(result, powers_of_five[expfive & 15]); |
| 408 | int shift = countl_zero(result.first); |
| 409 | if (shift != 0) { |
| 410 | result.first = (result.first << shift) + (result.second >> (64 - shift)); |
| 411 | result.second = (result.second << shift); |
| 412 | } |
| 413 | return result; |
| 414 | } |
| 415 |
| 416 | struct ExpDigits { |
| 417 | int32_t exponent; |
| 418 | char digits[6]; |
| 419 | }; |
| 420 |
| 421 | // SplitToSix converts value, a positive double-precision floating-point number, |
| 422 | // into a base-10 exponent and 6 ASCII digits, where the first digit is never |
| 423 | // zero. For example, SplitToSix(1) returns an exponent of zero and a digits |
| 424 | // array of {'1', '0', '0', '0', '0', '0'}. If value is exactly halfway between |
| 425 | // two possible representations, e.g. value = 100000.5, then "round to even" is |
| 426 | // performed. |
| 427 | static ExpDigits SplitToSix(const double value) { |
| 428 | ExpDigits exp_dig; |
| 429 | int exp = 5; |
| 430 | double d = value; |
| 431 | // First step: calculate a close approximation of the output, where the |
| 432 | // value d will be between 100,000 and 999,999, representing the digits |
| 433 | // in the output ASCII array, and exp is the base-10 exponent. It would be |
| 434 | // faster to use a table here, and to look up the base-2 exponent of value, |
| 435 | // however value is an IEEE-754 64-bit number, so the table would have 2,000 |
| 436 | // entries, which is not cache-friendly. |
| 437 | if (d >= 999999.5) { |
| 438 | if (d >= 1e+261) exp += 256, d *= 1e-256; |
| 439 | if (d >= 1e+133) exp += 128, d *= 1e-128; |
| 440 | if (d >= 1e+69) exp += 64, d *= 1e-64; |
| 441 | if (d >= 1e+37) exp += 32, d *= 1e-32; |
| 442 | if (d >= 1e+21) exp += 16, d *= 1e-16; |
| 443 | if (d >= 1e+13) exp += 8, d *= 1e-8; |
| 444 | if (d >= 1e+9) exp += 4, d *= 1e-4; |
| 445 | if (d >= 1e+7) exp += 2, d *= 1e-2; |
| 446 | if (d >= 1e+6) exp += 1, d *= 1e-1; |
| 447 | } else { |
| 448 | if (d < 1e-250) exp -= 256, d *= 1e256; |
| 449 | if (d < 1e-122) exp -= 128, d *= 1e128; |
| 450 | if (d < 1e-58) exp -= 64, d *= 1e64; |
| 451 | if (d < 1e-26) exp -= 32, d *= 1e32; |
| 452 | if (d < 1e-10) exp -= 16, d *= 1e16; |
| 453 | if (d < 1e-2) exp -= 8, d *= 1e8; |
| 454 | if (d < 1e+2) exp -= 4, d *= 1e4; |
| 455 | if (d < 1e+4) exp -= 2, d *= 1e2; |
| 456 | if (d < 1e+5) exp -= 1, d *= 1e1; |
| 457 | } |
| 458 | // At this point, d is in the range [99999.5..999999.5) and exp is in the |
| 459 | // range [-324..308]. Since we need to round d up, we want to add a half |
| 460 | // and truncate. |
| 461 | // However, the technique above may have lost some precision, due to its |
| 462 | // repeated multiplication by constants that each may be off by half a bit |
| 463 | // of precision. This only matters if we're close to the edge though. |
| 464 | // Since we'd like to know if the fractional part of d is close to a half, |
| 465 | // we multiply it by 65536 and see if the fractional part is close to 32768. |
| 466 | // (The number doesn't have to be a power of two,but powers of two are faster) |
| 467 | uint64_t d64k = d * 65536; |
| 468 | uint32_t dddddd; // A 6-digit decimal integer. |
| 469 | if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) { |
| 470 | // OK, it's fairly likely that precision was lost above, which is |
| 471 | // not a surprise given only 52 mantissa bits are available. Therefore |
| 472 | // redo the calculation using 128-bit numbers. (64 bits are not enough). |
| 473 |
| 474 | // Start out with digits rounded down; maybe add one below. |
| 475 | dddddd = static_cast<uint32_t>(d64k / 65536); |
| 476 |
| 477 | // mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual |
| 478 | // value we're representing, of course, is M.mmm... * 2^exp2. |
| 479 | int exp2; |
| 480 | double m = std::frexp(value, &exp2); |
| 481 | uint64_t mantissa = m * (32768.0 * 65536.0 * 65536.0 * 65536.0); |
| 482 | // std::frexp returns an m value in the range [0.5, 1.0), however we |
| 483 | // can't multiply it by 2^64 and convert to an integer because some FPUs |
| 484 | // throw an exception when converting an number higher than 2^63 into an |
| 485 | // integer - even an unsigned 64-bit integer! Fortunately it doesn't matter |
| 486 | // since m only has 52 significant bits anyway. |
| 487 | mantissa <<= 1; |
| 488 | exp2 -= 64; // not needed, but nice for debugging |
| 489 |
| 490 | // OK, we are here to compare: |
| 491 | // (dddddd + 0.5) * 10^(exp-5) vs. mantissa * 2^exp2 |
| 492 | // so we can round up dddddd if appropriate. Those values span the full |
| 493 | // range of 600 orders of magnitude of IEE 64-bit floating-point. |
| 494 | // Fortunately, we already know they are very close, so we don't need to |
| 495 | // track the base-2 exponent of both sides. This greatly simplifies the |
| 496 | // the math since the 2^exp2 calculation is unnecessary and the power-of-10 |
| 497 | // calculation can become a power-of-5 instead. |
| 498 |
| 499 | std::pair<uint64_t, uint64_t> edge, val; |
| 500 | if (exp >= 6) { |
| 501 | // Compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa |
| 502 | // Since we're tossing powers of two, 2 * dddddd + 1 is the |
| 503 | // same as dddddd + 0.5 |
| 504 | edge = PowFive(2 * dddddd + 1, exp - 5); |
| 505 |
| 506 | val.first = mantissa; |
| 507 | val.second = 0; |
| 508 | } else { |
| 509 | // We can't compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa as we did |
| 510 | // above because (exp - 5) is negative. So we compare (dddddd + 0.5) to |
| 511 | // mantissa * 5 ^ (5 - exp) |
| 512 | edge = PowFive(2 * dddddd + 1, 0); |
| 513 |
| 514 | val = PowFive(mantissa, 5 - exp); |
| 515 | } |
| 516 | // printf("exp=%d %016lx %016lx vs %016lx %016lx\n", exp, val.first, |
| 517 | // val.second, edge.first, edge.second); |
| 518 | if (val > edge) { |
| 519 | dddddd++; |
| 520 | } else if (val == edge) { |
| 521 | dddddd += (dddddd & 1); |
| 522 | } |
| 523 | } else { |
| 524 | // Here, we are not close to the edge. |
| 525 | dddddd = static_cast<uint32_t>((d64k + 32768) / 65536); |
| 526 | } |
| 527 | if (dddddd == 1000000) { |
| 528 | dddddd = 100000; |
| 529 | exp += 1; |
| 530 | } |
| 531 | exp_dig.exponent = exp; |
| 532 |
| 533 | uint32_t two_digits = dddddd / 10000; |
| 534 | dddddd -= two_digits * 10000; |
| 535 | numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]); |
| 536 |
| 537 | two_digits = dddddd / 100; |
| 538 | dddddd -= two_digits * 100; |
| 539 | numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[2]); |
| 540 |
| 541 | numbers_internal::PutTwoDigits(dddddd, &exp_dig.digits[4]); |
| 542 | return exp_dig; |
| 543 | } |
| 544 |
| 545 | // Helper function for fast formatting of floating-point. |
| 546 | // The result is the same as "%g", a.k.a. "%.6g". |
| 547 | size_t numbers_internal::SixDigitsToBuffer(double d, |
| 548 | absl::Nonnull<char*> const buffer) { |
| 549 | static_assert(std::numeric_limits<float>::is_iec559, |
| 550 | "IEEE-754/IEC-559 support only"); |
| 551 |
| 552 | char* out = buffer; // we write data to out, incrementing as we go, but |
| 553 | // FloatToBuffer always returns the address of the buffer |
| 554 | // passed in. |
| 555 |
| 556 | if (std::isnan(d)) { |
| 557 | strcpy(out, "nan"); // NOLINT(runtime/printf) |
| 558 | return 3; |
| 559 | } |
| 560 | if (d == 0) { // +0 and -0 are handled here |
| 561 | if (std::signbit(d)) *out++ = '-'; |
| 562 | *out++ = '0'; |
| 563 | *out = 0; |
| 564 | return static_cast<size_t>(out - buffer); |
| 565 | } |
| 566 | if (d < 0) { |
| 567 | *out++ = '-'; |
| 568 | d = -d; |
| 569 | } |
| 570 | if (d > std::numeric_limits<double>::max()) { |
| 571 | strcpy(out, "inf"); // NOLINT(runtime/printf) |
| 572 | return static_cast<size_t>(out + 3 - buffer); |
| 573 | } |
| 574 |
| 575 | auto exp_dig = SplitToSix(d); |
| 576 | int exp = exp_dig.exponent; |
| 577 | const char* digits = exp_dig.digits; |
| 578 | out[0] = '0'; |
| 579 | out[1] = '.'; |
| 580 | switch (exp) { |
| 581 | case 5: |
| 582 | memcpy(out, &digits[0], 6), out += 6; |
| 583 | *out = 0; |
| 584 | return static_cast<size_t>(out - buffer); |
| 585 | case 4: |
| 586 | memcpy(out, &digits[0], 5), out += 5; |
| 587 | if (digits[5] != '0') { |
| 588 | *out++ = '.'; |
| 589 | *out++ = digits[5]; |
| 590 | } |
| 591 | *out = 0; |
| 592 | return static_cast<size_t>(out - buffer); |
| 593 | case 3: |
| 594 | memcpy(out, &digits[0], 4), out += 4; |
| 595 | if ((digits[5] | digits[4]) != '0') { |
| 596 | *out++ = '.'; |
| 597 | *out++ = digits[4]; |
| 598 | if (digits[5] != '0') *out++ = digits[5]; |
| 599 | } |
| 600 | *out = 0; |
| 601 | return static_cast<size_t>(out - buffer); |
| 602 | case 2: |
| 603 | memcpy(out, &digits[0], 3), out += 3; |
| 604 | *out++ = '.'; |
| 605 | memcpy(out, &digits[3], 3); |
| 606 | out += 3; |
| 607 | while (out[-1] == '0') --out; |
| 608 | if (out[-1] == '.') --out; |
| 609 | *out = 0; |
| 610 | return static_cast<size_t>(out - buffer); |
| 611 | case 1: |
| 612 | memcpy(out, &digits[0], 2), out += 2; |
| 613 | *out++ = '.'; |
| 614 | memcpy(out, &digits[2], 4); |
| 615 | out += 4; |
| 616 | while (out[-1] == '0') --out; |
| 617 | if (out[-1] == '.') --out; |
| 618 | *out = 0; |
| 619 | return static_cast<size_t>(out - buffer); |
| 620 | case 0: |
| 621 | memcpy(out, &digits[0], 1), out += 1; |
| 622 | *out++ = '.'; |
| 623 | memcpy(out, &digits[1], 5); |
| 624 | out += 5; |
| 625 | while (out[-1] == '0') --out; |
| 626 | if (out[-1] == '.') --out; |
| 627 | *out = 0; |
| 628 | return static_cast<size_t>(out - buffer); |
| 629 | case -4: |
| 630 | out[2] = '0'; |
| 631 | ++out; |
| 632 | ABSL_FALLTHROUGH_INTENDED; |
| 633 | case -3: |
| 634 | out[2] = '0'; |
| 635 | ++out; |
| 636 | ABSL_FALLTHROUGH_INTENDED; |
| 637 | case -2: |
| 638 | out[2] = '0'; |
| 639 | ++out; |
| 640 | ABSL_FALLTHROUGH_INTENDED; |
| 641 | case -1: |
| 642 | out += 2; |
| 643 | memcpy(out, &digits[0], 6); |
| 644 | out += 6; |
| 645 | while (out[-1] == '0') --out; |
| 646 | *out = 0; |
| 647 | return static_cast<size_t>(out - buffer); |
| 648 | } |
| 649 | assert(exp < -4 || exp >= 6); |
| 650 | out[0] = digits[0]; |
| 651 | assert(out[1] == '.'); |
| 652 | out += 2; |
| 653 | memcpy(out, &digits[1], 5), out += 5; |
| 654 | while (out[-1] == '0') --out; |
| 655 | if (out[-1] == '.') --out; |
| 656 | *out++ = 'e'; |
| 657 | if (exp > 0) { |
| 658 | *out++ = '+'; |
| 659 | } else { |
| 660 | *out++ = '-'; |
| 661 | exp = -exp; |
| 662 | } |
| 663 | if (exp > 99) { |
| 664 | int dig1 = exp / 100; |
| 665 | exp -= dig1 * 100; |
| 666 | *out++ = '0' + static_cast<char>(dig1); |
| 667 | } |
| 668 | PutTwoDigits(static_cast<uint32_t>(exp), out); |
| 669 | out += 2; |
| 670 | *out = 0; |
| 671 | return static_cast<size_t>(out - buffer); |
| 672 | } |
| 673 |
| 674 | namespace { |
| 675 | // Represents integer values of digits. |
| 676 | // Uses 36 to indicate an invalid character since we support |
| 677 | // bases up to 36. |
| 678 | static constexpr std::array<int8_t, 256> kAsciiToInt = { |
| 679 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s. |
| 680 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 681 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0, 1, 2, 3, 4, 5, |
| 682 | 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, |
| 683 | 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, |
| 684 | 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 685 | 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36, |
| 686 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 687 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 688 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 689 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 690 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 691 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, |
| 692 | 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36}; |
| 693 |
| 694 | // Parse the sign and optional hex or oct prefix in text. |
| 695 | inline bool safe_parse_sign_and_base( |
| 696 | absl::Nonnull<absl::string_view*> text /*inout*/, |
| 697 | absl::Nonnull<int*> base_ptr /*inout*/, |
| 698 | absl::Nonnull<bool*> negative_ptr /*output*/) { |
| 699 | if (text->data() == nullptr) { |
| 700 | return false; |
| 701 | } |
| 702 |
| 703 | const char* start = text->data(); |
| 704 | const char* end = start + text->size(); |
| 705 | int base = *base_ptr; |
| 706 |
| 707 | // Consume whitespace. |
| 708 | while (start < end && |
| 709 | absl::ascii_isspace(static_cast<unsigned char>(start[0]))) { |
| 710 | ++start; |
| 711 | } |
| 712 | while (start < end && |
| 713 | absl::ascii_isspace(static_cast<unsigned char>(end[-1]))) { |
| 714 | --end; |
| 715 | } |
| 716 | if (start >= end) { |
| 717 | return false; |
| 718 | } |
| 719 |
| 720 | // Consume sign. |
| 721 | *negative_ptr = (start[0] == '-'); |
| 722 | if (*negative_ptr || start[0] == '+') { |
| 723 | ++start; |
| 724 | if (start >= end) { |
| 725 | return false; |
| 726 | } |
| 727 | } |
| 728 |
| 729 | // Consume base-dependent prefix. |
| 730 | // base 0: "0x" -> base 16, "0" -> base 8, default -> base 10 |
| 731 | // base 16: "0x" -> base 16 |
| 732 | // Also validate the base. |
| 733 | if (base == 0) { |
| 734 | if (end - start >= 2 && start[0] == '0' && |
| 735 | (start[1] == 'x' || start[1] == 'X')) { |
| 736 | base = 16; |
| 737 | start += 2; |
| 738 | if (start >= end) { |
| 739 | // "0x" with no digits after is invalid. |
| 740 | return false; |
| 741 | } |
| 742 | } else if (end - start >= 1 && start[0] == '0') { |
| 743 | base = 8; |
| 744 | start += 1; |
| 745 | } else { |
| 746 | base = 10; |
| 747 | } |
| 748 | } else if (base == 16) { |
| 749 | if (end - start >= 2 && start[0] == '0' && |
| 750 | (start[1] == 'x' || start[1] == 'X')) { |
| 751 | start += 2; |
| 752 | if (start >= end) { |
| 753 | // "0x" with no digits after is invalid. |
| 754 | return false; |
| 755 | } |
| 756 | } |
| 757 | } else if (base >= 2 && base <= 36) { |
| 758 | // okay |
| 759 | } else { |
| 760 | return false; |
| 761 | } |
| 762 | *text = absl::string_view(start, static_cast<size_t>(end - start)); |
| 763 | *base_ptr = base; |
| 764 | return true; |
| 765 | } |
| 766 |
| 767 | // Consume digits. |
| 768 | // |
| 769 | // The classic loop: |
| 770 | // |
| 771 | // for each digit |
| 772 | // value = value * base + digit |
| 773 | // value *= sign |
| 774 | // |
| 775 | // The classic loop needs overflow checking. It also fails on the most |
| 776 | // negative integer, -2147483648 in 32-bit two's complement representation. |
| 777 | // |
| 778 | // My improved loop: |
| 779 | // |
| 780 | // if (!negative) |
| 781 | // for each digit |
| 782 | // value = value * base |
| 783 | // value = value + digit |
| 784 | // else |
| 785 | // for each digit |
| 786 | // value = value * base |
| 787 | // value = value - digit |
| 788 | // |
| 789 | // Overflow checking becomes simple. |
| 790 |
| 791 | // Lookup tables per IntType: |
| 792 | // vmax/base and vmin/base are precomputed because division costs at least 8ns. |
| 793 | // TODO(junyer): Doing this per base instead (i.e. an array of structs, not a |
| 794 | // struct of arrays) would probably be better in terms of d-cache for the most |
| 795 | // commonly used bases. |
| 796 | template <typename IntType> |
| 797 | struct LookupTables { |
| 798 | ABSL_CONST_INIT static const IntType kVmaxOverBase[]; |
| 799 | ABSL_CONST_INIT static const IntType kVminOverBase[]; |
| 800 | }; |
| 801 |
| 802 | // An array initializer macro for X/base where base in [0, 36]. |
| 803 | // However, note that lookups for base in [0, 1] should never happen because |
| 804 | // base has been validated to be in [2, 36] by safe_parse_sign_and_base(). |
| 805 | #define X_OVER_BASE_INITIALIZER(X) \ |
| 806 | { \ |
| 807 | 0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \ |
| 808 | X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18, \ |
| 809 | X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26, \ |
| 810 | X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34, \ |
| 811 | X / 35, X / 36, \ |
| 812 | } |
| 813 |
| 814 | // This kVmaxOverBase is generated with |
| 815 | // for (int base = 2; base < 37; ++base) { |
| 816 | // absl::uint128 max = std::numeric_limits<absl::uint128>::max(); |
| 817 | // auto result = max / base; |
| 818 | // std::cout << " MakeUint128(" << absl::Uint128High64(result) << "u, " |
| 819 | // << absl::Uint128Low64(result) << "u),\n"; |
| 820 | // } |
| 821 | // See https://godbolt.org/z/aneYsb |
| 822 | // |
| 823 | // uint128& operator/=(uint128) is not constexpr, so hardcode the resulting |
| 824 | // array to avoid a static initializer. |
| 825 | template <> |
| 826 | ABSL_CONST_INIT const uint128 LookupTables<uint128>::kVmaxOverBase[] = { |
| 827 | 0, |
| 828 | 0, |
| 829 | MakeUint128(9223372036854775807u, 18446744073709551615u), |
| 830 | MakeUint128(6148914691236517205u, 6148914691236517205u), |
| 831 | MakeUint128(4611686018427387903u, 18446744073709551615u), |
| 832 | MakeUint128(3689348814741910323u, 3689348814741910323u), |
| 833 | MakeUint128(3074457345618258602u, 12297829382473034410u), |
| 834 | MakeUint128(2635249153387078802u, 5270498306774157604u), |
| 835 | MakeUint128(2305843009213693951u, 18446744073709551615u), |
| 836 | MakeUint128(2049638230412172401u, 14347467612885206812u), |
| 837 | MakeUint128(1844674407370955161u, 11068046444225730969u), |
| 838 | MakeUint128(1676976733973595601u, 8384883669867978007u), |
| 839 | MakeUint128(1537228672809129301u, 6148914691236517205u), |
| 840 | MakeUint128(1418980313362273201u, 4256940940086819603u), |
| 841 | MakeUint128(1317624576693539401u, 2635249153387078802u), |
| 842 | MakeUint128(1229782938247303441u, 1229782938247303441u), |
| 843 | MakeUint128(1152921504606846975u, 18446744073709551615u), |
| 844 | MakeUint128(1085102592571150095u, 1085102592571150095u), |
| 845 | MakeUint128(1024819115206086200u, 16397105843297379214u), |
| 846 | MakeUint128(970881267037344821u, 16504981539634861972u), |
| 847 | MakeUint128(922337203685477580u, 14757395258967641292u), |
| 848 | MakeUint128(878416384462359600u, 14054662151397753612u), |
| 849 | MakeUint128(838488366986797800u, 13415813871788764811u), |
| 850 | MakeUint128(802032351030850070u, 4812194106185100421u), |
| 851 | MakeUint128(768614336404564650u, 12297829382473034410u), |
| 852 | MakeUint128(737869762948382064u, 11805916207174113034u), |
| 853 | MakeUint128(709490156681136600u, 11351842506898185609u), |
| 854 | MakeUint128(683212743470724133u, 17080318586768103348u), |
| 855 | MakeUint128(658812288346769700u, 10540996613548315209u), |
| 856 | MakeUint128(636094623231363848u, 15266270957552732371u), |
| 857 | MakeUint128(614891469123651720u, 9838263505978427528u), |
| 858 | MakeUint128(595056260442243600u, 9520900167075897608u), |
| 859 | MakeUint128(576460752303423487u, 18446744073709551615u), |
| 860 | MakeUint128(558992244657865200u, 8943875914525843207u), |
| 861 | MakeUint128(542551296285575047u, 9765923333140350855u), |
| 862 | MakeUint128(527049830677415760u, 8432797290838652167u), |
| 863 | MakeUint128(512409557603043100u, 8198552921648689607u), |
| 864 | }; |
| 865 |
| 866 | // This kVmaxOverBase generated with |
| 867 | // for (int base = 2; base < 37; ++base) { |
| 868 | // absl::int128 max = std::numeric_limits<absl::int128>::max(); |
| 869 | // auto result = max / base; |
| 870 | // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", " |
| 871 | // << absl::Int128Low64(result) << "u),\n"; |
| 872 | // } |
| 873 | // See https://godbolt.org/z/7djYWz |
| 874 | // |
| 875 | // int128& operator/=(int128) is not constexpr, so hardcode the resulting array |
| 876 | // to avoid a static initializer. |
| 877 | template <> |
| 878 | ABSL_CONST_INIT const int128 LookupTables<int128>::kVmaxOverBase[] = { |
| 879 | 0, |
| 880 | 0, |
| 881 | MakeInt128(4611686018427387903, 18446744073709551615u), |
| 882 | MakeInt128(3074457345618258602, 12297829382473034410u), |
| 883 | MakeInt128(2305843009213693951, 18446744073709551615u), |
| 884 | MakeInt128(1844674407370955161, 11068046444225730969u), |
| 885 | MakeInt128(1537228672809129301, 6148914691236517205u), |
| 886 | MakeInt128(1317624576693539401, 2635249153387078802u), |
| 887 | MakeInt128(1152921504606846975, 18446744073709551615u), |
| 888 | MakeInt128(1024819115206086200, 16397105843297379214u), |
| 889 | MakeInt128(922337203685477580, 14757395258967641292u), |
| 890 | MakeInt128(838488366986797800, 13415813871788764811u), |
| 891 | MakeInt128(768614336404564650, 12297829382473034410u), |
| 892 | MakeInt128(709490156681136600, 11351842506898185609u), |
| 893 | MakeInt128(658812288346769700, 10540996613548315209u), |
| 894 | MakeInt128(614891469123651720, 9838263505978427528u), |
| 895 | MakeInt128(576460752303423487, 18446744073709551615u), |
| 896 | MakeInt128(542551296285575047, 9765923333140350855u), |
| 897 | MakeInt128(512409557603043100, 8198552921648689607u), |
| 898 | MakeInt128(485440633518672410, 17475862806672206794u), |
| 899 | MakeInt128(461168601842738790, 7378697629483820646u), |
| 900 | MakeInt128(439208192231179800, 7027331075698876806u), |
| 901 | MakeInt128(419244183493398900, 6707906935894382405u), |
| 902 | MakeInt128(401016175515425035, 2406097053092550210u), |
| 903 | MakeInt128(384307168202282325, 6148914691236517205u), |
| 904 | MakeInt128(368934881474191032, 5902958103587056517u), |
| 905 | MakeInt128(354745078340568300, 5675921253449092804u), |
| 906 | MakeInt128(341606371735362066, 17763531330238827482u), |
| 907 | MakeInt128(329406144173384850, 5270498306774157604u), |
| 908 | MakeInt128(318047311615681924, 7633135478776366185u), |
| 909 | MakeInt128(307445734561825860, 4919131752989213764u), |
| 910 | MakeInt128(297528130221121800, 4760450083537948804u), |
| 911 | MakeInt128(288230376151711743, 18446744073709551615u), |
| 912 | MakeInt128(279496122328932600, 4471937957262921603u), |
| 913 | MakeInt128(271275648142787523, 14106333703424951235u), |
| 914 | MakeInt128(263524915338707880, 4216398645419326083u), |
| 915 | MakeInt128(256204778801521550, 4099276460824344803u), |
| 916 | }; |
| 917 |
| 918 | // This kVminOverBase generated with |
| 919 | // for (int base = 2; base < 37; ++base) { |
| 920 | // absl::int128 min = std::numeric_limits<absl::int128>::min(); |
| 921 | // auto result = min / base; |
| 922 | // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", " |
| 923 | // << absl::Int128Low64(result) << "u),\n"; |
| 924 | // } |
| 925 | // |
| 926 | // See https://godbolt.org/z/7djYWz |
| 927 | // |
| 928 | // int128& operator/=(int128) is not constexpr, so hardcode the resulting array |
| 929 | // to avoid a static initializer. |
| 930 | template <> |
| 931 | ABSL_CONST_INIT const int128 LookupTables<int128>::kVminOverBase[] = { |
| 932 | 0, |
| 933 | 0, |
| 934 | MakeInt128(-4611686018427387904, 0u), |
| 935 | MakeInt128(-3074457345618258603, 6148914691236517206u), |
| 936 | MakeInt128(-2305843009213693952, 0u), |
| 937 | MakeInt128(-1844674407370955162, 7378697629483820647u), |
| 938 | MakeInt128(-1537228672809129302, 12297829382473034411u), |
| 939 | MakeInt128(-1317624576693539402, 15811494920322472814u), |
| 940 | MakeInt128(-1152921504606846976, 0u), |
| 941 | MakeInt128(-1024819115206086201, 2049638230412172402u), |
| 942 | MakeInt128(-922337203685477581, 3689348814741910324u), |
| 943 | MakeInt128(-838488366986797801, 5030930201920786805u), |
| 944 | MakeInt128(-768614336404564651, 6148914691236517206u), |
| 945 | MakeInt128(-709490156681136601, 7094901566811366007u), |
| 946 | MakeInt128(-658812288346769701, 7905747460161236407u), |
| 947 | MakeInt128(-614891469123651721, 8608480567731124088u), |
| 948 | MakeInt128(-576460752303423488, 0u), |
| 949 | MakeInt128(-542551296285575048, 8680820740569200761u), |
| 950 | MakeInt128(-512409557603043101, 10248191152060862009u), |
| 951 | MakeInt128(-485440633518672411, 970881267037344822u), |
| 952 | MakeInt128(-461168601842738791, 11068046444225730970u), |
| 953 | MakeInt128(-439208192231179801, 11419412998010674810u), |
| 954 | MakeInt128(-419244183493398901, 11738837137815169211u), |
| 955 | MakeInt128(-401016175515425036, 16040647020617001406u), |
| 956 | MakeInt128(-384307168202282326, 12297829382473034411u), |
| 957 | MakeInt128(-368934881474191033, 12543785970122495099u), |
| 958 | MakeInt128(-354745078340568301, 12770822820260458812u), |
| 959 | MakeInt128(-341606371735362067, 683212743470724134u), |
| 960 | MakeInt128(-329406144173384851, 13176245766935394012u), |
| 961 | MakeInt128(-318047311615681925, 10813608594933185431u), |
| 962 | MakeInt128(-307445734561825861, 13527612320720337852u), |
| 963 | MakeInt128(-297528130221121801, 13686293990171602812u), |
| 964 | MakeInt128(-288230376151711744, 0u), |
| 965 | MakeInt128(-279496122328932601, 13974806116446630013u), |
| 966 | MakeInt128(-271275648142787524, 4340410370284600381u), |
| 967 | MakeInt128(-263524915338707881, 14230345428290225533u), |
| 968 | MakeInt128(-256204778801521551, 14347467612885206813u), |
| 969 | }; |
| 970 |
| 971 | template <typename IntType> |
| 972 | ABSL_CONST_INIT const IntType LookupTables<IntType>::kVmaxOverBase[] = |
| 973 | X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max()); |
| 974 |
| 975 | template <typename IntType> |
| 976 | ABSL_CONST_INIT const IntType LookupTables<IntType>::kVminOverBase[] = |
| 977 | X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min()); |
| 978 |
| 979 | #undef X_OVER_BASE_INITIALIZER |
| 980 |
| 981 | template <typename IntType> |
| 982 | inline bool safe_parse_positive_int(absl::string_view text, int base, |
| 983 | absl::Nonnull<IntType*> value_p) { |
| 984 | IntType value = 0; |
| 985 | const IntType vmax = std::numeric_limits<IntType>::max(); |
| 986 | assert(vmax > 0); |
| 987 | assert(base >= 0); |
| 988 | const IntType base_inttype = static_cast<IntType>(base); |
| 989 | assert(vmax >= base_inttype); |
| 990 | const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base]; |
| 991 | assert(base < 2 || |
| 992 | std::numeric_limits<IntType>::max() / base_inttype == vmax_over_base); |
| 993 | const char* start = text.data(); |
| 994 | const char* end = start + text.size(); |
| 995 | // loop over digits |
| 996 | for (; start < end; ++start) { |
| 997 | unsigned char c = static_cast<unsigned char>(start[0]); |
| 998 | IntType digit = static_cast<IntType>(kAsciiToInt[c]); |
| 999 | if (digit >= base_inttype) { |
| 1000 | *value_p = value; |
| 1001 | return false; |
| 1002 | } |
| 1003 | if (value > vmax_over_base) { |
| 1004 | *value_p = vmax; |
| 1005 | return false; |
| 1006 | } |
| 1007 | value *= base_inttype; |
| 1008 | if (value > vmax - digit) { |
| 1009 | *value_p = vmax; |
| 1010 | return false; |
| 1011 | } |
| 1012 | value += digit; |
| 1013 | } |
| 1014 | *value_p = value; |
| 1015 | return true; |
| 1016 | } |
| 1017 |
| 1018 | template <typename IntType> |
| 1019 | inline bool safe_parse_negative_int(absl::string_view text, int base, |
| 1020 | absl::Nonnull<IntType*> value_p) { |
| 1021 | IntType value = 0; |
| 1022 | const IntType vmin = std::numeric_limits<IntType>::min(); |
| 1023 | assert(vmin < 0); |
| 1024 | assert(vmin <= 0 - base); |
| 1025 | IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base]; |
| 1026 | assert(base < 2 || |
| 1027 | std::numeric_limits<IntType>::min() / base == vmin_over_base); |
| 1028 | // 2003 c++ standard [expr.mul] |
| 1029 | // "... the sign of the remainder is implementation-defined." |
| 1030 | // Although (vmin/base)*base + vmin%base is always vmin. |
| 1031 | // 2011 c++ standard tightens the spec but we cannot rely on it. |
| 1032 | // TODO(junyer): Handle this in the lookup table generation. |
| 1033 | if (vmin % base > 0) { |
| 1034 | vmin_over_base += 1; |
| 1035 | } |
| 1036 | const char* start = text.data(); |
| 1037 | const char* end = start + text.size(); |
| 1038 | // loop over digits |
| 1039 | for (; start < end; ++start) { |
| 1040 | unsigned char c = static_cast<unsigned char>(start[0]); |
| 1041 | int digit = kAsciiToInt[c]; |
| 1042 | if (digit >= base) { |
| 1043 | *value_p = value; |
| 1044 | return false; |
| 1045 | } |
| 1046 | if (value < vmin_over_base) { |
| 1047 | *value_p = vmin; |
| 1048 | return false; |
| 1049 | } |
| 1050 | value *= base; |
| 1051 | if (value < vmin + digit) { |
| 1052 | *value_p = vmin; |
| 1053 | return false; |
| 1054 | } |
| 1055 | value -= digit; |
| 1056 | } |
| 1057 | *value_p = value; |
| 1058 | return true; |
| 1059 | } |
| 1060 |
| 1061 | // Input format based on POSIX.1-2008 strtol |
| 1062 | // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html |
| 1063 | template <typename IntType> |
| 1064 | inline bool safe_int_internal(absl::string_view text, |
| 1065 | absl::Nonnull<IntType*> value_p, int base) { |
| 1066 | *value_p = 0; |
| 1067 | bool negative; |
| 1068 | if (!safe_parse_sign_and_base(&text, &base, &negative)) { |
| 1069 | return false; |
| 1070 | } |
| 1071 | if (!negative) { |
| 1072 | return safe_parse_positive_int(text, base, value_p); |
| 1073 | } else { |
| 1074 | return safe_parse_negative_int(text, base, value_p); |
| 1075 | } |
| 1076 | } |
| 1077 |
| 1078 | template <typename IntType> |
| 1079 | inline bool safe_uint_internal(absl::string_view text, |
| 1080 | absl::Nonnull<IntType*> value_p, int base) { |
| 1081 | *value_p = 0; |
| 1082 | bool negative; |
| 1083 | if (!safe_parse_sign_and_base(&text, &base, &negative) || negative) { |
| 1084 | return false; |
| 1085 | } |
| 1086 | return safe_parse_positive_int(text, base, value_p); |
| 1087 | } |
| 1088 | } // anonymous namespace |
| 1089 |
| 1090 | namespace numbers_internal { |
| 1091 |
| 1092 | // Digit conversion. |
| 1093 | ABSL_CONST_INIT ABSL_DLL const char kHexChar[] = |
| 1094 | "0123456789abcdef"; |
| 1095 |
| 1096 | ABSL_CONST_INIT ABSL_DLL const char kHexTable[513] = |
| 1097 | "000102030405060708090a0b0c0d0e0f" |
| 1098 | "101112131415161718191a1b1c1d1e1f" |
| 1099 | "202122232425262728292a2b2c2d2e2f" |
| 1100 | "303132333435363738393a3b3c3d3e3f" |
| 1101 | "404142434445464748494a4b4c4d4e4f" |
| 1102 | "505152535455565758595a5b5c5d5e5f" |
| 1103 | "606162636465666768696a6b6c6d6e6f" |
| 1104 | "707172737475767778797a7b7c7d7e7f" |
| 1105 | "808182838485868788898a8b8c8d8e8f" |
| 1106 | "909192939495969798999a9b9c9d9e9f" |
| 1107 | "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf" |
| 1108 | "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf" |
| 1109 | "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf" |
| 1110 | "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf" |
| 1111 | "e0e1e2e3e4e5e6e7e8e9eaebecedeeef" |
| 1112 | "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff"; |
| 1113 |
| 1114 | bool safe_strto32_base(absl::string_view text, absl::Nonnull<int32_t*> value, |
| 1115 | int base) { |
| 1116 | return safe_int_internal<int32_t>(text, value, base); |
| 1117 | } |
| 1118 |
| 1119 | bool safe_strto64_base(absl::string_view text, absl::Nonnull<int64_t*> value, |
| 1120 | int base) { |
| 1121 | return safe_int_internal<int64_t>(text, value, base); |
| 1122 | } |
| 1123 |
| 1124 | bool safe_strto128_base(absl::string_view text, absl::Nonnull<int128*> value, |
| 1125 | int base) { |
| 1126 | return safe_int_internal<absl::int128>(text, value, base); |
| 1127 | } |
| 1128 |
| 1129 | bool safe_strtou32_base(absl::string_view text, absl::Nonnull<uint32_t*> value, |
| 1130 | int base) { |
| 1131 | return safe_uint_internal<uint32_t>(text, value, base); |
| 1132 | } |
| 1133 |
| 1134 | bool safe_strtou64_base(absl::string_view text, absl::Nonnull<uint64_t*> value, |
| 1135 | int base) { |
| 1136 | return safe_uint_internal<uint64_t>(text, value, base); |
| 1137 | } |
| 1138 |
| 1139 | bool safe_strtou128_base(absl::string_view text, absl::Nonnull<uint128*> value, |
| 1140 | int base) { |
| 1141 | return safe_uint_internal<absl::uint128>(text, value, base); |
| 1142 | } |
| 1143 |
| 1144 | } // namespace numbers_internal |
| 1145 | ABSL_NAMESPACE_END |
| 1146 | } // namespace absl |
| 1147 |