SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_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 "x64_simd_inl.h" // for IntelliSense
20
21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
22
23namespace superdex {
24
25/***********************************************************************************************
26 Simd<double, 2>
27*/
28template <>
29class Simd<double, 2> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(double, 2, __m128d);
32 Simd(double a, double b) : raw(_mm_set_pd(b, a)) {} // SSE2
33
34 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
35 Simd(U a) : raw(_mm_set_pd1(a)) {} // SSE2
36
37 template <int i>
38 [[nodiscard]] static MOCHI_FORCE_INLINE double Get(Simd v) {
39 static_assert(i >= 0 && i < 2, "Index out of range");
40 return v[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#if MOCHI_COMPILER_MSVC
46 return raw.m128d_f64[i];
47#else
48 return raw[i];
49#endif
50 }
51
52 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
53 MOCHI_ASSERT_VERBOSE(i >= 0 && i < 2, "Index out of range");
54#if MOCHI_COMPILER_MSVC
55 auto result = v;
56 result.raw.m128d_f64[i] = value;
57 return result;
58#else
59 static constexpr __m128i kMasks[] = {{-1LL, 0LL}, {0LL, -1LL}};
60 return _mm_blendv_pd(v.raw, _mm_set_pd1(value), _mm_castsi128_pd(kMasks[i])); // SSE4.1
61#endif
62 }
63
64 template <int i>
65 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
66 static_assert(i >= 0 && i < 2, "Index out of range");
67 if constexpr (i == 0) {
68 return _mm_shuffle_pd(_mm_set1_pd(value), v.raw, 0x02); // SSE2
69 } else {
70 return _mm_shuffle_pd(v.raw, _mm_set1_pd(value), 0x02); // SSE2
71 }
72 }
73
74 template <int N>
75 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
76 static_assert(N >= 1 && N <= kSize, "Unsupported N");
77 int constexpr kLanes = (1 << N) - 1;
78 return (ToMask(v) & kLanes) == kLanes;
79 }
80
81 template <int N>
82 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
83 static_assert(N >= 1 && N <= kSize, "Unsupported N");
84 int constexpr kLanes = (1 << N) - 1;
85 return (ToMask(v) & kLanes) != 0;
86 }
87
88 template <int x, int y>
89 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Blend(Simd a, Simd b) {
90 static_assert(x >= 0 && x < 2 && y >= 0 && y < 2, "invalid blend index");
91 if constexpr (x == 0 && y == 0) {
92 return a;
93 } else if constexpr (x == 1 && y == 1) {
94 return b;
95 } else {
96 return _mm_blend_pd(a.raw, b.raw, x | (y << 1)); // SSE4.1
97 }
98 }
99
100 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
101 return Simd{*p};
102 }
103
104 template <int i>
105 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
106 return Shuffle<i, i>(v);
107 }
108
109 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int64_t, 2> const& indices) {
110 return _mm_i64gather_pd(ptr, indices.raw, sizeof(double)); // AVX2
111 }
112
113 template <int N = kSize>
114 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
115 static_assert(N >= 0 && N <= kSize);
116 if constexpr (N == 0) {
117 return Simd::Zero();
118 } else if constexpr (N == 1) {
119 return Simd{*ptr, 0.0};
120 } else {
121 return _mm_loadu_pd(ptr); // SSE2
122 }
123 }
124
125 static_assert(sizeof(long long) == 8);
126
127#if !MOCHI_ARCH_X64_AVX512
128#if MOCHI_COMPILER_MSVC
129 // MSVC defines __m128i as a union. Byte arrays are required to initialize it this way.
130 // clang-format off
131 static constexpr __m128i kLoadMasks[] = {
132 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
133 {-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0},
134 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};
135 // clang-format on
136#else
137 // GCC and Clang define __m128i as 'long long' with special attributes.
138 static constexpr __m128i kLoadMasks[] = {{0LL, 0LL}, {-1LL, 0LL}, {-1LL, -1LL}};
139#endif
140#endif
141
142 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
143 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
144#if MOCHI_ARCH_X64_AVX512
145 return _mm_maskz_loadu_pd(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
146#else
147 return _mm_maskload_pd(ptr, kLoadMasks[n]); // AVX
148#endif
149 }
150
151 template <int kTupleCount = kSize>
152 MOCHI_FORCE_INLINE static void
153 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
154 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
155 constexpr int kCount1 = Clamp(kTupleCount * 3 - 2, 0, 2);
156 constexpr int kCount2 = Clamp(kTupleCount * 3 - 4, 0, 2);
157 auto a = Simd::Load<2>(ptr).raw; // [0,1]
158 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 2).raw; // [2,3]
159 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 4).raw; // [4,5]
160 Deinterleave3(a, b, c, out0, out1, out2);
161 }
162
163 MOCHI_FORCE_INLINE static void
164 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
165 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
166 int const n = count * 3;
167 auto a = Simd::Load(ptr, superdex::Min(n, 2)).raw; // [0,1]
168 auto b = Simd::Load(n > 2 ? ptr + 2 : ptr, Clamp(n - 2, 0, 2)).raw; // [2,3]
169 auto c = Simd::Load(n > 4 ? ptr + 4 : ptr, Clamp(n - 4, 0, 2)).raw; // [4,5]
170 Deinterleave3(a, b, c, out0, out1, out2);
171 }
172
173 template <int N = kSize>
174 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
175 static_assert(N >= 0 && N <= kSize);
176 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
177 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
178 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
179 // go through the stack.
180 if constexpr (N == 0) {
181 } else if constexpr (N == 1) {
182 _mm_store_sd(ptr, v.raw); // SSE2
183 } else {
184 _mm_storeu_pd(ptr, v.raw); // SSE2
185 }
186 }
187
188 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
189 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
190#if MOCHI_ARCH_X64_AVX512
191 _mm_mask_storeu_pd(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
192#else
193 switch (n) { // clang-format off
194 case 1: Store<1>(ptr, v); break;
195 case 2: Store<2>(ptr, v); break;
196 MOCHI_UNLIKELY default: break;
197 } // clang-format on
198#endif
199 }
200
201 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
202 auto mask = ToMask(condition);
203 auto swapped = _mm_shuffle_pd(values.raw, values.raw, 1); // swap halves
204 auto blendMask =
205 _mm_castsi128_pd(_mm_set1_epi32((mask & 1) - 1)); // swap first bit of mask is zero
206 _mm_storeu_pd(ptr, _mm_blendv_pd(values.raw, swapped, blendMask));
207 return _mm_popcnt_u32(mask);
208 }
209
210 template <int kTupleCount = kSize>
211 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
212 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
213 Simd x0, x1, x2;
214 Interleave3(a, b, c, x0, x1, x2);
215 Simd::Store<2>(ptr, x0);
216 constexpr int kCount1 = Clamp(kTupleCount * 3 - 2, 0, 2);
217 constexpr int kCount2 = Clamp(kTupleCount * 3 - 4, 0, 2);
218 if constexpr (kCount1 > 0) {
219 Simd::Store<kCount1>(ptr + 2, x1);
220 }
221 if constexpr (kCount2 > 0) {
222 Simd::Store<kCount2>(ptr + 4, x2);
223 }
224 }
225
226 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
227 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
228 Simd x0, x1, x2;
229 Interleave3(a, b, c, x0, x1, x2);
230#if MOCHI_ARCH_X64_AVX512
231 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
232 // back into branches. A zero mask stores nothing.
233 uint32_t const bits = (1u << (count * 3)) - 1;
234 _mm_mask_storeu_pd(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
235 _mm_mask_storeu_pd(ptr + (count > 0 ? 2 : 0), static_cast<__mmask8>(bits >> 2), x1.raw);
236 _mm_mask_storeu_pd(ptr + (count > 1 ? 4 : 0), static_cast<__mmask8>(bits >> 4), x2.raw);
237#else
238 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
239 // counts, which clang may turn back into branches. A zero mask stores nothing.
240 auto const n = _mm_set1_epi64x(count * 3);
241 auto const mask0 = _mm_cmpgt_epi64(n, _mm_set_epi64x(1, 0)); // SSE4.2
242 auto const mask1 = _mm_cmpgt_epi64(n, _mm_set_epi64x(3, 2));
243 auto const mask2 = _mm_cmpgt_epi64(n, _mm_set_epi64x(5, 4));
244 _mm_maskstore_pd(ptr, mask0, x0.raw); // AVX
245 _mm_maskstore_pd(ptr + (count > 0 ? 2 : 0), mask1, x1.raw);
246 _mm_maskstore_pd(ptr + (count > 1 ? 4 : 0), mask2, x2.raw);
247#endif
248 }
249
250 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
251 return _mm_blendv_pd(b.raw, a.raw, mask.raw); // SSE4.1
252 }
253
254 // return Simd{v[x], v[y]}
255 template <int x = 0, int y = 1>
256 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd v) {
257 static_assert(x >= 0 && x < 2, "Invalid index");
258 static_assert(y >= 0 && y < 2, "Invalid index");
259 if constexpr (x == 0 && y == 1) {
260 return v;
261 } else {
262 return _mm_shuffle_pd(v.raw, v.raw, x | (y << 1)); // SSE2
263 }
264 }
265
266 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
267 return _mm_sqrt_pd(v.raw); // SSE2
268 }
269
270 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
271 return Simd{1.0} / v;
272 }
273
274 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
275 return Simd{1.0} / Sqrt(v);
276 }
277
278 // Broadcast the value -0.0. Use this in bitwise operations to affect just the sign bit.
279 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
280 return _mm_castsi128_pd(_mm_set1_epi64x(0x8000000000000000LL)); // SSE2, SSE2
281 }
282
283 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
284 return _mm_andnot_pd(SignBitMask().raw, v.raw); // SSE2
285 }
286
287 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
288 return _mm_min_pd(a.raw, b.raw); // SSE2
289 }
290
291 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
292 return _mm_max_pd(a.raw, b.raw); // SSE2
293 }
294
295 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
296 return _mm_floor_pd(a.raw); // SSE4.1
297 }
298
299 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
300 return _mm_round_pd(v.raw, _MM_FROUND_TO_NEAREST_INT); // SSE4.1
301 }
302
303#if MOCHI_ARCH_X64_SVML
304 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
305 return _mm_cos_pd(a.raw); // SSE
306 }
307
308 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
309 return _mm_sin_pd(a.raw); // SSE
310 }
311
312 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
313 return _mm_tan_pd(a.raw); // SSE
314 }
315
316 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
317 return _mm_acos_pd(a.raw); // SSE
318 }
319
320 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
321 return _mm_asin_pd(a.raw); // SSE
322 }
323
324 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
325 return _mm_atan_pd(a.raw); // SSE
326 }
327
328 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
329 return _mm_exp_pd(a.raw); // SSE
330 }
331
332 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
333 return _mm_log_pd(a.raw); // SSE
334 }
335
336 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
337 return _mm_tanh_pd(a.raw); // SSE
338 }
339#endif // MOCHI_ARCH_X64_SVML
340
341 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
342#if MOCHI_ARCH_X64_FMA
343 return {_mm_fmadd_pd(a.raw, b.raw, c.raw)}; // FMA
344#else
345 return (a * b) + c;
346#endif
347 }
348
349 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
350#if MOCHI_ARCH_X64_FMA
351 return _mm_fmsub_pd(a.raw, b.raw, c.raw); // FMA
352#else
353 return (a * b) - c;
354#endif
355 }
356
357 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
358#if MOCHI_ARCH_X64_FMA
359 return _mm_fnmadd_pd(a.raw, b.raw, c.raw); // FMA
360#else
361 return -(a * b) + c;
362#endif
363 }
364
365 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
366#if MOCHI_ARCH_X64_FMA
367 return _mm_fnmsub_pd(a.raw, b.raw, c.raw); // FMA
368#else
369 return -(a * b) - c;
370#endif
371 }
372
373 template <int N = 2>
374 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
375 static_assert(N == 2, "Unsupported N");
376 return Get<0>(Min(a, Broadcast<1>(a)));
377 }
378
379 template <int N = 2>
380 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
381 static_assert(N == 2, "Unsupported N");
382 return Get<0>(Max(a, Broadcast<1>(a)));
383 }
384
385 template <int N>
386 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
387 static_assert(N == 2, "Unsupported N");
388 return Get<0>(a) + Get<1>(a);
389 }
390
391 [[nodiscard]] static MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b) {
392 return _mm_add_pd(_mm_unpacklo_pd(a.raw, b.raw), _mm_unpackhi_pd(a.raw, b.raw)); // SSE2
393 }
394
395 template <int N>
396 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
397 static_assert(N == 2, "Unsupported N");
398 return Get<0>(Broadcast<0>(a) * Broadcast<1>(a));
399 }
400
401 template <int N>
402 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
403 static_assert(N == 2, "Unsupported N");
404#if MOCHI_COMPILER_CLANG
405 return _mm_dp_pd(a.raw, b.raw, -1); // SSE4.1
406#else
407 return _mm_dp_pd(a.raw, b.raw, 0xFF); // SSE4.1
408#endif
409 }
410
411 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
412 return _mm_cmplt_pd(this->raw, rhs.raw); // SSE
413 }
414
415 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
416 return _mm_cmpgt_pd(this->raw, rhs.raw); // SSE2
417 }
418
419 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
420 return _mm_cmple_pd(this->raw, rhs.raw); // SSE2
421 }
422
423 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
424 return _mm_cmpge_pd(this->raw, rhs.raw); // SSE2
425 }
426
427 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
428 return _mm_cmpeq_pd(a.raw, b.raw); // SSE2
429 }
430
431 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
432 return _mm_cmpneq_pd(a.raw, b.raw); // SSE2
433 }
434
435 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
436 return _mm_setzero_pd(); // SSE2
437 }
438
439 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
440 return ToMask(Equal(*this, rhs)) == 0x3; // All values equal
441 }
442
443 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
444 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
445 }
446
447 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
448 // _mm_cmpeq_epi32 appears to be the fastest way to fill an SSE register with ones.
449 __m128i dummy{};
450 __m128d ones = _mm_castsi128_pd(_mm_cmpeq_epi32(dummy, dummy)); // SSE2, SSE2
451 return _mm_xor_pd(raw, ones); // SSE2
452 }
453
454 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
455 return _mm_xor_pd(raw, SignBitMask().raw); // SSE2, SSE2
456 }
457
458 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
459 return _mm_add_pd(raw, rhs.raw); // SSE2
460 }
461
462 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
463 return _mm_sub_pd(raw, rhs.raw); // SSE2
464 }
465
466 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
467 return _mm_mul_pd(raw, rhs.raw); // SSE2
468 }
469
470 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
471 return _mm_div_pd(raw, rhs.raw); // SSE2
472 }
473
474 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
475 return _mm_and_pd(raw, rhs.raw); // SSE2
476 }
477
478 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
479 return _mm_or_pd(raw, rhs.raw); // SSE2
480 }
481
482 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
483 return _mm_xor_pd(raw, rhs.raw); // SSE2
484 }
485
486 private:
487 // One bit per lane, from the lane's sign bit.
488 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
489 return _mm_movemask_pd(a.raw); // SSE2
490 }
491
492 // Splits 6 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
493 MOCHI_FORCE_INLINE static void
494 Deinterleave3(__m128d a, __m128d b, __m128d c, Simd& out0, Simd& out1, Simd& out2) {
495 out0.raw = _mm_shuffle_pd(a, b, 0b0010); // [0,3]
496 out1.raw = _mm_shuffle_pd(a, c, 0b0001); // [1,4]
497 out2.raw = _mm_shuffle_pd(b, c, 0b0010); // [2,5]
498 }
499
500 // Inverse of Deinterleave3: out0, out1, out2 are the 6 interleaved scalars in memory order.
501 MOCHI_FORCE_INLINE static void
502 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
503 // a = [0,3], b = [1,4], c = [2,5]
504 out0 = _mm_shuffle_pd(a.raw, b.raw, 0b00); // [0,1]
505 out1 = _mm_shuffle_pd(c.raw, a.raw, 0b10); // [2,3]
506 out2 = _mm_shuffle_pd(b.raw, c.raw, 0b11); // [4,5]
507 }
508};
509
510} // namespace superdex
511
512#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*(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 ACos(T a)
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)
constexpr T Sin(T a)
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 > Tanh(Simd< T, N > a)
Definition simd_inl.h:783
Simd< T, N > Set(Simd< T, N > a, T value)
Definition simd_inl.h:327
constexpr auto NotEqual(T const &a, T const &b)
constexpr T Exp(T a)
constexpr T Cos(T a)
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
constexpr T Tan(T a)
bool AnyTrue(T const &a)
Definition basic_utils.h:66
constexpr T Select(bool condition, T a, T b)
constexpr T Sqrt(T a)
constexpr T ATan(T a)
Simd< T, N > Ln(Simd< T, N > a)
Definition simd_inl.h:705
T Get(Simd< T, N > v)
Definition simd_inl.h:317
constexpr T Floor(T a)
constexpr T ASin(T a)
constexpr ValT Clamp(ValT value, MinT min, MaxT max)
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)