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