SuperDex Physics C++ API
Loading...
Searching...
No Matches
arm_simd_int64_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<int64_t, 2>
27*/
28template <>
29class Simd<int64_t, 2> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(int64_t, 2, int64x2_t);
32
33 MOCHI_FORCE_INLINE Simd(int64_t a, int64_t 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_s64(a)} {}
36
37 template <int i>
38 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar Get(Simd v) {
39 static_assert(i >= 0 && i < kSize, "Index out of range");
40 return vgetq_lane_s64(v.raw, i);
41 }
42
43 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
44 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "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_s64(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 x, int y>
74 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Blend(Simd a, Simd b) {
75 static_assert(x >= 0 && x <= 1 && y >= 0 && y <= 1, "invalid blend index");
76 if constexpr (x == 0 && y == 0) {
77 return a;
78 } else if constexpr (x == 1 && y == 1) {
79 return b;
80 } else {
81 auto mask = int64x2_t{x ? (int64_t)0 : (int64_t)-1, y ? (int64_t)0 : (int64_t)-1};
82 return Select(mask, a, b);
83 }
84 }
85
86 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Broadcast(Scalar const* p) {
87 return Simd{*p};
88 }
89
90 template <int i>
91 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Broadcast(Simd v) {
92 static_assert(i >= 0 && i < kSize, "Index out of range");
93 return vdupq_laneq_s64(v.raw, i);
94 }
95
96 template <int N = 2>
97 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HMin(Simd a) {
98 static_assert(N == 2, "Unsupported N");
99 return superdex::Min(a.raw[0], a.raw[1]);
100 }
101
102 template <int N = 2>
103 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HMax(Simd a) {
104 static_assert(N == 2, "Unsupported N");
105 return superdex::Max(a.raw[0], a.raw[1]);
106 }
107
108 template <int N>
109 [[nodiscard]] MOCHI_FORCE_INLINE static Scalar HSum(Simd a) {
110 static_assert(N == 2, "Unsupported N");
111 return vaddvq_s64(a.raw);
112 }
113
114 [[nodiscard]] MOCHI_FORCE_INLINE static Simd HSumEach(Simd a, Simd b) {
115 return vpaddq_s64(a.raw, b.raw);
116 }
117
118 template <int N = kSize>
119 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Load([[maybe_unused]] Scalar const* ptr) {
120 static_assert(N >= 0 && N <= kSize);
121 if constexpr (N == 0) {
122 return Simd::Zero();
123 } else if constexpr (N == 1) {
124 return Simd{ptr[0], 0};
125 } else {
126 return vld1q_s64(ptr);
127 }
128 }
129
130 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Load(Scalar const* ptr, int n) {
131 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
132 // clang-format off
133 switch (n) {
134 case 1: return Load<1>(ptr);
135 case 2: return Load<2>(ptr);
136 MOCHI_UNLIKELY default: return Zero();
137 } // clang-format on
138 }
139
140 template <int kTupleCount = kSize>
141 MOCHI_FORCE_INLINE static void
142 LoadTransposed(int64_t const* ptr, Simd& out0, Simd& out1, Simd& out2) {
143 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Unsupported kTupleCount");
144 if constexpr (kTupleCount == 1) {
145 out0.raw = int64x2_t{ptr[0], 0};
146 out1.raw = int64x2_t{ptr[1], 0};
147 out2.raw = int64x2_t{ptr[2], 0};
148 } else {
149 int64x2x3_t result = vld3q_s64(ptr);
150 out0.raw = result.val[0];
151 out1.raw = result.val[1];
152 out2.raw = result.val[2];
153 }
154 }
155
156 // Branchless: each lane loads one tuple from a selected address, inactive lanes from zeros.
157 MOCHI_FORCE_INLINE static void
158 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
159 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
160 int64_t const* const zero = arm_simd::kZeroTuple<int64_t>;
161 int64x2x3_t r = vld3q_dup_s64(count > 0 ? ptr : zero);
162 r = vld3q_lane_s64(count > 1 ? ptr + 3 : zero, r, 1);
163 out0.raw = r.val[0];
164 out1.raw = r.val[1];
165 out2.raw = r.val[2];
166 }
167
168 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Min(Simd a, Simd b) {
169 return vbslq_s64(vcltq_s64(a.raw, b.raw), a.raw, b.raw);
170 }
171
172 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Max(Simd a, Simd b) {
173 return vbslq_s64(vcgtq_s64(a.raw, b.raw), a.raw, b.raw);
174 }
175
176 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Select(Simd mask, Simd a, Simd b) {
177 return vbslq_s64(vreinterpretq_u64_s64(mask.raw), a.raw, b.raw);
178 }
179
180 // return Simd{v[x], v[y]}
181 template <int x = 0, int y = 1>
182 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Shuffle(Simd v) {
183 static_assert(x >= 0 && x < 2, "Invalid index");
184 static_assert(y >= 0 && y < 2, "Invalid index");
185 if constexpr (x == 0 && y == 0) {
186 return Broadcast<0>(v);
187 } else if constexpr (x == 0 && y == 1) {
188 return v; // no change
189 } else if constexpr (x == 1 && y == 0) {
190 return vcombine_s64(vget_high_s64(v.raw), vget_low_s64(v.raw));
191 } else if constexpr (x == 1 && y == 1) {
192 return Broadcast<1>(v);
193 }
194 }
195
196 template <int N = kSize>
197 MOCHI_FORCE_INLINE static void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
198 static_assert(N >= 0 && N <= kSize);
199 if constexpr (N == 0) {
200 } else if constexpr (N == 1) {
201 vst1q_lane_s64(ptr, v.raw, 0);
202 } else {
203 vst1q_s64(ptr, v.raw);
204 }
205 }
206
207 MOCHI_FORCE_INLINE static void Store(Scalar* ptr, Simd v, int n) {
208 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
209 // clang-format off
210 switch (n) {
211 case 1: Store<1>(ptr, v); break;
212 case 2: Store<2>(ptr, v); break;
213 MOCHI_UNLIKELY default: break;
214 } // clang-format on
215 }
216
217 MOCHI_FORCE_INLINE static int StoreSelected(int64_t* ptr, Simd condition, Simd values) {
218 uint64x2_t shifted = vshrq_n_u64(vreinterpretq_u64_s64(condition.raw), 63);
219 uint32_t bit0 = vgetq_lane_u64(shifted, 0);
220 uint32_t bit1 = vgetq_lane_u64(shifted, 1);
221 uint32_t mask = bit0 | (bit1 << 1);
222 uint32_t count = bit0 + bit1;
223 uint8x16_t pattern = vld1q_u8(arm_simd::kStoreSelectedShuffleTableD2[mask]);
224 uint8x16_t packed = vqtbl1q_u8(vreinterpretq_u8_s64(values.raw), pattern);
225 vst1q_s64(ptr, vreinterpretq_s64_u8(packed));
226 return static_cast<int>(count);
227 }
228
229 template <int kTupleCount = kSize>
230 MOCHI_FORCE_INLINE static void StoreTransposed(int64_t* ptr, Simd a, Simd b, Simd c) {
231 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
232 if constexpr (kTupleCount == 1) {
233 ptr[0] = a.raw[0];
234 ptr[1] = b.raw[0];
235 ptr[2] = c.raw[0];
236 } else {
237 vst3q_s64(ptr, int64x2x3_t({a.raw, b.raw, c.raw}));
238 }
239 }
240
241 // Inactive lanes write tuple 0, which lane 0 stores last.
242 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
243 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
244 if (count == 0) {
245 return;
246 }
247 int64x2x3_t const v = {a.raw, b.raw, c.raw};
248 vst3q_lane_s64(count > 1 ? ptr + 3 : ptr, v, 1);
249 vst3q_lane_s64(ptr, v, 0);
250 }
251
252 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Zero() {
253 return vdupq_n_s64(0);
254 }
255
256 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
257 return vreinterpretq_s64_u64(vcltq_s64(this->raw, rhs.raw));
258 }
259
260 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
261 return vreinterpretq_s64_u64(vcgtq_s64(this->raw, rhs.raw));
262 }
263
264 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
265 return vreinterpretq_s64_u64(vcleq_s64(this->raw, rhs.raw));
266 }
267
268 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
269 return vreinterpretq_s64_u64(vcgeq_s64(this->raw, rhs.raw));
270 }
271
272 [[nodiscard]] MOCHI_FORCE_INLINE static Simd Equal(Simd a, Simd b) {
273 return vreinterpretq_s64_u64(vceqq_s64(a.raw, b.raw));
274 }
275
276 [[nodiscard]] MOCHI_FORCE_INLINE static Simd NotEqual(Simd a, Simd b) {
277 return ~Equal(a, b);
278 }
279
280 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
281 return vnegq_s64(raw);
282 }
283
284 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
285 return vaddq_s64(raw, rhs.raw);
286 }
287
288 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
289 return vsubq_s64(raw, rhs.raw);
290 }
291
292 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
293 return Simd{
294 // NEON does not implement long integer multiplication
295 raw[0] * rhs.raw[0],
296 raw[1] * rhs.raw[1]};
297 }
298
299 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
300 return Simd{
301 // NEON does not implement long integer division
302 raw[0] / rhs.raw[0],
303 raw[1] / rhs.raw[1]};
304 }
305
306 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
307 uint32x2_t t = vqmovn_u64(vceqq_s64(raw, rhs.raw));
308 return vget_lane_u64(vreinterpret_u64_u32(t), 0) == uint64_t(-1);
309 }
310
311 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
312 return !(*this == rhs);
313 }
314
315 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
316 return vreinterpretq_s64_u32(vmvnq_u32(vreinterpretq_u32_s64(raw)));
317 }
318
319 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
320 return vandq_s64(raw, rhs.raw);
321 }
322
323 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
324 return vorrq_s64(raw, rhs.raw);
325 }
326
327 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
328 return veorq_s64(raw, rhs.raw);
329 }
330
331 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<<(int shift) const {
332 // NOTE: If shift were a constexpr, then vshlq_n_s64 would be better because the shift amount
333 // could be an immediate value. Fortunately, Clang appears to be smart enough to do the right
334 // thing.
335 auto vShift = Simd(shift);
336 return vshlq_s64(raw, vShift.raw);
337 }
338
339 template <int kShift>
340 [[nodiscard]] MOCHI_FORCE_INLINE static Simd ShiftRight(Simd a) {
341 static_assert(kShift >= 0 && kShift < 64, "Shift amount out-of-range");
342 return vshrq_n_s64(a.raw, kShift);
343 }
344};
345
346} // namespace superdex
347
348#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<<(int shift) 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
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
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
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
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:302
constexpr T Select(bool condition, T a, T b)
T Get(Simd< T, N > v)
Definition simd_inl.h:317
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
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
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
Simd< T, N > ShiftRight(Simd< T, N > a)
Definition simd_inl.h:274
#define MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(T, N, NativeT)