|
| 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