| 1 |
package decoder |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"math/bits" |
| 6 |
) |
| 7 |
|
| 8 |
//go:generate go run ../cmd/pow10table pow10_table.go |
| 9 |
|
| 10 |
// pow10Entry is 10^q as a truncated 128-bit significand: ( hi*2^64 + lo ) * 2^exp2 <= 10^q, less than the |
| 11 |
// next significand times 2^exp2. |
| 12 |
type pow10Entry struct { |
| 13 |
hi, lo uint64 |
| 14 |
exp2 int |
| 15 |
} |
| 16 |
|
| 17 |
// mulPow10 returns the float64 nearest to w * 10^q, rounded to even, and true; or false when that is not decided |
| 18 |
// by the table, which the caller then leaves to strconv. |
| 19 |
// |
| 20 |
// The significand T of 10^q is truncated, so the exact product w * 10^q is in [w*T, w*T + w) times 2^exp2. Both |
| 21 |
// ends are rounded to 53 bits: when they give the same float64, it is the one of every value between them, so it |
| 22 |
// is the float64 of w * 10^q. Only a product very near a half-way point between two float64 values gives two, |
| 23 |
// which is rare. |
| 24 |
func mulPow10(w uint64, q int) (float64, bool) { |
| 25 |
if w == 0 { |
| 26 |
return 0, true |
| 27 |
} |
| 28 |
if q < minPow10Exp10 || q > maxPow10Exp10 { |
| 29 |
return 0, false |
| 30 |
} |
| 31 |
p := &pow10Table[q-minPow10Exp10] |
| 32 |
// the lower end: w * T, a number of 192 bits in p2, p1, p0. |
| 33 |
hi1, lo1 := bits.Mul64(w, p.lo) |
| 34 |
hi2, lo2 := bits.Mul64(w, p.hi) |
| 35 |
p0 := lo1 |
| 36 |
p1, carry := bits.Add64(lo2, hi1, 0) |
| 37 |
p2 := hi2 + carry |
| 38 |
// the upper end: w * T + w. |
| 39 |
u0, c0 := bits.Add64(p0, w, 0) |
| 40 |
u1, c1 := bits.Add64(p1, 0, c0) |
| 41 |
u2 := p2 + c1 |
| 42 |
m, e, ok := round192(p2, p1, p0) |
| 43 |
if !ok { |
| 44 |
return 0, false |
| 45 |
} |
| 46 |
mu, eu, ok := round192(u2, u1, u0) |
| 47 |
if !ok || mu != m || eu != e { |
| 48 |
return 0, false |
| 49 |
} |
| 50 |
// the value is m * 2^( e + exp2 ), and m has 53 bits. |
| 51 |
exp := e + p.exp2 |
| 52 |
// a normal float64 is m * 2^exp with 53-bit m and exp from -1074 to 971. |
| 53 |
if exp < -1074 || exp > 971 { |
| 54 |
return 0, false |
| 55 |
} |
| 56 |
return math.Ldexp(float64(m), exp), true |
| 57 |
} |
| 58 |
|
| 59 |
// round192 rounds the 192-bit number x2*2^128 + x1*2^64 + x0, whose top word is not zero, to 53 bits, to even: |
| 60 |
// it returns m and e such that the rounded number is m * 2^e with 2^52 <= m < 2^53. |
| 61 |
func round192(x2, x1, x0 uint64) (uint64, int, bool) { |
| 62 |
if x2 == 0 { |
| 63 |
return 0, 0, false |
| 64 |
} |
| 65 |
lz := bits.LeadingZeros64(x2) |
| 66 |
// the top 64 bits of the number, and whether a bit below them is set. |
| 67 |
top := x2<<uint(lz) | x1>>uint(64-lz) |
| 68 |
if lz == 0 { |
| 69 |
top = x2 |
| 70 |
} |
| 71 |
rest := x1<<uint(lz) | x0 |
| 72 |
sticky := rest != 0 || top&(1<<10-1) != 0 |
| 73 |
m := top >> 11 |
| 74 |
half := top>>10&1 != 0 |
| 75 |
if half && (sticky || m&1 != 0) { |
| 76 |
m++ |
| 77 |
if m == 1<<53 { |
| 78 |
m >>= 1 |
| 79 |
return m, 11 + 128 - lz + 1, true |
| 80 |
} |
| 81 |
} |
| 82 |
return m, 11 + 128 - lz, true |
| 83 |
} |