github.com/rsc/tmp@v0.0.0-20240517235954-6deaab19748b/bootstrap/internal/gc/mparith3.go (about)

     1  // Do not edit. Bootstrap copy of /Users/rsc/g/go/src/cmd/internal/gc/mparith3.go
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package gc
     8  
     9  import (
    10  	"rsc.io/tmp/bootstrap/internal/gc/big"
    11  	"rsc.io/tmp/bootstrap/internal/obj"
    12  	"fmt"
    13  	"math"
    14  )
    15  
    16  /// implements float arihmetic
    17  
    18  func newMpflt() *Mpflt {
    19  	var a Mpflt
    20  	a.Val.SetPrec(Mpprec)
    21  	return &a
    22  }
    23  
    24  func Mpmovefixflt(a *Mpflt, b *Mpint) {
    25  	if b.Ovf {
    26  		// sign doesn't really matter but copy anyway
    27  		a.Val.SetInf(b.Val.Sign() < 0)
    28  		return
    29  	}
    30  	a.Val.SetInt(&b.Val)
    31  }
    32  
    33  func mpmovefltflt(a *Mpflt, b *Mpflt) {
    34  	a.Val.Set(&b.Val)
    35  }
    36  
    37  func mpaddfltflt(a *Mpflt, b *Mpflt) {
    38  	if Mpdebug {
    39  		fmt.Printf("\n%v + %v", a, b)
    40  	}
    41  
    42  	a.Val.Add(&a.Val, &b.Val)
    43  
    44  	if Mpdebug {
    45  		fmt.Printf(" = %v\n\n", a)
    46  	}
    47  }
    48  
    49  func mpaddcflt(a *Mpflt, c float64) {
    50  	var b Mpflt
    51  
    52  	Mpmovecflt(&b, c)
    53  	mpaddfltflt(a, &b)
    54  }
    55  
    56  func mpsubfltflt(a *Mpflt, b *Mpflt) {
    57  	if Mpdebug {
    58  		fmt.Printf("\n%v - %v", a, b)
    59  	}
    60  
    61  	a.Val.Sub(&a.Val, &b.Val)
    62  
    63  	if Mpdebug {
    64  		fmt.Printf(" = %v\n\n", a)
    65  	}
    66  }
    67  
    68  func mpmulfltflt(a *Mpflt, b *Mpflt) {
    69  	if Mpdebug {
    70  		fmt.Printf("%v\n * %v\n", a, b)
    71  	}
    72  
    73  	a.Val.Mul(&a.Val, &b.Val)
    74  
    75  	if Mpdebug {
    76  		fmt.Printf(" = %v\n\n", a)
    77  	}
    78  }
    79  
    80  func mpmulcflt(a *Mpflt, c float64) {
    81  	var b Mpflt
    82  
    83  	Mpmovecflt(&b, c)
    84  	mpmulfltflt(a, &b)
    85  }
    86  
    87  func mpdivfltflt(a *Mpflt, b *Mpflt) {
    88  	if Mpdebug {
    89  		fmt.Printf("%v\n / %v\n", a, b)
    90  	}
    91  
    92  	a.Val.Quo(&a.Val, &b.Val)
    93  
    94  	if Mpdebug {
    95  		fmt.Printf(" = %v\n\n", a)
    96  	}
    97  }
    98  
    99  func mpcmpfltflt(a *Mpflt, b *Mpflt) int {
   100  	return a.Val.Cmp(&b.Val)
   101  }
   102  
   103  func mpcmpfltc(b *Mpflt, c float64) int {
   104  	var a Mpflt
   105  
   106  	Mpmovecflt(&a, c)
   107  	return mpcmpfltflt(b, &a)
   108  }
   109  
   110  func mpgetfltN(a *Mpflt, prec int, bias int) float64 {
   111  	var x float64
   112  	switch prec {
   113  	case 53:
   114  		x, _ = a.Val.Float64()
   115  	case 24:
   116  		// We should be using a.Val.Float32() here but that seems incorrect
   117  		// for certain denormal values (all.bash fails). The current code
   118  		// appears to work for all existing test cases, though there ought
   119  		// to be issues with denormal numbers that are incorrectly rounded.
   120  		// TODO(gri) replace with a.Val.Float32() once correctly working
   121  		// See also: https://github.com/golang/go/issues/10321
   122  		var t Mpflt
   123  		t.Val.SetPrec(24).Set(&a.Val)
   124  		x, _ = t.Val.Float64()
   125  	default:
   126  		panic("unreachable")
   127  	}
   128  
   129  	// check for overflow
   130  	if math.IsInf(x, 0) && nsavederrors+nerrors == 0 {
   131  		Yyerror("mpgetflt ovf")
   132  	}
   133  
   134  	return x
   135  }
   136  
   137  func mpgetflt(a *Mpflt) float64 {
   138  	return mpgetfltN(a, 53, -1023)
   139  }
   140  
   141  func mpgetflt32(a *Mpflt) float64 {
   142  	return mpgetfltN(a, 24, -127)
   143  }
   144  
   145  func Mpmovecflt(a *Mpflt, c float64) {
   146  	if Mpdebug {
   147  		fmt.Printf("\nconst %g", c)
   148  	}
   149  
   150  	a.Val.SetFloat64(c)
   151  
   152  	if Mpdebug {
   153  		fmt.Printf(" = %v\n", a)
   154  	}
   155  }
   156  
   157  func mpnegflt(a *Mpflt) {
   158  	a.Val.Neg(&a.Val)
   159  }
   160  
   161  //
   162  // floating point input
   163  // required syntax is [+-]d*[.]d*[e[+-]d*] or [+-]0xH*[e[+-]d*]
   164  //
   165  func mpatoflt(a *Mpflt, as string) {
   166  	for len(as) > 0 && (as[0] == ' ' || as[0] == '\t') {
   167  		as = as[1:]
   168  	}
   169  
   170  	f, ok := a.Val.SetString(as)
   171  	if !ok {
   172  		// At the moment we lose precise error cause;
   173  		// the old code additionally distinguished between:
   174  		// - malformed hex constant
   175  		// - decimal point in hex constant
   176  		// - constant exponent out of range
   177  		// - decimal point and binary point in constant
   178  		// TODO(gri) use different conversion function or check separately
   179  		Yyerror("malformed constant: %s", as)
   180  		a.Val.SetUint64(0)
   181  	}
   182  
   183  	if f.IsInf() {
   184  		Yyerror("constant too large: %s", as)
   185  		a.Val.SetUint64(0)
   186  	}
   187  }
   188  
   189  func (f *Mpflt) String() string {
   190  	return Fconv(f, 0)
   191  }
   192  
   193  func Fconv(fvp *Mpflt, flag int) string {
   194  	if flag&obj.FmtSharp == 0 {
   195  		return fvp.Val.Format('b', 0)
   196  	}
   197  
   198  	// use decimal format for error messages
   199  
   200  	// determine sign
   201  	f := &fvp.Val
   202  	var sign string
   203  	if fvp.Val.Signbit() {
   204  		sign = "-"
   205  		f = new(big.Float).Abs(f)
   206  	} else if flag&obj.FmtSign != 0 {
   207  		sign = "+"
   208  	}
   209  
   210  	// Use fmt formatting if in float64 range (common case).
   211  	if x, _ := f.Float64(); !math.IsInf(x, 0) {
   212  		return fmt.Sprintf("%s%.6g", sign, x)
   213  	}
   214  
   215  	// Out of float64 range. Do approximate manual to decimal
   216  	// conversion to avoid precise but possibly slow Float
   217  	// formatting. The exponent is > 0 since a negative out-
   218  	// of-range exponent would have underflowed and led to 0.
   219  	// f = mant * 2**exp
   220  	var mant big.Float
   221  	exp := float64(f.MantExp(&mant)) // 0.5 <= mant < 1.0, exp > 0
   222  
   223  	// approximate float64 mantissa m and decimal exponent d
   224  	// f ~ m * 10**d
   225  	m, _ := mant.Float64()            // 0.5 <= m < 1.0
   226  	d := exp * (math.Ln2 / math.Ln10) // log_10(2)
   227  
   228  	// adjust m for truncated (integer) decimal exponent e
   229  	e := int64(d)
   230  	m *= math.Pow(10, d-float64(e))
   231  	for m >= 10 {
   232  		m /= 10
   233  		e++
   234  	}
   235  
   236  	return fmt.Sprintf("%s%.5fe+%d", sign, m, e)
   237  }