SuperDex Physics C++ API
Loading...
Searching...
No Matches
arm_simd_double_2_inl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include "arm_simd_inl.h" // for IntelliSense
20
21#if MOCHI_USE_SIMD && MOCHI_ARCH_ARM_NEON
22
23namespace superdex {
24
25/***********************************************************************************************
26 Simd<double, 2> (NEON Implementation)
27*/
28template <>
29class Simd<double, 2> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(double, 2, float64x2_t);
32
33 MOCHI_FORCE_INLINE Simd(double a, double b) : raw{a, b} {}
34 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
35 MOCHI_FORCE_INLINE Simd(U a) : raw{vdupq_n_f64(a)} {}
36
37 template <int i>
38 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar Get(Simd v) {
39 static_assert(i >= 0 && i < 2, "Index out of range");
40 return vgetq_lane_f64(v.raw, i);
41 }
42
43 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
44 MOCHI_ASSERT_VERBOSE(i >= 0 && i < 2, "Index out of range");
45 return raw[i];
46 }
47
48 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Set(Simd v, int i, Scalar value) {
49 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
50 auto result = v;
51 result.raw[i] = value;
52 return result;
53 }
54
55 template <int i>
56 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Set(Simd v, Scalar value) {
57 static_assert(i >= 0 && i < kSize, "Index out of range");
58 return Set(v, i, value);
59 }
60
61 template <int N>
62 [[nodiscard]] MOCHI_FORCE_INLINE static bool AllTrue(Simd v) {
63 static_assert(N == 1 || N == 2, "Invalid N");
64 uint64_t mask =
65 vget_lane_u64(vreinterpret_u64_u16(vqmovn_u32(vreinterpretq_u32_f64(v.raw))), 0);
66 if constexpr (N == 1) {
67 return (mask & 0x00000000FFFFFFFFULL) == 0x00000000FFFFFFFFULL;
68 } else {
69 return mask == 0xFFFFFFFFFFFFFFFFULL;
70 }
71 }
72
73 template <int N>
74 [[nodiscard]] MOCHI_FORCE_INLINE static bool AnyTrue(Simd v) {
75 static_assert(N == 1 || N == 2, "Invalid N");
76 uint64_t mask =
77 vget_lane_u64(vreinterpret_u64_u16(vqmovn_u32(vreinterpretq_u32_f64(v.raw))), 0);
78 if constexpr (N == 1) {
79 return (mask & 0x00000000FFFFFFFFULL) == 0x00000000FFFFFFFFULL;
80 } else {
81 return mask != 0;
82 }
83 }
84
85 template <int x, int y>
86 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Blend(Simd a, Simd b) {
87 static_assert(x >= 0 && x <= 1 && y >= 0 && y <= 1, "invalid blend index");
88 if constexpr (x == 0 && y == 0) {
89 return a;
90 } else if constexpr (x == 1 && y == 1) {
91 return b;
92 } else {
93 auto mask = vreinterpretq_f64_s64(
94 int64x2_t{x ? (int64_t)0 : (int64_t)-1, y ? (int64_t)0 : (int64_t)-1});
95 return Select(mask, a, b);
96 }
97 }
98
99 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Broadcast(double const* p) {
100 return Simd{*p};
101 }
102
103 template <int i>
104 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Broadcast(Simd v) {
105 static_assert(i >= 0 && i < 2, "Index out of range");
106 return vdupq_laneq_f64(v.raw, i);
107 }
108
109 template <int N = kSize>
110 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Load([[maybe_unused]] double const* ptr) {
111 static_assert(N >= 0 && N <= kSize);
112 if constexpr (N == 0) {
113 return Simd::Zero();
114 } else if constexpr (N == 1) {
115 return Simd{*ptr, 0.0};
116 } else {
117 return vld1q_f64(ptr);
118 }
119 }
120
121 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Load(Scalar const* ptr, int n) {
122 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
123 // clang-format off
124 switch (n) {
125 case 1: return Load<1>(ptr);
126 case 2: return Load<2>(ptr);
127 MOCHI_UNLIKELY default: return Zero();
128 } // clang-format on
129 }
130
131 [[nodiscard]] MOCHI_FORCE_INLINE static Simd LoadIndexed(
132 double const* ptr,
133 Simd<int64_t, 2> const& indices) {
134 return Simd<double, 2>{ptr[indices.raw[0]], ptr[indices.raw[1]]};
135 }
136
137 template <int kTupleCount = kSize>
138 MOCHI_FORCE_INLINE static void
139 LoadTransposed(double const* ptr, Simd& out0, Simd& out1, Simd& out2) {
140 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Unsupported kTupleCount");
141 if constexpr (kTupleCount == 1) {
142 out0.raw = float64x2_t{ptr[0], 0.0};
143 out1.raw = float64x2_t{ptr[1], 0.0};
144 out2.raw = float64x2_t{ptr[2], 0.0};
145 } else {
146 float64x2x3_t result = vld3q_f64(ptr);
147 out0.raw = result.val[0];
148 out1.raw = result.val[1];
149 out2.raw = result.val[2];
150 }
151 }
152
153 // Branchless: each lane loads one tuple from a selected address, inactive lanes from zeros.
154 MOCHI_FORCE_INLINE static void
155 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
156 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
157 double const* const zero = arm_simd::kZeroTuple<double>;
158 float64x2x3_t r = vld3q_dup_f64(count > 0 ? ptr : zero);
159 r = vld3q_lane_f64(count > 1 ? ptr + 3 : zero, r, 1);
160 out0.raw = r.val[0];
161 out1.raw = r.val[1];
162 out2.raw = r.val[2];
163 }
164
165 template <int N = kSize>
166 static void Store([[maybe_unused]] double* ptr, [[maybe_unused]] Simd v) {
167 static_assert(N >= 0 && N <= kSize);
168 if constexpr (N == 0) {
169 } else if constexpr (N == 1) {
170 *ptr = Get<0>(v);
171 } else {
172 vst1q_f64(ptr, v.raw);
173 }
174 }
175
176 MOCHI_FORCE_INLINE static void Store(Scalar* ptr, Simd v, int n) {
177 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
178 // clang-format off
179 switch (n) {
180 case 1: Store<1>(ptr, v); break;
181 case 2: Store<2>(ptr, v); break;
182 MOCHI_UNLIKELY default: break;
183 } // clang-format on
184 }
185
186 MOCHI_FORCE_INLINE static int StoreSelected(double* ptr, Simd condition, Simd values) {
187 uint64x2_t shifted = vshrq_n_u64(vreinterpretq_u64_f64(condition.raw), 63);
188 uint32_t bit0 = vgetq_lane_u64(shifted, 0);
189 uint32_t bit1 = vgetq_lane_u64(shifted, 1);
190 uint32_t mask = bit0 | (bit1 << 1);
191 uint32_t count = bit0 + bit1;
192 uint8x16_t pattern = vld1q_u8(arm_simd::kStoreSelectedShuffleTableD2[mask]);
193 uint8x16_t packed = vqtbl1q_u8(vreinterpretq_u8_f64(values.raw), pattern);
194 vst1q_f64(ptr, vreinterpretq_f64_u8(packed));
195 return static_cast<int>(count);
196 }
197
198 template <int kTupleCount = kSize>
199 MOCHI_FORCE_INLINE static void StoreTransposed(double* ptr, Simd a, Simd b, Simd c) {
200 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
201 if constexpr (kTupleCount == 1) {
202 ptr[0] = a.raw[0];
203 ptr[1] = b.raw[0];
204 ptr[2] = c.raw[0];
205 } else {
206 vst3q_f64(ptr, float64x2x3_t({a.raw, b.raw, c.raw}));
207 }
208 }
209
210 // Inactive lanes write tuple 0, which lane 0 stores last.
211 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
212 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
213 if (count == 0) {
214 return;
215 }
216 float64x2x3_t const v = {a.raw, b.raw, c.raw};
217 vst3q_lane_f64(count > 1 ? ptr + 3 : ptr, v, 1);
218 vst3q_lane_f64(ptr, v, 0);
219 }
220
221 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Select(Simd mask, Simd a, Simd b) {
222 return vbslq_f64(vreinterpretq_u64_f64(mask.raw), a.raw, b.raw);
223 }
224
225 // return Simd{v[x], v[y]}
226 template <int x = 0, int y = 1>
227 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Shuffle(Simd v) {
228 static_assert(x >= 0 && x < 2, "Invalid index");
229 static_assert(y >= 0 && y < 2, "Invalid index");
230 if constexpr (x == 0 && y == 0) {
231 return Broadcast<0>(v);
232 } else if constexpr (x == 0 && y == 1) {
233 return v; // no change
234 } else if constexpr (x == 1 && y == 0) {
235 return vcombine_f64(vget_high_f64(v.raw), vget_low_f64(v.raw));
236 } else if constexpr (x == 1 && y == 1) {
237 return Broadcast<1>(v);
238 }
239 }
240
241 [[nodiscard]] MOCHI_FORCE_INLINE static Simd SignBitMask() {
242 return vreinterpretq_f64_s64(vdupq_n_s64((int64_t)0x8000000000000000LL));
243 }
244
245 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Sqrt(Simd v) {
246 return vsqrtq_f64(v.raw);
247 }
248
249 [[nodiscard]] MOCHI_FORCE_INLINE static Simd RcpApprox(Simd v) {
250 return vrecpeq_f64(v.raw);
251 }
252
253 [[nodiscard]] MOCHI_FORCE_INLINE static Simd RcpSqrtApprox(Simd v) {
254 return vrsqrteq_f64(v.raw);
255 }
256
257 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Abs(Simd v) {
258 return vabsq_f64(v.raw);
259 }
260
261 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Min(Simd a, Simd b) {
262 return vminq_f64(a.raw, b.raw);
263 }
264
265 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Max(Simd a, Simd b) {
266 return vmaxq_f64(a.raw, b.raw);
267 }
268
269 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Floor(Simd a) {
270 return vrndmq_f64(a.raw);
271 }
272
273 [[nodiscard]] MOCHI_FORCE_INLINE static Simd FastRound(Simd v) {
274 return vrndaq_f64(v.raw);
275 }
276
277 [[nodiscard]] MOCHI_FORCE_INLINE static Simd MulAdd(Simd a, Simd b, Simd c) {
278 return vfmaq_f64(c.raw, b.raw, a.raw);
279 }
280
281 [[nodiscard]] MOCHI_FORCE_INLINE static Simd MulSub(Simd a, Simd b, Simd c) {
282 return MulAdd(a, b, -c);
283 }
284
285 [[nodiscard]] MOCHI_FORCE_INLINE static Simd NegMulAdd(Simd a, Simd b, Simd c) {
286 return vfmsq_f64(c.raw, b.raw, a.raw);
287 }
288
289 [[nodiscard]] MOCHI_FORCE_INLINE static Simd NegMulSub(Simd a, Simd b, Simd c) {
290 return NegMulAdd(a, b, -c);
291 }
292
293 template <int N = 2>
294 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HMin(Simd a) {
295 static_assert(N == 2, "Unsupported N");
296 return vminvq_f64(a.raw);
297 }
298
299 template <int N = 2>
300 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HMax(Simd a) {
301 static_assert(N == 2, "Unsupported N");
302 return vmaxvq_f64(a.raw);
303 }
304
305 template <int N>
306 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HSum(Simd a) {
307 static_assert(N == 2, "Unsupported N");
308 return vaddvq_f64(a.raw);
309 }
310
311 [[nodiscard]] MOCHI_FORCE_INLINE static Simd HSumEach(Simd a, Simd b) {
312 return vpaddq_f64(a.raw, b.raw);
313 }
314
315 template <int N>
316 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HProd(Simd a) {
317 static_assert(N == 2, "Unsupported N");
318 return Get<0>(a) * Get<1>(a);
319 }
320
321 template <int N>
322 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Dot(Simd a, Simd b) {
323 static_assert(N == 2, "Unsupported N");
324 return Simd{HSum<N>(a * b)};
325 }
326
327 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
328 return vreinterpretq_f64_u64(vcltq_f64(this->raw, rhs.raw));
329 }
330
331 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
332 return vreinterpretq_f64_u64(vcgtq_f64(this->raw, rhs.raw));
333 }
334
335 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
336 return vreinterpretq_f64_u64(vcleq_f64(this->raw, rhs.raw));
337 }
338
339 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
340 return vreinterpretq_f64_u64(vcgeq_f64(this->raw, rhs.raw));
341 }
342
343 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Equal(Simd a, Simd b) {
344 return vreinterpretq_f64_u64(vceqq_f64(a.raw, b.raw));
345 }
346
347 [[nodiscard]] MOCHI_FORCE_INLINE static Simd NotEqual(Simd a, Simd b) {
348 return ~Equal(a, b);
349 }
350
351 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Zero() {
352 return vdupq_n_f64(0);
353 }
354
355 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
356 return AllTrue<kSize>(Equal(*this, rhs));
357 }
358
359 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
360 return !(*this == rhs);
361 }
362
363 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
364 return vreinterpretq_f64_u32(vmvnq_u32(vreinterpretq_u32_f64(raw)));
365 }
366
367 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
368 return vnegq_f64(raw);
369 }
370
371 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
372 return vaddq_f64(raw, rhs.raw);
373 }
374
375 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
376 return vsubq_f64(raw, rhs.raw);
377 }
378
379 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
380 return vmulq_f64(raw, rhs.raw);
381 }
382
383 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
384 return vdivq_f64(raw, rhs.raw);
385 }
386
387 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
388 return vreinterpretq_f64_s64(
389 vandq_s64(vreinterpretq_s64_f64(raw), vreinterpretq_s64_f64(rhs.raw)));
390 }
391
392 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
393 return vreinterpretq_f64_s64(
394 vorrq_s64(vreinterpretq_s64_f64(raw), vreinterpretq_s64_f64(rhs.raw)));
395 }
396
397 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
398 return vreinterpretq_f64_s64(
399 veorq_s64(vreinterpretq_s64_f64(raw), vreinterpretq_s64_f64(rhs.raw)));
400 }
401};
402
403} // namespace superdex
404
405#endif // MOCHI_USE_SIMD && MOCHI_ARCH_ARM_NEON
Simd operator&(Simd rhs) const
bool operator==(Simd rhs) const
NativeType raw
Definition simd.h:174
Simd operator>(Simd rhs) const
Simd operator*(Simd rhs) const
Simd operator^(Simd rhs) const
Simd operator-() const
Simd operator>=(Simd rhs) const
Simd operator<(Simd rhs) const
bool operator!=(Simd rhs) const
Simd operator|(Simd rhs) const
static constexpr int kSize
Definition simd.h:96
Simd operator+(Simd rhs) const
Simd operator~() const
Simd operator/(Simd rhs) const
Simd operator<=(Simd rhs) const
Scalar operator[](int i) const
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
#define MOCHI_UNLIKELY
#define MOCHI_FORCE_INLINE
T Dot(Simd< T, N > a, Simd< T, N > b)
Definition simd.h:710
constexpr T NegMulAdd(T a, T b, T c)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
Definition simd_inl.h:287
Simd< T, 2 > HSumEach(Simd< T, 2 > a, Simd< T, 2 > b)
Returns {HSum(a), HSum(b)}.
Definition simd_inl.h:401
V LoadIndexed(typename V::Scalar const *ptr, Simd< I, V::kSize > indices)
Definition simd_inl.h:202
constexpr T const & Min(T const &a, T const &b)
constexpr auto Equal(T const &a, T const &b)
T HSum(Simd< T, N > a)
Definition simd_inl.h:391
T HMin(Simd< T, N > a)
Definition simd_inl.h:451
bool AllTrue(T const &a)
Definition basic_utils.h:60
constexpr T NegMulSub(T a, T b, T c)
Simd< T, N > Set(Simd< T, N > a, T value)
Definition simd_inl.h:327
constexpr auto NotEqual(T const &a, T const &b)
T HMax(Simd< T, N > a)
Definition simd_inl.h:461
V Broadcast(typename V::Scalar a)
Definition simd_inl.h:117
constexpr T Abs(T a)
Definition basic_utils.h:50
constexpr T MulSub(T a, T b, T c)
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:302
bool AnyTrue(T const &a)
Definition basic_utils.h:66
constexpr T Select(bool condition, T a, T b)
constexpr T Sqrt(T a)
T Get(Simd< T, N > v)
Definition simd_inl.h:317
constexpr T Floor(T a)
T HProd(Simd< T, N > a)
Definition simd_inl.h:441
constexpr T const & Max(T const &a, T const &b)
void LoadTransposed(T const *ptr, Simd< T, N > &out0, Simd< T, N > &out1, Simd< T, N > &out2)
Definition simd_inl.h:209
Simd< T, N > FastRound(Simd< T, N > a)
Definition simd_inl.h:486
void StoreTransposed(T *ptr, Simd< T, N > a, Simd< T, N > b, Simd< T, N > c)
Definition simd_inl.h:253
void Store(T *ptr, Simd< T, N > a)
Definition simd_inl.h:221
Simd< T, N > RcpSqrtApprox(Simd< T, N > a)
Definition simd_inl.h:371
constexpr T RcpApprox(T a)
constexpr T MulAdd(T a, T b, T c)
int StoreSelected(T *ptr, Simd< MaskT, N > condition, Simd< T, N > values)
Definition simd_inl.h:233
V Load(typename V::Scalar const *ptr)
Definition simd_inl.h:186
#define MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(T, N, NativeT)