Repository navigation
Expand file tree
/
Copy pathsimd.go
More file actions
169 lines (154 loc) · 3.82 KB
/
Copy pathsimd.go
File metadata and controls
169 lines (154 loc) · 3.82 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package simd
//go:generate sh -c "cd ./codegen && ./generate.sh"
import (
"runtime"
"github.com/klauspost/cpuid/v2"
)
var (
avx2 = cpuid.CPU.Supports(cpuid.AVX2) && cpuid.CPU.Supports(cpuid.FMA3)
apple = runtime.GOARCH == "arm64" && runtime.GOOS == "darwin"
neon = runtime.GOARCH == "arm64" && !apple
hardware = avx2 || apple || neon
)
// 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 {
switch v := any(input).(type) {
case []int8:
return T(SumInt8s(v))
case []int16:
return T(SumInt16s(v))
case []int32:
return T(SumInt32s(v))
case []int64:
return T(SumInt64s(v))
case []uint8:
return T(SumUint8s(v))
case []uint16:
return T(SumUint16s(v))
case []uint32:
return T(SumUint32s(v))
case []uint64:
return T(SumUint64s(v))
case []float32:
return T(SumFloat32s(v))
case []float64:
return T(SumFloat64s(v))
default:
return sum(input)
}
}
// Sum sums up all of the elements of the slice and returns the value
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 {
switch v := any(input).(type) {
case []int8:
return T(MinInt8s(v))
case []int16:
return T(MinInt16s(v))
case []int32:
return T(MinInt32s(v))
case []int64:
return T(MinInt64s(v))
case []uint8:
return T(MinUint8s(v))
case []uint16:
return T(MinUint16s(v))
case []uint32:
return T(MinUint32s(v))
case []uint64:
return T(MinUint64s(v))
case []float32:
return T(MinFloat32s(v))
case []float64:
return T(MinFloat64s(v))
default:
return min(input)
}
}
// Min returns the smallest element value in the slice
func min[T Number](input []T) T {
min := input[0]
for _, v := range input[1:] {
if v < min {
min = v
}
}
return min
}
// Max returns the largest element value in the slice
func Max[T Number](input []T) T {
switch v := any(input).(type) {
case []int8:
return T(MaxInt8s(v))
case []int16:
return T(MaxInt16s(v))
case []int32:
return T(MaxInt32s(v))
case []int64:
return T(MaxInt64s(v))
case []uint8:
return T(MaxUint8s(v))
case []uint16:
return T(MaxUint16s(v))
case []uint32:
return T(MaxUint32s(v))
case []uint64:
return T(MaxUint64s(v))
case []float32:
return T(MaxFloat32s(v))
case []float64:
return T(MaxFloat64s(v))
default:
return max(input)
}
}
// Max returns the largest element value in the slice
func max[T Number](input []T) T {
max := input[0]
for _, v := range input[1:] {
if v > max {
max = v
}
}
return max
}
// Add adds input1 to input2 and writes back the result into dst slice
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 {
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 {
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 {
for i, v := range input1 {
dst[i] = v / input2[i]
}
return dst
}