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");
40 if constexpr (i == 0) {
41 return _mm_cvtsd_f64(v.raw);
42 }
else if constexpr (i == 1) {
43 return _mm_cvtsd_f64(_mm_shuffle_pd(v.raw, v.raw, 0x01));
49#if MOCHI_COMPILER_MSVC
50 return v.raw.m128d_f64[i];
62#if MOCHI_COMPILER_MSVC
64 result.raw.m128d_f64[i] = value;
67 static constexpr __m128i kMasks[] = {{-1LL, 0LL}, {0LL, -1LL}};
68 return _mm_blendv_pd(v.raw, _mm_set_pd1(value), _mm_castsi128_pd(kMasks[i]));
74 static_assert(i >= 0 && i < 2,
"Index out of range");
75 if constexpr (i == 0) {
76 return _mm_shuffle_pd(_mm_set1_pd(value), v.raw, 0x02);
78 return _mm_shuffle_pd(v.raw, _mm_set1_pd(value), 0x02);
84 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
85 auto mask = GetMSBitMask(v);
86 if constexpr (N ==
kSize) {
87 return mask == 0x0000FFFF;
89 return (mask & 0x000000FF) == 0x000000FF;
95 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
96 int mask = GetMSBitMask(v);
97 if constexpr (N ==
kSize) {
100 return (mask & 0x000000FF) != 0;
104 template <
int x,
int y>
106 static_assert(x >= 0 && x < 2 && y >= 0 && y < 2,
"invalid blend index");
107 if constexpr (x == 0 && y == 0) {
109 }
else if constexpr (x == 1 && y == 1) {
112 return _mm_blend_pd(a.raw, b.raw, x | (y << 1));
126 return _mm_i64gather_pd(ptr, indices.raw,
sizeof(
double));
129 template <
int N = kSize>
131 static_assert(N >= 0 && N <=
kSize);
132 if constexpr (N == 0) {
134 }
else if constexpr (N == 1) {
135 return Simd{*ptr, 0.0};
137 return _mm_loadu_pd(ptr);
141 static_assert(
sizeof(
long long) == 8);
143#if MOCHI_COMPILER_MSVC
146 static constexpr __m128i kLoadMasks[] = {
147 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
148 {-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0},
149 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};
153 static constexpr __m128i kLoadMasks[] = {{0LL, 0LL}, {-1LL, 0LL}, {-1LL, -1LL}};
158 return _mm_maskload_pd(ptr, kLoadMasks[n]);
161 template <
int kTupleCount = kSize>
164 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
165 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
166 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
167 auto a = Simd::Load<2>(ptr).raw;
168 auto b = Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 2).raw;
169 auto c = Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 4).raw;
170 out0.raw = _mm_shuffle_pd(a, b, 0b0010);
171 out1.raw = _mm_shuffle_pd(a, c, 0b0001);
172 out2.raw = _mm_shuffle_pd(b, c, 0b0010);
175 template <
int N = kSize>
177 static_assert(N >= 0 && N <=
kSize);
178 if constexpr (N == 0) {
179 }
else if constexpr (N <
kSize) {
181 memcpy(ptr, &v,
sizeof(
Scalar) * N);
183 _mm_storeu_pd(ptr, v.raw);
198 auto mask = _mm_movemask_pd(condition.raw);
199 auto swapped = _mm_shuffle_pd(values.raw, values.raw, 1);
201 _mm_castsi128_pd(_mm_set1_epi32((mask & 1) - 1));
202 _mm_storeu_pd(ptr, _mm_blendv_pd(values.raw, swapped, blendMask));
203 return _mm_popcnt_u32(mask);
206 template <
int kTupleCount = kSize>
208 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
210 Simd::Store<2>(ptr, _mm_shuffle_pd(a.raw, b.raw, 0b00));
211 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
212 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
213 if constexpr (kCount1 > 0) {
214 Simd::Store<kCount1>(ptr + 2, _mm_shuffle_pd(c.raw, a.raw, 0b10));
216 if constexpr (kCount2 > 0) {
217 Simd::Store<kCount2>(ptr + 4, _mm_shuffle_pd(b.raw, c.raw, 0b11));
222 return _mm_blendv_pd(b.raw, a.raw, mask.raw);
226 template <
int x = 0,
int y = 1>
228 static_assert(x >= 0 && x < 2,
"Invalid index");
229 static_assert(y >= 0 && y < 2,
"Invalid index");
230 if constexpr (x == 0 && y == 1) {
233 return _mm_shuffle_pd(v.raw, v.raw, x | (y << 1));
238 return _mm_sqrt_pd(v.raw);
242 return Simd{1.0} / v;
251 return _mm_castsi128_pd(_mm_set1_epi64x(0x8000000000000000LL));
255 return _mm_andnot_pd(SignBitMask().
raw, v.raw);
259 return _mm_min_pd(a.raw, b.raw);
263 return _mm_max_pd(a.raw, b.raw);
267 return _mm_floor_pd(a.raw);
271 return _mm_round_pd(v.raw, _MM_FROUND_TO_NEAREST_INT);
274#if MOCHI_ARCH_X64_SVML
276 return _mm_cos_pd(a.raw);
280 return _mm_sin_pd(a.raw);
284 return _mm_tan_pd(a.raw);
288 return _mm_acos_pd(a.raw);
292 return _mm_asin_pd(a.raw);
296 return _mm_atan_pd(a.raw);
300 return _mm_exp_pd(a.raw);
304 return _mm_log_pd(a.raw);
308 return _mm_tanh_pd(a.raw);
313#if MOCHI_ARCH_X64_FMA
314 return {_mm_fmadd_pd(a.raw, b.raw, c.raw)};
321#if MOCHI_ARCH_X64_FMA
322 return _mm_fmsub_pd(a.raw, b.raw, c.raw);
329#if MOCHI_ARCH_X64_FMA
330 return _mm_fnmadd_pd(a.raw, b.raw, c.raw);
337#if MOCHI_ARCH_X64_FMA
338 return _mm_fnmsub_pd(a.raw, b.raw, c.raw);
346 static_assert(N == 2,
"Unsupported N");
352 static_assert(N == 2,
"Unsupported N");
358 static_assert(N == 2,
"Unsupported N");
364 static_assert(N == 2,
"Unsupported N");
370 static_assert(N == 2,
"Unsupported N");
371#if MOCHI_COMPILER_CLANG
372 return _mm_dp_pd(a.raw, b.raw, -1);
374 return _mm_dp_pd(a.raw, b.raw, 0xFF);
379 return _mm_cmplt_pd(this->
raw, rhs.raw);
383 return _mm_cmpgt_pd(this->
raw, rhs.raw);
387 return _mm_cmple_pd(this->
raw, rhs.raw);
391 return _mm_cmpge_pd(this->
raw, rhs.raw);
395 return _mm_cmpeq_pd(a.raw, b.raw);
399 return _mm_cmpneq_pd(a.raw, b.raw);
403 return _mm_setzero_pd();
407 auto mask = GetMSBitMask(
Equal(
raw, rhs.raw));
408 return mask == 0xFFFF;
419 __m128d ones = _mm_castsi128_pd(_mm_cmpeq_epi32(dummy, dummy));
420 return _mm_xor_pd(
raw, ones);
424 return _mm_xor_pd(
raw, SignBitMask().
raw);
428 return _mm_add_pd(
raw, rhs.raw);
432 return _mm_sub_pd(
raw, rhs.raw);
436 return _mm_mul_pd(
raw, rhs.raw);
440 return _mm_div_pd(
raw, rhs.raw);
444 return _mm_and_pd(
raw, rhs.raw);
448 return _mm_or_pd(
raw, rhs.raw);
452 return _mm_xor_pd(
raw, rhs.raw);
458 return _mm_movemask_epi8(_mm_castpd_si128(a.raw));
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
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
T Dot(Simd< T, N > a, Simd< T, N > b)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
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 auto MulAdd(A a, B b, C 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 auto MulSub(A a, B b, C 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 auto NegMulAdd(A a, B b, C c)
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 auto NegMulSub(A a, B b, C 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)