SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_double_8_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_AVX512
22
23namespace superdex {
24
25/***********************************************************************************************
26 Simd<double, 8>
27*/
28template <>
29class Simd<double, 8> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(double, 8, __m512d);
32 Simd(
33 double a,
34 double b,
35 double c = 0.0,
36 double d = 0.0,
37 double e = 0.0,
38 double f = 0.0,
39 double g = 0.0,
40 double h = 0.0)
41 : raw(_mm512_set_pd(h, g, f, e, d, c, b, a)) {} // AVX512F
42
43 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
44 Simd(U a) : raw(_mm512_set1_pd(a)) {} // AVX512F
45
46 Simd(Simd<double, 4> const& low, Simd<double, 4> const& high)
47 : raw(_mm512_insertf64x4(_mm512_castpd256_pd512(low.raw), high.raw, 1)) {} // AVX512F
48
49 template <int i>
50 [[nodiscard]] static MOCHI_FORCE_INLINE double Get(Simd v) {
51 static_assert(i >= 0 && i < kSize, "Index out of range");
52 return v[i];
53 }
54
55 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
56 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
57#if MOCHI_COMPILER_MSVC
58 return raw.m512d_f64[i];
59#else
60 return raw[i];
61#endif
62 }
63
64 template <int iHalf>
65 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<double, 4> GetHalf(Simd a) {
66 static_assert(iHalf == 0 || iHalf == 1);
67 if constexpr (iHalf == 0) {
68 return _mm512_castpd512_pd256(a.raw); // AVX512F
69 } else {
70 return _mm512_extractf64x4_pd(a.raw, 1); // AVX512F
71 }
72 }
73
74 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
75 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
76 auto const mask = static_cast<__mmask8>(1u << i);
77 return _mm512_mask_broadcastsd_pd(v.raw, mask, _mm_set_sd(value));
78 }
79
80 template <int i>
81 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
82 static_assert(i >= 0 && i < kSize, "Index out of range");
83 constexpr auto kMask = static_cast<__mmask8>(1u << i);
84 return _mm512_mask_broadcastsd_pd(v.raw, kMask, _mm_set_sd(value));
85 }
86
87 template <int N>
88 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
89 static_assert(N >= 1 && N <= kSize, "Unsupported N");
90 auto const mask = ToMask(v);
91 if constexpr (N == kSize) {
92 return _kortestc_mask8_u8(mask, mask) != 0;
93 } else {
94 constexpr auto kLanes = LaneMask<N>();
95 return (mask & kLanes) == kLanes;
96 }
97 }
98
99 template <int N>
100 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
101 static_assert(N >= 1 && N <= kSize, "Unsupported N");
102 auto const mask = ToMask(v);
103 if constexpr (N == kSize) {
104 return _kortestz_mask8_u8(mask, mask) == 0;
105 } else {
106 return (mask & LaneMask<N>()) != 0;
107 }
108 }
109
110 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
111 return _mm512_set1_pd(*p); // AVX512F
112 }
113
114 template <int i>
115 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
116 static_assert(i >= 0 && i < kSize, "Index out of range");
117 if constexpr (i == 0) {
118 return _mm512_broadcastsd_pd(_mm512_castpd512_pd128(v.raw));
119 } else {
120 constexpr int kLane = i % 2;
121 constexpr int kGroup = i / 2;
122 auto const group =
123 _mm512_shuffle_f64x2(v.raw, v.raw, _MM_SHUFFLE(kGroup, kGroup, kGroup, kGroup));
124 return _mm512_permute_pd(group, kLane == 0 ? 0x00 : 0xFF);
125 }
126 }
127
128 template <int N = kSize>
129 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
130 static_assert(N >= 0 && N <= kSize);
131 if constexpr (N == 0) {
132 return Zero();
133 } else if constexpr (N == 1) {
134 return _mm512_zextpd128_pd512(_mm_load_sd(ptr));
135 } else if constexpr (N == 2) {
136 return _mm512_zextpd128_pd512(_mm_loadu_pd(ptr));
137 } else if constexpr (N == 3) {
138 return _mm512_zextpd256_pd512(_mm256_maskz_loadu_pd(LaneMask<N>(), ptr));
139 } else if constexpr (N == 4) {
140 return _mm512_zextpd256_pd512(_mm256_loadu_pd(ptr));
141 } else if constexpr (N < kSize) {
142 return _mm512_maskz_loadu_pd(LaneMask<N>(), ptr); // AVX512F
143 } else {
144 return _mm512_loadu_pd(ptr); // AVX512F
145 }
146 }
147
148 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
149 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
150 return _mm512_maskz_loadu_pd(LaneMask(n), ptr); // AVX512F
151 }
152
153 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int, 8> const& indices) {
154 return _mm512_i32gather_pd(indices.raw, ptr, sizeof(double)); // AVX512F
155 }
156
157 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int64_t, 8> const& indices) {
158 return _mm512_i64gather_pd(indices.raw, ptr, sizeof(double)); // AVX512F
159 }
160
161 template <int kTupleCount = kSize>
162 MOCHI_FORCE_INLINE static void
163 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
164 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
165 if constexpr (kTupleCount == 1) {
166 out0 = Load<1>(ptr);
167 out1 = Load<1>(ptr + 1);
168 out2 = Load<1>(ptr + 2);
169 return;
170 }
171 constexpr int kTotalCount = kTupleCount * 3;
172 auto const x0 = Load<Clamp(kTotalCount, 0, kSize)>(ptr).raw;
173 if constexpr (kTupleCount <= 5) {
174 auto const index0 = LoadTransposeIndices<kTupleCount, 0>();
175 auto const index1 = LoadTransposeIndices<kTupleCount, 1>();
176 auto const index2 = LoadTransposeIndices<kTupleCount, 2>();
177 if constexpr (kTupleCount <= 2) {
178 out0.raw = _mm512_permutexvar_pd(index0, x0);
179 out1.raw = _mm512_permutexvar_pd(index1, x0);
180 out2.raw = _mm512_permutexvar_pd(index2, x0);
181 } else {
182 auto const x1 = Load<kTotalCount - kSize>(ptr + kSize).raw;
183 out0.raw = _mm512_permutex2var_pd(x0, index0, x1);
184 out1.raw = _mm512_permutex2var_pd(x0, index1, x1);
185 out2.raw = _mm512_permutex2var_pd(x0, index2, x1);
186 }
187 } else {
188 auto const x1 = Load<kSize>(ptr + kSize).raw;
189 constexpr int kCount2 = kTotalCount - 2 * kSize;
190 auto const x2 = Load<kCount2>(ptr + 2 * kSize).raw;
191 auto const index0 = _mm512_setr_epi64(0, 3, 6, 9, 12, 15, 0, 0);
192 auto const index1 = _mm512_setr_epi64(1, 4, 7, 10, 13, 0, 0, 0);
193 auto const index2 = _mm512_setr_epi64(2, 5, 8, 11, 14, 0, 0, 0);
194 if constexpr (kTupleCount == 6) {
195 out0.raw = _mm512_maskz_permutex2var_pd(LaneMask<kTupleCount>(), x0, index0, x1);
196 constexpr int kZeroIndex = kSize + kCount2;
197 auto const partial1 = _mm512_permutex2var_pd(x0, index1, x1);
198 auto const finalIndex1 = _mm512_setr_epi64(0, 1, 2, 3, 4, 8, kZeroIndex, kZeroIndex);
199 out1.raw = _mm512_permutex2var_pd(partial1, finalIndex1, x2);
200 auto const partial2 = _mm512_permutex2var_pd(x0, index2, x1);
201 auto const finalIndex2 = _mm512_setr_epi64(0, 1, 2, 3, 4, 9, kZeroIndex, kZeroIndex);
202 out2.raw = _mm512_permutex2var_pd(partial2, finalIndex2, x2);
203 } else {
204 constexpr int kZeroIndex = kSize + kCount2;
205 auto const partial0 = _mm512_permutex2var_pd(x0, index0, x1);
206 auto const finalIndex0 = _mm512_setr_epi64(
207 0, 1, 2, 3, 4, 5, kTupleCount > 6 ? 10 : kZeroIndex, kTupleCount > 7 ? 13 : kZeroIndex);
208 out0.raw = _mm512_permutex2var_pd(partial0, finalIndex0, x2);
209 auto const partial1 = _mm512_permutex2var_pd(x0, index1, x1);
210 auto const finalIndex1 = _mm512_setr_epi64(
211 0, 1, 2, 3, 4, 8, kTupleCount > 6 ? 11 : kZeroIndex, kTupleCount > 7 ? 14 : kZeroIndex);
212 out1.raw = _mm512_permutex2var_pd(partial1, finalIndex1, x2);
213 auto const partial2 = _mm512_permutex2var_pd(x0, index2, x1);
214 auto const finalIndex2 = _mm512_setr_epi64(
215 0, 1, 2, 3, 4, 9, kTupleCount > 6 ? 12 : kZeroIndex, kTupleCount > 7 ? 15 : kZeroIndex);
216 out2.raw = _mm512_permutex2var_pd(partial2, finalIndex2, x2);
217 }
218 }
219 }
220
221 MOCHI_FORCE_INLINE static void
222 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
223 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
224 int const n = count * 3;
225 auto const x0 = Load(ptr, superdex::Min(n, kSize)).raw;
226 auto const x1 = Load(n > kSize ? ptr + kSize : ptr, Clamp(n - kSize, 0, kSize)).raw;
227 auto const x2 = Load(n > 2 * kSize ? ptr + 2 * kSize : ptr, Clamp(n - 2 * kSize, 0, kSize)).raw;
228 out0.raw = DeinterleaveComponent<0>(x0, x1, x2);
229 out1.raw = DeinterleaveComponent<1>(x0, x1, x2);
230 out2.raw = DeinterleaveComponent<2>(x0, x1, x2);
231 }
232
233 template <int N = kSize>
234 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
235 static_assert(N >= 0 && N <= kSize);
236 if constexpr (N == 0) {
237 } else if constexpr (N == 1) {
238 _mm_store_sd(ptr, _mm512_castpd512_pd128(v.raw));
239 } else if constexpr (N == 2) {
240 _mm_storeu_pd(ptr, _mm512_castpd512_pd128(v.raw));
241 } else if constexpr (N == 3) {
242 _mm256_mask_storeu_pd(
243 ptr, static_cast<__mmask8>((uint32_t{1} << N) - 1), _mm512_castpd512_pd256(v.raw));
244 } else if constexpr (N == 4) {
245 _mm256_storeu_pd(ptr, _mm512_castpd512_pd256(v.raw));
246 } else if constexpr (N < kSize) {
247 _mm512_mask_storeu_pd(ptr, LaneMask(N), v.raw); // AVX512F
248 } else {
249 _mm512_storeu_pd(ptr, v.raw); // AVX512F
250 }
251 }
252
253 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
254 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
255 _mm512_mask_storeu_pd(ptr, LaneMask(n), v.raw); // AVX512F
256 }
257
258 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
259 __mmask8 const mask = ToMask(condition);
260 _mm512_mask_compressstoreu_pd(ptr, mask, values.raw); // AVX512F
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 if constexpr (kTupleCount == 1) {
268 Store<1>(ptr, a);
269 Store<1>(ptr + 1, b);
270 Store<1>(ptr + 2, c);
271 return;
272 }
273 constexpr int kTotalCount = kTupleCount * 3;
274 Simd x0, x1, x2;
275 Interleave3(a, b, c, x0, x1, x2);
277 if constexpr (kTotalCount > kSize) {
278 Store<Clamp(kTotalCount - kSize, 0, kSize)>(ptr + kSize, x1);
279 }
280 if constexpr (kTotalCount > 2 * kSize) {
281 Store<kTotalCount - 2 * kSize>(ptr + 2 * kSize, x2);
282 }
283 }
284
285 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
286 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
287 Simd x0, x1, x2;
288 Interleave3(a, b, c, x0, x1, x2);
289 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
290 // back into branches. A zero mask stores nothing.
291 uint32_t const bits = (1u << (count * 3)) - 1;
292 _mm512_mask_storeu_pd(ptr, static_cast<__mmask8>(bits), x0.raw); // AVX512F
293 _mm512_mask_storeu_pd(ptr + (count > 2 ? 8 : 0), static_cast<__mmask8>(bits >> 8), x1.raw);
294 _mm512_mask_storeu_pd(ptr + (count > 5 ? 16 : 0), static_cast<__mmask8>(bits >> 16), x2.raw);
295 }
296
297 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
298 return _mm512_mask_blend_pd(ToMask(mask), b.raw, a.raw); // AVX512F
299 }
300
301 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
302 return _mm512_sqrt_pd(v.raw); // AVX512F
303 }
304
305 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
306 return _mm512_rcp14_pd(v.raw); // AVX512F
307 }
308
309 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
310 return _mm512_rsqrt14_pd(v.raw); // AVX512F
311 }
312
313 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
314 return _mm512_castsi512_pd(
315 _mm512_set1_epi64(static_cast<long long>(0x8000000000000000ULL))); // AVX512F
316 }
317
318 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
319 return _mm512_abs_pd(v.raw); // AVX512F
320 }
321
322 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
323 return _mm512_min_pd(a.raw, b.raw); // AVX512F
324 }
325
326 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
327 return _mm512_max_pd(a.raw, b.raw); // AVX512F
328 }
329
330 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
331 return _mm512_floor_pd(a.raw); // AVX512F
332 }
333
334 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
335 return _mm512_roundscale_pd(v.raw, _MM_FROUND_TO_NEAREST_INT); // AVX512F
336 }
337
338#if MOCHI_ARCH_X64_SVML
339 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
340 return _mm512_cos_pd(a.raw);
341 }
342
343 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
344 return _mm512_sin_pd(a.raw);
345 }
346
347 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
348 return _mm512_tan_pd(a.raw);
349 }
350
351 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
352 return _mm512_acos_pd(a.raw);
353 }
354
355 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
356 return _mm512_asin_pd(a.raw);
357 }
358
359 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
360 return _mm512_atan_pd(a.raw);
361 }
362
363 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
364 return _mm512_exp_pd(a.raw);
365 }
366
367 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
368 return _mm512_log_pd(a.raw);
369 }
370
371 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
372 return _mm512_tanh_pd(a.raw);
373 }
374#endif // MOCHI_ARCH_X64_SVML
375
376 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
377 return _mm512_fmadd_pd(a.raw, b.raw, c.raw); // AVX512F
378 }
379
380 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
381 return _mm512_fmsub_pd(a.raw, b.raw, c.raw); // AVX512F
382 }
383
384 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
385 return _mm512_fnmadd_pd(a.raw, b.raw, c.raw); // AVX512F
386 }
387
388 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
389 return _mm512_fnmsub_pd(a.raw, b.raw, c.raw); // AVX512F
390 }
391
392 template <int N = kSize>
393 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
394 static_assert(N >= 2 && N <= kSize, "Unsupported N");
395 using HalfT = Simd<Scalar, 4>;
396 auto const lo = GetHalf<0>(a);
397 if constexpr (N <= 4) {
398 return HalfT::template HMin<N>(lo);
399 } else {
400 auto const hi = GetHalf<1>(a);
401 if constexpr (N == 5) {
402 return superdex::Min(HalfT::template HMin<4>(lo), HalfT::template Get<0>(hi));
403 } else if constexpr (N == 8) {
404 return HalfT::template HMin<4>(HalfT::Min(lo, hi));
405 } else {
406 return _mm512_mask_reduce_min_pd(LaneMask<N>(), a.raw);
407 }
408 }
409 }
410
411 template <int N = kSize>
412 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
413 static_assert(N >= 2 && N <= kSize, "Unsupported N");
414 using HalfT = Simd<Scalar, 4>;
415 auto const lo = GetHalf<0>(a);
416 if constexpr (N <= 4) {
417 return HalfT::template HMax<N>(lo);
418 } else {
419 auto const hi = GetHalf<1>(a);
420 if constexpr (N == 5) {
421 return superdex::Max(HalfT::template HMax<4>(lo), HalfT::template Get<0>(hi));
422 } else if constexpr (N == 8) {
423 return HalfT::template HMax<4>(HalfT::Max(lo, hi));
424 } else {
425 return _mm512_mask_reduce_max_pd(LaneMask<N>(), a.raw);
426 }
427 }
428 }
429
430 template <int N>
431 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
432 static_assert(N >= 2 && N <= kSize, "Unsupported N");
433 using HalfT = Simd<Scalar, 4>;
434 auto const lo = GetHalf<0>(a);
435 if constexpr (N <= 4) {
436 return HalfT::template HSum<N>(lo);
437 } else {
438 auto const hi = GetHalf<1>(a);
439 if constexpr (N == 5) {
440 return HalfT::template HSum<4>(lo) + HalfT::template Get<0>(hi);
441 } else if constexpr (N == kSize) {
442 return HalfT::template HSum<4>(lo + hi);
443 } else {
444 return _mm512_mask_reduce_add_pd(LaneMask<N>(), a.raw);
445 }
446 }
447 }
448
449 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<double, 4> HSumEach(Simd a, Simd b, Simd c, Simd d) {
450 // In each 128-bit lane: ab = {a[0] + a[1], b[0] + b[1]}, and cd likewise.
451 auto const ab = _mm512_add_pd(
452 _mm512_unpacklo_pd(a.raw, b.raw), _mm512_unpackhi_pd(a.raw, b.raw)); // AVX512F
453 auto const cd = _mm512_add_pd(
454 _mm512_unpacklo_pd(c.raw, d.raw), _mm512_unpackhi_pd(c.raw, d.raw)); // AVX512F
455 // Halves first: the lane-crossing shuffle below is cheaper in 256 bits than in 512.
456 auto const ab4 =
457 _mm256_add_pd(_mm512_castpd512_pd256(ab), _mm512_extractf64x4_pd(ab, 1)); // AVX512F
458 auto const cd4 =
459 _mm256_add_pd(_mm512_castpd512_pd256(cd), _mm512_extractf64x4_pd(cd, 1)); // AVX512F
460 // {ab4[0], ab4[1], cd4[2], cd4[3]} + {ab4[2], ab4[3], cd4[0], cd4[1]}.
461 return _mm256_add_pd(
462 _mm256_blend_pd(ab4, cd4, 0b1100), _mm256_permute2f128_pd(ab4, cd4, 0x21)); // AVX
463 }
464
465 template <int N>
466 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HProd(Simd a) {
467 static_assert(N >= 2 && N <= kSize, "Unsupported N");
468 using HalfT = Simd<Scalar, 4>;
469 auto const lo = GetHalf<0>(a);
470 if constexpr (N <= 4) {
471 return HalfT::template HProd<N>(lo);
472 } else {
473 auto const hi = GetHalf<1>(a);
474 if constexpr (N == 5) {
475 return HalfT::template HProd<4>(lo) * HalfT::template Get<0>(hi);
476 } else if constexpr (N == kSize) {
477 return HalfT::template HProd<4>(lo * hi);
478 } else {
479 return _mm512_mask_reduce_mul_pd(LaneMask<N>(), a.raw);
480 }
481 }
482 }
483
484 template <int N>
485 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
486 static_assert(N >= 2 && N <= kSize, "Unsupported N");
487 return Simd{HSum<N>(a * b)};
488 }
489
490 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
491 return FromMask(_mm512_cmp_pd_mask(raw, rhs.raw, _CMP_LT_OQ)); // AVX512F
492 }
493
494 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
495 return FromMask(_mm512_cmp_pd_mask(raw, rhs.raw, _CMP_GT_OQ)); // AVX512F
496 }
497
498 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
499 return FromMask(_mm512_cmp_pd_mask(raw, rhs.raw, _CMP_LE_OQ)); // AVX512F
500 }
501
502 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
503 return FromMask(_mm512_cmp_pd_mask(raw, rhs.raw, _CMP_GE_OQ)); // AVX512F
504 }
505
506 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
507 return FromMask(_mm512_cmp_pd_mask(a.raw, b.raw, _CMP_EQ_OQ)); // AVX512F
508 }
509
510 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
511 return FromMask(_mm512_cmp_pd_mask(a.raw, b.raw, _CMP_NEQ_UQ)); // AVX512F
512 }
513
514 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
515 return _mm512_setzero_pd(); // AVX512F
516 }
517
518 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
519 return _mm512_cmp_pd_mask(raw, rhs.raw, _CMP_EQ_OQ) == static_cast<__mmask8>(0xFFu);
520 }
521
522 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
523 return _mm512_cmp_pd_mask(raw, rhs.raw, _CMP_NEQ_UQ) != 0;
524 }
525
526 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
527 return _mm512_castsi512_pd(
528 _mm512_xor_si512(_mm512_castpd_si512(raw), _mm512_set1_epi64(-1))); // AVX512F
529 }
530
531 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
532 return _mm512_xor_pd(raw, SignBitMask().raw); // AVX512DQ
533 }
534
535 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
536 return _mm512_add_pd(raw, rhs.raw); // AVX512F
537 }
538
539 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
540 return _mm512_sub_pd(raw, rhs.raw); // AVX512F
541 }
542
543 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
544 return _mm512_mul_pd(raw, rhs.raw); // AVX512F
545 }
546
547 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
548 return _mm512_div_pd(raw, rhs.raw); // AVX512F
549 }
550
551 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
552 return _mm512_and_pd(raw, rhs.raw); // AVX512DQ
553 }
554
555 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
556 return _mm512_or_pd(raw, rhs.raw); // AVX512DQ
557 }
558
559 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
560 return _mm512_xor_pd(raw, rhs.raw); // AVX512DQ
561 }
562
563 private:
564 template <int kTupleCount, int kComponent>
565 [[nodiscard]] static MOCHI_FORCE_INLINE __m512i LoadTransposeIndices() {
566 constexpr int kZeroIndex = kTupleCount * 3;
567 return _mm512_setr_epi64(
568 kComponent,
569 kTupleCount > 1 ? 3 + kComponent : kZeroIndex,
570 kTupleCount > 2 ? 6 + kComponent : kZeroIndex,
571 kTupleCount > 3 ? 9 + kComponent : kZeroIndex,
572 kTupleCount > 4 ? 12 + kComponent : kZeroIndex,
573 kTupleCount > 5 ? 15 + kComponent : kZeroIndex,
574 kTupleCount > 6 ? 18 + kComponent : kZeroIndex,
575 kTupleCount > 7 ? 21 + kComponent : kZeroIndex);
576 }
577
578 // Returns every third scalar, starting at kComponent, of the 3 * kSize consecutive scalars
579 // {x0, x1, x2}.
580 template <int kComponent>
581 [[nodiscard]] static MOCHI_FORCE_INLINE __m512d
582 DeinterleaveComponent(__m512d x0, __m512d x1, __m512d x2) {
583 auto const index = LoadTransposeIndices<kSize, kComponent>();
584 // Lanes whose index is at least 2 * kSize read from x2. permutexvar uses the index modulo
585 // kSize.
586 constexpr auto kLanesFromX2 =
587 static_cast<__mmask8>(~LaneMask<(2 * kSize - kComponent + 2) / 3>());
588 auto const fromX0X1 = _mm512_permutex2var_pd(x0, index, x1); // AVX512F
589 return _mm512_mask_permutexvar_pd(fromX0X1, kLanesFromX2, index, x2); // AVX512F
590 }
591
592 // Inverse of DeinterleaveComponent<0..2>: out0, out1, out2 are the 24 interleaved scalars in
593 // memory order.
594 MOCHI_FORCE_INLINE static void
595 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
596 auto const ab0 =
597 _mm512_permutex2var_pd(a.raw, _mm512_setr_epi64(0, 8, 0, 1, 9, 0, 2, 10), b.raw);
598 out0.raw = _mm512_permutex2var_pd(ab0, _mm512_setr_epi64(0, 1, 8, 3, 4, 9, 6, 7), c.raw);
599 auto const ab1 =
600 _mm512_permutex2var_pd(a.raw, _mm512_setr_epi64(0, 3, 11, 0, 4, 12, 0, 5), b.raw);
601 out1.raw = _mm512_permutex2var_pd(ab1, _mm512_setr_epi64(10, 1, 2, 11, 4, 5, 12, 7), c.raw);
602 auto const ab2 =
603 _mm512_permutex2var_pd(a.raw, _mm512_setr_epi64(13, 0, 6, 14, 0, 7, 15, 0), b.raw);
604 out2.raw = _mm512_permutex2var_pd(ab2, _mm512_setr_epi64(0, 13, 2, 3, 14, 5, 6, 15), c.raw);
605 }
606
607 // Returns a mask selecting the lowest N lanes.
608 template <int N>
609 [[nodiscard]] static constexpr __mmask8 LaneMask() {
610 static_assert(N >= 0 && N <= kSize);
611 return LaneMask(N);
612 }
613
614 // Returns a mask selecting the lowest n lanes.
615 [[nodiscard]] static constexpr __mmask8 LaneMask(int n) {
616 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid lane count");
617 return static_cast<__mmask8>((uint32_t{1} << n) - 1);
618 }
619
620 // Converts a canonical logical vector (all-zero or all-one lanes) to a mask.
621 [[nodiscard]] static MOCHI_FORCE_INLINE __mmask8 ToMask(Simd a) {
622 auto const bits = _mm512_castpd_si512(a.raw);
623 auto const mask = _mm512_movepi64_mask(bits); // AVX512DQ
625 _mm512_cmpeq_epi64_mask(bits, _mm512_movm_epi64(mask)) == LaneMask<kSize>(),
626 "Expected a canonical logical mask");
627 return mask;
628 }
629
630 // Expands a mask into a canonical logical vector (all-zero or all-one lanes).
631 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FromMask(__mmask8 mask) {
632 return _mm512_castsi512_pd(_mm512_movm_epi64(mask)); // AVX512DQ
633 }
634};
635
636} // namespace superdex
637
638#endif // MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX512
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_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 > 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)
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)