mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-18 00:32:11 +01:00
Simplify bitMask calculation.
This commit is contained in:
1 parent
843e35aff5
commit
ef55631cbd
2 files changed
+9
-12
No files matched your search
+7
-10
@@ -62,12 +62,9 @@ func max(i, j *big.Int) *big.Int {
|
||||
return j
|
||||
}
|
||||
|
||||
func roundUpToPowerOf2Minus1(i *big.Int) *big.Int {
|
||||
if i.Cmp(zero) < 0 {
|
||||
panic("unreachable")
|
||||
}
|
||||
bitLen := i.BitLen()
|
||||
switch bitLen {
|
||||
// bitMask returns (1<<nBits - 1) as a big integer.
|
||||
func bitMask(nBits int) *big.Int {
|
||||
switch nBits {
|
||||
case 0:
|
||||
return zero
|
||||
case 1:
|
||||
@@ -81,7 +78,7 @@ func roundUpToPowerOf2Minus1(i *big.Int) *big.Int {
|
||||
case 64:
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -481,9 +478,9 @@ func (q *checker) bcheckExprBinaryOp(lhs *a.Expr, op t.Key, rhs *a.Expr, depth u
|
||||
} else {
|
||||
z = max(lMax, rMax)
|
||||
}
|
||||
// Round up z to the next-power-of-2-minus-1, and return [0, z]. This
|
||||
// is conservative, but works fine in practice.
|
||||
return zero, roundUpToPowerOf2Minus1(z), nil
|
||||
// Return [0, z rounded up to the next power-of-2-minus-1]. This is
|
||||
// conservative, but works fine in practice.
|
||||
return zero, bitMask(z.BitLen()), nil
|
||||
case t.KeyXBinaryAmpHat:
|
||||
case t.KeyXBinaryHat:
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ func TestConstValues(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundUpToPowerOf2Minus1(t *testing.T) {
|
||||
func TestBitMask(t *testing.T) {
|
||||
testCases := [][2]uint64{
|
||||
{0, 0},
|
||||
{1, 1},
|
||||
@@ -255,7 +255,7 @@ func TestRoundUpToPowerOf2Minus1(t *testing.T) {
|
||||
}
|
||||
|
||||
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])
|
||||
if got.Cmp(want) != 0 {
|
||||
t.Errorf("roundUpToPowerOf2Minus1(%v): got %v, want %v", tc[0], got, tc[1])
|
||||
|
||||
Reference in new issue
Block a user