SuperDex Physics C++ API
Loading...
Searching...
No Matches
x64_simd_float_16_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<float, 16>
27*/
28template <>
29class Simd<float, 16> {
30 public:
31 MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(float, 16, __m512);
32 Simd(
33 float a,
34 float b,
35 float c = 0.0f,
36 float d = 0.0f,
37 float e = 0.0f,
38 float f = 0.0f,
39 float g = 0.0f,
40 float h = 0.0f,
41 float i = 0.0f,
42 float j = 0.0f,
43 float k = 0.0f,
44 float l = 0.0f,
45 float m = 0.0f,
46 float n = 0.0f,
47 float o = 0.0f,
48 float p = 0.0f)
49 : raw(_mm512_set_ps(p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a)) {} // AVX512F
50
51 template <class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
52 Simd(U a) : raw(_mm512_set1_ps(a)) {} // AVX512F
53
54 Simd(Simd<float, 8> const& low, Simd<float, 8> const& high)
55 : raw(_mm512_insertf32x8(_mm512_castps256_ps512(low.raw), high.raw, 1)) {} // AVX512DQ
56
57 template <int i>
58 [[nodiscard]] static MOCHI_FORCE_INLINE float Get(Simd v) {
59 static_assert(i >= 0 && i < kSize, "Index out of range");
60 return v[i];
61 }
62
63 [[nodiscard]] MOCHI_FORCE_INLINE Scalar operator[](int i) const {
64 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
65#if MOCHI_COMPILER_MSVC
66 return raw.m512_f32[i];
67#else
68 return raw[i];
69#endif
70 }
71
72 template <int iHalf>
73 [[nodiscard]] static MOCHI_FORCE_INLINE Simd<float, 8> GetHalf(Simd a) {
74 static_assert(iHalf == 0 || iHalf == 1);
75 if constexpr (iHalf == 0) {
76 return _mm512_castps512_ps256(a.raw); // AVX512F
77 } else {
78 return _mm512_extractf32x8_ps(a.raw, 1); // AVX512DQ
79 }
80 }
81
82 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, int i, Scalar value) {
83 MOCHI_ASSERT_VERBOSE(i >= 0 && i < kSize, "Index out of range");
84 auto const mask = static_cast<__mmask16>(1u << i);
85 return _mm512_mask_broadcastss_ps(v.raw, mask, _mm_set_ss(value));
86 }
87
88 template <int i>
89 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Set(Simd v, Scalar value) {
90 static_assert(i >= 0 && i < kSize, "Index out of range");
91 constexpr auto kMask = static_cast<__mmask16>(1u << i);
92 return _mm512_mask_broadcastss_ps(v.raw, kMask, _mm_set_ss(value));
93 }
94
95 template <int N>
96 [[nodiscard]] static MOCHI_FORCE_INLINE bool AllTrue(Simd v) {
97 static_assert(N >= 1 && N <= kSize, "Unsupported N");
98 auto const mask = ToMask(v);
99 if constexpr (N == kSize) {
100 return _kortestc_mask16_u8(mask, mask) != 0;
101 } else {
102 constexpr auto kLanes = LaneMask<N>();
103 return (mask & kLanes) == kLanes;
104 }
105 }
106
107 template <int N>
108 [[nodiscard]] static MOCHI_FORCE_INLINE bool AnyTrue(Simd v) {
109 static_assert(N >= 1 && N <= kSize, "Unsupported N");
110 auto const mask = ToMask(v);
111 if constexpr (N == kSize) {
112 return _kortestz_mask16_u8(mask, mask) == 0;
113 } else {
114 return (mask & LaneMask<N>()) != 0;
115 }
116 }
117
118 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Scalar const* p) {
119 return _mm512_set1_ps(*p); // AVX512F
120 }
121
122 template <int i>
123 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Broadcast(Simd v) {
124 static_assert(i >= 0 && i < kSize, "Index out of range");
125 if constexpr (i == 0) {
126 return _mm512_broadcastss_ps(_mm512_castps512_ps128(v.raw));
127 } else {
128 constexpr int kLane = i % 4;
129 constexpr int kGroup = i / 4;
130 auto const group =
131 _mm512_shuffle_f32x4(v.raw, v.raw, _MM_SHUFFLE(kGroup, kGroup, kGroup, kGroup));
132 return _mm512_permute_ps(group, _MM_SHUFFLE(kLane, kLane, kLane, kLane));
133 }
134 }
135
136 template <int N = kSize>
137 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load([[maybe_unused]] Scalar const* ptr) {
138 static_assert(N >= 0 && N <= kSize);
139 if constexpr (N == 0) {
140 return Zero();
141 } else if constexpr (N == 1) {
142 return _mm512_zextps128_ps512(_mm_load_ss(ptr));
143 } else if constexpr (N == 2) {
144 auto const low = _mm_castsi128_ps(_mm_loadl_epi64(reinterpret_cast<__m128i const*>(ptr)));
145 return _mm512_zextps128_ps512(low);
146 } else if constexpr (N == 3) {
147 return _mm512_zextps128_ps512(
148 _mm_maskz_loadu_ps(static_cast<__mmask8>((uint32_t{1} << N) - 1), ptr));
149 } else if constexpr (N == 4) {
150 return _mm512_zextps128_ps512(_mm_loadu_ps(ptr));
151 } else if constexpr (N < 8) {
152 return _mm512_zextps256_ps512(
153 _mm256_maskz_loadu_ps(static_cast<__mmask8>((uint32_t{1} << N) - 1), ptr));
154 } else if constexpr (N == 8) {
155 return _mm512_zextps256_ps512(_mm256_loadu_ps(ptr));
156 } else if constexpr (N < kSize) {
157 return _mm512_maskz_loadu_ps(LaneMask<N>(), ptr); // AVX512F
158 } else {
159 return _mm512_loadu_ps(ptr); // AVX512F
160 }
161 }
162
163 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Load(Scalar const* ptr, int n) {
164 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
165 return _mm512_maskz_loadu_ps(LaneMask(n), ptr); // AVX512F
166 }
167
168 [[nodiscard]] static Simd LoadIndexed(Scalar const* ptr, Simd<int, 16> const& indices) {
169 return _mm512_i32gather_ps(indices.raw, ptr, sizeof(float)); // AVX512F
170 }
171
172 template <int kTupleCount = kSize>
173 MOCHI_FORCE_INLINE static void
174 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2) {
175 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
176 if constexpr (kTupleCount == 1) {
177 out0 = Load<1>(ptr);
178 out1 = Load<1>(ptr + 1);
179 out2 = Load<1>(ptr + 2);
180 return;
181 }
182 constexpr int kTotalCount = kTupleCount * 3;
183 auto const x0 = Load<Clamp(kTotalCount, 0, kSize)>(ptr).raw;
184 if constexpr (kTupleCount <= 10) {
185 auto const index0 = LoadTransposeIndices<kTupleCount, 0>();
186 auto const index1 = LoadTransposeIndices<kTupleCount, 1>();
187 auto const index2 = LoadTransposeIndices<kTupleCount, 2>();
188 if constexpr (kTupleCount <= 5) {
189 out0.raw = _mm512_permutexvar_ps(index0, x0);
190 out1.raw = _mm512_permutexvar_ps(index1, x0);
191 out2.raw = _mm512_permutexvar_ps(index2, x0);
192 } else {
193 auto const x1 = Load<kTotalCount - kSize>(ptr + kSize).raw;
194 out0.raw = _mm512_permutex2var_ps(x0, index0, x1);
195 out1.raw = _mm512_permutex2var_ps(x0, index1, x1);
196 out2.raw = _mm512_permutex2var_ps(x0, index2, x1);
197 }
198 } else {
199 // clang-format off
200 auto const x1 = Load<kSize>(ptr + kSize).raw;
201 constexpr int kCount2 = kTotalCount - 2 * kSize;
202 auto const x2 = Load<kCount2>(ptr + 2 * kSize).raw;
203 auto const index0 = _mm512_setr_epi32(0, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 0, 0, 0, 0, 0);
204 auto const index1 = _mm512_setr_epi32(1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 0, 0, 0, 0, 0);
205 auto const index2 = _mm512_setr_epi32(2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 0, 0, 0, 0, 0, 0);
206 if constexpr (kTupleCount == 11) {
207 out0.raw = _mm512_maskz_permutex2var_ps(LaneMask<kTupleCount>(), x0, index0, x1);
208 out1.raw = _mm512_maskz_permutex2var_ps(LaneMask<kTupleCount>(), x0, index1, x1);
209 constexpr int kZeroIndex = kSize + kCount2;
210 auto const partial2 = _mm512_permutex2var_ps(x0, index2, x1);
211 auto const finalIndex2 = _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, kZeroIndex, kZeroIndex, kZeroIndex, kZeroIndex, kZeroIndex);
212 out2.raw = _mm512_permutex2var_ps(partial2, finalIndex2, x2);
213 } else {
214 constexpr int kZeroIndex = kSize + kCount2;
215 auto const partial0 = _mm512_permutex2var_ps(x0, index0, x1);
216 auto const finalIndex0 = _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, kTupleCount > 11 ? 17 : kZeroIndex, kTupleCount > 12 ? 20 : kZeroIndex, kTupleCount > 13 ? 23 : kZeroIndex, kTupleCount > 14 ? 26 : kZeroIndex, kTupleCount > 15 ? 29 : kZeroIndex);
217 out0.raw = _mm512_permutex2var_ps(partial0, finalIndex0, x2);
218 auto const partial1 = _mm512_permutex2var_ps(x0, index1, x1);
219 auto const finalIndex1 = _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, kTupleCount > 11 ? 18 : kZeroIndex, kTupleCount > 12 ? 21 : kZeroIndex, kTupleCount > 13 ? 24 : kZeroIndex, kTupleCount > 14 ? 27 : kZeroIndex, kTupleCount > 15 ? 30 : kZeroIndex);
220 out1.raw = _mm512_permutex2var_ps(partial1, finalIndex1, x2);
221 auto const partial2 = _mm512_permutex2var_ps(x0, index2, x1);
222 auto const finalIndex2 = _mm512_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, kTupleCount > 11 ? 19 : kZeroIndex, kTupleCount > 12 ? 22 : kZeroIndex, kTupleCount > 13 ? 25 : kZeroIndex, kTupleCount > 14 ? 28 : kZeroIndex, kTupleCount > 15 ? 31 : kZeroIndex);
223 out2.raw = _mm512_permutex2var_ps(partial2, finalIndex2, x2);
224 }
225 // clang-format on
226 }
227 }
228
229 MOCHI_FORCE_INLINE static void
230 LoadTransposed(Scalar const* ptr, Simd& out0, Simd& out1, Simd& out2, int count) {
231 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
232 int const n = count * 3;
233 auto const x0 = Load(ptr, superdex::Min(n, kSize)).raw;
234 auto const x1 = Load(n > kSize ? ptr + kSize : ptr, Clamp(n - kSize, 0, kSize)).raw;
235 auto const x2 = Load(n > 2 * kSize ? ptr + 2 * kSize : ptr, Clamp(n - 2 * kSize, 0, kSize)).raw;
236 out0.raw = DeinterleaveComponent<0>(x0, x1, x2);
237 out1.raw = DeinterleaveComponent<1>(x0, x1, x2);
238 out2.raw = DeinterleaveComponent<2>(x0, x1, x2);
239 }
240
241 template <int N = kSize>
242 static MOCHI_FORCE_INLINE void Store([[maybe_unused]] Scalar* ptr, [[maybe_unused]] Simd v) {
243 static_assert(N >= 0 && N <= kSize);
244 if constexpr (N == 0) {
245 } else if constexpr (N == 1) {
246 _mm_store_ss(ptr, _mm512_castps512_ps128(v.raw));
247 } else if constexpr (N == 2) {
248 _mm_storel_epi64(
249 reinterpret_cast<__m128i*>(ptr), _mm_castps_si128(_mm512_castps512_ps128(v.raw)));
250 } else if constexpr (N == 3) {
251 _mm_mask_storeu_ps(
252 ptr, static_cast<__mmask8>((uint32_t{1} << N) - 1), _mm512_castps512_ps128(v.raw));
253 } else if constexpr (N == 4) {
254 _mm_storeu_ps(ptr, _mm512_castps512_ps128(v.raw));
255 } else if constexpr (N < 8) {
256 _mm256_mask_storeu_ps(
257 ptr, static_cast<__mmask8>((uint32_t{1} << N) - 1), _mm512_castps512_ps256(v.raw));
258 } else if constexpr (N == 8) {
259 _mm256_storeu_ps(ptr, _mm512_castps512_ps256(v.raw));
260 } else if constexpr (N < kSize) {
261 _mm512_mask_storeu_ps(ptr, LaneMask<N>(), v.raw); // AVX512F
262 } else {
263 _mm512_storeu_ps(ptr, v.raw); // AVX512F
264 }
265 }
266
267 static MOCHI_FORCE_INLINE void Store(Scalar* ptr, Simd v, int n) {
268 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid size parameter");
269 _mm512_mask_storeu_ps(ptr, LaneMask(n), v.raw); // AVX512F
270 }
271
272 MOCHI_FORCE_INLINE static int StoreSelected(Scalar* ptr, Simd condition, Simd values) {
273 __mmask16 const mask = ToMask(condition);
274 _mm512_mask_compressstoreu_ps(ptr, mask, values.raw); // AVX512F
275 return _mm_popcnt_u32(mask);
276 }
277
278 template <int kTupleCount = kSize>
279 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c) {
280 static_assert(kTupleCount >= 1 && kTupleCount <= kSize, "Invalid kTupleCount");
281 if constexpr (kTupleCount == 1) {
282 Store<1>(ptr, a);
283 Store<1>(ptr + 1, b);
284 Store<1>(ptr + 2, c);
285 return;
286 }
287 constexpr int kTotalCount = kTupleCount * 3;
288 Simd x0, x1, x2;
289 Interleave3(a, b, c, x0, x1, x2);
291 if constexpr (kTotalCount > kSize) {
292 Store<Clamp(kTotalCount - kSize, 0, kSize)>(ptr + kSize, x1);
293 }
294 if constexpr (kTotalCount > 2 * kSize) {
295 Store<kTotalCount - 2 * kSize>(ptr + 2 * kSize, x2);
296 }
297 }
298
299 MOCHI_FORCE_INLINE static void StoreTransposed(Scalar* ptr, Simd a, Simd b, Simd c, int count) {
300 MOCHI_ASSERT_VERBOSE(count >= 0 && count <= kSize, "Invalid tuple count");
301 Simd x0, x1, x2;
302 Interleave3(a, b, c, x0, x1, x2);
303 // Bit i selects scalar i. Masks come from shifts, not clamped counts, which clang may turn
304 // back into branches. A zero mask stores nothing.
305 uint64_t const bits = (uint64_t{1} << (count * 3)) - 1;
306 _mm512_mask_storeu_ps(ptr, static_cast<__mmask16>(bits), x0.raw); // AVX512F
307 _mm512_mask_storeu_ps(ptr + (count > 5 ? 16 : 0), static_cast<__mmask16>(bits >> 16), x1.raw);
308 _mm512_mask_storeu_ps(ptr + (count > 10 ? 32 : 0), static_cast<__mmask16>(bits >> 32), x2.raw);
309 }
310
311 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Select(Simd mask, Simd a, Simd b) {
312 return _mm512_mask_blend_ps(ToMask(mask), b.raw, a.raw); // AVX512F
313 }
314
315 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sqrt(Simd v) {
316 return _mm512_sqrt_ps(v.raw); // AVX512F
317 }
318
319 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpApprox(Simd v) {
320 return _mm512_rcp14_ps(v.raw); // AVX512F
321 }
322
323 [[nodiscard]] static MOCHI_FORCE_INLINE Simd RcpSqrtApprox(Simd v) {
324 return _mm512_rsqrt14_ps(v.raw); // AVX512F
325 }
326
327 [[nodiscard]] static MOCHI_FORCE_INLINE Simd SignBitMask() {
328 return _mm512_castsi512_ps(_mm512_set1_epi32(static_cast<int>(0x80000000u))); // AVX512F
329 }
330
331 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Abs(Simd v) {
332 return _mm512_abs_ps(v.raw); // AVX512F
333 }
334
335 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Min(Simd a, Simd b) {
336 return _mm512_min_ps(a.raw, b.raw); // AVX512F
337 }
338
339 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Max(Simd a, Simd b) {
340 return _mm512_max_ps(a.raw, b.raw); // AVX512F
341 }
342
343 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Floor(Simd a) {
344 return _mm512_floor_ps(a.raw); // AVX512F
345 }
346
347 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FastRound(Simd v) {
348 return _mm512_roundscale_ps(v.raw, _MM_FROUND_TO_NEAREST_INT); // AVX512F
349 }
350
351#if MOCHI_ARCH_X64_SVML
352 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Cos(Simd a) {
353 return _mm512_cos_ps(a.raw);
354 }
355
356 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Sin(Simd a) {
357 return _mm512_sin_ps(a.raw);
358 }
359
360 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tan(Simd a) {
361 return _mm512_tan_ps(a.raw);
362 }
363
364 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ACos(Simd a) {
365 return _mm512_acos_ps(a.raw);
366 }
367
368 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ASin(Simd a) {
369 return _mm512_asin_ps(a.raw);
370 }
371
372 [[nodiscard]] static MOCHI_FORCE_INLINE Simd ATan(Simd a) {
373 return _mm512_atan_ps(a.raw);
374 }
375
376 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Exp(Simd a) {
377 return _mm512_exp_ps(a.raw);
378 }
379
380 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Ln(Simd a) {
381 return _mm512_log_ps(a.raw);
382 }
383
384 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Tanh(Simd a) {
385 return _mm512_tanh_ps(a.raw);
386 }
387#endif // MOCHI_ARCH_X64_SVML
388
389 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulAdd(Simd a, Simd b, Simd c) {
390 return _mm512_fmadd_ps(a.raw, b.raw, c.raw); // AVX512F
391 }
392
393 [[nodiscard]] static MOCHI_FORCE_INLINE Simd MulSub(Simd a, Simd b, Simd c) {
394 return _mm512_fmsub_ps(a.raw, b.raw, c.raw); // AVX512F
395 }
396
397 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulAdd(Simd a, Simd b, Simd c) {
398 return _mm512_fnmadd_ps(a.raw, b.raw, c.raw); // AVX512F
399 }
400
401 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NegMulSub(Simd a, Simd b, Simd c) {
402 return _mm512_fnmsub_ps(a.raw, b.raw, c.raw); // AVX512F
403 }
404
405 template <int N = kSize>
406 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMin(Simd a) {
407 static_assert(N >= 2 && N <= kSize, "Unsupported N");
408 using HalfT = Simd<Scalar, 8>;
409 auto const lo = GetHalf<0>(a);
410 if constexpr (N <= 8) {
411 return HalfT::template HMin<N>(lo);
412 } else {
413 auto const hi = GetHalf<1>(a);
414 if constexpr (N == 9) {
415 return superdex::Min(HalfT::template HMin<8>(lo), HalfT::template Get<0>(hi));
416 } else if constexpr (N == kSize) {
417 return HalfT::template HMin<8>(HalfT::Min(lo, hi));
418 } else {
419 return _mm512_mask_reduce_min_ps(LaneMask<N>(), a.raw);
420 }
421 }
422 }
423
424 template <int N = kSize>
425 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HMax(Simd a) {
426 static_assert(N >= 2 && N <= kSize, "Unsupported N");
427 using HalfT = Simd<Scalar, 8>;
428 auto const lo = GetHalf<0>(a);
429 if constexpr (N <= 8) {
430 return HalfT::template HMax<N>(lo);
431 } else {
432 auto const hi = GetHalf<1>(a);
433 if constexpr (N == 9) {
434 return superdex::Max(HalfT::template HMax<8>(lo), HalfT::template Get<0>(hi));
435 } else if constexpr (N == kSize) {
436 return HalfT::template HMax<8>(HalfT::Max(lo, hi));
437 } else {
438 return _mm512_mask_reduce_max_ps(LaneMask<N>(), a.raw);
439 }
440 }
441 }
442
443 template <int N>
444 [[nodiscard]] static MOCHI_FORCE_INLINE Scalar HSum(Simd a) {
445 static_assert(N >= 2 && N <= kSize, "Unsupported N");
446 using HalfT = Simd<Scalar, 8>;
447 auto const lo = GetHalf<0>(a);
448 if constexpr (N <= 8) {
449 return HalfT::template HSum<N>(lo);
450 } else {
451 auto const hi = GetHalf<1>(a);
452 if constexpr (N == 9) {
453 return HalfT::template HSum<8>(lo) + HalfT::template Get<0>(hi);
454 } else if constexpr (N == kSize) {
455 return HalfT::template HSum<8>(lo + hi);
456 } else {
457 return _mm512_mask_reduce_add_ps(LaneMask<N>(), a.raw);
458 }
459 }
460 }
461
462 template <int N>
463 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Dot(Simd a, Simd b) {
464 static_assert(N >= 2 && N <= kSize, "Unsupported N");
465 return Simd{HSum<N>(a * b)};
466 }
467
468 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<(Simd rhs) const {
469 return FromMask(_mm512_cmp_ps_mask(raw, rhs.raw, _CMP_LT_OQ)); // AVX512F
470 }
471
472 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>(Simd rhs) const {
473 return FromMask(_mm512_cmp_ps_mask(raw, rhs.raw, _CMP_GT_OQ)); // AVX512F
474 }
475
476 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator<=(Simd rhs) const {
477 return FromMask(_mm512_cmp_ps_mask(raw, rhs.raw, _CMP_LE_OQ)); // AVX512F
478 }
479
480 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator>=(Simd rhs) const {
481 return FromMask(_mm512_cmp_ps_mask(raw, rhs.raw, _CMP_GE_OQ)); // AVX512F
482 }
483
484 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Equal(Simd a, Simd b) {
485 return FromMask(_mm512_cmp_ps_mask(a.raw, b.raw, _CMP_EQ_OQ)); // AVX512F
486 }
487
488 [[nodiscard]] static MOCHI_FORCE_INLINE Simd NotEqual(Simd a, Simd b) {
489 return FromMask(_mm512_cmp_ps_mask(a.raw, b.raw, _CMP_NEQ_UQ)); // AVX512F
490 }
491
492 [[nodiscard]] static MOCHI_FORCE_INLINE Simd Zero() {
493 return _mm512_setzero_ps(); // AVX512F
494 }
495
496 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Simd rhs) const {
497 return _mm512_cmp_ps_mask(raw, rhs.raw, _CMP_EQ_OQ) == static_cast<__mmask16>(0xFFFFu);
498 }
499
500 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Simd rhs) const {
501 return _mm512_cmp_ps_mask(raw, rhs.raw, _CMP_NEQ_UQ) != 0;
502 }
503
504 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator~() const {
505 return _mm512_castsi512_ps(
506 _mm512_xor_si512(_mm512_castps_si512(raw), _mm512_set1_epi32(-1))); // AVX512F
507 }
508
509 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-() const {
510 return _mm512_xor_ps(raw, SignBitMask().raw); // AVX512DQ
511 }
512
513 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator+(Simd rhs) const {
514 return _mm512_add_ps(raw, rhs.raw); // AVX512F
515 }
516
517 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator-(Simd rhs) const {
518 return _mm512_sub_ps(raw, rhs.raw); // AVX512F
519 }
520
521 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator*(Simd rhs) const {
522 return _mm512_mul_ps(raw, rhs.raw); // AVX512F
523 }
524
525 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator/(Simd rhs) const {
526 return _mm512_div_ps(raw, rhs.raw); // AVX512F
527 }
528
529 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator&(Simd rhs) const {
530 return _mm512_and_ps(raw, rhs.raw); // AVX512DQ
531 }
532
533 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator|(Simd rhs) const {
534 return _mm512_or_ps(raw, rhs.raw); // AVX512DQ
535 }
536
537 [[nodiscard]] MOCHI_FORCE_INLINE Simd operator^(Simd rhs) const {
538 return _mm512_xor_ps(raw, rhs.raw); // AVX512DQ
539 }
540
541 private:
542 template <int kTupleCount, int kComponent>
543 [[nodiscard]] static MOCHI_FORCE_INLINE __m512i LoadTransposeIndices() {
544 constexpr int kZeroIndex = kTupleCount * 3;
545 return _mm512_setr_epi32(
546 kComponent,
547 kTupleCount > 1 ? 3 + kComponent : kZeroIndex,
548 kTupleCount > 2 ? 6 + kComponent : kZeroIndex,
549 kTupleCount > 3 ? 9 + kComponent : kZeroIndex,
550 kTupleCount > 4 ? 12 + kComponent : kZeroIndex,
551 kTupleCount > 5 ? 15 + kComponent : kZeroIndex,
552 kTupleCount > 6 ? 18 + kComponent : kZeroIndex,
553 kTupleCount > 7 ? 21 + kComponent : kZeroIndex,
554 kTupleCount > 8 ? 24 + kComponent : kZeroIndex,
555 kTupleCount > 9 ? 27 + kComponent : kZeroIndex,
556 kTupleCount > 10 ? 30 + kComponent : kZeroIndex,
557 kTupleCount > 11 ? 33 + kComponent : kZeroIndex,
558 kTupleCount > 12 ? 36 + kComponent : kZeroIndex,
559 kTupleCount > 13 ? 39 + kComponent : kZeroIndex,
560 kTupleCount > 14 ? 42 + kComponent : kZeroIndex,
561 kTupleCount > 15 ? 45 + kComponent : kZeroIndex);
562 }
563
564 // Returns every third scalar, starting at kComponent, of the 3 * kSize consecutive scalars
565 // {x0, x1, x2}.
566 template <int kComponent>
567 [[nodiscard]] static MOCHI_FORCE_INLINE __m512
568 DeinterleaveComponent(__m512 x0, __m512 x1, __m512 x2) {
569 auto const index = LoadTransposeIndices<kSize, kComponent>();
570 // Lanes whose index is at least 2 * kSize read from x2. permutexvar uses the index modulo
571 // kSize.
572 constexpr auto kLanesFromX2 =
573 static_cast<__mmask16>(~LaneMask<(2 * kSize - kComponent + 2) / 3>());
574 auto const fromX0X1 = _mm512_permutex2var_ps(x0, index, x1); // AVX512F
575 return _mm512_mask_permutexvar_ps(fromX0X1, kLanesFromX2, index, x2); // AVX512F
576 }
577
578 // Inverse of DeinterleaveComponent<0..2>: out0, out1, out2 are the 48 interleaved scalars in
579 // memory order.
580 MOCHI_FORCE_INLINE static void
581 Interleave3(Simd a, Simd b, Simd c, Simd& out0, Simd& out1, Simd& out2) {
582 auto const ab0 = _mm512_permutex2var_ps(
583 a.raw, _mm512_setr_epi32(0, 16, 0, 1, 17, 0, 2, 18, 0, 3, 19, 0, 4, 20, 0, 5), b.raw);
584 out0.raw = _mm512_permutex2var_ps(
585 ab0, _mm512_setr_epi32(0, 1, 16, 3, 4, 17, 6, 7, 18, 9, 10, 19, 12, 13, 20, 15), c.raw);
586 auto const ab1 = _mm512_permutex2var_ps(
587 a.raw, _mm512_setr_epi32(21, 0, 6, 22, 0, 7, 23, 0, 8, 24, 0, 9, 25, 0, 10, 26), b.raw);
588 out1.raw = _mm512_permutex2var_ps(
589 ab1, _mm512_setr_epi32(0, 21, 2, 3, 22, 5, 6, 23, 8, 9, 24, 11, 12, 25, 14, 15), c.raw);
590 auto const ab2 = _mm512_permutex2var_ps(
591 a.raw, _mm512_setr_epi32(0, 11, 27, 0, 12, 28, 0, 13, 29, 0, 14, 30, 0, 15, 31, 0), b.raw);
592 out2.raw = _mm512_permutex2var_ps(
593 ab2, _mm512_setr_epi32(26, 1, 2, 27, 4, 5, 28, 7, 8, 29, 10, 11, 30, 13, 14, 31), c.raw);
594 }
595
596 // Returns a mask selecting the lowest N lanes.
597 template <int N>
598 [[nodiscard]] static constexpr __mmask16 LaneMask() {
599 static_assert(N >= 0 && N <= kSize);
600 return static_cast<__mmask16>((uint32_t{1} << N) - 1);
601 }
602
603 // Returns a mask selecting the lowest n lanes.
604 [[nodiscard]] static constexpr __mmask16 LaneMask(int n) {
605 MOCHI_ASSERT_VERBOSE(n >= 0 && n <= kSize, "Invalid lane count");
606 return static_cast<__mmask16>((uint32_t{1} << n) - 1);
607 }
608
609 // Converts a canonical logical vector (all-zero or all-one lanes) to a mask.
610 [[nodiscard]] static MOCHI_FORCE_INLINE __mmask16 ToMask(Simd a) {
611 auto const bits = _mm512_castps_si512(a.raw);
612 auto const mask = _mm512_movepi32_mask(bits); // AVX512DQ
614 _mm512_cmpeq_epi32_mask(bits, _mm512_movm_epi32(mask)) == LaneMask<kSize>(),
615 "Expected a canonical logical mask");
616 return mask;
617 }
618
619 // Expands a mask into a canonical logical vector (all-zero or all-one lanes).
620 [[nodiscard]] static MOCHI_FORCE_INLINE Simd FromMask(__mmask16 mask) {
621 return _mm512_castsi512_ps(_mm512_movm_epi32(mask)); // AVX512DQ
622 }
623};
624
625} // namespace superdex
626
627#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)
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
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)