Debian Codegraph @

examples/dot/dot_test.go

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