From 0659cf691194f30345442d66c94eba632ca6d7ae Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Wed, 23 Mar 2016 11:07:20 -0700 Subject: [PATCH] cmd/compile: small Mpint method simplifications Get rid of (*Mpint).Add's "quiet" parameter: it's always set to 0. Inline (*Mpint).shift into (*Mpint).Lsh and (*Mpint).Rsh. There's no need for a common shift method that can handle both left or right shifts based on sign when the higher level abstractions only ever do one or the other. Change-Id: Icd3b082413f9193961b6835279e0bd4b6a6a6621 Reviewed-on: https://go-review.googlesource.com/21050 Run-TryBot: Matthew Dempsky TryBot-Result: Gobot Gobot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/gc/const.go | 2 +- src/cmd/compile/internal/gc/mpint.go | 26 ++++++++------------------ src/cmd/compile/internal/gc/parser.go | 2 +- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/cmd/compile/internal/gc/const.go b/src/cmd/compile/internal/gc/const.go index a1271163ab..b09fc2215e 100644 --- a/src/cmd/compile/internal/gc/const.go +++ b/src/cmd/compile/internal/gc/const.go @@ -833,7 +833,7 @@ func evconst(n *Node) { case OADD_ | CTINT_, OADD_ | CTRUNE_: - v.U.(*Mpint).Add(rv.U.(*Mpint), 0) + v.U.(*Mpint).Add(rv.U.(*Mpint)) case OSUB_ | CTINT_, OSUB_ | CTRUNE_: diff --git a/src/cmd/compile/internal/gc/mpint.go b/src/cmd/compile/internal/gc/mpint.go index d0f87deb00..c4ff897e26 100644 --- a/src/cmd/compile/internal/gc/mpint.go +++ b/src/cmd/compile/internal/gc/mpint.go @@ -68,7 +68,7 @@ func (a *Mpint) SetFloat(b *Mpflt) int { return -1 } -func (a *Mpint) Add(b *Mpint, quiet int) { +func (a *Mpint) Add(b *Mpint) { if a.Ovf || b.Ovf { if nsavederrors+nerrors == 0 { Yyerror("ovf in mpaddfixfix") @@ -79,7 +79,7 @@ func (a *Mpint) Add(b *Mpint, quiet int) { a.Val.Add(&a.Val, &b.Val) - if a.checkOverflow(0) && quiet == 0 { + if a.checkOverflow(0) { Yyerror("constant addition overflow") } } @@ -198,20 +198,6 @@ func (a *Mpint) Xor(b *Mpint) { a.Val.Xor(&a.Val, &b.Val) } -// shift left by s (or right by -s) -func (a *Mpint) shift(s int) { - switch { - case s > 0: - if a.checkOverflow(s) { - Yyerror("constant shift overflow") - return - } - a.Val.Lsh(&a.Val, uint(s)) - case s < 0: - a.Val.Rsh(&a.Val, uint(-s)) - } -} - func (a *Mpint) Lsh(b *Mpint) { if a.Ovf || b.Ovf { if nsavederrors+nerrors == 0 { @@ -232,7 +218,11 @@ func (a *Mpint) Lsh(b *Mpint) { return } - a.shift(int(s)) + if a.checkOverflow(int(s)) { + Yyerror("constant shift overflow") + return + } + a.Val.Lsh(&a.Val, uint(s)) } func (a *Mpint) Rsh(b *Mpint) { @@ -255,7 +245,7 @@ func (a *Mpint) Rsh(b *Mpint) { return } - a.shift(int(-s)) + a.Val.Rsh(&a.Val, uint(s)) } func (a *Mpint) Cmp(b *Mpint) int { diff --git a/src/cmd/compile/internal/gc/parser.go b/src/cmd/compile/internal/gc/parser.go index 96b3d24af5..7634e1c2b0 100644 --- a/src/cmd/compile/internal/gc/parser.go +++ b/src/cmd/compile/internal/gc/parser.go @@ -3318,7 +3318,7 @@ func (p *parser) hidden_constant() *Node { if s2.Val().Ctype() == CTRUNE && s4.Val().Ctype() == CTINT { ss := s2 - s2.Val().U.(*Mpint).Add(s4.Val().U.(*Mpint), 0) + s2.Val().U.(*Mpint).Add(s4.Val().U.(*Mpint)) return ss } s4.Val().U.(*Mpcplx).Real = s4.Val().U.(*Mpcplx).Imag