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