X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=libgo%2Fgo%2Fstrconv%2Fextfloat.go;h=b7eaaa61bf4d92016c62a3fe4c4cd3bf6bb7da79;hb=4d8cd3a26294ce35abb17668eac2b6c38dd23bd0;hp=aa5e5607ca0b94c795ddeaff0e21f7c9faa76160;hpb=c944d49b3bd3667c65c299afd3b1d756084203f4;p=platform%2Fupstream%2Fgcc48.git diff --git a/libgo/go/strconv/extfloat.go b/libgo/go/strconv/extfloat.go index aa5e560..b7eaaa6 100644 --- a/libgo/go/strconv/extfloat.go +++ b/libgo/go/strconv/extfloat.go @@ -4,8 +4,6 @@ package strconv -import "math" - // An extFloat represents an extended floating-point number, with more // precision than a float64. It does not try to save bits: the // number represented by the structure is mant*(2^exp), with a negative @@ -127,8 +125,7 @@ var powersOfTen = [...]extFloat{ // floatBits returns the bits of the float64 that best approximates // the extFloat passed as receiver. Overflow is set to true if // the resulting float64 is ±Inf. -func (f *extFloat) floatBits() (bits uint64, overflow bool) { - flt := &float64info +func (f *extFloat) floatBits(flt *floatInfo) (bits uint64, overflow bool) { f.Normalize() exp := f.exp + 63 @@ -140,7 +137,7 @@ func (f *extFloat) floatBits() (bits uint64, overflow bool) { exp += n } - // Extract 1+flt.mantbits bits. + // Extract 1+flt.mantbits bits from the 64-bit mantissa. mant := f.mant >> (63 - flt.mantbits) if f.mant&(1<<(62-flt.mantbits)) != 0 { // Round up. @@ -155,22 +152,14 @@ func (f *extFloat) floatBits() (bits uint64, overflow bool) { // Infinities. if exp-flt.bias >= 1<>(flt.expbits+flt.mantbits) != 0 - expBiased := int(bits>>flt.mantbits) & (1<>uint(-f.exp))<>= uint(-f.exp) + f.exp = 0 + return *f, *f + } + expBiased := exp - flt.bias upper = extFloat{mant: 2*f.mant + 1, exp: f.exp - 1, neg: f.neg} - if mant != 0 || expBiased == 1 { + if mant != 1<>(64-32) == 0 { + mant <<= 32 + exp -= 32 + } + if mant>>(64-16) == 0 { + mant <<= 16 + exp -= 16 } - for f.mant < (1 << 63) { - f.mant <<= 1 - f.exp -= 1 + if mant>>(64-8) == 0 { + mant <<= 8 + exp -= 8 } - return uint(exp_before - f.exp) + if mant>>(64-4) == 0 { + mant <<= 4 + exp -= 4 + } + if mant>>(64-2) == 0 { + mant <<= 2 + exp -= 2 + } + if mant>>(64-1) == 0 { + mant <<= 1 + exp -= 1 + } + shift = uint(f.exp - exp) + f.mant, f.exp = mant, exp + return } // Multiply sets f to the product f*g: the result is correctly rounded, @@ -264,24 +255,22 @@ var uint64pow10 = [...]uint64{ 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, } -// AssignDecimal sets f to an approximate value of the decimal d. It +// AssignDecimal sets f to an approximate value mantissa*10^exp. It // returns true if the value represented by f is guaranteed to be the -// best approximation of d after being rounded to a float64. -func (f *extFloat) AssignDecimal(d *decimal) (ok bool) { +// best approximation of d after being rounded to a float64 or +// float32 depending on flt. +func (f *extFloat) AssignDecimal(mantissa uint64, exp10 int, neg bool, trunc bool, flt *floatInfo) (ok bool) { const uint64digits = 19 const errorscale = 8 - mant10, digits := d.atou64() - exp10 := d.dp - digits errors := 0 // An upper bound for error, computed in errorscale*ulp. - - if digits < d.nd { + if trunc { // the decimal number was truncated. errors += errorscale / 2 } - f.mant = mant10 + f.mant = mantissa f.exp = 0 - f.neg = d.neg + f.neg = neg // Multiply by powers of ten. i := (exp10 - firstPowerOfTen) / stepPowerOfTen @@ -291,9 +280,9 @@ func (f *extFloat) AssignDecimal(d *decimal) (ok bool) { adjExp := (exp10 - firstPowerOfTen) % stepPowerOfTen // We multiply by exp%step - if digits+adjExp <= uint64digits { - // We can multiply the mantissa - f.mant *= uint64(float64pow10[adjExp]) + if adjExp < uint64digits && mantissa < uint64pow10[uint64digits-adjExp] { + // We can multiply the mantissa exactly. + f.mant *= uint64pow10[adjExp] f.Normalize() } else { f.Normalize() @@ -318,10 +307,10 @@ func (f *extFloat) AssignDecimal(d *decimal) (ok bool) { // The 64 bits mantissa is 1 + 52 bits for float64 + 11 extra bits. // // In many cases the approximation will be good enough. - const denormalExp = -1023 - 63 - flt := &float64info + denormalExp := flt.bias - 63 var extrabits uint if f.exp <= denormalExp { + // f.mant * 2^f.exp is smaller than 2^(flt.bias+1). extrabits = uint(63 - flt.mantbits + 1 + uint(denormalExp-f.exp)) } else { extrabits = uint(63 - flt.mantbits) @@ -344,16 +333,17 @@ func (f *extFloat) AssignDecimal(d *decimal) (ok bool) { // f by an approximate power of ten 10^-exp, and returns exp10, so // that f*10^exp10 has the same value as the old f, up to an ulp, // as well as the index of 10^-exp in the powersOfTen table. -// The arguments expMin and expMax constrain the final value of the -// binary exponent of f. -func (f *extFloat) frexp10(expMin, expMax int) (exp10, index int) { - // it is illegal to call this function with a too restrictive exponent range. - if expMax-expMin <= 25 { - panic("strconv: invalid exponent range") - } +func (f *extFloat) frexp10() (exp10, index int) { + // The constants expMin and expMax constrain the final value of the + // binary exponent of f. We want a small integral part in the result + // because finding digits of an integer requires divisions, whereas + // digits of the fractional part can be found by repeatedly multiplying + // by 10. + const expMin = -60 + const expMax = -32 // Find power of ten such that x * 10^n has a binary exponent - // between expMin and expMax - approxExp10 := -(f.exp + 100) * 28 / 93 // log(10)/log(2) is close to 93/28. + // between expMin and expMax. + approxExp10 := ((expMin+expMax)/2 - f.exp) * 28 / 93 // log(10)/log(2) is close to 93/28. i := (approxExp10 - firstPowerOfTen) / stepPowerOfTen Loop: for { @@ -375,26 +365,202 @@ Loop: } // frexp10Many applies a common shift by a power of ten to a, b, c. -func frexp10Many(expMin, expMax int, a, b, c *extFloat) (exp10 int) { - exp10, i := c.frexp10(expMin, expMax) +func frexp10Many(a, b, c *extFloat) (exp10 int) { + exp10, i := c.frexp10() a.Multiply(powersOfTen[i]) b.Multiply(powersOfTen[i]) return } +// FixedDecimal stores in d the first n significant digits +// of the decimal representation of f. It returns false +// if it cannot be sure of the answer. +func (f *extFloat) FixedDecimal(d *decimalSlice, n int) bool { + if f.mant == 0 { + d.nd = 0 + d.dp = 0 + d.neg = f.neg + return true + } + if n == 0 { + panic("strconv: internal error: extFloat.FixedDecimal called with n == 0") + } + // Multiply by an appropriate power of ten to have a reasonable + // number to process. + f.Normalize() + exp10, _ := f.frexp10() + + shift := uint(-f.exp) + integer := uint32(f.mant >> shift) + fraction := f.mant - (uint64(integer) << shift) + ε := uint64(1) // ε is the uncertainty we have on the mantissa of f. + + // Write exactly n digits to d. + needed := n // how many digits are left to write. + integerDigits := 0 // the number of decimal digits of integer. + pow10 := uint64(1) // the power of ten by which f was scaled. + for i, pow := 0, uint64(1); i < 20; i++ { + if pow > uint64(integer) { + integerDigits = i + break + } + pow *= 10 + } + rest := integer + if integerDigits > needed { + // the integral part is already large, trim the last digits. + pow10 = uint64pow10[integerDigits-needed] + integer /= uint32(pow10) + rest -= integer * uint32(pow10) + } else { + rest = 0 + } + + // Write the digits of integer: the digits of rest are omitted. + var buf [32]byte + pos := len(buf) + for v := integer; v > 0; { + v1 := v / 10 + v -= 10 * v1 + pos-- + buf[pos] = byte(v + '0') + v = v1 + } + for i := pos; i < len(buf); i++ { + d.d[i-pos] = buf[i] + } + nd := len(buf) - pos + d.nd = nd + d.dp = integerDigits + exp10 + needed -= nd + + if needed > 0 { + if rest != 0 || pow10 != 1 { + panic("strconv: internal error, rest != 0 but needed > 0") + } + // Emit digits for the fractional part. Each time, 10*fraction + // fits in a uint64 without overflow. + for needed > 0 { + fraction *= 10 + ε *= 10 // the uncertainty scales as we multiply by ten. + if 2*ε > 1<> shift + d.d[nd] = byte(digit + '0') + fraction -= digit << shift + nd++ + needed-- + } + d.nd = nd + } + + // We have written a truncation of f (a numerator / 10^d.dp). The remaining part + // can be interpreted as a small number (< 1) to be added to the last digit of the + // numerator. + // + // If rest > 0, the amount is: + // (rest< 0 guarantees that pow10 << shift does not overflow a uint64. + // + // If rest = 0, pow10 == 1 and the amount is + // fraction / (1 << shift) + // fraction being known with a ±ε uncertainty. + // + // We pass this information to the rounding routine for adjustment. + + ok := adjustLastDigitFixed(d, uint64(rest)<= 0; i-- { + if d.d[i] != '0' { + d.nd = i + 1 + break + } + } + return true +} + +// adjustLastDigitFixed assumes d contains the representation of the integral part +// of some number, whose fractional part is num / (den << shift). The numerator +// num is only known up to an uncertainty of size ε, assumed to be less than +// (den << shift)/2. +// +// It will increase the last digit by one to account for correct rounding, typically +// when the fractional part is greater than 1/2, and will return false if ε is such +// that no correct answer can be given. +func adjustLastDigitFixed(d *decimalSlice, num, den uint64, shift uint, ε uint64) bool { + if num > den< den< den< (den< den<= 0; i-- { + if d.d[i] == '9' { + d.nd-- + } else { + break + } + } + if i < 0 { + d.d[0] = '1' + d.nd = 1 + d.dp++ + } else { + d.d[i]++ + } + return true + } + return false +} + // ShortestDecimal stores in d the shortest decimal representation of f // which belongs to the open interval (lower, upper), where f is supposed // to lie. It returns false whenever the result is unsure. The implementation // uses the Grisu3 algorithm. -func (f *extFloat) ShortestDecimal(d *decimal, lower, upper *extFloat) bool { +func (f *extFloat) ShortestDecimal(d *decimalSlice, lower, upper *extFloat) bool { if f.mant == 0 { - d.d[0] = '0' - d.nd = 1 + d.nd = 0 d.dp = 0 d.neg = f.neg + return true + } + if f.exp == 0 && *lower == *f && *lower == *upper { + // an exact integer. + var buf [24]byte + n := len(buf) - 1 + for v := f.mant; v > 0; { + v1 := v / 10 + v -= 10 * v1 + buf[n] = byte(v + '0') + n-- + v = v1 + } + nd := len(buf) - n - 1 + for i := 0; i < nd; i++ { + d.d[i] = buf[n+1+i] + } + d.nd, d.dp = nd, nd + for d.nd > 0 && d.d[d.nd-1] == '0' { + d.nd-- + } + if d.nd == 0 { + d.dp = 0 + } + d.neg = f.neg + return true } - const minExp = -60 - const maxExp = -32 upper.Normalize() // Uniformize exponents. if f.exp > upper.exp { @@ -406,7 +572,7 @@ func (f *extFloat) ShortestDecimal(d *decimal, lower, upper *extFloat) bool { lower.exp = upper.exp } - exp10 := frexp10Many(minExp, maxExp, lower, f, upper) + exp10 := frexp10Many(lower, f, upper) // Take a safety margin due to rounding in frexp10Many, but we lose precision. upper.mant++ lower.mant-- @@ -424,10 +590,12 @@ func (f *extFloat) ShortestDecimal(d *decimal, lower, upper *extFloat) bool { // Count integral digits: there are at most 10. var integerDigits int - for i, pow := range uint64pow10 { - if uint64(integer) >= pow { - integerDigits = i + 1 + for i, pow := 0, uint64(1); i < 20; i++ { + if pow > uint64(integer) { + integerDigits = i + break } + pow *= 10 } for i := 0; i < integerDigits; i++ { pow := uint64pow10[integerDigits-i-1] @@ -471,11 +639,11 @@ func (f *extFloat) ShortestDecimal(d *decimal, lower, upper *extFloat) bool { return false } -// adjustLastDigit modifies d = x-currentDiff*ε, to get closest to +// adjustLastDigit modifies d = x-currentDiff*ε, to get closest to // d = x-targetDiff*ε, without becoming smaller than x-maxDiff*ε. // It assumes that a decimal digit is worth ulpDecimal*ε, and that // all data is known with a error estimate of ulpBinary*ε. -func adjustLastDigit(d *decimal, currentDiff, targetDiff, maxDiff, ulpDecimal, ulpBinary uint64) bool { +func adjustLastDigit(d *decimalSlice, currentDiff, targetDiff, maxDiff, ulpDecimal, ulpBinary uint64) bool { if ulpDecimal < 2*ulpBinary { // Approximation is too wide. return false