From c33d7f0e212a950f37654e1a502d3ea7be1608ae Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 29 Aug 2017 11:31:16 +0200 Subject: [PATCH] Leverage math bitx --- common/net/ipnet.go | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/common/net/ipnet.go b/common/net/ipnet.go index 123ed3344..b0a527a5f 100644 --- a/common/net/ipnet.go +++ b/common/net/ipnet.go @@ -1,13 +1,10 @@ package net import ( + "math/bits" "net" ) -var ( - onesCount = make(map[byte]byte) -) - type IPNet struct { cache map[uint32]byte } @@ -30,7 +27,7 @@ func ipToUint32(ip net.IP) uint32 { func ipMaskToByte(mask net.IPMask) byte { value := byte(0) for _, b := range []byte(mask) { - value += onesCount[b] + value += byte(bits.OnesCount8(b)) } return value } @@ -83,11 +80,3 @@ func (n *IPNet) Contains(ip net.IP) bool { func (n *IPNet) IsEmpty() bool { return len(n.cache) == 0 } - -func init() { - value := byte(0) - for mask := byte(1); mask <= 8; mask++ { - value += 1 << byte(8-mask) - onesCount[value] = mask - } -}