Simplify bitMask calculation.

This commit is contained in:
Nigel Tao committed 2017-05-28 18:51:54 +10:00
1 parent 843e35aff5
commit ef55631cbd
2 files changed
+9 -12

No files matched your search

+7 -10
View File
@@ -62,12 +62,9 @@ func max(i, j *big.Int) *big.Int {
return j return j
} }
func roundUpToPowerOf2Minus1(i *big.Int) *big.Int { // bitMask returns (1<<nBits - 1) as a big integer.
if i.Cmp(zero) < 0 { func bitMask(nBits int) *big.Int {
panic("unreachable") switch nBits {
}
bitLen := i.BitLen()
switch bitLen {
case 0: case 0:
return zero return zero
case 1: case 1:
@@ -81,7 +78,7 @@ func roundUpToPowerOf2Minus1(i *big.Int) *big.Int {
case 64: case 64:
return numTypeBounds[t.KeyU64][1] return numTypeBounds[t.KeyU64][1]
} }
z := big.NewInt(0).Lsh(one, uint(bitLen)) z := big.NewInt(0).Lsh(one, uint(nBits))
return z.Sub(z, one) return z.Sub(z, one)
} }
@@ -481,9 +478,9 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
} else { } else {
z = max(lMax, rMax) z = max(lMax, rMax)
} }
// Round up z to the next-power-of-2-minus-1, and return [0, z]. This // Return [0, z rounded up to the next power-of-2-minus-1]. This is
// is conservative, but works fine in practice. // conservative, but works fine in practice.
return zero, roundUpToPowerOf2Minus1(z), nil return zero, bitMask(z.BitLen()), nil
case t.KeyXBinaryAmpHat: case t.KeyXBinaryAmpHat:
case t.KeyXBinaryHat: case t.KeyXBinaryHat:
+2 -2
View File
@@ -225,7 +225,7 @@ func TestConstValues(t *testing.T) {
} }
} }
func TestRoundUpToPowerOf2Minus1(t *testing.T) { func TestBitMask(t *testing.T) {
testCases := [][2]uint64{ testCases := [][2]uint64{
{0, 0}, {0, 0},
{1, 1}, {1, 1},
@@ -255,7 +255,7 @@ func TestRoundUpToPowerOf2Minus1(t *testing.T) {
} }
for _, tc := range testCases { for _, tc := range testCases {
got := roundUpToPowerOf2Minus1(big.NewInt(0).SetUint64(tc[0])) got := bitMask(big.NewInt(0).SetUint64(tc[0]).BitLen())
want := big.NewInt(0).SetUint64(tc[1]) want := big.NewInt(0).SetUint64(tc[1])
if got.Cmp(want) != 0 { if got.Cmp(want) != 0 {
t.Errorf("roundUpToPowerOf2Minus1(%v): got %v, want %v", tc[0], got, tc[1]) t.Errorf("roundUpToPowerOf2Minus1(%v): got %v, want %v", tc[0], got, tc[1])