SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_double_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<double, 4>
27*/
28template <>
29class Simd<double, 4> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(double, 4, __m256d);
32 Simd(double a, double b, double c = 0.0, double d = 0.0)
33 : raw(_mm256_set_pd(d, c, b, a)) {} // AVX
34 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
35 Simd(U a) : raw(_mm256_set1_pd(a)) {} // AVX
36
37 // Joint two Vec2d into a single Vec4d
38 Simd(Simd<double, 2> const& low, Simd<double, 2> const& high)
39 : raw(_mm256_set_m128d(high.raw, low.raw)) {} // AVX
40
41 template <int i>
42 [[nodiscard]] static MOCHI_FORCE_INLINE double Get(Simd v) {
43 static_assert(i >= 0 && i < 4, "Index out of range");
44 return v[i];
45 }
46
47 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
48 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
49#if MOCHI_COMPILER_MSVC
50 return raw.m256d_f64[i];
51#else
52 return raw[i];
53#endif
54 }
55
56 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
57 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
58#if MOCHI_COMPILER_MSVC
59 auto result = v;
60 result.raw.m256d_f64[i] = value;
61 return result;
62#else
63 static constexpr __m256i kMasks[] = {
64 {-1LL, 0LL, 0LL, 0LL}, {0LL, -1LL, 0LL, 0LL}, {0LL, 0LL, -1LL, 0LL}, {0LL, 0LL, 0LL, -1LL}};
65 return _mm256_blendv_pd(v.raw, _mm256_set1_pd(value), _mm256_castsi256_pd(kMasks[i])); // AVX
66#endif
67 }
68
69 template <int i>
70 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
71 static_assert(i >= 0 && i < kSize, "Index out of range");
72 return Set(v, i, value);
73 }
74
75 template <int iHalf>
76 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<double, 2> GetHalf(Simd a) {
77 static_assert(iHalf == 0 || iHalf == 1);
78 return _mm256_extractf128_pd(a.raw, iHalf); // AVX
79 }
80
81 [[nodiscard]] static MOCHI_FORCE_INLINE Simd AsPoint(Simd a) {
82 // Replace the 3rd component with an integer that has the same bits as 1.0.
83 auto araw = _mm256_castpd_si256(a.raw); // AVX
84 auto v = _mm256_insert_epi64(araw, 0x3FF0000000000000LL, 3); // AVX
85 return _mm256_castsi256_pd(v); // AVX
86 }
87
88 [[nodiscard]] static MOCHI_FORCE_INLINE Simd AsDirection(Simd a) {
89 // Replace the 3rd component with an integer that has the same bits as 0.0.
90 auto araw = _mm256_castpd_si256(a.raw); // AVX
91 auto v = _mm256_insert_epi64(araw, 0, 3); // AVX
92 return _mm256_castsi256_pd(v); // AVX
93 }
94
95 template <int x, int y, int z, int w>
96 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Blend(Simd a, Simd b) {
97 static_assert(
98 x >= 0 && x < 2 && y >= 0 && y < 2 && z >= 0 && z < 2 && w >= 0 && w < 2,
99 "invalid blend index");
100 if constexpr (x == 0 && y == 0 && z == 0 && w == 0) {
101 return a;
102 } else if constexpr (x == 1 && y == 1 && z == 1 && w == 1) {
103 return b;
104 } else {
105 return _mm256_blend_pd(a.raw, b.raw, x | (y << 1) | (z << 2) | (w << 3)); // SSE4.1
106 }
107 }
108
109 template <int N>
110 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
111 static_assert(N >= 1 && N <= kSize, "Unsupported N");
112 int constexpr kLanes = (1 << N) - 1;
113 return (ToMask(v) & kLanes) == kLanes;
114 }
115
116 template <int N>
117 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
118 static_assert(N >= 1 && N <= kSize, "Unsupported N");
119 int constexpr kLanes = (1 << N) - 1;
120 return (ToMask(v) & kLanes) != 0;
121 }
122
123 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
124 return _mm256_broadcast_sd(p); // AVX
125 }
126
127 template <int i>
128 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
129 return Shuffle<i, i, i, i>(v);
130 }
131
132 template <int N = kSize>
133 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
134 static_assert(N >= 0 && N <= 4);
135 if constexpr (N == 0) {
136 return Simd::Zero();
137 } else if constexpr (N == 1) {
138 return Simd{*ptr, 0.0};
139 } else if constexpr (N == 2) {
140 return _mm256_zextpd128_pd256(_mm_loadu_pd(ptr)); // AVX, SSE2
141 } else if constexpr (N == 3) {
142 __m256i mask = _mm256_set_epi64x(0, -1, -1, -1); // AVX
143 return _mm256_maskload_pd(ptr, mask); // AVX
144 } else {
145 return _mm256_loadu_pd(ptr); // AVX
146 }
147 }
148
149#if !MOCHI_ARCH_X64_AVX512
150#if MOCHI_COMPILER_MSVC
151 // MSVC defines __m128i as a union. Byte arrays are required to initialize it this way.
152 // clang-format off
153 static constexpr __m256i kLoadMasks[] = {
154 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
155 {-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
156 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
157 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0},
158 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};
159 // clang-format on
160#else
161 // GCC and Clang define __m256i as 'long long' with special attributes.
162 static constexpr __m256i kLoadMasks[] = {
163 {0LL, 0LL, 0LL, 0LL},
164 {-1LL, 0LL, 0LL, 0LL},
165 {-1LL, -1LL, 0LL, 0LL},
166 {-1LL, -1LL, -1LL, 0LL},
167 {-1LL, -1LL, -1LL, -1LL}};
168#endif
169#endif
170
171 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
172 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
173#if MOCHI_ARCH_X64_AVX512
174 return _mm256_maskz_loadu_pd(x64_simd::kLaneMasksS8[n], ptr); // AVX512VL
175#else
176 return _mm256_maskload_pd(ptr, kLoadMasks[n]); // AVX
177#endif
178 }
179
180 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int, 4> const& indices) {
181 return _mm256_i32gather_pd(ptr, indices.raw, sizeof(double)); // AVX2
182 }
183
184 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int64_t, 4> const& indices) {
185 return _mm256_i64gather_pd(ptr, indices.raw, sizeof(double)); // AVX2
186 }
187
188 template <int kTupleCount = kSize>
189 MOCHI_FORCE_INLINE static void
190 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
191 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
192 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
193 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
194 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
195 auto a = Simd::Load<kCount0>(ptr).raw; // [0,1,2,3]
196 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 4).raw; // [4,5,6,7]
197 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 8).raw; // [8,9,10,11]
198 Deinterleave3(a, b, c, out0, out1, out2);
199 }
200
201 MOCHI_FORCE_INLINE static void
202 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
203 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
204 int const n = count * 3;
205 auto a = Simd::Load(ptr, superdex::Min(n, 4)).raw; // [0,1,2,3]
206 auto b = Simd::Load(n > 4 ? ptr + 4 : ptr, Clamp(n - 4, 0, 4)).raw; // [4,5,6,7]
207 auto c = Simd::Load(n > 8 ? ptr + 8 : ptr, Clamp(n - 8, 0, 4)).raw; // [8,9,10,11]
208 Deinterleave3(a, b, c, out0, out1, out2);
209 }
210
211 template <int i>
212 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SetBasisVector() {
213 static_assert(i >= 0 && i <= 3, "Invalid component index");
214 auto zero = _mm256_setzero_si256(); // AVX
215 auto v = _mm256_insert_epi64(zero, 0x3FF0000000000000LL, i); // AVX
216 return _mm256_castsi256_pd(v); // AVX
217 }
218
219 template <int N = kSize>
220 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
221 static_assert(N >= 0 && N <= kSize);
222 // Partial stores write the lanes straight from the register. With AVX2, masked stores are
223 // several times slower on AMD and block store-to-load forwarding on Intel. With AVX-512, they
224 // block forwarding on both. Measured on Zen 4 and Sapphire Rapids. A memcpy of the vector can
225 // go through the stack.
226 using HalfT = Simd<Scalar, 2>;
227 if constexpr (N == 0) {
228 } else if constexpr (N <= 2) {
229 HalfT::Store<N>(ptr, HalfT(_mm256_castpd256_pd128(v.raw))); // AVX
230 } else if constexpr (N < kSize) {
231 HalfT::Store<N - 2>(ptr + 2, HalfT(_mm256_extractf128_pd(v.raw, 1))); // AVX
232 HalfT::Store<2>(ptr, HalfT(_mm256_castpd256_pd128(v.raw))); // AVX
233 } else {
234 _mm256_storeu_pd(ptr, v.raw); // AVX
235 }
236 }
237
238 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
239 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
240#if MOCHI_ARCH_X64_AVX512
241 _mm256_mask_storeu_pd(ptr, x64_simd::kLaneMasksS8[n], v.raw); // AVX512VL
242#else
243 switch (n) { // clang-format off
244 case 1: Store<1>(ptr, v); break;
245 case 2: Store<2>(ptr, v); break;
246 case 3: Store<3>(ptr, v); break;
247 case 4: Store<4>(ptr, v); break;
248 MOCHI_UNLIKELY default: break;
249 } // clang-format on
250#endif
251 }
252
253 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
254 auto mask = ToMask(condition);
255 // Load 8 bytes from the table, then zero-exend to get the shuffle pattern.
256 auto const* tableRow =
257 reinterpret_cast<__m128i const*>(x64_simd::kStoreSelectedShuffleTableD4[mask]);
258 __m256i pattern = _mm256_cvtepu8_epi32(_mm_loadl_epi64(tableRow));
259 __m256i packed = _mm256_permutevar8x32_epi32(_mm256_castpd_si256(values.raw), pattern);
260 _mm256_storeu_pd(ptr, _mm256_castsi256_pd(packed));
261 return _mm_popcnt_u32(mask);
262 }
263
264 template <int kTupleCount = kSize>
265 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
266 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
267 Simd x0, x1, x2;
268 Interleave3(a, b, c, x0, x1, x2);
269 constexpr int kCount0 = Clamp(kTupleCount * 3 - 0, 0, 4);
270 constexpr int kCount1 = Clamp(kTupleCount * 3 - 4, 0, 4);
271 constexpr int kCount2 = Clamp(kTupleCount * 3 - 8, 0, 4);
272 Simd::Store<kCount0>(ptr, x0);
273 if constexpr (kCount1 > 0) {
274 Simd::Store<kCount1>(ptr + 4, x1);
275 }
276 if constexpr (kCount2 > 0) {
277 Simd::Store<kCount2>(ptr + 8, x2);
278 }
279 }
280
281 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
282 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
283 Simd x0, x1, x2;
284 Interleave3(a, b, c, x0, x1, x2);
285#if MOCHI_ARCH_X64_AVX512
286 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
287 // back into branches. A zero mask stores nothing.
288 uint32_t const bits = (1u << (count * 3)) - 1;
289 _mm256_mask_storeu_pd(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512VL
290 _mm256_mask_storeu_pd(ptr + (count > 1 ? 4 : 0), static_cast<__mmask8>(bits >> 4), x1.raw);
291 _mm256_mask_storeu_pd(ptr + (count > 2 ? 8 : 0), static_cast<__mmask8>(bits >> 8), x2.raw);
292#else
293 // Scalar i is stored if i < count * 3. Comparing against scalar indices avoids clamped
294 // counts, which clang may turn back into branches. A zero mask stores nothing.
295 auto const n = _mm256_set1_epi64x(count * 3);
296 auto const mask0 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(0, 1, 2, 3)); // AVX2
297 auto const mask1 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(4, 5, 6, 7));
298 auto const mask2 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(8, 9, 10, 11));
299 _mm256_maskstore_pd(ptr, mask0, x0.raw); // AVX
300 _mm256_maskstore_pd(ptr + (count > 1 ? 4 : 0), mask1, x1.raw);
301 _mm256_maskstore_pd(ptr + (count > 2 ? 8 : 0), mask2, x2.raw);
302#endif
303 }
304
305 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
306 return _mm256_blendv_pd(b.raw, a.raw, mask.raw); // AVX
307 }
308
309 // return Simd{v[x], v[y], v[z], v[w]}
310 template <int x = 0, int y = 1, int z = 2, int w = 3>
311 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd v) {
312 static_assert(x >= 0 && x < 4, "Invalid index");
313 static_assert(y >= 0 && y < 4, "Invalid index");
314 static_assert(z >= 0 && z < 4, "Invalid index");
315 static_assert(w >= 0 && w < 4, "Invalid index");
316 if constexpr (x == 0 && y == 1 && z == 2 && w == 3) {
317 return v;
318 } else {
319 return _mm256_permute4x64_pd(v.raw, x | (y << 2) | (z << 4) | (w << 6)); // AVX2
320 }
321 }
322
323 // return Simd{a[x], a[y], b[z], b[w]}
324 template <int x = 0, int y = 1, int z = 2, int w = 3>
325 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Shuffle(Simd a, Simd b) {
326 static_assert(x >= 0 && x < 4, "Invalid index");
327 static_assert(y >= 0 && y < 4, "Invalid index");
328 static_assert(z >= 0 && z < 4, "Invalid index");
329 static_assert(w >= 0 && w < 4, "Invalid index");
330 return Simd{Get<x>(a), Get<y>(a), Get<z>(b), Get<w>(b)};
331 }
332
333 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
334 return _mm256_sqrt_pd(v.raw); // AVX
335 }
336
337 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
338 return Simd{1.0} / v;
339 }
340
341 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
342 return Simd{1.0} / Sqrt(v);
343 }
344
345 // Broadcast the value -0.0. Use this in bitwise operations to affect just the sign bit.
346 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
347 return _mm256_castsi256_pd(_mm256_set1_epi64x(0x8000000000000000LL)); // AVX, AVX
348 }
349
350 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
351 return _mm256_andnot_pd(SignBitMask().raw, v.raw); // AVX
352 }
353
354 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
355 return _mm256_min_pd(a.raw, b.raw); // AVX
356 }
357
358 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
359 return _mm256_max_pd(a.raw, b.raw); // AVX
360 }
361
362 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
363 return _mm256_floor_pd(a.raw); // AVX
364 }
365
366 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
367 return _mm256_round_pd(v.raw, _MM_FROUND_TO_NEAREST_INT); // AVX
368 }
369
370#if MOCHI_ARCH_X64_SVML
371 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
372 return _mm256_cos_pd(a.raw); // AVX
373 }
374
375 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
376 return _mm256_sin_pd(a.raw); // AVX
377 }
378
379 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
380 return _mm256_tan_pd(a.raw); // AVX
381 }
382
383 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
384 return _mm256_acos_pd(a.raw); // AVX
385 }
386
387 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
388 return _mm256_asin_pd(a.raw); // AVX
389 }
390
391 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
392 return _mm256_atan_pd(a.raw); // AVX
393 }
394
395 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
396 return _mm256_exp_pd(a.raw); // AVX
397 }
398
399 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
400 return _mm256_log_pd(a.raw); // AVX
401 }
402
403 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
404 return _mm256_tanh_pd(a.raw); // AVX
405 }
406#endif // MOCHI_ARCH_X64_SVML
407
408 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
409#if MOCHI_ARCH_X64_FMA
410 return {_mm256_fmadd_pd(a.raw, b.raw, c.raw)}; // FMA
411#else
412 return (a * b) + c;
413#endif
414 }
415
416 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
417#if MOCHI_ARCH_X64_FMA
418 return _mm256_fmsub_pd(a.raw, b.raw, c.raw); // FMA
419#else
420 return (a * b) - c;
421#endif
422 }
423
424 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
425#if MOCHI_ARCH_X64_FMA
426 return _mm256_fnmadd_pd(a.raw, b.raw, c.raw); // FMA
427#else
428 return -(a * b) + c;
429#endif
430 }
431
432 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
433#if MOCHI_ARCH_X64_FMA
434 return _mm256_fnmsub_pd(a.raw, b.raw, c.raw); // FMA
435#else
436 return -(a * b) - c;
437#endif
438 }
439
440 template <int N = 4>
441 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
442 static_assert(N >= 2 && N <= 4, "Unsupported N");
443 using HalfT = Simd<Scalar, 2>;
444 if constexpr (N == 2) {
445 return Get<0>(Min(a, Broadcast<1>(a)));
446 } else if constexpr (N == 3) {
447 auto lo = GetHalf<0>(a);
448 auto hi = GetHalf<1>(a);
449 return HalfT::Get<0>(HalfT::Min(HalfT::Min(lo, HalfT::Broadcast<1>(lo)), hi));
450 } else {
451 return HalfT::HMin(HalfT::Min(GetHalf<0>(a), GetHalf<1>(a)));
452 }
453 }
454
455 template <int N = 4>
456 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
457 static_assert(N >= 2 && N <= 4, "Unsupported N");
458 using HalfT = Simd<Scalar, 2>;
459 if constexpr (N == 2) {
460 return Get<0>(Max(a, Broadcast<1>(a)));
461 } else if constexpr (N == 3) {
462 auto lo = GetHalf<0>(a);
463 auto hi = GetHalf<1>(a);
464 return HalfT::Get<0>(HalfT::Max(HalfT::Max(lo, HalfT::Broadcast<1>(lo)), hi));
465 } else {
466 return HalfT::HMax(HalfT::Max(GetHalf<0>(a), GetHalf<1>(a)));
467 }
468 }
469
470 template <int N>
471 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
472 static_assert(N >= 2 && N <= 4, "Unsupported N");
473 if constexpr (N == 2) {
474 return Get<0>(a) + Get<1>(a);
475 } else if constexpr (N == 3) {
476 using HalfT = Simd<double, 2>;
477 HalfT tmp = GetHalf<0>(a) + GetHalf<1>(a);
478 return HalfT::Get<0>(tmp) + Get<1>(a); // (a[0] + a[2]) + a[1]
479 } else if constexpr (N == 4) {
480 // PERF NOTE: An alternate implementation could be: Get<0>(a) + Get<1>(a) + Get<2>(a) +
481 // Get<3>(a). In comparison, this implementation saves two instructions, while maintaining
482 // identical throughput and latency, when benchmarked on an AMD CPU.
483 using HalfT = Simd<double, 2>;
484 HalfT tmp = GetHalf<0>(a) + GetHalf<1>(a);
485 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp); // (a[0] + a[2]) + (a[1] + a[3]);
486 }
487 }
488
489 [[nodiscard]] static MOCHI_FORCE_INLINE Simd HSumEach(Simd a, Simd b, Simd c, Simd d) {
490 // ac = {a[0] + a[2], a[1] + a[3], c[0] + c[2], c[1] + c[3]}, and bd likewise.
491 auto const ac = _mm256_add_pd(
492 _mm256_blend_pd(a.raw, c.raw, 0b1100), _mm256_permute2f128_pd(a.raw, c.raw, 0x21)); // AVX
493 auto const bd = _mm256_add_pd(
494 _mm256_blend_pd(b.raw, d.raw, 0b1100), _mm256_permute2f128_pd(b.raw, d.raw, 0x21)); // AVX
495 // {ac[0], bd[0], ac[2], bd[2]} + {ac[1], bd[1], ac[3], bd[3]}.
496 return _mm256_add_pd(_mm256_unpacklo_pd(ac, bd), _mm256_unpackhi_pd(ac, bd)); // AVX
497 }
498
499 template <int N>
500 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
501 static_assert(N >= 2 && N <= 4, "Unsupported N");
502 alignas(alignof(Simd)) Scalar buf[4];
503 Store(buf, a);
504 if constexpr (N == 2) {
505 return buf[0] * buf[1];
506 } else if constexpr (N == 3) {
507 return buf[0] * buf[1] * buf[2];
508 } else if constexpr (N == 4) {
509 return buf[0] * buf[1] * buf[2] * buf[3];
510 }
511 }
512
513 template <int N>
514 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
515 static_assert(N >= 2 && N <= 4, "Unsupported N");
516 return Simd{HSum<N>(a * b)};
517 }
518
519 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
520 return _mm256_cmp_pd(this->raw, rhs.raw, _CMP_LT_OQ); // AVX
521 }
522
523 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
524 return _mm256_cmp_pd(this->raw, rhs.raw, _CMP_GT_OQ); // AVX
525 }
526
527 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
528 return _mm256_cmp_pd(this->raw, rhs.raw, _CMP_LE_OQ); // AVX
529 }
530
531 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
532 return _mm256_cmp_pd(this->raw, rhs.raw, _CMP_GE_OQ); // AVX
533 }
534
535 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
536 return _mm256_cmp_pd(a.raw, b.raw, _CMP_EQ_OQ); // AVX
537 }
538
539 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
540 return _mm256_cmp_pd(a.raw, b.raw, _CMP_NEQ_UQ); // AVX
541 }
542
543 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
544 return _mm256_setzero_pd(); // AVX
545 }
546
547 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
548 return ToMask(Equal(*this, rhs)) == 0xF; // All values equal
549 }
550
551 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
552 return ToMask(NotEqual(*this, rhs)) != 0; // Any values not equal
553 }
554
555 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
556 auto ones = _mm256_castsi256_pd(_mm256_set1_epi64x(-1)); // AVX, AVX
557 return _mm256_xor_pd(raw, ones); // AVX
558 }
559
560 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
561 return _mm256_xor_pd(raw, SignBitMask().raw); // AVX, AVX
562 }
563
564 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
565 return _mm256_add_pd(raw, rhs.raw); // AVX
566 }
567
568 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
569 return _mm256_sub_pd(raw, rhs.raw); // AVX
570 }
571
572 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
573 return _mm256_mul_pd(raw, rhs.raw); // AVX
574 }
575
576 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
577 return _mm256_div_pd(raw, rhs.raw); // AVX
578 }
579
580 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
581 return _mm256_and_pd(raw, rhs.raw); // AVX
582 }
583
584 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
585 return _mm256_or_pd(raw, rhs.raw); // AVX
586 }
587
588 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
589 return _mm256_xor_pd(raw, rhs.raw); // AVX
590 }
591
592 private:
593 // One bit per lane, from the lane's sign bit.
594 [[nodiscard]] static MOCHI_FORCE_INLINE int ToMask(Simd a) {
595 return _mm256_movemask_pd(a.raw); // AVX
596 }
597
598 // Splits 12 consecutive scalars {a, b, c} into 3 vectors of every third scalar.
599 MOCHI_FORCE_INLINE static void
600 Deinterleave3(__m256d a, __m256d b, __m256d c, Simd& out0, Simd& out1, Simd& out2) {
601 auto d = _mm256_blend_pd(a, b, 0b0100); // [0,_,6,3]
602 d = _mm256_blend_pd(d, c, 0b0010); // [0,9,6,3]
603 auto e = _mm256_permute2f128_pd(d, d, 0x01); // [6,3,0,9]
604 out0.raw = _mm256_blend_pd(d, e, 0b1010); // [0,3,6,9]
605
606 d = _mm256_blend_pd(a, b, 0b1001); // [4,1,_,7]
607 d = _mm256_blend_pd(d, c, 0b0100); // [4,1,10,7]
608 out1.raw = _mm256_shuffle_pd(d, d, 0b0101); // [1,4,7,10]
609
610 d = _mm256_blend_pd(a, b, 0b0010); // [_,5,2,_]
611 d = _mm256_blend_pd(d, c, 0b1001); // [8,5,2,11]
612 e = _mm256_permute2f128_pd(d, d, 0x01); // [2,11,8,5]
613 out2.raw = _mm256_blend_pd(d, e, 0b0101); // [2,5,8,11]
614 }
615
616 // Inverse of Deinterleave3: out0, out1, out2 are the 12 interleaved scalars in memory order.
617 MOCHI_FORCE_INLINE static void
618 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
619 // a = [0,3,6,9], b = [1,4,7,10], c = [2,5,8,11]
620 auto d = _mm256_shuffle_pd(b.raw, b.raw, 0b0101); // [4,1,10,7]
621 auto e = _mm256_blend_pd(a.raw, c.raw, 0b0101); // [2,3,8,9]
622 e = _mm256_permute2f128_pd(e, e, 0x01); // [8,9,2,3]
623 auto f = _mm256_blend_pd(a.raw, d, 0b1010); // [0,1,6,7]
624 auto g = _mm256_blend_pd(d, c.raw, 0b1010); // [4,5,10,11]
625 out0 = _mm256_blend_pd(f, e, 0b1100); // [0,1,2,3]
626 out1 = _mm256_blend_pd(g, f, 0b1100); // [4,5,6,7]
627 out2 = _mm256_blend_pd(e, g, 0b1100); // [8,9,10,11]
628 }
629};
630
631} // namespace superdex
632
633#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)
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Definition simd_inl.h:322
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)