floats,asm/f64: add asm routine for sum
MODIFY
floats/floats.go
floats/floats.go
@@ -897,11 +897,7 @@ func SubTo(dst, s, t []float64) []float64 {
// Sum returns the sum of the elements of the slice.
func Sum(s []float64) float64 {
- var sum float64
- for _, val := range s {
- sum += val
- }
- return sum
+ return f64.Sum(s)
}
// Within returns the first index i where s[i] <= v < s[i+1]. Within panics if:

MODIFY
internal/asm/f64/stubs_amd64.go
internal/asm/f64/stubs_amd64.go
@@ -163,3 +163,10 @@ func ScalInc(alpha float64, x []float64, n, incX uintptr)
// idst += incDst
// }
func ScalIncTo(dst []float64, incDst uintptr, alpha float64, x []float64, n, incX uintptr)
+
+// Sum is
+// var sum float64
+// for i := range x {
+// sum += x[i]
+// }
+func Sum(x []float64) float64

MODIFY
internal/asm/f64/stubs_noasm.go
internal/asm/f64/stubs_noasm.go
@@ -155,3 +155,16 @@ func LinfDist(s, t []float64) float64 {
}
return norm
}
+
+// Sum is
+// var sum float64
+// for i := range x {
+// sum += x[i]
+// }
+func Sum(x []float64) float64 {
+ var sum float64
+ for _, v := range x {
+ sum += v
+ }
+ return sum
+}

MODIFY
internal/asm/f64/stubs_test.go
internal/asm/f64/stubs_test.go
@@ -572,3 +572,63 @@ func TestLinfDist(t *testing.T) {
}
}
}
+
+func TestSum(t *testing.T) {
+ var srcGd float64 = -1
+ for j, v := range []struct {
+ src []float64
+ expect float64
+ }{
+ {
+ src: []float64{},
+ expect: 0,
+ },
+ {
+ src: []float64{1},
+ expect: 1,
+ },
+ {
+ src: []float64{nan},
+ expect: nan,
+ },
+ {
+ src: []float64{1, 2, 3},
+ expect: 6,
+ },
+ {
+ src: []float64{1, -4, 3},
+ expect: 0,
+ },
+ {
+ src: []float64{1, 2, 3, 4},
+ expect: 10,
+ },
+ {
+ src: []float64{1, 1, nan, 1, 1},
+ expect: nan,
+ },
+ {
+ src: []float64{inf, 4, nan, -inf, 9},
+ expect: nan,
+ },
+ {
+ src: []float64{1, 1, 1, 1, 9, 1, 1, 1, 2, 1, 1, 1, 1, 1, 5, 1},
+ expect: 29,
+ },
+ {
+ src: []float64{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},
+ expect: 67,
+ },
+ } {
+ gdLn := 4 + j%2
+ gsrc := guardVector(v.src, srcGd, gdLn)
+ src := gsrc[gdLn : len(gsrc)-gdLn]
+ ret := Sum(src)
+ if !same(ret, v.expect) {
+ t.Errorf("Test %d Sum error Got: %v Expected: %v", j, ret, v.expect)
+ }
+ if !isValidGuard(gsrc, srcGd, gdLn) {
+ t.Errorf("Test %d Guard violated in src vector %v %v", j, gsrc[:gdLn], gsrc[len(gsrc)-gdLn:])
+ }
+ }
+}

floats,asm/f64: add asm routine for sum