21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX512
29class Simd<float, 16> {
49 :
raw(_mm512_set_ps(p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a)) {}
51 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
52 Simd(U a) :
raw(_mm512_set1_ps(a)) {}
55 :
raw(_mm512_insertf32x8(_mm512_castps256_ps512(low.
raw), high.
raw, 1)) {}
59 static_assert(i >= 0 && i <
kSize,
"Index out of range");
65#if MOCHI_COMPILER_MSVC
66 return raw.m512_f32[i];
74 static_assert(iHalf == 0 || iHalf == 1);
75 if constexpr (iHalf == 0) {
76 return _mm512_castps512_ps256(a.raw);
78 return _mm512_extractf32x8_ps(a.raw, 1);
84 auto const mask =
static_cast<__mmask16
>(1u << i);
85 return _mm512_mask_broadcastss_ps(v.raw, mask, _mm_set_ss(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));
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;
102 constexpr auto kLanes = LaneMask<N>();
103 return (mask & kLanes) == kLanes;
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;
114 return (mask & LaneMask<N>()) != 0;
119 return _mm512_set1_ps(*p);
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));
128 constexpr int kLane = i % 4;
129 constexpr int kGroup = i / 4;
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));
136 template <
int N = kSize>
138 static_assert(N >= 0 && N <=
kSize);
139 if constexpr (N == 0) {
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);
159 return _mm512_loadu_ps(ptr);
165 return _mm512_maskz_loadu_ps(LaneMask(n), ptr);
169 return _mm512_i32gather_ps(indices.raw, ptr,
sizeof(
float));
172 template <
int kTupleCount = kSize>
175 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
176 if constexpr (kTupleCount == 1) {
182 constexpr int kTotalCount = kTupleCount * 3;
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);
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);
201 constexpr int kCount2 = kTotalCount - 2 *
kSize;
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);
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);
232 int const n = count * 3;
236 out0.raw = DeinterleaveComponent<0>(x0, x1, x2);
237 out1.raw = DeinterleaveComponent<1>(x0, x1, x2);
238 out2.raw = DeinterleaveComponent<2>(x0, x1, x2);
241 template <
int N = kSize>
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) {
249 reinterpret_cast<__m128i*
>(ptr), _mm_castps_si128(_mm512_castps512_ps128(v.raw)));
250 }
else if constexpr (N == 3) {
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);
263 _mm512_storeu_ps(ptr, v.raw);
269 _mm512_mask_storeu_ps(ptr, LaneMask(n), v.raw);
273 __mmask16
const mask = ToMask(condition);
274 _mm512_mask_compressstoreu_ps(ptr, mask, values.raw);
275 return _mm_popcnt_u32(mask);
278 template <
int kTupleCount = kSize>
280 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
281 if constexpr (kTupleCount == 1) {
287 constexpr int kTotalCount = kTupleCount * 3;
289 Interleave3(a, b, c, x0, x1, x2);
291 if constexpr (kTotalCount >
kSize) {
294 if constexpr (kTotalCount > 2 *
kSize) {
302 Interleave3(a, b, c, x0, x1, x2);
305 uint64_t
const bits = (uint64_t{1} << (count * 3)) - 1;
306 _mm512_mask_storeu_ps(ptr,
static_cast<__mmask16
>(bits), x0.raw);
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);
312 return _mm512_mask_blend_ps(ToMask(mask), b.raw, a.raw);
316 return _mm512_sqrt_ps(v.raw);
320 return _mm512_rcp14_ps(v.raw);
324 return _mm512_rsqrt14_ps(v.raw);
328 return _mm512_castsi512_ps(_mm512_set1_epi32(
static_cast<int>(0x80000000u)));
332 return _mm512_abs_ps(v.raw);
336 return _mm512_min_ps(a.raw, b.raw);
340 return _mm512_max_ps(a.raw, b.raw);
344 return _mm512_floor_ps(a.raw);
348 return _mm512_roundscale_ps(v.raw, _MM_FROUND_TO_NEAREST_INT);
351#if MOCHI_ARCH_X64_SVML
353 return _mm512_cos_ps(a.raw);
357 return _mm512_sin_ps(a.raw);
361 return _mm512_tan_ps(a.raw);
365 return _mm512_acos_ps(a.raw);
369 return _mm512_asin_ps(a.raw);
373 return _mm512_atan_ps(a.raw);
377 return _mm512_exp_ps(a.raw);
381 return _mm512_log_ps(a.raw);
385 return _mm512_tanh_ps(a.raw);
390 return _mm512_fmadd_ps(a.raw, b.raw, c.raw);
394 return _mm512_fmsub_ps(a.raw, b.raw, c.raw);
398 return _mm512_fnmadd_ps(a.raw, b.raw, c.raw);
402 return _mm512_fnmsub_ps(a.raw, b.raw, c.raw);
405 template <
int N = kSize>
407 static_assert(N >= 2 && N <=
kSize,
"Unsupported N");
410 if constexpr (N <= 8) {
411 return HalfT::template
HMin<N>(lo);
414 if constexpr (N == 9) {
416 }
else if constexpr (N ==
kSize) {
417 return HalfT::template
HMin<8>(HalfT::Min(lo, hi));
419 return _mm512_mask_reduce_min_ps(LaneMask<N>(), a.raw);
424 template <
int N = kSize>
426 static_assert(N >= 2 && N <=
kSize,
"Unsupported N");
429 if constexpr (N <= 8) {
430 return HalfT::template
HMax<N>(lo);
433 if constexpr (N == 9) {
435 }
else if constexpr (N ==
kSize) {
436 return HalfT::template
HMax<8>(HalfT::Max(lo, hi));
438 return _mm512_mask_reduce_max_ps(LaneMask<N>(), a.raw);
445 static_assert(N >= 2 && N <=
kSize,
"Unsupported N");
448 if constexpr (N <= 8) {
449 return HalfT::template
HSum<N>(lo);
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);
457 return _mm512_mask_reduce_add_ps(LaneMask<N>(), a.raw);
464 static_assert(N >= 2 && N <=
kSize,
"Unsupported N");
469 return FromMask(_mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_LT_OQ));
473 return FromMask(_mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_GT_OQ));
477 return FromMask(_mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_LE_OQ));
481 return FromMask(_mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_GE_OQ));
485 return FromMask(_mm512_cmp_ps_mask(a.raw, b.raw, _CMP_EQ_OQ));
489 return FromMask(_mm512_cmp_ps_mask(a.raw, b.raw, _CMP_NEQ_UQ));
493 return _mm512_setzero_ps();
497 return _mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_EQ_OQ) ==
static_cast<__mmask16
>(0xFFFFu);
501 return _mm512_cmp_ps_mask(
raw, rhs.raw, _CMP_NEQ_UQ) != 0;
505 return _mm512_castsi512_ps(
506 _mm512_xor_si512(_mm512_castps_si512(
raw), _mm512_set1_epi32(-1)));
510 return _mm512_xor_ps(
raw, SignBitMask().
raw);
514 return _mm512_add_ps(
raw, rhs.raw);
518 return _mm512_sub_ps(
raw, rhs.raw);
522 return _mm512_mul_ps(
raw, rhs.raw);
526 return _mm512_div_ps(
raw, rhs.raw);
530 return _mm512_and_ps(
raw, rhs.raw);
534 return _mm512_or_ps(
raw, rhs.raw);
538 return _mm512_xor_ps(
raw, rhs.raw);
542 template <
int kTupleCount,
int kComponent>
544 constexpr int kZeroIndex = kTupleCount * 3;
545 return _mm512_setr_epi32(
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);
566 template <
int kComponent>
568 DeinterleaveComponent(__m512 x0, __m512 x1, __m512 x2) {
569 auto const index = LoadTransposeIndices<kSize, kComponent>();
572 constexpr auto kLanesFromX2 =
573 static_cast<__mmask16
>(~LaneMask<(2 *
kSize - kComponent + 2) / 3>());
574 auto const fromX0X1 = _mm512_permutex2var_ps(x0, index, x1);
575 return _mm512_mask_permutexvar_ps(fromX0X1, kLanesFromX2, index, x2);
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);
598 [[nodiscard]]
static constexpr __mmask16 LaneMask() {
599 static_assert(N >= 0 && N <=
kSize);
600 return static_cast<__mmask16
>((uint32_t{1} << N) - 1);
604 [[nodiscard]]
static constexpr __mmask16 LaneMask(
int n) {
606 return static_cast<__mmask16
>((uint32_t{1} << n) - 1);
611 auto const bits = _mm512_castps_si512(a.raw);
612 auto const mask = _mm512_movepi32_mask(bits);
614 _mm512_cmpeq_epi32_mask(bits, _mm512_movm_epi32(mask)) == LaneMask<kSize>(),
615 "Expected a canonical logical mask");
621 return _mm512_castsi512_ps(_mm512_movm_epi32(mask));
Simd operator&(Simd rhs) const
bool operator==(Simd rhs) const
Simd operator>(Simd rhs) const
Simd operator*(Simd rhs) const
Simd operator^(Simd rhs) 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
Simd operator+(Simd rhs) const
Simd operator/(Simd rhs) const
Simd operator<=(Simd rhs) const
Scalar operator[](int i) const
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
T Dot(Simd< T, N > a, Simd< T, N > b)
constexpr T NegMulAdd(T a, T b, T c)
V LoadIndexed(typename V::Scalar const *ptr, Simd< I, V::kSize > indices)
constexpr T const & Min(T const &a, T const &b)
constexpr auto Equal(T const &a, T const &b)
constexpr T NegMulSub(T a, T b, T c)
Simd< T, N > Tanh(Simd< T, N > a)
Simd< T, N > Set(Simd< T, N > a, T value)
constexpr auto NotEqual(T const &a, T const &b)
V Broadcast(typename V::Scalar a)
constexpr T MulSub(T a, T b, T c)
constexpr T Select(bool condition, T a, T b)
Simd< T, N > Ln(Simd< T, N > a)
constexpr ValT Clamp(ValT value, MinT min, MaxT max)
Simd< T, N/2 > GetHalf(Simd< T, N > a)
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)
Simd< T, N > FastRound(Simd< T, N > a)
void StoreTransposed(T *ptr, Simd< T, N > a, Simd< T, N > b, Simd< T, N > c)
void Store(T *ptr, Simd< T, N > a)
Simd< T, N > RcpSqrtApprox(Simd< T, N > a)
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)
V Load(typename V::Scalar const *ptr)
#define MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(T, N, NativeT)