SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_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 "x64_simd_inl.h" // for IntelliSense
20
21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
22
23namespace superdex {
24
25/***********************************************************************************************
26 Simd<int64_t, 2>
27*/
28template <>
29class Simd<int64_t, 2> {
30 public:
31 static_assert(sizeof(int64_t) == sizeof(long long));
32
33 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(int64_t, 2, __m128i);
34 Simd(int64_t low, int64_t high)
35 : raw(_mm_set_epi64x(static_cast<long long>(high), static_cast<long long>(low))) {} // SSE2
36 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
37 Simd(U a) : raw(_mm_set1_epi64x(static_cast<long long>(a))) {} // SSE2
38
39 template <int i>
40 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar Get(Simd v) {
41 static_assert(i >= 0 && i < kSize, "Index out of range");
42 return v[i];
43 }
44
45 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
46 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
47#if MOCHI_COMPILER_MSVC
48 return raw.m128i_i64[i];
49#else
50 return static_cast<Scalar>(raw[i]);
51#endif
52 }
53
54 template <int N>
55 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
56 static_assert(N >= 1 && N <= kSize, "Unsupported N");
57 int constexpr kLanes = (1 << N) - 1;
58 return (ToMask(v) & kLanes) == kLanes;
59 }
60
61 template <int x, int y>
62 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Blend(Simd a, Simd b) {
63 static_assert(x >= 0 && x < 2 && y >= 0 && y < 2, "invalid blend index");
64 if constexpr (x == 0 && y == 0) {
65 return a;
66 } else if constexpr (x == 1 && y == 1) {
67 return b;
68 } else {
69 return _mm_castpd_si128(
70 _mm_blend_pd(_mm_castsi128_pd(a.raw), _mm_castsi128_pd(b.raw), x | (y << 1))); // SSE4.1
71 }
72 }
73
74 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
75 return Simd{*p};
76 }
77
78 template <int i>
79 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
80 return Shuffle<i, i>(v);
81 }
82
83 template <int N = 2>
84 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
85 static_assert(N == 2, "Unsupported N");
86 return Get<0>(Min(a, Broadcast<1>(a)));
87 }
88
89 template <int N = 2>
90 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
91 static_assert(N == 2, "Unsupported N");
92 return Get<0>(Max(a, Broadcast<1>(a)));
93 }
94
95 template <int N>
96 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
97 static_assert(N == 2, "Unsupported N");
98 return Get<0>(a) + Get<1>(a);
99 }
100
101 [[nodiscard]] static MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b) {
102 return _mm_add_epi64(
103 _mm_unpacklo_epi64(a.raw, b.raw), _mm_unpackhi_epi64(a.raw, b.raw)); // SSE2
104 }
105
106 template <int N = kSize>
107 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
108 static_assert(N >= 0 && N <= kSize);
109 if constexpr (N == 0) {
110 return Simd::Zero();
111 } else if constexpr (N == 1) {
112 return Simd{*ptr, 0};
113 } else if constexpr (N == 2) {
114 return _mm_loadu_si128(reinterpret_cast<__m128i const*>(ptr)); // SSE
115 }
116 }
117
118 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
119 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
120#if MOCHI_ARCH_X64_AVX512
121 return _mm_maskz_loadu_epi64(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
122#else
123 switch (n) { // clang-format off
124 case 1: return Load<1>(ptr);
125 case 2: return Load<2>(ptr);
126 MOCHI_UNLIKELY default: return Zero();
127 } // clang-format on
128#endif
129 }
130
131 template <int kTupleCount = kSize>
132 MOCHI_FORCE_INLINE static void
133 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
134 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
135 constexpr int kCount1 = Clamp(kTupleCount * 3 - 2, 0, 2);
136 constexpr int kCount2 = Clamp(kTupleCount * 3 - 4, 0, 2);
137 auto a = _mm_castsi128_pd(Simd::Load<2>(ptr).raw); // [0,1]
138 auto b = _mm_castsi128_pd(Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 2).raw); // [2,3]
139 auto c = _mm_castsi128_pd(Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 4).raw); // [4,5]
140 Deinterleave3(a, b, c, out0, out1, out2);
141 }
142
143 MOCHI_FORCE_INLINE static void
144 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
145 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
146 int const n = count * 3;
147 auto a = _mm_castsi128_pd(Simd::Load(ptr, superdex::Min(n, 2)).raw); // [0,1]
148 auto b = _mm_castsi128_pd(Simd::Load(n > 2 ? ptr + 2 : ptr, Clamp(n - 2, 0, 2)).raw); // [2,3]
149 auto c = _mm_castsi128_pd(Simd::Load(n > 4 ? ptr + 4 : ptr, Clamp(n - 4, 0, 2)).raw); // [4,5]
150 Deinterleave3(a, b, c, out0, out1, out2);
151 }
152
153 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
154#if MOCHI_ARCH_X64_AVX512
155 return _mm_min_epi64(a.raw, b.raw); // AVX512VL
156#else
157 return Simd{superdex::Min(Get<0>(a), Get<0>(b)), superdex::Min(Get<1>(a), Get<1>(b))};
158#endif
159 }
160
161 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
162#if MOCHI_ARCH_X64_AVX512
163 return _mm_max_epi64(a.raw, b.raw); // AVX512VL
164#else
165 return Simd{superdex::Max(Get<0>(a), Get<0>(b)), superdex::Max(Get<1>(a), Get<1>(b))};
166#endif
167 }
168
169 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
170 return _mm_blendv_epi8(b.raw, a.raw, mask.raw); // SSE4.1
171 }
172
173 template <int x = 0, int y = 1>
174 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd v) {
175 static_assert(x >= 0 && x < 2, "Invalid index");
176 static_assert(y >= 0 && y < 2, "Invalid index");
177 if constexpr (x == 0 && y == 1) {
178 return v;
179 } else {
180 return _mm_castpd_si128(
181 _mm_shuffle_pd(_mm_castsi128_pd(v.raw), _mm_castsi128_pd(v.raw), x | (y << 1))); // SSE2
182 }
183 }
184
185 template <int N = kSize>
186 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
187 static_assert(N >= 0 && N <= kSize);
188 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
189 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
190 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
191 // go through the stack.
192 if constexpr (N == 0) {
193 } else if constexpr (N == 1) {
194 _mm_storel_epi64(reinterpret_cast<__m128i*>(ptr), v.raw); // SSE2
195 } else {
196 _mm_storeu_si128(reinterpret_cast<__m128i*>(ptr), v.raw); // SSE
197 }
198 }
199
200 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
201 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
202#if MOCHI_ARCH_X64_AVX512
203 _mm_mask_storeu_epi64(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
204#else
205 // With AVX2, this is faster than masked store for a predictable value of n.
206 // It is much slower for a random value of n.
207 switch (n) { // clang-format off
208 case 1: Store<1>(ptr, v); break;
209 case 2: Store<2>(ptr, v); break;
210 MOCHI_UNLIKELY default: break;
211 } // clang-format on
212#endif
213 }
214
215 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
216#if MOCHI_ARCH_X64_AVX512
217 auto const mask = static_cast<__mmask8>(ToMask(condition));
218 _mm_mask_compressstoreu_epi64(ptr, mask, values.raw); // AVX512VL
219 return _mm_popcnt_u32(mask);
220#else
221 auto mask = ToMask(condition);
222 auto swapped = _mm_castpd_si128(_mm_shuffle_pd(
223 _mm_castsi128_pd(values.raw), _mm_castsi128_pd(values.raw), 1)); // swap halves
224 auto blendMask = _mm_set1_epi32((mask & 1) - 1); // swap first bit of mask is zero
225 auto packed = _mm_blendv_epi8(values.raw, swapped, blendMask);
226 _mm_storeu_si128(reinterpret_cast<__m128i*>(ptr), packed);
227 return _mm_popcnt_u32(mask);
228#endif
229 }
230
231 template <int kTupleCount = kSize>
232 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
233 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
234 Simd x0, x1, x2;
235 Interleave3(a, b, c, x0, x1, x2);
236 Simd::Store<2>(ptr, x0);
237 constexpr int kCount1 = Clamp(kTupleCount * 3 - 2, 0, 2);
238 constexpr int kCount2 = Clamp(kTupleCount * 3 - 4, 0, 2);
239 if constexpr (kCount1 > 0) {
240 Simd::Store<kCount1>(ptr + 2, x1);
241 }
242 if constexpr (kCount2 > 0) {
243 Simd::Store<kCount2>(ptr + 4, x2);
244 }
245 }
246
247 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
248 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
249 Simd x0, x1, x2;
250 Interleave3(a, b, c, x0, x1, x2);
251#if MOCHI_ARCH_X64_AVX512
252 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
253 // back into branches. A zero mask stores nothing.
254 uint32_t const bits = (1u << (count * 3)) - 1;
255 _mm_mask_storeu_epi64(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
256 _mm_mask_storeu_epi64(ptr + (count > 0 ? 2 : 0), static_cast<__mmask8>(bits >> 2), x1.raw);
257 _mm_mask_storeu_epi64(ptr + (count > 1 ? 4 : 0), static_cast<__mmask8>(bits >> 4), x2.raw);
258#else
259 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
260 // counts, which clang may turn back into branches. A zero mask stores nothing.
261 auto const n = _mm_set1_epi64x(count * 3);
262 auto const mask0 = _mm_cmpgt_epi64(n, _mm_set_epi64x(1, 0)); // SSE4.2
263 auto const mask1 = _mm_cmpgt_epi64(n, _mm_set_epi64x(3, 2));
264 auto const mask2 = _mm_cmpgt_epi64(n, _mm_set_epi64x(5, 4));
265 _mm_maskstore_epi64(reinterpret_cast<long long*>(ptr), mask0, x0.raw); // AVX2
266 _mm_maskstore_epi64(reinterpret_cast<long long*>(ptr + (count > 0 ? 2 : 0)), mask1, x1.raw);
267 _mm_maskstore_epi64(reinterpret_cast<long long*>(ptr + (count > 1 ? 4 : 0)), mask2, x2.raw);
268#endif
269 }
270
271 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
272 return _mm_setzero_si128(); // SSE2
273 }
274
275 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
276 return _mm_cmpgt_epi64(rhs.raw, this->raw); // SSE2
277 }
278
279 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
280 return _mm_cmpgt_epi64(this->raw, rhs.raw); // SSE2
281 }
282
283 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
284 return ~(*this > rhs); // No native support until AVX512
285 }
286
287 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
288 return ~(*this < rhs); // No native support until AVX512
289 }
290
291 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
292 return _mm_cmpeq_epi64(a.raw, b.raw); // SSE2
293 }
294
295 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
296 return ~Equal(a, b); // // No native support until AVX512
297 }
298
299 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
300 return ToMask(Equal(*this, rhs)) == 0x3; // All values equal
301 }
302
303 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
304 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
305 }
306
307 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
308 __m128i ones = _mm_cmpeq_epi64(raw, raw); // SSE2
309 return _mm_xor_si128(raw, ones); // SSE2
310 }
311
312 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
313 return _mm_sub_epi64(_mm_setzero_si128(), raw); // SSE2, SSE2
314 }
315
316 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
317 return _mm_add_epi64(raw, rhs.raw); // SSE
318 }
319
320 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
321 return _mm_sub_epi64(raw, rhs.raw); // SSE
322 }
323
324 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
325#if MOCHI_ARCH_X64_AVX512
326 return _mm_mullo_epi64(raw, rhs.raw); // AVX512VL
327#else
328 return Simd{Get<0>(*this) * Get<0>(rhs), Get<1>(*this) * Get<1>(rhs)};
329#endif
330 }
331
332 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
333#if MOCHI_ARCH_X64_SVML
334 return _mm_div_epi64(raw, rhs.raw); // SSE
335#else
336 // Fallback
337 return Simd{Get<0>(*this) / Get<0>(rhs), Get<1>(*this) / Get<1>(rhs)};
338#endif
339 }
340
341 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
342 return _mm_and_si128(raw, rhs.raw); // SSE2
343 }
344
345 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
346 return _mm_or_si128(raw, rhs.raw); // SSE2
347 }
348
349 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
350 return _mm_xor_si128(raw, rhs.raw); // SSE2
351 }
352
353 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<<(int rhs) const {
354 return _mm_slli_epi64(raw, rhs); // SSE2
355 }
356
357 template <int kShift>
358 [[nodiscard]] MOCHI_FORCE_INLINE static Simd ShiftRight(Simd a) {
359 static_assert(kShift >= 0 && kShift < 64, "Shift amount out-of-range");
360 if constexpr (kShift == 0) {
361 return a;
362 } else {
363#if MOCHI_ARCH_X64_AVX512
364 return _mm_srai_epi64(a.raw, kShift); // AVX512VL
365#else
366 auto shifted = _mm_srli_epi64(a.raw, kShift); // SSE2
367 auto signMask = _mm_cmpgt_epi64(_mm_setzero_si128(), a.raw); // SSE4.2
368 auto signFill = _mm_slli_epi64(signMask, 64 - kShift); // SSE2
369 return _mm_or_si128(shifted, signFill); // SSE2
370#endif
371 }
372 }
373
374 private:
375 // One bit per lane, from the lane's sign bit.
376 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
377#if MOCHI_ARCH_X64_AVX512
378 return _mm_movepi64_mask(a.raw); // AVX512DQ, AVX512VL
379#else
380 return _mm_movemask_pd(_mm_castsi128_pd(a.raw)); // SSE2, SSE2
381#endif
382 }
383
384 // Splits 6 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
385 MOCHI_FORCE_INLINE static void
386 Deinterleave3(__m128d a, __m128d b, __m128d c, Simd& out0, Simd& out1, Simd& out2) {
387 out0.raw = _mm_castpd_si128(_mm_shuffle_pd(a, b, 0b0010)); // [0,3]
388 out1.raw = _mm_castpd_si128(_mm_shuffle_pd(a, c, 0b0001)); // [1,4]
389 out2.raw = _mm_castpd_si128(_mm_shuffle_pd(b, c, 0b0010)); // [2,5]
390 }
391
392 // Inverse of Deinterleave3: out0, out1, out2 are the 6 interleaved scalars in memory order.
393 MOCHI_FORCE_INLINE static void
394 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
395 // a = [0,3], b = [1,4], c = [2,5]
396 auto const a_ = _mm_castsi128_pd(a.raw);
397 auto const b_ = _mm_castsi128_pd(b.raw);
398 auto const c_ = _mm_castsi128_pd(c.raw);
399 out0.raw = _mm_castpd_si128(_mm_shuffle_pd(a_, b_, 0b00)); // [0,1]
400 out1.raw = _mm_castpd_si128(_mm_shuffle_pd(c_, a_, 0b10)); // [2,3]
401 out2.raw = _mm_castpd_si128(_mm_shuffle_pd(b_, c_, 0b11)); // [4,5]
402 }
403};
404
405} // namespace superdex
406
407#endif // MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
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
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 ValT Clamp(ValT value, MinT min, MaxT max)
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)