Commit 07749aef authored by Robert Griesemer's avatar Robert Griesemer

cmd/compile: special-case const comparisons against zero

Constant comparisons against 0 are reasonably common.
Special-case and avoid allocating a new zero value each time.

Change-Id: I6c526c8ab30ef7f0fef59110133c764b7b90ba05
Reviewed-on: https://go-review.googlesource.com/20956Reviewed-by: default avatarAlan Donovan <adonovan@google.com>
parent d3253876
...@@ -116,11 +116,11 @@ func (a *Mpflt) Cmp(b *Mpflt) int { ...@@ -116,11 +116,11 @@ func (a *Mpflt) Cmp(b *Mpflt) int {
return a.Val.Cmp(&b.Val) return a.Val.Cmp(&b.Val)
} }
func (b *Mpflt) CmpFloat64(c float64) int { func (a *Mpflt) CmpFloat64(c float64) int {
var a Mpflt if c == 0 {
return a.Val.Sign() // common case shortcut
a.SetFloat64(c) }
return b.Cmp(&a) return a.Val.Cmp(big.NewFloat(c))
} }
func (a *Mpflt) Float64() float64 { func (a *Mpflt) Float64() float64 {
......
...@@ -262,8 +262,11 @@ func (a *Mpint) Cmp(b *Mpint) int { ...@@ -262,8 +262,11 @@ func (a *Mpint) Cmp(b *Mpint) int {
return a.Val.Cmp(&b.Val) return a.Val.Cmp(&b.Val)
} }
func (b *Mpint) CmpInt64(c int64) int { func (a *Mpint) CmpInt64(c int64) int {
return b.Val.Cmp(big.NewInt(c)) if c == 0 {
return a.Val.Sign() // common case shortcut
}
return a.Val.Cmp(big.NewInt(c))
} }
func (a *Mpint) Neg() { func (a *Mpint) Neg() {
......
...@@ -10,8 +10,6 @@ import ( ...@@ -10,8 +10,6 @@ import (
"strings" "strings"
) )
var mpzero Mpint
// The constant is known to runtime. // The constant is known to runtime.
const ( const (
tmpstringbufsize = 32 tmpstringbufsize = 32
...@@ -1229,7 +1227,7 @@ opswitch: ...@@ -1229,7 +1227,7 @@ opswitch:
} }
if Isconst(n.Right, CTINT) { if Isconst(n.Right, CTINT) {
if n.Right.Val().U.(*Mpint).Cmp(&mpzero) < 0 || n.Right.Val().U.(*Mpint).Cmp(Maxintval[TINT]) > 0 { if n.Right.Val().U.(*Mpint).CmpInt64(0) < 0 || n.Right.Val().U.(*Mpint).Cmp(Maxintval[TINT]) > 0 {
Yyerror("index out of bounds") Yyerror("index out of bounds")
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment