github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/strconv/atof.go (about)

     1  // Copyright 2009 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  // decimal to binary floating point conversion.
     8  // Algorithm:
     9  //   1) Store input in multiprecision decimal.
    10  //   2) Multiply/divide decimal by powers of two until in range [0.5, 1)
    11  //   3) Multiply by 2^precision and round to get mantissa.
    12  
    13  import "math"
    14  import "runtime"
    15  
    16  var optimize = true // set to false to force slow-path conversions for testing
    17  
    18  func equalIgnoreCase(s1, s2 string) bool {
    19  	if len(s1) != len(s2) {
    20  		return false
    21  	}
    22  	for i := 0; i < len(s1); i++ {
    23  		c1 := s1[i]
    24  		if 'A' <= c1 && c1 <= 'Z' {
    25  			c1 += 'a' - 'A'
    26  		}
    27  		c2 := s2[i]
    28  		if 'A' <= c2 && c2 <= 'Z' {
    29  			c2 += 'a' - 'A'
    30  		}
    31  		if c1 != c2 {
    32  			return false
    33  		}
    34  	}
    35  	return true
    36  }
    37  
    38  func special(s string) (f float64, ok bool) {
    39  	if len(s) == 0 {
    40  		return
    41  	}
    42  	switch s[0] {
    43  	default:
    44  		return
    45  	case '+':
    46  		if equalIgnoreCase(s, "+inf") || equalIgnoreCase(s, "+infinity") {
    47  			return math.Inf(1), true
    48  		}
    49  	case '-':
    50  		if equalIgnoreCase(s, "-inf") || equalIgnoreCase(s, "-infinity") {
    51  			return math.Inf(-1), true
    52  		}
    53  	case 'n', 'N':
    54  		if equalIgnoreCase(s, "nan") {
    55  			return math.NaN(), true
    56  		}
    57  	case 'i', 'I':
    58  		if equalIgnoreCase(s, "inf") || equalIgnoreCase(s, "infinity") {
    59  			return math.Inf(1), true
    60  		}
    61  	}
    62  	return
    63  }
    64  
    65  func (b *decimal) set(s string) (ok bool) {
    66  	i := 0
    67  	b.neg = false
    68  	b.trunc = false
    69  
    70  	// optional sign
    71  	if i >= len(s) {
    72  		return
    73  	}
    74  	switch {
    75  	case s[i] == '+':
    76  		i++
    77  	case s[i] == '-':
    78  		b.neg = true
    79  		i++
    80  	}
    81  
    82  	// digits
    83  	sawdot := false
    84  	sawdigits := false
    85  	for ; i < len(s); i++ {
    86  		switch {
    87  		case s[i] == '_':
    88  			// readFloat already checked underscores
    89  			continue
    90  		case s[i] == '.':
    91  			if sawdot {
    92  				return
    93  			}
    94  			sawdot = true
    95  			b.dp = b.nd
    96  			continue
    97  
    98  		case '0' <= s[i] && s[i] <= '9':
    99  			sawdigits = true
   100  			if s[i] == '0' && b.nd == 0 { // ignore leading zeros
   101  				b.dp--
   102  				continue
   103  			}
   104  			if b.nd < len(b.d) {
   105  				b.d[b.nd] = s[i]
   106  				b.nd++
   107  			} else if s[i] != '0' {
   108  				b.trunc = true
   109  			}
   110  			continue
   111  		}
   112  		break
   113  	}
   114  	if !sawdigits {
   115  		return
   116  	}
   117  	if !sawdot {
   118  		b.dp = b.nd
   119  	}
   120  
   121  	// optional exponent moves decimal point.
   122  	// if we read a very large, very long number,
   123  	// just be sure to move the decimal point by
   124  	// a lot (say, 100000).  it doesn't matter if it's
   125  	// not the exact number.
   126  	if i < len(s) && lower(s[i]) == 'e' {
   127  		i++
   128  		if i >= len(s) {
   129  			return
   130  		}
   131  		esign := 1
   132  		if s[i] == '+' {
   133  			i++
   134  		} else if s[i] == '-' {
   135  			i++
   136  			esign = -1
   137  		}
   138  		if i >= len(s) || s[i] < '0' || s[i] > '9' {
   139  			return
   140  		}
   141  		e := 0
   142  		for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
   143  			if s[i] == '_' {
   144  				// readFloat already checked underscores
   145  				continue
   146  			}
   147  			if e < 10000 {
   148  				e = e*10 + int(s[i]) - '0'
   149  			}
   150  		}
   151  		b.dp += e * esign
   152  	}
   153  
   154  	if i != len(s) {
   155  		return
   156  	}
   157  
   158  	ok = true
   159  	return
   160  }
   161  
   162  // readFloat reads a decimal mantissa and exponent from a float
   163  // string representation. It returns ok==false if the number
   164  // is invalid.
   165  func readFloat(s string) (mantissa uint64, exp int, neg, trunc, hex, ok bool) {
   166  	i := 0
   167  	underscores := false
   168  
   169  	// optional sign
   170  	if i >= len(s) {
   171  		return
   172  	}
   173  	switch {
   174  	case s[i] == '+':
   175  		i++
   176  	case s[i] == '-':
   177  		neg = true
   178  		i++
   179  	}
   180  
   181  	// digits
   182  	base := uint64(10)
   183  	maxMantDigits := 19 // 10^19 fits in uint64
   184  	expChar := byte('e')
   185  	if i+2 < len(s) && s[i] == '0' && lower(s[i+1]) == 'x' {
   186  		base = 16
   187  		maxMantDigits = 16 // 16^16 fits in uint64
   188  		i += 2
   189  		expChar = 'p'
   190  		hex = true
   191  	}
   192  	sawdot := false
   193  	sawdigits := false
   194  	nd := 0
   195  	ndMant := 0
   196  	dp := 0
   197  	for ; i < len(s); i++ {
   198  		switch c := s[i]; true {
   199  		case c == '_':
   200  			underscores = true
   201  			continue
   202  
   203  		case c == '.':
   204  			if sawdot {
   205  				return
   206  			}
   207  			sawdot = true
   208  			dp = nd
   209  			continue
   210  
   211  		case '0' <= c && c <= '9':
   212  			sawdigits = true
   213  			if c == '0' && nd == 0 { // ignore leading zeros
   214  				dp--
   215  				continue
   216  			}
   217  			nd++
   218  			if ndMant < maxMantDigits {
   219  				mantissa *= base
   220  				mantissa += uint64(c - '0')
   221  				ndMant++
   222  			} else if c != '0' {
   223  				trunc = true
   224  			}
   225  			continue
   226  
   227  		case base == 16 && 'a' <= lower(c) && lower(c) <= 'f':
   228  			sawdigits = true
   229  			nd++
   230  			if ndMant < maxMantDigits {
   231  				mantissa *= 16
   232  				mantissa += uint64(lower(c) - 'a' + 10)
   233  				ndMant++
   234  			} else {
   235  				trunc = true
   236  			}
   237  			continue
   238  		}
   239  		break
   240  	}
   241  	if !sawdigits {
   242  		return
   243  	}
   244  	if !sawdot {
   245  		dp = nd
   246  	}
   247  
   248  	if base == 16 {
   249  		dp *= 4
   250  		ndMant *= 4
   251  	}
   252  
   253  	// optional exponent moves decimal point.
   254  	// if we read a very large, very long number,
   255  	// just be sure to move the decimal point by
   256  	// a lot (say, 100000).  it doesn't matter if it's
   257  	// not the exact number.
   258  	if i < len(s) && lower(s[i]) == expChar {
   259  		i++
   260  		if i >= len(s) {
   261  			return
   262  		}
   263  		esign := 1
   264  		if s[i] == '+' {
   265  			i++
   266  		} else if s[i] == '-' {
   267  			i++
   268  			esign = -1
   269  		}
   270  		if i >= len(s) || s[i] < '0' || s[i] > '9' {
   271  			return
   272  		}
   273  		e := 0
   274  		for ; i < len(s) && ('0' <= s[i] && s[i] <= '9' || s[i] == '_'); i++ {
   275  			if s[i] == '_' {
   276  				underscores = true
   277  				continue
   278  			}
   279  			if e < 10000 {
   280  				e = e*10 + int(s[i]) - '0'
   281  			}
   282  		}
   283  		dp += e * esign
   284  	} else if base == 16 {
   285  		// Must have exponent.
   286  		return
   287  	}
   288  
   289  	if i != len(s) {
   290  		return
   291  	}
   292  
   293  	if mantissa != 0 {
   294  		exp = dp - ndMant
   295  	}
   296  
   297  	if underscores && !underscoreOK(s) {
   298  		return
   299  	}
   300  
   301  	ok = true
   302  	return
   303  }
   304  
   305  // decimal power of ten to binary power of two.
   306  var powtab = []int{1, 3, 6, 9, 13, 16, 19, 23, 26}
   307  
   308  func (d *decimal) floatBits(flt *floatInfo) (b uint64, overflow bool) {
   309  	var exp int
   310  	var mant uint64
   311  
   312  	// Zero is always a special case.
   313  	if d.nd == 0 {
   314  		mant = 0
   315  		exp = flt.bias
   316  		goto out
   317  	}
   318  
   319  	// Obvious overflow/underflow.
   320  	// These bounds are for 64-bit floats.
   321  	// Will have to change if we want to support 80-bit floats in the future.
   322  	if d.dp > 310 {
   323  		goto overflow
   324  	}
   325  	if d.dp < -330 {
   326  		// zero
   327  		mant = 0
   328  		exp = flt.bias
   329  		goto out
   330  	}
   331  
   332  	// Scale by powers of two until in range [0.5, 1.0)
   333  	exp = 0
   334  	for d.dp > 0 {
   335  		var n int
   336  		if d.dp >= len(powtab) {
   337  			n = 27
   338  		} else {
   339  			n = powtab[d.dp]
   340  		}
   341  		d.Shift(-n)
   342  		exp += n
   343  	}
   344  	for d.dp < 0 || d.dp == 0 && d.d[0] < '5' {
   345  		var n int
   346  		if -d.dp >= len(powtab) {
   347  			n = 27
   348  		} else {
   349  			n = powtab[-d.dp]
   350  		}
   351  		d.Shift(n)
   352  		exp -= n
   353  	}
   354  
   355  	// Our range is [0.5,1) but floating point range is [1,2).
   356  	exp--
   357  
   358  	// Minimum representable exponent is flt.bias+1.
   359  	// If the exponent is smaller, move it up and
   360  	// adjust d accordingly.
   361  	if exp < flt.bias+1 {
   362  		n := flt.bias + 1 - exp
   363  		d.Shift(-n)
   364  		exp += n
   365  	}
   366  
   367  	if exp-flt.bias >= 1<<flt.expbits-1 {
   368  		goto overflow
   369  	}
   370  
   371  	// Extract 1+flt.mantbits bits.
   372  	d.Shift(int(1 + flt.mantbits))
   373  	mant = d.RoundedInteger()
   374  
   375  	// Rounding might have added a bit; shift down.
   376  	if mant == 2<<flt.mantbits {
   377  		mant >>= 1
   378  		exp++
   379  		if exp-flt.bias >= 1<<flt.expbits-1 {
   380  			goto overflow
   381  		}
   382  	}
   383  
   384  	// Denormalized?
   385  	if mant&(1<<flt.mantbits) == 0 {
   386  		exp = flt.bias
   387  	}
   388  	goto out
   389  
   390  overflow:
   391  	// ±Inf
   392  	mant = 0
   393  	exp = 1<<flt.expbits - 1 + flt.bias
   394  	overflow = true
   395  
   396  out:
   397  	// Assemble bits.
   398  	bits := mant & (uint64(1)<<flt.mantbits - 1)
   399  	bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
   400  	if d.neg {
   401  		bits |= 1 << flt.mantbits << flt.expbits
   402  	}
   403  	return bits, overflow
   404  }
   405  
   406  // Exact powers of 10.
   407  var float64pow10 = []float64{
   408  	1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
   409  	1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
   410  	1e20, 1e21, 1e22,
   411  }
   412  var float32pow10 = []float32{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10}
   413  
   414  // If possible to convert decimal representation to 64-bit float f exactly,
   415  // entirely in floating-point math, do so, avoiding the expense of decimalToFloatBits.
   416  // Three common cases:
   417  //	value is exact integer
   418  //	value is exact integer * exact power of ten
   419  //	value is exact integer / exact power of ten
   420  // These all produce potentially inexact but correctly rounded answers.
   421  func atof64exact(mantissa uint64, exp int, neg bool) (f float64, ok bool) {
   422  	if mantissa>>float64info.mantbits != 0 {
   423  		return
   424  	}
   425  	// gccgo gets this wrong on 32-bit i386 when not using -msse.
   426  	// See TestRoundTrip in atof_test.go for a test case.
   427  	if runtime.GOARCH == "386" {
   428  		return
   429  	}
   430  	f = float64(mantissa)
   431  	if neg {
   432  		f = -f
   433  	}
   434  	switch {
   435  	case exp == 0:
   436  		// an integer.
   437  		return f, true
   438  	// Exact integers are <= 10^15.
   439  	// Exact powers of ten are <= 10^22.
   440  	case exp > 0 && exp <= 15+22: // int * 10^k
   441  		// If exponent is big but number of digits is not,
   442  		// can move a few zeros into the integer part.
   443  		if exp > 22 {
   444  			f *= float64pow10[exp-22]
   445  			exp = 22
   446  		}
   447  		if f > 1e15 || f < -1e15 {
   448  			// the exponent was really too large.
   449  			return
   450  		}
   451  		return f * float64pow10[exp], true
   452  	case exp < 0 && exp >= -22: // int / 10^k
   453  		return f / float64pow10[-exp], true
   454  	}
   455  	return
   456  }
   457  
   458  // If possible to compute mantissa*10^exp to 32-bit float f exactly,
   459  // entirely in floating-point math, do so, avoiding the machinery above.
   460  func atof32exact(mantissa uint64, exp int, neg bool) (f float32, ok bool) {
   461  	if mantissa>>float32info.mantbits != 0 {
   462  		return
   463  	}
   464  	f = float32(mantissa)
   465  	if neg {
   466  		f = -f
   467  	}
   468  	switch {
   469  	case exp == 0:
   470  		return f, true
   471  	// Exact integers are <= 10^7.
   472  	// Exact powers of ten are <= 10^10.
   473  	case exp > 0 && exp <= 7+10: // int * 10^k
   474  		// If exponent is big but number of digits is not,
   475  		// can move a few zeros into the integer part.
   476  		if exp > 10 {
   477  			f *= float32pow10[exp-10]
   478  			exp = 10
   479  		}
   480  		if f > 1e7 || f < -1e7 {
   481  			// the exponent was really too large.
   482  			return
   483  		}
   484  		return f * float32pow10[exp], true
   485  	case exp < 0 && exp >= -10: // int / 10^k
   486  		return f / float32pow10[-exp], true
   487  	}
   488  	return
   489  }
   490  
   491  // atofHex converts the hex floating-point string s
   492  // to a rounded float32 or float64 value (depending on flt==&float32info or flt==&float64info)
   493  // and returns it as a float64.
   494  // The string s has already been parsed into a mantissa, exponent, and sign (neg==true for negative).
   495  // If trunc is true, trailing non-zero bits have been omitted from the mantissa.
   496  func atofHex(s string, flt *floatInfo, mantissa uint64, exp int, neg, trunc bool) (float64, error) {
   497  	maxExp := 1<<flt.expbits + flt.bias - 2
   498  	minExp := flt.bias + 1
   499  	exp += int(flt.mantbits) // mantissa now implicitly divided by 2^mantbits.
   500  
   501  	// Shift mantissa and exponent to bring representation into float range.
   502  	// Eventually we want a mantissa with a leading 1-bit followed by mantbits other bits.
   503  	// For rounding, we need two more, where the bottom bit represents
   504  	// whether that bit or any later bit was non-zero.
   505  	// (If the mantissa has already lost non-zero bits, trunc is true,
   506  	// and we OR in a 1 below after shifting left appropriately.)
   507  	for mantissa != 0 && mantissa>>(flt.mantbits+2) == 0 {
   508  		mantissa <<= 1
   509  		exp--
   510  	}
   511  	if trunc {
   512  		mantissa |= 1
   513  	}
   514  	for mantissa>>(1+flt.mantbits+2) != 0 {
   515  		mantissa = mantissa>>1 | mantissa&1
   516  		exp++
   517  	}
   518  
   519  	// If exponent is too negative,
   520  	// denormalize in hopes of making it representable.
   521  	// (The -2 is for the rounding bits.)
   522  	for mantissa > 1 && exp < minExp-2 {
   523  		mantissa = mantissa>>1 | mantissa&1
   524  		exp++
   525  	}
   526  
   527  	// Round using two bottom bits.
   528  	round := mantissa & 3
   529  	mantissa >>= 2
   530  	round |= mantissa & 1 // round to even (round up if mantissa is odd)
   531  	exp += 2
   532  	if round == 3 {
   533  		mantissa++
   534  		if mantissa == 1<<(1+flt.mantbits) {
   535  			mantissa >>= 1
   536  			exp++
   537  		}
   538  	}
   539  
   540  	if mantissa>>flt.mantbits == 0 { // Denormal or zero.
   541  		exp = flt.bias
   542  	}
   543  	var err error
   544  	if exp > maxExp { // infinity and range error
   545  		mantissa = 1 << flt.mantbits
   546  		exp = maxExp + 1
   547  		err = rangeError(fnParseFloat, s)
   548  	}
   549  
   550  	bits := mantissa & (1<<flt.mantbits - 1)
   551  	bits |= uint64((exp-flt.bias)&(1<<flt.expbits-1)) << flt.mantbits
   552  	if neg {
   553  		bits |= 1 << flt.mantbits << flt.expbits
   554  	}
   555  	if flt == &float32info {
   556  		return float64(math.Float32frombits(uint32(bits))), err
   557  	}
   558  	return math.Float64frombits(bits), err
   559  }
   560  
   561  const fnParseFloat = "ParseFloat"
   562  
   563  func atof32(s string) (f float32, err error) {
   564  	if val, ok := special(s); ok {
   565  		return float32(val), nil
   566  	}
   567  
   568  	mantissa, exp, neg, trunc, hex, ok := readFloat(s)
   569  	if !ok {
   570  		return 0, syntaxError(fnParseFloat, s)
   571  	}
   572  
   573  	if hex {
   574  		f, err := atofHex(s, &float32info, mantissa, exp, neg, trunc)
   575  		return float32(f), err
   576  	}
   577  
   578  	if optimize {
   579  		// Try pure floating-point arithmetic conversion.
   580  		if !trunc {
   581  			if f, ok := atof32exact(mantissa, exp, neg); ok {
   582  				return f, nil
   583  			}
   584  		}
   585  		// Try another fast path.
   586  		ext := new(extFloat)
   587  		if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float32info); ok {
   588  			b, ovf := ext.floatBits(&float32info)
   589  			f = math.Float32frombits(uint32(b))
   590  			if ovf {
   591  				err = rangeError(fnParseFloat, s)
   592  			}
   593  			return f, err
   594  		}
   595  	}
   596  
   597  	// Slow fallback.
   598  	var d decimal
   599  	if !d.set(s) {
   600  		return 0, syntaxError(fnParseFloat, s)
   601  	}
   602  	b, ovf := d.floatBits(&float32info)
   603  	f = math.Float32frombits(uint32(b))
   604  	if ovf {
   605  		err = rangeError(fnParseFloat, s)
   606  	}
   607  	return f, err
   608  }
   609  
   610  func atof64(s string) (f float64, err error) {
   611  	if val, ok := special(s); ok {
   612  		return val, nil
   613  	}
   614  
   615  	mantissa, exp, neg, trunc, hex, ok := readFloat(s)
   616  	if !ok {
   617  		return 0, syntaxError(fnParseFloat, s)
   618  	}
   619  
   620  	if hex {
   621  		return atofHex(s, &float64info, mantissa, exp, neg, trunc)
   622  	}
   623  
   624  	if optimize {
   625  		// Try pure floating-point arithmetic conversion.
   626  		if !trunc {
   627  			if f, ok := atof64exact(mantissa, exp, neg); ok {
   628  				return f, nil
   629  			}
   630  		}
   631  		// Try another fast path.
   632  		ext := new(extFloat)
   633  		if ok := ext.AssignDecimal(mantissa, exp, neg, trunc, &float64info); ok {
   634  			b, ovf := ext.floatBits(&float64info)
   635  			f = math.Float64frombits(b)
   636  			if ovf {
   637  				err = rangeError(fnParseFloat, s)
   638  			}
   639  			return f, err
   640  		}
   641  	}
   642  
   643  	// Slow fallback.
   644  	var d decimal
   645  	if !d.set(s) {
   646  		return 0, syntaxError(fnParseFloat, s)
   647  	}
   648  	b, ovf := d.floatBits(&float64info)
   649  	f = math.Float64frombits(b)
   650  	if ovf {
   651  		err = rangeError(fnParseFloat, s)
   652  	}
   653  	return f, err
   654  }
   655  
   656  // ParseFloat converts the string s to a floating-point number
   657  // with the precision specified by bitSize: 32 for float32, or 64 for float64.
   658  // When bitSize=32, the result still has type float64, but it will be
   659  // convertible to float32 without changing its value.
   660  //
   661  // ParseFloat accepts decimal and hexadecimal floating-point number syntax.
   662  // If s is well-formed and near a valid floating-point number,
   663  // ParseFloat returns the nearest floating-point number rounded
   664  // using IEEE754 unbiased rounding.
   665  // (Parsing a hexadecimal floating-point value only rounds when
   666  // there are more bits in the hexadecimal representation than
   667  // will fit in the mantissa.)
   668  //
   669  // The errors that ParseFloat returns have concrete type *NumError
   670  // and include err.Num = s.
   671  //
   672  // If s is not syntactically well-formed, ParseFloat returns err.Err = ErrSyntax.
   673  //
   674  // If s is syntactically well-formed but is more than 1/2 ULP
   675  // away from the largest floating point number of the given size,
   676  // ParseFloat returns f = ±Inf, err.Err = ErrRange.
   677  //
   678  // ParseFloat recognizes the strings "NaN", "+Inf", and "-Inf" as their
   679  // respective special floating point values. It ignores case when matching.
   680  func ParseFloat(s string, bitSize int) (float64, error) {
   681  	if bitSize == 32 {
   682  		f, err := atof32(s)
   683  		return float64(f), err
   684  	}
   685  	return atof64(s)
   686  }