18 #include "absl/strings/numbers.h"
33 #include "absl/base/attributes.h"
34 #include "absl/base/internal/raw_logging.h"
35 #include "absl/numeric/bits.h"
36 #include "absl/strings/ascii.h"
37 #include "absl/strings/charconv.h"
38 #include "absl/strings/escaping.h"
39 #include "absl/strings/internal/memutil.h"
40 #include "absl/strings/match.h"
41 #include "absl/strings/str_cat.h"
51 if (!
str.empty() &&
str[0] ==
'+') {
53 if (!
str.empty() &&
str[0] ==
'-') {
58 if (
result.ec == std::errc::invalid_argument) {
67 if (
result.ec == std::errc::result_out_of_range) {
69 *
out = std::numeric_limits<float>::infinity();
70 }
else if (*
out < -1.0) {
71 *
out = -std::numeric_limits<float>::infinity();
82 if (!
str.empty() &&
str[0] ==
'+') {
84 if (!
str.empty() &&
str[0] ==
'-') {
89 if (
result.ec == std::errc::invalid_argument) {
98 if (
result.ec == std::errc::result_out_of_range) {
100 *
out = std::numeric_limits<double>::infinity();
101 }
else if (*
out < -1.0) {
102 *
out = -std::numeric_limits<double>::infinity();
140 const char one_ASCII_final_digits[10][2] {
141 {
'0', 0}, {
'1', 0}, {
'2', 0}, {
'3', 0}, {
'4', 0},
142 {
'5', 0}, {
'6', 0}, {
'7', 0}, {
'8', 0}, {
'9', 0},
155 if (i >= 1000000000) {
156 digits =
i / 100000000;
157 i -= digits * 100000000;
161 digits =
i / 1000000;
162 i -= digits * 1000000;
185 if (i >= 10)
goto lt100;
190 if (i >= 1000)
goto lt10_000;
197 if (i >= 100000)
goto lt1_000_000;
204 if (i >= 10000000)
goto lt100_000_000;
205 digits =
i / 1000000;
206 i -= digits * 1000000;
211 digits =
i / 100000000;
212 i -= digits * 100000000;
235 u32 =
static_cast<uint32_t>(
i - top_1to11 * 1000000000);
238 if (top_1to11_32 == top_1to11) {
251 u32 -= digits * 10000000;
254 digits = u32 / 100000;
255 u32 -= digits * 100000;
259 u32 -= digits * 1000;
282 static std::pair<uint64_t, uint64_t>
Mul32(std::pair<uint64_t, uint64_t>
num,
310 uint64_t bits0_63 = bits0_31 + (bits32_63 << 32);
311 uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) +
312 (bits0_63 < bits0_31);
313 uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95);
314 if (bits128_up == 0)
return {bits64_127, bits0_63};
316 auto shift =
static_cast<unsigned>(
bit_width(bits128_up));
317 uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift));
318 uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift));
326 std::pair<uint64_t, uint64_t>
result = {
num, 0};
327 while (expfive >= 13) {
332 constexpr
int powers_of_five[13] = {
339 5 * 5 * 5 * 5 * 5 * 5,
340 5 * 5 * 5 * 5 * 5 * 5 * 5,
341 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
342 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
343 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
344 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
345 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
377 if (d >= 1
e+261) exp += 256, d *= 1
e-256;
378 if (d >= 1
e+133) exp += 128, d *= 1
e-128;
379 if (d >= 1
e+69) exp += 64, d *= 1
e-64;
380 if (d >= 1
e+37) exp += 32, d *= 1
e-32;
381 if (d >= 1
e+21) exp += 16, d *= 1
e-16;
382 if (d >= 1
e+13) exp += 8, d *= 1
e-8;
383 if (d >= 1
e+9) exp += 4, d *= 1
e-4;
384 if (d >= 1
e+7) exp += 2, d *= 1
e-2;
385 if (d >= 1
e+6) exp += 1, d *= 1
e-1;
387 if (d < 1
e-250) exp -= 256, d *= 1e256;
388 if (d < 1
e-122) exp -= 128, d *= 1e128;
389 if (d < 1
e-58) exp -= 64, d *= 1e64;
390 if (d < 1
e-26) exp -= 32, d *= 1e32;
391 if (d < 1
e-10) exp -= 16, d *= 1e16;
392 if (d < 1
e-2) exp -= 8, d *= 1e8;
393 if (d < 1
e+2) exp -= 4, d *= 1e4;
394 if (d < 1
e+4) exp -= 2, d *= 1e2;
395 if (d < 1
e+5) exp -= 1, d *= 1e1;
408 if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
414 dddddd =
static_cast<int>(d64k / 65536);
419 double m = std::frexp(
value, &exp2);
438 std::pair<uint64_t, uint64_t> edge, val;
443 edge =
PowFive(2 * dddddd + 1, exp - 5);
451 edge =
PowFive(2 * dddddd + 1, 0);
459 }
else if (val == edge) {
460 dddddd += (dddddd & 1);
464 dddddd =
static_cast<int>((d64k + 32768) / 65536);
466 if (dddddd == 1000000) {
470 exp_dig.exponent = exp;
472 int two_digits = dddddd / 10000;
473 dddddd -= two_digits * 10000;
476 two_digits = dddddd / 100;
477 dddddd -= two_digits * 100;
487 static_assert(std::numeric_limits<float>::is_iec559,
488 "IEEE-754/IEC-559 support only");
499 if (std::signbit(d)) *
out++ =
'-';
514 int exp = exp_dig.exponent;
515 const char* digits = exp_dig.digits;
525 if (digits[5] !=
'0') {
533 if ((digits[5] | digits[4]) !=
'0') {
536 if (digits[5] !=
'0') *
out++ = digits[5];
545 while (
out[-1] ==
'0') --
out;
546 if (
out[-1] ==
'.') --
out;
554 while (
out[-1] ==
'0') --
out;
555 if (
out[-1] ==
'.') --
out;
563 while (
out[-1] ==
'0') --
out;
564 if (
out[-1] ==
'.') --
out;
583 while (
out[-1] ==
'0') --
out;
587 assert(exp < -4 || exp >= 6);
589 assert(
out[1] ==
'.');
592 while (
out[-1] ==
'0') --
out;
593 if (
out[-1] ==
'.') --
out;
602 int dig1 = exp / 100;
617 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, 0, 1, 2, 3, 4, 5,
620 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17,
621 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
622 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
623 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36,
624 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
625 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
626 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
627 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
628 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
629 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
630 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36};
635 bool* negative_ptr ) {
636 if (
text->data() ==
nullptr) {
642 int base = *base_ptr;
656 *negative_ptr = (
start[0] ==
'-');
657 if (*negative_ptr ||
start[0] ==
'+') {
683 }
else if (
base == 16) {
692 }
else if (
base >= 2 &&
base <= 36) {
731 template <
typename IntType>
732 struct LookupTables {
740 #define X_OVER_BASE_INITIALIZER(X) \
742 0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \
743 X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18, \
744 X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26, \
745 X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34, \
761 const uint128 LookupTables<uint128>::kVmaxOverBase[] = {
813 const int128 LookupTables<int128>::kVmaxOverBase[] = {
816 MakeInt128(4611686018427387903, 18446744073709551615u),
817 MakeInt128(3074457345618258602, 12297829382473034410u),
818 MakeInt128(2305843009213693951, 18446744073709551615u),
819 MakeInt128(1844674407370955161, 11068046444225730969u),
820 MakeInt128(1537228672809129301, 6148914691236517205u),
821 MakeInt128(1317624576693539401, 2635249153387078802u),
822 MakeInt128(1152921504606846975, 18446744073709551615u),
823 MakeInt128(1024819115206086200, 16397105843297379214u),
824 MakeInt128(922337203685477580, 14757395258967641292u),
825 MakeInt128(838488366986797800, 13415813871788764811u),
826 MakeInt128(768614336404564650, 12297829382473034410u),
827 MakeInt128(709490156681136600, 11351842506898185609u),
828 MakeInt128(658812288346769700, 10540996613548315209u),
829 MakeInt128(614891469123651720, 9838263505978427528u),
830 MakeInt128(576460752303423487, 18446744073709551615u),
831 MakeInt128(542551296285575047, 9765923333140350855u),
832 MakeInt128(512409557603043100, 8198552921648689607u),
833 MakeInt128(485440633518672410, 17475862806672206794u),
834 MakeInt128(461168601842738790, 7378697629483820646u),
835 MakeInt128(439208192231179800, 7027331075698876806u),
836 MakeInt128(419244183493398900, 6707906935894382405u),
837 MakeInt128(401016175515425035, 2406097053092550210u),
838 MakeInt128(384307168202282325, 6148914691236517205u),
839 MakeInt128(368934881474191032, 5902958103587056517u),
840 MakeInt128(354745078340568300, 5675921253449092804u),
841 MakeInt128(341606371735362066, 17763531330238827482u),
842 MakeInt128(329406144173384850, 5270498306774157604u),
843 MakeInt128(318047311615681924, 7633135478776366185u),
844 MakeInt128(307445734561825860, 4919131752989213764u),
845 MakeInt128(297528130221121800, 4760450083537948804u),
846 MakeInt128(288230376151711743, 18446744073709551615u),
847 MakeInt128(279496122328932600, 4471937957262921603u),
848 MakeInt128(271275648142787523, 14106333703424951235u),
849 MakeInt128(263524915338707880, 4216398645419326083u),
850 MakeInt128(256204778801521550, 4099276460824344803u),
866 const int128 LookupTables<int128>::kVminOverBase[] = {
870 MakeInt128(-3074457345618258603, 6148914691236517206u),
872 MakeInt128(-1844674407370955162, 7378697629483820647u),
873 MakeInt128(-1537228672809129302, 12297829382473034411u),
874 MakeInt128(-1317624576693539402, 15811494920322472814u),
876 MakeInt128(-1024819115206086201, 2049638230412172402u),
877 MakeInt128(-922337203685477581, 3689348814741910324u),
878 MakeInt128(-838488366986797801, 5030930201920786805u),
879 MakeInt128(-768614336404564651, 6148914691236517206u),
880 MakeInt128(-709490156681136601, 7094901566811366007u),
881 MakeInt128(-658812288346769701, 7905747460161236407u),
882 MakeInt128(-614891469123651721, 8608480567731124088u),
884 MakeInt128(-542551296285575048, 8680820740569200761u),
885 MakeInt128(-512409557603043101, 10248191152060862009u),
886 MakeInt128(-485440633518672411, 970881267037344822u),
887 MakeInt128(-461168601842738791, 11068046444225730970u),
888 MakeInt128(-439208192231179801, 11419412998010674810u),
889 MakeInt128(-419244183493398901, 11738837137815169211u),
890 MakeInt128(-401016175515425036, 16040647020617001406u),
891 MakeInt128(-384307168202282326, 12297829382473034411u),
892 MakeInt128(-368934881474191033, 12543785970122495099u),
893 MakeInt128(-354745078340568301, 12770822820260458812u),
894 MakeInt128(-341606371735362067, 683212743470724134u),
895 MakeInt128(-329406144173384851, 13176245766935394012u),
896 MakeInt128(-318047311615681925, 10813608594933185431u),
897 MakeInt128(-307445734561825861, 13527612320720337852u),
898 MakeInt128(-297528130221121801, 13686293990171602812u),
900 MakeInt128(-279496122328932601, 13974806116446630013u),
901 MakeInt128(-271275648142787524, 4340410370284600381u),
902 MakeInt128(-263524915338707881, 14230345428290225533u),
903 MakeInt128(-256204778801521551, 14347467612885206813u),
906 template <
typename IntType>
907 const IntType LookupTables<IntType>::kVmaxOverBase[] =
910 template <
typename IntType>
911 const IntType LookupTables<IntType>::kVminOverBase[] =
914 #undef X_OVER_BASE_INITIALIZER
916 template <
typename IntType>
923 assert(vmax >=
static_cast<IntType
>(
base));
924 const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[
base];
931 unsigned char c =
static_cast<unsigned char>(
start[0]);
937 if (
value > vmax_over_base) {
942 if (
value > vmax - digit) {
952 template <
typename IntType>
958 assert(vmin <= 0 -
base);
959 IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[
base];
967 if (vmin %
base > 0) {
974 unsigned char c =
static_cast<unsigned char>(
start[0]);
980 if (
value < vmin_over_base) {
985 if (
value < vmin + digit) {
997 template <
typename IntType>
1012 template <
typename IntType>
1024 namespace numbers_internal {
1031 "000102030405060708090a0b0c0d0e0f"
1032 "101112131415161718191a1b1c1d1e1f"
1033 "202122232425262728292a2b2c2d2e2f"
1034 "303132333435363738393a3b3c3d3e3f"
1035 "404142434445464748494a4b4c4d4e4f"
1036 "505152535455565758595a5b5c5d5e5f"
1037 "606162636465666768696a6b6c6d6e6f"
1038 "707172737475767778797a7b7c7d7e7f"
1039 "808182838485868788898a8b8c8d8e8f"
1040 "909192939495969798999a9b9c9d9e9f"
1041 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
1042 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
1043 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
1044 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
1045 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
1046 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
1049 {
'0',
'0'}, {
'0',
'1'}, {
'0',
'2'}, {
'0',
'3'}, {
'0',
'4'}, {
'0',
'5'},
1050 {
'0',
'6'}, {
'0',
'7'}, {
'0',
'8'}, {
'0',
'9'}, {
'1',
'0'}, {
'1',
'1'},
1051 {
'1',
'2'}, {
'1',
'3'}, {
'1',
'4'}, {
'1',
'5'}, {
'1',
'6'}, {
'1',
'7'},
1052 {
'1',
'8'}, {
'1',
'9'}, {
'2',
'0'}, {
'2',
'1'}, {
'2',
'2'}, {
'2',
'3'},
1053 {
'2',
'4'}, {
'2',
'5'}, {
'2',
'6'}, {
'2',
'7'}, {
'2',
'8'}, {
'2',
'9'},
1054 {
'3',
'0'}, {
'3',
'1'}, {
'3',
'2'}, {
'3',
'3'}, {
'3',
'4'}, {
'3',
'5'},
1055 {
'3',
'6'}, {
'3',
'7'}, {
'3',
'8'}, {
'3',
'9'}, {
'4',
'0'}, {
'4',
'1'},
1056 {
'4',
'2'}, {
'4',
'3'}, {
'4',
'4'}, {
'4',
'5'}, {
'4',
'6'}, {
'4',
'7'},
1057 {
'4',
'8'}, {
'4',
'9'}, {
'5',
'0'}, {
'5',
'1'}, {
'5',
'2'}, {
'5',
'3'},
1058 {
'5',
'4'}, {
'5',
'5'}, {
'5',
'6'}, {
'5',
'7'}, {
'5',
'8'}, {
'5',
'9'},
1059 {
'6',
'0'}, {
'6',
'1'}, {
'6',
'2'}, {
'6',
'3'}, {
'6',
'4'}, {
'6',
'5'},
1060 {
'6',
'6'}, {
'6',
'7'}, {
'6',
'8'}, {
'6',
'9'}, {
'7',
'0'}, {
'7',
'1'},
1061 {
'7',
'2'}, {
'7',
'3'}, {
'7',
'4'}, {
'7',
'5'}, {
'7',
'6'}, {
'7',
'7'},
1062 {
'7',
'8'}, {
'7',
'9'}, {
'8',
'0'}, {
'8',
'1'}, {
'8',
'2'}, {
'8',
'3'},
1063 {
'8',
'4'}, {
'8',
'5'}, {
'8',
'6'}, {
'8',
'7'}, {
'8',
'8'}, {
'8',
'9'},
1064 {
'9',
'0'}, {
'9',
'1'}, {
'9',
'2'}, {
'9',
'3'}, {
'9',
'4'}, {
'9',
'5'},
1065 {
'9',
'6'}, {
'9',
'7'}, {
'9',
'8'}, {
'9',
'9'}};
1088 return safe_uint_internal<absl::uint128>(
text,
value,
base);