Merge pull request #89530 from tklauser/use-bits-onescount
Use OnesCount8 from math/bits to implement countBits
This commit is contained in:
		@@ -16,49 +16,16 @@ limitations under the License.
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
package allocator
 | 
					package allocator
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "math/big"
 | 
					import (
 | 
				
			||||||
 | 
						"math/big"
 | 
				
			||||||
 | 
						"math/bits"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// countBits returns the number of set bits in n
 | 
					// countBits returns the number of set bits in n
 | 
				
			||||||
func countBits(n *big.Int) int {
 | 
					func countBits(n *big.Int) int {
 | 
				
			||||||
	var count int = 0
 | 
						var count int = 0
 | 
				
			||||||
	for _, b := range n.Bytes() {
 | 
						for _, b := range n.Bytes() {
 | 
				
			||||||
		count += int(bitCounts[b])
 | 
							count += bits.OnesCount8(uint8(b))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return count
 | 
						return count
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
// bitCounts is all of the bits counted for each number between 0-255
 | 
					 | 
				
			||||||
var bitCounts = []int8{
 | 
					 | 
				
			||||||
	0, 1, 1, 2, 1, 2, 2, 3,
 | 
					 | 
				
			||||||
	1, 2, 2, 3, 2, 3, 3, 4,
 | 
					 | 
				
			||||||
	1, 2, 2, 3, 2, 3, 3, 4,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	1, 2, 2, 3, 2, 3, 3, 4,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	1, 2, 2, 3, 2, 3, 3, 4,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	4, 5, 5, 6, 5, 6, 6, 7,
 | 
					 | 
				
			||||||
	1, 2, 2, 3, 2, 3, 3, 4,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	4, 5, 5, 6, 5, 6, 6, 7,
 | 
					 | 
				
			||||||
	2, 3, 3, 4, 3, 4, 4, 5,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	4, 5, 5, 6, 5, 6, 6, 7,
 | 
					 | 
				
			||||||
	3, 4, 4, 5, 4, 5, 5, 6,
 | 
					 | 
				
			||||||
	4, 5, 5, 6, 5, 6, 6, 7,
 | 
					 | 
				
			||||||
	4, 5, 5, 6, 5, 6, 6, 7,
 | 
					 | 
				
			||||||
	5, 6, 6, 7, 6, 7, 7, 8,
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
@@ -21,20 +21,6 @@ import (
 | 
				
			|||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestBitCount(t *testing.T) {
 | 
					 | 
				
			||||||
	for i, c := range bitCounts {
 | 
					 | 
				
			||||||
		actual := 0
 | 
					 | 
				
			||||||
		for j := 0; j < 8; j++ {
 | 
					 | 
				
			||||||
			if ((1 << uint(j)) & i) != 0 {
 | 
					 | 
				
			||||||
				actual++
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		if actual != int(c) {
 | 
					 | 
				
			||||||
			t.Errorf("%d should have %d bits but recorded as %d", i, actual, c)
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func TestCountBits(t *testing.T) {
 | 
					func TestCountBits(t *testing.T) {
 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		n        *big.Int
 | 
							n        *big.Int
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user