21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
29class Simd<double, 4> {
32 Simd(
double a,
double b,
double c = 0.0,
double d = 0.0)
33 :
raw(_mm256_set_pd(d, c, b, a)) {}
34 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
35 Simd(U a) :
raw(_mm256_set1_pd(a)) {}
39 :
raw(_mm256_set_m128d(high.
raw, low.
raw)) {}
43 static_assert(i >= 0 && i < 4,
"Index out of range");
49#if MOCHI_COMPILER_MSVC
50 return raw.m256d_f64[i];
58#if MOCHI_COMPILER_MSVC
60 result.raw.m256d_f64[i] = value;
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]));
71 static_assert(i >= 0 && i <
kSize,
"Index out of range");
72 return Set(v, i, value);
77 static_assert(iHalf == 0 || iHalf == 1);
78 return _mm256_extractf128_pd(a.raw, iHalf);
83 auto araw = _mm256_castpd_si256(a.raw);
84 auto v = _mm256_insert_epi64(araw, 0x3FF0000000000000LL, 3);
85 return _mm256_castsi256_pd(v);
90 auto araw = _mm256_castpd_si256(a.raw);
91 auto v = _mm256_insert_epi64(araw, 0, 3);
92 return _mm256_castsi256_pd(v);
95 template <
int x,
int y,
int z,
int w>
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) {
102 }
else if constexpr (x == 1 && y == 1 && z == 1 && w == 1) {
105 return _mm256_blend_pd(a.raw, b.raw, x | (y << 1) | (z << 2) | (w << 3));
111 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
112 int constexpr kLanes = (1 << N) - 1;
113 return (ToMask(v) & kLanes) == kLanes;
118 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
119 int constexpr kLanes = (1 << N) - 1;
120 return (ToMask(v) & kLanes) != 0;
124 return _mm256_broadcast_sd(p);
132 template <
int N = kSize>
134 static_assert(N >= 0 && N <= 4);
135 if constexpr (N == 0) {
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));
141 }
else if constexpr (N == 3) {
142 __m256i mask = _mm256_set_epi64x(0, -1, -1, -1);
143 return _mm256_maskload_pd(ptr, mask);
145 return _mm256_loadu_pd(ptr);
149#if !MOCHI_ARCH_X64_AVX512
150#if MOCHI_COMPILER_MSVC
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}};
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}};
173#if MOCHI_ARCH_X64_AVX512
174 return _mm256_maskz_loadu_pd(x64_simd::kLaneMasksS8[n], ptr);
176 return _mm256_maskload_pd(ptr, kLoadMasks[n]);
181 return _mm256_i32gather_pd(ptr, indices.raw,
sizeof(
double));
185 return _mm256_i64gather_pd(ptr, indices.raw,
sizeof(
double));
188 template <
int kTupleCount = kSize>
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;
196 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 4).raw;
197 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 8).raw;
198 Deinterleave3(a, b, c, out0, out1, out2);
204 int const n = count * 3;
206 auto b = Simd::Load(n > 4 ? ptr + 4 : ptr,
Clamp(n - 4, 0, 4)).raw;
207 auto c = Simd::Load(n > 8 ? ptr + 8 : ptr,
Clamp(n - 8, 0, 4)).raw;
208 Deinterleave3(a, b, c, out0, out1, out2);
213 static_assert(i >= 0 && i <= 3,
"Invalid component index");
214 auto zero = _mm256_setzero_si256();
215 auto v = _mm256_insert_epi64(zero, 0x3FF0000000000000LL, i);
216 return _mm256_castsi256_pd(v);
219 template <
int N = kSize>
221 static_assert(N >= 0 && N <=
kSize);
227 if constexpr (N == 0) {
228 }
else if constexpr (N <= 2) {
229 HalfT::Store<N>(ptr, HalfT(_mm256_castpd256_pd128(v.raw)));
230 }
else if constexpr (N <
kSize) {
231 HalfT::Store<N - 2>(ptr + 2, HalfT(_mm256_extractf128_pd(v.raw, 1)));
232 HalfT::Store<2>(ptr, HalfT(_mm256_castpd256_pd128(v.raw)));
234 _mm256_storeu_pd(ptr, v.raw);
240#if MOCHI_ARCH_X64_AVX512
241 _mm256_mask_storeu_pd(ptr, x64_simd::kLaneMasksS8[n], v.raw);
254 auto mask = ToMask(condition);
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);
264 template <
int kTupleCount = kSize>
266 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
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);
276 if constexpr (kCount2 > 0) {
277 Simd::Store<kCount2>(ptr + 8, x2);
284 Interleave3(a, b, c, x0, x1, x2);
285#if MOCHI_ARCH_X64_AVX512
288 uint32_t
const bits = (1u << (count * 3)) - 1;
289 _mm256_mask_storeu_pd(ptr,
static_cast<__mmask8
>(bits), x0.raw);
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);
295 auto const n = _mm256_set1_epi64x(count * 3);
296 auto const mask0 = _mm256_cmpgt_epi64(n, _mm256_setr_epi64x(0, 1, 2, 3));
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);
300 _mm256_maskstore_pd(ptr + (count > 1 ? 4 : 0), mask1, x1.raw);
301 _mm256_maskstore_pd(ptr + (count > 2 ? 8 : 0), mask2, x2.raw);
306 return _mm256_blendv_pd(b.raw, a.raw, mask.raw);
310 template <
int x = 0,
int y = 1,
int z = 2,
int w = 3>
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) {
319 return _mm256_permute4x64_pd(v.raw, x | (y << 2) | (z << 4) | (w << 6));
324 template <
int x = 0,
int y = 1,
int z = 2,
int w = 3>
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");
334 return _mm256_sqrt_pd(v.raw);
338 return Simd{1.0} / v;
347 return _mm256_castsi256_pd(_mm256_set1_epi64x(0x8000000000000000LL));
351 return _mm256_andnot_pd(SignBitMask().
raw, v.raw);
355 return _mm256_min_pd(a.raw, b.raw);
359 return _mm256_max_pd(a.raw, b.raw);
363 return _mm256_floor_pd(a.raw);
367 return _mm256_round_pd(v.raw, _MM_FROUND_TO_NEAREST_INT);
370#if MOCHI_ARCH_X64_SVML
372 return _mm256_cos_pd(a.raw);
376 return _mm256_sin_pd(a.raw);
380 return _mm256_tan_pd(a.raw);
384 return _mm256_acos_pd(a.raw);
388 return _mm256_asin_pd(a.raw);
392 return _mm256_atan_pd(a.raw);
396 return _mm256_exp_pd(a.raw);
400 return _mm256_log_pd(a.raw);
404 return _mm256_tanh_pd(a.raw);
409#if MOCHI_ARCH_X64_FMA
410 return {_mm256_fmadd_pd(a.raw, b.raw, c.raw)};
417#if MOCHI_ARCH_X64_FMA
418 return _mm256_fmsub_pd(a.raw, b.raw, c.raw);
425#if MOCHI_ARCH_X64_FMA
426 return _mm256_fnmadd_pd(a.raw, b.raw, c.raw);
433#if MOCHI_ARCH_X64_FMA
434 return _mm256_fnmsub_pd(a.raw, b.raw, c.raw);
442 static_assert(N >= 2 && N <= 4,
"Unsupported N");
444 if constexpr (N == 2) {
446 }
else if constexpr (N == 3) {
449 return HalfT::Get<0>(HalfT::Min(HalfT::Min(lo, HalfT::Broadcast<1>(lo)), hi));
457 static_assert(N >= 2 && N <= 4,
"Unsupported N");
459 if constexpr (N == 2) {
461 }
else if constexpr (N == 3) {
464 return HalfT::Get<0>(HalfT::Max(HalfT::Max(lo, HalfT::Broadcast<1>(lo)), hi));
472 static_assert(N >= 2 && N <= 4,
"Unsupported N");
473 if constexpr (N == 2) {
475 }
else if constexpr (N == 3) {
478 return HalfT::Get<0>(tmp) +
Get<1>(a);
479 }
else if constexpr (N == 4) {
485 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp);
491 auto const ac = _mm256_add_pd(
492 _mm256_blend_pd(a.raw, c.raw, 0b1100), _mm256_permute2f128_pd(a.raw, c.raw, 0x21));
493 auto const bd = _mm256_add_pd(
494 _mm256_blend_pd(b.raw, d.raw, 0b1100), _mm256_permute2f128_pd(b.raw, d.raw, 0x21));
496 return _mm256_add_pd(_mm256_unpacklo_pd(ac, bd), _mm256_unpackhi_pd(ac, bd));
501 static_assert(N >= 2 && N <= 4,
"Unsupported N");
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];
515 static_assert(N >= 2 && N <= 4,
"Unsupported N");
520 return _mm256_cmp_pd(this->
raw, rhs.raw, _CMP_LT_OQ);
524 return _mm256_cmp_pd(this->
raw, rhs.raw, _CMP_GT_OQ);
528 return _mm256_cmp_pd(this->
raw, rhs.raw, _CMP_LE_OQ);
532 return _mm256_cmp_pd(this->
raw, rhs.raw, _CMP_GE_OQ);
536 return _mm256_cmp_pd(a.raw, b.raw, _CMP_EQ_OQ);
540 return _mm256_cmp_pd(a.raw, b.raw, _CMP_NEQ_UQ);
544 return _mm256_setzero_pd();
548 return ToMask(
Equal(*
this, rhs)) == 0xF;
552 return ToMask(
NotEqual(*
this, rhs)) != 0;
556 auto ones = _mm256_castsi256_pd(_mm256_set1_epi64x(-1));
557 return _mm256_xor_pd(
raw, ones);
561 return _mm256_xor_pd(
raw, SignBitMask().
raw);
565 return _mm256_add_pd(
raw, rhs.raw);
569 return _mm256_sub_pd(
raw, rhs.raw);
573 return _mm256_mul_pd(
raw, rhs.raw);
577 return _mm256_div_pd(
raw, rhs.raw);
581 return _mm256_and_pd(
raw, rhs.raw);
585 return _mm256_or_pd(
raw, rhs.raw);
589 return _mm256_xor_pd(
raw, rhs.raw);
595 return _mm256_movemask_pd(a.raw);
600 Deinterleave3(__m256d a, __m256d b, __m256d c,
Simd& out0,
Simd& out1,
Simd& out2) {
601 auto d = _mm256_blend_pd(a, b, 0b0100);
602 d = _mm256_blend_pd(d, c, 0b0010);
603 auto e = _mm256_permute2f128_pd(d, d, 0x01);
604 out0.raw = _mm256_blend_pd(d, e, 0b1010);
606 d = _mm256_blend_pd(a, b, 0b1001);
607 d = _mm256_blend_pd(d, c, 0b0100);
608 out1.raw = _mm256_shuffle_pd(d, d, 0b0101);
610 d = _mm256_blend_pd(a, b, 0b0010);
611 d = _mm256_blend_pd(d, c, 0b1001);
612 e = _mm256_permute2f128_pd(d, d, 0x01);
613 out2.raw = _mm256_blend_pd(d, e, 0b0101);
620 auto d = _mm256_shuffle_pd(b.raw, b.raw, 0b0101);
621 auto e = _mm256_blend_pd(a.raw, c.raw, 0b0101);
622 e = _mm256_permute2f128_pd(e, e, 0x01);
623 auto f = _mm256_blend_pd(a.raw, d, 0b1010);
624 auto g = _mm256_blend_pd(d, c.raw, 0b1010);
625 out0 = _mm256_blend_pd(f, e, 0b1100);
626 out1 = _mm256_blend_pd(g, f, 0b1100);
627 out2 = _mm256_blend_pd(e, g, 0b1100);
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)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
Simd< T, 2 > HSumEach(Simd< T, 2 > a, Simd< T, 2 > b)
Returns {HSum(a), HSum(b)}.
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)
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
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)