Skip to content

Commit

Permalink
export constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jun 5, 2022
1 parent 24eff6a commit ba28839
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions simd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ var (
sve = cpuid.CPU.Supports(cpuid.SVE)
)

type number interface {
// Number represents a number constraint for SIMD operations
type Number interface {
~int | ~int8 | ~int16 | ~int32 | ~int64 | uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~float32 | ~float64
}

// Sum sums up all of the elements of the slice and returns the value
func Sum[T number](input []T) T {
func Sum[T Number](input []T) T {
switch v := any(input).(type) {
case []int8:
return T(SumInt8s(v))
Expand Down Expand Up @@ -46,15 +47,15 @@ func Sum[T number](input []T) T {
}

// Sum sums up all of the elements of the slice and returns the value
func sum[T number](input []T) (sum T) {
func sum[T Number](input []T) (sum T) {
for _, v := range input {
sum += v
}
return
}

// Min returns the smallest element value in the slice
func Min[T number](input []T) T {
func Min[T Number](input []T) T {
switch v := any(input).(type) {
case []int8:
return T(MinInt8s(v))
Expand Down Expand Up @@ -82,7 +83,7 @@ func Min[T number](input []T) T {
}

// Min returns the smallest element value in the slice
func min[T number](input []T) T {
func min[T Number](input []T) T {
min := input[0]
for _, v := range input[1:] {
if v < min {
Expand All @@ -93,7 +94,7 @@ func min[T number](input []T) T {
}

// Max returns the largest element value in the slice
func Max[T number](input []T) T {
func Max[T Number](input []T) T {
switch v := any(input).(type) {
case []int8:
return T(MaxInt8s(v))
Expand Down Expand Up @@ -121,7 +122,7 @@ func Max[T number](input []T) T {
}

// Max returns the largest element value in the slice
func max[T number](input []T) T {
func max[T Number](input []T) T {
max := input[0]
for _, v := range input[1:] {
if v > max {
Expand All @@ -132,31 +133,31 @@ func max[T number](input []T) T {
}

// Add adds input1 to input2 and writes back the result into dst slice
func add[T number](dst, input1, input2 []T) []T {
func add[T Number](dst, input1, input2 []T) []T {
for i, v := range input1 {
dst[i] = v + input2[i]
}
return dst
}

// Sub subtracts input2 from input1 and writes back the result into dst slice
func sub[T number](dst, input1, input2 []T) []T {
func sub[T Number](dst, input1, input2 []T) []T {
for i, v := range input1 {
dst[i] = v - input2[i]
}
return dst
}

// Mul multiplies input1 by input2 and writes back the result into dst slice
func mul[T number](dst, input1, input2 []T) []T {
func mul[T Number](dst, input1, input2 []T) []T {
for i, v := range input1 {
dst[i] = v * input2[i]
}
return dst
}

// Div divides input1 by input2 and writes back the result into dst slice
func div[T number](dst, input1, input2 []T) []T {
func div[T Number](dst, input1, input2 []T) []T {
for i, v := range input1 {
dst[i] = v / input2[i]
}
Expand Down
2 changes: 1 addition & 1 deletion simd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Result struct {
}

// makeVector generates a test vector
func makeVector[T number](count int) []T {
func makeVector[T Number](count int) []T {
arr := make([]T, count)
for i := 0; i < count; i++ {
arr[i] = T((i % 100) + 1)
Expand Down

0 comments on commit ba28839

Please sign in to comment.