]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compiler/internal/gc: remove flag argument from fconv (cleanup)
authorRobert Griesemer <gri@golang.org>
Wed, 19 Sep 2018 03:50:04 +0000 (20:50 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 20 Sep 2018 00:06:44 +0000 (00:06 +0000)
The fconv flag arguments were 0, FmtSharp, and FmtSharp|FmtSign.
The 0 value was used for binary representation only, which was
readily available via Mpflt.String. Otherwise, FmtSharp was always
passed. FmtSign was used to print the '+' sign in case of a positive
number and only needed for complex number formatting. Instead
implemented cconv and handled it there.

Change-Id: I1f77282f995be9cfda05efb71a0e027836a9da26
Reviewed-on: https://go-review.googlesource.com/136195
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/const.go
src/cmd/compile/internal/gc/fmt.go
src/cmd/compile/internal/gc/mpfloat.go

index 1403a2be11edfa66cf42871e55a8847bca6cfaf4..d87c4980d06d5dd52fb455dbf391b80a443bcee3 100644 (file)
@@ -476,7 +476,7 @@ func toflt(v Val) Val {
                f := newMpflt()
                f.Set(&u.Real)
                if u.Imag.CmpFloat64(0) != 0 {
-                       yyerror("constant %v%vi truncated to real", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
+                       yyerror("constant %v truncated to real", cconv(u))
                }
                v.U = f
        }
@@ -509,11 +509,11 @@ func toint(v Val) Val {
                                // value from the error message.
                                // (See issue #11371).
                                var t big.Float
-                               t.Parse(fconv(u, FmtSharp), 10)
+                               t.Parse(fconv(u), 10)
                                if t.IsInt() {
                                        yyerror("constant truncated to integer")
                                } else {
-                                       yyerror("constant %v truncated to integer", fconv(u, FmtSharp))
+                                       yyerror("constant %v truncated to integer", fconv(u))
                                }
                        }
                }
@@ -522,7 +522,7 @@ func toint(v Val) Val {
        case *Mpcplx:
                i := new(Mpint)
                if !i.SetFloat(&u.Real) || u.Imag.CmpFloat64(0) != 0 {
-                       yyerror("constant %v%vi truncated to integer", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
+                       yyerror("constant %v truncated to integer", cconv(u))
                }
 
                v.U = i
index 5b7445d4dbadee02e7d95386c4630f1954dcfcfc..be8a7ef6f58da7be8e0675ccfecfe17c02befabe 100644 (file)
@@ -537,10 +537,10 @@ func (v Val) vconv(s fmt.State, flag FmtFlag) {
 
        case *Mpflt:
                if flag&FmtSharp != 0 {
-                       fmt.Fprint(s, fconv(u, 0))
+                       fmt.Fprint(s, u.String())
                        return
                }
-               fmt.Fprint(s, fconv(u, FmtSharp))
+               fmt.Fprint(s, fconv(u))
                return
 
        case *Mpcplx:
@@ -549,16 +549,13 @@ func (v Val) vconv(s fmt.State, flag FmtFlag) {
                        fmt.Fprintf(s, "(%v+%vi)", &u.Real, &u.Imag)
 
                case v.U.(*Mpcplx).Real.CmpFloat64(0) == 0:
-                       fmt.Fprintf(s, "%vi", fconv(&u.Imag, FmtSharp))
+                       fmt.Fprintf(s, "%vi", fconv(&u.Imag))
 
                case v.U.(*Mpcplx).Imag.CmpFloat64(0) == 0:
-                       fmt.Fprint(s, fconv(&u.Real, FmtSharp))
-
-               case v.U.(*Mpcplx).Imag.CmpFloat64(0) < 0:
-                       fmt.Fprintf(s, "(%v%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
+                       fmt.Fprint(s, fconv(&u.Real))
 
                default:
-                       fmt.Fprintf(s, "(%v+%vi)", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp))
+                       fmt.Fprintf(s, "(%v)", cconv(u))
                }
 
        case string:
index 5977ef9748e8a4cd65ef7b28340385205fb94f1b..8837628d86648f8771c0ba8b2d6a701dba17b85d 100644 (file)
@@ -201,24 +201,16 @@ func (a *Mpflt) SetString(as string) {
 }
 
 func (f *Mpflt) String() string {
-       return fconv(f, 0)
+       return f.Val.Text('b', 0)
 }
 
-func fconv(fvp *Mpflt, flag FmtFlag) string {
-       if flag&FmtSharp == 0 {
-               return fvp.Val.Text('b', 0)
-       }
-
-       // use decimal format for error messages
-
+func fconv(fvp *Mpflt) string {
        // determine sign
+       sign := ""
        f := &fvp.Val
-       var sign string
        if f.Sign() < 0 {
                sign = "-"
                f = new(big.Float).Abs(f)
-       } else if flag&FmtSign != 0 {
-               sign = "+"
        }
 
        // Don't try to convert infinities (will not terminate).
@@ -334,3 +326,12 @@ func (v *Mpcplx) Div(rv *Mpcplx) bool {
 
        return true
 }
+
+func cconv(v *Mpcplx) string {
+       re := fconv(&v.Real)
+       im := fconv(&v.Imag)
+       if im[0] == '-' {
+               return re + im + "i"
+       }
+       return re + "+" + im + "i"
+}