├── .gitignore ├── .package ├── ryu.h ├── LICENSE ├── README.md ├── test.c └── ryu.c /.gitignore: -------------------------------------------------------------------------------- 1 | *.dSYM 2 | a.out 3 | .vscode -------------------------------------------------------------------------------- /.package: -------------------------------------------------------------------------------- 1 | file ryu.c 2 | file ryu.h 3 | -------------------------------------------------------------------------------- /ryu.h: -------------------------------------------------------------------------------- 1 | #ifndef RYU_H 2 | #define RYU_H 3 | 4 | #include 5 | 6 | // ryu_string converts a double into a string representation that is copied 7 | // into the provided C string buffer. 8 | // 9 | // Returns the number of characters, not including the null-terminator, needed 10 | // to store the double into the C string buffer. 11 | // If the returned length is greater than nbytes-1, then only a parital copy 12 | // occurred. 13 | // 14 | // The format is one of 15 | // 'e' (-d.ddddedd, a decimal exponent) 16 | // 'E' (-d.ddddEdd, a decimal exponent) 17 | // 'f' (-ddd.dddd, no exponent) 18 | // 'g' ('e' for large exponents, 'f' otherwise) 19 | // 'G' ('E' for large exponents, 'f' otherwise) 20 | // 'j' ('g' for large exponents, 'f' otherwise) (matches javascript format) 21 | // 'J' ('G' for large exponents, 'f' otherwise) (matches javascript format) 22 | size_t ryu_string(double d, char fmt, char dst[], size_t nbytes); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2023 Josh Baker 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | *This library is defunct. Consider using [tidwall/fp](https://github.com/tidwall/fp) instead.* 2 | 3 | # ryu 4 | 5 | Convert floating point numbers to strings in their shortest, most accurate 6 | representation. 7 | 8 | This implementation consists mostly of the code taken directly from the 9 | original work in the [ulfjack/ryu](https://github.com/ulfjack/ryu) project, 10 | with the additon of a new `ryu_string` function, that provides a little extra 11 | safety and convenience. 12 | 13 | Also, this library is a single self-contained C file for easily adding to 14 | exising projects. 15 | 16 | ## Usage 17 | 18 | ```C 19 | // ryu_string converts a double into a string representation that is copied 20 | // into the provided C string buffer. 21 | // 22 | // Returns the number of characters, not including the null-terminator, needed 23 | // to store the double into the C string buffer. 24 | // If the returned length is greater than nbytes-1, then only a parital copy 25 | // occurred. 26 | // 27 | // The format is one of 28 | // 'e' (-d.ddddedd, a decimal exponent) 29 | // 'E' (-d.ddddEdd, a decimal exponent) 30 | // 'f' (-ddd.dddd, no exponent) 31 | // 'g' ('e' for large exponents, 'f' otherwise) 32 | // 'G' ('E' for large exponents, 'f' otherwise) 33 | // 'j' ('g' for large exponents, 'f' otherwise) (matches javascript format) 34 | // 'J' ('G' for large exponents, 'f' otherwise) (matches javascript format) 35 | size_t ryu_string(double d, char fmt, char *dst, size_t nbytes) 36 | ``` 37 | 38 | ## Example 39 | 40 | ```C 41 | char buf[32]; 42 | size_t n = ryu_string(-112.89123883, 'f', buf, sizeof(buf)); 43 | if (n >= sizeof(buf)) { 44 | // Buffer is too small to store the floating point as a string. 45 | } 46 | printf("%s\n", buf); 47 | 48 | // Output: -112.89123883 49 | ``` 50 | 51 | ## License 52 | 53 | Code from the original [ulfjack/ryu](https://github.com/ulfjack/ryu) project: 54 | 55 | ``` 56 | Copyright 2018 Ulf Adams 57 | 58 | The contents of this file may be used under the terms of the Apache License, 59 | Version 2.0. 60 | 61 | (See accompanying file LICENSE-Apache or copy at 62 | http://www.apache.org/licenses/LICENSE-2.0) 63 | 64 | Alternatively, the contents of this file may be used under the terms of 65 | the Boost Software License, Version 1.0. 66 | (See accompanying file LICENSE-Boost or copy at 67 | https://www.boost.org/LICENSE_1_0.txt) 68 | 69 | Unless required by applicable law or agreed to in writing, this software 70 | is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 71 | KIND, either express or implied. 72 | ``` 73 | 74 | The `ryu_string` function: 75 | 76 | ``` 77 | Copyright (c) 2023 Josh Baker 78 | 79 | Permission is hereby granted, free of charge, to any person obtaining a copy of 80 | this software and associated documentation files (the "Software"), to deal in 81 | the Software without restriction, including without limitation the rights to 82 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 83 | the Software, and to permit persons to whom the Software is furnished to do so, 84 | subject to the following conditions: 85 | 86 | The above copyright notice and this permission notice shall be included in all 87 | copies or substantial portions of the Software. 88 | 89 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 90 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 91 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 92 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 93 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 94 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 95 | ``` 96 | -------------------------------------------------------------------------------- /test.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "ryu.h" 8 | 9 | #define test(fmt, input, expected) { \ 10 | char buf[256]; \ 11 | ryu_string((input), (fmt), buf, sizeof(buf)); \ 12 | if (strcmp(buf, (expected)) != 0) { \ 13 | fprintf(stderr, "line %d: expected %s, got %s\n", \ 14 | __LINE__, (expected), buf); \ 15 | exit(1); \ 16 | } \ 17 | } 18 | 19 | #define test_j(input) { \ 20 | char buf0[256]; \ 21 | strcpy(buf0, #input); \ 22 | if (buf0[strlen(buf0)-2] == '.') { \ 23 | buf0[strlen(buf0)-2] = '\0'; \ 24 | test('j', input, buf0); \ 25 | test('J', input, buf0); \ 26 | } else { \ 27 | char *e = strchr(buf0, 'e'); \ 28 | if (!e) e = strchr(buf0, 'E'); \ 29 | assert(e); \ 30 | *e = 'e'; \ 31 | test('j', input, buf0); \ 32 | *e = 'E'; \ 33 | test('J', input, buf0); \ 34 | } \ 35 | } 36 | 37 | int main(void) { 38 | // test_j(5.307740298202583E+22); 39 | // // test('j', 5.307740298202583E+22, "5.307740298202583E+22"); 40 | // // test('J', 5.307740298202583E+22, "5.307740298202583E+22"); 41 | 42 | // return 0; 43 | 44 | 45 | test('f', 212123123.123188832, "212123123.12318882"); 46 | test('e', 212123123.123188832, "2.1212312312318882e8"); 47 | test('E', 212123123.123188832, "2.1212312312318882E8"); 48 | test('f', 9223372036854775808.0, "9223372036854776000"); 49 | test('f', 0.000123123001, "0.000123123001"); 50 | test('f', 1.3441331, "1.3441331"); 51 | test('f', 1.0, "1"); 52 | test('f', -1.0, "-1"); 53 | test('f', -0.0, "-0"); 54 | test('f', 0.5, "0.5"); 55 | test('f', -0.5, "-0.5"); 56 | test('f', 0.0, "0"); 57 | test('f', -0.0, "-0"); 58 | test('f', 0.5, "0.5"); 59 | test('f', -0.5, "-0.5"); 60 | test('f', 0.0, "0"); 61 | test('f', -0.01, "-0.01"); 62 | test('f', -0.015, "-0.015"); 63 | test('f', 5000.0, "5000"); 64 | test('f', 0000.0, "0"); 65 | test('f', -0000.0, "-0"); 66 | test('f', 5123.0, "5123"); 67 | test('f', 5000000000000000000.0, "5000000000000000000"); 68 | test('f', -0.00000000000000005, "-0.00000000000000005"); 69 | char buf[32]; 70 | size_t n1 = ryu_string(-112.89123883, 'f', buf, sizeof(buf)); 71 | assert(strcmp(buf, "-112.89123883") == 0); 72 | size_t n2 = ryu_string(-112.89123883, 'f', NULL, 0); 73 | assert(n1 == n2); 74 | size_t n3 = ryu_string(-112.89123883, 'f', buf, 5); 75 | assert(n3 == n2); 76 | assert(strcmp(buf, "-112") == 0); 77 | ryu_string(-112.89123883, 'f', buf, 1); 78 | assert(strcmp(buf, "") == 0); 79 | ryu_string(-112.89123883, 'f', buf, 2); 80 | assert(strcmp(buf, "-") == 0); 81 | ryu_string(-112.89123883, 'f', buf, 6); 82 | assert(strcmp(buf, "-112.") == 0); 83 | test('g', -0.01, "-0.01"); 84 | test('f', 5000000000000000000.0, "5000000000000000000"); 85 | test('e', 5000000000000000000.0, "5e18"); 86 | test('g', 5000000000000000000.0, "5e18"); 87 | test('j', 5000000000000000000.0, "5000000000000000000"); 88 | test('f', 50000000000000000001.0, "50000000000000000000"); 89 | test('e', 50000000000000000001.0, "5e19"); 90 | test('g', 50000000000000000001.0, "5e19"); 91 | test('j', 50000000000000000001.0, "50000000000000000000"); 92 | test('f', 500000000000000000011.0, "500000000000000000000"); 93 | test('e', 500000000000000000011.0, "5e20"); 94 | test('g', 500000000000000000011.0, "5e20"); 95 | test('j', 500000000000000000011.0, "500000000000000000000"); 96 | test('f', -500000000000000000011.0, "-500000000000000000000"); 97 | test('e', -500000000000000000011.0, "-5e20"); 98 | test('g', -500000000000000000011.0, "-5e20"); 99 | test('j', -500000000000000000011.0, "-500000000000000000000"); 100 | test('f', 5000000000000000000111.0, "5000000000000000000000"); 101 | test('e', 5000000000000000000111.0, "5e21"); 102 | test('g', 5000000000000000000111.0, "5e21"); 103 | test('j', 5000000000000000000111.0, "5e+21"); 104 | test('f', -5000000000000000000111.0, "-5000000000000000000000"); 105 | test('e', -5000000000000000000111.0, "-5e21"); 106 | test('g', -5000000000000000000111.0, "-5e21"); 107 | test('j', -5000000000000000000111.0, "-5e+21"); 108 | test('f', 5000, "5000"); 109 | test('g', 5000, "5e3"); 110 | test('f', 500, "500"); 111 | test('g', 500, "500"); 112 | test('g', 500.1, "500.1"); 113 | test('e', 500.1123, "5.001123e2"); 114 | test('g', 500.1123, "500.1123"); 115 | test('e', 500.1123890123, "5.001123890123e2"); 116 | test('g', 500.1123890123, "500.1123890123"); 117 | test('e', 500123.1123890123, "5.001231123890123e5"); 118 | test('g', 500123.1123890123, "500123.1123890123"); 119 | test('e', -500123.1123890123, "-5.001231123890123e5"); 120 | test('g', -500123.1123890123, "-500123.1123890123"); 121 | test('e', 5001237.1123890123, "5.001237112389012e6"); 122 | test('g', 5001237.1123890123, "5001237.112389012"); 123 | test('e', 5001237910.1123890123, "5.001237910112389e9"); 124 | test('g', 5001237910.1123890123, "5001237910.112389"); 125 | test('e', 50012379109182039182.1123890123, "5.001237910918204e19"); 126 | test('g', 50012379109182039182.1123890123, "50012379109182040000"); 127 | test('e', 8888888899999999999999999.0, "8.8888889e24"); 128 | test('g', 8888888899999999999999999.0, "8.8888889e24"); 129 | 130 | test_j(35499548951617827000.0); 131 | test_j(2.1622774220303696e+23); 132 | test_j(426169229454865500000.0); 133 | test_j(1.1122428779605864e+22); 134 | test_j(208689857201900700000.0); 135 | test_j(9.215635951632788e+21); 136 | test_j(394363363386584860000.0); 137 | test_j(1.051552966719607e+21); 138 | test_j(619416341368231300000.0); 139 | test_j(1.855027370624036e+21); 140 | test_j(104781779522236330000.0); 141 | test_j(1.0142892004126468e+21); 142 | test_j(109047775277613840000.0); 143 | test_j(1.0842409975308099e+21); 144 | test_j(9195049728998015000.0); 145 | test_j(4.778131141654174e+21); 146 | test_j(62529739735412620000.0); 147 | test_j(1.1320063478633598e+21); 148 | test_j(196145233329870600000.0); 149 | test_j(1.8174708505162005e+21); 150 | test_j(373792539196554600000.0); 151 | test_j(3.0212836059446253e+21); 152 | test_j(72267036244919420000.0); 153 | test_j(2.664955831891294e+22); 154 | test_j(52935469346551820000.0); 155 | test_j(1.2946386961259915e+21); 156 | test_j(170603629769678500000.0); 157 | test_j(6.60526142584568e+21); 158 | test_j(42880714092059600000.0); 159 | test_j(2.362835377916814e+21); 160 | test_j(231014740272716150000.0); 161 | test_j(1.3022371556436764e+21); 162 | test_j(244316357434306170000.0); 163 | test_j(2.116436806095811e+21); 164 | test_j(5602693290733586000.0); 165 | test_j(1.2231536651405277e+22); 166 | test_j(15363133420173502000.0); 167 | test_j(7.964194881065025e+23); 168 | test_j(3681986271578664000.0); 169 | test_j(5.875116361313629e+22); 170 | test_j(115497775565041290000.0); 171 | test_j(1.5494985062998304e+21); 172 | test_j(3.332693432432603e+22); 173 | test_j(426464718456790000000.0); 174 | test_j(1.7226097008801732e+22); 175 | test_j(69957914410743990000.0); 176 | test_j(3.42160911381865e+22); 177 | test_j(1688019858430556000.0); 178 | test_j(1.9347549418669543e+22); 179 | test_j(733425086581656500000.0); 180 | test_j(2.0748775604932917e+22); 181 | test_j(147009784738754330000.0); 182 | test_j(2.086247207898731e+23); 183 | test_j(987306907069234300000.0); 184 | test_j(1.489134275661723e+21); 185 | test_j(629085397883884400000.0); 186 | test_j(1.5752300766133504e+22); 187 | test_j(987306907069234300000.0); 188 | test_j(1.095564255637569e+21); 189 | test_j(836056326897382300000.0); 190 | test_j(5.556055204583265e+21); 191 | test_j(13152608998628676000.0); 192 | test_j(2.6772291512932968e+23); 193 | test_j(987306907069234300000.0); 194 | test_j(2.2811913809996984e+21); 195 | test_j(29738866232455856000.0); 196 | test_j(3.8228990573369313e+21); 197 | test_j(76926234432060640000.0); 198 | test_j(3.746067584976078e+22); 199 | test_j(987306907069234300000.0); 200 | test_j(2.958850611740982e+22); 201 | test_j(717386127925367700000.0); 202 | test_j(2.520095981091263e+23); 203 | test_j(987306907069234300000.0); 204 | test_j(1.2382226889099641e+22); 205 | test_j(68438623369146106000.0); 206 | test_j(1.3567015796774057e+23); 207 | test_j(56555442318894600000.0); 208 | test_j(1.11167738462246e+22); 209 | test_j(28021174372971598000.0); 210 | test_j(2.467622844932717e+22); 211 | test_j(540993400866745800000.0); 212 | test_j(1.2490950941404075e+22); 213 | test_j(77500474384390620000.0); 214 | test_j(2.8991807958122163e+22); 215 | test_j(253113508231971300000.0); 216 | test_j(5.307740298202583E+22); 217 | test_j(186872634142835570000.0); 218 | 219 | return 0; 220 | } 221 | -------------------------------------------------------------------------------- /ryu.c: -------------------------------------------------------------------------------- 1 | // The "ryu_print" function is a lightweight wrapper around the original 2 | // ryu d2s_buffered function. 3 | 4 | // https://github.com/tidwall/ryu 5 | // 6 | // Copyright 2023 Joshua J Baker. All rights reserved. 7 | // Use of this source code is governed by an MIT-style 8 | // license that can be found in the LICENSE file. 9 | // 10 | 11 | // https://github.com/ulfjack/ryu 12 | // 13 | // Copyright 2018 Ulf Adams 14 | // 15 | // The contents of this file may be used under the terms of the Apache License, 16 | // Version 2.0. 17 | // 18 | // (See accompanying file LICENSE-Apache or copy at 19 | // http://www.apache.org/licenses/LICENSE-2.0) 20 | // 21 | // Alternatively, the contents of this file may be used under the terms of 22 | // the Boost Software License, Version 1.0. 23 | // (See accompanying file LICENSE-Boost or copy at 24 | // https://www.boost.org/LICENSE_1_0.txt) 25 | // 26 | // Unless required by applicable law or agreed to in writing, this software 27 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 28 | // KIND, either express or implied. 29 | 30 | // Runtime compiler options: 31 | // -DRYU_DEBUG Generate verbose debugging output to stdout. 32 | // 33 | // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower, 34 | // depending on your compiler. 35 | // 36 | // -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every 37 | // required power of 5, only store every 26th entry, and compute 38 | // intermediate values with a multiplication. This reduces the lookup table 39 | // size by about 10x (only one case, and only double) at the cost of some 40 | // performance. Currently requires MSVC intrinsics. 41 | 42 | #include 43 | #include 44 | #include 45 | #include 46 | #include 47 | #include 48 | #include 49 | 50 | #ifdef RYU_STATIC 51 | #define RYU_EXTERN static 52 | #endif 53 | 54 | #ifndef RYU_EXTERN 55 | #define RYU_EXTERN 56 | #endif 57 | 58 | #ifdef RYU_DEBUG 59 | #include 60 | #include 61 | #endif 62 | 63 | #if defined(_M_IX86) || defined(_M_ARM) 64 | #define RYU_32_BIT_PLATFORM 65 | #endif 66 | 67 | // Returns e == 0 ? 1 : ceil(log_2(5^e)); requires 0 <= e <= 3528. 68 | static inline int32_t pow5bits(const int32_t e) { 69 | // This approximation works up to the point that the multiplication 70 | // overflows at e = 3529. 71 | // If the multiplication were done in 64 bits, it would fail at 5^4004 72 | // which is just greater than 2^9297. 73 | assert(e >= 0); 74 | assert(e <= 3528); 75 | return (int32_t) (((((uint32_t) e) * 1217359) >> 19) + 1); 76 | } 77 | 78 | // Returns floor(log_10(2^e)); requires 0 <= e <= 1650. 79 | static inline uint32_t log10Pow2(const int32_t e) { 80 | // The first value this approximation fails for is 2^1651 which is just 81 | // greater than 10^297. 82 | assert(e >= 0); 83 | assert(e <= 1650); 84 | return (((uint32_t) e) * 78913) >> 18; 85 | } 86 | 87 | // Returns floor(log_10(5^e)); requires 0 <= e <= 2620. 88 | static inline uint32_t log10Pow5(const int32_t e) { 89 | // The first value this approximation fails for is 5^2621 which is just 90 | // greater than 10^1832. 91 | assert(e >= 0); 92 | assert(e <= 2620); 93 | return (((uint32_t) e) * 732923) >> 20; 94 | } 95 | 96 | static inline int copy_special_str(char * const result, const bool sign, 97 | const bool exponent, const bool mantissa) 98 | { 99 | if (mantissa) { 100 | memcpy(result, "NaN", 3); 101 | return 3; 102 | } 103 | if (sign) { 104 | result[0] = '-'; 105 | } 106 | if (exponent) { 107 | memcpy(result + sign, "Infinity", 8); 108 | return sign + 8; 109 | } 110 | memcpy(result + sign, "0E0", 3); 111 | return sign + 3; 112 | } 113 | 114 | static inline uint64_t double_to_bits(const double d) { 115 | uint64_t bits = 0; 116 | memcpy(&bits, &d, sizeof(double)); 117 | return bits; 118 | } 119 | 120 | // A table of all two-digit numbers. This is used to speed up decimal digit 121 | // generation by copying pairs of digits into the final output. 122 | static const char DIGIT_TABLE[200] = { 123 | '0','0','0','1','0','2','0','3','0','4','0','5','0','6','0','7','0','8', 124 | '0','9','1','0','1','1','1','2','1','3','1','4','1','5','1','6','1','7', 125 | '1','8','1','9','2','0','2','1','2','2','2','3','2','4','2','5','2','6', 126 | '2','7','2','8','2','9','3','0','3','1','3','2','3','3','3','4','3','5', 127 | '3','6','3','7','3','8','3','9','4','0','4','1','4','2','4','3','4','4', 128 | '4','5','4','6','4','7','4','8','4','9','5','0','5','1','5','2','5','3', 129 | '5','4','5','5','5','6','5','7','5','8','5','9','6','0','6','1','6','2', 130 | '6','3','6','4','6','5','6','6','6','7','6','8','6','9','7','0','7','1', 131 | '7','2','7','3','7','4','7','5','7','6','7','7','7','8','7','9','8','0', 132 | '8','1','8','2','8','3','8','4','8','5','8','6','8','7','8','8','8','9', 133 | '9','0','9','1','9','2','9','3','9','4','9','5','9','6','9','7','9','8', 134 | '9','9', 135 | }; 136 | 137 | // Defines RYU_32_BIT_PLATFORM if applicable. 138 | 139 | // ABSL avoids uint128_t on Win32 even if __SIZEOF_INT128__ is defined. 140 | // Let's do the same for now. 141 | #if defined(__SIZEOF_INT128__) && !defined(_MSC_VER) && \ 142 | !defined(RYU_ONLY_64_BIT_OPS) 143 | #define HAS_UINT128 144 | #elif defined(_MSC_VER) && !defined(RYU_ONLY_64_BIT_OPS) && defined(_M_X64) 145 | #define HAS_64_BIT_INTRINSICS 146 | #endif 147 | 148 | #if defined(HAS_UINT128) 149 | typedef __uint128_t uint128_t; 150 | #endif 151 | 152 | #if defined(HAS_64_BIT_INTRINSICS) 153 | 154 | #include 155 | 156 | static inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* 157 | const productHi) 158 | { 159 | return _umul128(a, b, productHi); 160 | } 161 | 162 | // Returns the lower 64 bits of (hi*2^64 + lo) >> dist, with 0 < dist < 64. 163 | static inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, 164 | const uint32_t dist) 165 | { 166 | // For the __shiftright128 intrinsic, the shift value is always 167 | // modulo 64. 168 | // In the current implementation of the double-precision version 169 | // of Ryu, the shift value is always < 64. (In the case 170 | // RYU_OPTIMIZE_SIZE == 0, the shift value is in the range [49, 58]. 171 | // Otherwise in the range [2, 59].) 172 | // However, this function is now also called by s2d, which requires 173 | // supporting the larger shift range (TODO: what is the actual range?). 174 | // Check this here in case a future change requires larger shift 175 | // values. In this case this function needs to be adjusted. 176 | assert(dist < 64); 177 | return __shiftright128(lo, hi, (unsigned char) dist); 178 | } 179 | 180 | #else // defined(HAS_64_BIT_INTRINSICS) 181 | 182 | static inline uint64_t umul128(const uint64_t a, const uint64_t b, uint64_t* 183 | const productHi) 184 | { 185 | // The casts here help MSVC to avoid calls to the __allmul library function. 186 | const uint32_t aLo = (uint32_t)a; 187 | const uint32_t aHi = (uint32_t)(a >> 32); 188 | const uint32_t bLo = (uint32_t)b; 189 | const uint32_t bHi = (uint32_t)(b >> 32); 190 | 191 | const uint64_t b00 = (uint64_t)aLo * bLo; 192 | const uint64_t b01 = (uint64_t)aLo * bHi; 193 | const uint64_t b10 = (uint64_t)aHi * bLo; 194 | const uint64_t b11 = (uint64_t)aHi * bHi; 195 | 196 | const uint32_t b00Lo = (uint32_t)b00; 197 | const uint32_t b00Hi = (uint32_t)(b00 >> 32); 198 | 199 | const uint64_t mid1 = b10 + b00Hi; 200 | const uint32_t mid1Lo = (uint32_t)(mid1); 201 | const uint32_t mid1Hi = (uint32_t)(mid1 >> 32); 202 | 203 | const uint64_t mid2 = b01 + mid1Lo; 204 | const uint32_t mid2Lo = (uint32_t)(mid2); 205 | const uint32_t mid2Hi = (uint32_t)(mid2 >> 32); 206 | 207 | const uint64_t pHi = b11 + mid1Hi + mid2Hi; 208 | const uint64_t pLo = ((uint64_t)mid2Lo << 32) | b00Lo; 209 | 210 | *productHi = pHi; 211 | return pLo; 212 | } 213 | 214 | static inline uint64_t shiftright128(const uint64_t lo, const uint64_t hi, 215 | const uint32_t dist) 216 | { 217 | // We don't need to handle the case dist >= 64 here (see above). 218 | assert(dist < 64); 219 | assert(dist > 0); 220 | return (hi << (64 - dist)) | (lo >> dist); 221 | } 222 | 223 | #endif // defined(HAS_64_BIT_INTRINSICS) 224 | 225 | #if defined(RYU_32_BIT_PLATFORM) 226 | 227 | // Returns the high 64 bits of the 128-bit product of a and b. 228 | static inline uint64_t umulh(const uint64_t a, const uint64_t b) { 229 | // Reuse the umul128 implementation. 230 | // Optimizers will likely eliminate the instructions used to compute the 231 | // low part of the product. 232 | uint64_t hi; 233 | umul128(a, b, &hi); 234 | return hi; 235 | } 236 | 237 | // On 32-bit platforms, compilers typically generate calls to library 238 | // functions for 64-bit divisions, even if the divisor is a constant. 239 | // 240 | // E.g.: 241 | // https://bugs.llvm.org/show_bug.cgi?id=37932 242 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=17958 243 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=37443 244 | // 245 | // The functions here perform division-by-constant using multiplications 246 | // in the same way as 64-bit compilers would do. 247 | // 248 | // NB: 249 | // The multipliers and shift values are the ones generated by clang x64 250 | // for expressions like x/5, x/10, etc. 251 | 252 | static inline uint64_t div5(const uint64_t x) { 253 | return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 2; 254 | } 255 | 256 | static inline uint64_t div10(const uint64_t x) { 257 | return umulh(x, 0xCCCCCCCCCCCCCCCDu) >> 3; 258 | } 259 | 260 | static inline uint64_t div100(const uint64_t x) { 261 | return umulh(x >> 2, 0x28F5C28F5C28F5C3u) >> 2; 262 | } 263 | 264 | static inline uint64_t div1e8(const uint64_t x) { 265 | return umulh(x, 0xABCC77118461CEFDu) >> 26; 266 | } 267 | 268 | static inline uint64_t div1e9(const uint64_t x) { 269 | return umulh(x >> 9, 0x44B82FA09B5A53u) >> 11; 270 | } 271 | 272 | static inline uint32_t mod1e9(const uint64_t x) { 273 | // Avoid 64-bit math as much as possible. 274 | // Returning (uint32_t) (x - 1000000000 * div1e9(x)) would 275 | // perform 32x64-bit multiplication and 64-bit subtraction. 276 | // x and 1000000000 * div1e9(x) are guaranteed to differ by 277 | // less than 10^9, so their highest 32 bits must be identical, 278 | // so we can truncate both sides to uint32_t before subtracting. 279 | // We can also simplify (uint32_t) (1000000000 * div1e9(x)). 280 | // We can truncate before multiplying instead of after, as multiplying 281 | // the highest 32 bits of div1e9(x) can't affect the lowest 32 bits. 282 | return ((uint32_t) x) - 1000000000 * ((uint32_t) div1e9(x)); 283 | } 284 | 285 | #else // defined(RYU_32_BIT_PLATFORM) 286 | 287 | static inline uint64_t div5(const uint64_t x) { 288 | return x / 5; 289 | } 290 | 291 | static inline uint64_t div10(const uint64_t x) { 292 | return x / 10; 293 | } 294 | 295 | static inline uint64_t div100(const uint64_t x) { 296 | return x / 100; 297 | } 298 | 299 | static inline uint64_t div1e8(const uint64_t x) { 300 | return x / 100000000; 301 | } 302 | 303 | static inline uint64_t div1e9(const uint64_t x) { 304 | return x / 1000000000; 305 | } 306 | 307 | static inline uint32_t mod1e9(const uint64_t x) { 308 | return (uint32_t) (x - 1000000000 * div1e9(x)); 309 | } 310 | 311 | #endif // defined(RYU_32_BIT_PLATFORM) 312 | 313 | static inline uint32_t pow5Factor(uint64_t value) { 314 | const uint64_t m_inv_5 = 14757395258967641293u; 315 | const uint64_t n_div_5 = 3689348814741910323u; 316 | uint32_t count = 0; 317 | for (;;) { 318 | assert(value != 0); 319 | value *= m_inv_5; 320 | if (value > n_div_5) 321 | break; 322 | ++count; 323 | } 324 | return count; 325 | } 326 | 327 | // Returns true if value is divisible by 5^p. 328 | static inline bool multipleOfPowerOf5(const uint64_t value, const uint32_t p) { 329 | // I tried a case distinction on p, but there was no performance difference. 330 | return pow5Factor(value) >= p; 331 | } 332 | 333 | // Returns true if value is divisible by 2^p. 334 | static inline bool multipleOfPowerOf2(const uint64_t value, const uint32_t p) { 335 | assert(value != 0); 336 | assert(p < 64); 337 | // __builtin_ctzll doesn't appear to be faster here. 338 | return (value & ((1ull << p) - 1)) == 0; 339 | } 340 | 341 | // We need a 64x128-bit multiplication and a subsequent 128-bit shift. 342 | // Multiplication: 343 | // The 64-bit factor is variable and passed in, the 128-bit factor comes 344 | // from a lookup table. We know that the 64-bit factor only has 55 345 | // significant bits (i.e., the 9 topmost bits are zeros). The 128-bit 346 | // factor only has 124 significant bits (i.e., the 4 topmost bits are 347 | // zeros). 348 | // Shift: 349 | // In principle, the multiplication result requires 55 + 124 = 179 bits to 350 | // represent. However, we then shift this value to the right by j, which is 351 | // at least j >= 115, so the result is guaranteed to fit into 179 - 115 = 64 352 | // bits. This means that we only need the topmost 64 significant bits of 353 | // the 64x128-bit multiplication. 354 | // 355 | // There are several ways to do this: 356 | // 1. Best case: the compiler exposes a 128-bit type. 357 | // We perform two 64x64-bit multiplications, add the higher 64 bits of the 358 | // lower result to the higher result, and shift by j - 64 bits. 359 | // 360 | // We explicitly cast from 64-bit to 128-bit, so the compiler can tell 361 | // that these are only 64-bit inputs, and can map these to the best 362 | // possible sequence of assembly instructions. 363 | // x64 machines happen to have matching assembly instructions for 364 | // 64x64-bit multiplications and 128-bit shifts. 365 | // 366 | // 2. Second best case: the compiler exposes intrinsics for the x64 assembly 367 | // instructions mentioned in 1. 368 | // 369 | // 3. We only have 64x64 bit instructions that return the lower 64 bits of 370 | // the result, i.e., we have to use plain C. 371 | // Our inputs are less than the full width, so we have three options: 372 | // a. Ignore this fact and just implement the intrinsics manually. 373 | // b. Split both into 31-bit pieces, which guarantees no internal overflow, 374 | // but requires extra work upfront (unless we change the lookup table). 375 | // c. Split only the first factor into 31-bit pieces, which also guarantees 376 | // no internal overflow, but requires extra work since the intermediate 377 | // results are not perfectly aligned. 378 | #if defined(HAS_UINT128) 379 | 380 | // Best case: use 128-bit type. 381 | static inline uint64_t mulShift64(const uint64_t m, const uint64_t* const mul, 382 | const int32_t j) 383 | { 384 | const uint128_t b0 = ((uint128_t) m) * mul[0]; 385 | const uint128_t b2 = ((uint128_t) m) * mul[1]; 386 | return (uint64_t) (((b0 >> 64) + b2) >> (j - 64)); 387 | } 388 | 389 | static inline uint64_t mulShiftAll64(const uint64_t m, const uint64_t* 390 | const mul, const int32_t j, uint64_t* const vp, uint64_t* const vm, 391 | const uint32_t mmShift) 392 | { 393 | *vp = mulShift64(4 * m + 2, mul, j); 394 | *vm = mulShift64(4 * m - 1 - mmShift, mul, j); 395 | return mulShift64(4 * m, mul, j); 396 | } 397 | 398 | #elif defined(HAS_64_BIT_INTRINSICS) 399 | 400 | static inline uint64_t mulShift64(const uint64_t m, const uint64_t* const mul, 401 | const int32_t j) 402 | { 403 | // m is maximum 55 bits 404 | uint64_t high1; // 128 405 | const uint64_t low1 = umul128(m, mul[1], &high1); // 64 406 | uint64_t high0; // 64 407 | umul128(m, mul[0], &high0); // 0 408 | const uint64_t sum = high0 + low1; 409 | if (sum < high0) { 410 | ++high1; // overflow into high1 411 | } 412 | return shiftright128(sum, high1, j - 64); 413 | } 414 | 415 | static inline uint64_t mulShiftAll64(const uint64_t m, const uint64_t* 416 | const mul, const int32_t j, uint64_t* const vp, uint64_t* const vm, 417 | const uint32_t mmShift) 418 | { 419 | *vp = mulShift64(4 * m + 2, mul, j); 420 | *vm = mulShift64(4 * m - 1 - mmShift, mul, j); 421 | return mulShift64(4 * m, mul, j); 422 | } 423 | 424 | #else // !defined(HAS_UINT128) && !defined(HAS_64_BIT_INTRINSICS) 425 | 426 | // This is faster if we don't have a 64x64->128-bit multiplication. 427 | static inline uint64_t mulShiftAll64(uint64_t m, const uint64_t* const mul, 428 | const int32_t j, uint64_t* const vp, uint64_t* const vm, 429 | const uint32_t mmShift) 430 | { 431 | m <<= 1; 432 | // m is maximum 55 bits 433 | uint64_t tmp; 434 | const uint64_t lo = umul128(m, mul[0], &tmp); 435 | uint64_t hi; 436 | const uint64_t mid = tmp + umul128(m, mul[1], &hi); 437 | hi += mid < tmp; // overflow into hi 438 | 439 | const uint64_t lo2 = lo + mul[0]; 440 | const uint64_t mid2 = mid + mul[1] + (lo2 < lo); 441 | const uint64_t hi2 = hi + (mid2 < mid); 442 | *vp = shiftright128(mid2, hi2, (uint32_t) (j - 64 - 1)); 443 | 444 | if (mmShift == 1) { 445 | const uint64_t lo3 = lo - mul[0]; 446 | const uint64_t mid3 = mid - mul[1] - (lo3 > lo); 447 | const uint64_t hi3 = hi - (mid3 > mid); 448 | *vm = shiftright128(mid3, hi3, (uint32_t) (j - 64 - 1)); 449 | } else { 450 | const uint64_t lo3 = lo + lo; 451 | const uint64_t mid3 = mid + mid + (lo3 < lo); 452 | const uint64_t hi3 = hi + hi + (mid3 < mid); 453 | const uint64_t lo4 = lo3 - mul[0]; 454 | const uint64_t mid4 = mid3 - mul[1] - (lo4 > lo3); 455 | const uint64_t hi4 = hi3 - (mid4 > mid3); 456 | *vm = shiftright128(mid4, hi4, (uint32_t) (j - 64)); 457 | } 458 | 459 | return shiftright128(mid, hi, (uint32_t) (j - 64 - 1)); 460 | } 461 | 462 | #endif // HAS_64_BIT_INTRINSICS 463 | 464 | // Include either the small or the full lookup tables depending on the mode. 465 | #if defined(RYU_OPTIMIZE_SIZE) 466 | 467 | // These tables are generated by PrintDoubleLookupTable. 468 | #define DOUBLE_POW5_INV_BITCOUNT 125 469 | #define DOUBLE_POW5_BITCOUNT 125 470 | 471 | static const uint64_t DOUBLE_POW5_INV_SPLIT2[15][2] = { 472 | { 1u, 2305843009213693952u }, 473 | { 5955668970331000884u, 1784059615882449851u }, 474 | { 8982663654677661702u, 1380349269358112757u }, 475 | { 7286864317269821294u, 2135987035920910082u }, 476 | { 7005857020398200553u, 1652639921975621497u }, 477 | { 17965325103354776697u, 1278668206209430417u }, 478 | { 8928596168509315048u, 1978643211784836272u }, 479 | { 10075671573058298858u, 1530901034580419511u }, 480 | { 597001226353042382u, 1184477304306571148u }, 481 | { 1527430471115325346u, 1832889850782397517u }, 482 | { 12533209867169019542u, 1418129833677084982u }, 483 | { 5577825024675947042u, 2194449627517475473u }, 484 | { 11006974540203867551u, 1697873161311732311u }, 485 | { 10313493231639821582u, 1313665730009899186u }, 486 | { 12701016819766672773u, 2032799256770390445u } 487 | }; 488 | static const uint32_t POW5_INV_OFFSETS[19] = { 489 | 0x54544554, 0x04055545, 0x10041000, 0x00400414, 0x40010000, 0x41155555, 490 | 0x00000454, 0x00010044, 0x40000000, 0x44000041, 0x50454450, 0x55550054, 491 | 0x51655554, 0x40004000, 0x01000001, 0x00010500, 0x51515411, 0x05555554, 492 | 0x00000000 493 | }; 494 | 495 | static const uint64_t DOUBLE_POW5_SPLIT2[13][2] = { 496 | { 0u, 1152921504606846976u }, 497 | { 0u, 1490116119384765625u }, 498 | { 1032610780636961552u, 1925929944387235853u }, 499 | { 7910200175544436838u, 1244603055572228341u }, 500 | { 16941905809032713930u, 1608611746708759036u }, 501 | { 13024893955298202172u, 2079081953128979843u }, 502 | { 6607496772837067824u, 1343575221513417750u }, 503 | { 17332926989895652603u, 1736530273035216783u }, 504 | { 13037379183483547984u, 2244412773384604712u }, 505 | { 1605989338741628675u, 1450417759929778918u }, 506 | { 9630225068416591280u, 1874621017369538693u }, 507 | { 665883850346957067u, 1211445438634777304u }, 508 | { 14931890668723713708u, 1565756531257009982u } 509 | }; 510 | static const uint32_t POW5_OFFSETS[21] = { 511 | 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x40000000, 0x59695995, 512 | 0x55545555, 0x56555515, 0x41150504, 0x40555410, 0x44555145, 0x44504540, 513 | 0x45555550, 0x40004000, 0x96440440, 0x55565565, 0x54454045, 0x40154151, 514 | 0x55559155, 0x51405555, 0x00000105 515 | }; 516 | 517 | #define POW5_TABLE_SIZE 26 518 | static const uint64_t DOUBLE_POW5_TABLE[POW5_TABLE_SIZE] = { 519 | 1ull, 5ull, 25ull, 125ull, 625ull, 3125ull, 15625ull, 78125ull, 390625ull, 520 | 1953125ull, 9765625ull, 48828125ull, 244140625ull, 1220703125ull, 521 | 6103515625ull, 30517578125ull, 152587890625ull, 762939453125ull, 522 | 3814697265625ull, 19073486328125ull, 95367431640625ull, 523 | 476837158203125ull, 2384185791015625ull, 11920928955078125ull, 524 | 59604644775390625ull, 298023223876953125ull //, 1490116119384765625ull 525 | }; 526 | 527 | #if defined(HAS_UINT128) 528 | 529 | // Computes 5^i in the form required by Ryu, and stores it in the given pointer. 530 | static inline void double_computePow5(const uint32_t i, uint64_t* const result) 531 | { 532 | const uint32_t base = i / POW5_TABLE_SIZE; 533 | const uint32_t base2 = base * POW5_TABLE_SIZE; 534 | const uint32_t offset = i - base2; 535 | const uint64_t* const mul = DOUBLE_POW5_SPLIT2[base]; 536 | if (offset == 0) { 537 | result[0] = mul[0]; 538 | result[1] = mul[1]; 539 | return; 540 | } 541 | const uint64_t m = DOUBLE_POW5_TABLE[offset]; 542 | const uint128_t b0 = ((uint128_t) m) * mul[0]; 543 | const uint128_t b2 = ((uint128_t) m) * mul[1]; 544 | const uint32_t delta = pow5bits(i) - pow5bits(base2); 545 | const uint128_t shiftedSum = (b0 >> delta) + (b2 << (64 - delta)) + 546 | ((POW5_OFFSETS[i / 16] >> ((i % 16) << 1)) & 3); 547 | result[0] = (uint64_t) shiftedSum; 548 | result[1] = (uint64_t) (shiftedSum >> 64); 549 | } 550 | 551 | // Computes 5^-i in the form required by Ryu, and stores it in the given pointer. 552 | static inline void double_computeInvPow5(const uint32_t i, 553 | uint64_t* const result) 554 | { 555 | const uint32_t base = (i + POW5_TABLE_SIZE - 1) / POW5_TABLE_SIZE; 556 | const uint32_t base2 = base * POW5_TABLE_SIZE; 557 | const uint32_t offset = base2 - i; 558 | const uint64_t* const mul = DOUBLE_POW5_INV_SPLIT2[base]; // 1/5^base2 559 | if (offset == 0) { 560 | result[0] = mul[0]; 561 | result[1] = mul[1]; 562 | return; 563 | } 564 | const uint64_t m = DOUBLE_POW5_TABLE[offset]; // 5^offset 565 | const uint128_t b0 = ((uint128_t) m) * (mul[0] - 1); 566 | const uint128_t b2 = ((uint128_t) m) * mul[1]; 567 | const uint32_t delta = pow5bits(base2) - pow5bits(i); 568 | const uint128_t shiftedSum = ((b0 >> delta) + (b2 << (64 - delta))) + 1 + 569 | ((POW5_INV_OFFSETS[i / 16] >> ((i % 16) << 1)) & 3); 570 | result[0] = (uint64_t) shiftedSum; 571 | result[1] = (uint64_t) (shiftedSum >> 64); 572 | } 573 | 574 | #else // defined(HAS_UINT128) 575 | 576 | // Computes 5^i in the form required by Ryu, and stores it in the given pointer. 577 | static inline void double_computePow5(const uint32_t i, uint64_t* const result) 578 | { 579 | const uint32_t base = i / POW5_TABLE_SIZE; 580 | const uint32_t base2 = base * POW5_TABLE_SIZE; 581 | const uint32_t offset = i - base2; 582 | const uint64_t* const mul = DOUBLE_POW5_SPLIT2[base]; 583 | if (offset == 0) { 584 | result[0] = mul[0]; 585 | result[1] = mul[1]; 586 | return; 587 | } 588 | const uint64_t m = DOUBLE_POW5_TABLE[offset]; 589 | uint64_t high1; 590 | const uint64_t low1 = umul128(m, mul[1], &high1); 591 | uint64_t high0; 592 | const uint64_t low0 = umul128(m, mul[0], &high0); 593 | const uint64_t sum = high0 + low1; 594 | if (sum < high0) { 595 | ++high1; // overflow into high1 596 | } 597 | // high1 | sum | low0 598 | const uint32_t delta = pow5bits(i) - pow5bits(base2); 599 | result[0] = shiftright128(low0, sum, delta) + 600 | ((POW5_OFFSETS[i / 16] >> ((i % 16) << 1)) & 3); 601 | result[1] = shiftright128(sum, high1, delta); 602 | } 603 | 604 | // Computes 5^-i in the form required by Ryu, and stores it in the given 605 | // pointer. 606 | static inline void double_computeInvPow5(const uint32_t i, 607 | uint64_t* const result) 608 | { 609 | const uint32_t base = (i + POW5_TABLE_SIZE - 1) / POW5_TABLE_SIZE; 610 | const uint32_t base2 = base * POW5_TABLE_SIZE; 611 | const uint32_t offset = base2 - i; 612 | const uint64_t* const mul = DOUBLE_POW5_INV_SPLIT2[base]; // 1/5^base2 613 | if (offset == 0) { 614 | result[0] = mul[0]; 615 | result[1] = mul[1]; 616 | return; 617 | } 618 | const uint64_t m = DOUBLE_POW5_TABLE[offset]; 619 | uint64_t high1; 620 | const uint64_t low1 = umul128(m, mul[1], &high1); 621 | uint64_t high0; 622 | const uint64_t low0 = umul128(m, mul[0] - 1, &high0); 623 | const uint64_t sum = high0 + low1; 624 | if (sum < high0) { 625 | ++high1; // overflow into high1 626 | } 627 | // high1 | sum | low0 628 | const uint32_t delta = pow5bits(base2) - pow5bits(i); 629 | result[0] = shiftright128(low0, sum, delta) + 1 + 630 | ((POW5_INV_OFFSETS[i / 16] >> ((i % 16) << 1)) & 3); 631 | result[1] = shiftright128(sum, high1, delta); 632 | } 633 | 634 | #endif // defined(HAS_UINT128) 635 | 636 | #else 637 | // These tables are generated by PrintDoubleLookupTable. 638 | #define DOUBLE_POW5_INV_BITCOUNT 125 639 | #define DOUBLE_POW5_BITCOUNT 125 640 | 641 | #define DOUBLE_POW5_INV_TABLE_SIZE 342 642 | #define DOUBLE_POW5_TABLE_SIZE 326 643 | 644 | static const uint64_t DOUBLE_POW5_INV_SPLIT[DOUBLE_POW5_INV_TABLE_SIZE][2] = { 645 | { 1u, 2305843009213693952u }, 646 | { 11068046444225730970u, 1844674407370955161u }, 647 | { 5165088340638674453u, 1475739525896764129u }, 648 | { 7821419487252849886u, 1180591620717411303u }, 649 | { 8824922364862649494u, 1888946593147858085u }, 650 | { 7059937891890119595u, 1511157274518286468u }, 651 | { 13026647942995916322u, 1208925819614629174u }, 652 | { 9774590264567735146u, 1934281311383406679u }, 653 | { 11509021026396098440u, 1547425049106725343u }, 654 | { 16585914450600699399u, 1237940039285380274u }, 655 | { 15469416676735388068u, 1980704062856608439u }, 656 | { 16064882156130220778u, 1584563250285286751u }, 657 | { 9162556910162266299u, 1267650600228229401u }, 658 | { 7281393426775805432u, 2028240960365167042u }, 659 | { 16893161185646375315u, 1622592768292133633u }, 660 | { 2446482504291369283u, 1298074214633706907u }, 661 | { 7603720821608101175u, 2076918743413931051u }, 662 | { 2393627842544570617u, 1661534994731144841u }, 663 | { 16672297533003297786u, 1329227995784915872u }, 664 | { 11918280793837635165u, 2126764793255865396u }, 665 | { 5845275820328197809u, 1701411834604692317u }, 666 | { 15744267100488289217u, 1361129467683753853u }, 667 | { 3054734472329800808u, 2177807148294006166u }, 668 | { 17201182836831481939u, 1742245718635204932u }, 669 | { 6382248639981364905u, 1393796574908163946u }, 670 | { 2832900194486363201u, 2230074519853062314u }, 671 | { 5955668970331000884u, 1784059615882449851u }, 672 | { 1075186361522890384u, 1427247692705959881u }, 673 | { 12788344622662355584u, 2283596308329535809u }, 674 | { 13920024512871794791u, 1826877046663628647u }, 675 | { 3757321980813615186u, 1461501637330902918u }, 676 | { 10384555214134712795u, 1169201309864722334u }, 677 | { 5547241898389809503u, 1870722095783555735u }, 678 | { 4437793518711847602u, 1496577676626844588u }, 679 | { 10928932444453298728u, 1197262141301475670u }, 680 | { 17486291911125277965u, 1915619426082361072u }, 681 | { 6610335899416401726u, 1532495540865888858u }, 682 | { 12666966349016942027u, 1225996432692711086u }, 683 | { 12888448528943286597u, 1961594292308337738u }, 684 | { 17689456452638449924u, 1569275433846670190u }, 685 | { 14151565162110759939u, 1255420347077336152u }, 686 | { 7885109000409574610u, 2008672555323737844u }, 687 | { 9997436015069570011u, 1606938044258990275u }, 688 | { 7997948812055656009u, 1285550435407192220u }, 689 | { 12796718099289049614u, 2056880696651507552u }, 690 | { 2858676849947419045u, 1645504557321206042u }, 691 | { 13354987924183666206u, 1316403645856964833u }, 692 | { 17678631863951955605u, 2106245833371143733u }, 693 | { 3074859046935833515u, 1684996666696914987u }, 694 | { 13527933681774397782u, 1347997333357531989u }, 695 | { 10576647446613305481u, 2156795733372051183u }, 696 | { 15840015586774465031u, 1725436586697640946u }, 697 | { 8982663654677661702u, 1380349269358112757u }, 698 | { 18061610662226169046u, 2208558830972980411u }, 699 | { 10759939715039024913u, 1766847064778384329u }, 700 | { 12297300586773130254u, 1413477651822707463u }, 701 | { 15986332124095098083u, 2261564242916331941u }, 702 | { 9099716884534168143u, 1809251394333065553u }, 703 | { 14658471137111155161u, 1447401115466452442u }, 704 | { 4348079280205103483u, 1157920892373161954u }, 705 | { 14335624477811986218u, 1852673427797059126u }, 706 | { 7779150767507678651u, 1482138742237647301u }, 707 | { 2533971799264232598u, 1185710993790117841u }, 708 | { 15122401323048503126u, 1897137590064188545u }, 709 | { 12097921058438802501u, 1517710072051350836u }, 710 | { 5988988032009131678u, 1214168057641080669u }, 711 | { 16961078480698431330u, 1942668892225729070u }, 712 | { 13568862784558745064u, 1554135113780583256u }, 713 | { 7165741412905085728u, 1243308091024466605u }, 714 | { 11465186260648137165u, 1989292945639146568u }, 715 | { 16550846638002330379u, 1591434356511317254u }, 716 | { 16930026125143774626u, 1273147485209053803u }, 717 | { 4951948911778577463u, 2037035976334486086u }, 718 | { 272210314680951647u, 1629628781067588869u }, 719 | { 3907117066486671641u, 1303703024854071095u }, 720 | { 6251387306378674625u, 2085924839766513752u }, 721 | { 16069156289328670670u, 1668739871813211001u }, 722 | { 9165976216721026213u, 1334991897450568801u }, 723 | { 7286864317269821294u, 2135987035920910082u }, 724 | { 16897537898041588005u, 1708789628736728065u }, 725 | { 13518030318433270404u, 1367031702989382452u }, 726 | { 6871453250525591353u, 2187250724783011924u }, 727 | { 9186511415162383406u, 1749800579826409539u }, 728 | { 11038557946871817048u, 1399840463861127631u }, 729 | { 10282995085511086630u, 2239744742177804210u }, 730 | { 8226396068408869304u, 1791795793742243368u }, 731 | { 13959814484210916090u, 1433436634993794694u }, 732 | { 11267656730511734774u, 2293498615990071511u }, 733 | { 5324776569667477496u, 1834798892792057209u }, 734 | { 7949170070475892320u, 1467839114233645767u }, 735 | { 17427382500606444826u, 1174271291386916613u }, 736 | { 5747719112518849781u, 1878834066219066582u }, 737 | { 15666221734240810795u, 1503067252975253265u }, 738 | { 12532977387392648636u, 1202453802380202612u }, 739 | { 5295368560860596524u, 1923926083808324180u }, 740 | { 4236294848688477220u, 1539140867046659344u }, 741 | { 7078384693692692099u, 1231312693637327475u }, 742 | { 11325415509908307358u, 1970100309819723960u }, 743 | { 9060332407926645887u, 1576080247855779168u }, 744 | { 14626963555825137356u, 1260864198284623334u }, 745 | { 12335095245094488799u, 2017382717255397335u }, 746 | { 9868076196075591040u, 1613906173804317868u }, 747 | { 15273158586344293478u, 1291124939043454294u }, 748 | { 13369007293925138595u, 2065799902469526871u }, 749 | { 7005857020398200553u, 1652639921975621497u }, 750 | { 16672732060544291412u, 1322111937580497197u }, 751 | { 11918976037903224966u, 2115379100128795516u }, 752 | { 5845832015580669650u, 1692303280103036413u }, 753 | { 12055363241948356366u, 1353842624082429130u }, 754 | { 841837113407818570u, 2166148198531886609u }, 755 | { 4362818505468165179u, 1732918558825509287u }, 756 | { 14558301248600263113u, 1386334847060407429u }, 757 | { 12225235553534690011u, 2218135755296651887u }, 758 | { 2401490813343931363u, 1774508604237321510u }, 759 | { 1921192650675145090u, 1419606883389857208u }, 760 | { 17831303500047873437u, 2271371013423771532u }, 761 | { 6886345170554478103u, 1817096810739017226u }, 762 | { 1819727321701672159u, 1453677448591213781u }, 763 | { 16213177116328979020u, 1162941958872971024u }, 764 | { 14873036941900635463u, 1860707134196753639u }, 765 | { 15587778368262418694u, 1488565707357402911u }, 766 | { 8780873879868024632u, 1190852565885922329u }, 767 | { 2981351763563108441u, 1905364105417475727u }, 768 | { 13453127855076217722u, 1524291284333980581u }, 769 | { 7073153469319063855u, 1219433027467184465u }, 770 | { 11317045550910502167u, 1951092843947495144u }, 771 | { 12742985255470312057u, 1560874275157996115u }, 772 | { 10194388204376249646u, 1248699420126396892u }, 773 | { 1553625868034358140u, 1997919072202235028u }, 774 | { 8621598323911307159u, 1598335257761788022u }, 775 | { 17965325103354776697u, 1278668206209430417u }, 776 | { 13987124906400001422u, 2045869129935088668u }, 777 | { 121653480894270168u, 1636695303948070935u }, 778 | { 97322784715416134u, 1309356243158456748u }, 779 | { 14913111714512307107u, 2094969989053530796u }, 780 | { 8241140556867935363u, 1675975991242824637u }, 781 | { 17660958889720079260u, 1340780792994259709u }, 782 | { 17189487779326395846u, 2145249268790815535u }, 783 | { 13751590223461116677u, 1716199415032652428u }, 784 | { 18379969808252713988u, 1372959532026121942u }, 785 | { 14650556434236701088u, 2196735251241795108u }, 786 | { 652398703163629901u, 1757388200993436087u }, 787 | { 11589965406756634890u, 1405910560794748869u }, 788 | { 7475898206584884855u, 2249456897271598191u }, 789 | { 2291369750525997561u, 1799565517817278553u }, 790 | { 9211793429904618695u, 1439652414253822842u }, 791 | { 18428218302589300235u, 2303443862806116547u }, 792 | { 7363877012587619542u, 1842755090244893238u }, 793 | { 13269799239553916280u, 1474204072195914590u }, 794 | { 10615839391643133024u, 1179363257756731672u }, 795 | { 2227947767661371545u, 1886981212410770676u }, 796 | { 16539753473096738529u, 1509584969928616540u }, 797 | { 13231802778477390823u, 1207667975942893232u }, 798 | { 6413489186596184024u, 1932268761508629172u }, 799 | { 16198837793502678189u, 1545815009206903337u }, 800 | { 5580372605318321905u, 1236652007365522670u }, 801 | { 8928596168509315048u, 1978643211784836272u }, 802 | { 18210923379033183008u, 1582914569427869017u }, 803 | { 7190041073742725760u, 1266331655542295214u }, 804 | { 436019273762630246u, 2026130648867672343u }, 805 | { 7727513048493924843u, 1620904519094137874u }, 806 | { 9871359253537050198u, 1296723615275310299u }, 807 | { 4726128361433549347u, 2074757784440496479u }, 808 | { 7470251503888749801u, 1659806227552397183u }, 809 | { 13354898832594820487u, 1327844982041917746u }, 810 | { 13989140502667892133u, 2124551971267068394u }, 811 | { 14880661216876224029u, 1699641577013654715u }, 812 | { 11904528973500979224u, 1359713261610923772u }, 813 | { 4289851098633925465u, 2175541218577478036u }, 814 | { 18189276137874781665u, 1740432974861982428u }, 815 | { 3483374466074094362u, 1392346379889585943u }, 816 | { 1884050330976640656u, 2227754207823337509u }, 817 | { 5196589079523222848u, 1782203366258670007u }, 818 | { 15225317707844309248u, 1425762693006936005u }, 819 | { 5913764258841343181u, 2281220308811097609u }, 820 | { 8420360221814984868u, 1824976247048878087u }, 821 | { 17804334621677718864u, 1459980997639102469u }, 822 | { 17932816512084085415u, 1167984798111281975u }, 823 | { 10245762345624985047u, 1868775676978051161u }, 824 | { 4507261061758077715u, 1495020541582440929u }, 825 | { 7295157664148372495u, 1196016433265952743u }, 826 | { 7982903447895485668u, 1913626293225524389u }, 827 | { 10075671573058298858u, 1530901034580419511u }, 828 | { 4371188443704728763u, 1224720827664335609u }, 829 | { 14372599139411386667u, 1959553324262936974u }, 830 | { 15187428126271019657u, 1567642659410349579u }, 831 | { 15839291315758726049u, 1254114127528279663u }, 832 | { 3206773216762499739u, 2006582604045247462u }, 833 | { 13633465017635730761u, 1605266083236197969u }, 834 | { 14596120828850494932u, 1284212866588958375u }, 835 | { 4907049252451240275u, 2054740586542333401u }, 836 | { 236290587219081897u, 1643792469233866721u }, 837 | { 14946427728742906810u, 1315033975387093376u }, 838 | { 16535586736504830250u, 2104054360619349402u }, 839 | { 5849771759720043554u, 1683243488495479522u }, 840 | { 15747863852001765813u, 1346594790796383617u }, 841 | { 10439186904235184007u, 2154551665274213788u }, 842 | { 15730047152871967852u, 1723641332219371030u }, 843 | { 12584037722297574282u, 1378913065775496824u }, 844 | { 9066413911450387881u, 2206260905240794919u }, 845 | { 10942479943902220628u, 1765008724192635935u }, 846 | { 8753983955121776503u, 1412006979354108748u }, 847 | { 10317025513452932081u, 2259211166966573997u }, 848 | { 874922781278525018u, 1807368933573259198u }, 849 | { 8078635854506640661u, 1445895146858607358u }, 850 | { 13841606313089133175u, 1156716117486885886u }, 851 | { 14767872471458792434u, 1850745787979017418u }, 852 | { 746251532941302978u, 1480596630383213935u }, 853 | { 597001226353042382u, 1184477304306571148u }, 854 | { 15712597221132509104u, 1895163686890513836u }, 855 | { 8880728962164096960u, 1516130949512411069u }, 856 | { 10793931984473187891u, 1212904759609928855u }, 857 | { 17270291175157100626u, 1940647615375886168u }, 858 | { 2748186495899949531u, 1552518092300708935u }, 859 | { 2198549196719959625u, 1242014473840567148u }, 860 | { 18275073973719576693u, 1987223158144907436u }, 861 | { 10930710364233751031u, 1589778526515925949u }, 862 | { 12433917106128911148u, 1271822821212740759u }, 863 | { 8826220925580526867u, 2034916513940385215u }, 864 | { 7060976740464421494u, 1627933211152308172u }, 865 | { 16716827836597268165u, 1302346568921846537u }, 866 | { 11989529279587987770u, 2083754510274954460u }, 867 | { 9591623423670390216u, 1667003608219963568u }, 868 | { 15051996368420132820u, 1333602886575970854u }, 869 | { 13015147745246481542u, 2133764618521553367u }, 870 | { 3033420566713364587u, 1707011694817242694u }, 871 | { 6116085268112601993u, 1365609355853794155u }, 872 | { 9785736428980163188u, 2184974969366070648u }, 873 | { 15207286772667951197u, 1747979975492856518u }, 874 | { 1097782973908629988u, 1398383980394285215u }, 875 | { 1756452758253807981u, 2237414368630856344u }, 876 | { 5094511021344956708u, 1789931494904685075u }, 877 | { 4075608817075965366u, 1431945195923748060u }, 878 | { 6520974107321544586u, 2291112313477996896u }, 879 | { 1527430471115325346u, 1832889850782397517u }, 880 | { 12289990821117991246u, 1466311880625918013u }, 881 | { 17210690286378213644u, 1173049504500734410u }, 882 | { 9090360384495590213u, 1876879207201175057u }, 883 | { 18340334751822203140u, 1501503365760940045u }, 884 | { 14672267801457762512u, 1201202692608752036u }, 885 | { 16096930852848599373u, 1921924308174003258u }, 886 | { 1809498238053148529u, 1537539446539202607u }, 887 | { 12515645034668249793u, 1230031557231362085u }, 888 | { 1578287981759648052u, 1968050491570179337u }, 889 | { 12330676829633449412u, 1574440393256143469u }, 890 | { 13553890278448669853u, 1259552314604914775u }, 891 | { 3239480371808320148u, 2015283703367863641u }, 892 | { 17348979556414297411u, 1612226962694290912u }, 893 | { 6500486015647617283u, 1289781570155432730u }, 894 | { 10400777625036187652u, 2063650512248692368u }, 895 | { 15699319729512770768u, 1650920409798953894u }, 896 | { 16248804598352126938u, 1320736327839163115u }, 897 | { 7551343283653851484u, 2113178124542660985u }, 898 | { 6041074626923081187u, 1690542499634128788u }, 899 | { 12211557331022285596u, 1352433999707303030u }, 900 | { 1091747655926105338u, 2163894399531684849u }, 901 | { 4562746939482794594u, 1731115519625347879u }, 902 | { 7339546366328145998u, 1384892415700278303u }, 903 | { 8053925371383123274u, 2215827865120445285u }, 904 | { 6443140297106498619u, 1772662292096356228u }, 905 | { 12533209867169019542u, 1418129833677084982u }, 906 | { 5295740528502789974u, 2269007733883335972u }, 907 | { 15304638867027962949u, 1815206187106668777u }, 908 | { 4865013464138549713u, 1452164949685335022u }, 909 | { 14960057215536570740u, 1161731959748268017u }, 910 | { 9178696285890871890u, 1858771135597228828u }, 911 | { 14721654658196518159u, 1487016908477783062u }, 912 | { 4398626097073393881u, 1189613526782226450u }, 913 | { 7037801755317430209u, 1903381642851562320u }, 914 | { 5630241404253944167u, 1522705314281249856u }, 915 | { 814844308661245011u, 1218164251424999885u }, 916 | { 1303750893857992017u, 1949062802279999816u }, 917 | { 15800395974054034906u, 1559250241823999852u }, 918 | { 5261619149759407279u, 1247400193459199882u }, 919 | { 12107939454356961969u, 1995840309534719811u }, 920 | { 5997002748743659252u, 1596672247627775849u }, 921 | { 8486951013736837725u, 1277337798102220679u }, 922 | { 2511075177753209390u, 2043740476963553087u }, 923 | { 13076906586428298482u, 1634992381570842469u }, 924 | { 14150874083884549109u, 1307993905256673975u }, 925 | { 4194654460505726958u, 2092790248410678361u }, 926 | { 18113118827372222859u, 1674232198728542688u }, 927 | { 3422448617672047318u, 1339385758982834151u }, 928 | { 16543964232501006678u, 2143017214372534641u }, 929 | { 9545822571258895019u, 1714413771498027713u }, 930 | { 15015355686490936662u, 1371531017198422170u }, 931 | { 5577825024675947042u, 2194449627517475473u }, 932 | { 11840957649224578280u, 1755559702013980378u }, 933 | { 16851463748863483271u, 1404447761611184302u }, 934 | { 12204946739213931940u, 2247116418577894884u }, 935 | { 13453306206113055875u, 1797693134862315907u }, 936 | { 3383947335406624054u, 1438154507889852726u }, 937 | { 16482362180876329456u, 2301047212623764361u }, 938 | { 9496540929959153242u, 1840837770099011489u }, 939 | { 11286581558709232917u, 1472670216079209191u }, 940 | { 5339916432225476010u, 1178136172863367353u }, 941 | { 4854517476818851293u, 1885017876581387765u }, 942 | { 3883613981455081034u, 1508014301265110212u }, 943 | { 14174937629389795797u, 1206411441012088169u }, 944 | { 11611853762797942306u, 1930258305619341071u }, 945 | { 5600134195496443521u, 1544206644495472857u }, 946 | { 15548153800622885787u, 1235365315596378285u }, 947 | { 6430302007287065643u, 1976584504954205257u }, 948 | { 16212288050055383484u, 1581267603963364205u }, 949 | { 12969830440044306787u, 1265014083170691364u }, 950 | { 9683682259845159889u, 2024022533073106183u }, 951 | { 15125643437359948558u, 1619218026458484946u }, 952 | { 8411165935146048523u, 1295374421166787957u }, 953 | { 17147214310975587960u, 2072599073866860731u }, 954 | { 10028422634038560045u, 1658079259093488585u }, 955 | { 8022738107230848036u, 1326463407274790868u }, 956 | { 9147032156827446534u, 2122341451639665389u }, 957 | { 11006974540203867551u, 1697873161311732311u }, 958 | { 5116230817421183718u, 1358298529049385849u }, 959 | { 15564666937357714594u, 2173277646479017358u }, 960 | { 1383687105660440706u, 1738622117183213887u }, 961 | { 12174996128754083534u, 1390897693746571109u }, 962 | { 8411947361780802685u, 2225436309994513775u }, 963 | { 6729557889424642148u, 1780349047995611020u }, 964 | { 5383646311539713719u, 1424279238396488816u }, 965 | { 1235136468979721303u, 2278846781434382106u }, 966 | { 15745504434151418335u, 1823077425147505684u }, 967 | { 16285752362063044992u, 1458461940118004547u }, 968 | { 5649904260166615347u, 1166769552094403638u }, 969 | { 5350498001524674232u, 1866831283351045821u }, 970 | { 591049586477829062u, 1493465026680836657u }, 971 | { 11540886113407994219u, 1194772021344669325u }, 972 | { 18673707743239135u, 1911635234151470921u }, 973 | { 14772334225162232601u, 1529308187321176736u }, 974 | { 8128518565387875758u, 1223446549856941389u }, 975 | { 1937583260394870242u, 1957514479771106223u }, 976 | { 8928764237799716840u, 1566011583816884978u }, 977 | { 14521709019723594119u, 1252809267053507982u }, 978 | { 8477339172590109297u, 2004494827285612772u }, 979 | { 17849917782297818407u, 1603595861828490217u }, 980 | { 6901236596354434079u, 1282876689462792174u }, 981 | { 18420676183650915173u, 2052602703140467478u }, 982 | { 3668494502695001169u, 1642082162512373983u }, 983 | { 10313493231639821582u, 1313665730009899186u }, 984 | { 9122891541139893884u, 2101865168015838698u }, 985 | { 14677010862395735754u, 1681492134412670958u }, 986 | { 673562245690857633u, 1345193707530136767u } 987 | }; 988 | 989 | static const uint64_t DOUBLE_POW5_SPLIT[DOUBLE_POW5_TABLE_SIZE][2] = { 990 | { 0u, 1152921504606846976u }, 991 | { 0u, 1441151880758558720u }, 992 | { 0u, 1801439850948198400u }, 993 | { 0u, 2251799813685248000u }, 994 | { 0u, 1407374883553280000u }, 995 | { 0u, 1759218604441600000u }, 996 | { 0u, 2199023255552000000u }, 997 | { 0u, 1374389534720000000u }, 998 | { 0u, 1717986918400000000u }, 999 | { 0u, 2147483648000000000u }, 1000 | { 0u, 1342177280000000000u }, 1001 | { 0u, 1677721600000000000u }, 1002 | { 0u, 2097152000000000000u }, 1003 | { 0u, 1310720000000000000u }, 1004 | { 0u, 1638400000000000000u }, 1005 | { 0u, 2048000000000000000u }, 1006 | { 0u, 1280000000000000000u }, 1007 | { 0u, 1600000000000000000u }, 1008 | { 0u, 2000000000000000000u }, 1009 | { 0u, 1250000000000000000u }, 1010 | { 0u, 1562500000000000000u }, 1011 | { 0u, 1953125000000000000u }, 1012 | { 0u, 1220703125000000000u }, 1013 | { 0u, 1525878906250000000u }, 1014 | { 0u, 1907348632812500000u }, 1015 | { 0u, 1192092895507812500u }, 1016 | { 0u, 1490116119384765625u }, 1017 | { 4611686018427387904u, 1862645149230957031u }, 1018 | { 9799832789158199296u, 1164153218269348144u }, 1019 | { 12249790986447749120u, 1455191522836685180u }, 1020 | { 15312238733059686400u, 1818989403545856475u }, 1021 | { 14528612397897220096u, 2273736754432320594u }, 1022 | { 13692068767113150464u, 1421085471520200371u }, 1023 | { 12503399940464050176u, 1776356839400250464u }, 1024 | { 15629249925580062720u, 2220446049250313080u }, 1025 | { 9768281203487539200u, 1387778780781445675u }, 1026 | { 7598665485932036096u, 1734723475976807094u }, 1027 | { 274959820560269312u, 2168404344971008868u }, 1028 | { 9395221924704944128u, 1355252715606880542u }, 1029 | { 2520655369026404352u, 1694065894508600678u }, 1030 | { 12374191248137781248u, 2117582368135750847u }, 1031 | { 14651398557727195136u, 1323488980084844279u }, 1032 | { 13702562178731606016u, 1654361225106055349u }, 1033 | { 3293144668132343808u, 2067951531382569187u }, 1034 | { 18199116482078572544u, 1292469707114105741u }, 1035 | { 8913837547316051968u, 1615587133892632177u }, 1036 | { 15753982952572452864u, 2019483917365790221u }, 1037 | { 12152082354571476992u, 1262177448353618888u }, 1038 | { 15190102943214346240u, 1577721810442023610u }, 1039 | { 9764256642163156992u, 1972152263052529513u }, 1040 | { 17631875447420442880u, 1232595164407830945u }, 1041 | { 8204786253993389888u, 1540743955509788682u }, 1042 | { 1032610780636961552u, 1925929944387235853u }, 1043 | { 2951224747111794922u, 1203706215242022408u }, 1044 | { 3689030933889743652u, 1504632769052528010u }, 1045 | { 13834660704216955373u, 1880790961315660012u }, 1046 | { 17870034976990372916u, 1175494350822287507u }, 1047 | { 17725857702810578241u, 1469367938527859384u }, 1048 | { 3710578054803671186u, 1836709923159824231u }, 1049 | { 26536550077201078u, 2295887403949780289u }, 1050 | { 11545800389866720434u, 1434929627468612680u }, 1051 | { 14432250487333400542u, 1793662034335765850u }, 1052 | { 8816941072311974870u, 2242077542919707313u }, 1053 | { 17039803216263454053u, 1401298464324817070u }, 1054 | { 12076381983474541759u, 1751623080406021338u }, 1055 | { 5872105442488401391u, 2189528850507526673u }, 1056 | { 15199280947623720629u, 1368455531567204170u }, 1057 | { 9775729147674874978u, 1710569414459005213u }, 1058 | { 16831347453020981627u, 2138211768073756516u }, 1059 | { 1296220121283337709u, 1336382355046097823u }, 1060 | { 15455333206886335848u, 1670477943807622278u }, 1061 | { 10095794471753144002u, 2088097429759527848u }, 1062 | { 6309871544845715001u, 1305060893599704905u }, 1063 | { 12499025449484531656u, 1631326116999631131u }, 1064 | { 11012095793428276666u, 2039157646249538914u }, 1065 | { 11494245889320060820u, 1274473528905961821u }, 1066 | { 532749306367912313u, 1593091911132452277u }, 1067 | { 5277622651387278295u, 1991364888915565346u }, 1068 | { 7910200175544436838u, 1244603055572228341u }, 1069 | { 14499436237857933952u, 1555753819465285426u }, 1070 | { 8900923260467641632u, 1944692274331606783u }, 1071 | { 12480606065433357876u, 1215432671457254239u }, 1072 | { 10989071563364309441u, 1519290839321567799u }, 1073 | { 9124653435777998898u, 1899113549151959749u }, 1074 | { 8008751406574943263u, 1186945968219974843u }, 1075 | { 5399253239791291175u, 1483682460274968554u }, 1076 | { 15972438586593889776u, 1854603075343710692u }, 1077 | { 759402079766405302u, 1159126922089819183u }, 1078 | { 14784310654990170340u, 1448908652612273978u }, 1079 | { 9257016281882937117u, 1811135815765342473u }, 1080 | { 16182956370781059300u, 2263919769706678091u }, 1081 | { 7808504722524468110u, 1414949856066673807u }, 1082 | { 5148944884728197234u, 1768687320083342259u }, 1083 | { 1824495087482858639u, 2210859150104177824u }, 1084 | { 1140309429676786649u, 1381786968815111140u }, 1085 | { 1425386787095983311u, 1727233711018888925u }, 1086 | { 6393419502297367043u, 2159042138773611156u }, 1087 | { 13219259225790630210u, 1349401336733506972u }, 1088 | { 16524074032238287762u, 1686751670916883715u }, 1089 | { 16043406521870471799u, 2108439588646104644u }, 1090 | { 803757039314269066u, 1317774742903815403u }, 1091 | { 14839754354425000045u, 1647218428629769253u }, 1092 | { 4714634887749086344u, 2059023035787211567u }, 1093 | { 9864175832484260821u, 1286889397367007229u }, 1094 | { 16941905809032713930u, 1608611746708759036u }, 1095 | { 2730638187581340797u, 2010764683385948796u }, 1096 | { 10930020904093113806u, 1256727927116217997u }, 1097 | { 18274212148543780162u, 1570909908895272496u }, 1098 | { 4396021111970173586u, 1963637386119090621u }, 1099 | { 5053356204195052443u, 1227273366324431638u }, 1100 | { 15540067292098591362u, 1534091707905539547u }, 1101 | { 14813398096695851299u, 1917614634881924434u }, 1102 | { 13870059828862294966u, 1198509146801202771u }, 1103 | { 12725888767650480803u, 1498136433501503464u }, 1104 | { 15907360959563101004u, 1872670541876879330u }, 1105 | { 14553786618154326031u, 1170419088673049581u }, 1106 | { 4357175217410743827u, 1463023860841311977u }, 1107 | { 10058155040190817688u, 1828779826051639971u }, 1108 | { 7961007781811134206u, 2285974782564549964u }, 1109 | { 14199001900486734687u, 1428734239102843727u }, 1110 | { 13137066357181030455u, 1785917798878554659u }, 1111 | { 11809646928048900164u, 2232397248598193324u }, 1112 | { 16604401366885338411u, 1395248280373870827u }, 1113 | { 16143815690179285109u, 1744060350467338534u }, 1114 | { 10956397575869330579u, 2180075438084173168u }, 1115 | { 6847748484918331612u, 1362547148802608230u }, 1116 | { 17783057643002690323u, 1703183936003260287u }, 1117 | { 17617136035325974999u, 2128979920004075359u }, 1118 | { 17928239049719816230u, 1330612450002547099u }, 1119 | { 17798612793722382384u, 1663265562503183874u }, 1120 | { 13024893955298202172u, 2079081953128979843u }, 1121 | { 5834715712847682405u, 1299426220705612402u }, 1122 | { 16516766677914378815u, 1624282775882015502u }, 1123 | { 11422586310538197711u, 2030353469852519378u }, 1124 | { 11750802462513761473u, 1268970918657824611u }, 1125 | { 10076817059714813937u, 1586213648322280764u }, 1126 | { 12596021324643517422u, 1982767060402850955u }, 1127 | { 5566670318688504437u, 1239229412751781847u }, 1128 | { 2346651879933242642u, 1549036765939727309u }, 1129 | { 7545000868343941206u, 1936295957424659136u }, 1130 | { 4715625542714963254u, 1210184973390411960u }, 1131 | { 5894531928393704067u, 1512731216738014950u }, 1132 | { 16591536947346905892u, 1890914020922518687u }, 1133 | { 17287239619732898039u, 1181821263076574179u }, 1134 | { 16997363506238734644u, 1477276578845717724u }, 1135 | { 2799960309088866689u, 1846595723557147156u }, 1136 | { 10973347230035317489u, 1154122327223216972u }, 1137 | { 13716684037544146861u, 1442652909029021215u }, 1138 | { 12534169028502795672u, 1803316136286276519u }, 1139 | { 11056025267201106687u, 2254145170357845649u }, 1140 | { 18439230838069161439u, 1408840731473653530u }, 1141 | { 13825666510731675991u, 1761050914342066913u }, 1142 | { 3447025083132431277u, 2201313642927583642u }, 1143 | { 6766076695385157452u, 1375821026829739776u }, 1144 | { 8457595869231446815u, 1719776283537174720u }, 1145 | { 10571994836539308519u, 2149720354421468400u }, 1146 | { 6607496772837067824u, 1343575221513417750u }, 1147 | { 17482743002901110588u, 1679469026891772187u }, 1148 | { 17241742735199000331u, 2099336283614715234u }, 1149 | { 15387775227926763111u, 1312085177259197021u }, 1150 | { 5399660979626290177u, 1640106471573996277u }, 1151 | { 11361262242960250625u, 2050133089467495346u }, 1152 | { 11712474920277544544u, 1281333180917184591u }, 1153 | { 10028907631919542777u, 1601666476146480739u }, 1154 | { 7924448521472040567u, 2002083095183100924u }, 1155 | { 14176152362774801162u, 1251301934489438077u }, 1156 | { 3885132398186337741u, 1564127418111797597u }, 1157 | { 9468101516160310080u, 1955159272639746996u }, 1158 | { 15140935484454969608u, 1221974545399841872u }, 1159 | { 479425281859160394u, 1527468181749802341u }, 1160 | { 5210967620751338397u, 1909335227187252926u }, 1161 | { 17091912818251750210u, 1193334516992033078u }, 1162 | { 12141518985959911954u, 1491668146240041348u }, 1163 | { 15176898732449889943u, 1864585182800051685u }, 1164 | { 11791404716994875166u, 1165365739250032303u }, 1165 | { 10127569877816206054u, 1456707174062540379u }, 1166 | { 8047776328842869663u, 1820883967578175474u }, 1167 | { 836348374198811271u, 2276104959472719343u }, 1168 | { 7440246761515338900u, 1422565599670449589u }, 1169 | { 13911994470321561530u, 1778206999588061986u }, 1170 | { 8166621051047176104u, 2222758749485077483u }, 1171 | { 2798295147690791113u, 1389224218428173427u }, 1172 | { 17332926989895652603u, 1736530273035216783u }, 1173 | { 17054472718942177850u, 2170662841294020979u }, 1174 | { 8353202440125167204u, 1356664275808763112u }, 1175 | { 10441503050156459005u, 1695830344760953890u }, 1176 | { 3828506775840797949u, 2119787930951192363u }, 1177 | { 86973725686804766u, 1324867456844495227u }, 1178 | { 13943775212390669669u, 1656084321055619033u }, 1179 | { 3594660960206173375u, 2070105401319523792u }, 1180 | { 2246663100128858359u, 1293815875824702370u }, 1181 | { 12031700912015848757u, 1617269844780877962u }, 1182 | { 5816254103165035138u, 2021587305976097453u }, 1183 | { 5941001823691840913u, 1263492066235060908u }, 1184 | { 7426252279614801142u, 1579365082793826135u }, 1185 | { 4671129331091113523u, 1974206353492282669u }, 1186 | { 5225298841145639904u, 1233878970932676668u }, 1187 | { 6531623551432049880u, 1542348713665845835u }, 1188 | { 3552843420862674446u, 1927935892082307294u }, 1189 | { 16055585193321335241u, 1204959932551442058u }, 1190 | { 10846109454796893243u, 1506199915689302573u }, 1191 | { 18169322836923504458u, 1882749894611628216u }, 1192 | { 11355826773077190286u, 1176718684132267635u }, 1193 | { 9583097447919099954u, 1470898355165334544u }, 1194 | { 11978871809898874942u, 1838622943956668180u }, 1195 | { 14973589762373593678u, 2298278679945835225u }, 1196 | { 2440964573842414192u, 1436424174966147016u }, 1197 | { 3051205717303017741u, 1795530218707683770u }, 1198 | { 13037379183483547984u, 2244412773384604712u }, 1199 | { 8148361989677217490u, 1402757983365377945u }, 1200 | { 14797138505523909766u, 1753447479206722431u }, 1201 | { 13884737113477499304u, 2191809349008403039u }, 1202 | { 15595489723564518921u, 1369880843130251899u }, 1203 | { 14882676136028260747u, 1712351053912814874u }, 1204 | { 9379973133180550126u, 2140438817391018593u }, 1205 | { 17391698254306313589u, 1337774260869386620u }, 1206 | { 3292878744173340370u, 1672217826086733276u }, 1207 | { 4116098430216675462u, 2090272282608416595u }, 1208 | { 266718509671728212u, 1306420176630260372u }, 1209 | { 333398137089660265u, 1633025220787825465u }, 1210 | { 5028433689789463235u, 2041281525984781831u }, 1211 | { 10060300083759496378u, 1275800953740488644u }, 1212 | { 12575375104699370472u, 1594751192175610805u }, 1213 | { 1884160825592049379u, 1993438990219513507u }, 1214 | { 17318501580490888525u, 1245899368887195941u }, 1215 | { 7813068920331446945u, 1557374211108994927u }, 1216 | { 5154650131986920777u, 1946717763886243659u }, 1217 | { 915813323278131534u, 1216698602428902287u }, 1218 | { 14979824709379828129u, 1520873253036127858u }, 1219 | { 9501408849870009354u, 1901091566295159823u }, 1220 | { 12855909558809837702u, 1188182228934474889u }, 1221 | { 2234828893230133415u, 1485227786168093612u }, 1222 | { 2793536116537666769u, 1856534732710117015u }, 1223 | { 8663489100477123587u, 1160334207943823134u }, 1224 | { 1605989338741628675u, 1450417759929778918u }, 1225 | { 11230858710281811652u, 1813022199912223647u }, 1226 | { 9426887369424876662u, 2266277749890279559u }, 1227 | { 12809333633531629769u, 1416423593681424724u }, 1228 | { 16011667041914537212u, 1770529492101780905u }, 1229 | { 6179525747111007803u, 2213161865127226132u }, 1230 | { 13085575628799155685u, 1383226165704516332u }, 1231 | { 16356969535998944606u, 1729032707130645415u }, 1232 | { 15834525901571292854u, 2161290883913306769u }, 1233 | { 2979049660840976177u, 1350806802445816731u }, 1234 | { 17558870131333383934u, 1688508503057270913u }, 1235 | { 8113529608884566205u, 2110635628821588642u }, 1236 | { 9682642023980241782u, 1319147268013492901u }, 1237 | { 16714988548402690132u, 1648934085016866126u }, 1238 | { 11670363648648586857u, 2061167606271082658u }, 1239 | { 11905663298832754689u, 1288229753919426661u }, 1240 | { 1047021068258779650u, 1610287192399283327u }, 1241 | { 15143834390605638274u, 2012858990499104158u }, 1242 | { 4853210475701136017u, 1258036869061940099u }, 1243 | { 1454827076199032118u, 1572546086327425124u }, 1244 | { 1818533845248790147u, 1965682607909281405u }, 1245 | { 3442426662494187794u, 1228551629943300878u }, 1246 | { 13526405364972510550u, 1535689537429126097u }, 1247 | { 3072948650933474476u, 1919611921786407622u }, 1248 | { 15755650962115585259u, 1199757451116504763u }, 1249 | { 15082877684217093670u, 1499696813895630954u }, 1250 | { 9630225068416591280u, 1874621017369538693u }, 1251 | { 8324733676974063502u, 1171638135855961683u }, 1252 | { 5794231077790191473u, 1464547669819952104u }, 1253 | { 7242788847237739342u, 1830684587274940130u }, 1254 | { 18276858095901949986u, 2288355734093675162u }, 1255 | { 16034722328366106645u, 1430222333808546976u }, 1256 | { 1596658836748081690u, 1787777917260683721u }, 1257 | { 6607509564362490017u, 2234722396575854651u }, 1258 | { 1823850468512862308u, 1396701497859909157u }, 1259 | { 6891499104068465790u, 1745876872324886446u }, 1260 | { 17837745916940358045u, 2182346090406108057u }, 1261 | { 4231062170446641922u, 1363966306503817536u }, 1262 | { 5288827713058302403u, 1704957883129771920u }, 1263 | { 6611034641322878003u, 2131197353912214900u }, 1264 | { 13355268687681574560u, 1331998346195134312u }, 1265 | { 16694085859601968200u, 1664997932743917890u }, 1266 | { 11644235287647684442u, 2081247415929897363u }, 1267 | { 4971804045566108824u, 1300779634956185852u }, 1268 | { 6214755056957636030u, 1625974543695232315u }, 1269 | { 3156757802769657134u, 2032468179619040394u }, 1270 | { 6584659645158423613u, 1270292612261900246u }, 1271 | { 17454196593302805324u, 1587865765327375307u }, 1272 | { 17206059723201118751u, 1984832206659219134u }, 1273 | { 6142101308573311315u, 1240520129162011959u }, 1274 | { 3065940617289251240u, 1550650161452514949u }, 1275 | { 8444111790038951954u, 1938312701815643686u }, 1276 | { 665883850346957067u, 1211445438634777304u }, 1277 | { 832354812933696334u, 1514306798293471630u }, 1278 | { 10263815553021896226u, 1892883497866839537u }, 1279 | { 17944099766707154901u, 1183052186166774710u }, 1280 | { 13206752671529167818u, 1478815232708468388u }, 1281 | { 16508440839411459773u, 1848519040885585485u }, 1282 | { 12623618533845856310u, 1155324400553490928u }, 1283 | { 15779523167307320387u, 1444155500691863660u }, 1284 | { 1277659885424598868u, 1805194375864829576u }, 1285 | { 1597074856780748586u, 2256492969831036970u }, 1286 | { 5609857803915355770u, 1410308106144398106u }, 1287 | { 16235694291748970521u, 1762885132680497632u }, 1288 | { 1847873790976661535u, 2203606415850622041u }, 1289 | { 12684136165428883219u, 1377254009906638775u }, 1290 | { 11243484188358716120u, 1721567512383298469u }, 1291 | { 219297180166231438u, 2151959390479123087u }, 1292 | { 7054589765244976505u, 1344974619049451929u }, 1293 | { 13429923224983608535u, 1681218273811814911u }, 1294 | { 12175718012802122765u, 2101522842264768639u }, 1295 | { 14527352785642408584u, 1313451776415480399u }, 1296 | { 13547504963625622826u, 1641814720519350499u }, 1297 | { 12322695186104640628u, 2052268400649188124u }, 1298 | { 16925056528170176201u, 1282667750405742577u }, 1299 | { 7321262604930556539u, 1603334688007178222u }, 1300 | { 18374950293017971482u, 2004168360008972777u }, 1301 | { 4566814905495150320u, 1252605225005607986u }, 1302 | { 14931890668723713708u, 1565756531257009982u }, 1303 | { 9441491299049866327u, 1957195664071262478u }, 1304 | { 1289246043478778550u, 1223247290044539049u }, 1305 | { 6223243572775861092u, 1529059112555673811u }, 1306 | { 3167368447542438461u, 1911323890694592264u }, 1307 | { 1979605279714024038u, 1194577431684120165u }, 1308 | { 7086192618069917952u, 1493221789605150206u }, 1309 | { 18081112809442173248u, 1866527237006437757u }, 1310 | { 13606538515115052232u, 1166579523129023598u }, 1311 | { 7784801107039039482u, 1458224403911279498u }, 1312 | { 507629346944023544u, 1822780504889099373u }, 1313 | { 5246222702107417334u, 2278475631111374216u }, 1314 | { 3278889188817135834u, 1424047269444608885u }, 1315 | { 8710297504448807696u, 1780059086805761106u } 1316 | }; 1317 | 1318 | #endif 1319 | 1320 | #define DOUBLE_MANTISSA_BITS 52 1321 | #define DOUBLE_EXPONENT_BITS 11 1322 | #define DOUBLE_BIAS 1023 1323 | 1324 | static inline uint32_t decimalLength17(const uint64_t v) { 1325 | // This is slightly faster than a loop. 1326 | // The average output length is 16.38 digits, so we check high-to-low. 1327 | // Function precondition: v is not an 18, 19, or 20-digit number. 1328 | // (17 digits are sufficient for round-tripping.) 1329 | assert(v < 100000000000000000L); 1330 | if (v >= 10000000000000000L) { return 17; } 1331 | if (v >= 1000000000000000L) { return 16; } 1332 | if (v >= 100000000000000L) { return 15; } 1333 | if (v >= 10000000000000L) { return 14; } 1334 | if (v >= 1000000000000L) { return 13; } 1335 | if (v >= 100000000000L) { return 12; } 1336 | if (v >= 10000000000L) { return 11; } 1337 | if (v >= 1000000000L) { return 10; } 1338 | if (v >= 100000000L) { return 9; } 1339 | if (v >= 10000000L) { return 8; } 1340 | if (v >= 1000000L) { return 7; } 1341 | if (v >= 100000L) { return 6; } 1342 | if (v >= 10000L) { return 5; } 1343 | if (v >= 1000L) { return 4; } 1344 | if (v >= 100L) { return 3; } 1345 | if (v >= 10L) { return 2; } 1346 | return 1; 1347 | } 1348 | 1349 | // A floating decimal representing m * 10^e. 1350 | typedef struct floating_decimal_64 { 1351 | uint64_t mantissa; 1352 | // Decimal exponent's range is -324 to 308 1353 | // inclusive, and can fit in a short if needed. 1354 | int32_t exponent; 1355 | } floating_decimal_64; 1356 | 1357 | static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, 1358 | const uint32_t ieeeExponent) 1359 | { 1360 | int32_t e2; 1361 | uint64_t m2; 1362 | if (ieeeExponent == 0) { 1363 | // We subtract 2 so that the bounds computation has 2 additional bits. 1364 | e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2; 1365 | m2 = ieeeMantissa; 1366 | } else { 1367 | e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2; 1368 | m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa; 1369 | } 1370 | const bool even = (m2 & 1) == 0; 1371 | const bool acceptBounds = even; 1372 | 1373 | #ifdef RYU_DEBUG 1374 | printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2); 1375 | #endif 1376 | 1377 | // Step 2: Determine the interval of valid decimal representations. 1378 | const uint64_t mv = 4 * m2; 1379 | // Implicit bool -> int conversion. True is 1, false is 0. 1380 | const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1; 1381 | // We would compute mp and mm like this: 1382 | // uint64_t mp = 4 * m2 + 2; 1383 | // uint64_t mm = mv - 1 - mmShift; 1384 | 1385 | // Step 3: Convert to a decimal power base using 128-bit arithmetic. 1386 | uint64_t vr, vp, vm; 1387 | int32_t e10; 1388 | bool vmIsTrailingZeros = false; 1389 | bool vrIsTrailingZeros = false; 1390 | if (e2 >= 0) { 1391 | // I tried special-casing q == 0, but there was no effect on 1392 | // performance. 1393 | // This expression is slightly faster than max(0, log10Pow2(e2) - 1). 1394 | const uint32_t q = log10Pow2(e2) - (e2 > 3); 1395 | e10 = (int32_t) q; 1396 | const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1; 1397 | const int32_t i = -e2 + (int32_t) q + k; 1398 | #if defined(RYU_OPTIMIZE_SIZE) 1399 | uint64_t pow5[2]; 1400 | double_computeInvPow5(q, pow5); 1401 | vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift); 1402 | #else 1403 | vr = mulShiftAll64(m2, DOUBLE_POW5_INV_SPLIT[q], i, &vp, &vm, mmShift); 1404 | #endif 1405 | #ifdef RYU_DEBUG 1406 | printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q); 1407 | printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); 1408 | #endif 1409 | if (q <= 21) { 1410 | // This should use q <= 22, but I think 21 is also safe. Smaller 1411 | // values may still be safe, but it's more difficult to reason 1412 | // about them. Only one of mp, mv, and mm can be a multiple of 5, 1413 | // if any. 1414 | const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv)); 1415 | if (mvMod5 == 0) { 1416 | vrIsTrailingZeros = multipleOfPowerOf5(mv, q); 1417 | } else if (acceptBounds) { 1418 | // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q 1419 | // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q 1420 | // <=> true && pow5Factor(mm) >= q, since e2 >= q. 1421 | vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q); 1422 | } else { 1423 | // Same as min(e2 + 1, pow5Factor(mp)) >= q. 1424 | vp -= multipleOfPowerOf5(mv + 2, q); 1425 | } 1426 | } 1427 | } else { 1428 | // This expression is slightly faster than max(0, log10Pow5(-e2) - 1). 1429 | const uint32_t q = log10Pow5(-e2) - (-e2 > 1); 1430 | e10 = (int32_t) q + e2; 1431 | const int32_t i = -e2 - (int32_t) q; 1432 | const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT; 1433 | const int32_t j = (int32_t) q - k; 1434 | #if defined(RYU_OPTIMIZE_SIZE) 1435 | uint64_t pow5[2]; 1436 | double_computePow5(i, pow5); 1437 | vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift); 1438 | #else 1439 | vr = mulShiftAll64(m2, DOUBLE_POW5_SPLIT[i], j, &vp, &vm, mmShift); 1440 | #endif 1441 | #ifdef RYU_DEBUG 1442 | printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q); 1443 | printf("%u %d %d %d\n", q, i, k, j); 1444 | printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); 1445 | #endif 1446 | if (q <= 1) { 1447 | // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q 1448 | // trailing 0 bits. mv = 4 * m2, so it always has at least two 1449 | // trailing 0 bits. 1450 | vrIsTrailingZeros = true; 1451 | if (acceptBounds) { 1452 | // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff 1453 | // mmShift == 1. 1454 | vmIsTrailingZeros = mmShift == 1; 1455 | } else { 1456 | // mp = mv + 2, so it always has at least one trailing 0 bit. 1457 | --vp; 1458 | } 1459 | } else if (q < 63) { // TODO(ulfjack): Use a tighter bound here. 1460 | // We want to know if the full product has at least q trailing 1461 | // zeros. 1462 | // We need to compute min(p2(mv), p5(mv) - e2) >= q 1463 | // <=> p2(mv) >= q && p5(mv) - e2 >= q 1464 | // <=> p2(mv) >= q (because -e2 >= q) 1465 | vrIsTrailingZeros = multipleOfPowerOf2(mv, q); 1466 | #ifdef RYU_DEBUG 1467 | printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : 1468 | "false"); 1469 | #endif 1470 | } 1471 | } 1472 | #ifdef RYU_DEBUG 1473 | printf("e10=%d\n", e10); 1474 | printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); 1475 | printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false"); 1476 | printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false"); 1477 | #endif 1478 | 1479 | // Step 4: Find the shortest decimal representation in the interval of 1480 | // valid representations. 1481 | int32_t removed = 0; 1482 | uint8_t lastRemovedDigit = 0; 1483 | uint64_t output; 1484 | // On average, we remove ~2 digits. 1485 | if (vmIsTrailingZeros || vrIsTrailingZeros) { 1486 | // General case, which happens rarely (~0.7%). 1487 | for (;;) { 1488 | const uint64_t vpDiv10 = div10(vp); 1489 | const uint64_t vmDiv10 = div10(vm); 1490 | if (vpDiv10 <= vmDiv10) { 1491 | break; 1492 | } 1493 | const uint32_t vmMod10 = ((uint32_t) vm) - 10 * 1494 | ((uint32_t) vmDiv10); 1495 | const uint64_t vrDiv10 = div10(vr); 1496 | const uint32_t vrMod10 = ((uint32_t) vr) - 10 * 1497 | ((uint32_t) vrDiv10); 1498 | vmIsTrailingZeros &= vmMod10 == 0; 1499 | vrIsTrailingZeros &= lastRemovedDigit == 0; 1500 | lastRemovedDigit = (uint8_t) vrMod10; 1501 | vr = vrDiv10; 1502 | vp = vpDiv10; 1503 | vm = vmDiv10; 1504 | ++removed; 1505 | } 1506 | #ifdef RYU_DEBUG 1507 | printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); 1508 | printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false"); 1509 | #endif 1510 | if (vmIsTrailingZeros) { 1511 | for (;;) { 1512 | const uint64_t vmDiv10 = div10(vm); 1513 | const uint32_t vmMod10 = ((uint32_t) vm) - 10 * 1514 | ((uint32_t) vmDiv10); 1515 | if (vmMod10 != 0) { 1516 | break; 1517 | } 1518 | const uint64_t vpDiv10 = div10(vp); 1519 | const uint64_t vrDiv10 = div10(vr); 1520 | const uint32_t vrMod10 = ((uint32_t) vr) - 10 * 1521 | ((uint32_t) vrDiv10); 1522 | vrIsTrailingZeros &= lastRemovedDigit == 0; 1523 | lastRemovedDigit = (uint8_t) vrMod10; 1524 | vr = vrDiv10; 1525 | vp = vpDiv10; 1526 | vm = vmDiv10; 1527 | ++removed; 1528 | } 1529 | } 1530 | #ifdef RYU_DEBUG 1531 | printf("%" PRIu64 " %d\n", vr, lastRemovedDigit); 1532 | printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : 1533 | "false"); 1534 | #endif 1535 | if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) { 1536 | // Round even if the exact number is .....50..0. 1537 | lastRemovedDigit = 4; 1538 | } 1539 | // We need to take vr + 1 if vr is outside bounds or we need to round up. 1540 | output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || 1541 | lastRemovedDigit >= 5); 1542 | } else { 1543 | // Specialized for the common case (~99.3%). Percentages below are 1544 | // relative to this. 1545 | bool roundUp = false; 1546 | const uint64_t vpDiv100 = div100(vp); 1547 | const uint64_t vmDiv100 = div100(vm); 1548 | if (vpDiv100 > vmDiv100) { // Opt: remove two digits at a time (~86.2%). 1549 | const uint64_t vrDiv100 = div100(vr); 1550 | const uint32_t vrMod100 = ((uint32_t) vr) - 100 * 1551 | ((uint32_t) vrDiv100); 1552 | roundUp = vrMod100 >= 50; 1553 | vr = vrDiv100; 1554 | vp = vpDiv100; 1555 | vm = vmDiv100; 1556 | removed += 2; 1557 | } 1558 | // Loop iterations below (approximately), without optimization above: 1559 | // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02% 1560 | // Loop iterations below (approximately), with optimization above: 1561 | // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02% 1562 | for (;;) { 1563 | const uint64_t vpDiv10 = div10(vp); 1564 | const uint64_t vmDiv10 = div10(vm); 1565 | if (vpDiv10 <= vmDiv10) { 1566 | break; 1567 | } 1568 | const uint64_t vrDiv10 = div10(vr); 1569 | const uint32_t vrMod10 = ((uint32_t) vr) - 10 * 1570 | ((uint32_t) vrDiv10); 1571 | roundUp = vrMod10 >= 5; 1572 | vr = vrDiv10; 1573 | vp = vpDiv10; 1574 | vm = vmDiv10; 1575 | ++removed; 1576 | } 1577 | #ifdef RYU_DEBUG 1578 | printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false"); 1579 | printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : 1580 | "false"); 1581 | #endif 1582 | // We need to take vr + 1 if vr is outside bounds or we need to round 1583 | // up. 1584 | output = vr + (vr == vm || roundUp); 1585 | } 1586 | const int32_t exp = e10 + removed; 1587 | 1588 | #ifdef RYU_DEBUG 1589 | printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm); 1590 | printf("O=%" PRIu64 "\n", output); 1591 | printf("EXP=%d\n", exp); 1592 | #endif 1593 | 1594 | floating_decimal_64 fd; 1595 | fd.exponent = exp; 1596 | fd.mantissa = output; 1597 | return fd; 1598 | } 1599 | 1600 | static inline int to_chars(const floating_decimal_64 v, const bool sign, 1601 | char* const result) 1602 | { 1603 | // Step 5: Print the decimal representation. 1604 | int index = 0; 1605 | if (sign) { 1606 | result[index++] = '-'; 1607 | } 1608 | 1609 | uint64_t output = v.mantissa; 1610 | const uint32_t olength = decimalLength17(output); 1611 | 1612 | #ifdef RYU_DEBUG 1613 | printf("DIGITS=%" PRIu64 "\n", v.mantissa); 1614 | printf("OLEN=%u\n", olength); 1615 | printf("EXP=%u\n", v.exponent + olength); 1616 | #endif 1617 | 1618 | // Print the decimal digits. 1619 | // The following code is equivalent to: 1620 | // for (uint32_t i = 0; i < olength - 1; ++i) { 1621 | // const uint32_t c = output % 10; output /= 10; 1622 | // result[index + olength - i] = (char) ('0' + c); 1623 | // } 1624 | // result[index] = '0' + output % 10; 1625 | 1626 | uint32_t i = 0; 1627 | // We prefer 32-bit operations, even on 64-bit platforms. 1628 | // We have at most 17 digits, and uint32_t can store 9 digits. 1629 | // If output doesn't fit into uint32_t, we cut off 8 digits, 1630 | // so the rest will fit into uint32_t. 1631 | if ((output >> 32) != 0) { 1632 | // Expensive 64-bit division. 1633 | const uint64_t q = div1e8(output); 1634 | uint32_t output2 = ((uint32_t) output) - 100000000 * ((uint32_t) q); 1635 | output = q; 1636 | 1637 | const uint32_t c = output2 % 10000; 1638 | output2 /= 10000; 1639 | const uint32_t d = output2 % 10000; 1640 | const uint32_t c0 = (c % 100) << 1; 1641 | const uint32_t c1 = (c / 100) << 1; 1642 | const uint32_t d0 = (d % 100) << 1; 1643 | const uint32_t d1 = (d / 100) << 1; 1644 | memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2); 1645 | memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2); 1646 | memcpy(result + index + olength - i - 5, DIGIT_TABLE + d0, 2); 1647 | memcpy(result + index + olength - i - 7, DIGIT_TABLE + d1, 2); 1648 | i += 8; 1649 | } 1650 | uint32_t output2 = (uint32_t) output; 1651 | while (output2 >= 10000) { 1652 | #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217 1653 | const uint32_t c = output2 - 10000 * (output2 / 10000); 1654 | #else 1655 | const uint32_t c = output2 % 10000; 1656 | #endif 1657 | output2 /= 10000; 1658 | const uint32_t c0 = (c % 100) << 1; 1659 | const uint32_t c1 = (c / 100) << 1; 1660 | memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2); 1661 | memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2); 1662 | i += 4; 1663 | } 1664 | if (output2 >= 100) { 1665 | const uint32_t c = (output2 % 100) << 1; 1666 | output2 /= 100; 1667 | memcpy(result + index + olength - i - 1, DIGIT_TABLE + c, 2); 1668 | i += 2; 1669 | } 1670 | if (output2 >= 10) { 1671 | const uint32_t c = output2 << 1; 1672 | // We can't use memcpy here: the decimal dot goes between these two 1673 | // digits. 1674 | result[index + olength - i] = DIGIT_TABLE[c + 1]; 1675 | result[index] = DIGIT_TABLE[c]; 1676 | } else { 1677 | result[index] = (char) ('0' + output2); 1678 | } 1679 | 1680 | // Print decimal point if needed. 1681 | if (olength > 1) { 1682 | result[index + 1] = '.'; 1683 | index += olength + 1; 1684 | } else { 1685 | ++index; 1686 | } 1687 | 1688 | // Print the exponent. 1689 | result[index++] = 'E'; 1690 | int32_t exp = v.exponent + (int32_t) olength - 1; 1691 | if (exp < 0) { 1692 | result[index++] = '-'; 1693 | exp = -exp; 1694 | } 1695 | 1696 | if (exp >= 100) { 1697 | const int32_t c = exp % 10; 1698 | memcpy(result + index, DIGIT_TABLE + 2 * (exp / 10), 2); 1699 | result[index + 2] = (char) ('0' + c); 1700 | index += 3; 1701 | } else if (exp >= 10) { 1702 | memcpy(result + index, DIGIT_TABLE + 2 * exp, 2); 1703 | index += 2; 1704 | } else { 1705 | result[index++] = (char) ('0' + exp); 1706 | } 1707 | 1708 | return index; 1709 | } 1710 | 1711 | static inline bool d2d_small_int(const uint64_t ieeeMantissa, 1712 | const uint32_t ieeeExponent, floating_decimal_64* const v) 1713 | { 1714 | const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa; 1715 | const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - 1716 | DOUBLE_MANTISSA_BITS; 1717 | 1718 | if (e2 > 0) { 1719 | // f = m2 * 2^e2 >= 2^53 is an integer. 1720 | // Ignore this case for now. 1721 | return false; 1722 | } 1723 | 1724 | if (e2 < -52) { 1725 | // f < 1. 1726 | return false; 1727 | } 1728 | 1729 | // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53. 1730 | // Test if the lower -e2 bits of the significand are 0, i.e. whether the 1731 | // fraction is 0. 1732 | const uint64_t mask = (1ull << -e2) - 1; 1733 | const uint64_t fraction = m2 & mask; 1734 | if (fraction != 0) { 1735 | return false; 1736 | } 1737 | 1738 | // f is an integer in the range [1, 2^53). 1739 | // Note: mantissa might contain trailing (decimal) 0's. 1740 | // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17(). 1741 | v->mantissa = m2 >> -e2; 1742 | v->exponent = 0; 1743 | return true; 1744 | } 1745 | 1746 | static int d2s_buffered_n(double f, char* result) { 1747 | // Step 1: Decode the floating-point number, and unify normalized and 1748 | // subnormal cases. 1749 | const uint64_t bits = double_to_bits(f); 1750 | 1751 | #ifdef RYU_DEBUG 1752 | printf("IN="); 1753 | for (int32_t bit = 63; bit >= 0; --bit) { 1754 | printf("%d", (int) ((bits >> bit) & 1)); 1755 | } 1756 | printf("\n"); 1757 | #endif 1758 | 1759 | // Decode bits into sign, mantissa, and exponent. 1760 | const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + 1761 | DOUBLE_EXPONENT_BITS)) & 1) != 0; 1762 | const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1); 1763 | const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & 1764 | ((1u << DOUBLE_EXPONENT_BITS) - 1)); 1765 | // Case distinction; exit early for the easy cases. 1766 | if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || 1767 | (ieeeExponent == 0 && ieeeMantissa == 0)) 1768 | { 1769 | return copy_special_str(result, ieeeSign, ieeeExponent, ieeeMantissa); 1770 | } 1771 | 1772 | floating_decimal_64 v; 1773 | const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v); 1774 | if (isSmallInt) { 1775 | // For small integers in the range [1, 2^53), v.mantissa might contain 1776 | // trailing (decimal) zeros. 1777 | // For scientific notation we need to move these zeros into the 1778 | // exponent. 1779 | // (This is not needed for fixed-point notation, so it might be 1780 | // beneficial to trim 1781 | // trailing zeros in to_chars only if needed - once fixed-point 1782 | // notation output is implemented.) 1783 | for (;;) { 1784 | const uint64_t q = div10(v.mantissa); 1785 | const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q); 1786 | if (r != 0) { 1787 | break; 1788 | } 1789 | v.mantissa = q; 1790 | ++v.exponent; 1791 | } 1792 | } else { 1793 | v = d2d(ieeeMantissa, ieeeExponent); 1794 | } 1795 | 1796 | return to_chars(v, ieeeSign, result); 1797 | } 1798 | 1799 | static void d2s_buffered(double f, char* result) { 1800 | (void)umul128; (void)shiftright128; (void)mod1e9; 1801 | const int index = d2s_buffered_n(f, result); 1802 | 1803 | // Terminate the string. 1804 | result[index] = '\0'; 1805 | } 1806 | 1807 | #ifndef RYU_NOWRITER 1808 | struct writer { 1809 | uint8_t *dst; 1810 | size_t n; 1811 | size_t count; 1812 | }; 1813 | 1814 | static void write_nullterm(struct writer *wr) { 1815 | if (wr->n > wr->count) wr->dst[wr->count] = '\0'; 1816 | else if (wr->n > 0) wr->dst[wr->n-1] = '\0'; 1817 | } 1818 | 1819 | static void write_char(struct writer *wr, char b) { 1820 | if (wr->count < wr->n) wr->dst[wr->count] = b; 1821 | wr->count++; 1822 | } 1823 | #endif 1824 | 1825 | RYU_EXTERN 1826 | size_t ryu_string(double d, char fmt, char dst[], size_t nbytes) { 1827 | struct writer wr = { .dst = (uint8_t*)dst, .n = nbytes }; 1828 | char buf[25]; 1829 | bool f = true; 1830 | bool g = false; 1831 | bool j = false; 1832 | char ech = 'e'; 1833 | switch (fmt) { 1834 | case 'j': case 'J': 1835 | fmt -= 3; 1836 | j = true; 1837 | // fall through 1838 | case 'g': case 'G': 1839 | fmt -= 2; 1840 | g = true; 1841 | // fall through 1842 | case 'e': case 'E': 1843 | f = g; 1844 | if (fmt == 'E') ech = 'E'; 1845 | // fall through 1846 | case 'f': 1847 | d2s_buffered(d, buf); 1848 | break; 1849 | default: 1850 | buf[0] = '\0'; 1851 | } 1852 | bool neg = false; 1853 | char *p = buf; 1854 | if (p[0] == '-') { 1855 | write_char(&wr, '-'); 1856 | p++; 1857 | neg = true; 1858 | } 1859 | char *end = p; 1860 | char *e = NULL; 1861 | while (*end) { 1862 | if (*end >= 'E' && (*end == 'E' || *end == 'e')) { 1863 | e = end; 1864 | } 1865 | end++; 1866 | } 1867 | if (!e) { 1868 | if (*p == 'i' || *p == 'I') { 1869 | strcpy(p, "Infinity"); 1870 | } else if (*p == 'n' || *p == 'N') { 1871 | strcpy(p, "NaN"); 1872 | } else { 1873 | *p = '\0'; 1874 | } 1875 | while (*p) write_char(&wr, *(p++)); 1876 | write_nullterm(&wr); 1877 | return wr.count; 1878 | } 1879 | if (!f) { 1880 | *e = '\0'; 1881 | while (*p) write_char(&wr, *(p++)); 1882 | write_char(&wr, ech); 1883 | p++; 1884 | if (j && *p != '-') write_char(&wr, '+'); 1885 | while (*p) write_char(&wr, *(p++)); 1886 | write_nullterm(&wr); 1887 | return wr.count; 1888 | } 1889 | int en = atoi(e+1); 1890 | *e = '\0'; 1891 | if (en < 0) { 1892 | write_char(&wr, '0'); 1893 | write_char(&wr, '.'); 1894 | en = -en; 1895 | for (int i = 0; i < en-1; i++) { 1896 | write_char(&wr, '0'); 1897 | } 1898 | write_char(&wr, *(p++)); 1899 | if (*p) { 1900 | p++; 1901 | while (*p) write_char(&wr, *(p++)); 1902 | } 1903 | } else { 1904 | write_char(&wr, *(p++)); 1905 | if (*p) p++; 1906 | for (int i = 0; i < en; i++) { 1907 | if (*p) { 1908 | write_char(&wr, *(p++)); 1909 | } else { 1910 | write_char(&wr, '0'); 1911 | } 1912 | } 1913 | if (*p && !(*p == '0' && *(p+1) == '\0')) { 1914 | write_char(&wr, '.'); 1915 | while (*p) write_char(&wr, *(p++)); 1916 | } 1917 | } 1918 | write_nullterm(&wr); 1919 | if (g) { 1920 | bool rewrite = false; 1921 | if (j) { 1922 | rewrite = neg ? wr.count > 22 : wr.count > 21; 1923 | } else { 1924 | rewrite = (size_t)(end-buf) < wr.count; 1925 | } 1926 | if (rewrite) { 1927 | // rewind and rewrite the buffer 1928 | wr = (struct writer){ .dst = (uint8_t*)dst, .n = nbytes }; 1929 | p = buf; 1930 | *e = '\0'; 1931 | while (*p) write_char(&wr, *(p++)); 1932 | write_char(&wr, ech); 1933 | p++; 1934 | if (j && *p != '-') write_char(&wr, '+'); 1935 | while (*p) write_char(&wr, *(p++)); 1936 | write_nullterm(&wr); 1937 | } 1938 | } 1939 | return wr.count; 1940 | } 1941 | --------------------------------------------------------------------------------