OSDN Git Service

new repo
[bytom/vapor.git] / vendor / gonum.org / v1 / gonum / internal / asm / c128 / stubs_test.go
1 // Copyright ©2016 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 c128
6
7 import (
8         "fmt"
9         "testing"
10 )
11
12 var tests = []struct {
13         incX, incY, incDst int
14         ix, iy, idst       uintptr
15         a                  complex128
16         dst, x, y          []complex128
17         ex                 []complex128
18 }{
19         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
20                 a:   1 + 1i,
21                 dst: []complex128{5},
22                 x:   []complex128{1},
23                 y:   []complex128{1i},
24                 ex:  []complex128{1 + 2i}},
25         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
26                 a:   1 + 2i,
27                 dst: []complex128{0, 0, 0},
28                 x:   []complex128{0, 0, 0},
29                 y:   []complex128{1, 1, 1},
30                 ex:  []complex128{1, 1, 1}},
31         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
32                 a:   1 + 2i,
33                 dst: []complex128{0, 0, 0},
34                 x:   []complex128{0, 0},
35                 y:   []complex128{1, 1, 1},
36                 ex:  []complex128{1, 1}},
37         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
38                 a:   1 + 2i,
39                 dst: []complex128{1i, 1i, 1i},
40                 x:   []complex128{1i, 1i, 1i},
41                 y:   []complex128{1, 2, 1},
42                 ex:  []complex128{-1 + 1i, 1i, -1 + 1i}},
43         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
44                 a:   -1i,
45                 dst: []complex128{1i, 1i, 1i},
46                 x:   []complex128{1i, 1i, 1i},
47                 y:   []complex128{1, 2, 1},
48                 ex:  []complex128{2, 3, 2}},
49         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
50                 a:   -1i,
51                 dst: []complex128{1i, 1i, 1i},
52                 x:   []complex128{1i, 1i, 1i, 1i, 1i}[1:4],
53                 y:   []complex128{1, 1, 2, 1, 1}[1:4],
54                 ex:  []complex128{2, 3, 2}},
55         {incX: 2, incY: 4, incDst: 3, ix: 0, iy: 0, idst: 0,
56                 a:   -2,
57                 dst: []complex128{1i, 1i, 1i, 1i, 1i},
58                 x:   []complex128{2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i},
59                 y:   []complex128{1, 1, 2, 1, 1},
60                 ex:  []complex128{-3 - 2i, -3 - 2i, -2 - 2i, -3 - 2i, -3 - 2i}},
61         // Run big test twice, once aligned once unaligned.
62         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
63                 a:   1 - 1i,
64                 dst: make([]complex128, 10),
65                 x:   []complex128{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
66                 y:   []complex128{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
67                 ex:  []complex128{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
68         {incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
69                 a:   1 - 1i,
70                 dst: make([]complex128, 10),
71                 x:   []complex128{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
72                 y:   []complex128{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
73                 ex:  []complex128{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
74         {incX: -2, incY: -2, incDst: -3, ix: 18, iy: 18, idst: 27,
75                 a:   1 - 1i,
76                 dst: make([]complex128, 10),
77                 x:   []complex128{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
78                 y:   []complex128{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
79                 ex:  []complex128{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
80         {incX: -2, incY: 2, incDst: -3, ix: 18, iy: 0, idst: 27,
81                 a:   1 - 1i,
82                 dst: make([]complex128, 10),
83                 x:   []complex128{1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i, 1i},
84                 y:   []complex128{1, 1, 2, 1, 1, 1, 1, 2, 1, 1},
85                 ex:  []complex128{2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 2 + 1i, 3 + 1i, 2 + 1i, 2 + 1i}},
86 }
87
88 func TestAxpyUnitary(t *testing.T) {
89         const xGdVal, yGdVal = 1, 1
90         for cas, test := range tests {
91                 xgLn, ygLn := 4+cas%2, 4+cas%3
92                 test.x, test.y = guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
93                 x, y := test.x[xgLn:len(test.x)-xgLn], test.y[ygLn:len(test.y)-ygLn]
94                 AxpyUnitary(test.a, x, y)
95                 for i := range test.ex {
96                         if y[i] != test.ex[i] {
97                                 t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i], test.ex[i])
98                         }
99                 }
100                 if !isValidGuard(test.x, xGdVal, xgLn) {
101                         t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xgLn], test.x[len(test.x)-xgLn:])
102                 }
103                 if !isValidGuard(test.y, yGdVal, ygLn) {
104                         t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:ygLn], test.y[len(test.y)-ygLn:])
105                 }
106         }
107 }
108
109 func TestAxpyUnitaryTo(t *testing.T) {
110         const xGdVal, yGdVal, dstGdVal = 1, 1, 0
111         for cas, test := range tests {
112                 xgLn, ygLn := 4+cas%2, 4+cas%3
113                 test.x, test.y = guardVector(test.x, xGdVal, xgLn), guardVector(test.y, yGdVal, ygLn)
114                 test.dst = guardVector(test.dst, dstGdVal, xgLn)
115                 x, y := test.x[xgLn:len(test.x)-xgLn], test.y[ygLn:len(test.y)-ygLn]
116                 dst := test.dst[xgLn : len(test.dst)-xgLn]
117                 AxpyUnitaryTo(dst, test.a, x, y)
118                 for i := range test.ex {
119                         if dst[i] != test.ex[i] {
120                                 t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i], test.ex[i])
121                         }
122                 }
123                 if !isValidGuard(test.x, xGdVal, xgLn) {
124                         t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xgLn], test.x[len(test.x)-xgLn:])
125                 }
126                 if !isValidGuard(test.y, yGdVal, ygLn) {
127                         t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:ygLn], test.y[len(test.y)-ygLn:])
128                 }
129                 if !isValidGuard(test.dst, dstGdVal, xgLn) {
130                         t.Errorf("Test %d Guard violated in dst vector %v %v", cas, test.dst[:xgLn], test.dst[len(test.dst)-xgLn:])
131                 }
132
133         }
134 }
135
136 func TestAxpyInc(t *testing.T) {
137         const xGdVal, yGdVal = 1, 1
138         for cas, test := range tests {
139                 xgLn, ygLn := 4+cas%2, 4+cas%3
140                 test.x, test.y = guardIncVector(test.x, xGdVal, test.incX, xgLn), guardIncVector(test.y, yGdVal, test.incY, ygLn)
141                 x, y := test.x[xgLn:len(test.x)-xgLn], test.y[ygLn:len(test.y)-ygLn]
142                 AxpyInc(test.a, x, y, uintptr(len(test.ex)), uintptr(test.incX), uintptr(test.incY), test.ix, test.iy)
143                 for i := range test.ex {
144                         if y[int(test.iy)+i*int(test.incY)] != test.ex[i] {
145                                 t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i*int(test.incY)], test.ex[i])
146                         }
147                 }
148                 checkValidIncGuard(t, test.x, xGdVal, test.incX, xgLn)
149                 checkValidIncGuard(t, test.y, yGdVal, test.incY, ygLn)
150         }
151 }
152
153 func TestAxpyIncTo(t *testing.T) {
154         const xGdVal, yGdVal, dstGdVal = 1, 1, 0
155         for cas, test := range tests {
156                 xgLn, ygLn := 4+cas%2, 4+cas%3
157                 test.x, test.y = guardIncVector(test.x, xGdVal, test.incX, xgLn), guardIncVector(test.y, yGdVal, test.incY, ygLn)
158                 test.dst = guardIncVector(test.dst, dstGdVal, test.incDst, xgLn)
159                 x, y := test.x[xgLn:len(test.x)-xgLn], test.y[ygLn:len(test.y)-ygLn]
160                 dst := test.dst[xgLn : len(test.dst)-xgLn]
161                 AxpyIncTo(dst, uintptr(test.incDst), test.idst, test.a, x, y, uintptr(len(test.ex)), uintptr(test.incX), uintptr(test.incY), test.ix, test.iy)
162                 for i := range test.ex {
163                         if dst[int(test.idst)+i*int(test.incDst)] != test.ex[i] {
164                                 t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i*int(test.incDst)], test.ex[i])
165                         }
166                 }
167                 checkValidIncGuard(t, test.x, xGdVal, test.incX, xgLn)
168                 checkValidIncGuard(t, test.y, yGdVal, test.incY, ygLn)
169                 checkValidIncGuard(t, test.dst, dstGdVal, test.incDst, xgLn)
170         }
171 }
172
173 var dscalTests = []struct {
174         alpha float64
175         x     []complex128
176         want  []complex128
177 }{
178         {
179                 alpha: 0,
180                 x:     []complex128{},
181                 want:  []complex128{},
182         },
183         {
184                 alpha: 1,
185                 x:     []complex128{1 + 2i},
186                 want:  []complex128{1 + 2i},
187         },
188         {
189                 alpha: 2,
190                 x:     []complex128{1 + 2i},
191                 want:  []complex128{2 + 4i},
192         },
193         {
194                 alpha: 2,
195                 x:     []complex128{1 + 2i, 3 + 5i, 6 + 11i, 12 - 23i},
196                 want:  []complex128{2 + 4i, 6 + 10i, 12 + 22i, 24 - 46i},
197         },
198         {
199                 alpha: 3,
200                 x:     []complex128{1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i},
201                 want:  []complex128{3 + 6i, 15 + 12i, 9 + 18i, 24 + 36i, -9 - 6i, -15 + 15i},
202         },
203         {
204                 alpha: 5,
205                 x:     []complex128{1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i, 1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i},
206                 want:  []complex128{5 + 10i, 25 + 20i, 15 + 30i, 40 + 60i, -15 - 10i, -25 + 25i, 5 + 10i, 25 + 20i, 15 + 30i, 40 + 60i, -15 - 10i, -25 + 25i},
207         },
208 }
209
210 func TestDscalUnitary(t *testing.T) {
211         const xGdVal = -0.5
212         for i, test := range dscalTests {
213                 for _, align := range align1 {
214                         prefix := fmt.Sprintf("Test %v (x:%v)", i, align)
215                         xgLn := 4 + align
216                         xg := guardVector(test.x, xGdVal, xgLn)
217                         x := xg[xgLn : len(xg)-xgLn]
218
219                         DscalUnitary(test.alpha, x)
220
221                         for i := range test.want {
222                                 if !same(x[i], test.want[i]) {
223                                         t.Errorf(msgVal, prefix, i, x[i], test.want[i])
224                                 }
225                         }
226                         if !isValidGuard(xg, xGdVal, xgLn) {
227                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
228                         }
229                 }
230         }
231 }
232
233 func TestDscalInc(t *testing.T) {
234         const xGdVal = -0.5
235         gdLn := 4
236         for i, test := range dscalTests {
237                 n := len(test.x)
238                 for _, incX := range []int{1, 2, 3, 4, 7, 10} {
239                         prefix := fmt.Sprintf("Test %v (x:%v)", i, incX)
240                         xg := guardIncVector(test.x, xGdVal, incX, gdLn)
241                         x := xg[gdLn : len(xg)-gdLn]
242
243                         DscalInc(test.alpha, x, uintptr(n), uintptr(incX))
244
245                         for i := range test.want {
246                                 if !same(x[i*incX], test.want[i]) {
247                                         t.Errorf(msgVal, prefix, i, x[i*incX], test.want[i])
248                                 }
249                         }
250                         checkValidIncGuard(t, xg, xGdVal, incX, gdLn)
251                 }
252         }
253 }
254
255 var scalTests = []struct {
256         alpha complex128
257         x     []complex128
258         want  []complex128
259 }{
260         {
261                 alpha: 0,
262                 x:     []complex128{},
263                 want:  []complex128{},
264         },
265         {
266                 alpha: 1 + 1i,
267                 x:     []complex128{1 + 2i},
268                 want:  []complex128{-1 + 3i},
269         },
270         {
271                 alpha: 2 + 3i,
272                 x:     []complex128{1 + 2i},
273                 want:  []complex128{-4 + 7i},
274         },
275         {
276                 alpha: 2 - 4i,
277                 x:     []complex128{1 + 2i},
278                 want:  []complex128{10},
279         },
280         {
281                 alpha: 2 + 8i,
282                 x:     []complex128{1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i},
283                 want:  []complex128{-14 + 12i, -22 + 48i, -42 + 36i, -80 + 88i, 10 - 28i, -50 - 30i},
284         },
285         {
286                 alpha: 5 - 10i,
287                 x:     []complex128{1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i, 1 + 2i, 5 + 4i, 3 + 6i, 8 + 12i, -3 - 2i, -5 + 5i},
288                 want:  []complex128{25, 65 - 30i, 75, 160 - 20i, -35 + 20i, 25 + 75i, 25, 65 - 30i, 75, 160 - 20i, -35 + 20i, 25 + 75i},
289         },
290 }
291
292 func TestScalUnitary(t *testing.T) {
293         const xGdVal = -0.5
294         for i, test := range scalTests {
295                 for _, align := range align1 {
296                         prefix := fmt.Sprintf("Test %v (x:%v)", i, align)
297                         xgLn := 4 + align
298                         xg := guardVector(test.x, xGdVal, xgLn)
299                         x := xg[xgLn : len(xg)-xgLn]
300
301                         ScalUnitary(test.alpha, x)
302
303                         for i := range test.want {
304                                 if !same(x[i], test.want[i]) {
305                                         t.Errorf(msgVal, prefix, i, x[i], test.want[i])
306                                 }
307                         }
308                         if !isValidGuard(xg, xGdVal, xgLn) {
309                                 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:])
310                         }
311                 }
312         }
313 }
314
315 func TestScalInc(t *testing.T) {
316         const xGdVal = -0.5
317         gdLn := 4
318         for i, test := range scalTests {
319                 n := len(test.x)
320                 for _, inc := range []int{1, 2, 3, 4, 7, 10} {
321                         prefix := fmt.Sprintf("Test %v (x:%v)", i, inc)
322                         xg := guardIncVector(test.x, xGdVal, inc, gdLn)
323                         x := xg[gdLn : len(xg)-gdLn]
324
325                         ScalInc(test.alpha, x, uintptr(n), uintptr(inc))
326
327                         for i := range test.want {
328                                 if !same(x[i*inc], test.want[i]) {
329                                         t.Errorf(msgVal, prefix, i, x[i*inc], test.want[i])
330                                 }
331                         }
332                         checkValidIncGuard(t, xg, xGdVal, inc, gdLn)
333                 }
334         }
335 }