VCTR
Loading...
Searching...
No Matches
NeonRegister.h
1/*
2 ==============================================================================
3 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
5 Copyright 2022- by sonible GmbH.
6
7 This file is part of VCTR - Versatile Container Templates Reconceptualized.
8
9 VCTR is free software: you can redistribute it and/or modify
10 it under the terms of the GNU Lesser General Public License version 3
11 only, as published by the Free Software Foundation.
12
13 VCTR is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU Lesser General Public License version 3 for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 version 3 along with VCTR. If not, see <https://www.gnu.org/licenses/>.
20 ==============================================================================
21*/
22
23namespace vctr
24{
25
26template <class T>
28{
29 static constexpr NeonRegister broadcast (const T&) { return {}; }
30};
31
32#if VCTR_ARM
33
34namespace detail
35{
36
37// clang-format off
38template <CompareOp, class>
39struct NeonCompare {};
40
41template <> struct NeonCompare<CompareOp::less, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vcltq_f32 (a, b); } };
42template <> struct NeonCompare<CompareOp::less, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vcltq_f64 (a, b); } };
43template <> struct NeonCompare<CompareOp::lessOrEqual, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vcleq_f32 (a, b); } };
44template <> struct NeonCompare<CompareOp::lessOrEqual, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vcleq_f64 (a, b); } };
45template <> struct NeonCompare<CompareOp::greater, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vcgtq_f32 (a, b); } };
46template <> struct NeonCompare<CompareOp::greater, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vcgtq_f64 (a, b); } };
47template <> struct NeonCompare<CompareOp::greaterOrEqual, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vcgeq_f32 (a, b); } };
48template <> struct NeonCompare<CompareOp::greaterOrEqual, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vcgeq_f64 (a, b); } };
49template <> struct NeonCompare<CompareOp::equal, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vceqq_f32 (a, b); } };
50template <> struct NeonCompare<CompareOp::equal, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vceqq_f64 (a, b); } };
51template <> struct NeonCompare<CompareOp::notEqual, float> { static auto cmp (float32x4_t a, float32x4_t b) { return vmvnq_u32 (vceqq_f32 (a, b)); } };
52template <> struct NeonCompare<CompareOp::notEqual, double> { static auto cmp (float64x2_t a, float64x2_t b) { return vreinterpretq_u64_u32 (vmvnq_u32 (vreinterpretq_u32_u64 (vceqq_f64 (a, b)))); } };
53template <> struct NeonCompare<CompareOp::less, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vcltq_s32 (a, b); } };
54template <> struct NeonCompare<CompareOp::lessOrEqual, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vcleq_s32 (a, b); } };
55template <> struct NeonCompare<CompareOp::greater, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vcgtq_s32 (a, b); } };
56template <> struct NeonCompare<CompareOp::greaterOrEqual, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vcgeq_s32 (a, b); } };
57template <> struct NeonCompare<CompareOp::equal, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vceqq_s32 (a, b); } };
58template <> struct NeonCompare<CompareOp::notEqual, int32_t> { static auto cmp (int32x4_t a, int32x4_t b) { return vmvnq_u32 (vceqq_s32 (a, b)); } };
59// clang-format on
60}
61
62template <>
63struct NeonRegister<float>
64{
65 static constexpr size_t numElements = 4;
66
67 using NativeType = float32x4_t;
68 float32x4_t value;
69
70 //==============================================================================
71 // Loading
72 // clang-format off
73 static NeonRegister load (const float* d) { return { vld1q_f32 (d) }; }
74 static NeonRegister broadcast (float x) { return { vdupq_n_f32 (x) }; }
75
76 //==============================================================================
77 // Storing
78 void store (float* d) const { vst1q_f32 (d, value); }
79
80 //==============================================================================
81 // Generate Compare Masks
82 template <CompareOp op>
83 static NeonRegister compare (NeonRegister a, NeonRegister b) { return { vreinterpretq_f32_u32 (detail::NeonCompare<op, float>::cmp (a.value, b.value)) }; }
84
85 //==============================================================================
86 // Bit Operations
88 static NeonRegister bitwiseAndNot (NeonRegister a, NeonRegister b) { return { vreinterpretq_f32_u32 (vandq_u32 (vreinterpretq_u32_f32 (a.value), vmvnq_u32 (vreinterpretq_u32_f32 (b.value)))) }; }
89 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vreinterpretq_f32_u32 (vandq_u32 (vreinterpretq_u32_f32 (a.value), vreinterpretq_u32_f32 (b.value))) }; }
90 static NeonRegister bitwiseBlend (NeonRegister a, NeonRegister b, NeonRegister mask) { return { vbslq_f32 (vreinterpretq_u32_f32 (mask.value), b.value, a.value) }; }
91
92 //==============================================================================
93 // Math
94 static NeonRegister abs (NeonRegister x) { return { vabsq_f32 (x.value) }; }
95 static NeonRegister floor (NeonRegister x) { return { vrndmq_f32 (x.value) }; }
96 static NeonRegister ceil (NeonRegister x) { return { vrndpq_f32 (x.value) }; }
97 static NeonRegister mul (NeonRegister a, NeonRegister b) { return { vmulq_f32 (a.value, b.value) }; }
98 static NeonRegister div (NeonRegister a, NeonRegister b) { return { vdivq_f32 (a.value, b.value) }; }
99 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_f32 (a.value, b.value) }; }
100 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_f32 (a.value, b.value) }; }
101 static NeonRegister max (NeonRegister a, NeonRegister b) { return { vmaxq_f32 (a.value, b.value) }; }
102 static NeonRegister min (NeonRegister a, NeonRegister b) { return { vminq_f32 (a.value, b.value) }; }
103 static NeonRegister sqrt (NeonRegister x) { return { vsqrtq_f32 (x.value) }; }
105 static NeonRegister copysign (NeonRegister mag, NeonRegister sgn) { return { vbslq_f32 (vreinterpretq_u32_f32 (vdupq_n_f32 (float (-0.0))), sgn.value, mag.value) }; }
106 static NeonRegister fma (NeonRegister a, NeonRegister b, NeonRegister c) { return { vfmaq_f32 (c.value, a.value, b.value) }; }
107 static NeonRegister fms (NeonRegister a, NeonRegister b, NeonRegister c) { return { vfmsq_f32 (c.value, a.value, b.value) }; }
108
109#if VCTR_APPLE
110 // The Apple Accelerate vfp function collection contains some optimised math functions that can be directly called
111 // on 128 bit float vectors and which are therefore compatible to float32x4_t arguments.
112 static NeonRegister exp (NeonRegister x) { return { vexpf (x.value) }; }
113 static NeonRegister exp2 (NeonRegister x) { return { vexp2f (x.value) }; }
114 static NeonRegister expm1 (NeonRegister x) { return { vexpm1f (x.value) }; }
115 static NeonRegister log (NeonRegister x) { return { vlogf (x.value) }; }
116 static NeonRegister log1p (NeonRegister x) { return { vlog1pf (x.value) }; }
117 static NeonRegister log10 (NeonRegister x) { return { vlog10f (x.value) }; }
118 static NeonRegister logb (NeonRegister x) { return { vlogbf (x.value) }; }
119 static NeonRegister log2 (NeonRegister x) { return { vlog2f (x.value) }; }
120 static NeonRegister sin (NeonRegister x) { return { vsinf (x.value) }; }
121 static NeonRegister cos (NeonRegister x) { return { vcosf (x.value) }; }
122 static NeonRegister tan (NeonRegister x) { return { vtanf (x.value) }; }
123 static NeonRegister sinh (NeonRegister x) { return { vsinhf (x.value) }; }
124 static NeonRegister cosh (NeonRegister x) { return { vcoshf (x.value) }; }
125 static NeonRegister tanh (NeonRegister x) { return { vtanhf (x.value) }; }
126 static NeonRegister asinh (NeonRegister x) { return { vasinhf (x.value) }; }
127 static NeonRegister acosh (NeonRegister x) { return { vacoshf (x.value) }; }
128 static NeonRegister asin (NeonRegister x) { return { vasinf (x.value) }; }
129 static NeonRegister acos (NeonRegister x) { return { vacosf (x.value) }; }
130 static NeonRegister atan (NeonRegister x) { return { vatanf (x.value) }; }
131 static NeonRegister atanh (NeonRegister x) { return { vatanhf (x.value) }; }
132 static NeonRegister pow (NeonRegister x, NeonRegister y) { return { vpowf (x.value, y.value) }; }
133 static NeonRegister pow (NeonRegister x, NeonRegister<int32_t> y);
134#endif
135
136 //==============================================================================
137 // Type conversion
138 static NeonRegister<int32_t> convertToInt (NeonRegister x);
139 static NeonRegister<int32_t> reinterpretAsInt (NeonRegister x);
140 // clang-format on
141};
142
143template <>
144struct NeonRegister<double>
145{
146 static constexpr size_t numElements = 2;
147
148 using NativeType = float64x2_t;
149 float64x2_t value;
150
151 //==============================================================================
152 // Loading
153 // clang-format off
154 static NeonRegister load (const double* d) { return { vld1q_f64 (d) }; }
155 static NeonRegister broadcast (double x) { return { vdupq_n_f64 (x) }; }
156
157 //==============================================================================
158 // Storing
159 void store (double* d) const { vst1q_f64 (d, value); }
160
161 //==============================================================================
162 // Generate Compare Masks
163 template <CompareOp op>
164 static NeonRegister compare (NeonRegister a, NeonRegister b) { return { vreinterpretq_f64_u64 (detail::NeonCompare<op, double>::cmp (a.value, b.value)) }; }
165
166 //==============================================================================
167 // Bit Operations
169 static NeonRegister bitwiseAndNot (NeonRegister a, NeonRegister b) { return { vreinterpretq_f64_u32 (vandq_u32 (vreinterpretq_u32_f64 (a.value), vmvnq_u32 (vreinterpretq_u32_f64 (b.value)))) }; }
170 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vreinterpretq_f64_u32 (vandq_u32 (vreinterpretq_u32_f64 (a.value), vreinterpretq_u32_f64 (b.value))) }; }
171 static NeonRegister bitwiseBlend (NeonRegister a, NeonRegister b, NeonRegister mask) { return { vbslq_f64 (vreinterpretq_u64_f64 (mask.value), b.value, a.value) }; }
172
173 //==============================================================================
174 // Math
175 static NeonRegister abs (NeonRegister x) { return { vabsq_f64 (x.value) }; }
176 static NeonRegister floor (NeonRegister x) { return { vrndmq_f64 (x.value) }; }
177 static NeonRegister ceil (NeonRegister x) { return { vrndpq_f64 (x.value) }; }
178 static NeonRegister mul (NeonRegister a, NeonRegister b) { return { vmulq_f64 (a.value, b.value) }; }
179 static NeonRegister div (NeonRegister a, NeonRegister b) { return { vdivq_f64 (a.value, b.value) }; }
180 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_f64 (a.value, b.value) }; }
181 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_f64 (a.value, b.value) }; }
182 static NeonRegister max (NeonRegister a, NeonRegister b) { return { vmaxq_f64 (a.value, b.value) }; }
183 static NeonRegister min (NeonRegister a, NeonRegister b) { return { vminq_f64 (a.value, b.value) }; }
184 static NeonRegister sqrt (NeonRegister x) { return { vsqrtq_f64 (x.value) }; }
186 static NeonRegister copysign (NeonRegister mag, NeonRegister sgn) { return { vbslq_f64 (vreinterpretq_u64_f64 (vdupq_n_f64 (double (-0.0))), sgn.value, mag.value) }; }
187 static NeonRegister fma (NeonRegister a, NeonRegister b, NeonRegister c) { return { vfmaq_f64 (c.value, a.value, b.value) }; }
188 static NeonRegister fms (NeonRegister a, NeonRegister b, NeonRegister c) { return { vfmsq_f64 (c.value, a.value, b.value) }; }
189
190 //==============================================================================
191 // Type conversion
192 static NeonRegister<int64_t> convertToInt (NeonRegister x);
193 static NeonRegister<int64_t> reinterpretAsInt (NeonRegister x);
194 // clang-format on
195};
196
197template <>
198struct NeonRegister<int32_t>
199{
200 static constexpr size_t numElements = 4;
201
202 using NativeType = int32x4_t;
203 int32x4_t value;
204
205 //==============================================================================
206 // Loading
207 // clang-format off
208 static NeonRegister load (const int32_t* d) { return { vld1q_s32 (d) }; }
209 static NeonRegister broadcast (int32_t x) { return { vdupq_n_s32 (x) }; }
210
211 //==============================================================================
212 // Storing
213 void store (int32_t* d) const { vst1q_s32 (d, value); }
214
215 //==============================================================================
216 // Bit Operations
217 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vandq_s32 (a.value, b.value) }; }
218 static NeonRegister bitwiseOr (NeonRegister a, NeonRegister b) { return { vorrq_s32 (a.value, b.value) }; }
219 static NeonRegister bitwiseBlend (NeonRegister a, NeonRegister b, NeonRegister mask) { return { vbslq_s32 (vreinterpretq_u32_s32 (mask.value), b.value, a.value) }; }
220
221 //==============================================================================
222 // Generate Compare Masks
223 template <CompareOp op>
224 static NeonRegister compare (NeonRegister a, NeonRegister b) { return { vreinterpretq_s32_u32 (detail::NeonCompare<op, int32_t>::cmp (a.value, b.value)) }; }
225
226 //==============================================================================
227 // Math
228 static NeonRegister abs (NeonRegister x) { return { vabsq_s32 (x.value) }; }
229 static NeonRegister mul (NeonRegister a, NeonRegister b) { return { vmulq_s32 (a.value, b.value) }; }
230 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_s32 (a.value, b.value) }; }
231 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_s32 (a.value, b.value) }; }
232 static NeonRegister max (NeonRegister a, NeonRegister b) { return { vmaxq_s32 (a.value, b.value) }; }
233 static NeonRegister min (NeonRegister a, NeonRegister b) { return { vminq_s32 (a.value, b.value) }; }
234
235 //==============================================================================
236 // Type conversion
237 static NeonRegister<float> convertToFp (NeonRegister x) { return { vcvtq_f32_s32 (x.value) }; }
238 static NeonRegister<float> reinterpretAsFp (NeonRegister x) { return { vreinterpretq_f32_s32 (x.value) }; }
239 // clang-format on
240};
241
242template <>
243struct NeonRegister<uint32_t>
244{
245 static constexpr size_t numElements = 4;
246
247 using NativeType = uint32x4_t;
248 uint32x4_t value;
249
250 //==============================================================================
251 // Loading
252 // clang-format off
253 static NeonRegister load (const uint32_t* d) { return { vld1q_u32 (d) }; }
254 static NeonRegister broadcast (uint32_t x) { return { vdupq_n_u32 (x) }; }
255
256 //==============================================================================
257 // Storing
258 void store (uint32_t* d) const { vst1q_u32 (d, value); }
259
260 //==============================================================================
261 // Bit Operations
262 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vandq_u32 (a.value, b.value) }; }
263 static NeonRegister bitwiseOr (NeonRegister a, NeonRegister b) { return { vorrq_u32 (a.value, b.value) }; }
264
265 //==============================================================================
266 // Math
267 static NeonRegister mul (NeonRegister a, NeonRegister b) { return { vmulq_u32 (a.value, b.value) }; }
268 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_u32 (a.value, b.value) }; }
269 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_u32 (a.value, b.value) }; }
270 static NeonRegister max (NeonRegister a, NeonRegister b) { return { vmaxq_u32 (a.value, b.value) }; }
271 static NeonRegister min (NeonRegister a, NeonRegister b) { return { vminq_u32 (a.value, b.value) }; }
272 // clang-format on
273};
274
275template <>
276struct NeonRegister<int64_t>
277{
278 static constexpr size_t numElements = 2;
279
280 using NativeType = int64x2_t;
281 int64x2_t value;
282
283 //==============================================================================
284 // Loading
285 // clang-format off
286 static NeonRegister load (const int64_t* d) { return { vld1q_s64 (d) }; }
287 static NeonRegister broadcast (int64_t x) { return { vdupq_n_s64 (x) }; }
288
289 //==============================================================================
290 // Storing
291 void store (int64_t* d) const { vst1q_s64 (d, value); }
292
293 //==============================================================================
294 // Bit Operations
295 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vandq_s64 (a.value, b.value) }; }
296 static NeonRegister bitwiseOr (NeonRegister a, NeonRegister b) { return { vorrq_s64 (a.value, b.value) }; }
297
298 //==============================================================================
299 // Math
300 static NeonRegister abs (NeonRegister x) { return { vabsq_s64 (x.value) }; }
301 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_s64 (a.value, b.value) }; }
302 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_s64 (a.value, b.value) }; }
303
304 //==============================================================================
305 // Type conversion
306 static NeonRegister<double> convertToFp (NeonRegister x) { return { vcvtq_f64_s64 (x.value) }; }
307 static NeonRegister<double> reinterpretAsFp (NeonRegister x) { return { vreinterpretq_f64_s64 (x.value) }; }
308 // clang-format on
309};
310
311template <>
312struct NeonRegister<uint64_t>
313{
314 static constexpr size_t numElements = 2;
315
316 using NativeType = uint64x2_t;
317 uint64x2_t value;
318
319 //==============================================================================
320 // Loading
321 // clang-format off
322 static NeonRegister load (const uint64_t* d) { return { vld1q_u64 (d) }; }
323 static NeonRegister broadcast (uint64_t x) { return { vdupq_n_u64 (x) }; }
324
325 //==============================================================================
326 // Storing
327 void store (uint64_t* d) const { vst1q_u64 (d, value); }
328
329 //==============================================================================
330 // Bit Operations
331 static NeonRegister bitwiseAnd (NeonRegister a, NeonRegister b) { return { vandq_u64 (a.value, b.value) }; }
332 static NeonRegister bitwiseOr (NeonRegister a, NeonRegister b) { return { vorrq_u64 (a.value, b.value) }; }
333
334 //==============================================================================
335 // Math
336 static NeonRegister add (NeonRegister a, NeonRegister b) { return { vaddq_u64 (a.value, b.value) }; }
337 static NeonRegister sub (NeonRegister a, NeonRegister b) { return { vsubq_u64 (a.value, b.value) }; }
338 // clang-format on
339};
340
341inline NeonRegister<int32_t> NeonRegister<float>::convertToInt (NeonRegister<float> x) { return { vcvtq_s32_f32 (x.value) }; }
342inline NeonRegister<int32_t> NeonRegister<float>::reinterpretAsInt (NeonRegister<float> x) { return { vreinterpretq_s32_f32 (x.value) }; }
343inline NeonRegister<int64_t> NeonRegister<double>::convertToInt (NeonRegister<double> x) { return { vcvtq_s64_f64 (x.value) }; }
344inline NeonRegister<int64_t> NeonRegister<double>::reinterpretAsInt (NeonRegister<double> x) { return { vreinterpretq_s64_f64 (x.value) }; }
345
346#if VCTR_APPLE
347inline NeonRegister<float> NeonRegister<float>::pow (NeonRegister<float> x, NeonRegister<int32_t> y) { return { vipowf (x.value, y.value) }; }
348#endif
349
350#endif
351
352} // namespace vctr
constexpr ExpressionChainBuilder< expressions::Log10 > log10
Computes the logarithm to the base of ten of the source values.
Definition: Log10.h:84
constexpr ExpressionChainBuilder< expressions::Sin > sin
Computes the sine of each source element.
Definition: Sin.h:90
constexpr ExpressionChainBuilder< expressions::Asinh > asinh
Computes the inverse hyperbolic sine of each source element.
Definition: Asinh.h:90
constexpr ExpressionChainBuilder< expressions::Exp > exp
Computes e (Euler's number, 2.7182818...) raised to the source vector elements power.
Definition: Exp.h:122
constexpr ExpressionChainBuilder< expressions::Asin > asin
Computes the inverse sine of each source element.
Definition: Asin.h:90
constexpr ExpressionChainBuilder< expressions::Acosh > acosh
Computes the inverse hyperbolic cosine of each source element.
Definition: Acosh.h:90
constexpr ExpressionChainBuilder< expressions::Atan > atan
Computes the inverse tangent of each source element.
Definition: Atan.h:90
constexpr ExpressionChainBuilder< expressions::Cosh > cosh
Computes the hyperbolic cosine of each source element.
Definition: Cosh.h:90
constexpr ExpressionChainBuilder< expressions::Acos > acos
Computes the inverse cosine of each source element.
Definition: Acos.h:90
constexpr ExpressionChainBuilder< expressions::Cos > cos
Computes the cosine of each source element.
Definition: Cos.h:90
constexpr ExpressionChainBuilder< expressions::Tan > tan
Computes the tangent of each source element.
Definition: Tan.h:90
constexpr ExpressionChainBuilder< expressions::Sqrt > sqrt
Computes the square root of the source values.
Definition: Sqrt.h:115
constexpr ExpressionChainBuilder< expressions::Atanh > atanh
Computes the inverse hyperbolic tangent of each source element.
Definition: Atanh.h:90
constexpr auto pow(SrcBaseType &&bases, SrcExpType &&exponents)
Returns an expression that raises the elements in bases element-wise to the power of the elements in ...
Definition: Pow.h:213
constexpr ExpressionChainBuilder< expressions::Max > max
Computes the maximum value of the source values.
Definition: Max.h:198
constexpr ExpressionChainBuilder< expressions::Abs > abs
Computes the absolute value of the source values.
Definition: Abs.h:147
constexpr ExpressionChainBuilder< expressions::Log2 > log2
Computes the logarithm to the base of two of the source values.
Definition: Log2.h:93
constexpr ExpressionChainBuilder< expressions::Sinh > sinh
Computes the hyperbolic sine of each source element.
Definition: Sinh.h:90
constexpr ExpressionChainBuilder< expressions::Min > min
Computes the minimum value of the source values.
Definition: Min.h:198
constexpr ExpressionChainBuilder< expressions::Tanh > tanh
Computes the hyperbolic tangent of each source element.
Definition: Tanh.h:90
The main namespace of the VCTR project.
Definition: Array.h:24
CompareOp
Possible types of (SIMD) compare operations.
Definition: CompareOp.h:41
Definition: NeonRegister.h:28