Simplify big.Int comparisons to zero

This commit is contained in:
Nigel Tao committed 2018-01-04 11:08:41 +11:00
1 parent 8a986019f8
commit 0b06dbcf02
3 files changed
+20 -20

No files matched your search

+12 -12
View File
@@ -660,7 +660,7 @@ func (q *checker) bcheckWhile(n *a.While) error {
} }
} }
if cv := n.Condition().ConstValue(); cv != nil && cv.Cmp(zero) == 0 { if cv := n.Condition().ConstValue(); cv != nil && cv.Sign() == 0 {
// We effectively have a "while false { etc }" loop. There's no need to // We effectively have a "while false { etc }" loop. There's no need to
// check the body. // check the body.
} else { } else {
@@ -815,7 +815,7 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
if aMin.Cmp(zero) < 0 { if aMin.Sign() < 0 {
// TODO: error, but a better check than aMin < 0 is that // TODO: error, but a better check than aMin < 0 is that
// a.MType() is u32. Checking this properly should fall out // a.MType() is u32. Checking this properly should fall out
// when a *a.TypeExpr can express function types. // when a *a.TypeExpr can express function types.
@@ -1046,10 +1046,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
case t.KeyXBinaryStar: case t.KeyXBinaryStar:
// TODO: handle multiplication by negative numbers. Note that this // TODO: handle multiplication by negative numbers. Note that this
// might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c. // might reverse the inequality: if 0 < a < b but c < 0 then a*c > b*c.
if lMin.Cmp(zero) < 0 { if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", lhs.Str(q.tm))
} }
if rMin.Cmp(zero) < 0 { if rMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", rhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: multiply op argument %q is possibly negative", rhs.Str(q.tm))
} }
return big.NewInt(0).Mul(lMin, rMin), big.NewInt(0).Mul(lMax, rMax), nil return big.NewInt(0).Mul(lMin, rMin), big.NewInt(0).Mul(lMax, rMax), nil
@@ -1058,10 +1058,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
// TODO. // TODO.
case t.KeyXBinaryShiftL: case t.KeyXBinaryShiftL:
if lMin.Cmp(zero) < 0 { if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
} }
if rMin.Cmp(zero) < 0 { if rMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm))
} }
if rMax.Cmp(ffff) > 0 { if rMax.Cmp(ffff) > 0 {
@@ -1070,10 +1070,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil return big.NewInt(0).Lsh(lMin, uint(rMin.Uint64())), big.NewInt(0).Lsh(lMax, uint(rMax.Uint64())), nil
case t.KeyXBinaryShiftR: case t.KeyXBinaryShiftR:
if lMin.Cmp(zero) < 0 { if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", lhs.Str(q.tm))
} }
if rMin.Cmp(zero) < 0 { if rMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: shift op argument %q is possibly negative", rhs.Str(q.tm))
} }
if rMin.Cmp(maxIntBits) >= 0 { if rMin.Cmp(maxIntBits) >= 0 {
@@ -1088,13 +1088,13 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
case t.KeyXBinaryAmp, t.KeyXBinaryPipe: case t.KeyXBinaryAmp, t.KeyXBinaryPipe:
// TODO: should type-checking ensure that bitwise ops only apply to // TODO: should type-checking ensure that bitwise ops only apply to
// *unsigned* integer types? // *unsigned* integer types?
if lMin.Cmp(zero) < 0 { if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", lhs.Str(q.tm))
} }
if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 { if lMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly too large", lhs.Str(q.tm))
} }
if rMin.Cmp(zero) < 0 { if rMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: bitwise op argument %q is possibly negative", rhs.Str(q.tm))
} }
if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 { if rMax.Cmp(numTypeBounds[t.KeyU64][1]) > 0 {
@@ -1117,10 +1117,10 @@ func (q *checker) bcheckExprBinaryOp1(op t.Key, lhs *a.Expr, lMin *big.Int, lMax
// TODO. // TODO.
case t.KeyXBinaryPercent: case t.KeyXBinaryPercent:
if lMin.Cmp(zero) < 0 { if lMin.Sign() < 0 {
return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly negative", lhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly negative", lhs.Str(q.tm))
} }
if rMin.Cmp(zero) <= 0 { if rMin.Sign() <= 0 {
return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly non-positive", rhs.Str(q.tm)) return nil, nil, fmt.Errorf("check: modulus op argument %q is possibly non-positive", rhs.Str(q.tm))
} }
return zero, big.NewInt(0).Sub(rMax, one), nil return zero, big.NewInt(0).Sub(rMax, one), nil
+1 -1
View File
@@ -119,7 +119,7 @@ check1:
if lhs := x.LHS().Expr(); !lhs.Eq(d) { if lhs := x.LHS().Expr(); !lhs.Eq(d) {
continue continue
} }
if rcv := x.RHS().Expr().ConstValue(); rcv == nil || rcv.Cmp(zero) != 0 { if rcv := x.RHS().Expr().ConstValue(); rcv == nil || rcv.Sign() != 0 {
continue continue
} }
break check1 break check1
+7 -7
View File
@@ -769,7 +769,7 @@ func (q *checker) tcheckExprUnaryOp(n *a.Expr, depth uint32) error {
n.Operator().AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm)) n.Operator().AmbiguousForm().Str(q.tm), rhs.Str(q.tm), rTyp.Str(q.tm))
} }
if cv := rhs.ConstValue(); cv != nil { if cv := rhs.ConstValue(); cv != nil {
n.SetConstValue(btoi(cv.Cmp(zero) == 0)) n.SetConstValue(btoi(cv.Sign() == 0))
} }
n.SetMType(typeExprBool) n.SetMType(typeExprBool)
return nil return nil
@@ -906,20 +906,20 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.
case t.KeyXBinaryStar: case t.KeyXBinaryStar:
return big.NewInt(0).Mul(l, r), nil return big.NewInt(0).Mul(l, r), nil
case t.KeyXBinarySlash: case t.KeyXBinarySlash:
if r.Cmp(zero) == 0 { if r.Sign() == 0 {
return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm))
} }
// TODO: decide on Euclidean division vs other definitions. See "go doc // TODO: decide on Euclidean division vs other definitions. See "go doc
// math/big int.divmod" for details. // math/big int.divmod" for details.
return big.NewInt(0).Div(l, r), nil return big.NewInt(0).Div(l, r), nil
case t.KeyXBinaryShiftL: case t.KeyXBinaryShiftL:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { if r.Sign() < 0 || r.Cmp(ffff) > 0 {
return nil, fmt.Errorf("check: shift %q out of range in const expression %q", return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().Str(tm), n.Str(tm)) n.RHS().Expr().Str(tm), n.Str(tm))
} }
return big.NewInt(0).Lsh(l, uint(r.Uint64())), nil return big.NewInt(0).Lsh(l, uint(r.Uint64())), nil
case t.KeyXBinaryShiftR: case t.KeyXBinaryShiftR:
if r.Cmp(zero) < 0 || r.Cmp(ffff) > 0 { if r.Sign() < 0 || r.Cmp(ffff) > 0 {
return nil, fmt.Errorf("check: shift %q out of range in const expression %q", return nil, fmt.Errorf("check: shift %q out of range in const expression %q",
n.RHS().Expr().Str(tm), n.Str(tm)) n.RHS().Expr().Str(tm), n.Str(tm))
} }
@@ -933,7 +933,7 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.
case t.KeyXBinaryHat: case t.KeyXBinaryHat:
return big.NewInt(0).Xor(l, r), nil return big.NewInt(0).Xor(l, r), nil
case t.KeyXBinaryPercent: case t.KeyXBinaryPercent:
if r.Cmp(zero) == 0 { if r.Sign() == 0 {
return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm)) return nil, fmt.Errorf("check: division by zero in const expression %q", n.Str(tm))
} }
return big.NewInt(0).Mod(l, r), nil return big.NewInt(0).Mod(l, r), nil
@@ -950,9 +950,9 @@ func evalConstValueBinaryOp(tm *t.Map, n *a.Expr, l *big.Int, r *big.Int) (*big.
case t.KeyXBinaryGreaterThan: case t.KeyXBinaryGreaterThan:
return btoi(l.Cmp(r) > 0), nil return btoi(l.Cmp(r) > 0), nil
case t.KeyXBinaryAnd: case t.KeyXBinaryAnd:
return btoi((l.Cmp(zero) != 0) && (r.Cmp(zero) != 0)), nil return btoi((l.Sign() != 0) && (r.Sign() != 0)), nil
case t.KeyXBinaryOr: case t.KeyXBinaryOr:
return btoi((l.Cmp(zero) != 0) || (r.Cmp(zero) != 0)), nil return btoi((l.Sign() != 0) || (r.Sign() != 0)), nil
case t.KeyXBinaryTildePlus: case t.KeyXBinaryTildePlus:
return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers") return nil, fmt.Errorf("check: cannot apply ~+ operator to ideal numbers")
} }