From cf28da99874d88e39cbfa1b969b33c3ab70ecc8e Mon Sep 17 00:00:00 2001 From: xiangpengzhao Date: Wed, 8 Nov 2017 12:18:55 +0800 Subject: [PATCH] Use go native func after upgrading to go1.9 --- pkg/controller/node/ipam/cidrset/cidr_set.go | 46 +------------------- 1 file changed, 2 insertions(+), 44 deletions(-) diff --git a/pkg/controller/node/ipam/cidrset/cidr_set.go b/pkg/controller/node/ipam/cidrset/cidr_set.go index c977fd08b89..80e0f8ec135 100644 --- a/pkg/controller/node/ipam/cidrset/cidr_set.go +++ b/pkg/controller/node/ipam/cidrset/cidr_set.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "math/big" + "math/bits" "net" "sync" ) @@ -74,48 +75,6 @@ func NewCIDRSet(clusterCIDR *net.IPNet, subNetMaskSize int) *CidrSet { } } -// TODO: Remove this function when upgrading to go 1.9 -var len8tab = [256]uint8{ - 0x00, 0x01, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, - 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, - 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, -} - -// TODO: Remove this function when upgrading to go 1.9 -// len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0. -func len64(x uint64) (n int) { - if x >= 1<<32 { - x >>= 32 - n = 32 - } - if x >= 1<<16 { - x >>= 16 - n += 16 - } - if x >= 1<<8 { - x >>= 8 - n += 8 - } - return n + int(len8tab[x]) -} - -// TODO: Remove this function when upgrading to go 1.9 -// leadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0. -func leadingZeros64(x uint64) int { return 64 - len64(x) } - func (s *CidrSet) indexToCIDRBlock(index int) *net.IPNet { var ip []byte var mask int @@ -147,8 +106,7 @@ func (s *CidrSet) indexToCIDRBlock(index int) *net.IPNet { if s.clusterMaskSize < halfV6NBits { // see how many bits are needed to reach the left side btl := uint(s.subNetMaskSize - halfV6NBits) - // TODO: Replace this with math/bits.LeadingZeros64 when upgrading to go 1.9 - indexMaxBit := uint(64 - leadingZeros64(uint64(index))) + indexMaxBit := uint(64 - bits.LeadingZeros64(uint64(index))) if indexMaxBit > btl { leftClusterIP |= uint64(index) >> btl }