OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f32 / util_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 f32
6
7 import (
8         "math"
9         "testing"
10 )
11
12 var (
13         nan = float32(math.NaN())
14         inf = float32(math.Inf(1))
15 )
16
17 func same(x, y float32) bool {
18         a, b := float64(x), float64(y)
19         return a == b || (math.IsNaN(a) && math.IsNaN(b))
20 }
21
22 func same64(a, b float64) bool {
23         return a == b || (math.IsNaN(a) && math.IsNaN(b))
24 }
25
26 func guardVector(v []float32, g float32, gdLn int) (guarded []float32) {
27         guarded = make([]float32, len(v)+gdLn*2)
28         copy(guarded[gdLn:], v)
29         for i := 0; i < gdLn; i++ {
30                 guarded[i] = g
31                 guarded[len(guarded)-1-i] = g
32         }
33         return guarded
34 }
35
36 func isValidGuard(v []float32, g float32, gdLn int) bool {
37         for i := 0; i < gdLn; i++ {
38                 if !same(v[i], g) || !same(v[len(v)-1-i], g) {
39                         return false
40                 }
41         }
42         return true
43 }
44
45 func guardIncVector(vec []float32, gdVal float32, inc, gdLen int) (guarded []float32) {
46         if inc < 0 {
47                 inc = -inc
48         }
49         inrLen := len(vec) * inc
50         guarded = make([]float32, inrLen+gdLen*2)
51         for i := range guarded {
52                 guarded[i] = gdVal
53         }
54         for i, v := range vec {
55                 guarded[gdLen+i*inc] = v
56         }
57         return guarded
58 }
59
60 func checkValidIncGuard(t *testing.T, v []float32, g float32, inc, gdLn int) {
61         srcLn := len(v) - 2*gdLn
62         for i := range v {
63                 switch {
64                 case same(v[i], g):
65                         // Correct value
66                 case i < gdLn:
67                         t.Error("Front guard violated at", i, v[:gdLn])
68                 case i > gdLn+srcLn:
69                         t.Error("Back guard violated at", i-gdLn-srcLn, v[gdLn+srcLn:])
70                 case (i-gdLn)%inc == 0 && (i-gdLn)/inc < len(v):
71                 default:
72                         t.Error("Internal guard violated at", i-gdLn, v[gdLn:gdLn+srcLn])
73                 }
74         }
75 }
76
77 var ( // Offset sets for testing alignment handling in Unitary assembly functions.
78         align2 = newIncSet(0, 1, 2, 3)
79         align3 = newIncToSet(0, 1, 2, 3)
80 )
81
82 type incSet struct {
83         x, y int
84 }
85
86 // genInc will generate all (x,y) combinations of the input increment set.
87 func newIncSet(inc ...int) []incSet {
88         n := len(inc)
89         is := make([]incSet, n*n)
90         for x := range inc {
91                 for y := range inc {
92                         is[x*n+y] = incSet{inc[x], inc[y]}
93                 }
94         }
95         return is
96 }
97
98 type incToSet struct {
99         dst, x, y int
100 }
101
102 // genIncTo will generate all (dst,x,y) combinations of the input increment set.
103 func newIncToSet(inc ...int) []incToSet {
104         n := len(inc)
105         is := make([]incToSet, n*n*n)
106         for i, dst := range inc {
107                 for x := range inc {
108                         for y := range inc {
109                                 is[i*n*n+x*n+y] = incToSet{dst, inc[x], inc[y]}
110                         }
111                 }
112         }
113         return is
114 }