1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
package dot
import (
"math/rand"
"testing"
"golang.org/x/sys/cpu"
)
//go:generate go run asm.go -out dot.s -stubs stub.go
func RequireISAs(t *testing.T) {
t.Helper()
if !(cpu.X86.HasAVX && cpu.X86.HasFMA) {
t.Skip("requires AVX and FMA3 instruction sets")
}
}
func TestEmpty(t *testing.T) {
RequireISAs(t)
if Dot(nil, nil) != 0.0 {
t.Fatal("expect dot product of empty vectors to be zero")
}
}
func TestLengths(t *testing.T) {
RequireISAs(t)
const epsilon = 0.00001
for n := 0; n < 1000; n++ {
x, y := RandomVector(n), RandomVector(n)
got := Dot(x, y)
expect := Expect(x, y)
relerr := got/expect - 1.0
if Abs(relerr) > epsilon {
t.Fatalf("bad result on vector length %d: got %v expect %v relative error %f", n, got, expect, relerr)
}
}
}
func Expect(x, y []float32) float32 {
var p float32
for i := range x {
p += x[i] * y[i]
}
return p
}
func RandomVector(n int) []float32 {
x := make([]float32, n)
for i := 0; i < n; i++ {
x[i] = rand.Float32() * 100
}
return x
}
func Abs(x float32) float32 {
if x < 0.0 {
return -x
}
return x
}
|