├── LICENSE ├── README.md ├── go.mod ├── go.sum ├── maketables.go ├── ryu.go ├── ryu32.go ├── ryu64.go ├── ryu_test.go └── tables.go /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ryu 2 | 3 | [![GoDoc](https://godoc.org/github.com/cespare/ryu?status.svg)](https://godoc.org/github.com/cespare/ryu) 4 | 5 | This is a Go implementation of [Ryu](https://github.com/ulfjack/ryu), a fast 6 | algorithm for converting floating-point numbers to strings. 7 | 8 | The API is: 9 | 10 | ``` 11 | func AppendFloat32(b []byte, f float32) []byte 12 | func AppendFloat64(b []byte, f float64) []byte 13 | func FormatFloat32(f float32) string 14 | func FormatFloat64(f float64) string 15 | ``` 16 | 17 | These functions are the equivalents of calling strconv.FormatFloat or 18 | strconv.AppendFloat using the formatter `'e'` and precision `-1`: 19 | 20 | ``` 21 | // These are the same: 22 | const f float32 = 1.234 23 | s := ryu.FormatFloat32(f) 24 | s := strconv.FormatFloat(float64(f), 'e', -1, 32) 25 | ``` 26 | 27 | ## Benchmarks 28 | 29 | These benchmarks were taken with Go 1.12beta1 on Linux/amd64 using an 30 | Intel i7-8700K. 31 | 32 | ``` 33 | name old time/op new time/op delta 34 | FormatFloat32-12 128ns ± 1% 50ns ± 2% -60.82% (p=0.000 n=7+8) 35 | FormatFloat64-12 129ns ± 4% 65ns ± 5% -49.54% (p=0.000 n=7+8) 36 | AppendFloat32/0e+00-12 24.4ns ± 1% 3.0ns ± 1% -87.88% (p=0.000 n=8+8) 37 | AppendFloat32/1e+00-12 26.5ns ± 1% 13.2ns ± 3% -49.98% (p=0.000 n=8+8) 38 | AppendFloat32/3e-01-12 52.2ns ± 1% 32.5ns ± 2% -37.73% (p=0.000 n=8+8) 39 | AppendFloat32/1e+06-12 41.2ns ± 1% 17.9ns ± 1% -56.45% (p=0.000 n=8+7) 40 | AppendFloat32/-1.2345e+02-12 83.3ns ± 2% 34.2ns ± 1% -58.90% (p=0.000 n=8+8) 41 | AppendFloat64/0e+00-12 24.5ns ± 2% 3.3ns ± 2% -86.50% (p=0.000 n=8+8) 42 | AppendFloat64/1e+00-12 26.9ns ± 1% 14.5ns ± 1% -46.06% (p=0.001 n=8+6) 43 | AppendFloat64/3e-01-12 53.0ns ± 1% 42.5ns ± 0% -19.75% (p=0.001 n=8+6) 44 | AppendFloat64/1e+06-12 41.4ns ± 1% 21.1ns ± 1% -49.05% (p=0.000 n=8+8) 45 | AppendFloat64/-1.2345e+02-12 83.8ns ± 1% 43.3ns ± 1% -48.32% (p=0.000 n=8+8) 46 | AppendFloat64/6.226662346353213e-309-12 25.5µs ± 1% 0.0µs ± 1% -99.84% (p=0.000 n=8+8) 47 | ``` 48 | 49 | The test `TestRandomBenchmark` gathers statistics about the distribution of call 50 | latencies for random float64 values. Here is the summary for one sample of 10,000 51 | random floats: 52 | 53 | ``` 54 | ryu_test.go:279: after sampling 50000 float64s: 55 | ryu: min = 2ns max = 90ns median = 41ns mean = 41ns 56 | strconv (stdlib): min = 8ns max = 25845ns median = 106ns mean = 154ns 57 | ``` 58 | 59 | The `strconv.FormatFloat` latency is bimodal because of an infrequently-taken 60 | slow path that is orders of magnitude more expensive 61 | (https://golang.org/issue/15672). 62 | 63 | ## Size optimization 64 | 65 | The Ryu algorithm requires several lookup tables. Ulf Adams's C library 66 | implements a size optimization (`RYU_OPTIMIZE_SIZE`) which greatly reduces the 67 | size of the float64 tables in exchange for a little more CPU cost. 68 | 69 | I have a WIP implementation of this optimization on the `size` branch. A binary 70 | built using that version is 7.96 kB smaller. The benchmark results take a hit as 71 | compared with the non-size-optimized build: 72 | 73 | ``` 74 | name old time/op new time/op delta 75 | FormatFloat32-12 50.0ns ± 2% 49.4ns ± 1% ~ (p=0.183 n=8+8) 76 | FormatFloat64-12 65.0ns ± 5% 72.1ns ± 5% +10.96% (p=0.000 n=8+8) 77 | AppendFloat32/0e+00-12 2.95ns ± 1% 2.98ns ± 1% ~ (p=0.072 n=8+8) 78 | AppendFloat32/1e+00-12 13.2ns ± 3% 13.1ns ± 1% ~ (p=0.275 n=8+8) 79 | AppendFloat32/3e-01-12 32.5ns ± 2% 32.4ns ± 1% ~ (p=0.742 n=8+8) 80 | AppendFloat32/1e+06-12 17.9ns ± 1% 17.6ns ± 1% -2.12% (p=0.001 n=7+8) 81 | AppendFloat32/-1.2345e+02-12 34.2ns ± 1% 34.4ns ± 1% ~ (p=0.426 n=8+8) 82 | AppendFloat64/0e+00-12 3.31ns ± 2% 3.29ns ± 1% ~ (p=0.394 n=8+8) 83 | AppendFloat64/1e+00-12 14.5ns ± 1% 14.6ns ± 4% ~ (p=0.641 n=6+8) 84 | AppendFloat64/3e-01-12 42.5ns ± 0% 50.0ns ± 1% +17.44% (p=0.001 n=6+8) 85 | AppendFloat64/1e+06-12 21.1ns ± 1% 21.1ns ± 2% ~ (p=0.452 n=8+8) 86 | AppendFloat64/-1.2345e+02-12 43.3ns ± 1% 50.9ns ± 1% +17.57% (p=0.000 n=8+8) 87 | AppendFloat64/6.226662346353213e-309-12 40.6ns ± 1% 47.7ns ± 1% +17.38% (p=0.000 n=8+8) 88 | ``` 89 | 90 | However, it's still generally faster than strconv: 91 | 92 | ``` 93 | name old time/op new time/op delta 94 | FormatFloat32-12 129ns ± 2% 49ns ± 1% -61.72% (p=0.000 n=8+8) 95 | FormatFloat64-12 130ns ± 3% 72ns ± 5% -44.32% (p=0.000 n=7+8) 96 | AppendFloat32/0e+00-12 24.5ns ± 2% 3.0ns ± 1% -87.83% (p=0.000 n=8+8) 97 | AppendFloat32/1e+00-12 26.4ns ± 1% 13.1ns ± 1% -50.26% (p=0.000 n=7+8) 98 | AppendFloat32/3e-01-12 52.6ns ± 2% 32.4ns ± 1% -38.43% (p=0.000 n=8+8) 99 | AppendFloat32/1e+06-12 41.3ns ± 2% 17.6ns ± 1% -57.51% (p=0.000 n=8+8) 100 | AppendFloat32/-1.2345e+02-12 83.5ns ± 1% 34.4ns ± 1% -58.82% (p=0.000 n=8+8) 101 | AppendFloat64/0e+00-12 24.6ns ± 2% 3.3ns ± 1% -86.63% (p=0.000 n=8+8) 102 | AppendFloat64/1e+00-12 26.7ns ± 1% 14.6ns ± 4% -45.51% (p=0.000 n=8+8) 103 | AppendFloat64/3e-01-12 52.7ns ± 1% 50.0ns ± 1% -5.17% (p=0.000 n=8+8) 104 | AppendFloat64/1e+06-12 41.2ns ± 1% 21.1ns ± 2% -48.61% (p=0.000 n=7+8) 105 | AppendFloat64/-1.2345e+02-12 83.7ns ± 1% 50.9ns ± 1% -39.17% (p=0.000 n=8+8) 106 | AppendFloat64/6.226662346353213e-309-12 25.8µs ± 2% 0.0µs ± 1% -99.81% (p=0.000 n=8+8) 107 | ``` 108 | 109 | ## Notes 110 | 111 | This package is a fairly direct Go translation of Ulf Adams's C library at 112 | https://github.com/ulfjack/ryu. This code is also licensed with Apache 2.0 as a 113 | derived work of that code. 114 | 115 | This package requires Go 1.12 (expected to be released February 2019). 116 | 117 | For a small fraction of inputs, Ryu gives a different value than strconv does 118 | for the last digit. This is due to a bug in strconv: https://golang.org/issue/29491. 119 | 120 | ## Future work 121 | 122 | My plan is to incorporate this into strconv (see 123 | https://golang.org/issue/15672). Then everyone will benefit from the faster 124 | algorithm and there will be no need for this library. 125 | 126 | If you would like to contribute, I'm interested in any bugfixes or clear-cut 127 | optimizations, but given the above I don't intend to add more features or APIs 128 | to this package. 129 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/cespare/ryu 2 | 3 | go 1.12 4 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cespare/ryu/ba56a33f39e3bbbfa409095d0f9ae168a595feea/go.sum -------------------------------------------------------------------------------- /maketables.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Ulf Adams 2 | // Modifications copyright 2019 Caleb Spare 3 | // 4 | // Licensed under the Apache License, Version 2.0 (the "License"); 5 | // you may not use this file except in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, software 11 | // distributed under the License is distributed on an "AS IS" BASIS, 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | // See the License for the specific language governing permissions and 14 | // limitations under the License. 15 | // 16 | // The code in this file is a Go translation of the Java code written by 17 | // Ulf Adams which may be found at 18 | // 19 | // https://github.com/ulfjack/ryu/analysis/PrintFloatLookupTable.java 20 | // https://github.com/ulfjack/ryu/analysis/PrintDoubleLookupTable.java 21 | // 22 | // That source code is licensed under Apache 2.0 and this code is derivative 23 | // work thereof. 24 | 25 | // +build ignore 26 | 27 | // This program generates tables.go. 28 | 29 | package main 30 | 31 | import ( 32 | "bytes" 33 | "fmt" 34 | "go/format" 35 | "io/ioutil" 36 | "log" 37 | "math/big" 38 | ) 39 | 40 | var header = []byte(`// Code generated by running "go generate". DO NOT EDIT. 41 | 42 | // Copyright 2018 Ulf Adams 43 | // Modifications copyright 2019 Caleb Spare 44 | // 45 | // The contents of this file may be used under the terms of the Apache License, 46 | // Version 2.0. 47 | // 48 | // (See accompanying file LICENSE or copy at 49 | // http://www.apache.org/licenses/LICENSE-2.0) 50 | // 51 | // Unless required by applicable law or agreed to in writing, this software 52 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 53 | // KIND, either express or implied. 54 | // 55 | // The code in this file is part of a Go translation of the C code written by 56 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 57 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 58 | 59 | package ryu 60 | 61 | `) 62 | 63 | const ( 64 | posTableSize32 = 47 65 | negTableSize32 = 31 66 | pow5NumBits32 = 61 // max 63 67 | pow5InvNumBits32 = 59 // max 63 68 | 69 | posTableSize64 = 326 70 | negTableSize64 = 291 + 1 71 | pow5NumBits64 = 121 // max 127 72 | pow5InvNumBits64 = 122 // max 127 73 | ) 74 | 75 | func main() { 76 | b := bytes.NewBuffer(header) 77 | 78 | fmt.Fprintf(b, "const pow5NumBits32 = %d\n", pow5NumBits32) 79 | fmt.Fprintln(b, "var pow5Split32 = [...]uint64{") 80 | for i := int64(0); i < posTableSize32; i++ { 81 | pow5 := big.NewInt(5) 82 | pow5.Exp(pow5, big.NewInt(i), nil) 83 | shift := pow5.BitLen() - pow5NumBits32 84 | rsh(pow5, shift) 85 | fmt.Fprintf(b, "%d,", pow5.Uint64()) 86 | if i%4 == 3 { 87 | fmt.Fprintln(b) 88 | } 89 | } 90 | fmt.Fprintln(b, "\n}") 91 | 92 | fmt.Fprintf(b, "const pow5InvNumBits32 = %d\n", pow5InvNumBits32) 93 | fmt.Fprintln(b, "var pow5InvSplit32 = [...]uint64{") 94 | for i := int64(0); i < negTableSize32; i++ { 95 | pow5 := big.NewInt(5) 96 | pow5.Exp(pow5, big.NewInt(i), nil) 97 | shift := pow5.BitLen() - 1 + pow5InvNumBits32 98 | inv := big.NewInt(1) 99 | rsh(inv, -shift) 100 | inv.Quo(inv, pow5) 101 | inv.Add(inv, big.NewInt(1)) 102 | fmt.Fprintf(b, "%d,", inv.Uint64()) 103 | if i%4 == 3 { 104 | fmt.Fprintln(b) 105 | } 106 | } 107 | fmt.Fprintln(b, "\n}") 108 | 109 | mask64 := big.NewInt(1) 110 | mask64.Lsh(mask64, 64) 111 | mask64.Sub(mask64, big.NewInt(1)) 112 | 113 | fmt.Fprintf(b, "const pow5NumBits64 = %d\n", pow5NumBits64) 114 | fmt.Fprintln(b, "var pow5Split64 = [...]uint128{") 115 | for i := int64(0); i < posTableSize64; i++ { 116 | pow5 := big.NewInt(5) 117 | pow5.Exp(pow5, big.NewInt(i), nil) 118 | shift := pow5.BitLen() - pow5NumBits64 119 | rsh(pow5, shift) 120 | lo := new(big.Int).And(pow5, mask64) 121 | hi := new(big.Int).Rsh(pow5, 64) 122 | fmt.Fprintf(b, "{%d, %d},\n", lo.Uint64(), hi.Uint64()) 123 | } 124 | fmt.Fprintln(b, "\n}") 125 | 126 | fmt.Fprintf(b, "const pow5InvNumBits64 = %d\n", pow5InvNumBits64) 127 | fmt.Fprintln(b, "var pow5InvSplit64 = [...]uint128{") 128 | for i := int64(0); i < negTableSize64; i++ { 129 | pow5 := big.NewInt(5) 130 | pow5.Exp(pow5, big.NewInt(i), nil) 131 | // We want floor(log_2 5^q) here, which is pow5.BitLen() - 1. 132 | shift := pow5.BitLen() - 1 + pow5InvNumBits64 133 | inv := big.NewInt(1) 134 | rsh(inv, -shift) 135 | inv.Quo(inv, pow5) 136 | inv.Add(inv, big.NewInt(1)) 137 | lo := new(big.Int).And(inv, mask64) 138 | hi := new(big.Int).Rsh(inv, 64) 139 | fmt.Fprintf(b, "{%d, %d},\n", lo.Uint64(), hi.Uint64()) 140 | } 141 | fmt.Fprintln(b, "\n}") 142 | 143 | text, err := format.Source(b.Bytes()) 144 | if err != nil { 145 | log.Fatal(err) 146 | } 147 | if err := ioutil.WriteFile("tables.go", text, 0644); err != nil { 148 | log.Fatal(err) 149 | } 150 | } 151 | 152 | func rsh(x *big.Int, n int) { 153 | if n < 0 { 154 | x.Lsh(x, uint(-n)) 155 | } else { 156 | x.Rsh(x, uint(n)) 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /ryu.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Ulf Adams 2 | // Modifications copyright 2019 Caleb Spare 3 | // 4 | // The contents of this file may be used under the terms of the Apache License, 5 | // Version 2.0. 6 | // 7 | // (See accompanying file LICENSE or copy at 8 | // http://www.apache.org/licenses/LICENSE-2.0) 9 | // 10 | // Unless required by applicable law or agreed to in writing, this software 11 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 | // KIND, either express or implied. 13 | // 14 | // The code in this file is part of a Go translation of the C code written by 15 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 16 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 17 | 18 | // Package ryu implements the Ryu algorithm for quickly converting floating 19 | // point numbers into strings. 20 | package ryu 21 | 22 | import ( 23 | "math" 24 | "reflect" 25 | "unsafe" 26 | ) 27 | 28 | //go:generate go run maketables.go 29 | 30 | const ( 31 | mantBits32 = 23 32 | expBits32 = 8 33 | bias32 = 127 34 | 35 | mantBits64 = 52 36 | expBits64 = 11 37 | bias64 = 1023 38 | ) 39 | 40 | // FormatFloat32 converts a 32-bit floating point number f to a string. 41 | // It behaves like strconv.FormatFloat(float64(f), 'e', -1, 32). 42 | func FormatFloat32(f float32) string { 43 | b := make([]byte, 0, 15) 44 | b = AppendFloat32(b, f) 45 | 46 | // Convert the output to a string without copying. 47 | var s string 48 | sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) 49 | sh.Data = uintptr(unsafe.Pointer(&b[0])) 50 | sh.Len = len(b) 51 | return s 52 | } 53 | 54 | // AppendFloat32 appends the string form of the 32-bit floating point number f, 55 | // as generated by FormatFloat32, to b and returns the extended buffer. 56 | func AppendFloat32(b []byte, f float32) []byte { 57 | // Step 1: Decode the floating-point number. 58 | // Unify normalized and subnormal cases. 59 | u := math.Float32bits(f) 60 | neg := u>>(mantBits32+expBits32) != 0 61 | mant := u & (uint32(1)<> mantBits32) & (uint32(1)<>(mantBits64+expBits64) != 0 97 | mant := u & (uint64(1)<> mantBits64) & (uint64(1)<= 0, "e >= 0") 140 | assert(e <= 1650, "e <= 1650") 141 | return (uint32(e) * 78913) >> 18 142 | } 143 | 144 | // log10Pow5 returns floor(log_10(5^e)). 145 | func log10Pow5(e int32) uint32 { 146 | // The first value this approximation fails for is 5^2621 147 | // which is just greater than 10^1832. 148 | assert(e >= 0, "e >= 0") 149 | assert(e <= 2620, "e <= 2620") 150 | return (uint32(e) * 732923) >> 20 151 | } 152 | 153 | // pow5Bits returns ceil(log_2(5^e)), or else 1 if e==0. 154 | func pow5Bits(e int32) int32 { 155 | // This approximation works up to the point that the multiplication 156 | // overflows at e = 3529. If the multiplication were done in 64 bits, 157 | // it would fail at 5^4004 which is just greater than 2^9297. 158 | assert(e >= 0, "e >= 0") 159 | assert(e <= 3528, "e <= 3528") 160 | return int32((uint32(e)*1217359)>>19 + 1) 161 | } 162 | 163 | // These boolToXxx all inline as a movzx. 164 | 165 | func boolToInt(b bool) int { 166 | if b { 167 | return 1 168 | } 169 | return 0 170 | } 171 | 172 | func boolToUint32(b bool) uint32 { 173 | if b { 174 | return 1 175 | } 176 | return 0 177 | } 178 | 179 | func boolToUint64(b bool) uint64 { 180 | if b { 181 | return 1 182 | } 183 | return 0 184 | } 185 | -------------------------------------------------------------------------------- /ryu32.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Ulf Adams 2 | // Modifications copyright 2019 Caleb Spare 3 | // 4 | // The contents of this file may be used under the terms of the Apache License, 5 | // Version 2.0. 6 | // 7 | // (See accompanying file LICENSE or copy at 8 | // http://www.apache.org/licenses/LICENSE-2.0) 9 | // 10 | // Unless required by applicable law or agreed to in writing, this software 11 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 | // KIND, either express or implied. 13 | // 14 | // The code in this file is part of a Go translation of the C code written by 15 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 16 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 17 | 18 | package ryu 19 | 20 | import ( 21 | "math" 22 | "math/bits" 23 | ) 24 | 25 | // dec32 is a floating decimal type representing m * 10^e. 26 | type dec32 struct { 27 | m uint32 28 | e int32 29 | } 30 | 31 | func (d dec32) append(b []byte, neg bool) []byte { 32 | // Step 5: Print the decimal representation. 33 | if neg { 34 | b = append(b, '-') 35 | } 36 | 37 | out := d.m 38 | outLen := decimalLen32(out) 39 | bufLen := outLen 40 | if bufLen > 1 { 41 | bufLen++ // extra space for '.' 42 | } 43 | 44 | // Print the decimal digits. 45 | n := len(b) 46 | b = append(b, make([]byte, bufLen)...) 47 | for i := 0; i < outLen-1; i++ { 48 | b[n+outLen-i] = '0' + byte(out%10) 49 | out /= 10 50 | } 51 | b[n] = '0' + byte(out%10) 52 | 53 | // Print the '.' if needed. 54 | if outLen > 1 { 55 | b[n+1] = '.' 56 | } 57 | 58 | // Print the exponent. 59 | b = append(b, 'e') 60 | exp := d.e + int32(outLen) - 1 61 | if exp < 0 { 62 | b = append(b, '-') 63 | exp = -exp 64 | } else { 65 | // Unconditionally print a + here to match strconv's formatting. 66 | b = append(b, '+') 67 | } 68 | // Always print two digits to match strconv's formatting. 69 | d1 := exp % 10 70 | d0 := exp / 10 71 | b = append(b, '0'+byte(d0), '0'+byte(d1)) 72 | 73 | return b 74 | } 75 | 76 | func float32ToDecimalExactInt(mant, exp uint32) (d dec32, ok bool) { 77 | e := exp - bias32 78 | if e > mantBits32 { 79 | return d, false 80 | } 81 | shift := mantBits32 - e 82 | mant |= 1 << mantBits32 // implicit 1 83 | d.m = mant >> shift 84 | if d.m<= 0 { 126 | q := log10Pow2(e2) 127 | e10 = int32(q) 128 | k := pow5InvNumBits32 + pow5Bits(int32(q)) - 1 129 | i := -e2 + int32(q) + k 130 | vr = mulPow5InvDivPow2(mv, q, i) 131 | vp = mulPow5InvDivPow2(mp, q, i) 132 | vm = mulPow5InvDivPow2(mm, q, i) 133 | if q != 0 && (vp-1)/10 <= vm/10 { 134 | // We need to know one removed digit even if we are not 135 | // going to loop below. We could use q = X - 1 above, 136 | // except that would require 33 bits for the result, and 137 | // we've found that 32-bit arithmetic is faster even on 138 | // 64-bit machines. 139 | l := pow5InvNumBits32 + pow5Bits(int32(q-1)) - 1 140 | lastRemovedDigit = uint8(mulPow5InvDivPow2(mv, q-1, -e2+int32(q-1)+l) % 10) 141 | } 142 | if q <= 9 { 143 | // The largest power of 5 that fits in 24 bits is 5^10, 144 | // but q <= 9 seems to be safe as well. Only one of mp, 145 | // mv, and mm can be a multiple of 5, if any. 146 | if mv%5 == 0 { 147 | vrIsTrailingZeros = multipleOfPowerOfFive32(mv, q) 148 | } else if acceptBounds { 149 | vmIsTrailingZeros = multipleOfPowerOfFive32(mm, q) 150 | } else if multipleOfPowerOfFive32(mp, q) { 151 | vp-- 152 | } 153 | } 154 | } else { 155 | q := log10Pow5(-e2) 156 | e10 = int32(q) + e2 157 | i := -e2 - int32(q) 158 | k := pow5Bits(i) - pow5NumBits32 159 | j := int32(q) - k 160 | vr = mulPow5DivPow2(mv, uint32(i), j) 161 | vp = mulPow5DivPow2(mp, uint32(i), j) 162 | vm = mulPow5DivPow2(mm, uint32(i), j) 163 | if q != 0 && (vp-1)/10 <= vm/10 { 164 | j = int32(q) - 1 - (pow5Bits(i+1) - pow5NumBits32) 165 | lastRemovedDigit = uint8(mulPow5DivPow2(mv, uint32(i+1), j) % 10) 166 | } 167 | if q <= 1 { 168 | // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at 169 | // least q trailing 0 bits. mv = 4 * m2, so it always 170 | // has at least two trailing 0 bits. 171 | vrIsTrailingZeros = true 172 | if acceptBounds { 173 | // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit 174 | // iff mmShift == 1. 175 | vmIsTrailingZeros = mmShift == 1 176 | } else { 177 | // mp = mv + 2, so it always has at least one 178 | // trailing 0 bit. 179 | vp-- 180 | } 181 | } else if q < 31 { 182 | vrIsTrailingZeros = multipleOfPowerOfTwo32(mv, q-1) 183 | } 184 | } 185 | 186 | // Step 4: Find the shortest decimal representation 187 | // in the interval of valid representations. 188 | var removed int32 189 | var out uint32 190 | if vmIsTrailingZeros || vrIsTrailingZeros { 191 | // General case, which happens rarely (~4.0%). 192 | for vp/10 > vm/10 { 193 | vmIsTrailingZeros = vmIsTrailingZeros && vm%10 == 0 194 | vrIsTrailingZeros = vrIsTrailingZeros && lastRemovedDigit == 0 195 | lastRemovedDigit = uint8(vr % 10) 196 | vr /= 10 197 | vp /= 10 198 | vm /= 10 199 | removed++ 200 | } 201 | if vmIsTrailingZeros { 202 | for vm%10 == 0 { 203 | vrIsTrailingZeros = vrIsTrailingZeros && lastRemovedDigit == 0 204 | lastRemovedDigit = uint8(vr % 10) 205 | vr /= 10 206 | vp /= 10 207 | vm /= 10 208 | removed++ 209 | } 210 | } 211 | if vrIsTrailingZeros && lastRemovedDigit == 5 && vr%2 == 0 { 212 | // Round even if the exact number is .....50..0. 213 | lastRemovedDigit = 4 214 | } 215 | out = vr 216 | // We need to take vr + 1 if vr is outside bounds 217 | // or we need to round up. 218 | if (vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5 { 219 | out++ 220 | } 221 | } else { 222 | // Specialized for the common case (~96.0%). Percentages below 223 | // are relative to this. Loop iterations below (approximately): 224 | // 0: 13.6%, 1: 70.7%, 2: 14.1%, 3: 1.39%, 4: 0.14%, 5+: 0.01% 225 | for vp/10 > vm/10 { 226 | lastRemovedDigit = uint8(vr % 10) 227 | vr /= 10 228 | vp /= 10 229 | vm /= 10 230 | removed++ 231 | } 232 | // We need to take vr + 1 if vr is outside bounds 233 | // or we need to round up. 234 | out = vr + boolToUint32(vr == vm || lastRemovedDigit >= 5) 235 | } 236 | 237 | return dec32{m: out, e: e10 + removed} 238 | } 239 | 240 | func decimalLen32(u uint32) int { 241 | // Function precondition: u is not a 10-digit number. 242 | // (9 digits are sufficient for round-tripping.) 243 | // This benchmarked faster than the log2 approach used for uint64s. 244 | assert(u < 1000000000, "too big") 245 | switch { 246 | case u >= 100000000: 247 | return 9 248 | case u >= 10000000: 249 | return 8 250 | case u >= 1000000: 251 | return 7 252 | case u >= 100000: 253 | return 6 254 | case u >= 10000: 255 | return 5 256 | case u >= 1000: 257 | return 4 258 | case u >= 100: 259 | return 3 260 | case u >= 10: 261 | return 2 262 | default: 263 | return 1 264 | } 265 | } 266 | 267 | func mulShift32(m uint32, mul uint64, shift int32) uint32 { 268 | assert(shift > 32, "shift > 32") 269 | 270 | hi, lo := bits.Mul64(uint64(m), mul) 271 | shiftedSum := (lo >> uint(shift)) + (hi << uint(64-shift)) 272 | assert(shiftedSum <= math.MaxUint32, "shiftedSum <= math.MaxUint32") 273 | return uint32(shiftedSum) 274 | } 275 | 276 | func mulPow5InvDivPow2(m, q uint32, j int32) uint32 { 277 | return mulShift32(m, pow5InvSplit32[q], j) 278 | } 279 | 280 | func mulPow5DivPow2(m, i uint32, j int32) uint32 { 281 | return mulShift32(m, pow5Split32[i], j) 282 | } 283 | 284 | func pow5Factor32(v uint32) uint32 { 285 | for n := uint32(0); ; n++ { 286 | q, r := v/5, v%5 287 | if r != 0 { 288 | return n 289 | } 290 | v = q 291 | } 292 | } 293 | 294 | // multipleOfPowerOfFive32 reports whether v is divisible by 5^p. 295 | func multipleOfPowerOfFive32(v, p uint32) bool { 296 | return pow5Factor32(v) >= p 297 | } 298 | 299 | // multipleOfPowerOfTwo32 reports whether v is divisible by 2^p. 300 | func multipleOfPowerOfTwo32(v, p uint32) bool { 301 | return uint32(bits.TrailingZeros32(v)) >= p 302 | } 303 | -------------------------------------------------------------------------------- /ryu64.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Ulf Adams 2 | // Modifications copyright 2019 Caleb Spare 3 | // 4 | // The contents of this file may be used under the terms of the Apache License, 5 | // Version 2.0. 6 | // 7 | // (See accompanying file LICENSE or copy at 8 | // http://www.apache.org/licenses/LICENSE-2.0) 9 | // 10 | // Unless required by applicable law or agreed to in writing, this software 11 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 | // KIND, either express or implied. 13 | // 14 | // The code in this file is part of a Go translation of the C code written by 15 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 16 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 17 | 18 | package ryu 19 | 20 | import ( 21 | "math/bits" 22 | ) 23 | 24 | type uint128 struct { 25 | lo uint64 26 | hi uint64 27 | } 28 | 29 | // dec64 is a floating decimal type representing m * 10^e. 30 | type dec64 struct { 31 | m uint64 32 | e int32 33 | } 34 | 35 | func (d dec64) append(b []byte, neg bool) []byte { 36 | // Step 5: Print the decimal representation. 37 | if neg { 38 | b = append(b, '-') 39 | } 40 | 41 | out := d.m 42 | outLen := decimalLen64(out) 43 | bufLen := outLen 44 | if bufLen > 1 { 45 | bufLen++ // extra space for '.' 46 | } 47 | 48 | // Print the decimal digits. 49 | n := len(b) 50 | if cap(b)-len(b) >= bufLen { 51 | // Avoid function call in the common case. 52 | b = b[:len(b)+bufLen] 53 | } else { 54 | b = append(b, make([]byte, bufLen)...) 55 | } 56 | 57 | // Avoid expensive 64-bit divisions. 58 | // We have at most 17 digits, and uint32 can store 9 digits. 59 | // If the output doesn't fit into a uint32, cut off 8 digits 60 | // so the rest will fit into a uint32. 61 | var i int 62 | if out>>32 > 0 { 63 | var out32 uint32 64 | out, out32 = out/1e8, uint32(out%1e8) 65 | for ; i < 8; i++ { 66 | b[n+outLen-i] = '0' + byte(out32%10) 67 | out32 /= 10 68 | } 69 | } 70 | out32 := uint32(out) 71 | for ; i < outLen-1; i++ { 72 | b[n+outLen-i] = '0' + byte(out32%10) 73 | out32 /= 10 74 | } 75 | b[n] = '0' + byte(out32%10) 76 | 77 | // Print the '.' if needed. 78 | if outLen > 1 { 79 | b[n+1] = '.' 80 | } 81 | 82 | // Print the exponent. 83 | b = append(b, 'e') 84 | exp := d.e + int32(outLen) - 1 85 | if exp < 0 { 86 | b = append(b, '-') 87 | exp = -exp 88 | } else { 89 | // Unconditionally print a + here to match strconv's formatting. 90 | b = append(b, '+') 91 | } 92 | // Always print at least two digits to match strconv's formatting. 93 | d2 := exp % 10 94 | exp /= 10 95 | d1 := exp % 10 96 | d0 := exp / 10 97 | if d0 > 0 { 98 | b = append(b, '0'+byte(d0)) 99 | } 100 | b = append(b, '0'+byte(d1), '0'+byte(d2)) 101 | 102 | return b 103 | } 104 | 105 | func float64ToDecimalExactInt(mant, exp uint64) (d dec64, ok bool) { 106 | e := exp - bias64 107 | if e > mantBits64 { 108 | return d, false 109 | } 110 | shift := mantBits64 - e 111 | mant |= 1 << mantBits64 // implicit 1 112 | d.m = mant >> shift 113 | if d.m<= 0 { 154 | // This expression is slightly faster than max(0, log10Pow2(e2) - 1). 155 | q := log10Pow2(e2) - boolToUint32(e2 > 3) 156 | e10 = int32(q) 157 | k := pow5InvNumBits64 + pow5Bits(int32(q)) - 1 158 | i := -e2 + int32(q) + k 159 | mul := pow5InvSplit64[q] 160 | vr = mulShift64(4*m2, mul, i) 161 | vp = mulShift64(4*m2+2, mul, i) 162 | vm = mulShift64(4*m2-1-mmShift, mul, i) 163 | if q <= 21 { 164 | // This should use q <= 22, but I think 21 is also safe. 165 | // Smaller values may still be safe, but it's more 166 | // difficult to reason about them. Only one of mp, mv, 167 | // and mm can be a multiple of 5, if any. 168 | if mv%5 == 0 { 169 | vrIsTrailingZeros = multipleOfPowerOfFive64(mv, q) 170 | } else if acceptBounds { 171 | // Same as min(e2 + (^mm & 1), pow5Factor64(mm)) >= q 172 | // <=> e2 + (^mm & 1) >= q && pow5Factor64(mm) >= q 173 | // <=> true && pow5Factor64(mm) >= q, since e2 >= q. 174 | vmIsTrailingZeros = multipleOfPowerOfFive64(mv-1-mmShift, q) 175 | } else if multipleOfPowerOfFive64(mv+2, q) { 176 | vp-- 177 | } 178 | } 179 | } else { 180 | // This expression is slightly faster than max(0, log10Pow5(-e2) - 1). 181 | q := log10Pow5(-e2) - boolToUint32(-e2 > 1) 182 | e10 = int32(q) + e2 183 | i := -e2 - int32(q) 184 | k := pow5Bits(i) - pow5NumBits64 185 | j := int32(q) - k 186 | mul := pow5Split64[i] 187 | vr = mulShift64(4*m2, mul, j) 188 | vp = mulShift64(4*m2+2, mul, j) 189 | vm = mulShift64(4*m2-1-mmShift, mul, j) 190 | if q <= 1 { 191 | // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits. 192 | // mv = 4 * m2, so it always has at least two trailing 0 bits. 193 | vrIsTrailingZeros = true 194 | if acceptBounds { 195 | // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1. 196 | vmIsTrailingZeros = mmShift == 1 197 | } else { 198 | // mp = mv + 2, so it always has at least one trailing 0 bit. 199 | vp-- 200 | } 201 | } else if q < 63 { // TODO(ulfjack/cespare): Use a tighter bound here. 202 | // We need to compute min(ntz(mv), pow5Factor64(mv) - e2) >= q - 1 203 | // <=> ntz(mv) >= q - 1 && pow5Factor64(mv) - e2 >= q - 1 204 | // <=> ntz(mv) >= q - 1 (e2 is negative and -e2 >= q) 205 | // <=> (mv & ((1 << (q - 1)) - 1)) == 0 206 | // We also need to make sure that the left shift does not overflow. 207 | vrIsTrailingZeros = multipleOfPowerOfTwo64(mv, q-1) 208 | } 209 | } 210 | 211 | // Step 4: Find the shortest decimal representation 212 | // in the interval of valid representations. 213 | var removed int32 214 | var lastRemovedDigit uint8 215 | var out uint64 216 | // On average, we remove ~2 digits. 217 | if vmIsTrailingZeros || vrIsTrailingZeros { 218 | // General case, which happens rarely (~0.7%). 219 | for { 220 | vpDiv10 := vp / 10 221 | vmDiv10 := vm / 10 222 | if vpDiv10 <= vmDiv10 { 223 | break 224 | } 225 | vmMod10 := vm % 10 226 | vrDiv10 := vr / 10 227 | vrMod10 := vr % 10 228 | vmIsTrailingZeros = vmIsTrailingZeros && vmMod10 == 0 229 | vrIsTrailingZeros = vrIsTrailingZeros && lastRemovedDigit == 0 230 | lastRemovedDigit = uint8(vrMod10) 231 | vr = vrDiv10 232 | vp = vpDiv10 233 | vm = vmDiv10 234 | removed++ 235 | } 236 | if vmIsTrailingZeros { 237 | for { 238 | vmDiv10 := vm / 10 239 | vmMod10 := vm % 10 240 | if vmMod10 != 0 { 241 | break 242 | } 243 | vpDiv10 := vp / 10 244 | vrDiv10 := vr / 10 245 | vrMod10 := vr % 10 246 | vrIsTrailingZeros = vrIsTrailingZeros && lastRemovedDigit == 0 247 | lastRemovedDigit = uint8(vrMod10) 248 | vr = vrDiv10 249 | vp = vpDiv10 250 | vm = vmDiv10 251 | removed++ 252 | } 253 | } 254 | if vrIsTrailingZeros && lastRemovedDigit == 5 && vr%2 == 0 { 255 | // Round even if the exact number is .....50..0. 256 | lastRemovedDigit = 4 257 | } 258 | out = vr 259 | // We need to take vr + 1 if vr is outside bounds 260 | // or we need to round up. 261 | if (vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5 { 262 | out++ 263 | } 264 | } else { 265 | // Specialized for the common case (~99.3%). 266 | // Percentages below are relative to this. 267 | roundUp := false 268 | for vp/100 > vm/100 { 269 | // Optimization: remove two digits at a time (~86.2%). 270 | roundUp = vr%100 >= 50 271 | vr /= 100 272 | vp /= 100 273 | vm /= 100 274 | removed += 2 275 | } 276 | // Loop iterations below (approximately), without optimization above: 277 | // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02% 278 | // Loop iterations below (approximately), with optimization above: 279 | // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02% 280 | for vp/10 > vm/10 { 281 | roundUp = vr%10 >= 5 282 | vr /= 10 283 | vp /= 10 284 | vm /= 10 285 | removed++ 286 | } 287 | // We need to take vr + 1 if vr is outside bounds 288 | // or we need to round up. 289 | out = vr + boolToUint64(vr == vm || roundUp) 290 | } 291 | 292 | return dec64{m: out, e: e10 + removed} 293 | } 294 | 295 | var powersOf10 = [...]uint64{ 296 | 1e0, 297 | 1e1, 298 | 1e2, 299 | 1e3, 300 | 1e4, 301 | 1e5, 302 | 1e6, 303 | 1e7, 304 | 1e8, 305 | 1e9, 306 | 1e10, 307 | 1e11, 308 | 1e12, 309 | 1e13, 310 | 1e14, 311 | 1e15, 312 | 1e16, 313 | 1e17, 314 | // We only need to find the length of at most 17 digit numbers. 315 | } 316 | 317 | func decimalLen64(u uint64) int { 318 | // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLog10 319 | log2 := 64 - bits.LeadingZeros64(u) - 1 320 | t := (log2 + 1) * 1233 >> 12 321 | return t - boolToInt(u < powersOf10[t]) + 1 322 | } 323 | 324 | func mulShift64(m uint64, mul uint128, shift int32) uint64 { 325 | hihi, hilo := bits.Mul64(m, mul.hi) 326 | lohi, _ := bits.Mul64(m, mul.lo) 327 | sum := uint128{hi: hihi, lo: lohi + hilo} 328 | if sum.lo < lohi { 329 | sum.hi++ // overflow 330 | } 331 | return shiftRight128(sum, shift-64) 332 | } 333 | 334 | func shiftRight128(v uint128, shift int32) uint64 { 335 | // The shift value is always modulo 64. 336 | // In the current implementation of the 64-bit version 337 | // of Ryu, the shift value is always < 64. 338 | // (It is in the range [2, 59].) 339 | // Check this here in case a future change requires larger shift 340 | // values. In this case this function needs to be adjusted. 341 | assert(shift < 64, "shift < 64") 342 | return (v.hi << uint64(64-shift)) | (v.lo >> uint(shift)) 343 | } 344 | 345 | func pow5Factor64(v uint64) uint32 { 346 | for n := uint32(0); ; n++ { 347 | q, r := v/5, v%5 348 | if r != 0 { 349 | return n 350 | } 351 | v = q 352 | } 353 | } 354 | 355 | func multipleOfPowerOfFive64(v uint64, p uint32) bool { 356 | return pow5Factor64(v) >= p 357 | } 358 | 359 | func multipleOfPowerOfTwo64(v uint64, p uint32) bool { 360 | return uint32(bits.TrailingZeros64(v)) >= p 361 | } 362 | -------------------------------------------------------------------------------- /ryu_test.go: -------------------------------------------------------------------------------- 1 | // Copyright 2018 Ulf Adams 2 | // Modifications copyright 2019 Caleb Spare 3 | // 4 | // The contents of this file may be used under the terms of the Apache License, 5 | // Version 2.0. 6 | // 7 | // (See accompanying file LICENSE or copy at 8 | // http://www.apache.org/licenses/LICENSE-2.0) 9 | // 10 | // Unless required by applicable law or agreed to in writing, this software 11 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 12 | // KIND, either express or implied. 13 | // 14 | // The code in this file is part of a Go translation of the C code written by 15 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 16 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 17 | 18 | package ryu 19 | 20 | import ( 21 | "bytes" 22 | "fmt" 23 | "math" 24 | "math/big" 25 | "math/rand" 26 | "sort" 27 | "strconv" 28 | "testing" 29 | "text/tabwriter" 30 | "time" 31 | ) 32 | 33 | var genericTestCases = []float64{ 34 | 0, 35 | math.Float64frombits(uint64(1) << 63), // -0 36 | math.NaN(), 37 | math.Inf(-1), 38 | math.Inf(1), 39 | 1, 40 | -1, 41 | 10, 42 | -10, 43 | 0.3, 44 | -0.3, 45 | 1000000, 46 | 123456.7, 47 | 123e45, 48 | -123.45, 49 | 1e23, 50 | math.SmallestNonzeroFloat32, 51 | math.MaxFloat32, 52 | below1e23, 53 | above1e23, 54 | 55 | // https://golang.org/issue/2625 56 | 383260575764816448, 57 | 383260575764816448, 58 | } 59 | 60 | const ( 61 | below1e23 = 99999999999999974834176 62 | above1e23 = 100000000000000008388608 63 | ) 64 | 65 | func TestFormatFloat32(t *testing.T) { 66 | for _, f64 := range genericTestCases { 67 | f := float32(f64) 68 | got := FormatFloat32(f) 69 | want := strconv.FormatFloat(float64(f), 'e', -1, 32) 70 | if got != want { 71 | t.Errorf("FormatFloat32(%g): got %q; want %q", f, got, want) 72 | } 73 | } 74 | } 75 | 76 | var float64TestCases = []float64{ 77 | 123e300, 78 | 123e-300, 79 | 5e-324, 80 | -5e-324, 81 | math.SmallestNonzeroFloat64, 82 | math.MaxFloat64, 83 | 84 | // https://www.exploringbinary.com/java-hangs-when-converting-2-2250738585072012e-308/ 85 | 2.2250738585072012e-308, 86 | // https://www.exploringbinary.com/php-hangs-on-numeric-value-2-2250738585072011e-308/ 87 | 2.2250738585072011e-308, 88 | 89 | // https://github.com/golang/go/issues/29491 90 | //498484681984085570, 91 | //-5.8339553793802237e+23, 92 | } 93 | 94 | func TestFormatFloat64(t *testing.T) { 95 | for _, f := range append(genericTestCases, float64TestCases...) { 96 | got := FormatFloat64(f) 97 | want := strconv.FormatFloat(f, 'e', -1, 64) 98 | if got != want { 99 | t.Errorf("FormatFloat64(%g): got %q; want %q", f, got, want) 100 | } 101 | } 102 | } 103 | 104 | func TestFormatFloatRandom(t *testing.T) { 105 | t.Skip("disabled because of Go bug: https://github.com/golang/go/issues/29491") 106 | for i := 0; i < 1e6; i++ { 107 | f := math.Float64frombits(rand.Uint64()) 108 | 109 | got32 := FormatFloat32(float32(f)) 110 | want32 := strconv.FormatFloat(f, 'e', -1, 32) 111 | if got32 != want32 { 112 | t.Fatalf("FormatFloat32(%g): got %q; want %q", f, got32, want32) 113 | } 114 | 115 | got := FormatFloat64(f) 116 | want := strconv.FormatFloat(f, 'e', -1, 64) 117 | if got != want { 118 | t.Fatalf("FormatFloat64(%g): got %q; want %q", f, got, want) 119 | } 120 | } 121 | } 122 | 123 | func TestDecimalLen(t *testing.T) { 124 | for n := uint64(1); n < 1000; n++ { 125 | testDecimalLen(t, n) 126 | } 127 | for i := 0; i < 1e5; i++ { 128 | n := uint64(rand.Intn(99999999999999999) + 1) 129 | testDecimalLen(t, n) 130 | } 131 | } 132 | 133 | func testDecimalLen(t *testing.T, n uint64) { 134 | t.Helper() 135 | want := len(big.NewInt(int64(n)).String()) // n fits into int64 136 | if got := decimalLen64(n); got != want { 137 | t.Fatalf("decimalLen64(%d): got %d; want %d", n, got, want) 138 | } 139 | if n < math.MaxUint32 { 140 | if got := decimalLen32(uint32(n)); got != want { 141 | t.Fatalf("decimalLen32(%d): got %d; want %d", n, got, want) 142 | } 143 | } 144 | } 145 | 146 | var sink string 147 | var sinkb []byte 148 | 149 | // Much of the Format cost is allocation, so most of the interesting benchmarks 150 | // are for Append, below. 151 | 152 | const benchFloat = 123.456 153 | 154 | func BenchmarkFormatFloat32(b *testing.B) { 155 | var s string 156 | f := float32(benchFloat) 157 | for i := 0; i < b.N; i++ { 158 | s = FormatFloat32(f) 159 | } 160 | sink = s 161 | } 162 | 163 | func BenchmarkStrconvFormatFloat32(b *testing.B) { 164 | var s string 165 | f := float32(benchFloat) 166 | for i := 0; i < b.N; i++ { 167 | s = strconv.FormatFloat(float64(f), 'e', -1, 32) 168 | } 169 | sink = s 170 | } 171 | 172 | func BenchmarkFormatFloat64(b *testing.B) { 173 | var s string 174 | f := float64(benchFloat) 175 | for i := 0; i < b.N; i++ { 176 | s = FormatFloat64(f) 177 | } 178 | sink = s 179 | } 180 | 181 | func BenchmarkStrconvFormatFloat64(b *testing.B) { 182 | var s string 183 | f := float64(benchFloat) 184 | for i := 0; i < b.N; i++ { 185 | s = strconv.FormatFloat(f, 'e', -1, 64) 186 | } 187 | sink = s 188 | } 189 | 190 | var benchCases = []float64{ 191 | 0, 192 | 1, 193 | 0.3, 194 | 1000000, 195 | -123.45, 196 | } 197 | 198 | func BenchmarkAppendFloat32(b *testing.B) { 199 | for _, f64 := range benchCases { 200 | f := float32(f64) 201 | b.Run(FormatFloat32(f), func(b *testing.B) { 202 | var buf []byte 203 | for i := 0; i < b.N; i++ { 204 | buf = AppendFloat32(buf[:0], f) 205 | } 206 | sinkb = buf 207 | }) 208 | } 209 | } 210 | 211 | func BenchmarkStrconvAppendFloat32(b *testing.B) { 212 | for _, f64 := range benchCases { 213 | f := float32(f64) 214 | b.Run(FormatFloat32(f), func(b *testing.B) { 215 | var buf []byte 216 | for i := 0; i < b.N; i++ { 217 | buf = strconv.AppendFloat(buf[:0], float64(f), 'e', -1, 32) 218 | } 219 | sinkb = buf 220 | }) 221 | } 222 | } 223 | 224 | var benchCases64 = []float64{ 225 | 622666234635.3213e-320, // https://golang.org/issue/15672 226 | } 227 | 228 | func BenchmarkAppendFloat64(b *testing.B) { 229 | for _, f := range append(benchCases, benchCases64...) { 230 | b.Run(FormatFloat64(f), func(b *testing.B) { 231 | var buf []byte 232 | for i := 0; i < b.N; i++ { 233 | buf = AppendFloat64(buf[:0], f) 234 | } 235 | sinkb = buf 236 | }) 237 | } 238 | } 239 | 240 | func BenchmarkStrconvAppendFloat64(b *testing.B) { 241 | for _, f := range append(benchCases, benchCases64...) { 242 | b.Run(FormatFloat64(f), func(b *testing.B) { 243 | var buf []byte 244 | for i := 0; i < b.N; i++ { 245 | buf = strconv.AppendFloat(buf[:0], f, 'e', -1, 64) 246 | } 247 | sinkb = buf 248 | }) 249 | } 250 | } 251 | 252 | // This is a test (not benchmark) because it uses a slightly different strategy 253 | // than normal Go benchmarks. 254 | func TestRandomBenchmark(t *testing.T) { 255 | t.Skip("unskip to run long benchmark test") 256 | var ryuDist, stdlibDist dist 257 | const n = 50e3 258 | stdlibAppend := func(b []byte, f float64) []byte { 259 | return strconv.AppendFloat(b, f, 'e', -1, 64) 260 | } 261 | b := make([]byte, 50) 262 | for i := 0; i < n; i++ { 263 | f := math.Float64frombits(rand.Uint64()) 264 | ryu := runRandomBenchmark(b, f, AppendFloat64) 265 | ryuDist = append(ryuDist, ryu.Nanoseconds()) 266 | stdlib := runRandomBenchmark(b, f, stdlibAppend) 267 | stdlibDist = append(stdlibDist, stdlib.Nanoseconds()) 268 | } 269 | var buf bytes.Buffer 270 | w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0) 271 | summarize := func(name string, d dist) { 272 | min, max, median, mean := d.summarize() 273 | fmt.Fprintf(w, "%s\tmin = %dns\tmax = %dns\tmedian = %dns\tmean = %dns\t\n", 274 | name, min, max, median, mean) 275 | } 276 | summarize("ryu:", ryuDist) 277 | summarize("strconv (stdlib):", stdlibDist) 278 | w.Flush() 279 | t.Logf("after sampling %d float64s:\n%s", int64(n), buf.String()) 280 | } 281 | 282 | type dist []int64 283 | 284 | func (d dist) summarize() (min, max, median, mean int64) { 285 | sort.Slice(d, func(i, j int) bool { return d[i] < d[j] }) 286 | min = d[0] 287 | max = d[len(d)-1] 288 | median = d[len(d)/2] 289 | var sum float64 290 | for _, ns := range d { 291 | sum += float64(ns) 292 | } 293 | mean = int64(sum / float64(len(d))) 294 | return 295 | } 296 | 297 | func runRandomBenchmark(b []byte, f float64, format func([]byte, float64) []byte) time.Duration { 298 | // Estimate the time. 299 | d0 := measureCall(b, f, format, 1) 300 | times := int(100 * time.Microsecond / d0) 301 | if times < 10 { 302 | times = 10 303 | } 304 | var min time.Duration 305 | for i := 0; i < 5; i++ { 306 | d := measureCall(b, f, format, times) 307 | if min == 0 || d < min { 308 | min = d 309 | } 310 | } 311 | return min 312 | } 313 | 314 | func measureCall(b []byte, f float64, format func([]byte, float64) []byte, times int) time.Duration { 315 | start := time.Now() 316 | for i := 0; i < times; i++ { 317 | _ = format(b[:0], f) 318 | } 319 | elapsed := time.Since(start) 320 | return elapsed / time.Duration(times) 321 | } 322 | -------------------------------------------------------------------------------- /tables.go: -------------------------------------------------------------------------------- 1 | // Code generated by running "go generate". DO NOT EDIT. 2 | 3 | // Copyright 2018 Ulf Adams 4 | // Modifications copyright 2019 Caleb Spare 5 | // 6 | // The contents of this file may be used under the terms of the Apache License, 7 | // Version 2.0. 8 | // 9 | // (See accompanying file LICENSE or copy at 10 | // http://www.apache.org/licenses/LICENSE-2.0) 11 | // 12 | // Unless required by applicable law or agreed to in writing, this software 13 | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | // KIND, either express or implied. 15 | // 16 | // The code in this file is part of a Go translation of the C code written by 17 | // Ulf Adams which may be found at https://github.com/ulfjack/ryu. That source 18 | // code is licensed under Apache 2.0 and this code is derivative work thereof. 19 | 20 | package ryu 21 | 22 | const pow5NumBits32 = 61 23 | 24 | var pow5Split32 = [...]uint64{ 25 | 1152921504606846976, 1441151880758558720, 1801439850948198400, 2251799813685248000, 26 | 1407374883553280000, 1759218604441600000, 2199023255552000000, 1374389534720000000, 27 | 1717986918400000000, 2147483648000000000, 1342177280000000000, 1677721600000000000, 28 | 2097152000000000000, 1310720000000000000, 1638400000000000000, 2048000000000000000, 29 | 1280000000000000000, 1600000000000000000, 2000000000000000000, 1250000000000000000, 30 | 1562500000000000000, 1953125000000000000, 1220703125000000000, 1525878906250000000, 31 | 1907348632812500000, 1192092895507812500, 1490116119384765625, 1862645149230957031, 32 | 1164153218269348144, 1455191522836685180, 1818989403545856475, 2273736754432320594, 33 | 1421085471520200371, 1776356839400250464, 2220446049250313080, 1387778780781445675, 34 | 1734723475976807094, 2168404344971008868, 1355252715606880542, 1694065894508600678, 35 | 2117582368135750847, 1323488980084844279, 1654361225106055349, 2067951531382569187, 36 | 1292469707114105741, 1615587133892632177, 2019483917365790221, 37 | } 38 | 39 | const pow5InvNumBits32 = 59 40 | 41 | var pow5InvSplit32 = [...]uint64{ 42 | 576460752303423489, 461168601842738791, 368934881474191033, 295147905179352826, 43 | 472236648286964522, 377789318629571618, 302231454903657294, 483570327845851670, 44 | 386856262276681336, 309485009821345069, 495176015714152110, 396140812571321688, 45 | 316912650057057351, 507060240091291761, 405648192073033409, 324518553658426727, 46 | 519229685853482763, 415383748682786211, 332306998946228969, 531691198313966350, 47 | 425352958651173080, 340282366920938464, 544451787073501542, 435561429658801234, 48 | 348449143727040987, 557518629963265579, 446014903970612463, 356811923176489971, 49 | 570899077082383953, 456719261665907162, 365375409332725730, 50 | } 51 | 52 | const pow5NumBits64 = 121 53 | 54 | var pow5Split64 = [...]uint128{ 55 | {0, 72057594037927936}, 56 | {0, 90071992547409920}, 57 | {0, 112589990684262400}, 58 | {0, 140737488355328000}, 59 | {0, 87960930222080000}, 60 | {0, 109951162777600000}, 61 | {0, 137438953472000000}, 62 | {0, 85899345920000000}, 63 | {0, 107374182400000000}, 64 | {0, 134217728000000000}, 65 | {0, 83886080000000000}, 66 | {0, 104857600000000000}, 67 | {0, 131072000000000000}, 68 | {0, 81920000000000000}, 69 | {0, 102400000000000000}, 70 | {0, 128000000000000000}, 71 | {0, 80000000000000000}, 72 | {0, 100000000000000000}, 73 | {0, 125000000000000000}, 74 | {0, 78125000000000000}, 75 | {0, 97656250000000000}, 76 | {0, 122070312500000000}, 77 | {0, 76293945312500000}, 78 | {0, 95367431640625000}, 79 | {0, 119209289550781250}, 80 | {4611686018427387904, 74505805969238281}, 81 | {10376293541461622784, 93132257461547851}, 82 | {8358680908399640576, 116415321826934814}, 83 | {612489549322387456, 72759576141834259}, 84 | {14600669991935148032, 90949470177292823}, 85 | {13639151471491547136, 113686837721616029}, 86 | {3213881284082270208, 142108547152020037}, 87 | {4314518811765112832, 88817841970012523}, 88 | {781462496279003136, 111022302462515654}, 89 | {10200200157203529728, 138777878078144567}, 90 | {13292654125893287936, 86736173798840354}, 91 | {7392445620511834112, 108420217248550443}, 92 | {4628871007212404736, 135525271560688054}, 93 | {16728102434789916672, 84703294725430033}, 94 | {7075069988205232128, 105879118406787542}, 95 | {18067209522111315968, 132348898008484427}, 96 | {8986162942105878528, 82718061255302767}, 97 | {6621017659204960256, 103397576569128459}, 98 | {3664586055578812416, 129246970711410574}, 99 | {16125424340018921472, 80779356694631608}, 100 | {1710036351314100224, 100974195868289511}, 101 | {15972603494424788992, 126217744835361888}, 102 | {9982877184015493120, 78886090522101180}, 103 | {12478596480019366400, 98607613152626475}, 104 | {10986559581596820096, 123259516440783094}, 105 | {2254913720070624656, 77037197775489434}, 106 | {12042014186943056628, 96296497219361792}, 107 | {15052517733678820785, 120370621524202240}, 108 | {9407823583549262990, 75231638452626400}, 109 | {11759779479436578738, 94039548065783000}, 110 | {14699724349295723422, 117549435082228750}, 111 | {4575641699882439235, 73468396926392969}, 112 | {10331238143280436948, 91835496157991211}, 113 | {8302361660673158281, 114794370197489014}, 114 | {1154580038986672043, 143492962746861268}, 115 | {9944984561221445835, 89683101716788292}, 116 | {12431230701526807293, 112103877145985365}, 117 | {1703980321626345405, 140129846432481707}, 118 | {17205888765512323542, 87581154020301066}, 119 | {12283988920035628619, 109476442525376333}, 120 | {1519928094762372062, 136845553156720417}, 121 | {12479170105294952299, 85528470722950260}, 122 | {15598962631618690374, 106910588403687825}, 123 | {5663645234241199255, 133638235504609782}, 124 | {17374836326682913246, 83523897190381113}, 125 | {7883487353071477846, 104404871487976392}, 126 | {9854359191339347308, 130506089359970490}, 127 | {10770660513014479971, 81566305849981556}, 128 | {13463325641268099964, 101957882312476945}, 129 | {2994098996302961243, 127447352890596182}, 130 | {15706369927971514489, 79654595556622613}, 131 | {5797904354682229399, 99568244445778267}, 132 | {2635694424925398845, 124460305557222834}, 133 | {6258995034005762182, 77787690973264271}, 134 | {3212057774079814824, 97234613716580339}, 135 | {17850130272881932242, 121543267145725423}, 136 | {18073860448192289507, 75964541966078389}, 137 | {8757267504958198172, 94955677457597987}, 138 | {6334898362770359811, 118694596821997484}, 139 | {13182683513586250689, 74184123013748427}, 140 | {11866668373555425458, 92730153767185534}, 141 | {5609963430089506015, 115912692208981918}, 142 | {17341285199088104971, 72445432630613698}, 143 | {12453234462005355406, 90556790788267123}, 144 | {10954857059079306353, 113195988485333904}, 145 | {13693571323849132942, 141494985606667380}, 146 | {17781854114260483896, 88434366004167112}, 147 | {3780573569116053255, 110542957505208891}, 148 | {114030942967678664, 138178696881511114}, 149 | {4682955357782187069, 86361685550944446}, 150 | {15077066234082509644, 107952106938680557}, 151 | {5011274737320973344, 134940133673350697}, 152 | {14661261756894078100, 84337583545844185}, 153 | {4491519140835433913, 105421979432305232}, 154 | {5614398926044292391, 131777474290381540}, 155 | {12732371365632458552, 82360921431488462}, 156 | {6692092170185797382, 102951151789360578}, 157 | {17588487249587022536, 128688939736700722}, 158 | {15604490549419276989, 80430587335437951}, 159 | {14893927168346708332, 100538234169297439}, 160 | {14005722942005997511, 125672792711621799}, 161 | {15671105866394830300, 78545495444763624}, 162 | {1142138259283986260, 98181869305954531}, 163 | {15262730879387146537, 122727336632443163}, 164 | {7233363790403272633, 76704585395276977}, 165 | {13653390756431478696, 95880731744096221}, 166 | {3231680390257184658, 119850914680120277}, 167 | {4325643253124434363, 74906821675075173}, 168 | {10018740084832930858, 93633527093843966}, 169 | {3300053069186387764, 117041908867304958}, 170 | {15897591223523656064, 73151193042065598}, 171 | {10648616992549794273, 91438991302581998}, 172 | {4087399203832467033, 114298739128227498}, 173 | {14332621041645359599, 142873423910284372}, 174 | {18181260187883125557, 89295889943927732}, 175 | {4279831161144355331, 111619862429909666}, 176 | {14573160988285219972, 139524828037387082}, 177 | {13719911636105650386, 87203017523366926}, 178 | {7926517508277287175, 109003771904208658}, 179 | {684774848491833161, 136254714880260823}, 180 | {7345513307948477581, 85159196800163014}, 181 | {18405263671790372785, 106448996000203767}, 182 | {18394893571310578077, 133061245000254709}, 183 | {13802651491282805250, 83163278125159193}, 184 | {3418256308821342851, 103954097656448992}, 185 | {4272820386026678563, 129942622070561240}, 186 | {2670512741266674102, 81214138794100775}, 187 | {17173198981865506339, 101517673492625968}, 188 | {3019754653622331308, 126897091865782461}, 189 | {4193189667727651020, 79310682416114038}, 190 | {14464859121514339583, 99138353020142547}, 191 | {13469387883465536574, 123922941275178184}, 192 | {8418367427165960359, 77451838296986365}, 193 | {15134645302384838353, 96814797871232956}, 194 | {471562554271496325, 121018497339041196}, 195 | {9518098633274461011, 75636560836900747}, 196 | {7285937273165688360, 94545701046125934}, 197 | {18330793628311886258, 118182126307657417}, 198 | {4539216990053847055, 73863828942285886}, 199 | {14897393274422084627, 92329786177857357}, 200 | {4786683537745442072, 115412232722321697}, 201 | {14520892257159371055, 72132645451451060}, 202 | {18151115321449213818, 90165806814313825}, 203 | {8853836096529353561, 112707258517892282}, 204 | {1843923083806916143, 140884073147365353}, 205 | {12681666973447792349, 88052545717103345}, 206 | {2017025661527576725, 110065682146379182}, 207 | {11744654113764246714, 137582102682973977}, 208 | {422879793461572340, 85988814176858736}, 209 | {528599741826965425, 107486017721073420}, 210 | {660749677283706782, 134357522151341775}, 211 | {7330497575943398595, 83973451344588609}, 212 | {13774807988356636147, 104966814180735761}, 213 | {3383451930163631472, 131208517725919702}, 214 | {15949715511634433382, 82005323578699813}, 215 | {6102086334260878016, 102506654473374767}, 216 | {3015921899398709616, 128133318091718459}, 217 | {18025852251620051174, 80083323807324036}, 218 | {4085571240815512351, 100104154759155046}, 219 | {14330336087874166247, 125130193448943807}, 220 | {15873989082562435760, 78206370905589879}, 221 | {15230800334775656796, 97757963631987349}, 222 | {5203442363187407284, 122197454539984187}, 223 | {946308467778435600, 76373409087490117}, 224 | {5794571603150432404, 95466761359362646}, 225 | {16466586540792816313, 119333451699203307}, 226 | {7985773578781816244, 74583407312002067}, 227 | {5370530955049882401, 93229259140002584}, 228 | {6713163693812353001, 116536573925003230}, 229 | {18030785363914884337, 72835358703127018}, 230 | {13315109668038829614, 91044198378908773}, 231 | {2808829029766373305, 113805247973635967}, 232 | {17346094342490130344, 142256559967044958}, 233 | {6229622945628943561, 88910349979403099}, 234 | {3175342663608791547, 111137937474253874}, 235 | {13192550366365765242, 138922421842817342}, 236 | {3633657960551215372, 86826513651760839}, 237 | {18377130505971182927, 108533142064701048}, 238 | {4524669058754427043, 135666427580876311}, 239 | {9745447189362598758, 84791517238047694}, 240 | {2958436949848472639, 105989396547559618}, 241 | {12921418224165366607, 132486745684449522}, 242 | {12687572408530742033, 82804216052780951}, 243 | {11247779492236039638, 103505270065976189}, 244 | {224666310012885835, 129381587582470237}, 245 | {2446259452971747599, 80863492239043898}, 246 | {12281196353069460307, 101079365298804872}, 247 | {15351495441336825384, 126349206623506090}, 248 | {14206370669262903769, 78968254139691306}, 249 | {8534591299723853903, 98710317674614133}, 250 | {15279925143082205283, 123387897093267666}, 251 | {14161639232853766206, 77117435683292291}, 252 | {13090363022639819853, 96396794604115364}, 253 | {16362953778299774816, 120495993255144205}, 254 | {12532689120651053212, 75309995784465128}, 255 | {15665861400813816515, 94137494730581410}, 256 | {10358954714162494836, 117671868413226763}, 257 | {4168503687137865320, 73544917758266727}, 258 | {598943590494943747, 91931147197833409}, 259 | {5360365506546067587, 114913933997291761}, 260 | {11312142901609972388, 143642417496614701}, 261 | {9375932322719926695, 89776510935384188}, 262 | {11719915403399908368, 112220638669230235}, 263 | {10038208235822497557, 140275798336537794}, 264 | {10885566165816448877, 87672373960336121}, 265 | {18218643725697949000, 109590467450420151}, 266 | {18161618638695048346, 136988084313025189}, 267 | {13656854658398099168, 85617552695640743}, 268 | {12459382304570236056, 107021940869550929}, 269 | {1739169825430631358, 133777426086938662}, 270 | {14922039196176308311, 83610891304336663}, 271 | {14040862976792997485, 104513614130420829}, 272 | {3716020665709083144, 130642017663026037}, 273 | {4628355925281870917, 81651261039391273}, 274 | {10397130925029726550, 102064076299239091}, 275 | {8384727637859770284, 127580095374048864}, 276 | {5240454773662356427, 79737559608780540}, 277 | {6550568467077945534, 99671949510975675}, 278 | {3576524565420044014, 124589936888719594}, 279 | {6847013871814915412, 77868710555449746}, 280 | {17782139376623420074, 97335888194312182}, 281 | {13004302183924499284, 121669860242890228}, 282 | {17351060901807587860, 76043662651806392}, 283 | {3242082053549933210, 95054578314757991}, 284 | {17887660622219580224, 118818222893447488}, 285 | {11179787888887237640, 74261389308404680}, 286 | {13974734861109047050, 92826736635505850}, 287 | {8245046539531533005, 116033420794382313}, 288 | {16682369133275677888, 72520887996488945}, 289 | {7017903361312433648, 90651109995611182}, 290 | {17995751238495317868, 113313887494513977}, 291 | {8659630992836983623, 141642359368142472}, 292 | {5412269370523114764, 88526474605089045}, 293 | {11377022731581281359, 110658093256361306}, 294 | {4997906377621825891, 138322616570451633}, 295 | {14652906532082110942, 86451635356532270}, 296 | {9092761128247862869, 108064544195665338}, 297 | {2142579373455052779, 135080680244581673}, 298 | {12868327154477877747, 84425425152863545}, 299 | {2250350887815183471, 105531781441079432}, 300 | {2812938609768979339, 131914726801349290}, 301 | {6369772649532999991, 82446704250843306}, 302 | {17185587848771025797, 103058380313554132}, 303 | {3035240737254230630, 128822975391942666}, 304 | {6508711479211282048, 80514359619964166}, 305 | {17359261385868878368, 100642949524955207}, 306 | {17087390713908710056, 125803686906194009}, 307 | {3762090168551861929, 78627304316371256}, 308 | {4702612710689827411, 98284130395464070}, 309 | {15101637925217060072, 122855162994330087}, 310 | {16356052730901744401, 76784476871456304}, 311 | {1998321839917628885, 95980596089320381}, 312 | {7109588318324424010, 119975745111650476}, 313 | {13666864735807540814, 74984840694781547}, 314 | {12471894901332038114, 93731050868476934}, 315 | {6366496589810271835, 117163813585596168}, 316 | {3979060368631419896, 73227383490997605}, 317 | {9585511479216662775, 91534229363747006}, 318 | {2758517312166052660, 114417786704683758}, 319 | {12671518677062341634, 143022233380854697}, 320 | {1002170145522881665, 89388895863034186}, 321 | {10476084718758377889, 111736119828792732}, 322 | {13095105898447972362, 139670149785990915}, 323 | {5878598177316288774, 87293843616244322}, 324 | {16571619758500136775, 109117304520305402}, 325 | {11491152661270395161, 136396630650381753}, 326 | {264441385652915120, 85247894156488596}, 327 | {330551732066143900, 106559867695610745}, 328 | {5024875683510067779, 133199834619513431}, 329 | {10058076329834874218, 83249896637195894}, 330 | {3349223375438816964, 104062370796494868}, 331 | {4186529219298521205, 130077963495618585}, 332 | {14145795808130045513, 81298727184761615}, 333 | {13070558741735168987, 101623408980952019}, 334 | {11726512408741573330, 127029261226190024}, 335 | {7329070255463483331, 79393288266368765}, 336 | {13773023837756742068, 99241610332960956}, 337 | {17216279797195927585, 124052012916201195}, 338 | {8454331864033760789, 77532508072625747}, 339 | {5956228811614813082, 96915635090782184}, 340 | {7445286014518516353, 121144543863477730}, 341 | {9264989777501460624, 75715339914673581}, 342 | {16192923240304213684, 94644174893341976}, 343 | {1794409976670715490, 118305218616677471}, 344 | {8039035263060279037, 73940761635423419}, 345 | {5437108060397960892, 92425952044279274}, 346 | {16019757112352226923, 115532440055349092}, 347 | {788976158365366019, 72207775034593183}, 348 | {14821278253238871236, 90259718793241478}, 349 | {9303225779693813237, 112824648491551848}, 350 | {11629032224617266546, 141030810614439810}, 351 | {11879831158813179495, 88144256634024881}, 352 | {1014730893234310657, 110180320792531102}, 353 | {10491785653397664129, 137725400990663877}, 354 | {8863209042587234033, 86078375619164923}, 355 | {6467325284806654637, 107597969523956154}, 356 | {17307528642863094104, 134497461904945192}, 357 | {10817205401789433815, 84060913690590745}, 358 | {18133192770664180173, 105076142113238431}, 359 | {18054804944902837312, 131345177641548039}, 360 | {18201782118205355176, 82090736025967524}, 361 | {4305483574047142354, 102613420032459406}, 362 | {14605226504413703751, 128266775040574257}, 363 | {2210737537617482988, 80166734400358911}, 364 | {16598479977304017447, 100208418000448638}, 365 | {11524727934775246001, 125260522500560798}, 366 | {2591268940807140847, 78287826562850499}, 367 | {17074144231291089770, 97859783203563123}, 368 | {16730994270686474309, 122324729004453904}, 369 | {10456871419179046443, 76452955627783690}, 370 | {3847717237119032246, 95566194534729613}, 371 | {9421332564826178211, 119457743168412016}, 372 | {5888332853016361382, 74661089480257510}, 373 | {16583788103125227536, 93326361850321887}, 374 | {16118049110479146516, 116657952312902359}, 375 | {16991309721690548428, 72911220195563974}, 376 | {12015765115258409727, 91139025244454968}, 377 | {15019706394073012159, 113923781555568710}, 378 | {9551260955736489391, 142404726944460888}, 379 | {5969538097335305869, 89002954340288055}, 380 | {2850236603241744433, 111253692925360069}, 381 | } 382 | 383 | const pow5InvNumBits64 = 122 384 | 385 | var pow5InvSplit64 = [...]uint128{ 386 | {1, 288230376151711744}, 387 | {3689348814741910324, 230584300921369395}, 388 | {2951479051793528259, 184467440737095516}, 389 | {17118578500402463900, 147573952589676412}, 390 | {12632330341676300947, 236118324143482260}, 391 | {10105864273341040758, 188894659314785808}, 392 | {15463389048156653253, 151115727451828646}, 393 | {17362724847566824558, 241785163922925834}, 394 | {17579528692795369969, 193428131138340667}, 395 | {6684925324752475329, 154742504910672534}, 396 | {18074578149087781173, 247588007857076054}, 397 | {18149011334012135262, 198070406285660843}, 398 | {3451162622983977240, 158456325028528675}, 399 | {5521860196774363583, 253530120045645880}, 400 | {4417488157419490867, 202824096036516704}, 401 | {7223339340677503017, 162259276829213363}, 402 | {7867994130342094503, 259614842926741381}, 403 | {2605046489531765280, 207691874341393105}, 404 | {2084037191625412224, 166153499473114484}, 405 | {10713157136084480204, 265845599156983174}, 406 | {12259874523609494487, 212676479325586539}, 407 | {13497248433629505913, 170141183460469231}, 408 | {14216899864323388813, 272225893536750770}, 409 | {11373519891458711051, 217780714829400616}, 410 | {5409467098425058518, 174224571863520493}, 411 | {4965798542738183305, 278759314981632789}, 412 | {7661987648932456967, 223007451985306231}, 413 | {2440241304404055250, 178405961588244985}, 414 | {3904386087046488400, 285449538541191976}, 415 | {17880904128604832013, 228359630832953580}, 416 | {14304723302883865611, 182687704666362864}, 417 | {15133127457049002812, 146150163733090291}, 418 | {16834306301794583852, 233840261972944466}, 419 | {9778096226693756759, 187072209578355573}, 420 | {15201174610838826053, 149657767662684458}, 421 | {2185786488890659746, 239452428260295134}, 422 | {5437978005854438120, 191561942608236107}, 423 | {15418428848909281466, 153249554086588885}, 424 | {6222742084545298729, 245199286538542217}, 425 | {16046240111861969953, 196159429230833773}, 426 | {1768945645263844993, 156927543384667019}, 427 | {10209010661905972635, 251084069415467230}, 428 | {8167208529524778108, 200867255532373784}, 429 | {10223115638361732810, 160693804425899027}, 430 | {1599589762411131202, 257110087081438444}, 431 | {4969020624670815285, 205688069665150755}, 432 | {3975216499736652228, 164550455732120604}, 433 | {13739044029062464211, 263280729171392966}, 434 | {7301886408508061046, 210624583337114373}, 435 | {13220206756290269483, 168499666669691498}, 436 | {17462981995322520850, 269599466671506397}, 437 | {6591687966774196033, 215679573337205118}, 438 | {12652048002903177473, 172543658669764094}, 439 | {9175230360419352987, 276069853871622551}, 440 | {3650835473593572067, 220855883097298041}, 441 | {17678063637842498946, 176684706477838432}, 442 | {13527506561580357021, 282695530364541492}, 443 | {3443307619780464970, 226156424291633194}, 444 | {6443994910566282300, 180925139433306555}, 445 | {5155195928453025840, 144740111546645244}, 446 | {15627011115008661990, 231584178474632390}, 447 | {12501608892006929592, 185267342779705912}, 448 | {2622589484121723027, 148213874223764730}, 449 | {4196143174594756843, 237142198758023568}, 450 | {10735612169159626121, 189713759006418854}, 451 | {12277838550069611220, 151771007205135083}, 452 | {15955192865369467629, 242833611528216133}, 453 | {1696107848069843133, 194266889222572907}, 454 | {12424932722681605476, 155413511378058325}, 455 | {1433148282581017146, 248661618204893321}, 456 | {15903913885032455010, 198929294563914656}, 457 | {9033782293284053685, 159143435651131725}, 458 | {14454051669254485895, 254629497041810760}, 459 | {11563241335403588716, 203703597633448608}, 460 | {16629290697806691620, 162962878106758886}, 461 | {781423413297334329, 260740604970814219}, 462 | {4314487545379777786, 208592483976651375}, 463 | {3451590036303822229, 166873987181321100}, 464 | {5522544058086115566, 266998379490113760}, 465 | {4418035246468892453, 213598703592091008}, 466 | {10913125826658934609, 170878962873672806}, 467 | {10082303693170474728, 273406340597876490}, 468 | {8065842954536379782, 218725072478301192}, 469 | {17520720807854834795, 174980057982640953}, 470 | {5897060404116273733, 279968092772225526}, 471 | {1028299508551108663, 223974474217780421}, 472 | {15580034865808528224, 179179579374224336}, 473 | {17549358155809824511, 286687326998758938}, 474 | {2971440080422128639, 229349861599007151}, 475 | {17134547323305344204, 183479889279205720}, 476 | {13707637858644275364, 146783911423364576}, 477 | {14553522944347019935, 234854258277383322}, 478 | {4264120725993795302, 187883406621906658}, 479 | {10789994210278856888, 150306725297525326}, 480 | {9885293106962350374, 240490760476040522}, 481 | {529536856086059653, 192392608380832418}, 482 | {7802327114352668369, 153914086704665934}, 483 | {1415676938738538420, 246262538727465495}, 484 | {1132541550990830736, 197010030981972396}, 485 | {15663428499760305882, 157608024785577916}, 486 | {17682787970132668764, 252172839656924666}, 487 | {10456881561364224688, 201738271725539733}, 488 | {15744202878575200397, 161390617380431786}, 489 | {17812026976236499989, 258224987808690858}, 490 | {3181575136763469022, 206579990246952687}, 491 | {13613306553636506187, 165263992197562149}, 492 | {10713244041592678929, 264422387516099439}, 493 | {12259944048016053467, 211537910012879551}, 494 | {6118606423670932450, 169230328010303641}, 495 | {2411072648389671274, 270768524816485826}, 496 | {16686253377679378312, 216614819853188660}, 497 | {13349002702143502650, 173291855882550928}, 498 | {17669055508687693916, 277266969412081485}, 499 | {14135244406950155133, 221813575529665188}, 500 | {240149081334393137, 177450860423732151}, 501 | {11452284974360759988, 283921376677971441}, 502 | {5472479164746697667, 227137101342377153}, 503 | {11756680961281178780, 181709681073901722}, 504 | {2026647139541122378, 145367744859121378}, 505 | {18000030682233437097, 232588391774594204}, 506 | {18089373360528660001, 186070713419675363}, 507 | {3403452244197197031, 148856570735740291}, 508 | {16513570034941246220, 238170513177184465}, 509 | {13210856027952996976, 190536410541747572}, 510 | {3189987192878576934, 152429128433398058}, 511 | {1414630693863812771, 243886605493436893}, 512 | {8510402184574870864, 195109284394749514}, 513 | {10497670562401807014, 156087427515799611}, 514 | {9417575270359070576, 249739884025279378}, 515 | {14912757845771077107, 199791907220223502}, 516 | {4551508647133041040, 159833525776178802}, 517 | {10971762650154775986, 255733641241886083}, 518 | {16156107749607641435, 204586912993508866}, 519 | {9235537384944202825, 163669530394807093}, 520 | {11087511001168814197, 261871248631691349}, 521 | {12559357615676961681, 209496998905353079}, 522 | {13736834907283479668, 167597599124282463}, 523 | {18289587036911657145, 268156158598851941}, 524 | {10942320814787415393, 214524926879081553}, 525 | {16132554281313752961, 171619941503265242}, 526 | {11054691591134363444, 274591906405224388}, 527 | {16222450902391311402, 219673525124179510}, 528 | {12977960721913049122, 175738820099343608}, 529 | {17075388340318968271, 281182112158949773}, 530 | {2592264228029443648, 224945689727159819}, 531 | {5763160197165465241, 179956551781727855}, 532 | {9221056315464744386, 287930482850764568}, 533 | {14755542681855616155, 230344386280611654}, 534 | {15493782960226403247, 184275509024489323}, 535 | {1326979923955391628, 147420407219591459}, 536 | {9501865507812447252, 235872651551346334}, 537 | {11290841220991868125, 188698121241077067}, 538 | {1653975347309673853, 150958496992861654}, 539 | {10025058185179298811, 241533595188578646}, 540 | {4330697733401528726, 193226876150862917}, 541 | {14532604630946953951, 154581500920690333}, 542 | {1116074521063664381, 247330401473104534}, 543 | {4582208431592841828, 197864321178483627}, 544 | {14733813189500004432, 158291456942786901}, 545 | {16195403473716186445, 253266331108459042}, 546 | {5577625149489128510, 202613064886767234}, 547 | {8151448934333213131, 162090451909413787}, 548 | {16731667109675051333, 259344723055062059}, 549 | {17074682502481951390, 207475778444049647}, 550 | {6281048372501740465, 165980622755239718}, 551 | {6360328581260874421, 265568996408383549}, 552 | {8777611679750609860, 212455197126706839}, 553 | {10711438158542398211, 169964157701365471}, 554 | {9759603424184016492, 271942652322184754}, 555 | {11497031554089123517, 217554121857747803}, 556 | {16576322872755119460, 174043297486198242}, 557 | {11764721337440549842, 278469275977917188}, 558 | {16790474699436260520, 222775420782333750}, 559 | {13432379759549008416, 178220336625867000}, 560 | {3045063541568861850, 285152538601387201}, 561 | {17193446092222730773, 228122030881109760}, 562 | {13754756873778184618, 182497624704887808}, 563 | {18382503128506368341, 145998099763910246}, 564 | {3586563302416817083, 233596959622256395}, 565 | {2869250641933453667, 186877567697805116}, 566 | {17052795772514404226, 149502054158244092}, 567 | {12527077977055405469, 239203286653190548}, 568 | {17400360011128145022, 191362629322552438}, 569 | {2852241564676785048, 153090103458041951}, 570 | {15631632947708587046, 244944165532867121}, 571 | {8815957543424959314, 195955332426293697}, 572 | {18120812478965698421, 156764265941034957}, 573 | {14235904707377476180, 250822825505655932}, 574 | {4010026136418160298, 200658260404524746}, 575 | {17965416168102169531, 160526608323619796}, 576 | {2919224165770098987, 256842573317791675}, 577 | {2335379332616079190, 205474058654233340}, 578 | {1868303466092863352, 164379246923386672}, 579 | {6678634360490491686, 263006795077418675}, 580 | {5342907488392393349, 210405436061934940}, 581 | {4274325990713914679, 168324348849547952}, 582 | {10528270399884173809, 269318958159276723}, 583 | {15801313949391159694, 215455166527421378}, 584 | {1573004715287196786, 172364133221937103}, 585 | {17274202803427156150, 275782613155099364}, 586 | {17508711057483635243, 220626090524079491}, 587 | {10317620031244997871, 176500872419263593}, 588 | {12818843235250086271, 282401395870821749}, 589 | {13944423402941979340, 225921116696657399}, 590 | {14844887537095493795, 180736893357325919}, 591 | {15565258844418305359, 144589514685860735}, 592 | {6457670077359736959, 231343223497377177}, 593 | {16234182506113520537, 185074578797901741}, 594 | {9297997190148906106, 148059663038321393}, 595 | {11187446689496339446, 236895460861314229}, 596 | {12639306166338981880, 189516368689051383}, 597 | {17490142562555006151, 151613094951241106}, 598 | {2158786396894637579, 242580951921985771}, 599 | {16484424376483351356, 194064761537588616}, 600 | {9498190686444770762, 155251809230070893}, 601 | {11507756283569722895, 248402894768113429}, 602 | {12895553841597688639, 198722315814490743}, 603 | {17695140702761971558, 158977852651592594}, 604 | {17244178680193423523, 254364564242548151}, 605 | {10105994129412828495, 203491651394038521}, 606 | {4395446488788352473, 162793321115230817}, 607 | {10722063196803274280, 260469313784369307}, 608 | {1198952927958798777, 208375451027495446}, 609 | {15716557601334680315, 166700360821996356}, 610 | {17767794532651667857, 266720577315194170}, 611 | {14214235626121334286, 213376461852155336}, 612 | {7682039686155157106, 170701169481724269}, 613 | {1223217053622520399, 273121871170758831}, 614 | {15735968901865657612, 218497496936607064}, 615 | {16278123936234436413, 174797997549285651}, 616 | {219556594781725998, 279676796078857043}, 617 | {7554342905309201445, 223741436863085634}, 618 | {9732823138989271479, 178993149490468507}, 619 | {815121763415193074, 286389039184749612}, 620 | {11720143854957885429, 229111231347799689}, 621 | {13065463898708218666, 183288985078239751}, 622 | {6763022304224664610, 146631188062591801}, 623 | {3442138057275642729, 234609900900146882}, 624 | {13821756890046245153, 187687920720117505}, 625 | {11057405512036996122, 150150336576094004}, 626 | {6623802375033462826, 240240538521750407}, 627 | {16367088344252501231, 192192430817400325}, 628 | {13093670675402000985, 153753944653920260}, 629 | {2503129006933649959, 246006311446272417}, 630 | {13070549649772650937, 196805049157017933}, 631 | {17835137349301941396, 157444039325614346}, 632 | {2710778055689733971, 251910462920982955}, 633 | {2168622444551787177, 201528370336786364}, 634 | {5424246770383340065, 161222696269429091}, 635 | {1300097203129523457, 257956314031086546}, 636 | {15797473021471260058, 206365051224869236}, 637 | {8948629602435097724, 165092040979895389}, 638 | {3249760919670425388, 264147265567832623}, 639 | {9978506365220160957, 211317812454266098}, 640 | {15361502721659949412, 169054249963412878}, 641 | {2442311466204457120, 270486799941460606}, 642 | {16711244431931206989, 216389439953168484}, 643 | {17058344360286875914, 173111551962534787}, 644 | {12535955717491360170, 276978483140055660}, 645 | {10028764573993088136, 221582786512044528}, 646 | {15401709288678291155, 177266229209635622}, 647 | {9885339602917624555, 283625966735416996}, 648 | {4218922867592189321, 226900773388333597}, 649 | {14443184738299482427, 181520618710666877}, 650 | {4175850161155765295, 145216494968533502}, 651 | {10370709072591134795, 232346391949653603}, 652 | {15675264887556728482, 185877113559722882}, 653 | {5161514280561562140, 148701690847778306}, 654 | {879725219414678777, 237922705356445290}, 655 | {703780175531743021, 190338164285156232}, 656 | {11631070584651125387, 152270531428124985}, 657 | {162968861732249003, 243632850284999977}, 658 | {11198421533611530172, 194906280227999981}, 659 | {5269388412147313814, 155925024182399985}, 660 | {8431021459435702103, 249480038691839976}, 661 | {3055468352806651359, 199584030953471981}, 662 | {17201769941212962380, 159667224762777584}, 663 | {16454785461715008838, 255467559620444135}, 664 | {13163828369372007071, 204374047696355308}, 665 | {17909760324981426303, 163499238157084246}, 666 | {2830174816776909822, 261598781051334795}, 667 | {2264139853421527858, 209279024841067836}, 668 | {16568707141704863579, 167423219872854268}, 669 | {4373838538276319787, 267877151796566830}, 670 | {3499070830621055830, 214301721437253464}, 671 | {6488605479238754987, 171441377149802771}, 672 | {3003071137298187333, 274306203439684434}, 673 | {6091805724580460189, 219444962751747547}, 674 | {15941491023890099121, 175555970201398037}, 675 | {10748990379256517301, 280889552322236860}, 676 | {8599192303405213841, 224711641857789488}, 677 | {14258051472207991719, 179769313486231590}, 678 | } 679 | --------------------------------------------------------------------------------