Skip to content

Commit 5fe476d

Browse files
tklausermvdan
authored andcommitted
unix: use bits.OnesCount64 instead of local copy
The local copy of bits.OnesCount64 was added for compatibility with Go 1.8 and earlier in CL 86477. Go 1.8 is no longer supported and go.mod requires Go 1.12, so drop the local copy and use bits.OnesCount64. Change-Id: Ieb77f0cef5f8f206b74ca737449efdcfe1949d44 Reviewed-on: https://go-review.googlesource.com/c/sys/+/192357 Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
1 parent c7b8b68 commit 5fe476d

File tree

1 file changed

+2
-44
lines changed

1 file changed

+2
-44
lines changed

unix/affinity_linux.go

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package unix
88

99
import (
10+
"math/bits"
1011
"unsafe"
1112
)
1213

@@ -79,50 +80,7 @@ func (s *CPUSet) IsSet(cpu int) bool {
7980
func (s *CPUSet) Count() int {
8081
c := 0
8182
for _, b := range s {
82-
c += onesCount64(uint64(b))
83+
c += bits.OnesCount64(uint64(b))
8384
}
8485
return c
8586
}
86-
87-
// onesCount64 is a copy of Go 1.9's math/bits.OnesCount64.
88-
// Once this package can require Go 1.9, we can delete this
89-
// and update the caller to use bits.OnesCount64.
90-
func onesCount64(x uint64) int {
91-
const m0 = 0x5555555555555555 // 01010101 ...
92-
const m1 = 0x3333333333333333 // 00110011 ...
93-
const m2 = 0x0f0f0f0f0f0f0f0f // 00001111 ...
94-
95-
// Unused in this function, but definitions preserved for
96-
// documentation purposes:
97-
//
98-
// const m3 = 0x00ff00ff00ff00ff // etc.
99-
// const m4 = 0x0000ffff0000ffff
100-
//
101-
// Implementation: Parallel summing of adjacent bits.
102-
// See "Hacker's Delight", Chap. 5: Counting Bits.
103-
// The following pattern shows the general approach:
104-
//
105-
// x = x>>1&(m0&m) + x&(m0&m)
106-
// x = x>>2&(m1&m) + x&(m1&m)
107-
// x = x>>4&(m2&m) + x&(m2&m)
108-
// x = x>>8&(m3&m) + x&(m3&m)
109-
// x = x>>16&(m4&m) + x&(m4&m)
110-
// x = x>>32&(m5&m) + x&(m5&m)
111-
// return int(x)
112-
//
113-
// Masking (& operations) can be left away when there's no
114-
// danger that a field's sum will carry over into the next
115-
// field: Since the result cannot be > 64, 8 bits is enough
116-
// and we can ignore the masks for the shifts by 8 and up.
117-
// Per "Hacker's Delight", the first line can be simplified
118-
// more, but it saves at best one instruction, so we leave
119-
// it alone for clarity.
120-
const m = 1<<64 - 1
121-
x = x>>1&(m0&m) + x&(m0&m)
122-
x = x>>2&(m1&m) + x&(m1&m)
123-
x = (x>>4 + x) & (m2 & m)
124-
x += x >> 8
125-
x += x >> 16
126-
x += x >> 32
127-
return int(x) & (1<<7 - 1)
128-
}

0 commit comments

Comments
 (0)