21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
29class Simd<double, 2> {
32 Simd(
double a,
double b) :
raw(_mm_set_pd(b, a)) {}
34 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
35 Simd(U a) :
raw(_mm_set_pd1(a)) {}
39 static_assert(i >= 0 && i < 2,
"Index out of range");
45#if MOCHI_COMPILER_MSVC
46 return raw.m128d_f64[i];
54#if MOCHI_COMPILER_MSVC
56 result.raw.m128d_f64[i] = value;
59 static constexpr __m128i kMasks[] = {{-1LL, 0LL}, {0LL, -1LL}};
60 return _mm_blendv_pd(v.raw, _mm_set_pd1(value), _mm_castsi128_pd(kMasks[i]));
66 static_assert(i >= 0 && i < 2,
"Index out of range");
67 if constexpr (i == 0) {
68 return _mm_shuffle_pd(_mm_set1_pd(value), v.raw, 0x02);
70 return _mm_shuffle_pd(v.raw, _mm_set1_pd(value), 0x02);
76 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
77 int constexpr kLanes = (1 << N) - 1;
78 return (ToMask(v) & kLanes) == kLanes;
83 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
84 int constexpr kLanes = (1 << N) - 1;
85 return (ToMask(v) & kLanes) != 0;
88 template <
int x,
int y>
90 static_assert(x >= 0 && x < 2 && y >= 0 && y < 2,
"invalid blend index");
91 if constexpr (x == 0 && y == 0) {
93 }
else if constexpr (x == 1 && y == 1) {
96 return _mm_blend_pd(a.raw, b.raw, x | (y << 1));
110 return _mm_i64gather_pd(ptr, indices.raw,
sizeof(
double));
113 template <
int N = kSize>
115 static_assert(N >= 0 && N <=
kSize);
116 if constexpr (N == 0) {
118 }
else if constexpr (N == 1) {
119 return Simd{*ptr, 0.0};
121 return _mm_loadu_pd(ptr);
125 static_assert(
sizeof(
long long) == 8);
127#if !MOCHI_ARCH_X64_AVX512
128#if MOCHI_COMPILER_MSVC
131 static constexpr __m128i kLoadMasks[] = {
132 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
133 {-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0},
134 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};
138 static constexpr __m128i kLoadMasks[] = {{0LL, 0LL}, {-1LL, 0LL}, {-1LL, -1LL}};
144#if MOCHI_ARCH_X64_AVX512
145 return _mm_maskz_loadu_pd(x64_simd::kLaneMasksS8[n], ptr);
147 return _mm_maskload_pd(ptr, kLoadMasks[n]);
151 template <
int kTupleCount = kSize>
154 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
155 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
156 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
157 auto a = Simd::Load<2>(ptr).raw;
158 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 2).raw;
159 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 4).raw;
160 Deinterleave3(a, b, c, out0, out1, out2);
166 int const n = count * 3;
168 auto b = Simd::Load(n > 2 ? ptr + 2 : ptr,
Clamp(n - 2, 0, 2)).raw;
169 auto c = Simd::Load(n > 4 ? ptr + 4 : ptr,
Clamp(n - 4, 0, 2)).raw;
170 Deinterleave3(a, b, c, out0, out1, out2);
173 template <
int N = kSize>
175 static_assert(N >= 0 && N <=
kSize);
180 if constexpr (N == 0) {
181 }
else if constexpr (N == 1) {
182 _mm_store_sd(ptr, v.raw);
184 _mm_storeu_pd(ptr, v.raw);
190#if MOCHI_ARCH_X64_AVX512
191 _mm_mask_storeu_pd(ptr, x64_simd::kLaneMasksS8[n], v.raw);
202 auto mask = ToMask(condition);
203 auto swapped = _mm_shuffle_pd(values.raw, values.raw, 1);
205 _mm_castsi128_pd(_mm_set1_epi32((mask & 1) - 1));
206 _mm_storeu_pd(ptr, _mm_blendv_pd(values.raw, swapped, blendMask));
207 return _mm_popcnt_u32(mask);
210 template <
int kTupleCount = kSize>
212 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
214 Interleave3(a, b, c, x0, x1, x2);
215 Simd::Store<2>(ptr, x0);
216 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
217 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
218 if constexpr (kCount1 > 0) {
219 Simd::Store<kCount1>(ptr + 2, x1);
221 if constexpr (kCount2 > 0) {
222 Simd::Store<kCount2>(ptr + 4, x2);
229 Interleave3(a, b, c, x0, x1, x2);
230#if MOCHI_ARCH_X64_AVX512
233 uint32_t
const bits = (1u << (count * 3)) - 1;
234 _mm_mask_storeu_pd(ptr,
static_cast<__mmask8
>(bits), x0.raw);
235 _mm_mask_storeu_pd(ptr + (count > 0 ? 2 : 0),
static_cast<__mmask8
>(bits >> 2), x1.raw);
236 _mm_mask_storeu_pd(ptr + (count > 1 ? 4 : 0),
static_cast<__mmask8
>(bits >> 4), x2.raw);
240 auto const n = _mm_set1_epi64x(count * 3);
241 auto const mask0 = _mm_cmpgt_epi64(n, _mm_set_epi64x(1, 0));
242 auto const mask1 = _mm_cmpgt_epi64(n, _mm_set_epi64x(3, 2));
243 auto const mask2 = _mm_cmpgt_epi64(n, _mm_set_epi64x(5, 4));
244 _mm_maskstore_pd(ptr, mask0, x0.raw);
245 _mm_maskstore_pd(ptr + (count > 0 ? 2 : 0), mask1, x1.raw);
246 _mm_maskstore_pd(ptr + (count > 1 ? 4 : 0), mask2, x2.raw);
251 return _mm_blendv_pd(b.raw, a.raw, mask.raw);
255 template <
int x = 0,
int y = 1>
257 static_assert(x >= 0 && x < 2,
"Invalid index");
258 static_assert(y >= 0 && y < 2,
"Invalid index");
259 if constexpr (x == 0 && y == 1) {
262 return _mm_shuffle_pd(v.raw, v.raw, x | (y << 1));
267 return _mm_sqrt_pd(v.raw);
271 return Simd{1.0} / v;
280 return _mm_castsi128_pd(_mm_set1_epi64x(0x8000000000000000LL));
284 return _mm_andnot_pd(SignBitMask().
raw, v.raw);
288 return _mm_min_pd(a.raw, b.raw);
292 return _mm_max_pd(a.raw, b.raw);
296 return _mm_floor_pd(a.raw);
300 return _mm_round_pd(v.raw, _MM_FROUND_TO_NEAREST_INT);
303#if MOCHI_ARCH_X64_SVML
305 return _mm_cos_pd(a.raw);
309 return _mm_sin_pd(a.raw);
313 return _mm_tan_pd(a.raw);
317 return _mm_acos_pd(a.raw);
321 return _mm_asin_pd(a.raw);
325 return _mm_atan_pd(a.raw);
329 return _mm_exp_pd(a.raw);
333 return _mm_log_pd(a.raw);
337 return _mm_tanh_pd(a.raw);
342#if MOCHI_ARCH_X64_FMA
343 return {_mm_fmadd_pd(a.raw, b.raw, c.raw)};
350#if MOCHI_ARCH_X64_FMA
351 return _mm_fmsub_pd(a.raw, b.raw, c.raw);
358#if MOCHI_ARCH_X64_FMA
359 return _mm_fnmadd_pd(a.raw, b.raw, c.raw);
366#if MOCHI_ARCH_X64_FMA
367 return _mm_fnmsub_pd(a.raw, b.raw, c.raw);
375 static_assert(N == 2,
"Unsupported N");
381 static_assert(N == 2,
"Unsupported N");
387 static_assert(N == 2,
"Unsupported N");
392 return _mm_add_pd(_mm_unpacklo_pd(a.raw, b.raw), _mm_unpackhi_pd(a.raw, b.raw));
397 static_assert(N == 2,
"Unsupported N");
403 static_assert(N == 2,
"Unsupported N");
404#if MOCHI_COMPILER_CLANG
405 return _mm_dp_pd(a.raw, b.raw, -1);
407 return _mm_dp_pd(a.raw, b.raw, 0xFF);
412 return _mm_cmplt_pd(this->
raw, rhs.raw);
416 return _mm_cmpgt_pd(this->
raw, rhs.raw);
420 return _mm_cmple_pd(this->
raw, rhs.raw);
424 return _mm_cmpge_pd(this->
raw, rhs.raw);
428 return _mm_cmpeq_pd(a.raw, b.raw);
432 return _mm_cmpneq_pd(a.raw, b.raw);
436 return _mm_setzero_pd();
440 return ToMask(
Equal(*
this, rhs)) == 0x3;
444 return ToMask(
NotEqual(*
this, rhs)) != 0;
450 __m128d ones = _mm_castsi128_pd(_mm_cmpeq_epi32(dummy, dummy));
451 return _mm_xor_pd(
raw, ones);
455 return _mm_xor_pd(
raw, SignBitMask().
raw);
459 return _mm_add_pd(
raw, rhs.raw);
463 return _mm_sub_pd(
raw, rhs.raw);
467 return _mm_mul_pd(
raw, rhs.raw);
471 return _mm_div_pd(
raw, rhs.raw);
475 return _mm_and_pd(
raw, rhs.raw);
479 return _mm_or_pd(
raw, rhs.raw);
483 return _mm_xor_pd(
raw, rhs.raw);
489 return _mm_movemask_pd(a.raw);
494 Deinterleave3(__m128d a, __m128d b, __m128d c,
Simd& out0,
Simd& out1,
Simd& out2) {
495 out0.raw = _mm_shuffle_pd(a, b, 0b0010);
496 out1.raw = _mm_shuffle_pd(a, c, 0b0001);
497 out2.raw = _mm_shuffle_pd(b, c, 0b0010);
504 out0 = _mm_shuffle_pd(a.raw, b.raw, 0b00);
505 out1 = _mm_shuffle_pd(c.raw, a.raw, 0b10);
506 out2 = _mm_shuffle_pd(b.raw, c.raw, 0b11);
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)
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)