github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/internal/asm/c128/dot_test.go (about) 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 c128 6 7 import ( 8 "fmt" 9 "testing" 10 11 "github.com/jingcheng-WU/gonum/cmplxs/cscalar" 12 ) 13 14 var dotTests = []struct { 15 x, y []complex128 16 wantu, wantc complex128 17 wantuRev, wantcRev complex128 18 n int 19 }{ 20 { 21 x: []complex128{}, 22 y: []complex128{}, 23 n: 0, 24 wantu: 0, wantc: 0, 25 wantuRev: 0, wantcRev: 0, 26 }, 27 { 28 x: []complex128{1 + 1i}, 29 y: []complex128{1 + 1i}, 30 n: 1, 31 wantu: 0 + 2i, wantc: 2, 32 wantuRev: 0 + 2i, wantcRev: 2, 33 }, 34 { 35 x: []complex128{1 + 2i}, 36 y: []complex128{1 + 1i}, 37 n: 1, 38 wantu: -1 + 3i, wantc: 3 - 1i, 39 wantuRev: -1 + 3i, wantcRev: 3 - 1i, 40 }, 41 { 42 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i}, 43 y: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i}, 44 n: 10, 45 wantu: -210 + 2860i, wantc: 2870 + 0i, 46 wantuRev: -210 + 1540i, wantcRev: 1550 + 0i, 47 }, 48 { 49 x: []complex128{1 + 1i, 1 + 1i, 1 + 2i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 3i, 1 + 1i, 1 + 1i, 1 + 4i}, 50 y: []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}, 51 n: 10, 52 wantu: -22 + 36i, wantc: 42 + 4i, 53 wantuRev: -22 + 36i, wantcRev: 42 + 4i, 54 }, 55 { 56 x: []complex128{1 + 1i, 1 + 1i, 2 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 2 + 1i}, 57 y: []complex128{1 + 2i, 1 + 2i, 1 + 3i, 1 + 2i, 1 + 3i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}, 58 n: 10, 59 wantu: -10 + 37i, wantc: 34 + 17i, 60 wantuRev: -10 + 36i, wantcRev: 34 + 16i, 61 }, 62 { 63 x: []complex128{1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, complex(inf, 1), 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i, 1 + 1i}, 64 y: []complex128{1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i, 1 + 2i}, 65 n: 10, 66 wantu: complex(inf, inf), wantc: complex(inf, inf), 67 wantuRev: complex(inf, inf), wantcRev: complex(inf, inf), 68 }, 69 } 70 71 func TestDotcUnitary(t *testing.T) { 72 const gd = 1 + 5i 73 for i, test := range dotTests { 74 for _, align := range align2 { 75 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y) 76 xgLn, ygLn := 4+align.x, 4+align.y 77 xg, yg := guardVector(test.x, gd, xgLn), guardVector(test.y, gd, ygLn) 78 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn] 79 res := DotcUnitary(x, y) 80 if !cscalar.Same(res, test.wantc) { 81 t.Errorf(msgVal, prefix, i, res, test.wantc) 82 } 83 if !isValidGuard(xg, gd, xgLn) { 84 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 85 } 86 if !isValidGuard(yg, gd, ygLn) { 87 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:]) 88 } 89 } 90 } 91 } 92 93 func TestDotcInc(t *testing.T) { 94 const gd, gdLn = 2 + 5i, 4 95 for i, test := range dotTests { 96 for _, inc := range newIncSet(1, 2, 5, 10, -1, -2, -5, -10) { 97 xg, yg := guardIncVector(test.x, gd, inc.x, gdLn), guardIncVector(test.y, gd, inc.y, gdLn) 98 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn] 99 want := test.wantc 100 var ix, iy int 101 if inc.x < 0 { 102 ix, want = -inc.x*(test.n-1), test.wantcRev 103 } 104 if inc.y < 0 { 105 iy, want = -inc.y*(test.n-1), test.wantcRev 106 } 107 prefix := fmt.Sprintf("Test %v (x:%v y:%v) (ix:%v iy:%v)", i, inc.x, inc.y, ix, iy) 108 res := DotcInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy)) 109 if inc.x*inc.y > 0 { 110 want = test.wantc 111 } 112 if !cscalar.Same(res, want) { 113 t.Errorf(msgVal, prefix, i, res, want) 114 t.Error(x, y) 115 } 116 checkValidIncGuard(t, xg, gd, inc.x, gdLn) 117 checkValidIncGuard(t, yg, gd, inc.y, gdLn) 118 } 119 } 120 } 121 122 func TestDotuUnitary(t *testing.T) { 123 const gd = 1 + 5i 124 for i, test := range dotTests { 125 for _, align := range align2 { 126 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, align.x, align.y) 127 xgLn, ygLn := 4+align.x, 4+align.y 128 xg, yg := guardVector(test.x, gd, xgLn), guardVector(test.y, gd, ygLn) 129 x, y := xg[xgLn:len(xg)-xgLn], yg[ygLn:len(yg)-ygLn] 130 res := DotuUnitary(x, y) 131 if !cscalar.Same(res, test.wantu) { 132 t.Errorf(msgVal, prefix, i, res, test.wantu) 133 } 134 if !isValidGuard(xg, gd, xgLn) { 135 t.Errorf(msgGuard, prefix, "x", xg[:xgLn], xg[len(xg)-xgLn:]) 136 } 137 if !isValidGuard(yg, gd, ygLn) { 138 t.Errorf(msgGuard, prefix, "y", yg[:ygLn], yg[len(yg)-ygLn:]) 139 } 140 } 141 } 142 } 143 144 func TestDotuInc(t *testing.T) { 145 const gd, gdLn = 1 + 5i, 4 146 for i, test := range dotTests { 147 for _, inc := range newIncSet(1, 2, 5, 10, -1, -2, -5, -10) { 148 prefix := fmt.Sprintf("Test %v (x:%v y:%v)", i, inc.x, inc.y) 149 xg, yg := guardIncVector(test.x, gd, inc.x, gdLn), guardIncVector(test.y, gd, inc.y, gdLn) 150 x, y := xg[gdLn:len(xg)-gdLn], yg[gdLn:len(yg)-gdLn] 151 want := test.wantc 152 var ix, iy int 153 if inc.x < 0 { 154 ix, want = -inc.x*(test.n-1), test.wantuRev 155 } 156 if inc.y < 0 { 157 iy, want = -inc.y*(test.n-1), test.wantuRev 158 } 159 res := DotuInc(x, y, uintptr(test.n), uintptr(inc.x), uintptr(inc.y), uintptr(ix), uintptr(iy)) 160 if inc.x*inc.y > 0 { 161 want = test.wantu 162 } 163 if !cscalar.Same(res, want) { 164 t.Errorf(msgVal, prefix, i, res, want) 165 } 166 checkValidIncGuard(t, xg, gd, inc.x, gdLn) 167 checkValidIncGuard(t, yg, gd, inc.y, gdLn) 168 } 169 } 170 }