SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_int64_4_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, 4>
27*/
28template <>
29class Simd<int64_t, 4> {
30 public:
31 static_assert(sizeof(int64_t) == sizeof(long long));
32
33 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(int64_t, 4, __m256i);
34 Simd(int64_t a, int64_t b, int64_t c = 0, int64_t d = 0)
35 : raw(_mm256_set_epi64x(
36 static_cast<long long>(d),
37 static_cast<long long>(c),
38 static_cast<long long>(b),
39 static_cast<long long>(a))) {} // AVX
40 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
41 Simd(U a) : raw(_mm256_set1_epi64x(static_cast<long long>(a))) {} // AVX
42
43 template <int i>
44 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar Get(Simd v) {
45 static_assert(i >= 0 && i < kSize, "Index out of range");
46 return v[i];
47 }
48
49 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
50 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
51#if MOCHI_COMPILER_MSVC
52 return raw.m256i_i64[i];
53#else
54 return static_cast<Scalar>(raw[i]);
55#endif
56 }
57
58 template <int iHalf>
59 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<int64_t, 2> GetHalf(Simd a) {
60 static_assert(iHalf == 0 || iHalf == 1);
61 return _mm256_extracti128_si256(a.raw, iHalf); // AVX2
62 }
63
64 template <int N>
65 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
66 static_assert(N >= 1 && N <= kSize, "Unsupported N");
67 int constexpr kLanes = (1 << N) - 1;
68 return (ToMask(v) & kLanes) == kLanes;
69 }
70
71 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
72 return Simd{*p};
73 }
74
75 template <int i>
76 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
77 return Shuffle<i, i, i, i>(v);
78 }
79
80 template <int N = 4>
81 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
82 static_assert(N >= 2 && N <= 4, "Unsupported N");
83 using HalfT = Simd<Scalar, 2>;
84 if constexpr (N == 2) {
85 return Get<0>(Min(a, Broadcast<1>(a)));
86 } else if constexpr (N == 3) {
87 auto lo = GetHalf<0>(a);
88 auto hi = GetHalf<1>(a);
89 return HalfT::Get<0>(HalfT::Min(HalfT::Min(lo, HalfT::Broadcast<1>(lo)), hi));
90 } else {
91 return HalfT::HMin(HalfT::Min(GetHalf<0>(a), GetHalf<1>(a)));
92 }
93 }
94
95 template <int N = 4>
96 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
97 static_assert(N >= 2 && N <= 4, "Unsupported N");
98 using HalfT = Simd<Scalar, 2>;
99 if constexpr (N == 2) {
100 return Get<0>(Max(a, Broadcast<1>(a)));
101 } else if constexpr (N == 3) {
102 auto lo = GetHalf<0>(a);
103 auto hi = GetHalf<1>(a);
104 return HalfT::Get<0>(HalfT::Max(HalfT::Max(lo, HalfT::Broadcast<1>(lo)), hi));
105 } else {
106 return HalfT::HMax(HalfT::Max(GetHalf<0>(a), GetHalf<1>(a)));
107 }
108 }
109
110 template <int N>
111 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
112 static_assert(N >= 2 && N <= 4, "Unsupported N");
113 if constexpr (N == 2) {
114 return Get<0>(a) + Get<1>(a);
115 } else if constexpr (N == 3) {
116 using HalfT = Simd<int64_t, 2>;
117 HalfT tmp = GetHalf<0>(a) + GetHalf<1>(a);
118 return HalfT::Get<0>(tmp) + Get<1>(a); // (a[0] + a[2]) + a[1]
119 } else if constexpr (N == 4) {
120 using HalfT = Simd<int64_t, 2>;
121 HalfT tmp = GetHalf<0>(a) + GetHalf<1>(a);
122 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp); // (a[0] + a[2]) + (a[1] + a[3]);
123 }
124 }
125
126 [[nodiscard]] static MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b, Simd c, Simd d) {
127 // ac = {a[0] + a[2], a[1] + a[3], c[0] + c[2], c[1] + c[3]}, and bd likewise.
128 auto const ac = _mm256_add_epi64(
129 _mm256_blend_epi32(a.raw, c.raw, 0xF0),
130 _mm256_permute2x128_si256(a.raw, c.raw, 0x21)); // AVX2
131 auto const bd = _mm256_add_epi64(
132 _mm256_blend_epi32(b.raw, d.raw, 0xF0),
133 _mm256_permute2x128_si256(b.raw, d.raw, 0x21)); // AVX2
134 // {ac[0], bd[0], ac[2], bd[2]} + {ac[1], bd[1], ac[3], bd[3]}.
135 return _mm256_add_epi64(_mm256_unpacklo_epi64(ac, bd), _mm256_unpackhi_epi64(ac, bd)); // AVX2
136 }
137
138 template <int N = kSize>
139 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
140 static_assert(N >= 0 && N <= kSize);
141 if constexpr (N == 0) {
142 return Simd::Zero();
143 } else if constexpr (N == 1) {
144 return Simd{*ptr, 0, 0, 0};
145 } else if constexpr (N == 2) {
146 auto const low = _mm_loadu_si128(reinterpret_cast<__m128i const*>(ptr)); // SSE2
147 return _mm256_zextsi128_si256(low); // AVX
148 } else if constexpr (N == 3) {
149 __m256i mask = _mm256_set_epi32(0, 0, -1, -1, -1, -1, -1, -1); // AVX
150 return _mm256_maskload_epi64(reinterpret_cast<long long const*>(ptr), mask); // AVX2
151 } else {
152 return _mm256_loadu_si256(reinterpret_cast<__m256i const*>(ptr)); // AVX
153 }
154 }
155
156 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
157 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
158#if MOCHI_ARCH_X64_AVX512
159 return _mm256_maskz_loadu_epi64(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
160#else
161 switch (n) { // clang-format off
162 case 1: return Load<1>(ptr);
163 case 2: return Load<2>(ptr);
164 case 3: return Load<3>(ptr);
165 case 4: return Load<4>(ptr);
166 MOCHI_UNLIKELY default: return Zero();
167 } // clang-format on
168#endif
169 }
170
171 template <int kTupleCount = kSize>
172 MOCHI_FORCE_INLINE static void
173 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
174 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
175 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
176 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
177 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
178 auto a = _mm256_castsi256_pd(Simd::Load<kCount0>(ptr).raw); // [0,1,2,3]
179 auto b =
180 _mm256_castsi256_pd(Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 4).raw); // [4,5,6,7]
181 auto c =
182 _mm256_castsi256_pd(Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 8).raw); // [8,9,10,11]
183 Deinterleave3(a, b, c, out0, out1, out2);
184 }
185
186 MOCHI_FORCE_INLINE static void
187 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
188 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
189 int const n = count * 3;
190 auto a = _mm256_castsi256_pd(Simd::Load(ptr, superdex::Min(n, 4)).raw); // [0,1,2,3]
191 auto b =
192 _mm256_castsi256_pd(Simd::Load(n > 4 ? ptr + 4 : ptr, Clamp(n - 4, 0, 4)).raw); // [4,5,6,7]
193 auto c = _mm256_castsi256_pd(
194 Simd::Load(n > 8 ? ptr + 8 : ptr, Clamp(n - 8, 0, 4)).raw); // [8,9,10,11]
195 Deinterleave3(a, b, c, out0, out1, out2);
196 }
197
198 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
199#if MOCHI_ARCH_X64_AVX512
200 return _mm256_min_epi64(a.raw, b.raw); // AVX512VL
201#else
202 return Simd{
206 superdex::Min(Get<3>(a), Get<3>(b))};
207#endif
208 }
209
210 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
211#if MOCHI_ARCH_X64_AVX512
212 return _mm256_max_epi64(a.raw, b.raw); // AVX512VL
213#else
214 return Simd{
218 superdex::Max(Get<3>(a), Get<3>(b))};
219#endif
220 }
221
222 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
223 return _mm256_blendv_epi8(b.raw, a.raw, mask.raw); // AVX2
224 }
225
226 template <int x, int y, int z, int w>
227 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd a) {
228 if constexpr (x == 0 && y == 1 && z == 2 && w == 3) {
229 return a;
230 } else {
231 return _mm256_permute4x64_epi64(a.raw, _MM_SHUFFLE(w, z, y, x)); // AVX2
232 }
233 }
234
235 template <int x, int y, int z, int w>
236 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd a, Simd b) {
237 return Simd{Get<x>(a), Get<y>(a), Get<z>(b), Get<w>(b)};
238 }
239
240 template <int N = kSize>
241 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
242 static_assert(N >= 0 && N <= kSize);
243 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
244 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
245 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
246 // go through the stack.
247 using HalfT = Simd<Scalar, 2>;
248 if constexpr (N == 0) {
249 } else if constexpr (N <= 2) {
250 HalfT::Store<N>(ptr, HalfT(_mm256_castsi256_si128(v.raw))); // AVX
251 } else if constexpr (N < kSize) {
252 HalfT::Store<N - 2>(ptr + 2, HalfT(_mm256_extracti128_si256(v.raw, 1))); // AVX2
253 HalfT::Store<2>(ptr, HalfT(_mm256_castsi256_si128(v.raw))); // AVX
254 } else {
255 _mm256_storeu_si256(reinterpret_cast<__m256i*>(ptr), v.raw); // AVX2
256 }
257 }
258
259 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
260 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
261#if MOCHI_ARCH_X64_AVX512
262 _mm256_mask_storeu_epi64(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
263#else
264 // With AVX2, this is faster than masked store for a predictable value of n.
265 // It is much slower for a random value of n.
266 switch (n) { // clang-format off
267 case 1: Store<1>(ptr, v); break;
268 case 2: Store<2>(ptr, v); break;
269 case 3: Store<3>(ptr, v); break;
270 case 4: Store<4>(ptr, v); break;
271 MOCHI_UNLIKELY default: break;
272 } // clang-format on
273#endif
274 }
275
276 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
277#if MOCHI_ARCH_X64_AVX512
278 auto const mask = static_cast<__mmask8>(ToMask(condition));
279 _mm256_mask_compressstoreu_epi64(ptr, mask, values.raw); // AVX512VL
280 return _mm_popcnt_u32(mask);
281#else
282 auto mask = ToMask(condition);
283 // Load 8 bytes from the table, then zero-exend to get the shuffle pattern.
284 auto const* tableRow =
285 reinterpret_cast<__m128i const*>(x64_simd::kStoreSelectedShuffleTableD4[mask]);
286 auto pattern = _mm256_cvtepu8_epi32(_mm_loadl_epi64(tableRow));
287 auto packed = _mm256_permutevar8x32_epi32(values.raw, pattern);
288 _mm256_storeu_si256(reinterpret_cast<__m256i*>(ptr), packed);
289 return _mm_popcnt_u32(mask);
290#endif
291 }
292
293 template <int kTupleCount = kSize>
294 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
295 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
296 Simd x0, x1, x2;
297 Interleave3(a, b, c, x0, x1, x2);
298 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
299 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
300 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
301 Simd::Store<kCount0>(ptr, x0);
302 if constexpr (kCount1 > 0) {
303 Simd::Store<kCount1>(ptr + 4, x1);
304 }
305 if constexpr (kCount2 > 0) {
306 Simd::Store<kCount2>(ptr + 8, x2);
307 }
308 }
309
310 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
311 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
312 Simd x0, x1, x2;
313 Interleave3(a, b, c, x0, x1, x2);
314#if MOCHI_ARCH_X64_AVX512
315 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
316 // back into branches. A zero mask stores nothing.
317 uint32_t const bits = (1u << (count * 3)) - 1;
318 _mm256_mask_storeu_epi64(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
319 _mm256_mask_storeu_epi64(ptr + (count > 1 ? 4 : 0), static_cast<__mmask8>(bits >> 4), x1.raw);
320 _mm256_mask_storeu_epi64(ptr + (count > 2 ? 8 : 0), static_cast<__mmask8>(bits >> 8), x2.raw);
321#else
322 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
323 // counts, which clang may turn back into branches. A zero mask stores nothing.
324 auto const n = _mm256_set1_epi64x(count * 3);
325 auto const mask0 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(0, 1, 2, 3)); // AVX2
326 auto const mask1 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(4, 5, 6, 7));
327 auto const mask2 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(8, 9, 10, 11));
328 _mm256_maskstore_epi64(reinterpret_cast<long long*>(ptr), mask0, x0.raw); // AVX2
329 _mm256_maskstore_epi64(reinterpret_cast<long long*>(ptr + (count > 1 ? 4 : 0)), mask1, x1.raw);
330 _mm256_maskstore_epi64(reinterpret_cast<long long*>(ptr + (count > 2 ? 8 : 0)), mask2, x2.raw);
331#endif
332 }
333
334 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
335 return _mm256_setzero_si256(); // AVX
336 }
337
338 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
339 return _mm256_cmpgt_epi64(rhs.raw, this->raw); // AVX2
340 }
341
342 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
343 return _mm256_cmpgt_epi64(this->raw, rhs.raw); // AVX2
344 }
345
346 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
347 return ~(*this > rhs); // No native support
348 }
349
350 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
351 return ~(*this < rhs); // No native support
352 }
353
354 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
355 return _mm256_cmpeq_epi64(a.raw, b.raw); // AVX2
356 }
357
358 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
359 return ~Equal(a, b); // // No native support
360 }
361
362 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
363 return ToMask(Equal(*this, rhs)) == 0xF; // All values equal
364 }
365
366 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
367 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
368 }
369
370 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
371 auto ones = _mm256_cmpeq_epi64(raw, raw); // AVX2
372 return _mm256_xor_si256(raw, ones); // AVX2
373 }
374
375 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
376 return _mm256_sub_epi64(_mm256_setzero_si256(), raw); // AVX2
377 }
378
379 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
380 return _mm256_add_epi64(raw, rhs.raw); // SSE
381 }
382
383 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
384 return _mm256_sub_epi64(raw, rhs.raw); // SSE
385 }
386
387 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
388#if MOCHI_ARCH_X64_AVX512
389 return _mm256_mullo_epi64(raw, rhs.raw); // AVX512VL
390#else
391 return Simd{
392 Get<0>(*this) * Get<0>(rhs),
393 Get<1>(*this) * Get<1>(rhs),
394 Get<2>(*this) * Get<2>(rhs),
395 Get<3>(*this) * Get<3>(rhs)};
396#endif
397 }
398
399 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
400#if MOCHI_ARCH_X64_SVML
401 return _mm256_div_epi64(raw, rhs.raw); // SSE
402#else
403 // Fallback
404 return Simd{
405 Get<0>(*this) / Get<0>(rhs),
406 Get<1>(*this) / Get<1>(rhs),
407 Get<2>(*this) / Get<2>(rhs),
408 Get<3>(*this) / Get<3>(rhs)};
409#endif
410 }
411
412 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
413 return _mm256_and_si256(raw, rhs.raw); // AVX2
414 }
415
416 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
417 return _mm256_or_si256(raw, rhs.raw); // AVX2
418 }
419
420 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
421 return _mm256_xor_si256(raw, rhs.raw); // AVX2
422 }
423
424 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<<(int rhs) const {
425 return _mm256_slli_epi64(raw, rhs); // AVX2
426 }
427
428 template <int kShift>
429 [[nodiscard]] MOCHI_FORCE_INLINE static Simd ShiftRight(Simd a) {
430 static_assert(kShift >= 0 && kShift < 64, "Shift amount out-of-range");
431 if constexpr (kShift == 0) {
432 return a;
433 } else {
434#if MOCHI_ARCH_X64_AVX512
435 return _mm256_srai_epi64(a.raw, kShift); // AVX512VL
436#else
437 auto shifted = _mm256_srli_epi64(a.raw, kShift); // AVX2
438 auto signMask = _mm256_cmpgt_epi64(_mm256_setzero_si256(), a.raw); // AVX2
439 auto signFill = _mm256_slli_epi64(signMask, 64 - kShift); // AVX2
440 return _mm256_or_si256(shifted, signFill); // AVX2
441#endif
442 }
443 }
444
445 private:
446 // One bit per lane, from the lane's sign bit.
447 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
448#if MOCHI_ARCH_X64_AVX512
449 return _mm256_movepi64_mask(a.raw); // AVX512DQ, AVX512VL
450#else
451 return _mm256_movemask_pd(_mm256_castsi256_pd(a.raw)); // AVX, AVX
452#endif
453 }
454
455 // Splits 12 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
456 MOCHI_FORCE_INLINE static void
457 Deinterleave3(__m256d a, __m256d b, __m256d c, Simd& out0, Simd& out1, Simd& out2) {
458 auto d = _mm256_blend_pd(a, b, 0b0100); // [0,_,6,3]
459 d = _mm256_blend_pd(d, c, 0b0010); // [0,9,6,3]
460 auto e = _mm256_permute2f128_pd(d, d, 0x01); // [6,3,0,9]
461 out0.raw = _mm256_castpd_si256(_mm256_blend_pd(d, e, 0b1010)); // [0,3,6,9]
462
463 d = _mm256_blend_pd(a, b, 0b1001); // [4,1,_,7]
464 d = _mm256_blend_pd(d, c, 0b0100); // [4,1,10,7]
465 out1.raw = _mm256_castpd_si256(_mm256_shuffle_pd(d, d, 0b0101)); // [1,4,7,10]
466
467 d = _mm256_blend_pd(a, b, 0b0010); // [_,5,2,_]
468 d = _mm256_blend_pd(d, c, 0b1001); // [8,5,2,11]
469 e = _mm256_permute2f128_pd(d, d, 0x01); // [2,11,8,5]
470 out2.raw = _mm256_castpd_si256(_mm256_blend_pd(d, e, 0b0101)); // [2,5,8,11]
471 }
472
473 // Inverse of Deinterleave3: out0, out1, out2 are the 12 interleaved scalars in memory order.
474 MOCHI_FORCE_INLINE static void
475 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
476 // a = [0,3,6,9], b = [1,4,7,10], c = [2,5,8,11]
477 auto a_ = _mm256_castsi256_pd(a.raw);
478 auto b_ = _mm256_castsi256_pd(b.raw);
479 auto c_ = _mm256_castsi256_pd(c.raw);
480 auto d = _mm256_shuffle_pd(b_, b_, 0b0101); // [4,1,10,7]
481 auto e = _mm256_blend_pd(a_, c_, 0b0101); // [2,3,8,9]
482 e = _mm256_permute2f128_pd(e, e, 0x01); // [8,9,2,3]
483 auto f = _mm256_blend_pd(a_, d, 0b1010); // [0,1,6,7]
484 auto g = _mm256_blend_pd(d, c_, 0b1010); // [4,5,10,11]
485 out0.raw = _mm256_castpd_si256(_mm256_blend_pd(f, e, 0b1100)); // [0,1,2,3]
486 out1.raw = _mm256_castpd_si256(_mm256_blend_pd(g, f, 0b1100)); // [4,5,6,7]
487 out2.raw = _mm256_castpd_si256(_mm256_blend_pd(e, g, 0b1100)); // [8,9,10,11]
488 }
489};
490
491} // namespace superdex
492
493#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
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)
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Definition simd_inl.h:322
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)