Skip to content

Commit 34fec51

Browse files
rscgopherbot
authored andcommitted
internal/strconv: extract fixed-precision ftoa from ftoaryu.go
The fixed-precision ftoa algorithm is not actually documented in the Ryū paper, and it is fairly straightforward: multiply by a power of 10 to get an integer that contains the digits we need. There is also no need for separate float32 and float64 implementations. This CL implements a new fixedFtoa, separate from Ryū. The overall algorithm is the same, but the new code is simpler, faster, and better documented. Now ftoaryu.go is only about shortest-output formatting, so if and when yet another algorithm comes along, it will be clearer what should be replaced (all of ftoaryu.go) and what should not (all of ftoafixed.go). benchmark \ host linux-arm64 local linux-amd64 s7 linux-386 s7:GOARCH=386 vs base vs base vs base vs base vs base vs base AppendFloat/Decimal -0.18% ~ ~ -0.68% +0.49% -0.79% AppendFloat/Float +0.09% ~ +1.50% +0.84% -0.37% -0.69% AppendFloat/Exp -0.51% ~ ~ +1.20% -1.27% -1.01% AppendFloat/NegExp -1.01% ~ +3.43% +1.35% -2.33% ~ AppendFloat/LongExp -1.22% +0.77% ~ ~ -1.48% ~ AppendFloat/Big -2.07% ~ -2.07% -1.97% -2.89% -2.93% AppendFloat/BinaryExp -0.28% +1.06% ~ +1.35% -0.64% -1.64% AppendFloat/32Integer ~ ~ ~ -0.79% ~ -0.66% AppendFloat/32ExactFraction -0.50% ~ +5.69% ~ -1.24% +0.69% AppendFloat/32Point ~ -1.19% +2.59% +1.03% -1.37% +0.80% AppendFloat/32Exp -3.39% -2.79% -8.36% -0.94% -5.72% -5.92% AppendFloat/32NegExp -0.63% ~ ~ +0.98% -1.34% -0.73% AppendFloat/32Shortest -1.00% +1.36% +2.94% ~ ~ ~ AppendFloat/32Fixed8Hard -5.91% -12.45% -6.62% ~ +18.46% +11.61% AppendFloat/32Fixed9Hard -6.53% -11.35% -6.01% -0.97% -18.31% -9.16% AppendFloat/64Fixed1 -13.84% -16.90% -13.13% -10.71% -24.52% -18.94% AppendFloat/64Fixed2 -11.12% -16.97% -12.13% -9.88% -22.73% -15.48% AppendFloat/64Fixed2.5 -21.98% -20.75% -19.08% -14.74% -28.11% -24.92% AppendFloat/64Fixed3 -11.53% -16.21% -10.75% -7.53% -23.11% -15.78% AppendFloat/64Fixed4 -12.89% -12.36% -11.07% -9.79% -14.51% -13.44% AppendFloat/64Fixed5Hard -47.62% -38.59% -40.83% -37.06% -60.51% -55.29% AppendFloat/64Fixed12 -7.40% ~ -8.56% -4.31% -13.82% -8.61% AppendFloat/64Fixed16 -9.10% -8.95% -6.92% -3.92% -12.99% -9.03% AppendFloat/64Fixed12Hard -9.14% -5.24% -6.23% -4.82% -13.58% -8.99% AppendFloat/64Fixed17Hard -6.80% ~ -4.03% -2.84% -19.81% -10.27% AppendFloat/64Fixed18Hard -0.12% ~ ~ ~ ~ ~ AppendFloat/64FixedF1 ~ ~ ~ ~ -0.40% +2.72% AppendFloat/64FixedF2 -0.18% ~ -1.98% -0.95% ~ +1.25% AppendFloat/64FixedF3 -0.29% ~ ~ ~ ~ +1.22% AppendFloat/Slowpath64 -1.16% ~ ~ ~ ~ -2.16% AppendFloat/SlowpathDenormal64 -1.09% ~ ~ -0.88% -0.83% ~ host: linux-arm64 goos: linux goarch: arm64 pkg: internal/strconv cpu: unknown │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-8 60.35n ± 0% 60.24n ± 0% -0.18% (p=0.000 n=20) AppendFloat/Float-8 88.83n ± 0% 88.91n ± 0% +0.09% (p=0.000 n=20) AppendFloat/Exp-8 93.55n ± 0% 93.06n ± 0% -0.51% (p=0.000 n=20) AppendFloat/NegExp-8 94.01n ± 0% 93.06n ± 0% -1.01% (p=0.000 n=20) AppendFloat/LongExp-8 101.00n ± 0% 99.77n ± 0% -1.22% (p=0.000 n=20) AppendFloat/Big-8 106.1n ± 0% 103.9n ± 0% -2.07% (p=0.000 n=20) AppendFloat/BinaryExp-8 47.48n ± 0% 47.35n ± 0% -0.28% (p=0.000 n=20) AppendFloat/32Integer-8 60.45n ± 0% 60.43n ± 0% ~ (p=0.150 n=20) AppendFloat/32ExactFraction-8 86.65n ± 0% 86.22n ± 0% -0.50% (p=0.000 n=20) AppendFloat/32Point-8 83.26n ± 0% 83.21n ± 0% ~ (p=0.046 n=20) AppendFloat/32Exp-8 92.55n ± 0% 89.42n ± 0% -3.39% (p=0.000 n=20) AppendFloat/32NegExp-8 87.89n ± 0% 87.34n ± 0% -0.63% (p=0.000 n=20) AppendFloat/32Shortest-8 77.05n ± 0% 76.28n ± 0% -1.00% (p=0.000 n=20) AppendFloat/32Fixed8Hard-8 55.73n ± 0% 52.44n ± 0% -5.91% (p=0.000 n=20) AppendFloat/32Fixed9Hard-8 64.80n ± 0% 60.57n ± 0% -6.53% (p=0.000 n=20) AppendFloat/64Fixed1-8 53.72n ± 0% 46.29n ± 0% -13.84% (p=0.000 n=20) AppendFloat/64Fixed2-8 52.64n ± 0% 46.79n ± 0% -11.12% (p=0.000 n=20) AppendFloat/64Fixed2.5-8 56.01n ± 0% 43.70n ± 0% -21.98% (p=0.000 n=20) AppendFloat/64Fixed3-8 53.38n ± 0% 47.23n ± 0% -11.53% (p=0.000 n=20) AppendFloat/64Fixed4-8 50.62n ± 0% 44.10n ± 0% -12.89% (p=0.000 n=20) AppendFloat/64Fixed5Hard-8 98.94n ± 0% 51.82n ± 0% -47.62% (p=0.000 n=20) AppendFloat/64Fixed12-8 84.70n ± 0% 78.44n ± 0% -7.40% (p=0.000 n=20) AppendFloat/64Fixed16-8 71.68n ± 0% 65.16n ± 0% -9.10% (p=0.000 n=20) AppendFloat/64Fixed12Hard-8 68.41n ± 0% 62.16n ± 0% -9.14% (p=0.000 n=20) AppendFloat/64Fixed17Hard-8 79.31n ± 0% 73.92n ± 0% -6.80% (p=0.000 n=20) AppendFloat/64Fixed18Hard-8 4.290µ ± 0% 4.285µ ± 0% -0.12% (p=0.000 n=20) AppendFloat/64FixedF1-8 216.0n ± 0% 216.1n ± 0% ~ (p=0.090 n=20) AppendFloat/64FixedF2-8 228.2n ± 0% 227.8n ± 0% -0.18% (p=0.000 n=20) AppendFloat/64FixedF3-8 208.8n ± 0% 208.2n ± 0% -0.29% (p=0.000 n=20) AppendFloat/Slowpath64-8 98.56n ± 0% 97.42n ± 0% -1.16% (p=0.000 n=20) AppendFloat/SlowpathDenormal64-8 95.81n ± 0% 94.77n ± 0% -1.09% (p=0.000 n=20) geomean 93.81n 87.87n -6.33% host: local goos: darwin cpu: Apple M3 Pro │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-12 21.14n ± 0% 21.15n ± 0% ~ (p=0.963 n=20) AppendFloat/Float-12 32.48n ± 1% 32.43n ± 0% ~ (p=0.358 n=20) AppendFloat/Exp-12 31.85n ± 0% 31.94n ± 1% ~ (p=0.634 n=20) AppendFloat/NegExp-12 31.75n ± 0% 32.04n ± 0% ~ (p=0.004 n=20) AppendFloat/LongExp-12 33.55n ± 0% 33.81n ± 0% +0.77% (p=0.000 n=20) AppendFloat/Big-12 35.62n ± 1% 35.73n ± 1% ~ (p=0.888 n=20) AppendFloat/BinaryExp-12 19.26n ± 0% 19.46n ± 1% +1.06% (p=0.000 n=20) AppendFloat/32Integer-12 21.41n ± 0% 21.46n ± 1% ~ (p=0.733 n=20) AppendFloat/32ExactFraction-12 31.23n ± 1% 31.30n ± 1% ~ (p=0.857 n=20) AppendFloat/32Point-12 31.39n ± 1% 31.02n ± 0% -1.19% (p=0.000 n=20) AppendFloat/32Exp-12 32.42n ± 1% 31.52n ± 1% -2.79% (p=0.000 n=20) AppendFloat/32NegExp-12 30.66n ± 1% 30.66n ± 1% ~ (p=0.380 n=20) AppendFloat/32Shortest-12 26.88n ± 1% 27.25n ± 1% +1.36% (p=0.000 n=20) AppendFloat/32Fixed8Hard-12 19.52n ± 0% 17.09n ± 1% -12.45% (p=0.000 n=20) AppendFloat/32Fixed9Hard-12 21.55n ± 2% 19.11n ± 1% -11.35% (p=0.000 n=20) AppendFloat/64Fixed1-12 18.64n ± 0% 15.49n ± 0% -16.90% (p=0.000 n=20) AppendFloat/64Fixed2-12 18.65n ± 0% 15.49n ± 0% -16.97% (p=0.000 n=20) AppendFloat/64Fixed2.5-12 19.23n ± 1% 15.24n ± 0% -20.75% (p=0.000 n=20) AppendFloat/64Fixed3-12 18.61n ± 0% 15.59n ± 1% -16.21% (p=0.000 n=20) AppendFloat/64Fixed4-12 17.55n ± 1% 15.38n ± 0% -12.36% (p=0.000 n=20) AppendFloat/64Fixed5Hard-12 29.27n ± 1% 17.97n ± 0% -38.59% (p=0.000 n=20) AppendFloat/64Fixed12-12 28.26n ± 1% 28.17n ± 10% ~ (p=0.941 n=20) AppendFloat/64Fixed16-12 23.56n ± 0% 21.46n ± 0% -8.95% (p=0.000 n=20) AppendFloat/64Fixed12Hard-12 21.85n ± 2% 20.70n ± 1% -5.24% (p=0.000 n=20) AppendFloat/64Fixed17Hard-12 26.91n ± 1% 27.10n ± 0% ~ (p=0.059 n=20) AppendFloat/64Fixed18Hard-12 2.197µ ± 1% 2.169µ ± 1% ~ (p=0.013 n=20) AppendFloat/64FixedF1-12 103.7n ± 1% 103.3n ± 0% ~ (p=0.035 n=20) AppendFloat/64FixedF2-12 114.8n ± 1% 114.1n ± 1% ~ (p=0.234 n=20) AppendFloat/64FixedF3-12 107.8n ± 1% 107.1n ± 1% ~ (p=0.180 n=20) AppendFloat/Slowpath64-12 32.05n ± 1% 32.00n ± 0% ~ (p=0.952 n=20) AppendFloat/SlowpathDenormal64-12 29.98n ± 1% 30.20n ± 0% ~ (p=0.004 n=20) geomean 33.83n 31.91n -5.68% host: linux-amd64 goos: linux goarch: amd64 cpu: Intel(R) Xeon(R) CPU @ 2.30GHz │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-16 64.00n ± 1% 63.67n ± 1% ~ (p=0.784 n=20) AppendFloat/Float-16 95.99n ± 1% 97.42n ± 1% +1.50% (p=0.000 n=20) AppendFloat/Exp-16 97.59n ± 1% 97.72n ± 1% ~ (p=0.984 n=20) AppendFloat/NegExp-16 97.80n ± 1% 101.15n ± 1% +3.43% (p=0.000 n=20) AppendFloat/LongExp-16 103.1n ± 1% 104.5n ± 1% ~ (p=0.006 n=20) AppendFloat/Big-16 110.8n ± 1% 108.5n ± 1% -2.07% (p=0.000 n=20) AppendFloat/BinaryExp-16 47.82n ± 1% 47.33n ± 1% ~ (p=0.007 n=20) AppendFloat/32Integer-16 63.65n ± 1% 63.51n ± 0% ~ (p=0.560 n=20) AppendFloat/32ExactFraction-16 91.81n ± 1% 97.03n ± 1% +5.69% (p=0.000 n=20) AppendFloat/32Point-16 89.84n ± 1% 92.16n ± 1% +2.59% (p=0.000 n=20) AppendFloat/32Exp-16 103.80n ± 1% 95.12n ± 1% -8.36% (p=0.000 n=20) AppendFloat/32NegExp-16 93.70n ± 1% 94.87n ± 1% ~ (p=0.003 n=20) AppendFloat/32Shortest-16 83.98n ± 1% 86.45n ± 1% +2.94% (p=0.000 n=20) AppendFloat/32Fixed8Hard-16 61.91n ± 1% 57.81n ± 1% -6.62% (p=0.000 n=20) AppendFloat/32Fixed9Hard-16 71.08n ± 0% 66.81n ± 1% -6.01% (p=0.000 n=20) AppendFloat/64Fixed1-16 59.27n ± 2% 51.49n ± 1% -13.13% (p=0.000 n=20) AppendFloat/64Fixed2-16 57.89n ± 1% 50.87n ± 1% -12.13% (p=0.000 n=20) AppendFloat/64Fixed2.5-16 61.04n ± 1% 49.40n ± 1% -19.08% (p=0.000 n=20) AppendFloat/64Fixed3-16 58.42n ± 1% 52.14n ± 1% -10.75% (p=0.000 n=20) AppendFloat/64Fixed4-16 56.52n ± 1% 50.27n ± 1% -11.07% (p=0.000 n=20) AppendFloat/64Fixed5Hard-16 97.79n ± 1% 57.86n ± 1% -40.83% (p=0.000 n=20) AppendFloat/64Fixed12-16 90.78n ± 1% 83.01n ± 1% -8.56% (p=0.000 n=20) AppendFloat/64Fixed16-16 76.11n ± 1% 70.84n ± 0% -6.92% (p=0.000 n=20) AppendFloat/64Fixed12Hard-16 73.56n ± 1% 68.98n ± 2% -6.23% (p=0.000 n=20) AppendFloat/64Fixed17Hard-16 83.20n ± 1% 79.85n ± 1% -4.03% (p=0.000 n=20) AppendFloat/64Fixed18Hard-16 4.947µ ± 1% 4.915µ ± 1% ~ (p=0.229 n=20) AppendFloat/64FixedF1-16 242.4n ± 1% 239.4n ± 1% ~ (p=0.038 n=20) AppendFloat/64FixedF2-16 257.7n ± 2% 252.6n ± 1% -1.98% (p=0.000 n=20) AppendFloat/64FixedF3-16 237.5n ± 0% 237.5n ± 1% ~ (p=0.440 n=20) AppendFloat/Slowpath64-16 99.75n ± 1% 99.78n ± 1% ~ (p=0.995 n=20) AppendFloat/SlowpathDenormal64-16 97.41n ± 1% 98.20n ± 1% ~ (p=0.006 n=20) geomean 100.7n 95.60n -5.05% host: s7 cpu: AMD Ryzen 9 7950X 16-Core Processor │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-32 22.19n ± 0% 22.04n ± 0% -0.68% (p=0.000 n=20) AppendFloat/Float-32 34.59n ± 0% 34.88n ± 0% +0.84% (p=0.000 n=20) AppendFloat/Exp-32 34.47n ± 0% 34.88n ± 0% +1.20% (p=0.000 n=20) AppendFloat/NegExp-32 34.85n ± 0% 35.32n ± 0% +1.35% (p=0.000 n=20) AppendFloat/LongExp-32 37.23n ± 0% 37.09n ± 0% ~ (p=0.003 n=20) AppendFloat/Big-32 39.27n ± 0% 38.50n ± 0% -1.97% (p=0.000 n=20) AppendFloat/BinaryExp-32 17.38n ± 0% 17.61n ± 0% +1.35% (p=0.000 n=20) AppendFloat/32Integer-32 22.26n ± 0% 22.08n ± 0% -0.79% (p=0.000 n=20) AppendFloat/32ExactFraction-32 32.82n ± 0% 32.91n ± 0% ~ (p=0.018 n=20) AppendFloat/32Point-32 32.88n ± 0% 33.22n ± 0% +1.03% (p=0.000 n=20) AppendFloat/32Exp-32 34.95n ± 0% 34.62n ± 0% -0.94% (p=0.000 n=20) AppendFloat/32NegExp-32 33.23n ± 0% 33.55n ± 0% +0.98% (p=0.000 n=20) AppendFloat/32Shortest-32 30.19n ± 0% 30.12n ± 0% ~ (p=0.122 n=20) AppendFloat/32Fixed8Hard-32 22.94n ± 0% 22.88n ± 0% ~ (p=0.124 n=20) AppendFloat/32Fixed9Hard-32 26.20n ± 0% 25.94n ± 1% -0.97% (p=0.000 n=20) AppendFloat/64Fixed1-32 21.10n ± 0% 18.84n ± 0% -10.71% (p=0.000 n=20) AppendFloat/64Fixed2-32 20.75n ± 0% 18.70n ± 0% -9.88% (p=0.000 n=20) AppendFloat/64Fixed2.5-32 21.07n ± 0% 17.96n ± 0% -14.74% (p=0.000 n=20) AppendFloat/64Fixed3-32 21.24n ± 0% 19.64n ± 0% -7.53% (p=0.000 n=20) AppendFloat/64Fixed4-32 20.63n ± 0% 18.61n ± 0% -9.79% (p=0.000 n=20) AppendFloat/64Fixed5Hard-32 34.48n ± 0% 21.70n ± 0% -37.06% (p=0.000 n=20) AppendFloat/64Fixed12-32 32.26n ± 0% 30.87n ± 1% -4.31% (p=0.000 n=20) AppendFloat/64Fixed16-32 27.95n ± 0% 26.86n ± 0% -3.92% (p=0.000 n=20) AppendFloat/64Fixed12Hard-32 27.30n ± 0% 25.98n ± 1% -4.82% (p=0.000 n=20) AppendFloat/64Fixed17Hard-32 30.80n ± 0% 29.93n ± 0% -2.84% (p=0.000 n=20) AppendFloat/64Fixed18Hard-32 1.833µ ± 0% 1.831µ ± 0% ~ (p=0.663 n=20) AppendFloat/64FixedF1-32 83.42n ± 1% 84.00n ± 1% ~ (p=0.003 n=20) AppendFloat/64FixedF2-32 90.10n ± 0% 89.23n ± 1% -0.95% (p=0.001 n=20) AppendFloat/64FixedF3-32 84.42n ± 1% 84.39n ± 0% ~ (p=0.878 n=20) AppendFloat/Slowpath64-32 35.72n ± 0% 35.59n ± 0% ~ (p=0.007 n=20) AppendFloat/SlowpathDenormal64-32 35.36n ± 0% 35.05n ± 0% -0.88% (p=0.000 n=20) geomean 36.05n 34.69n -3.77% host: linux-386 goarch: 386 cpu: Intel(R) Xeon(R) CPU @ 2.30GHz │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-16 132.8n ± 0% 133.5n ± 0% +0.49% (p=0.001 n=20) AppendFloat/Float-16 242.6n ± 0% 241.7n ± 0% -0.37% (p=0.000 n=20) AppendFloat/Exp-16 252.2n ± 0% 249.1n ± 0% -1.27% (p=0.000 n=20) AppendFloat/NegExp-16 253.6n ± 0% 247.7n ± 0% -2.33% (p=0.000 n=20) AppendFloat/LongExp-16 260.9n ± 0% 257.1n ± 0% -1.48% (p=0.000 n=20) AppendFloat/Big-16 293.7n ± 0% 285.2n ± 0% -2.89% (p=0.000 n=20) AppendFloat/BinaryExp-16 89.63n ± 1% 89.06n ± 0% -0.64% (p=0.000 n=20) AppendFloat/32Integer-16 132.6n ± 0% 133.2n ± 0% ~ (p=0.016 n=20) AppendFloat/32ExactFraction-16 216.9n ± 0% 214.2n ± 0% -1.24% (p=0.000 n=20) AppendFloat/32Point-16 205.0n ± 0% 202.2n ± 0% -1.37% (p=0.000 n=20) AppendFloat/32Exp-16 250.2n ± 0% 235.9n ± 0% -5.72% (p=0.000 n=20) AppendFloat/32NegExp-16 213.5n ± 0% 210.6n ± 0% -1.34% (p=0.000 n=20) AppendFloat/32Shortest-16 198.3n ± 0% 197.8n ± 0% ~ (p=0.147 n=20) AppendFloat/32Fixed8Hard-16 114.9n ± 1% 136.0n ± 1% +18.46% (p=0.000 n=20) AppendFloat/32Fixed9Hard-16 189.8n ± 0% 155.0n ± 1% -18.31% (p=0.000 n=20) AppendFloat/64Fixed1-16 175.8n ± 0% 132.7n ± 0% -24.52% (p=0.000 n=20) AppendFloat/64Fixed2-16 166.6n ± 0% 128.7n ± 0% -22.73% (p=0.000 n=20) AppendFloat/64Fixed2.5-16 176.5n ± 0% 126.8n ± 0% -28.11% (p=0.000 n=20) AppendFloat/64Fixed3-16 165.3n ± 0% 127.1n ± 0% -23.11% (p=0.000 n=20) AppendFloat/64Fixed4-16 141.3n ± 0% 120.8n ± 1% -14.51% (p=0.000 n=20) AppendFloat/64Fixed5Hard-16 344.6n ± 0% 136.0n ± 0% -60.51% (p=0.000 n=20) AppendFloat/64Fixed12-16 184.2n ± 0% 158.7n ± 0% -13.82% (p=0.000 n=20) AppendFloat/64Fixed16-16 174.0n ± 0% 151.3n ± 0% -12.99% (p=0.000 n=20) AppendFloat/64Fixed12Hard-16 169.7n ± 0% 146.7n ± 0% -13.58% (p=0.000 n=20) AppendFloat/64Fixed17Hard-16 207.7n ± 0% 166.6n ± 0% -19.81% (p=0.000 n=20) AppendFloat/64Fixed18Hard-16 10.66µ ± 0% 10.63µ ± 0% ~ (p=0.030 n=20) AppendFloat/64FixedF1-16 615.9n ± 0% 613.5n ± 0% -0.40% (p=0.000 n=20) AppendFloat/64FixedF2-16 846.6n ± 0% 847.4n ± 0% ~ (p=0.551 n=20) AppendFloat/64FixedF3-16 609.9n ± 0% 609.5n ± 0% ~ (p=0.213 n=20) AppendFloat/Slowpath64-16 254.1n ± 0% 252.6n ± 1% ~ (p=0.048 n=20) AppendFloat/SlowpathDenormal64-16 251.5n ± 0% 249.4n ± 0% -0.83% (p=0.000 n=20) geomean 249.2n 225.4n -9.54% host: s7:GOARCH=386 cpu: AMD Ryzen 9 7950X 16-Core Processor │ 14b7e09f493 │ f9bf7fcb8e2 │ │ sec/op │ sec/op vs base │ AppendFloat/Decimal-32 42.65n ± 0% 42.31n ± 0% -0.79% (p=0.000 n=20) AppendFloat/Float-32 71.56n ± 0% 71.06n ± 0% -0.69% (p=0.000 n=20) AppendFloat/Exp-32 75.61n ± 1% 74.85n ± 1% -1.01% (p=0.000 n=20) AppendFloat/NegExp-32 74.36n ± 0% 74.30n ± 0% ~ (p=0.482 n=20) AppendFloat/LongExp-32 75.82n ± 0% 75.73n ± 0% ~ (p=0.490 n=20) AppendFloat/Big-32 85.10n ± 0% 82.61n ± 0% -2.93% (p=0.000 n=20) AppendFloat/BinaryExp-32 33.02n ± 0% 32.48n ± 1% -1.64% (p=0.000 n=20) AppendFloat/32Integer-32 41.54n ± 1% 41.27n ± 1% -0.66% (p=0.000 n=20) AppendFloat/32ExactFraction-32 62.48n ± 0% 62.91n ± 0% +0.69% (p=0.000 n=20) AppendFloat/32Point-32 60.17n ± 0% 60.65n ± 0% +0.80% (p=0.000 n=20) AppendFloat/32Exp-32 73.34n ± 0% 68.99n ± 0% -5.92% (p=0.000 n=20) AppendFloat/32NegExp-32 63.29n ± 0% 62.83n ± 0% -0.73% (p=0.000 n=20) AppendFloat/32Shortest-32 58.97n ± 0% 59.07n ± 0% ~ (p=0.029 n=20) AppendFloat/32Fixed8Hard-32 37.42n ± 0% 41.76n ± 1% +11.61% (p=0.000 n=20) AppendFloat/32Fixed9Hard-32 55.18n ± 0% 50.13n ± 1% -9.16% (p=0.000 n=20) AppendFloat/64Fixed1-32 50.89n ± 1% 41.25n ± 0% -18.94% (p=0.000 n=20) AppendFloat/64Fixed2-32 48.33n ± 1% 40.85n ± 1% -15.48% (p=0.000 n=20) AppendFloat/64Fixed2.5-32 52.46n ± 0% 39.39n ± 0% -24.92% (p=0.000 n=20) AppendFloat/64Fixed3-32 48.28n ± 1% 40.66n ± 0% -15.78% (p=0.000 n=20) AppendFloat/64Fixed4-32 44.57n ± 0% 38.58n ± 0% -13.44% (p=0.000 n=20) AppendFloat/64Fixed5Hard-32 96.16n ± 0% 42.99n ± 1% -55.29% (p=0.000 n=20) AppendFloat/64Fixed12-32 56.84n ± 0% 51.95n ± 1% -8.61% (p=0.000 n=20) AppendFloat/64Fixed16-32 54.23n ± 0% 49.33n ± 0% -9.03% (p=0.000 n=20) AppendFloat/64Fixed12Hard-32 53.47n ± 0% 48.67n ± 0% -8.99% (p=0.000 n=20) AppendFloat/64Fixed17Hard-32 61.76n ± 0% 55.42n ± 1% -10.27% (p=0.000 n=20) AppendFloat/64Fixed18Hard-32 3.998µ ± 1% 4.001µ ± 0% ~ (p=0.449 n=20) AppendFloat/64FixedF1-32 161.8n ± 0% 166.2n ± 1% +2.72% (p=0.000 n=20) AppendFloat/64FixedF2-32 223.4n ± 2% 226.2n ± 1% +1.25% (p=0.000 n=20) AppendFloat/64FixedF3-32 159.6n ± 0% 161.6n ± 1% +1.22% (p=0.000 n=20) AppendFloat/Slowpath64-32 76.69n ± 0% 75.03n ± 0% -2.16% (p=0.000 n=20) AppendFloat/SlowpathDenormal64-32 75.02n ± 0% 74.36n ± 1% ~ (p=0.003 n=20) geomean 74.66n 69.39n -7.06% Change-Id: I9db46471a93bd2aab3c2796e563d154cb531d4cb Reviewed-on: https://go-review.googlesource.com/c/go/+/717182 Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Russ Cox <rsc@golang.org>
1 parent 162ba6c commit 34fec51

File tree

9 files changed

+383
-228
lines changed

9 files changed

+383
-228
lines changed

src/internal/strconv/atoi.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ const intSize = 32 << (^uint(0) >> 63)
4141
// IntSize is the size in bits of an int or uint value.
4242
const IntSize = intSize
4343

44-
const maxUint64 = 1<<64 - 1
45-
4644
// ParseUint is like [ParseInt] but for unsigned numbers.
4745
//
4846
// A sign prefix is not permitted.

src/internal/strconv/export_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ var (
1818
Pow10 = pow10
1919
Umul128 = umul128
2020
Umul192 = umul192
21+
Div5Tab = div5Tab
22+
DivisiblePow5 = divisiblePow5
23+
TrimZeros = trimZeros
2124
)
2225

2326
func NewDecimal(i uint64) *decimal {

src/internal/strconv/ftoa.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,17 @@ func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
123123
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
124124
}
125125

126-
var digs decimalSlice
127-
ok := false
128126
// Negative precision means "only as much as needed to be exact."
129127
shortest := prec < 0
128+
var digs decimalSlice
129+
if mant == 0 {
130+
return formatDigits(dst, shortest, neg, digs, prec, fmt)
131+
}
130132
if shortest {
131133
// Use Ryu algorithm.
132134
var buf [32]byte
133135
digs.d = buf[:]
134136
ryuFtoaShortest(&digs, mant, exp-int(flt.mantbits), flt)
135-
ok = true
136137
// Precision for shortest representation mode.
137138
switch fmt {
138139
case 'e', 'E':
@@ -142,7 +143,11 @@ func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
142143
case 'g', 'G':
143144
prec = digs.nd
144145
}
145-
} else if fmt != 'f' {
146+
return formatDigits(dst, shortest, neg, digs, prec, fmt)
147+
}
148+
149+
// TODO figure out when we can use fast code for f
150+
if fmt != 'f' {
146151
// Fixed number of digits.
147152
digits := prec
148153
switch fmt {
@@ -157,21 +162,15 @@ func genericFtoa(dst []byte, val float64, fmt byte, prec, bitSize int) []byte {
157162
// Invalid mode.
158163
digits = 1
159164
}
160-
var buf [24]byte
161-
if bitSize == 32 && digits <= 9 {
165+
if digits <= 18 {
166+
var buf [24]byte
162167
digs.d = buf[:]
163-
ryuFtoaFixed32(&digs, uint32(mant), exp-int(flt.mantbits), digits)
164-
ok = true
165-
} else if digits <= 18 {
166-
digs.d = buf[:]
167-
ryuFtoaFixed64(&digs, mant, exp-int(flt.mantbits), digits)
168-
ok = true
168+
fixedFtoa(&digs, mant, exp-int(flt.mantbits), digits)
169+
return formatDigits(dst, false, neg, digs, prec, fmt)
169170
}
170171
}
171-
if !ok {
172-
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
173-
}
174-
return formatDigits(dst, shortest, neg, digs, prec, fmt)
172+
173+
return bigFtoa(dst, prec, fmt, neg, mant, exp, flt)
175174
}
176175

177176
// bigFtoa uses multiprecision computations to format a float.

src/internal/strconv/ftoa_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,16 @@ var ftoatests = []ftoaTest{
177177
{1.801439850948199e+16, 'g', -1, "1.801439850948199e+16"},
178178
{5.960464477539063e-08, 'g', -1, "5.960464477539063e-08"},
179179
{1.012e-320, 'g', -1, "1.012e-320"},
180+
181+
// Cases from TestFtoaRandom that caught bugs in fixedFtoa.
182+
{8177880169308380. * (1 << 1), 'e', 14, "1.63557603386168e+16"},
183+
{8393378656576888. * (1 << 1), 'e', 15, "1.678675731315378e+16"},
184+
{8738676561280626. * (1 << 4), 'e', 16, "1.3981882498049002e+17"},
185+
{8291032395191335. / (1 << 30), 'e', 5, "7.72163e+06"},
186+
187+
// Exercise divisiblePow5 case in fixedFtoa
188+
{2384185791015625. * (1 << 12), 'e', 5, "9.76562e+18"},
189+
{2384185791015625. * (1 << 13), 'e', 5, "1.95312e+19"},
180190
}
181191

182192
func TestFtoa(t *testing.T) {
@@ -253,7 +263,7 @@ func TestFtoaRandom(t *testing.T) {
253263
shortSlow = FormatFloat(x, 'e', prec, 64)
254264
SetOptimize(true)
255265
if shortSlow != shortFast {
256-
t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
266+
t.Errorf("%b printed with %%.%de as %s, want %s", x, prec, shortFast, shortSlow)
257267
}
258268
}
259269
}

src/internal/strconv/ftoafixed.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package strconv
6+
7+
import "math/bits"
8+
9+
var uint64pow10 = [...]uint64{
10+
1, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
11+
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
12+
}
13+
14+
// fixedFtoa formats a number of decimal digits of mant*(2^exp) into d,
15+
// where mant > 0 and 1 ≤ digits ≤ 18.
16+
func fixedFtoa(d *decimalSlice, mant uint64, exp, digits int) {
17+
// The strategy here is to multiply (mant * 2^exp) by a power of 10
18+
// to make the resulting integer be the number of digits we want.
19+
//
20+
// Adams proved in the Ryu paper that 128-bit precision in the
21+
// power-of-10 constant is sufficient to produce correctly
22+
// rounded output for all float64s, up to 18 digits.
23+
// https://dl.acm.org/doi/10.1145/3192366.3192369
24+
//
25+
// TODO(rsc): The paper is not focused on, nor terribly clear about,
26+
// this fact in this context, and the proof seems too complicated.
27+
// Post a shorter, more direct proof and link to it here.
28+
29+
if digits > 18 {
30+
panic("fixedFtoa called with digits > 18")
31+
}
32+
33+
// Shift mantissa to have 64 bits,
34+
// so that the 192-bit product below will
35+
// have at least 63 bits in its top word.
36+
b := 64 - bits.Len64(mant)
37+
mant <<= b
38+
exp -= b
39+
40+
// We have f = mant * 2^exp ≥ 2^(63+exp)
41+
// and we want to multiply it by some 10^p
42+
// to make it have the number of digits plus one rounding bit:
43+
//
44+
// 2 * 10^(digits-1) ≤ f * 10^p < ~2 * 10^digits
45+
//
46+
// The lower bound is required, but the upper bound is approximate:
47+
// we must not have too few digits, but we can round away extra ones.
48+
//
49+
// f * 10^p ≥ 2 * 10^(digits-1)
50+
// 10^p ≥ 2 * 10^(digits-1) / f [dividing by f]
51+
// p ≥ (log₁₀ 2) + (digits-1) - log₁₀ f [taking log₁₀]
52+
// p ≥ (log₁₀ 2) + (digits-1) - log₁₀ (mant * 2^exp) [expanding f]
53+
// p ≥ (log₁₀ 2) + (digits-1) - (log₁₀ 2) * (64 + exp) [mant < 2⁶⁴]
54+
// p ≥ (digits - 1) - (log₁₀ 2) * (63 + exp) [refactoring]
55+
//
56+
// Once we have p, we can compute the scaled value:
57+
//
58+
// dm * 2^de = mant * 2^exp * 10^p
59+
// = mant * 2^exp * pow/2^128 * 2^exp2.
60+
// = (mant * pow/2^128) * 2^(exp+exp2).
61+
p := (digits - 1) - mulLog10_2(63+exp)
62+
pow, exp2, ok := pow10(p)
63+
if !ok {
64+
// This never happens due to the range of float32/float64 exponent
65+
panic("fixedFtoa: pow10 out of range")
66+
}
67+
if -22 <= p && p < 0 {
68+
// Special case: Let q=-p. q is in [1,22]. We are dividing by 10^q
69+
// and the mantissa may be a multiple of 5^q (5^22 < 2^53),
70+
// in which case the division must be computed exactly and
71+
// recorded as exact for correct rounding. Our normal computation is:
72+
//
73+
// dm = floor(mant * floor(10^p * 2^s))
74+
//
75+
// for some scaling shift s. To make this an exact division,
76+
// it suffices to change the inner floor to a ceil:
77+
//
78+
// dm = floor(mant * ceil(10^p * 2^s))
79+
//
80+
// In the range of values we are using, the floor and ceil
81+
// cancel each other out and the high 64 bits of the product
82+
// come out exactly right.
83+
// (This is the same trick compilers use for division by constants.
84+
// See Hacker's Delight, 2nd ed., Chapter 10.)
85+
pow.Lo++
86+
}
87+
dm, lo1, lo0 := umul192(mant, pow)
88+
de := exp + exp2
89+
90+
// Check whether any bits have been truncated from dm.
91+
// If so, set dt != 0. If not, leave dt == 0 (meaning dm is exact).
92+
var dt uint
93+
switch {
94+
default:
95+
// Most powers of 10 use a truncated constant,
96+
// meaning the result is also truncated.
97+
dt = 1
98+
case 0 <= p && p <= 55:
99+
// Small positive powers of 10 (up to 10⁵⁵) can be represented
100+
// precisely in a 128-bit mantissa (5⁵⁵ ≤ 2¹²⁸), so the only truncation
101+
// comes from discarding the low bits of the 192-bit product.
102+
//
103+
// TODO(rsc): The new proof mentioned above should also
104+
// prove that we can't have lo1 == 0 and lo0 != 0.
105+
// After proving that, drop computation and use of lo0 here.
106+
dt = bool2uint(lo1|lo0 != 0)
107+
case -22 <= p && p < 0 && divisiblePow5(mant, -p):
108+
// If the original mantissa was a multiple of 5^p,
109+
// the result is exact. (See comment above for pow.Lo++.)
110+
dt = 0
111+
}
112+
113+
// The value we want to format is dm * 2^de, where de < 0.
114+
// Multply by 2^de by shifting, but leave one extra bit for rounding.
115+
// After the shift, the "integer part" of dm is dm>>1,
116+
// the "rounding bit" (the first fractional bit) is dm&1,
117+
// and the "truncated bit" (have any bits been discarded?) is dt.
118+
shift := -de - 1
119+
dt |= bool2uint(dm&(1<<shift-1) != 0)
120+
dm >>= shift
121+
122+
// Set decimal point in eventual formatted digits,
123+
// so we can update it as we adjust the digits.
124+
d.dp = digits - p
125+
126+
// Trim excess digit if any, updating truncation and decimal point.
127+
// The << 1 is leaving room for the rounding bit.
128+
max := uint64pow10[digits] << 1
129+
if dm >= max {
130+
var r uint
131+
dm, r = dm/10, uint(dm%10)
132+
dt |= bool2uint(r != 0)
133+
d.dp++
134+
}
135+
136+
// Round and shift away rounding bit.
137+
// We want to round up when
138+
// (a) the fractional part is > 0.5 (dm&1 != 0 and dt == 1)
139+
// (b) or the fractional part is ≥ 0.5 and the integer part is odd
140+
// (dm&1 != 0 and dm&2 != 0).
141+
// The bitwise expression encodes that logic.
142+
dm += uint64(uint(dm) & (dt | uint(dm)>>1) & 1)
143+
dm >>= 1
144+
if dm == max>>1 {
145+
// 999... rolled over to 1000...
146+
dm = uint64pow10[digits-1]
147+
d.dp++
148+
}
149+
150+
// Format digits into d.
151+
formatBase10(d.d[:digits], dm)
152+
d.nd = digits
153+
for d.d[d.nd-1] == '0' {
154+
d.nd--
155+
}
156+
}

0 commit comments

Comments
 (0)