asm/f32: Adding tests for dot asm functions.
MODIFY
internal/asm/f32/bench_test.go
internal/asm/f32/bench_test.go
@@ -6,11 +6,15 @@ package f32
import "testing"
+const (
+ benchLen = 1e5
+ a = 2
+)
+
var (
- a = float32(2)
- x = make([]float32, 1000000)
- y = make([]float32, 1000000)
- z = make([]float32, 1000000)
+ x = make([]float32, benchLen)
+ y = make([]float32, benchLen)
+ z = make([]float32, benchLen)
)
func init() {

MODIFY
internal/asm/f32/stubs_test.go
internal/asm/f32/stubs_test.go
package f32
-import (
- "math"
- "testing"
-)
-
-var (
- nan = float32(math.NaN())
- inf = float32(math.Inf(1))
-)
+import "testing"
var tests = []struct {
incX, incY, incDst uintptr
@@ -71,147 +63,86 @@ var tests = []struct {
ex: []float32{9.25, 10.25, 11.25, 12.25, 13.25, 14.25, 15.25, 16.25, 17.25, 18.25}},
}
-func guardVector(vec []float32, guard_val float32, guard_len int) (guarded []float32) {
- guarded = make([]float32, len(vec)+guard_len*2)
- copy(guarded[guard_len:], vec)
- for i := 0; i < guard_len; i++ {
- guarded[i] = guard_val
- guarded[len(guarded)-1-i] = guard_val
- }
- return guarded
-}
-
-func isValidGuard(vec []float32, guard_val float32, guard_len int) bool {
- for i := 0; i < guard_len; i++ {
- if vec[i] != guard_val || vec[len(vec)-1-i] != guard_val {
- return false
- }
- }
- return true
-}
-
-func same(x, y float32) bool {
- a, b := float64(x), float64(y)
- return a == b || (math.IsNaN(a) && math.IsNaN(b))
-}
-
func TestAxpyUnitary(t *testing.T) {
- var x_gd, y_gd float32 = 1, 1
- for cas, test := range tests {
- xg_ln, yg_ln := 4+cas%2, 4+cas%3
- test.x, test.y = guardVector(test.x, x_gd, xg_ln), guardVector(test.y, y_gd, yg_ln)
- x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
- AxpyUnitary(test.a, x, y)
- for i := range test.ex {
- if !same(y[i], test.ex[i]) {
- t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i], test.ex[i])
+ for j, v := range tests {
+ gdLn := 4 + j%2
+ v.x, v.y = guardVector(v.x, 1, gdLn), guardVector(v.y, 1, gdLn)
+ x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
+ AxpyUnitary(v.a, x, y)
+ for i := range v.ex {
+ if !same(y[i], v.ex[i]) {
+ t.Error("Test", j, "Unexpected result at", i, "Got:", int(y[i]), "Expected:", v.ex[i])
}
}
- if !isValidGuard(test.x, x_gd, xg_ln) {
- t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xg_ln], test.x[len(test.x)-xg_ln:])
+ if !isValidGuard(v.x, 1, gdLn) {
+ t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
}
- if !isValidGuard(test.y, y_gd, yg_ln) {
- t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:yg_ln], test.y[len(test.y)-yg_ln:])
+ if !isValidGuard(v.y, 1, gdLn) {
+ t.Error("Test", j, "Guard violated in y vector", v.y[:gdLn], v.y[len(v.x)-gdLn:])
}
}
}
func TestAxpyUnitaryTo(t *testing.T) {
- var x_gd, y_gd, dst_gd float32 = 1, 1, 0
- for cas, test := range tests {
- xg_ln, yg_ln := 4+cas%2, 4+cas%3
- test.x, test.y = guardVector(test.x, x_gd, xg_ln), guardVector(test.y, y_gd, yg_ln)
- test.dst = guardVector(test.dst, dst_gd, xg_ln)
- x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
- dst := test.dst[xg_ln : len(test.dst)-xg_ln]
- AxpyUnitaryTo(dst, test.a, x, y)
- for i := range test.ex {
- if !same(test.ex[i], dst[i]) {
- t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i], test.ex[i])
- }
- }
- if !isValidGuard(test.x, x_gd, xg_ln) {
- t.Errorf("Test %d Guard violated in x vector %v %v", cas, test.x[:xg_ln], test.x[len(test.x)-xg_ln:])
+ for j, v := range tests {
+ gdLn := 4 + j%2
+ v.x, v.y = guardVector(v.x, 1, gdLn), guardVector(v.y, 1, gdLn)
+ v.dst = guardVector(v.dst, 0, gdLn)
+ x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
+ dst := v.dst[gdLn : len(v.dst)-gdLn]
+ AxpyUnitaryTo(dst, v.a, x, y)
+ for i := range v.ex {
+ if !same(v.ex[i], dst[i]) {
+ t.Error("Test", j, "Unexpected result at", i, "Got:", dst[i], "Expected:", v.ex[i])
}
- if !isValidGuard(test.y, y_gd, yg_ln) {
- t.Errorf("Test %d Guard violated in y vector %v %v", cas, test.y[:yg_ln], test.y[len(test.y)-yg_ln:])
}
- if !isValidGuard(test.dst, dst_gd, xg_ln) {
- t.Errorf("Test %d Guard violated in dst vector %v %v", cas, test.dst[:xg_ln], test.dst[len(test.dst)-xg_ln:])
+ if !isValidGuard(v.x, 1, gdLn) {
+ t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
}
+ if !isValidGuard(v.y, 1, gdLn) {
+ t.Error("Test", j, "Guard violated in y vector", v.y[:gdLn], v.y[len(v.x)-gdLn:])
}
-}
-
-func guardIncVector(vec []float32, guard_val float32, incV uintptr, guard_len int) (guarded []float32) {
- inc := int(incV)
- s_ln := len(vec) * (inc)
- guarded = make([]float32, s_ln+guard_len*2)
- for i, j := 0, 0; i < len(guarded); i++ {
- switch {
- case i < guard_len, i > guard_len+s_ln:
- guarded[i] = guard_val
- case (i-guard_len)%(inc) == 0 && j < len(vec):
- guarded[i] = vec[j]
- j++
- default:
- guarded[i] = guard_val
- }
- }
- return guarded
-}
-
-func checkValidIncGuard(t *testing.T, vec []float32, guard_val float32, incV uintptr, guard_len int) {
- inc := int(incV)
- s_ln := len(vec) - 2*guard_len
- for i := range vec {
- switch {
- case same(vec[i], guard_val):
- // Correct value
- case i < guard_len:
- t.Errorf("Front guard violated at %d %v", i, vec[:guard_len])
- case i > guard_len+s_ln:
- t.Errorf("Back guard violated at %d %v", i-guard_len-s_ln, vec[guard_len+s_ln:])
- case (i-guard_len)%inc == 0 && (i-guard_len)/inc < len(vec):
- // Ignore input values
- default:
- t.Errorf("Internal guard violated at %d %v", i-guard_len, vec[guard_len:guard_len+s_ln])
+ if !isValidGuard(v.dst, 0, gdLn) {
+ t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
}
}
}
func TestAxpyInc(t *testing.T) {
- var x_gd, y_gd float32 = 1, 1
- for cas, test := range tests {
- xg_ln, yg_ln := 4+cas%2, 4+cas%3
- test.x, test.y = guardIncVector(test.x, x_gd, uintptr(test.incX), xg_ln), guardIncVector(test.y, y_gd, uintptr(test.incY), yg_ln)
- x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
- AxpyInc(test.a, x, y, uintptr(len(test.ex)), test.incX, test.incY, test.ix, test.iy)
- for i := range test.ex {
- if !same(y[i*int(test.incY)], test.ex[i]) {
- t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, y[i*int(test.incY)], test.ex[i])
+ for j, v := range tests {
+ gdLn := 4 + j%2
+ v.x, v.y = guardIncVector(v.x, 1, int(v.incX), gdLn), guardIncVector(v.y, 1, int(v.incY), gdLn)
+ x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
+ AxpyInc(v.a, x, y, uintptr(len(v.ex)), v.incX, v.incY, v.ix, v.iy)
+ for i := range v.ex {
+ if !same(y[i*int(v.incY)], v.ex[i]) {
+ t.Error("Test", j, "Unexpected result at", i, "Got:", y[i*int(v.incY)], "Expected:", v.ex[i])
+ t.Error("Result:", y)
+ t.Error("Expect:", v.ex)
}
}
- checkValidIncGuard(t, test.x, x_gd, uintptr(test.incX), xg_ln)
- checkValidIncGuard(t, test.y, y_gd, uintptr(test.incY), yg_ln)
+ checkValidIncGuard(t, v.x, 1, int(v.incX), gdLn)
+ checkValidIncGuard(t, v.y, 1, int(v.incY), gdLn)
}
}
func TestAxpyIncTo(t *testing.T) {
- var x_gd, y_gd, dst_gd float32 = 1, 1, 0
- for cas, test := range tests {
- xg_ln, yg_ln := 4+cas%2, 4+cas%3
- test.x, test.y = guardIncVector(test.x, x_gd, uintptr(test.incX), xg_ln), guardIncVector(test.y, y_gd, uintptr(test.incY), yg_ln)
- test.dst = guardIncVector(test.dst, dst_gd, uintptr(test.incDst), xg_ln)
- x, y := test.x[xg_ln:len(test.x)-xg_ln], test.y[yg_ln:len(test.y)-yg_ln]
- dst := test.dst[xg_ln : len(test.dst)-xg_ln]
- AxpyIncTo(dst, test.incDst, test.idst, test.a, x, y, uintptr(len(test.ex)), test.incX, test.incY, test.ix, test.iy)
- for i := range test.ex {
- if !same(dst[i*int(test.incDst)], test.ex[i]) {
- t.Errorf("Test %d Unexpected result at %d Got: %v Expected: %v", cas, i, dst[i*int(test.incDst)], test.ex[i])
- }
- }
- checkValidIncGuard(t, test.x, x_gd, uintptr(test.incX), xg_ln)
- checkValidIncGuard(t, test.y, y_gd, uintptr(test.incY), yg_ln)
- checkValidIncGuard(t, test.dst, dst_gd, uintptr(test.incDst), xg_ln)
+ for j, v := range tests {
+ gdLn := 4 + j%2
+ v.x, v.y = guardIncVector(v.x, 1, int(v.incX), gdLn), guardIncVector(v.y, 1, int(v.incY), gdLn)
+ v.dst = guardIncVector(v.dst, 0, int(v.incDst), gdLn)
+ x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
+ dst := v.dst[gdLn : len(v.dst)-gdLn]
+ AxpyIncTo(dst, v.incDst, v.idst, v.a, x, y, uintptr(len(v.ex)), v.incX, v.incY, v.ix, v.iy)
+ for i := range v.ex {
+ if !same(dst[i*int(v.incDst)], v.ex[i]) {
+ t.Error("Test", j, "Unexpected result at", i, "Got:", dst[i*int(v.incDst)], "Expected:", v.ex[i])
+ t.Error(v.dst)
+ t.Error(v.ex)
+ }
+ }
+ checkValidIncGuard(t, v.x, 1, int(v.incX), gdLn)
+ checkValidIncGuard(t, v.y, 1, int(v.incY), gdLn)
+ checkValidIncGuard(t, v.dst, 0, int(v.incDst), gdLn)
}
}

asm/f32: Adding tests for dot asm functions.