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.
11 "golang.org/x/exp/rand"
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) {
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 {
35 for i, d := range data {
38 return v, whole[:guard], whole[len(whole)-guard:]
41 // allNaN returns true if x contains only NaN values, and false otherwise.
42 func allNaN(x []float64) bool {
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 {
57 for i, v := range ref {
58 if !same(x[i*inc], v) {
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 {
72 if i%inc != 0 && !math.IsNaN(v) {
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++ {
86 guarded[len(guarded)-1-i] = gdVal
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) {
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) {
108 inrLen := len(vec) * inc
109 guarded = make([]float64, inrLen+gdLen*2)
110 for i := range guarded {
113 for i, v := range vec {
114 guarded[gdLen+i*inc] = v
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
124 case same(vec[i], gdVal):
126 case (i-gdLen)%inc == 0 && (i-gdLen)/inc < len(vec):
127 // Ignore input values
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:])
133 t.Errorf("Internal guard violated at %d %v", i-gdLen, vec[gdLen:gdLen+srcLn])
138 // same tests for nan-aware equality.
139 func same(a, b float64) bool {
140 return a == b || (math.IsNaN(a) && math.IsNaN(b))
143 var ( // Offset sets for testing alignment handling in Unitary assembly functions.
145 align2 = newIncSet(0, 1)
146 align3 = newIncToSet(0, 1)
153 // genInc will generate all (x,y) combinations of the input increment set.
154 func newIncSet(inc ...int) []incSet {
156 is := make([]incSet, n*n)
159 is[x*n+y] = incSet{inc[x], inc[y]}
165 type incToSet struct {
169 // genIncTo will generate all (dst,x,y) combinations of the input increment set.
170 func newIncToSet(inc ...int) []incToSet {
172 is := make([]incToSet, n*n*n)
173 for i, dst := range inc {
176 is[i*n*n+x*n+y] = incToSet{dst, inc[x], inc[y]}
183 var benchSink []float64
185 func randomSlice(n, inc int) []float64 {
189 x := make([]float64, (n-1)*inc+1)
191 x[i] = rand.Float64()
196 func randSlice(n, inc int, r *rand.Rand) []float64 {
200 x := make([]float64, (n-1)*inc+1)