21#if MOCHI_USE_SIMD && MOCHI_ARCH_X64_AVX2
29class Simd<int64_t, 2> {
31 static_assert(
sizeof(int64_t) ==
sizeof(
long long));
34 Simd(int64_t low, int64_t high)
35 :
raw(_mm_set_epi64x(static_cast<long long>(high), static_cast<long long>(low))) {}
36 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
37 Simd(U a) :
raw(_mm_set1_epi64x(static_cast<long long>(a))) {}
41 static_assert(i >= 0 && i <
kSize,
"Index out of range");
42 if constexpr (i == 0) {
43 return _mm_cvtsi128_si64(v.raw);
44 }
else if constexpr (i == 1) {
45 return _mm_cvtsi128_si64(_mm_unpackhi_epi64(v.raw, v.raw));
51#if MOCHI_COMPILER_MSVC
52 return v.raw.m128i_i64[i];
64 static_assert(N >= 1 && N <=
kSize,
"Unsupported N");
65 auto mask = GetMSBitMask(v);
66 if constexpr (N ==
kSize) {
67 return mask == 0x0000FFFF;
69 return (mask & 0x000000FF) == 0x000000FF;
73 template <
int x,
int y>
75 static_assert(x >= 0 && x < 2 && y >= 0 && y < 2,
"invalid blend index");
76 if constexpr (x == 0 && y == 0) {
78 }
else if constexpr (x == 1 && y == 1) {
81 return _mm_castpd_si128(
82 _mm_blend_pd(_mm_castsi128_pd(a.raw), _mm_castsi128_pd(b.raw), x | (y << 1)));
97 static_assert(N == 2,
"Unsupported N");
103 static_assert(N == 2,
"Unsupported N");
109 static_assert(N == 2,
"Unsupported N");
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};
120 }
else if constexpr (N == 2) {
121 return _mm_loadu_si128(
reinterpret_cast<__m128i const*
>(ptr));
134 template <
int kTupleCount = kSize>
137 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
138 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
139 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
140 auto a = _mm_castsi128_pd(Simd::Load<2>(ptr).
raw);
141 auto b = _mm_castsi128_pd(Simd::Load<kCount1>(kCount1 == 0 ? ptr : ptr + 2).
raw);
142 auto c = _mm_castsi128_pd(Simd::Load<kCount2>(kCount2 == 0 ? ptr : ptr + 4).
raw);
143 out0.raw = _mm_castpd_si128(_mm_shuffle_pd(a, b, 0b0010));
144 out1.raw = _mm_castpd_si128(_mm_shuffle_pd(a, c, 0b0001));
145 out2.raw = _mm_castpd_si128(_mm_shuffle_pd(b, c, 0b0010));
159 return _mm_blendv_epi8(b.raw, a.raw, mask.raw);
162 template <
int x = 0,
int y = 1>
164 static_assert(x >= 0 && x < 2,
"Invalid index");
165 static_assert(y >= 0 && y < 2,
"Invalid index");
166 if constexpr (x == 0 && y == 1) {
169 return _mm_castpd_si128(
170 _mm_shuffle_pd(_mm_castsi128_pd(v.raw), _mm_castsi128_pd(v.raw), x | (y << 1)));
174 template <
int N = kSize>
176 static_assert(N >= 0 && N <=
kSize);
177 if constexpr (N == 0) {
178 }
else if constexpr (N <
kSize) {
180 memcpy(ptr, &v.raw,
sizeof(
Scalar) * N);
182 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(ptr), v.raw);
198 auto mask = _mm_movemask_pd(_mm_castsi128_pd(condition.raw));
199 auto swapped = _mm_castpd_si128(_mm_shuffle_pd(
200 _mm_castsi128_pd(values.raw), _mm_castsi128_pd(values.raw), 1));
201 auto blendMask = _mm_set1_epi32((mask & 1) - 1);
202 auto packed = _mm_blendv_epi8(values.raw, swapped, blendMask);
203 _mm_storeu_si128(
reinterpret_cast<__m128i*
>(ptr), packed);
204 return _mm_popcnt_u32(mask);
207 template <
int kTupleCount = kSize>
209 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
211 auto d = _mm_shuffle_pd(_mm_castsi128_pd(a.raw), _mm_castsi128_pd(b.raw), 0b00);
212 auto e = _mm_shuffle_pd(_mm_castsi128_pd(c.raw), _mm_castsi128_pd(a.raw), 0b10);
213 auto f = _mm_shuffle_pd(_mm_castsi128_pd(b.raw), _mm_castsi128_pd(c.raw), 0b11);
214 Simd::Store<2>(ptr, _mm_castpd_si128(d));
215 constexpr int kCount1 =
Clamp(kTupleCount * 3 - 2, 0, 2);
216 constexpr int kCount2 =
Clamp(kTupleCount * 3 - 4, 0, 2);
217 if constexpr (kCount1 > 0) {
218 Simd::Store<kCount1>(ptr + 2, _mm_castpd_si128(e));
220 if constexpr (kCount2 > 0) {
221 Simd::Store<kCount2>(ptr + 4, _mm_castpd_si128(f));
226 return _mm_setzero_si128();
230 return _mm_cmpgt_epi64(rhs.raw, this->raw);
234 return _mm_cmpgt_epi64(this->
raw, rhs.raw);
238 return ~(*
this > rhs);
242 return ~(*
this < rhs);
246 return _mm_cmpeq_epi64(a.raw, b.raw);
254 auto mask = GetMSBitMask(
Equal(*
this, rhs));
255 return mask == 0xFFFF;
259 auto mask = GetMSBitMask(
NotEqual(*
this, rhs));
264 __m128i ones = _mm_cmpeq_epi64(
raw,
raw);
265 return _mm_xor_si128(
raw, ones);
269 return _mm_sub_epi64(_mm_setzero_si128(),
raw);
273 return _mm_add_epi64(
raw, rhs.raw);
277 return _mm_sub_epi64(
raw, rhs.raw);
287#if MOCHI_ARCH_X64_SVML
288 return _mm_div_epi64(
raw, rhs.raw);
296 return _mm_and_si128(
raw, rhs.raw);
300 return _mm_or_si128(
raw, rhs.raw);
304 return _mm_xor_si128(
raw, rhs.raw);
308 return _mm_slli_epi64(
raw, rhs);
311 template <
int kShift>
313 return _mm_srli_epi64(a.raw, kShift);
319 return _mm_movemask_epi8(a.raw);
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
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
constexpr T const & Min(T const &a, T const &b)
constexpr auto Equal(T const &a, T const &b)
constexpr auto NotEqual(T const &a, T const &b)
V Broadcast(typename V::Scalar a)
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
constexpr T Select(bool condition, T a, T b)
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)
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)