35#if MOCHI_ARCH_X64_SVML
36inline constexpr bool kUseSvml =
true;
38inline constexpr bool kUseSvml =
false;
48#define MOCHI_DEFINE_SIMD_OP_EQ(OP_EQ, OP) \
49 template <class T, int N, class RHS> \
50 MOCHI_ANY MOCHI_FORCE_INLINE Simd<T, N>& operator OP_EQ(Simd<T, N>& lhs, RHS rhs) { \
56#define MOCHI_DEFINE_MIXED_SIMD_SCALAR_OP(OP) \
57 template <class T, int N> \
58 MOCHI_ANY MOCHI_FORCE_INLINE auto operator OP(Simd<T, N> lhs, T rhs) { \
59 return lhs OP Simd<T, N>{rhs}; \
61 template <class T, int N> \
62 MOCHI_ANY MOCHI_FORCE_INLINE auto operator OP(T lhs, Simd<T, N> rhs) { \
63 return Simd<T, N>{lhs} OP rhs; \
73#undef MOCHI_DEFINE_SIMD_OP_EQ
79#undef MOCHI_DEFINE_MIXED_SIMD_SCALAR_OP
83template <
class T,
int N>
85 static_assert(
sizeof(T) ==
sizeof(int) ||
sizeof(T) ==
sizeof(int64_t));
86 using I = std::conditional_t<
sizeof(T) ==
sizeof(
int), int, int64_t>;
88 auto const ia = ReinterpretCast<IVec>(a);
89 auto const zero = IVec{};
90 auto const ones = ~zero;
96template <
class T,
int N>
99 superdex::details::IsValidLogicalMask(lhs) && superdex::details::IsValidLogicalMask(rhs),
100 "operator&& requires all lanes to have all-bits-0 or all-bits-1");
104template <
class T,
int N>
107 superdex::details::IsValidLogicalMask(lhs) && superdex::details::IsValidLogicalMask(rhs),
108 "operator|| requires all lanes to have all-bits-0 or all-bits-1");
116template <
class V, MOCHI_CONCEPT_DEF(IsSimd<V>)>
122template <
class V, MOCHI_CONCEPT_DEF(IsSimd<V>)>
124 return V::Broadcast(p);
127template <
int i,
class T,
int N>
132template <
class T,
int N>
138template <
class V,
class... MoreBools>
140 static_assert(V::kSize ==
sizeof...(bs) + 2,
"Incorrect number of arguments");
141 using I = std::conditional_t<
sizeof(
typename V::Scalar) ==
sizeof(
int), int, int64_t>;
143 constexpr I kSimdBool[2] = {0, -1};
144 return ReinterpretCast<V>(IVec{kSimdBool[b0], kSimdBool[b1], kSimdBool[bs]...});
152template <
int i,
class V>
154 return V::template SetBasisVector<i>();
159 static_assert(V::kSize == 4,
"Unsupported SIMD size");
162 return V::template SetBasisVector<0>();
164 return V::template SetBasisVector<1>();
166 return V::template SetBasisVector<2>();
168 return V::template SetBasisVector<3>();
170 MOCHI_ASSERT(axis >= 0 && axis <= 3,
"Invalid component index");
177 return V::AsPoint(a);
182 return V::AsDirection(a);
185template <
class V, MOCHI_CONCEPT_DEF(IsSimd<V>)>
190template <
int N,
class V, MOCHI_CONCEPT_DEF(IsSimd<V>)>
192 return V::template
Load<N>(ptr);
195template <
class V, MOCHI_CONCEPT_DEF(IsSimd<V>)>
197 return V::Load(ptr, n);
200template <
class V,
class I, MOCHI_CONCEPT_DEF(IsSimd<V>)>
203 static_assert(std::is_integral_v<I>,
"Requires integral type");
204 return V::LoadIndexed(ptr, indices);
207template <
int kTupleCount,
class T,
int N>
210 constexpr int kTupleCount_ = (kTupleCount == -1) ? N : kTupleCount;
214template <
class T,
int N>
220template <
int COUNT,
class T,
int N>
222 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
226template <
class T,
int N>
231template <
class T,
int N,
class MaskT>
235 superdex::details::IsValidLogicalMask(condition),
236 "Not a valid logical mask. Each lane must be all-bits-0 or all-bits-1.");
237 if constexpr (
sizeof(MaskT) == 8 &&
sizeof(T) == 4) {
238 auto conditionI64 = ReinterpretCast<Simd<int64_t, N>>(condition);
242 "Expected StaticCast from int64_t to int32_t to return the lower 32 bits. This is not guaranteed by the C++ standard for static_cast, "
243 "but it is guaranteed by the x64 and ARM implementations. Your new CPU architecture behaves differenty. Therefore, this code will "
244 "need to perform a masking operation, or use Simd<uint64_t, N> and Simd<uint32_t, N> (not supported at the time of writing).");
251template <
int kTupleCount,
class T,
int N>
254 constexpr int kTupleCount_ = (kTupleCount == -1) ? N : kTupleCount;
258template <
class T,
int N>
264template <
class T,
int N,
class MaskT>
268 superdex::details::IsValidLogicalMask(conditionMask),
269 "Not a valid logical mask. Each lane must be all-bits-0 or all-bits-1.");
273template <
int kShift,
class T,
int N>
277 "ShiftRight requires a supported signed integer Simd type");
278 static_assert(kShift >= 0 && kShift < (8 *
sizeof(T)),
"Shift amount out-of-range");
279 if constexpr (kShift == 0) {
286template <
int x,
int y,
class T>
291template <
int x,
int y,
int z,
int w,
class T>
296template <
int x,
int y,
int z,
int w,
class T>
301template <
int x,
int y,
class T,
int N>
306template <
int x,
int y,
int z,
int w,
class T,
int N>
311template <
class T,
int N>
316template <
int i,
class T,
int N>
321template <
int iHalf,
class T,
int N>
326template <
int i,
class T,
int N>
331template <
class T,
int N>
338 using T =
typename V::Scalar;
339 static_assert(V::kIsSupported && std::is_integral_v<T>,
"Must be a supported integral Simd type");
340 if constexpr (V::kIsComposite) {
341 return V::Sequence();
343 alignas(V) T
constexpr kSequence[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
345 std::size(kSequence) >= V::kSize,
346 "Vector size is too large. Size of kSequence must be increased.");
351template <
bool x,
bool y,
bool z,
bool w,
class T,
int N>
354 std::is_floating_point_v<T> && (N == 4),
355 "This implementation is intended for Vec4f or Vec4d only");
359template <
class T,
int N>
361 static_assert(std::is_floating_point_v<T>,
"Requires float or double");
365template <
class T,
int N>
370template <
class T,
int N>
375template <
class T,
int N>
380template <
class T,
int N>
385template <
class T,
int N>
390template <
int COUNT,
class T,
int N>
392 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
393 if constexpr (COUNT_ == 1) {
409template <
class V,
class =
void>
410inline constexpr bool kHasOwnHSumEach =
false;
412inline constexpr bool kHasOwnHSumEach<
414 std::void_t<
decltype(V::HSumEach(
418 std::declval<V>()))>> =
true;
421template <
class T,
int N>
425 if constexpr (N == 4) {
427 }
else if constexpr (details::kHasOwnHSumEach<Simd<T, N>>) {
440template <
int COUNT,
class T,
int N>
442 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
443 if constexpr (COUNT_ == 1) {
450template <
int COUNT,
class T,
int N>
452 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
453 if constexpr (COUNT_ == 1) {
460template <
int COUNT,
class T,
int N>
462 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
463 if constexpr (COUNT_ == 1) {
470#define MOCHI_SIMD_MEMBERWISE_FALLBACK(T, N, FN, inVec) \
472 alignas(alignof(Simd<T, N>)) T buf[N]; \
474 for (int i = 0; i < N; ++i) { \
475 buf[i] = FN(buf[i]); \
477 return Load<Simd<T, N>>(buf); \
480template <
class T,
int N>
485template <
class T,
int N>
502SinCosImpl(Simd<float, N> xf, Simd<float, N>& outSin, Simd<float, N>& outCos, Simd<int, N>& outN) {
507 auto r =
FastRound(x * 0x1.45f306dc9c883p-1);
509 x = x - r * 0x1.921fb54442d18p0;
515 auto s1 = 0x1.1107605230bc4p-7 - x2 * 0x1.994eb3774cf24p-13;
516 auto c1 = 1.0 - x2 * 0.5;
517 auto c2 = -0x1.6c087e89a359dp-10 + x2 * 0x1.99343027bf8c3p-16;
520 auto s0 = x - x3 * 0x1.555545995a603p-3;
521 auto c0 = c1 + x4 * 0x1.55553e1068f19p-5;
532inline constexpr float kMaxFastSinCosInput = 1e6f;
536 return AllTrue(
Abs(a) <= Simd<float, N>{kMaxFastSinCosInput});
540template <
class T,
int N>
547 }
else if constexpr (std::is_same_v<T, float>) {
554 details::SinCosImpl(a, sin, cos, n);
556 auto result =
Select((n & 1) - 1, cos, sin);
558 return result ^ ReinterpretCast<Simd<float, N>>((n ^
ShiftRight<1>(n)) << 31);
564template <
class T,
int N>
569 }
else if constexpr (std::is_same_v<T, float>) {
576 details::SinCosImpl(a, sin, cos, n);
578 auto result =
Select((n & 1) - 1, sin, cos);
580 return result ^ ReinterpretCast<Simd<T, N>>(
ShiftRight<1>(n) << 31);
586template <
class T,
int N>
597 details::SinCosImpl(a, sin, cos, n);
598 auto mask = ReinterpretCast<Simd<float, N>>((n & 1) - 1);
600 auto sresult =
Select(mask, sin, cos) ^ ReinterpretCast<Simd<float, N>>(nr << 31);
601 auto cresult =
Select(mask, cos, sin) ^ ReinterpretCast<Simd<float, N>>((n ^ nr) << 31);
602 return {sresult, cresult};
608template <
class T,
int N>
618template <
class T,
int N>
628template <
class T,
int N>
638template <
class T,
int N>
648template <
class T,
int N>
659 using I =
typename VI::Scalar;
660 constexpr int kMantissaBits = std::numeric_limits<T>::digits - 1;
661 constexpr int kExponentBias = std::numeric_limits<T>::max_exponent - 1;
662 constexpr T kInvLn2 = T(1) /
kLn2<T>;
667 V
const lo(T(-kExponentBias) *
kLn2<T>);
668 V
const hi(T(kExponentBias + 1) *
kLn2<T>);
669 V clamped =
Select(a < lo, lo, a);
670 clamped =
Select(clamped > hi, hi, clamped);
675 V
const shift(T(1.5) *
static_cast<T
>(I{1} << kMantissaBits) + T(kExponentBias));
676 V
const t = clamped * kInvLn2 + shift;
677 V
const n = t - shift;
678 V x = clamped - n * T(0.693145751953125);
679 x -= n * T(1.42860682030941723212e-6);
683 if constexpr (std::is_same_v<T, float>) {
684 V
const p01 = x * T(1.6666521e-1) + T(4.9999994e-1);
685 V
const p23 = x * T(8.36871e-3) + T(4.166839e-2);
686 p = p01 + z * (p23 + z * T(1.3814613e-3));
688 static_assert(std::is_same_v<T, double>);
690 V
const p01 = x * T(1.6666666666666413e-1) + T(5.000000000000011e-1);
691 V
const p23 = x * T(8.33333333349434e-3) + T(4.166666666653026e-2);
692 V
const p45 = x * T(1.98412695067651e-4) + T(1.3888888943598715e-3);
693 V
const p67 = x * T(2.755758627776603e-6) + T(2.4801493135021792e-5);
694 V
const p89 = x * T(2.500006925707656e-8) + T(2.763023436834589e-7);
695 p = (p23 * z + p01) + z2 * ((p67 * z + p45) + z2 * p89);
699 V
const scale = ReinterpretCast<V>(ReinterpretCast<VI>(t) << kMantissaBits);
700 return (p * z + x + T(1)) * scale;
704template <
class T,
int N>
718 using I =
typename VI::Scalar;
719 constexpr int kMantissaBits = std::numeric_limits<T>::digits - 1;
720 constexpr int kExponentBias = std::numeric_limits<T>::max_exponent - 1;
724 V
const twoToMantissaBits(
static_cast<T
>(I{1} << kMantissaBits));
725 V
const isDenormal = a < V(std::numeric_limits<T>::min());
726 VI
const bits = ReinterpretCast<VI>(a *
Select(isDenormal, twoToMantissaBits, one));
732 VI
const sqrtHalfBits = ReinterpretCast<VI>(V(T(0.707106781186547524)));
733 VI
const reducedBits = bits - (sqrtHalfBits - VI(I{kExponentBias} << kMantissaBits));
735 ReinterpretCast<V>((reducedBits & VI((I{1} << kMantissaBits) - 1)) + sqrtHalfBits) - one;
736 V e = ReinterpretCast<V>(
738 (twoToMantissaBits + T(kExponentBias));
739 e -= isDenormal & V(T(kMantissaBits));
743 if constexpr (std::is_same_v<T, float>) {
744 y = x * T(7.0376836292e-2) - T(1.1514610310e-1);
745 y = y * x + T(1.1676998740e-1);
746 y = y * x - T(1.2420140846e-1);
747 y = y * x + T(1.4249322787e-1);
748 y = y * x - T(1.6668057665e-1);
749 y = y * x + T(2.0000714765e-1);
750 y = y * x - T(2.4999993993e-1);
751 y = y * x + T(3.3333331174e-1);
754 static_assert(std::is_same_v<T, double>);
755 V p = x * T(1.01875663804580931796e-4) + T(4.97494994976747001425e-1);
756 p = p * x + T(4.70579119878881725854e0);
757 p = p * x + T(1.44989225341610930846e1);
758 p = p * x + T(1.79368678507819816313e1);
759 p = p * x + T(7.70838733755885391666e0);
761 V q = x + T(1.12873587189167450590e1);
762 q = q * x + T(4.52279145837532221105e1);
763 q = q * x + T(8.29875266912776603211e1);
764 q = q * x + T(7.11544750618563894466e1);
765 q = q * x + T(2.31251620126765340583e1);
769 y -= e * T(2.121944400546905827679e-4);
771 V
const result = x + y + e * T(0.693359375);
774 V
const infinity(std::numeric_limits<T>::infinity());
778 V(std::numeric_limits<T>::quiet_NaN()));
782template <
class T,
int N>
792#undef MOCHI_SIMD_MEMBERWISE_FALLBACK
796 return V::Equal(a, b);
801 return V::NotEqual(a, b);
804template <
int COUNT,
class T,
int N>
806 if constexpr (COUNT == 1) {
813template <
int COUNT,
class T,
int N>
815#if MOCHI_PLATFORM_MACOS && MOCHI_ARCH_X64 && MOCHI_OPTIMIZED
819 if constexpr (COUNT == 1) {
829 return Abs(a - b) <= epsilon;
834 return Abs(a) <= epsilon;
837template <
int COUNT,
class T,
int N>
839 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
841 if constexpr (
sizeof(T) ==
sizeof(int) ||
sizeof(T) ==
sizeof(int64_t)) {
843 superdex::details::IsValidLogicalMask(a),
844 "Not a valid logical mask. Each lane must be all-bits-0 or all-bits-1.");
849template <
int COUNT,
class T,
int N>
851 constexpr int COUNT_ = (COUNT == -1) ? N : COUNT;
853 if constexpr (
sizeof(T) ==
sizeof(int) ||
sizeof(T) ==
sizeof(int64_t)) {
855 superdex::details::IsValidLogicalMask(a),
856 "Not a valid logical mask. Each lane must be all-bits-0 or all-bits-1.");
861template <
class T,
int N>
863 if constexpr (std::is_same_v<T, float>) {
864 constexpr int kMask = 0x7F800000;
868 static_assert(std::is_same_v<T, double>,
"VIsFinite only supports float and double.");
869 constexpr int kMask = 0x7FF00000;
876 return ReinterpretCast<Simd<double, 2>>(temp2i64);
885 return ReinterpretCast<Simd<double, 4>>(temp4i64);
888 auto bits = ReinterpretCast<Simd<int64_t, 8>>(a);
889 return ReinterpretCast<Simd<double, 8>>(
VNotEqual(bits & mask, mask));
895 for (
int i = 0; i < N; ++i) {
896 uint64_t isFinite = -
static_cast<uint64_t
>(
IsFinite(a.
raw[i]));
897 memcpy(&result.
raw[i], &isFinite,
sizeof(T));
901 static_assert(std::is_void_v<T>,
"Unsupported type or size");
906template <
int i,
class T,
int N>
911 constexpr int kNumInts{(
sizeof(T) * N) /
sizeof(
int)};
912 constexpr int kStride = kNumInts / N;
916template <
class T,
int N>
921template <
class T,
int N>
926template <
class T,
int N>
931template <
class T,
int N>
936template <
int COUNT,
class V>
938 constexpr int COUNT_ = (COUNT == -1) ? V::kSize : COUNT;
939 if constexpr (COUNT_ == 1) {
946template <
class T,
int N>
948 return Shuffle<1, 2, 0, 3>(
MulSub(a,
Shuffle<1, 2, 0, 3>(b), b *
Shuffle<1, 2, 0, 3>(a)));
951template <
int COUNT,
class T,
int N>
953 static_assert(COUNT == -1 || COUNT >= 2,
"Unsupported COUNT");
954 static_assert(std::is_floating_point_v<T>,
"Requires float or double");
958template <
int COUNT,
class T,
int N>
963template <
int COUNT,
class T,
int N>
968template <
int COUNT,
class T,
int N>
973template <
int COUNT,
class T,
int N>
977 return a / (
VNorm<COUNT>(a) + std::numeric_limits<T>::min());
980template <
class T,
int N>
984 return a / (
Sqrt(normSqr) + std::numeric_limits<T>::min());
987template <
class T,
int N>
994 static_assert(std::is_floating_point_v<T>,
"Requires float or double");
1005 if (absMin ==
Get<0>(abs)) {
1007 }
else if (absMin ==
Get<1>(abs)) {
1022struct NextSupportedSimdSizeHelper;
1024template <
typename T,
int kSize>
1025struct NextSupportedSimdSizeHelper<T, kSize, true> {
1026 static_assert(kSize > 0,
"SIMD size must be positive.");
1028 static constexpr int value =
1032template <
typename T,
int kSize>
1033struct NextSupportedSimdSizeHelper<T, kSize, false> {
1034 static_assert(kSize > 0,
"SIMD size must be positive.");
1036 static constexpr bool kIsSingleRegister =
1038 static constexpr int value = std::conditional_t<
1040 std::integral_constant<int, kSize>,
1041 NextSupportedSimdSizeHelper<T, kSize + 1>>::value;
1044template <
typename T,
int kSize>
1045inline constexpr int kNextSupportedSimdSize = NextSupportedSimdSizeHelper<T, kSize>::value;
1055#if MOCHI_USE_REFLECTION
1056template <
typename T,
int N>
1057struct SReflectTypeTraits<
superdex::Simd<T, N>> {
1058 static constexpr SReflect::CoreType coreType = SReflect::CoreType::CT_array;
1059 static SReflect::ArrayTypeInfo
const& GetTypeInfo() {
1060 static auto* s_typeInfo =
1061 SReflect::MakeFixedArrayTypeInfo<superdex::Simd<T, N>, T, N>(
"superdex::Simd",
true);
static constexpr bool kIsComposite
static constexpr bool kIsEmulated
static constexpr bool kIsSupported
#define MOCHI_ASSERT(condition_without_side_effects,...)
#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, N > VIsFinite(Simd< T, N > a)
Simd< T, 2 > Shuffle(Simd< T, 2 > a)
constexpr int kSimdDefaultSize
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)
T NormSqr(Simd< T, N > a)
constexpr auto Equal(T const &a, T const &b)
Simd< T, N > Cross3(Simd< T, N > a, Simd< T, N > b)
constexpr To StaticCast(From const &a)
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 VNearEqual(V a, V b, V epsilon)
constexpr bool kHasHSumEach
True if HSumEach of four vectors of type V is supported.
Simd< T, N > VNormSqr(Simd< T, N > a)
int IsTrue(Simd< T, N > mask)
V Broadcast(typename V::Scalar a)
constexpr T MulSub(T a, T b, T c)
Simd< T, N > Normalize(Simd< T, N > a)
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)
std::pair< Simd< T, N >, Simd< T, N > > SinCos(Simd< T, N > a)
Simd< T, N > operator||(Simd< T, N > lhs, Simd< T, N > rhs)
Simd< T, N > operator&&(Simd< T, N > lhs, Simd< T, N > rhs)
V SimdMask(bool b0, bool b1, MoreBools... bs)
Simd< T, N/2 > GetHalf(Simd< T, N > a)
Simd< T, 4 > OrthogonalVector3(Simd< T, 4 > a)
Simd< T, N > VNorm(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)
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)
V VNearZero(V a, V epsilon)
int StoreSelected(T *ptr, Simd< MaskT, N > condition, Simd< T, N > values)
V Load(typename V::Scalar const *ptr)
Simd< T, N > Neg(Simd< T, N > a)
bool IsFinite(TransformRT const &a)
Simd< T, N > ShiftRight(Simd< T, N > a)
#define MOCHI_SIMD_MEMBERWISE_FALLBACK(T, N, FN, inVec)
#define MOCHI_DEFINE_MIXED_SIMD_SCALAR_OP(OP)
#define MOCHI_DEFINE_SIMD_OP_EQ(OP_EQ, OP)