21#if MOCHI_USE_SIMD && MOCHI_ARCH_ARM_NEON
34 template <
class U, MOCHI_REQUIRES_NON_BOOL_SCALAR(U, Scalar)>
39 static_assert(i >= 0 && i < 4,
"Index out of range");
40 return vgetq_lane_f32(v.raw, i);
51 result.raw[i] = value;
57 static_assert(i >= 0 && i <
kSize,
"Index out of range");
58 return Set(v, i, value);
62 return vsetq_lane_f32(1.0f, a.raw, 3);
66 return vsetq_lane_f32(0.0f, a.raw, 3);
69 template <
int x,
int y,
int z,
int w>
72 x >= 0 && x <= 1 && y >= 0 && y <= 1 && z >= 0 && z <= 1 && w >= 0 && w <= 1,
73 "invalid blend index");
74 int constexpr kCount = x + y + z + w;
75 if constexpr (kCount == 0) {
77 }
else if constexpr (kCount == 4) {
79 }
else if constexpr (kCount == 1) {
81 int constexpr kLane = x ? 0 : (y ? 1 : (z ? 2 : 3));
82 return vcopyq_laneq_f32(a.raw, kLane, b.raw, kLane);
83 }
else if constexpr (kCount == 3) {
85 int constexpr kLane = !x ? 0 : (!y ? 1 : (!z ? 2 : 3));
86 return vcopyq_laneq_f32(b.raw, kLane, a.raw, kLane);
87 }
else if constexpr (x == 1 && y == 1) {
88 return vcombine_f32(vget_low_f32(b.raw), vget_high_f32(a.raw));
89 }
else if constexpr (z == 1 && w == 1) {
90 return vcombine_f32(vget_low_f32(a.raw), vget_high_f32(b.raw));
94 auto mask = int32x4_t{x ? 0 : -1, y ? 0 : -1, z ? 0 : -1, w ? 0 : -1};
95 return Select(vreinterpretq_f32_s32(mask), a, b);
101 static_assert(N >= 1 && N <= 4,
"Invalid number of components");
103 vget_lane_u64(vreinterpret_u64_u16(vqmovn_u32(vreinterpretq_u32_f32(v.raw))), 0);
104 if constexpr (N ==
kSize) {
105 return mask == 0xFFFFFFFFFFFFFFFFULL;
107 int constexpr kNumBits = N * 16;
108 auto constexpr kMustBeSet = (uint64_t(1) << kNumBits) - 1;
109 return (mask & kMustBeSet) == kMustBeSet;
115 static_assert(N >= 1 && N <= 4,
"Invalid number of components");
117 vget_lane_u64(vreinterpret_u64_u16(vqmovn_u32(vreinterpretq_u32_f32(v.raw))), 0);
118 if constexpr (N ==
kSize) {
121 int constexpr kNumBits = N * 16;
122 auto constexpr kMayBeSet = (uint64_t(1) << kNumBits) - 1;
123 return (mask & kMayBeSet) != 0;
133 return vdupq_laneq_f32(v.raw, i);
136 template <
int N = kSize>
138 static_assert(N >= 0 && N <= 4);
139 if constexpr (N == 0) {
141 }
else if constexpr (N == 1) {
142 return Simd{ptr[0], 0, 0, 0};
143 }
else if constexpr (N == 2) {
144 return Simd{ptr[0], ptr[1], 0, 0};
145 }
else if constexpr (N == 3) {
146 return Simd{ptr[0], ptr[1], ptr[2], 0};
148 return vld1q_f32(ptr);
168 ptr[indices.raw[0]], ptr[indices.raw[1]], ptr[indices.raw[2]], ptr[indices.raw[3]]};
171 template <
int kTupleCount = kSize>
174 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Unsupported kTupleCount");
175 if constexpr (kTupleCount == 1) {
176 out0.raw = float32x4_t{ptr[0], 0.0f, 0.0f, 0.0f};
177 out1.raw = float32x4_t{ptr[1], 0.0f, 0.0f, 0.0f};
178 out2.raw = float32x4_t{ptr[2], 0.0f, 0.0f, 0.0f};
179 }
else if constexpr (kTupleCount == 2) {
180 out0.raw = float32x4_t{ptr[0], ptr[3], 0.0f, 0.0f};
181 out1.raw = float32x4_t{ptr[1], ptr[4], 0.0f, 0.0f};
182 out2.raw = float32x4_t{ptr[2], ptr[5], 0.0f, 0.0f};
183 }
else if constexpr (kTupleCount == 3) {
184 out0.raw = float32x4_t{ptr[0], ptr[3], ptr[6], 0.0f};
185 out1.raw = float32x4_t{ptr[1], ptr[4], ptr[7], 0.0f};
186 out2.raw = float32x4_t{ptr[2], ptr[5], ptr[8], 0.0f};
188 float32x4x3_t result = vld3q_f32(ptr);
189 out0.raw = result.val[0];
190 out1.raw = result.val[1];
191 out2.raw = result.val[2];
199 float const*
const zero = arm_simd::kZeroTuple<float>;
200 float32x4x3_t r = vld3q_dup_f32(count > 0 ? ptr : zero);
201 r = vld3q_lane_f32(count > 1 ? ptr + 3 : zero, r, 1);
202 r = vld3q_lane_f32(count > 2 ? ptr + 6 : zero, r, 2);
203 r = vld3q_lane_f32(count > 3 ? ptr + 9 : zero, r, 3);
210 return vbslq_f32(vreinterpretq_u32_f32(mask.raw), a.raw, b.raw);
215 static_assert(i >= 0 && i <= 3,
"Invalid component index");
216 auto zeros = vdupq_n_f32(0);
217 return vsetq_lane_f32(1.0f, zeros, i);
220 template <
int x = 0,
int y = 1,
int z = 2,
int w = 3>
223 x >= 0 && x < 4 && y >= 0 && y < 4 && z >= 0 && z < 4 && w >= 0 && w < 4,
"Invalid index");
224 return Simd{a.raw[x], a.raw[y], b.raw[z], b.raw[w]};
227 template <
int x = 0,
int y = 1,
int z = 2,
int w = 3>
233 return vreinterpretq_f32_s32(vdupq_n_s32((int32_t)0x80000000));
236 template <
int N = kSize>
238 static_assert(N >= 0 && N <=
kSize);
241 if constexpr (N == 0) {
242 }
else if constexpr (N == 1) {
243 vst1q_lane_f32(ptr, v.raw, 0);
244 }
else if constexpr (N == 2) {
245 vst1_f32(ptr, vget_low_f32(v.raw));
246 }
else if constexpr (N == 3) {
247 vst1_f32(ptr, vget_low_f32(v.raw));
248 vst1q_lane_f32(ptr + 2, v.raw, 2);
250 vst1q_f32(ptr, v.raw);
267 uint32x4_t shifted = vshrq_n_u32(vreinterpretq_u32_f32(condition.raw), 31);
268 uint32x4_t
const multipliers = {1, 2, 4, 8};
269 uint32x4_t weighted = vmulq_u32(shifted, multipliers);
270 uint32_t count = vaddvq_u32(shifted);
271 uint32_t mask = vaddvq_u32(weighted);
272 uint8x16_t pattern = vld1q_u8(arm_simd::kStoreSelectedShuffleTableS4[mask]);
273 uint8x16_t packed = vqtbl1q_u8(vreinterpretq_u8_f32(values.raw), pattern);
274 vst1q_f32(ptr, vreinterpretq_f32_u8(packed));
275 return static_cast<int>(count);
278 template <
int kTupleCount = kSize>
280 static_assert(kTupleCount >= 1 && kTupleCount <=
kSize,
"Invalid kTupleCount");
281 if constexpr (kTupleCount == 1) {
285 }
else if constexpr (kTupleCount == 2) {
286 Simd::Store(ptr,
Simd{a[0], b[0], c[0], a[1]});
289 }
else if constexpr (kTupleCount == 3) {
290 Simd::Store(ptr + 0,
Simd{a[0], b[0], c[0], a[1]});
291 Simd::Store(ptr + 4,
Simd{b[1], c[1], a[2], b[2]});
294 vst3q_f32(ptr, float32x4x3_t({a.raw, b.raw, c.raw}));
304 float32x4x3_t
const v = {a.raw, b.raw, c.raw};
305 vst3q_lane_f32(count > 3 ? ptr + 9 : ptr, v, 3);
306 vst3q_lane_f32(count > 2 ? ptr + 6 : ptr, v, 2);
307 vst3q_lane_f32(count > 1 ? ptr + 3 : ptr, v, 1);
308 vst3q_lane_f32(ptr, v, 0);
312 return vsqrtq_f32(v.raw);
316 return vrecpeq_f32(v.raw);
320 return vrsqrteq_f32(v.raw);
324 return vabsq_f32(v.raw);
328 return vminq_f32(a.raw, b.raw);
332 return vmaxq_f32(a.raw, b.raw);
336 return vrndmq_f32(a.raw);
340 return vrndaq_f32(v.raw);
344 return vfmaq_f32(c.raw, b.raw, a.raw);
352 return vfmsq_f32(c.raw, b.raw, a.raw);
361 static_assert(N >= 2 && N <= 4,
"Unsupported N");
362 if constexpr (N == 2) {
364 }
else if constexpr (N == 3) {
365 return vminvq_f32(vsetq_lane_f32(std::numeric_limits<Scalar>::infinity(), a.raw, 3));
367 return vminvq_f32(a.raw);
373 static_assert(N >= 2 && N <= 4,
"Unsupported N");
374 if constexpr (N == 2) {
376 }
else if constexpr (N == 3) {
378 return vmaxvq_f32(vsetq_lane_f32(-std::numeric_limits<Scalar>::infinity(), a.raw, 3));
380 return vmaxvq_f32(a.raw);
386 static_assert(N >= 2 && N <= 4,
"Unsupported N");
387 if constexpr (N == 2) {
388 return a.raw[0] + a.raw[1];
389 }
else if constexpr (N == 3) {
390 return a.raw[0] + a.raw[1] + a.raw[2];
391 }
else if constexpr (N == 4) {
392 return vaddvq_f32(a.raw);
397 return vpaddq_f32(vpaddq_f32(a.raw, b.raw), vpaddq_f32(c.raw, d.raw));
402 static_assert(N >= 2 && N <= 4,
"Unsupported N");
403 if constexpr (N == 2) {
404 return a.raw[0] * a.raw[1];
405 }
else if constexpr (N == 3) {
406 return a.raw[0] * a.raw[1] * a.raw[2];
407 }
else if constexpr (N == 4) {
408 return a.raw[0] * a.raw[1] * a.raw[2] * a.raw[3];
414 static_assert(N >= 2 && N <= 4,
"Unsupported N");
419 return vreinterpretq_f32_u32(vcltq_f32(this->
raw, rhs.raw));
423 return vreinterpretq_f32_u32(vcgtq_f32(this->
raw, rhs.raw));
427 return vreinterpretq_f32_u32(vcleq_f32(this->
raw, rhs.raw));
431 return vreinterpretq_f32_u32(vcgeq_f32(this->
raw, rhs.raw));
435 return vreinterpretq_f32_u32(vceqq_f32(a.raw, b.raw));
443 return vdupq_n_f32(0);
451 return !(*
this == rhs);
455 return vreinterpretq_f32_u32(vmvnq_u32(vreinterpretq_u32_f32(
raw)));
459 return vnegq_f32(
raw);
463 return vaddq_f32(
raw, rhs.raw);
467 return vsubq_f32(
raw, rhs.raw);
471 return vmulq_f32(
raw, rhs.raw);
475 return vdivq_f32(
raw, rhs.raw);
479 return vreinterpretq_f32_u32(
480 vandq_u32(vreinterpretq_u32_f32(
raw), vreinterpretq_u32_f32(rhs.raw)));
484 return vreinterpretq_f32_u32(
485 vorrq_u32(vreinterpretq_u32_f32(
raw), vreinterpretq_u32_f32(rhs.raw)));
489 return vreinterpretq_f32_u32(
490 veorq_u32(vreinterpretq_u32_f32(
raw), vreinterpretq_u32_f32(rhs.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
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 > 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)
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)