OSDN Git Service

test (#52)
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f64 / asm_test.go
1 // Copyright ©2015 The Gonum 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
5 package f64
6
7 import (
8         "math"
9         "testing"
10
11         "golang.org/x/exp/rand"
12 )
13
14 var (
15         nan = math.NaN()
16         inf = math.Inf(1)
17 )
18
19 // newGuardedVector allocates a new slice and returns it as three subslices.
20 // v is a strided vector that contains elements of data at indices i*inc and
21 // NaN elsewhere. frontGuard and backGuard are filled with NaN values, and
22 // their backing arrays are directly adjacent to v in memory. The three slices
23 // can be used to detect invalid memory reads and writes.
24 func newGuardedVector(data []float64, inc int) (v, frontGuard, backGuard []float64) {
25         if inc < 0 {
26                 inc = -inc
27         }
28         guard := 2 * inc
29         size := (len(data)-1)*inc + 1
30         whole := make([]float64, size+2*guard)
31         v = whole[guard : len(whole)-guard]
32         for i := range whole {
33                 whole[i] = math.NaN()
34         }
35         for i, d := range data {
36                 v[i*inc] = d
37         }
38         return v, whole[:guard], whole[len(whole)-guard:]
39 }
40
41 // allNaN returns true if x contains only NaN values, and false otherwise.
42 func allNaN(x []float64) bool {
43         for _, v := range x {
44                 if !math.IsNaN(v) {
45                         return false
46                 }
47         }
48         return true
49 }
50
51 // equalStrided returns true if the strided vector x contains elements of the
52 // dense vector ref at indices i*inc, false otherwise.
53 func equalStrided(ref, x []float64, inc int) bool {
54         if inc < 0 {
55                 inc = -inc
56         }
57         for i, v := range ref {
58                 if !same(x[i*inc], v) {
59                         return false
60                 }
61         }
62         return true
63 }
64
65 // nonStridedWrite returns false if all elements of x at non-stride indices are
66 // equal to NaN, true otherwise.
67 func nonStridedWrite(x []float64, inc int) bool {
68         if inc < 0 {
69                 inc = -inc
70         }
71         for i, v := range x {
72                 if i%inc != 0 && !math.IsNaN(v) {
73                         return true
74                 }
75         }
76         return false
77 }
78
79 // guardVector copies the source vector (vec) into a new slice with guards.
80 // Guards guarded[:gdLn] and guarded[len-gdLn:] will be filled with sigil value gdVal.
81 func guardVector(vec []float64, gdVal float64, gdLn int) (guarded []float64) {
82         guarded = make([]float64, len(vec)+gdLn*2)
83         copy(guarded[gdLn:], vec)
84         for i := 0; i < gdLn; i++ {
85                 guarded[i] = gdVal
86                 guarded[len(guarded)-1-i] = gdVal
87         }
88         return guarded
89 }
90
91 // isValidGuard will test for violated guards, generated by guardVector.
92 func isValidGuard(vec []float64, gdVal float64, gdLn int) bool {
93         for i := 0; i < gdLn; i++ {
94                 if !same(vec[i], gdVal) || !same(vec[len(vec)-1-i], gdVal) {
95                         return false
96                 }
97         }
98         return true
99 }
100
101 // guardIncVector copies the source vector (vec) into a new incremented slice with guards.
102 // End guards will be length gdLen.
103 // Internal and end guards will be filled with sigil value gdVal.
104 func guardIncVector(vec []float64, gdVal float64, inc, gdLen int) (guarded []float64) {
105         if inc < 0 {
106                 inc = -inc
107         }
108         inrLen := len(vec) * inc
109         guarded = make([]float64, inrLen+gdLen*2)
110         for i := range guarded {
111                 guarded[i] = gdVal
112         }
113         for i, v := range vec {
114                 guarded[gdLen+i*inc] = v
115         }
116         return guarded
117 }
118
119 // checkValidIncGuard will test for violated guards, generated by guardIncVector
120 func checkValidIncGuard(t *testing.T, vec []float64, gdVal float64, inc, gdLen int) {
121         srcLn := len(vec) - 2*gdLen
122         for i := range vec {
123                 switch {
124                 case same(vec[i], gdVal):
125                         // Correct value
126                 case (i-gdLen)%inc == 0 && (i-gdLen)/inc < len(vec):
127                         // Ignore input values
128                 case i < gdLen:
129                         t.Errorf("Front guard violated at %d %v", i, vec[:gdLen])
130                 case i > gdLen+srcLn:
131                         t.Errorf("Back guard violated at %d %v", i-gdLen-srcLn, vec[gdLen+srcLn:])
132                 default:
133                         t.Errorf("Internal guard violated at %d %v", i-gdLen, vec[gdLen:gdLen+srcLn])
134                 }
135         }
136 }
137
138 // same tests for nan-aware equality.
139 func same(a, b float64) bool {
140         return a == b || (math.IsNaN(a) && math.IsNaN(b))
141 }
142
143 var ( // Offset sets for testing alignment handling in Unitary assembly functions.
144         align1 = []int{0, 1}
145         align2 = newIncSet(0, 1)
146         align3 = newIncToSet(0, 1)
147 )
148
149 type incSet struct {
150         x, y int
151 }
152
153 // genInc will generate all (x,y) combinations of the input increment set.
154 func newIncSet(inc ...int) []incSet {
155         n := len(inc)
156         is := make([]incSet, n*n)
157         for x := range inc {
158                 for y := range inc {
159                         is[x*n+y] = incSet{inc[x], inc[y]}
160                 }
161         }
162         return is
163 }
164
165 type incToSet struct {
166         dst, x, y int
167 }
168
169 // genIncTo will generate all (dst,x,y) combinations of the input increment set.
170 func newIncToSet(inc ...int) []incToSet {
171         n := len(inc)
172         is := make([]incToSet, n*n*n)
173         for i, dst := range inc {
174                 for x := range inc {
175                         for y := range inc {
176                                 is[i*n*n+x*n+y] = incToSet{dst, inc[x], inc[y]}
177                         }
178                 }
179         }
180         return is
181 }
182
183 var benchSink []float64
184
185 func randomSlice(n, inc int) []float64 {
186         if inc < 0 {
187                 inc = -inc
188         }
189         x := make([]float64, (n-1)*inc+1)
190         for i := range x {
191                 x[i] = rand.Float64()
192         }
193         return x
194 }
195
196 func randSlice(n, inc int, r *rand.Rand) []float64 {
197         if inc < 0 {
198                 inc = -inc
199         }
200         x := make([]float64, (n-1)*inc+1)
201         for i := range x {
202                 x[i] = r.Float64()
203         }
204         return x
205 }