OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / f32 / dot_test.go
1 // Copyright ©2017 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         "fmt"
9         "math"
10         "testing"
11 )
12
13 const (
14         msgRes   = "%v: unexpected result Got: %v Expected: %v"
15         msgGuard = "%v: Guard violated in %s vector %v %v"
16 )
17
18 var dotTests = []struct {
19         x, y     []float32
20         sWant    float32 // single-precision
21         dWant    float64 // double-precision
22         sWantRev float32 // single-precision increment
23         dWantRev float64 // double-precision increment
24         n        int
25         ix, iy   int
26 }{
27         { // 0
28                 x:     []float32{},
29                 y:     []float32{},
30                 n:     0,
31                 sWant: 0, dWant: 0,
32                 sWantRev: 0, dWantRev: 0,
33                 ix: 0, iy: 0,
34         },
35         { // 1
36                 x:     []float32{0},
37                 y:     []float32{0},
38                 n:     1,
39                 sWant: 0, dWant: 0,
40                 sWantRev: 0, dWantRev: 0,
41                 ix: 0, iy: 0,
42         },
43         { // 2
44                 x:     []float32{1},
45                 y:     []float32{1},
46                 n:     1,
47                 sWant: 1, dWant: 1,
48                 sWantRev: 1, dWantRev: 1,
49                 ix: 0, iy: 0,
50         },
51         { // 3
52                 x:     []float32{1, 2, 3, 4, 5, 6, 7, 8},
53                 y:     []float32{2, 2, 2, 2, 2, 2, 2, 2},
54                 n:     8,
55                 sWant: 72, dWant: 72,
56                 sWantRev: 72, dWantRev: 72,
57                 ix: 1, iy: 1,
58         },
59         { // 4
60                 x:     []float32{math.MaxFloat32},
61                 y:     []float32{2},
62                 n:     1,
63                 sWant: inf, dWant: 2 * float64(math.MaxFloat32),
64                 sWantRev: inf, dWantRev: 2 * float64(math.MaxFloat32),
65                 ix: 0, iy: 0,
66         },
67         { // 5
68                 x:     []float32{1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2},
69                 y:     []float32{3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2},
70                 n:     20,
71                 sWant: 70, dWant: 70,
72                 sWantRev: 80, dWantRev: 80,
73                 ix: 0, iy: 0,
74         },
75 }
76
77 func TestDotUnitary(t *testing.T) {
78         const xGdVal, yGdVal = 0.5, 0.25
79         for i, test := range dotTests {
80                 for _, align := range align2 {
81                         prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
82                         xgLn, ygLn := 8+align.x, 8+align.y
83                         xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
84                         x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
85                         res := DotUnitary(x, y)
86                         if !same(res, test.sWant) {
87                                 t.Errorf(msgRes, prefix, res, test.sWant)
88                         }
89                         if !isValidGuard(xg, xGdVal, xgLn) {
90                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
91                         }
92                         if !isValidGuard(yg, yGdVal, ygLn) {
93                                 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
94                         }
95                 }
96         }
97 }
98
99 func TestDotInc(t *testing.T) {
100         const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
101         for i, test := range dotTests {
102                 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
103                         xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
104                         x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
105                         want := test.sWant
106                         var ix, iy int
107                         if inc.x < 0 {
108                                 ix = -inc.x * (test.n - 1)
109                         }
110                         if inc.y < 0 {
111                                 iy = -inc.y * (test.n - 1)
112                         }
113                         if inc.x*inc.y < 0 {
114                                 want = test.sWantRev
115                         }
116                         prefix := fmt.Sprintf("Test %v (x:%v y:%v) (ix:%v iy:%v)", i, inc.x, inc.y, ix, iy)
117                         res := DotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
118                         if !same(res, want) {
119                                 t.Errorf(msgRes, prefix, res, want)
120                         }
121                         checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
122                         checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
123                 }
124         }
125 }
126
127 func TestDdotUnitary(t *testing.T) {
128         const xGdVal, yGdVal = 0.5, 0.25
129         for i, test := range dotTests {
130                 for _, align := range align2 {
131                         prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y)
132                         xgLn, ygLn := 8+align.x, 8+align.y
133                         xg, yg := guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
134                         x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn]
135                         res := DdotUnitary(x, y)
136                         if !same64(res, test.dWant) {
137                                 t.Errorf(msgRes, prefix, res, test.dWant)
138                         }
139                         if !isValidGuard(xg, xGdVal, xgLn) {
140                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
141                         }
142                         if !isValidGuard(yg, yGdVal, ygLn) {
143                                 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:])
144                         }
145                 }
146         }
147 }
148
149 func TestDdotInc(t *testing.T) {
150         const xGdVal, yGdVal, gdLn = 0.5, 0.25, 8
151         for i, test := range dotTests {
152                 for _, inc := range newIncSet(1, 2, 3, 4, 7, 10, -1, -2, -5, -10) {
153                         prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, inc.x, inc.y)
154                         xg, yg := guardIncVector(test.x, xGdVal, inc.x, gdLn), guardIncVector(test.y, yGdVal, inc.y, gdLn)
155                         x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn]
156                         want := test.dWant
157                         var ix, iy int
158                         if inc.x < 0 {
159                                 ix = -inc.x * (test.n - 1)
160                         }
161                         if inc.y < 0 {
162                                 iy = -inc.y * (test.n - 1)
163                         }
164                         if inc.x*inc.y < 0 {
165                                 want = test.dWantRev
166                         }
167                         res := DdotInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy))
168                         if !same64(res, want) {
169                                 t.Errorf(msgRes, prefix, res, want)
170                         }
171                         checkValidIncGuard(t, xg, xGdVal, inc.x, gdLn)
172                         checkValidIncGuard(t, yg, yGdVal, inc.y, gdLn)
173                 }
174         }
175 }