github.com/gopherd/gonum@v0.0.4/internal/asm/f32/stubs_test.go (about)

     1  // Copyright ©2015 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 f32_test
     6  
     7  import (
     8  	"testing"
     9  
    10  	. "github.com/gopherd/gonum/internal/asm/f32"
    11  )
    12  
    13  var tests = []struct {
    14  	incX, incY, incDst uintptr
    15  	ix, iy, idst       uintptr
    16  	a                  float32
    17  	dst, x, y          []float32
    18  	ex                 []float32
    19  }{
    20  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    21  		a:   3,
    22  		dst: []float32{5},
    23  		x:   []float32{2},
    24  		y:   []float32{1},
    25  		ex:  []float32{7}},
    26  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    27  		a:   5,
    28  		dst: []float32{0, 0, 0},
    29  		x:   []float32{0, 0, 0},
    30  		y:   []float32{1, 1, 1},
    31  		ex:  []float32{1, 1, 1}},
    32  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    33  		a:   5,
    34  		dst: []float32{0, 0, 0},
    35  		x:   []float32{0, 0},
    36  		y:   []float32{1, 1, 1},
    37  		ex:  []float32{1, 1}},
    38  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    39  		a:   -1,
    40  		dst: []float32{-1, -1, -1},
    41  		x:   []float32{1, 1, 1},
    42  		y:   []float32{1, 2, 1},
    43  		ex:  []float32{0, 1, 0}},
    44  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    45  		a:   -1,
    46  		dst: []float32{1, 1, 1},
    47  		x:   []float32{1, 2, 1},
    48  		y:   []float32{-1, -2, -1},
    49  		ex:  []float32{-2, -4, -2}},
    50  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    51  		a:   2.5,
    52  		dst: []float32{1, 1, 1, 1, 1},
    53  		x:   []float32{1, 2, 3, 2, 1},
    54  		y:   []float32{0, 0, 0, 0, 0},
    55  		ex:  []float32{2.5, 5, 7.5, 5, 2.5}},
    56  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0, // Run big test twice, once aligned once unaligned.
    57  		a:   16.5,
    58  		dst: make([]float32, 20),
    59  		x:   []float32{.5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5, .5},
    60  		y:   []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    61  		ex:  []float32{9.25, 10.25, 11.25, 12.25, 13.25, 14.25, 15.25, 16.25, 17.25, 18.25, 9.25, 10.25, 11.25, 12.25, 13.25, 14.25, 15.25, 16.25, 17.25, 18.25}},
    62  	{incX: 2, incY: 2, incDst: 3, ix: 0, iy: 0, idst: 0,
    63  		a:   16.5,
    64  		dst: make([]float32, 10),
    65  		x:   []float32{.5, .5, .5, .5, .5, .5, .5, .5, .5, .5},
    66  		y:   []float32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
    67  		ex:  []float32{9.25, 10.25, 11.25, 12.25, 13.25, 14.25, 15.25, 16.25, 17.25, 18.25}},
    68  }
    69  
    70  func TestAxpyUnitary(t *testing.T) {
    71  	for j, v := range tests {
    72  		gdLn := 4 + j%2
    73  		v.x, v.y = guardVector(v.x, 1, gdLn), guardVector(v.y, 1, gdLn)
    74  		x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
    75  		AxpyUnitary(v.a, x, y)
    76  		for i := range v.ex {
    77  			if !same(y[i], v.ex[i]) {
    78  				t.Error("Test", j, "Unexpected result at", i, "Got:", int(y[i]), "Expected:", v.ex[i])
    79  			}
    80  		}
    81  		if !isValidGuard(v.x, 1, gdLn) {
    82  			t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
    83  		}
    84  		if !isValidGuard(v.y, 1, gdLn) {
    85  			t.Error("Test", j, "Guard violated in y vector", v.y[:gdLn], v.y[len(v.x)-gdLn:])
    86  		}
    87  	}
    88  }
    89  
    90  func TestAxpyUnitaryTo(t *testing.T) {
    91  	for j, v := range tests {
    92  		gdLn := 4 + j%2
    93  		v.x, v.y = guardVector(v.x, 1, gdLn), guardVector(v.y, 1, gdLn)
    94  		v.dst = guardVector(v.dst, 0, gdLn)
    95  		x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
    96  		dst := v.dst[gdLn : len(v.dst)-gdLn]
    97  		AxpyUnitaryTo(dst, v.a, x, y)
    98  		for i := range v.ex {
    99  			if !same(v.ex[i], dst[i]) {
   100  				t.Error("Test", j, "Unexpected result at", i, "Got:", dst[i], "Expected:", v.ex[i])
   101  			}
   102  		}
   103  		if !isValidGuard(v.x, 1, gdLn) {
   104  			t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
   105  		}
   106  		if !isValidGuard(v.y, 1, gdLn) {
   107  			t.Error("Test", j, "Guard violated in y vector", v.y[:gdLn], v.y[len(v.x)-gdLn:])
   108  		}
   109  		if !isValidGuard(v.dst, 0, gdLn) {
   110  			t.Error("Test", j, "Guard violated in x vector", v.x[:gdLn], v.x[len(v.x)-gdLn:])
   111  		}
   112  	}
   113  }
   114  
   115  func TestAxpyInc(t *testing.T) {
   116  	for j, v := range tests {
   117  		gdLn := 4 + j%2
   118  		v.x, v.y = guardIncVector(v.x, 1, int(v.incX), gdLn), guardIncVector(v.y, 1, int(v.incY), gdLn)
   119  		x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
   120  		AxpyInc(v.a, x, y, uintptr(len(v.ex)), v.incX, v.incY, v.ix, v.iy)
   121  		for i := range v.ex {
   122  			if !same(y[i*int(v.incY)], v.ex[i]) {
   123  				t.Error("Test", j, "Unexpected result at", i, "Got:", y[i*int(v.incY)], "Expected:", v.ex[i])
   124  				t.Error("Result:", y)
   125  				t.Error("Expect:", v.ex)
   126  			}
   127  		}
   128  		checkValidIncGuard(t, v.x, 1, int(v.incX), gdLn)
   129  		checkValidIncGuard(t, v.y, 1, int(v.incY), gdLn)
   130  	}
   131  }
   132  
   133  func TestAxpyIncTo(t *testing.T) {
   134  	for j, v := range tests {
   135  		gdLn := 4 + j%2
   136  		v.x, v.y = guardIncVector(v.x, 1, int(v.incX), gdLn), guardIncVector(v.y, 1, int(v.incY), gdLn)
   137  		v.dst = guardIncVector(v.dst, 0, int(v.incDst), gdLn)
   138  		x, y := v.x[gdLn:len(v.x)-gdLn], v.y[gdLn:len(v.y)-gdLn]
   139  		dst := v.dst[gdLn : len(v.dst)-gdLn]
   140  		AxpyIncTo(dst, v.incDst, v.idst, v.a, x, y, uintptr(len(v.ex)), v.incX, v.incY, v.ix, v.iy)
   141  		for i := range v.ex {
   142  			if !same(dst[i*int(v.incDst)], v.ex[i]) {
   143  				t.Error("Test", j, "Unexpected result at", i, "Got:", dst[i*int(v.incDst)], "Expected:", v.ex[i])
   144  				t.Error(v.dst)
   145  				t.Error(v.ex)
   146  			}
   147  		}
   148  		checkValidIncGuard(t, v.x, 1, int(v.incX), gdLn)
   149  		checkValidIncGuard(t, v.y, 1, int(v.incY), gdLn)
   150  		checkValidIncGuard(t, v.dst, 0, int(v.incDst), gdLn)
   151  	}
   152  }
   153  
   154  func TestSum(t *testing.T) {
   155  	var srcGd float32 = -1
   156  	for j, v := range []struct {
   157  		src    []float32
   158  		expect float32
   159  	}{
   160  		{
   161  			src:    []float32{},
   162  			expect: 0,
   163  		},
   164  		{
   165  			src:    []float32{1},
   166  			expect: 1,
   167  		},
   168  		{
   169  			src:    []float32{nan},
   170  			expect: nan,
   171  		},
   172  		{
   173  			src:    []float32{1, 2, 3},
   174  			expect: 6,
   175  		},
   176  		{
   177  			src:    []float32{1, -4, 3},
   178  			expect: 0,
   179  		},
   180  		{
   181  			src:    []float32{1, 2, 3, 4},
   182  			expect: 10,
   183  		},
   184  		{
   185  			src:    []float32{1, 1, nan, 1, 1},
   186  			expect: nan,
   187  		},
   188  		{
   189  			src:    []float32{inf, 4, nan, -inf, 9},
   190  			expect: nan,
   191  		},
   192  		{
   193  			src:    []float32{1, 1, 1, 1, 9, 1, 1, 1, 2, 1, 1, 1, 1, 1, 5, 1},
   194  			expect: 29,
   195  		},
   196  		{
   197  			src:    []float32{1, 1, 1, 1, 9, 1, 1, 1, 2, 1, 1, 1, 1, 1, 5, 11, 1, 1, 1, 9, 1, 1, 1, 2, 1, 1, 1, 1, 1, 5, 1},
   198  			expect: 67,
   199  		},
   200  	} {
   201  		for _, i := range [4]int{0, 1, 2, 3} {
   202  			gdLn := 4 + j%4 + i
   203  			gsrc := guardVector(v.src, srcGd, gdLn)
   204  			src := gsrc[gdLn : len(gsrc)-gdLn]
   205  			ret := Sum(src)
   206  			if !same(ret, v.expect) {
   207  				t.Errorf("Test %d Sum error Got: %v Expected: %v", j, ret, v.expect)
   208  			}
   209  			if !isValidGuard(gsrc, srcGd, gdLn) {
   210  				t.Errorf("Test %d Guard violated in src vector %v %v", j, gsrc[:gdLn], gsrc[len(gsrc)-gdLn:])
   211  			}
   212  		}
   213  	}
   214  }