go/src/cmd/compile/internal/gc/mpint.go

312 lines
5.5 KiB
Go
Raw Normal View History

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import (
"fmt"
"math/big"
)
// implements integer arithmetic
// Mpint represents an integer constant.
type Mpint struct {
Val big.Int
Ovf bool // set if Val overflowed compiler limit (sticky)
Rune bool // set if syntax indicates default type rune
}
func (a *Mpint) SetOverflow() {
a.Val.SetUint64(1) // avoid spurious div-zero errors
a.Ovf = true
}
func (a *Mpint) checkOverflow(extra int) bool {
// We don't need to be precise here, any reasonable upper limit would do.
// For now, use existing limit so we pass all the tests unchanged.
if a.Val.BitLen()+extra > Mpprec {
a.SetOverflow()
}
return a.Ovf
}
func (a *Mpint) Set(b *Mpint) {
a.Val.Set(&b.Val)
}
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
func (a *Mpint) SetFloat(b *Mpflt) bool {
// avoid converting huge floating-point numbers to integers
// (2*Mpprec is large enough to permit all tests to pass)
if b.Val.MantExp(nil) > 2*Mpprec {
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
a.SetOverflow()
return false
}
if _, acc := b.Val.Int(&a.Val); acc == big.Exact {
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
return true
}
const delta = 16 // a reasonably small number of bits > 0
var t big.Float
t.SetPrec(Mpprec - delta)
// try rounding down a little
t.SetMode(big.ToZero)
t.Set(&b.Val)
if _, acc := t.Int(&a.Val); acc == big.Exact {
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
return true
}
// try rounding up a little
t.SetMode(big.AwayFromZero)
t.Set(&b.Val)
if _, acc := t.Int(&a.Val); acc == big.Exact {
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
return true
}
cmd/compile: fix misleading "truncated to int" messages When defining an int const, the compiler tries to cast the RHS expression to int. The cast may fail for three reasons: 1. expr is an integer constant that overflows int 2. expr is a floating point constant 3. expr is a complex constant, or not a number In the second case, in order to print a sensible error message, we must distinguish between a floating point constant that should be included in the error message and a floating point constant that cannot be reasonably formatted for inclusion in an error message. For example, in: const a int = 1.1 const b int = 1 + 1e-100 a is in the former group, while b is in the latter, since the floating point value resulting from the evaluation of the rhs of the assignment (1.00...01) is too long to be fully printed in an error message, and cannot be shortened without making the error message misleading (rounding or truncating it would result in a "1", which looks like an integer constant, and it makes little sense in an error message about an invalid floating point expression). To fix this problem, we try to format the float value using fconv (which is used by the error reporting mechanism to format float arguments), and then parse the resulting string back to a big.Float. If the result is an integer, we assume that expr is a float value that cannot be reasonably be formatted as a string, and we emit an error message that does not include its string representation. Also, change the error message for overflows to a more conservative "integer too large", which does not mention overflows that are only caused by an internal implementation restriction. Also, change (*Mpint) SetFloat so that it returns a bool (instead of 0/-1 for success/failure). Fixes #11371 Change-Id: Ibbc73e2ed2eaf41f07827b0649d0eb637150ecaa Reviewed-on: https://go-review.googlesource.com/35411 Run-TryBot: Alberto Donizetti <alb.donizetti@gmail.com> Run-TryBot: Robert Griesemer <gri@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
2017-01-18 11:48:14 +01:00
a.Ovf = false
return false
}
func (a *Mpint) Add(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Add")
}
a.SetOverflow()
return
}
a.Val.Add(&a.Val, &b.Val)
if a.checkOverflow(0) {
yyerror("constant addition overflow")
}
}
func (a *Mpint) Sub(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Sub")
}
a.SetOverflow()
return
}
a.Val.Sub(&a.Val, &b.Val)
if a.checkOverflow(0) {
yyerror("constant subtraction overflow")
}
}
func (a *Mpint) Mul(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Mul")
}
a.SetOverflow()
return
}
a.Val.Mul(&a.Val, &b.Val)
if a.checkOverflow(0) {
yyerror("constant multiplication overflow")
}
}
func (a *Mpint) Quo(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Quo")
}
a.SetOverflow()
return
}
a.Val.Quo(&a.Val, &b.Val)
if a.checkOverflow(0) {
// can only happen for div-0 which should be checked elsewhere
yyerror("constant division overflow")
}
}
func (a *Mpint) Rem(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Rem")
}
a.SetOverflow()
return
}
a.Val.Rem(&a.Val, &b.Val)
if a.checkOverflow(0) {
// should never happen
yyerror("constant modulo overflow")
}
}
func (a *Mpint) Or(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Or")
}
a.SetOverflow()
return
}
a.Val.Or(&a.Val, &b.Val)
}
func (a *Mpint) And(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint And")
}
a.SetOverflow()
return
}
a.Val.And(&a.Val, &b.Val)
}
func (a *Mpint) AndNot(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint AndNot")
}
a.SetOverflow()
return
}
a.Val.AndNot(&a.Val, &b.Val)
}
func (a *Mpint) Xor(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Xor")
}
a.SetOverflow()
return
}
a.Val.Xor(&a.Val, &b.Val)
}
func (a *Mpint) Lsh(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Lsh")
}
a.SetOverflow()
return
}
s := b.Int64()
if s < 0 || s >= Mpprec {
msg := "shift count too large"
if s < 0 {
msg = "invalid negative shift count"
}
yyerror("%s: %d", msg, s)
a.SetInt64(0)
return
}
if a.checkOverflow(int(s)) {
yyerror("constant shift overflow")
return
}
a.Val.Lsh(&a.Val, uint(s))
}
func (a *Mpint) Rsh(b *Mpint) {
if a.Ovf || b.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("ovf in Mpint Rsh")
}
a.SetOverflow()
return
}
s := b.Int64()
if s < 0 {
yyerror("invalid negative shift count: %d", s)
if a.Val.Sign() < 0 {
a.SetInt64(-1)
} else {
a.SetInt64(0)
}
return
}
a.Val.Rsh(&a.Val, uint(s))
}
func (a *Mpint) Cmp(b *Mpint) int {
return a.Val.Cmp(&b.Val)
}
func (a *Mpint) CmpInt64(c int64) int {
if c == 0 {
return a.Val.Sign() // common case shortcut
}
return a.Val.Cmp(big.NewInt(c))
}
func (a *Mpint) Neg() {
a.Val.Neg(&a.Val)
}
func (a *Mpint) Int64() int64 {
if a.Ovf {
if nsavederrors+nerrors == 0 {
yyerror("constant overflow")
}
return 0
}
return a.Val.Int64()
}
func (a *Mpint) SetInt64(c int64) {
a.Val.SetInt64(c)
}
func (a *Mpint) SetString(as string) {
_, ok := a.Val.SetString(as, 0)
if !ok {
// required syntax is [+-][0[x]]d*
// At the moment we lose precise error cause;
// the old code distinguished between:
// - malformed hex constant
// - malformed octal constant
// - malformed decimal constant
// TODO(gri) use different conversion function
yyerror("malformed integer constant: %s", as)
a.Val.SetUint64(0)
return
}
if a.checkOverflow(0) {
yyerror("constant too large: %s", as)
}
}
func (x *Mpint) String() string {
return bconv(x, 0)
}
func bconv(xval *Mpint, flag FmtFlag) string {
if flag&FmtSharp != 0 {
return fmt.Sprintf("%#x", &xval.Val)
}
return xval.Val.String()
}