Skip to content

Commit 7c2550b

Browse files
committed
cmd/compile: add math benchmarks
This adds benchmarks for division and modulus of 64 bit signed and unsigned integers. Updates #59089 Change-Id: Ie757c6d74a1f355873e79619eae26ece21a8f23e Reviewed-on: https://go-review.googlesource.com/c/go/+/482656 Reviewed-by: David Chase <[email protected]> Run-TryBot: Joel Sing <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 4c20c0a commit 7c2550b

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package test
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var Output int
8+
9+
func BenchmarkDiv64UnsignedSmall(b *testing.B) {
10+
q := uint64(1)
11+
for i := 1; i <= b.N; i++ {
12+
q = (q + uint64(i)) / uint64(i)
13+
}
14+
Output = int(q)
15+
}
16+
17+
func BenchmarkDiv64Small(b *testing.B) {
18+
q := int64(1)
19+
for i := 1; i <= b.N; i++ {
20+
q = (q + int64(i)) / int64(i)
21+
}
22+
Output = int(q)
23+
}
24+
25+
func BenchmarkDiv64SmallNegDivisor(b *testing.B) {
26+
q := int64(-1)
27+
for i := 1; i <= b.N; i++ {
28+
q = (int64(i) - q) / -int64(i)
29+
}
30+
Output = int(q)
31+
}
32+
33+
func BenchmarkDiv64SmallNegDividend(b *testing.B) {
34+
q := int64(-1)
35+
for i := 1; i <= b.N; i++ {
36+
q = -(int64(i) - q) / int64(i)
37+
}
38+
Output = int(q)
39+
}
40+
41+
func BenchmarkDiv64SmallNegBoth(b *testing.B) {
42+
q := int64(1)
43+
for i := 1; i <= b.N; i++ {
44+
q = -(int64(i) + q) / -int64(i)
45+
}
46+
Output = int(q)
47+
}
48+
49+
func BenchmarkDiv64Unsigned(b *testing.B) {
50+
q := uint64(1)
51+
for i := 1; i <= b.N; i++ {
52+
q = (uint64(0x7fffffffffffffff) - uint64(i) - (q & 1)) / uint64(i)
53+
}
54+
Output = int(q)
55+
}
56+
57+
func BenchmarkDiv64(b *testing.B) {
58+
q := int64(1)
59+
for i := 1; i <= b.N; i++ {
60+
q = (int64(0x7fffffffffffffff) - int64(i) - (q & 1)) / int64(i)
61+
}
62+
Output = int(q)
63+
}
64+
65+
func BenchmarkDiv64NegDivisor(b *testing.B) {
66+
q := int64(-1)
67+
for i := 1; i <= b.N; i++ {
68+
q = (int64(0x7fffffffffffffff) - int64(i) - (q & 1)) / -int64(i)
69+
}
70+
Output = int(q)
71+
}
72+
73+
func BenchmarkDiv64NegDividend(b *testing.B) {
74+
q := int64(-1)
75+
for i := 1; i <= b.N; i++ {
76+
q = -(int64(0x7fffffffffffffff) - int64(i) - (q & 1)) / int64(i)
77+
}
78+
Output = int(q)
79+
}
80+
81+
func BenchmarkDiv64NegBoth(b *testing.B) {
82+
q := int64(-1)
83+
for i := 1; i <= b.N; i++ {
84+
q = -(int64(0x7fffffffffffffff) - int64(i) - (q & 1)) / -int64(i)
85+
}
86+
Output = int(q)
87+
}
88+
89+
func BenchmarkMod64UnsignedSmall(b *testing.B) {
90+
r := uint64(1)
91+
for i := 1; i <= b.N; i++ {
92+
r = (uint64(i) + r) % uint64(i)
93+
}
94+
Output = int(r)
95+
}
96+
97+
func BenchmarkMod64Small(b *testing.B) {
98+
r := int64(1)
99+
for i := 1; i <= b.N; i++ {
100+
r = (int64(i) + r) % int64(i)
101+
}
102+
Output = int(r)
103+
}
104+
105+
func BenchmarkMod64SmallNegDivisor(b *testing.B) {
106+
r := int64(-1)
107+
for i := 1; i <= b.N; i++ {
108+
r = (int64(i) - r) % -int64(i)
109+
}
110+
Output = int(r)
111+
}
112+
113+
func BenchmarkMod64SmallNegDividend(b *testing.B) {
114+
r := int64(-1)
115+
for i := 1; i <= b.N; i++ {
116+
r = -(int64(i) - r) % int64(i)
117+
}
118+
Output = int(r)
119+
}
120+
121+
func BenchmarkMod64SmallNegBoth(b *testing.B) {
122+
r := int64(1)
123+
for i := 1; i <= b.N; i++ {
124+
r = -(int64(i) + r) % -int64(i)
125+
}
126+
Output = int(r)
127+
}
128+
129+
func BenchmarkMod64Unsigned(b *testing.B) {
130+
r := uint64(1)
131+
for i := 1; i <= b.N; i++ {
132+
r = (uint64(0x7fffffffffffffff) - uint64(i) - (r & 1)) % uint64(i)
133+
}
134+
Output = int(r)
135+
}
136+
137+
func BenchmarkMod64(b *testing.B) {
138+
r := int64(1)
139+
for i := 1; i <= b.N; i++ {
140+
r = (int64(0x7fffffffffffffff) - int64(i) - (r & 1)) % int64(i)
141+
}
142+
Output = int(r)
143+
}
144+
145+
func BenchmarkMod64NegDivisor(b *testing.B) {
146+
r := int64(-1)
147+
for i := 1; i <= b.N; i++ {
148+
r = (int64(0x7fffffffffffffff) - int64(i) - (r & 1)) % -int64(i)
149+
}
150+
Output = int(r)
151+
}
152+
153+
func BenchmarkMod64NegDividend(b *testing.B) {
154+
r := int64(-1)
155+
for i := 1; i <= b.N; i++ {
156+
r = -(int64(0x7fffffffffffffff) - int64(i) - (r & 1)) % int64(i)
157+
}
158+
Output = int(r)
159+
}
160+
161+
func BenchmarkMod64NegBoth(b *testing.B) {
162+
r := int64(1)
163+
for i := 1; i <= b.N; i++ {
164+
r = -(int64(0x7fffffffffffffff) - int64(i) - (r & 1)) % -int64(i)
165+
}
166+
Output = int(r)
167+
}

0 commit comments

Comments
 (0)