]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/compile: enable rational constant arithmetic
authorMatthew Dempsky <mdempsky@google.com>
Mon, 25 Jan 2021 07:39:16 +0000 (23:39 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 25 Jan 2021 18:53:24 +0000 (18:53 +0000)
This allows more precision and matches types2's behavior.

For backwards compatibility with gcimporter, for now we still need to
write out declared constants as limited-precision floating-point
values. To ensure consistent behavior of constant arithmetic whether
it spans package boundaries or not, we include the full-precision
rational representation in the compiler's extension section of the
export data.

Also, this CL simply uses the math/big.Rat.String text representation
as the encoding. This is inefficient, but because it's only in the
compiler's extension section, we can easily revisit this in the
future.

Declaring exported untyped float and complex constants isn't very
common anyway. Within the standard library, only package math declares
any at all, containing just 15. And those 15 are only imported a total
of 12 times elsewhere in the standard library.

Change-Id: I85ea23ab712e93fd3b68e52d60cbedce9be696a0
Reviewed-on: https://go-review.googlesource.com/c/go/+/286215
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Trust: Matthew Dempsky <mdempsky@google.com>
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/cmd/compile/internal/noder/noder.go
src/cmd/compile/internal/typecheck/iexport.go
src/cmd/compile/internal/typecheck/iimport.go
test/fixedbugs/issue7740.go
test/float_lit3.go

index 6aab18549aefc0f9fc2371d4f54e413644a56d08..5b5b09cb2df930a0055fa89fd3db023b3ef3b2b1 100644 (file)
@@ -1455,14 +1455,6 @@ func (p *noder) basicLit(lit *syntax.BasicLit) constant.Value {
                p.errorAt(lit.Pos(), "malformed constant: %s", lit.Value)
        }
 
-       // go/constant uses big.Rat by default, which is more precise, but
-       // causes toolstash -cmp and some tests to fail. For now, convert
-       // to big.Float to match cmd/compile's historical precision.
-       // TODO(mdempsky): Remove.
-       if v.Kind() == constant.Float {
-               v = constant.Make(ir.BigFloat(v))
-       }
-
        return v
 }
 
index be4a689836cec2b258b28b4ea37f4db91f79eb73..6fab74e61fe7762c0ef842f00753db63d5ad586c 100644 (file)
@@ -462,12 +462,16 @@ func (p *iexporter) doDecl(n *ir.Name) {
                }
 
        case ir.OLITERAL:
+               // TODO(mdempsky): Extend check to all declarations.
+               if n.Typecheck() == 0 {
+                       base.FatalfAt(n.Pos(), "missed typecheck: %v", n)
+               }
+
                // Constant.
-               // TODO(mdempsky): Do we still need this typecheck? If so, why?
-               n = Expr(n).(*ir.Name)
                w.tag('C')
                w.pos(n.Pos())
                w.value(n.Type(), n.Val())
+               w.constExt(n)
 
        case ir.OTYPE:
                if types.IsDotAlias(n.Sym()) {
@@ -956,6 +960,17 @@ func (w *exportWriter) mpfloat(v constant.Value, typ *types.Type) {
        }
 }
 
+func (w *exportWriter) mprat(v constant.Value) {
+       r, ok := constant.Val(v).(*big.Rat)
+       if !w.bool(ok) {
+               return
+       }
+       // TODO(mdempsky): Come up with a more efficient binary
+       // encoding before bumping iexportVersion to expose to
+       // gcimporter.
+       w.string(r.String())
+}
+
 func (w *exportWriter) bool(b bool) bool {
        var x uint64
        if b {
@@ -971,7 +986,37 @@ func (w *exportWriter) string(s string) { w.uint64(w.p.stringOff(s)) }
 
 // Compiler-specific extensions.
 
-func (w *exportWriter) varExt(n ir.Node) {
+func (w *exportWriter) constExt(n *ir.Name) {
+       // Internally, we now represent untyped float and complex
+       // constants with infinite-precision rational numbers using
+       // go/constant, but the "public" export data format known to
+       // gcimporter only supports 512-bit floating point constants.
+       // In case rationals turn out to be a bad idea and we want to
+       // switch back to fixed-precision constants, for now we
+       // continue writing out the 512-bit truncation in the public
+       // data section, and write the exact, rational constant in the
+       // compiler's extension data. Also, we only need to worry
+       // about exporting rationals for declared constants, because
+       // constants that appear in an expression will already have
+       // been coerced to a concrete, fixed-precision type.
+       //
+       // Eventually, assuming we stick with using rationals, we
+       // should bump iexportVersion to support rationals, and do the
+       // whole gcimporter update song-and-dance.
+       //
+       // TODO(mdempsky): Prepare vocals for that.
+
+       switch n.Type() {
+       case types.UntypedFloat:
+               w.mprat(n.Val())
+       case types.UntypedComplex:
+               v := n.Val()
+               w.mprat(constant.Real(v))
+               w.mprat(constant.Imag(v))
+       }
+}
+
+func (w *exportWriter) varExt(n *ir.Name) {
        w.linkname(n.Sym())
        w.symIdx(n.Sym())
 }
index f2682257f3456d9d641f8307d0a6c3af53b971e9..b73ef5176b9586ab367d18286350218b6adcfed3 100644 (file)
@@ -303,7 +303,9 @@ func (r *importReader) doDecl(sym *types.Sym) *ir.Name {
                typ := r.typ()
                val := r.value(typ)
 
-               return importconst(r.p.ipkg, pos, sym, typ, val)
+               n := importconst(r.p.ipkg, pos, sym, typ, val)
+               r.constExt(n)
+               return n
 
        case 'F':
                typ := r.signature(nil)
@@ -440,6 +442,15 @@ func (p *importReader) float(typ *types.Type) constant.Value {
        return constant.Make(&f)
 }
 
+func (p *importReader) mprat(orig constant.Value) constant.Value {
+       if !p.bool() {
+               return orig
+       }
+       var rat big.Rat
+       rat.SetString(p.string())
+       return constant.Make(&rat)
+}
+
 func (r *importReader) ident(selector bool) *types.Sym {
        name := r.string()
        if name == "" {
@@ -641,7 +652,19 @@ func (r *importReader) byte() byte {
 
 // Compiler-specific extensions.
 
-func (r *importReader) varExt(n ir.Node) {
+func (r *importReader) constExt(n *ir.Name) {
+       switch n.Type() {
+       case types.UntypedFloat:
+               n.SetVal(r.mprat(n.Val()))
+       case types.UntypedComplex:
+               v := n.Val()
+               re := r.mprat(constant.Real(v))
+               im := r.mprat(constant.Imag(v))
+               n.SetVal(makeComplex(re, im))
+       }
+}
+
+func (r *importReader) varExt(n *ir.Name) {
        r.linkname(n.Sym())
        r.symIdx(n.Sym())
 }
index 8f1afe86dae5ed1258190fdb1308522de238d2c7..6bc6249d7e0b6bfc597bade2f4d506d63814e5a1 100644 (file)
@@ -21,7 +21,7 @@ func main() {
        var prec float64
        switch runtime.Compiler {
        case "gc":
-               prec = 512
+               prec = math.Inf(1) // exact precision using rational arithmetic
        case "gccgo":
                prec = 256
        default:
index c4d1aa567cef2bf8c64f4c87ef8669a8c01160af..850d02c9c7f97366ae3d5db35ec9e78b5417290e 100644 (file)
@@ -37,12 +37,11 @@ var x = []interface{}{
 
        // If the compiler's internal floating point representation
        // is shorter than 1024 bits, it cannot distinguish max64+ulp64/2-1 and max64+ulp64/2.
-       // gc uses fewer than 1024 bits, so allow it to print the overflow error for the -1 case.
        float64(max64 + ulp64/2 - two1024/two256), // ok
-       float64(max64 + ulp64/2 - 1),              // GC_ERROR "constant 1\.79769e\+308 overflows float64"
+       float64(max64 + ulp64/2 - 1),              // ok
        float64(max64 + ulp64/2),                  // ERROR "constant 1\.79769e\+308 overflows float64"
 
        float64(-max64 - ulp64/2 + two1024/two256), // ok
-       float64(-max64 - ulp64/2 + 1),              // GC_ERROR "constant -1\.79769e\+308 overflows float64"
+       float64(-max64 - ulp64/2 + 1),              // ok
        float64(-max64 - ulp64/2),                  // ERROR "constant -1\.79769e\+308 overflows float64"
 }