1// Copyright 2012 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package runtime_test
6
7import (
8	"runtime"
9	"testing"
10)
11
12// arm soft division benchmarks adapted from
13// https://ridiculousfish.com/files/division_benchmarks.tar.gz
14
15const numeratorsSize = 1 << 21
16
17var numerators = randomNumerators()
18
19type randstate struct {
20	hi, lo uint32
21}
22
23func (r *randstate) rand() uint32 {
24	r.hi = r.hi<<16 + r.hi>>16
25	r.hi += r.lo
26	r.lo += r.hi
27	return r.hi
28}
29
30func randomNumerators() []uint32 {
31	numerators := make([]uint32, numeratorsSize)
32	random := &randstate{2147483563, 2147483563 ^ 0x49616E42}
33	for i := range numerators {
34		numerators[i] = random.rand()
35	}
36	return numerators
37}
38
39func bmUint32Div(divisor uint32, b *testing.B) {
40	var sum uint32
41	for i := 0; i < b.N; i++ {
42		sum += numerators[i&(numeratorsSize-1)] / divisor
43	}
44}
45
46func BenchmarkUint32Div7(b *testing.B)         { bmUint32Div(7, b) }
47func BenchmarkUint32Div37(b *testing.B)        { bmUint32Div(37, b) }
48func BenchmarkUint32Div123(b *testing.B)       { bmUint32Div(123, b) }
49func BenchmarkUint32Div763(b *testing.B)       { bmUint32Div(763, b) }
50func BenchmarkUint32Div1247(b *testing.B)      { bmUint32Div(1247, b) }
51func BenchmarkUint32Div9305(b *testing.B)      { bmUint32Div(9305, b) }
52func BenchmarkUint32Div13307(b *testing.B)     { bmUint32Div(13307, b) }
53func BenchmarkUint32Div52513(b *testing.B)     { bmUint32Div(52513, b) }
54func BenchmarkUint32Div60978747(b *testing.B)  { bmUint32Div(60978747, b) }
55func BenchmarkUint32Div106956295(b *testing.B) { bmUint32Div(106956295, b) }
56
57func bmUint32Mod(divisor uint32, b *testing.B) {
58	var sum uint32
59	for i := 0; i < b.N; i++ {
60		sum += numerators[i&(numeratorsSize-1)] % divisor
61	}
62}
63
64func BenchmarkUint32Mod7(b *testing.B)         { bmUint32Mod(7, b) }
65func BenchmarkUint32Mod37(b *testing.B)        { bmUint32Mod(37, b) }
66func BenchmarkUint32Mod123(b *testing.B)       { bmUint32Mod(123, b) }
67func BenchmarkUint32Mod763(b *testing.B)       { bmUint32Mod(763, b) }
68func BenchmarkUint32Mod1247(b *testing.B)      { bmUint32Mod(1247, b) }
69func BenchmarkUint32Mod9305(b *testing.B)      { bmUint32Mod(9305, b) }
70func BenchmarkUint32Mod13307(b *testing.B)     { bmUint32Mod(13307, b) }
71func BenchmarkUint32Mod52513(b *testing.B)     { bmUint32Mod(52513, b) }
72func BenchmarkUint32Mod60978747(b *testing.B)  { bmUint32Mod(60978747, b) }
73func BenchmarkUint32Mod106956295(b *testing.B) { bmUint32Mod(106956295, b) }
74
75func TestUsplit(t *testing.T) {
76	var den uint32 = 1000000
77	for _, x := range []uint32{0, 1, 999999, 1000000, 1010101, 0xFFFFFFFF} {
78		q1, r1 := runtime.Usplit(x)
79		q2, r2 := x/den, x%den
80		if q1 != q2 || r1 != r2 {
81			t.Errorf("%d/1e6, %d%%1e6 = %d, %d, want %d, %d", x, x, q1, r1, q2, r2)
82		}
83	}
84}
85
86//go:noinline
87func armFloatWrite(a *[129]float64) {
88	// This used to miscompile on arm5.
89	// The offset is too big to fit in a load.
90	// So the code does:
91	//   ldr     r0, [sp, #8]
92	//   bl      6f690 <_sfloat>
93	//   ldr     fp, [pc, #32]   ; (address of 128.0)
94	//   vldr    d0, [fp]
95	//   ldr     fp, [pc, #28]   ; (1024)
96	//   add     fp, fp, r0
97	//   vstr    d0, [fp]
98	// The software floating-point emulator gives up on the add.
99	// This causes the store to not work.
100	// See issue 15440.
101	a[128] = 128.0
102}
103func TestArmFloatBigOffsetWrite(t *testing.T) {
104	var a [129]float64
105	for i := 0; i < 128; i++ {
106		a[i] = float64(i)
107	}
108	armFloatWrite(&a)
109	for i, x := range a {
110		if x != float64(i) {
111			t.Errorf("bad entry %d:%f\n", i, x)
112		}
113	}
114}
115
116//go:noinline
117func armFloatRead(a *[129]float64) float64 {
118	return a[128]
119}
120func TestArmFloatBigOffsetRead(t *testing.T) {
121	var a [129]float64
122	for i := 0; i < 129; i++ {
123		a[i] = float64(i)
124	}
125	if x := armFloatRead(&a); x != 128.0 {
126		t.Errorf("bad value %f\n", x)
127	}
128}
129