| |
| |
| |
|
|
| package strconv |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| func dboxFtoa(d *decimalSlice, mant uint64, exp int, denorm bool, bitSize int) { |
| if bitSize == 32 { |
| dboxFtoa32(d, uint32(mant), exp, denorm) |
| return |
| } |
| dboxFtoa64(d, mant, exp, denorm) |
| } |
|
|
| func dboxFtoa64(d *decimalSlice, mant uint64, exp int, denorm bool) { |
| if mant == 1<<float64MantBits && !denorm { |
| |
| k0 := -mulLog10_2MinusLog10_4Over3(exp) |
| φ, β := dboxPow64(k0, exp) |
| xi, zi := dboxRange64(φ, β) |
| if exp != 2 && exp != 3 { |
| xi++ |
| } |
| q := zi / 10 |
| if xi <= q*10 { |
| q, zeros := trimZeros(q) |
| dboxDigits(d, q, -k0+1+zeros) |
| return |
| } |
| yru := dboxRoundUp64(φ, β) |
| if exp == -77 && yru%2 != 0 { |
| yru-- |
| } else if yru < xi { |
| yru++ |
| } |
| dboxDigits(d, yru, -k0) |
| return |
| } |
|
|
| |
| const ( |
| κ = 2 |
| p10κ = 100 |
| p10κ1 = p10κ * 10 |
| ) |
|
|
| |
| k0 := -mulLog10_2(exp) |
| φ, β := dboxPow64(κ+k0, exp) |
| zi, exact := dboxMulPow64(uint64(mant*2+1)<<β, φ) |
| s, r := zi/p10κ1, uint32(zi%p10κ1) |
| δi := dboxDelta64(φ, β) |
|
|
| if r < δi { |
| if r != 0 || !exact || mant%2 == 0 { |
| s, zeros := trimZeros(s) |
| dboxDigits(d, s, -k0+1+zeros) |
| return |
| } |
| s-- |
| r = p10κ * 10 |
| } else if r == δi { |
| parity, exact := dboxParity64(uint64(mant*2-1), φ, β) |
| if parity || (exact && mant%2 == 0) { |
| s, zeros := trimZeros(s) |
| dboxDigits(d, s, -k0+1+zeros) |
| return |
| } |
| } |
|
|
| |
| D := r + p10κ/2 - δi/2 |
| t, ρ := D/p10κ, D%p10κ |
| yru := 10*s + uint64(t) |
| if ρ == 0 { |
| parity, exact := dboxParity64(mant*2, φ, β) |
| if parity != ((D-p10κ/2)%2 != 0) || exact && yru%2 != 0 { |
| yru-- |
| } |
| } |
| dboxDigits(d, yru, -k0) |
| } |
|
|
| |
| |
| func dboxFtoa32(d *decimalSlice, mant uint32, exp int, denorm bool) { |
| if mant == 1<<float32MantBits && !denorm { |
| |
| k0 := -mulLog10_2MinusLog10_4Over3(exp) |
| φ, β := dboxPow32(k0, exp) |
| xi, zi := dboxRange32(φ, β) |
| if exp != 2 && exp != 3 { |
| xi++ |
| } |
| q := zi / 10 |
| if xi <= q*10 { |
| q, zeros := trimZeros(uint64(q)) |
| dboxDigits(d, q, -k0+1+zeros) |
| return |
| } |
| yru := dboxRoundUp32(φ, β) |
| if exp == -77 && yru%2 != 0 { |
| yru-- |
| } else if yru < xi { |
| yru++ |
| } |
| dboxDigits(d, uint64(yru), -k0) |
| return |
| } |
|
|
| |
| const ( |
| κ = 1 |
| p10κ = 10 |
| p10κ1 = p10κ * 10 |
| ) |
|
|
| |
| k0 := -mulLog10_2(exp) |
| φ, β := dboxPow32(κ+k0, exp) |
| zi, exact := dboxMulPow32(uint32(mant*2+1)<<β, φ) |
| s, r := zi/p10κ1, uint32(zi%p10κ1) |
| δi := dboxDelta32(φ, β) |
|
|
| if r < δi { |
| if r != 0 || !exact || mant%2 == 0 { |
| s, zeros := trimZeros(uint64(s)) |
| dboxDigits(d, s, -k0+1+zeros) |
| return |
| } |
| s-- |
| r = p10κ * 10 |
| } else if r == δi { |
| parity, exact := dboxParity32(uint32(mant*2-1), φ, β) |
| if parity || (exact && mant%2 == 0) { |
| s, zeros := trimZeros(uint64(s)) |
| dboxDigits(d, s, -k0+1+zeros) |
| return |
| } |
| } |
|
|
| |
| D := r + p10κ/2 - δi/2 |
| t, ρ := D/p10κ, D%p10κ |
| yru := 10*s + uint32(t) |
| if ρ == 0 { |
| parity, exact := dboxParity32(mant*2, φ, β) |
| if parity != ((D-p10κ/2)%2 != 0) || exact && yru%2 != 0 { |
| yru-- |
| } |
| } |
| dboxDigits(d, uint64(yru), -k0) |
| } |
|
|
| |
| |
| func dboxDigits(d *decimalSlice, mant uint64, exp int) { |
| i := formatBase10(d.d, mant) |
| d.d = d.d[i:] |
| d.nd = len(d.d) |
| d.dp = d.nd + exp |
| } |
|
|
| |
| func uadd128(u uint128, n uint64) uint128 { |
| sum := uint64(u.Lo + n) |
| |
| if sum < u.Lo { |
| u.Hi++ |
| } |
| u.Lo = sum |
| return u |
| } |
|
|
| |
| func umul64(x, y uint32) uint64 { |
| return uint64(x) * uint64(y) |
| } |
|
|
| |
| func umul96Upper64(x uint32, y uint64) uint64 { |
| yh := uint32(y >> 32) |
| yl := uint32(y) |
|
|
| xyh := umul64(x, yh) |
| xyl := umul64(x, yl) |
|
|
| return xyh + (xyl >> 32) |
| } |
|
|
| |
| func umul96Lower64(x uint32, y uint64) uint64 { |
| return uint64(uint64(x) * y) |
| } |
|
|
| |
| func umul128Upper64(x, y uint64) uint64 { |
| a := uint32(x >> 32) |
| b := uint32(x) |
| c := uint32(y >> 32) |
| d := uint32(y) |
|
|
| ac := umul64(a, c) |
| bc := umul64(b, c) |
| ad := umul64(a, d) |
| bd := umul64(b, d) |
|
|
| intermediate := (bd >> 32) + uint64(uint32(ad)) + uint64(uint32(bc)) |
|
|
| return ac + (intermediate >> 32) + (ad >> 32) + (bc >> 32) |
| } |
|
|
| |
| func umul192Upper128(x uint64, y uint128) uint128 { |
| r := umul128(x, y.Hi) |
| t := umul128Upper64(x, y.Lo) |
| return uadd128(r, t) |
| } |
|
|
| |
| func umul192Lower128(x uint64, y uint128) uint128 { |
| high := x * y.Hi |
| highLow := umul128(x, y.Lo) |
| return uint128{uint64(high + highLow.Hi), highLow.Lo} |
| } |
|
|
| |
| |
| |
| func dboxMulPow64(u uint64, phi uint128) (intPart uint64, isInt bool) { |
| r := umul192Upper128(u, phi) |
| intPart = r.Hi |
| isInt = r.Lo == 0 |
| return |
| } |
|
|
| |
| |
| |
| func dboxMulPow32(u uint32, phi uint64) (intPart uint32, isInt bool) { |
| r := umul96Upper64(u, phi) |
| intPart = uint32(r >> 32) |
| isInt = uint32(r) == 0 |
| return |
| } |
|
|
| |
| |
| |
| func dboxParity64(mant2 uint64, phi uint128, beta int) (parity bool, isInt bool) { |
| r := umul192Lower128(mant2, phi) |
| parity = ((r.Hi >> (64 - beta)) & 1) != 0 |
| isInt = ((uint64(r.Hi << beta)) | (r.Lo >> (64 - beta))) == 0 |
| return |
| } |
|
|
| |
| |
| |
| func dboxParity32(mant2 uint32, phi uint64, beta int) (parity bool, isInt bool) { |
| r := umul96Lower64(mant2, phi) |
| parity = ((r >> (64 - beta)) & 1) != 0 |
| isInt = uint32(r>>(32-beta)) == 0 |
| return |
| } |
|
|
| |
| func dboxDelta64(φ uint128, β int) uint32 { |
| return uint32(φ.Hi >> (64 - 1 - β)) |
| } |
|
|
| |
| func dboxDelta32(φ uint64, β int) uint32 { |
| return uint32(φ >> (64 - 1 - β)) |
| } |
|
|
| |
| |
| func mulLog10_2MinusLog10_4Over3(e int) int { |
| |
| return (e*631305 - 261663) >> 21 |
| } |
|
|
| const ( |
| floatMantBits64 = 52 |
| floatMantBits32 = 23 |
| ) |
|
|
| |
| func dboxRange64(φ uint128, β int) (left, right uint64) { |
| left = (φ.Hi - (φ.Hi >> (float64MantBits + 2))) >> (64 - float64MantBits - 1 - β) |
| right = (φ.Hi + (φ.Hi >> (float64MantBits + 1))) >> (64 - float64MantBits - 1 - β) |
| return left, right |
| } |
|
|
| |
| func dboxRange32(φ uint64, β int) (left, right uint32) { |
| left = uint32((φ - (φ >> (floatMantBits32 + 2))) >> (64 - floatMantBits32 - 1 - β)) |
| right = uint32((φ + (φ >> (floatMantBits32 + 1))) >> (64 - floatMantBits32 - 1 - β)) |
| return left, right |
| } |
|
|
| |
| func dboxRoundUp64(phi uint128, beta int) uint64 { |
| return (phi.Hi>>(128/2-floatMantBits64-2-beta) + 1) / 2 |
| } |
|
|
| |
| func dboxRoundUp32(phi uint64, beta int) uint32 { |
| return uint32(phi>>(64-floatMantBits32-2-beta)+1) / 2 |
| } |
|
|
| |
| func dboxPow64(k, e int) (φ uint128, β int) { |
| φ, e1, _ := pow10(k) |
| if k < 0 || k > 55 { |
| φ.Lo++ |
| } |
| β = e + e1 - 1 |
| return φ, β |
| } |
|
|
| |
| func dboxPow32(k, e int) (mant uint64, exp int) { |
| m, e1, _ := pow10(k) |
| if k < 0 || k > 27 { |
| m.Hi++ |
| } |
| exp = e + e1 - 1 |
| return m.Hi, exp |
| } |
|
|