summaryrefslogtreecommitdiffstats
path: root/src/cmd/compile/internal/test/divconst_test.go
blob: 9358a60374996aa29b0cf694ca002914f62c1457 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package test

import (
	"testing"
)

var boolres bool

var i64res int64

func BenchmarkDivconstI64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i64res = int64(i) / 7
	}
}

func BenchmarkModconstI64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i64res = int64(i) % 7
	}
}

func BenchmarkDivisiblePow2constI64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int64(i)%16 == 0
	}
}
func BenchmarkDivisibleconstI64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int64(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstI64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i64res = int64(i) / 7
		boolres = int64(i)%7 == 0
	}
}

var u64res uint64

func TestDivmodConstU64(t *testing.T) {
	// Test division by c. Function f must be func(n) { return n/c, n%c }
	testdiv := func(c uint64, f func(uint64) (uint64, uint64)) func(*testing.T) {
		return func(t *testing.T) {
			x := uint64(12345)
			for i := 0; i < 10000; i++ {
				x += x << 2
				q, r := f(x)
				if r < 0 || r >= c || q*c+r != x {
					t.Errorf("divmod(%d, %d) returned incorrect (%d, %d)", x, c, q, r)
				}
			}
			max := uint64(1<<64-1) / c * c
			xs := []uint64{0, 1, c - 1, c, c + 1, 2*c - 1, 2 * c, 2*c + 1,
				c*c - 1, c * c, c*c + 1, max - 1, max, max + 1, 1<<64 - 1}
			for _, x := range xs {
				q, r := f(x)
				if r < 0 || r >= c || q*c+r != x {
					t.Errorf("divmod(%d, %d) returned incorrect (%d, %d)", x, c, q, r)
				}
			}
		}
	}
	t.Run("2", testdiv(2, func(n uint64) (uint64, uint64) { return n / 2, n % 2 }))
	t.Run("3", testdiv(3, func(n uint64) (uint64, uint64) { return n / 3, n % 3 }))
	t.Run("4", testdiv(4, func(n uint64) (uint64, uint64) { return n / 4, n % 4 }))
	t.Run("5", testdiv(5, func(n uint64) (uint64, uint64) { return n / 5, n % 5 }))
	t.Run("6", testdiv(6, func(n uint64) (uint64, uint64) { return n / 6, n % 6 }))
	t.Run("7", testdiv(7, func(n uint64) (uint64, uint64) { return n / 7, n % 7 }))
	t.Run("8", testdiv(8, func(n uint64) (uint64, uint64) { return n / 8, n % 8 }))
	t.Run("9", testdiv(9, func(n uint64) (uint64, uint64) { return n / 9, n % 9 }))
	t.Run("10", testdiv(10, func(n uint64) (uint64, uint64) { return n / 10, n % 10 }))
	t.Run("11", testdiv(11, func(n uint64) (uint64, uint64) { return n / 11, n % 11 }))
	t.Run("12", testdiv(12, func(n uint64) (uint64, uint64) { return n / 12, n % 12 }))
	t.Run("13", testdiv(13, func(n uint64) (uint64, uint64) { return n / 13, n % 13 }))
	t.Run("14", testdiv(14, func(n uint64) (uint64, uint64) { return n / 14, n % 14 }))
	t.Run("15", testdiv(15, func(n uint64) (uint64, uint64) { return n / 15, n % 15 }))
	t.Run("16", testdiv(16, func(n uint64) (uint64, uint64) { return n / 16, n % 16 }))
	t.Run("17", testdiv(17, func(n uint64) (uint64, uint64) { return n / 17, n % 17 }))
	t.Run("255", testdiv(255, func(n uint64) (uint64, uint64) { return n / 255, n % 255 }))
	t.Run("256", testdiv(256, func(n uint64) (uint64, uint64) { return n / 256, n % 256 }))
	t.Run("257", testdiv(257, func(n uint64) (uint64, uint64) { return n / 257, n % 257 }))
	t.Run("65535", testdiv(65535, func(n uint64) (uint64, uint64) { return n / 65535, n % 65535 }))
	t.Run("65536", testdiv(65536, func(n uint64) (uint64, uint64) { return n / 65536, n % 65536 }))
	t.Run("65537", testdiv(65537, func(n uint64) (uint64, uint64) { return n / 65537, n % 65537 }))
	t.Run("1<<32-1", testdiv(1<<32-1, func(n uint64) (uint64, uint64) { return n / (1<<32 - 1), n % (1<<32 - 1) }))
	t.Run("1<<32+1", testdiv(1<<32+1, func(n uint64) (uint64, uint64) { return n / (1<<32 + 1), n % (1<<32 + 1) }))
	t.Run("1<<64-1", testdiv(1<<64-1, func(n uint64) (uint64, uint64) { return n / (1<<64 - 1), n % (1<<64 - 1) }))
}

func BenchmarkDivconstU64(b *testing.B) {
	b.Run("3", func(b *testing.B) {
		x := uint64(123456789123456789)
		for i := 0; i < b.N; i++ {
			x += x << 4
			u64res = uint64(x) / 3
		}
	})
	b.Run("5", func(b *testing.B) {
		x := uint64(123456789123456789)
		for i := 0; i < b.N; i++ {
			x += x << 4
			u64res = uint64(x) / 5
		}
	})
	b.Run("37", func(b *testing.B) {
		x := uint64(123456789123456789)
		for i := 0; i < b.N; i++ {
			x += x << 4
			u64res = uint64(x) / 37
		}
	})
	b.Run("1234567", func(b *testing.B) {
		x := uint64(123456789123456789)
		for i := 0; i < b.N; i++ {
			x += x << 4
			u64res = uint64(x) / 1234567
		}
	})
}

func BenchmarkModconstU64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u64res = uint64(i) % 7
	}
}

func BenchmarkDivisibleconstU64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = uint64(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstU64(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u64res = uint64(i) / 7
		boolres = uint64(i)%7 == 0
	}
}

var i32res int32

func BenchmarkDivconstI32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i32res = int32(i) / 7
	}
}

func BenchmarkModconstI32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i32res = int32(i) % 7
	}
}

func BenchmarkDivisiblePow2constI32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int32(i)%16 == 0
	}
}

func BenchmarkDivisibleconstI32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int32(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstI32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i32res = int32(i) / 7
		boolres = int32(i)%7 == 0
	}
}

var u32res uint32

func BenchmarkDivconstU32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u32res = uint32(i) / 7
	}
}

func BenchmarkModconstU32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u32res = uint32(i) % 7
	}
}

func BenchmarkDivisibleconstU32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = uint32(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstU32(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u32res = uint32(i) / 7
		boolres = uint32(i)%7 == 0
	}
}

var i16res int16

func BenchmarkDivconstI16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i16res = int16(i) / 7
	}
}

func BenchmarkModconstI16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i16res = int16(i) % 7
	}
}

func BenchmarkDivisiblePow2constI16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int16(i)%16 == 0
	}
}

func BenchmarkDivisibleconstI16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int16(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstI16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i16res = int16(i) / 7
		boolres = int16(i)%7 == 0
	}
}

var u16res uint16

func BenchmarkDivconstU16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u16res = uint16(i) / 7
	}
}

func BenchmarkModconstU16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u16res = uint16(i) % 7
	}
}

func BenchmarkDivisibleconstU16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = uint16(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstU16(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u16res = uint16(i) / 7
		boolres = uint16(i)%7 == 0
	}
}

var i8res int8

func BenchmarkDivconstI8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i8res = int8(i) / 7
	}
}

func BenchmarkModconstI8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i8res = int8(i) % 7
	}
}

func BenchmarkDivisiblePow2constI8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int8(i)%16 == 0
	}
}

func BenchmarkDivisibleconstI8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = int8(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstI8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		i8res = int8(i) / 7
		boolres = int8(i)%7 == 0
	}
}

var u8res uint8

func BenchmarkDivconstU8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u8res = uint8(i) / 7
	}
}

func BenchmarkModconstU8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u8res = uint8(i) % 7
	}
}

func BenchmarkDivisibleconstU8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		boolres = uint8(i)%7 == 0
	}
}

func BenchmarkDivisibleWDivconstU8(b *testing.B) {
	for i := 0; i < b.N; i++ {
		u8res = uint8(i) / 7
		boolres = uint8(i)%7 == 0
	}
}