21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
32 Simd(
int a,
int b,
int c = 0,
int d = 0,
int e = 0,
int f = 0,
int g = 0,
int h = 0)
33 :
raw(_mm256_set_epi32(h, g, f, e, d, c, b, a)) {}
35 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
36 Simd(U a) :
raw(_mm256_set1_epi32(a)) {}
40 :
raw(_mm256_set_m128i(high.
raw, low.
raw)) {}
44 static_assert(i >= 0 && i < 8,
"Index out of range");
45 if constexpr (i == 0) {
46 return _mm256_cvtsi256_si32(v.raw);
47 }
else if constexpr (i == 1) {
48 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(v.raw, 0x01));
49 }
else if constexpr (i == 2) {
50 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(v.raw, 0x02));
51 }
else if constexpr (i == 3) {
52 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(v.raw, 0x03));
53 }
else if constexpr (i == 4) {
54 return _mm256_cvtsi256_si32(_mm256_permute2f128_si256(v.raw, v.raw, 0x01));
55 }
else if constexpr (i == 5) {
56 auto tmp = _mm256_permute2f128_si256(v.raw, v.raw, 0x01);
57 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(tmp, 0x01));
58 }
else if constexpr (i == 6) {
59 auto tmp = _mm256_permute2f128_si256(v.raw, v.raw, 0x01);
60 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(tmp, 0x02));
61 }
else if constexpr (i == 7) {
62 auto tmp = _mm256_permute2f128_si256(v.raw, v.raw, 0x01);
63 return _mm256_cvtsi256_si32(_mm256_shuffle_epi32(tmp, 0x03));
69#if MOCHI_COMPILER_MSVC
70 return raw.m256i_i32[i];
73 case 0:
return Get<0>(*
this);
74 case 1:
return Get<1>(*
this);
75 case 2:
return Get<2>(*
this);
76 case 3:
return Get<3>(*
this);
77 case 4:
return Get<4>(*
this);
78 case 5:
return Get<5>(*
this);
79 case 6:
return Get<6>(*
this);
80 case 7:
return Get<7>(*
this);
88 static_assert(iHalf == 0 || iHalf == 1);
89 return _mm256_extracti128_si256(a.raw, iHalf);
94#if MOCHI_COMPILER_MSVC
96 result.raw.m256i_i32[i] = value;
100 static constexpr __m256i kMasks[] = {
101 {
static_cast<long long>(0x00000000FFFFFFFFLL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL)},
102 {
static_cast<long long>(0xFFFFFFFF00000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL)},
103 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x00000000FFFFFFFFLL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL)},
104 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0xFFFFFFFF00000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL)},
105 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x00000000FFFFFFFFLL),
static_cast<long long>(0x0000000000000000LL)},
106 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0xFFFFFFFF00000000LL),
static_cast<long long>(0x0000000000000000LL)},
107 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x00000000FFFFFFFFLL)},
108 {
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0x0000000000000000LL),
static_cast<long long>(0xFFFFFFFF00000000LL)}};
110 return _mm256_blendv_epi8(v.raw, _mm256_set1_epi32(value), kMasks[i]);
116 static_assert(i >= 0 && i <
kSize,
"Index out of range");
117 return Set(v, i, value);
122 SetInt64(int64_t a, int64_t b, int64_t c, int64_t d) {
123 return _mm256_set_epi64x(d, c, b, a);
128 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
129 int constexpr kLanes = (1 << N) - 1;
130 return (ToMask(v) & kLanes) == kLanes;
135 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
136 int constexpr kLanes = (1 << N) - 1;
137 return (ToMask(v) & kLanes) != 0;
151 static_assert(N >= 2 && N <= 8,
"Unsupported N");
153 if constexpr (N >= 2 && N <= 4) {
158 if constexpr (N != 8) {
160 auto inf = HalfT{std::numeric_limits<Scalar>::max()};
161 hi = HalfT::Blend<1, N >= 6, N >= 7, 0>(inf, hi);
163 return HalfT::HMin<4>(HalfT::Min(lo, hi));
169 static_assert(N >= 2 && N <= 8,
"Unsupported N");
171 if constexpr (N >= 2 && N <= 4) {
176 if constexpr (N != 8) {
178 auto lowest = HalfT{std::numeric_limits<Scalar>::lowest()};
179 hi = HalfT::Blend<1, N >= 6, N >= 7, 0>(lowest, hi);
181 return HalfT::HMax<4>(HalfT::Max(lo, hi));
187 static_assert(N >= 2 && N <= 8,
"Unsupported N");
189 if constexpr (N >= 2 && N <= 4) {
191 }
else if constexpr (N == 5) {
194 }
else if constexpr (N == 6) {
196 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp) +
Get<2>(a) +
Get<3>(a);
197 }
else if constexpr (N == 7) {
199 return HalfT::Get<0>(tmp) + HalfT::Get<1>(tmp) + HalfT::Get<2>(tmp) +
Get<3>(a);
200 }
else if constexpr (N == 8) {
205 template <
int N = kSize>
207 static_assert(N >= 0 && N <=
kSize);
208 if constexpr (N == 0) {
210 }
else if constexpr (N == 1) {
211 return Simd{*ptr, 0};
212 }
else if constexpr (N == 2) {
213 auto const low = _mm_loadl_epi64(
reinterpret_cast<__m128i const*
>(ptr));
214 return _mm256_zextsi128_si256(low);
215 }
else if constexpr (N == 3) {
216 __m256i mask = _mm256_set_epi32(0, 0, 0, 0, 0, -1, -1, -1);
217 return _mm256_maskload_epi32(ptr, mask);
218 }
else if constexpr (N == 4) {
219 auto const low = _mm_loadu_si128(
reinterpret_cast<__m128i const*
>(ptr));
220 return _mm256_zextsi128_si256(low);
221 }
else if constexpr (N == 5) {
222 __m256i mask = _mm256_set_epi32(0, 0, 0, -1, -1, -1, -1, -1);
223 return _mm256_maskload_epi32(ptr, mask);
224 }
else if constexpr (N == 6) {
225 __m256i mask = _mm256_set_epi32(0, 0, -1, -1, -1, -1, -1, -1);
226 return _mm256_maskload_epi32(ptr, mask);
227 }
else if constexpr (N == 7) {
228 __m256i mask = _mm256_set_epi32(0, -1, -1, -1, -1, -1, -1, -1);
229 return _mm256_maskload_epi32(ptr, mask);
231 return _mm256_loadu_si256(
reinterpret_cast<__m256i const*
>(ptr));
237#if MOCHI_ARCH_X64_AVX512
238 return _mm256_maskz_loadu_epi32(x64_simd::kLaneMasksS8[n], ptr);
241 return _mm256_maskload_epi32(ptr, x64_simd::kLoadMasksS8[n]);
245 template <
int kTupleCount = kSize>
248 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
249 constexpr int kCount0 =
Clamp(kTupleCount * 3 - 0, 0, 8);
250 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 8, 0, 8);
251 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 16, 0, 8);
254 auto a = Simd::Load<kCount0>(ptr).raw;
256 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 8).raw;
258 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 16).raw;
259 Deinterleave3(a, b, c, out0, out1, out2);
265 int const n = count * 3;
267 auto b = Simd::Load(n > 8 ? ptr + 8 : ptr,
Clamp(n - 8, 0, 8)).raw;
268 auto c = Simd::Load(n > 16 ? ptr + 16 : ptr,
Clamp(n - 16, 0, 8)).raw;
269 Deinterleave3(a, b, c, out0, out1, out2);
272 template <
int N = kSize>
274 static_assert(N >= 0 && N <=
kSize);
280 if constexpr (N == 0) {
281 }
else if constexpr (N <= 4) {
282 HalfT::Store<N>(ptr, HalfT(_mm256_castsi256_si128(v.raw)));
283 }
else if constexpr (N <
kSize) {
284 HalfT::Store<N - 4>(ptr + 4, HalfT(_mm256_extracti128_si256(v.raw, 1)));
285 HalfT::Store<4>(ptr, HalfT(_mm256_castsi256_si128(v.raw)));
287 return _mm256_storeu_si256(
reinterpret_cast<__m256i*
>(ptr), v.raw);
293#if MOCHI_ARCH_X64_AVX512
294 _mm256_mask_storeu_epi32(ptr, x64_simd::kLaneMasksS8[n], v.raw);
313 auto mask = ToMask(condition);
315 auto const* tableRow =
316 reinterpret_cast<__m128i const*
>(x64_simd::kStoreSelectedShuffleTableS8[mask]);
317 auto pattern = _mm256_cvtepu8_epi32(_mm_loadl_epi64(tableRow));
318 auto packed = _mm256_permutevar8x32_ps(_mm256_castsi256_ps(values.raw), pattern);
319 _mm256_storeu_si256(
reinterpret_cast<__m256i*
>(ptr), _mm256_castps_si256(packed));
320 return _mm_popcnt_u32(mask);
323 template <
int kTupleCount = kSize>
325 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
327 Interleave3(a, b, c, x0, x1, x2);
328 constexpr int kCount0 =
Clamp(kTupleCount * 3 - 0, 0, 8);
329 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 8, 0, 8);
330 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 16, 0, 8);
331 Simd::Store<kCount0>(ptr, x0);
332 if constexpr (kCount1 > 0) {
333 Simd::Store<kCount1>(ptr + 8, x1);
335 if constexpr (kCount2 > 0) {
336 Simd::Store<kCount2>(ptr + 16, x2);
343 Interleave3(a, b, c, x0, x1, x2);
344#if MOCHI_ARCH_X64_AVX512
347 uint32_t
const bits = (1u << (count * 3)) - 1;
348 _mm256_mask_storeu_epi32(ptr,
static_cast<__mmask8
>(bits), x0.raw);
349 _mm256_mask_storeu_epi32(ptr + (count > 2 ? 8 : 0),
static_cast<__mmask8
>(bits >> 8), x1.raw);
350 _mm256_mask_storeu_epi32(ptr + (count > 5 ? 16 : 0),
static_cast<__mmask8
>(bits >> 16), x2.raw);
354 auto const n = _mm256_set1_epi32(count * 3);
355 auto const mask0 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(0, 1, 2, 3, 4, 5, 6, 7));
356 auto const mask1 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(8, 9, 10, 11, 12, 13, 14, 15));
357 auto const mask2 = _mm256_cmpgt_epi32(n, _mm256_setr_epi32(16, 17, 18, 19, 20, 21, 22, 23));
358 _mm256_maskstore_epi32(ptr, mask0, x0.raw);
359 _mm256_maskstore_epi32(ptr + (count > 2 ? 8 : 0), mask1, x1.raw);
360 _mm256_maskstore_epi32(ptr + (count > 5 ? 16 : 0), mask2, x2.raw);
365 return _mm256_min_epi32(a.raw, b.raw);
369 return _mm256_max_epi32(a.raw, b.raw);
373 return _mm256_blendv_epi8(b.raw, a.raw, mask.raw);
377 return _mm256_setzero_si256();
381 return _mm256_cmpgt_epi32(rhs.raw, this->raw);
385 return _mm256_cmpgt_epi32(this->
raw, rhs.raw);
389 return ~(*
this > rhs);
393 return ~(*
this < rhs);
397 return _mm256_cmpeq_epi32(a.raw, b.raw);
405 return ToMask(
Equal(*
this, rhs)) == 0xFF;
409 return ToMask(
NotEqual(*
this, rhs)) != 0;
413 auto ones = _mm256_set1_epi32(-1);
414 return _mm256_xor_si256(
raw, ones);
418 return Zero() - *
this;
422 return _mm256_add_epi32(
raw, rhs.raw);
426 return _mm256_sub_epi32(
raw, rhs.raw);
430 return _mm256_mullo_epi32(
raw, rhs.raw);
434#if MOCHI_ARCH_X64_SVML
435 return _mm256_div_epi32(
raw, rhs.raw);
451 return _mm256_and_si256(
raw, rhs.raw);
455 return _mm256_or_si256(
raw, rhs.raw);
459 return _mm256_xor_si256(
raw, rhs.raw);
463 return _mm256_slli_epi32(
raw, rhs);
466 template <
int kShift>
468 static_assert(kShift >= 0 && kShift < 32,
"Shift amount out-of-range");
469 return _mm256_srai_epi32(a.raw, kShift);
475 return _mm256_movemask_ps(_mm256_castsi256_ps(a.raw));
480 Deinterleave3(__m256i a, __m256i b, __m256i c,
Simd& out0,
Simd& out1,
Simd& out2) {
481 auto d = _mm256_blend_epi32(a, b, 0b10010010);
482 auto e = _mm256_blend_epi32(d, c, 0b00100100);
483 auto f = _mm256_permute2x128_si256(e, e, 0x01);
484 auto g = _mm256_blend_epi32(e, f, 0b01000100);
485 out0.raw = _mm256_shuffle_epi32(g, _MM_SHUFFLE(1, 2, 3, 0));
487 d = _mm256_blend_epi32(a, b, 0b00100100);
488 e = _mm256_blend_epi32(d, c, 0b01001001);
489 f = _mm256_permute2x128_si256(e, e, 0x01);
490 g = _mm256_blend_epi32(e, f, 0b10011001);
491 out1.raw = _mm256_shuffle_epi32(g, _MM_SHUFFLE(2, 3, 0, 1));
493 d = _mm256_blend_epi32(a, b, 0b01001001);
494 e = _mm256_blend_epi32(d, c, 0b10010010);
495 f = _mm256_permute2x128_si256(e, e, 0x01);
496 g = _mm256_blend_epi32(e, f, 0b00100010);
497 out2.raw = _mm256_shuffle_epi32(g, _MM_SHUFFLE(3, 0, 1, 2));
506 auto d = _mm256_shuffle_epi32(a.raw, _MM_SHUFFLE(1, 2, 3, 0));
507 auto e = _mm256_shuffle_epi32(b.raw, _MM_SHUFFLE(2, 3, 0, 1));
508 auto f = _mm256_shuffle_epi32(c.raw, _MM_SHUFFLE(3, 0, 1, 2));
509 __m256i g = _mm256_blend_epi32(d, e, 0b00100010);
510 g = _mm256_blend_epi32(g, f, 0b01000100);
511 __m256i h = _mm256_blend_epi32(d, e, 0b10011001);
512 h = _mm256_blend_epi32(h, f, 0b00100010);
513 h = _mm256_permute2x128_si256(h, h, 0x01);
514 __m256i i = _mm256_blend_epi32(d, e, 0b01000100);
515 i = _mm256_blend_epi32(i, f, 0b10011001);
516 out0 = _mm256_blend_epi32(g, h, 0b11110000);
517 out1 = _mm256_blend_epi32(i, g, 0b11110000);
518 out2 = _mm256_blend_epi32(h, i, 0b11110000);
Simd operator&(Simd rhs) const
bool operator==(Simd rhs) const
Simd operator>(Simd rhs) const
Simd operator<<(int shift) 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,...)
constexpr T const & Min(T const &a, T const &b)
constexpr auto Equal(T const &a, T const &b)
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 Select(bool condition, T a, T b)
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)
void StoreTransposed(T *ptr, Simd< T, N > a, Simd< T, N > b, Simd< T, N > c)
void Store(T *ptr, Simd< T, N > a)
int StoreSelected(T *ptr, Simd< MaskT, N > condition, Simd< T, N > values)
V Load(typename V::Scalar const *ptr)
Simd< T, N > ShiftRight(Simd< T, N > a)
#define MOCHI_NATIVE_SIMD_IMPL_BOILERPLATE(T, N, NativeT)