-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopcount.go
85 lines (71 loc) · 1.57 KB
/
popcount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"time"
)
var pc [256]byte
func main() {
start_pop := time.Now()
pop_count := PopCount(42)
elapsed_pop := time.Since(start_pop)
fmt.Println("Regular popcount")
fmt.Println("Elapsed time: ", elapsed_pop)
fmt.Println("Pop count of 42: ", pop_count)
start_loop := time.Now()
loop_count := PopCountLoop(42)
elapsed_loop := time.Since(start_loop)
fmt.Println("Loop popcount")
fmt.Println("Elapsed time: ", elapsed_loop)
fmt.Println("Pop count of 42: ", loop_count)
start_SF := time.Now()
SF_count := PopCountSF(42)
elapsed_SF := time.Since(start_SF)
fmt.Println("64 popcount")
fmt.Println("Elapsed time: ", elapsed_SF)
fmt.Println("Pop count of 42: ", SF_count)
start_clear := time.Now()
clear_count := PopCountClear(42)
elapsed_clear := time.Since(start_clear)
fmt.Println("Clear popcount")
fmt.Println("Elapsed time: ", elapsed_clear)
fmt.Println("Pop count of 42: ", clear_count)
}
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(i&1)
}
}
func PopCount(x uint64) int {
return int(pc[byte(x>>(0*8))] +
pc[byte(x>>(1*8))] +
pc[byte(x>>(2*8))] +
pc[byte(x>>(3*8))] +
pc[byte(x>>(4*8))] +
pc[byte(x>>(5*8))] +
pc[byte(x>>(6*8))] +
pc[byte(x>>(7*8))])
}
func PopCountLoop(x uint64) int {
var sum byte
for i := uint(0); i < 8; i++ {
sum += pc[byte(x>>(i*8))]
}
return int(sum)
}
func PopCountSF(x uint64) int {
var sum int
for i := uint(0); i < 64; i++ {
if x&(x<<i) != 0 {
sum++
}
}
return sum
}
func PopCountClear(x uint64) int {
var sum int
for x != 0 {
x = x & (x - 1)
sum++
}
return sum
}