SVE: Add a 64-bit integer packet and the double transcendentals libeigen/eigen!2935 Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
diff --git a/Eigen/src/Core/arch/SVE/MathFunctions.h b/Eigen/src/Core/arch/SVE/MathFunctions.h index 1d127a3..5215537 100644 --- a/Eigen/src/Core/arch/SVE/MathFunctions.h +++ b/Eigen/src/Core/arch/SVE/MathFunctions.h
@@ -18,6 +18,7 @@ namespace internal { EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_FLOAT(PacketXf) +EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_DOUBLE(PacketXd) } // end namespace internal } // end namespace Eigen
diff --git a/Eigen/src/Core/arch/SVE/PacketMath.h b/Eigen/src/Core/arch/SVE/PacketMath.h index 07e024f..c58eb07 100644 --- a/Eigen/src/Core/arch/SVE/PacketMath.h +++ b/Eigen/src/Core/arch/SVE/PacketMath.h
@@ -341,6 +341,292 @@ } } +/********************************* int64 **************************************/ +// Keep typedef: Doxygen 1.13.2 misparses an attributed using declaration. +typedef svint64_t PacketXl __attribute__((arm_sve_vector_bits(EIGEN_ARM64_SVE_VL))); + +template <> +struct packet_traits<numext::int64_t> : default_packet_traits { + typedef PacketXl type; + typedef PacketXl half; // Half not implemented yet + enum { + Vectorizable = 1, + AlignedOnScalar = 1, + size = sve_packet_size_selector<numext::int64_t, EIGEN_ARM64_SVE_VL>::size, + + HasAdd = 1, + HasSub = 1, + HasShift = 1, + HasMul = 1, + HasNegate = 1, + HasAbs = 1, + HasArg = 0, + HasMin = 1, + HasMax = 1, + HasConj = 1, + // See the int32 traits above for why HasSetLinear stays 0. + HasSetLinear = 0, + HasReduxp = 0 // Not implemented in SVE + }; +}; + +template <> +struct unpacket_traits<PacketXl> { + typedef numext::int64_t type; + typedef PacketXl half; // Half not yet implemented + enum { + size = sve_packet_size_selector<numext::int64_t, EIGEN_ARM64_SVE_VL>::size, + alignment = sve_packet_alignment_selector<EIGEN_ARM64_SVE_VL>::alignment, + vectorizable = true, + masked_load_available = false, + masked_store_available = false + }; +}; + +template <> +EIGEN_STRONG_INLINE void prefetch<numext::int64_t>(const numext::int64_t* addr) { + svprfd(svptrue_b64(), addr, SV_PLDL1KEEP); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pset1<PacketXl>(const numext::int64_t& from) { + return svdup_n_s64(from); +} + +template <> +EIGEN_STRONG_INLINE PacketXl plset<PacketXl>(const numext::int64_t& a) { + return svindex_s64(a, 1); +} + +template <> +EIGEN_STRONG_INLINE PacketXl padd<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svadd_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl psub<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svsub_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pnegate(const PacketXl& a) { + return svneg_s64_x(svptrue_b64(), a); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pconj(const PacketXl& a) { + return a; +} + +template <> +EIGEN_STRONG_INLINE PacketXl pmul<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svmul_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pdiv<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svdiv_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pmadd(const PacketXl& a, const PacketXl& b, const PacketXl& c) { + return svmla_s64_x(svptrue_b64(), c, a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pmin<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svmin_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pmax<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svmax_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pcmp_le<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svdup_n_s64_z(svcmple_s64(svptrue_b64(), a, b), numext::int64_t(-1)); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pcmp_lt<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svdup_n_s64_z(svcmplt_s64(svptrue_b64(), a, b), numext::int64_t(-1)); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pcmp_eq<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svdup_n_s64_z(svcmpeq_s64(svptrue_b64(), a, b), numext::int64_t(-1)); +} + +template <> +EIGEN_STRONG_INLINE PacketXl ptrue<PacketXl>(const PacketXl& /*a*/) { + return svdup_n_s64_x(svptrue_b64(), numext::int64_t(-1)); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pzero<PacketXl>(const PacketXl& /*a*/) { + return svdup_n_s64_x(svptrue_b64(), 0); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pand<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svand_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl por<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svorr_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pxor<PacketXl>(const PacketXl& a, const PacketXl& b) { + return sveor_s64_x(svptrue_b64(), a, b); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pandnot<PacketXl>(const PacketXl& a, const PacketXl& b) { + return svbic_s64_x(svptrue_b64(), a, b); +} + +// See pselect<PacketXi>. +template <> +EIGEN_STRONG_INLINE PacketXl pselect<PacketXl>(const PacketXl& mask, const PacketXl& a, const PacketXl& b) { + return svsel_s64(svcmpne_n_s64(svptrue_b64(), mask, 0), a, b); +} + +template <int N> +EIGEN_STRONG_INLINE PacketXl parithmetic_shift_right(PacketXl a) { + // ASR, not ASRD: see parithmetic_shift_right<PacketXi>. + return svasr_n_s64_x(svptrue_b64(), a, N); +} + +template <int N> +EIGEN_STRONG_INLINE PacketXl plogical_shift_right(PacketXl a) { + return svreinterpret_s64_u64(svlsr_n_u64_x(svptrue_b64(), svreinterpret_u64_s64(a), N)); +} + +template <int N> +EIGEN_STRONG_INLINE PacketXl plogical_shift_left(PacketXl a) { + return svlsl_n_s64_x(svptrue_b64(), a, N); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pload<PacketXl>(const numext::int64_t* from) { + EIGEN_DEBUG_ALIGNED_LOAD return svld1_s64(svptrue_b64(), from); +} + +template <> +EIGEN_STRONG_INLINE PacketXl ploadu<PacketXl>(const numext::int64_t* from) { + EIGEN_DEBUG_UNALIGNED_LOAD return svld1_s64(svptrue_b64(), from); +} + +template <> +EIGEN_STRONG_INLINE PacketXl ploaddup<PacketXl>(const numext::int64_t* from) { + // Load the size/2 values this reads into the low half and interleave them + // with themselves: svzip1 only consumes the low halves of its operands. + // The predicate is exact rather than svptrue -- ploaddup may only touch + // size/2 elements, and a wider one would read past the end of the input. + constexpr uint64_t kHalf = uint64_t(packet_traits<numext::int64_t>::size) / 2; + svint64_t lo = svld1_s64(svwhilelt_b64(uint64_t(0), kHalf), from); + return svzip1_s64(lo, lo); +} + +template <> +EIGEN_STRONG_INLINE PacketXl ploadquad<PacketXl>(const numext::int64_t* from) { + // As ploaddup, one zip further: size/4 values, each repeated four times. + // At the smallest vector length size/4 rounds to zero, where one element + // still has to be read. + constexpr uint64_t kQuarter = numext::maxi(uint64_t(packet_traits<numext::int64_t>::size) / 4, uint64_t(1)); + svint64_t lo = svld1_s64(svwhilelt_b64(uint64_t(0), kQuarter), from); + lo = svzip1_s64(lo, lo); + return svzip1_s64(lo, lo); +} + +template <> +EIGEN_STRONG_INLINE void pstore<numext::int64_t>(numext::int64_t* to, const PacketXl& from) { + EIGEN_DEBUG_ALIGNED_STORE svst1_s64(svptrue_b64(), to, from); +} + +template <> +EIGEN_STRONG_INLINE void pstoreu<numext::int64_t>(numext::int64_t* to, const PacketXl& from) { + EIGEN_DEBUG_UNALIGNED_STORE svst1_s64(svptrue_b64(), to, from); +} + +template <> +EIGEN_DEVICE_FUNC inline PacketXl pgather<numext::int64_t, PacketXl>(const numext::int64_t* from, Index stride) { + // Index format: {base=0, base+stride, base+stride*2, base+stride*3, ...} + svint64_t indices = svindex_s64(0, stride); + return svld1_gather_s64index_s64(svptrue_b64(), from, indices); +} + +template <> +EIGEN_DEVICE_FUNC inline void pscatter<numext::int64_t, PacketXl>(numext::int64_t* to, const PacketXl& from, + Index stride) { + // Index format: {base=0, base+stride, base+stride*2, base+stride*3, ...} + svint64_t indices = svindex_s64(0, stride); + svst1_scatter_s64index_s64(svptrue_b64(), to, indices, from); +} + +template <> +EIGEN_STRONG_INLINE numext::int64_t pfirst<PacketXl>(const PacketXl& a) { + // svlasta returns the first element if all predicate bits are 0 + return svlasta_s64(svpfalse_b(), a); +} + +template <> +EIGEN_STRONG_INLINE PacketXl preverse(const PacketXl& a) { + return svrev_s64(a); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pabs(const PacketXl& a) { + return svabs_s64_x(svptrue_b64(), a); +} + +template <> +EIGEN_STRONG_INLINE numext::int64_t predux<PacketXl>(const PacketXl& a) { + return static_cast<numext::int64_t>(svaddv_s64(svptrue_b64(), a)); +} + +template <> +EIGEN_STRONG_INLINE numext::int64_t predux_mul<PacketXl>(const PacketXl& a) { + // Multiplying by the reverse pairs lane i with lane n-1-i, so every product of + // a pair appears in both halves; interleaving the halves then folds them + // together, halving the live span each round. At VL = 128 there are two lanes + // and the first multiply has already combined them. + PacketXl prod = svmul_s64_x(svptrue_b64(), a, svrev_s64(a)); + EIGEN_UNROLL_LOOP + for (int n = unpacket_traits<PacketXl>::size; n > 2; n >>= 1) { + prod = svmul_s64_x(svptrue_b64(), svzip1_s64(prod, prod), svzip2_s64(prod, prod)); + } + return pfirst<PacketXl>(prod); +} + +template <> +EIGEN_STRONG_INLINE numext::int64_t predux_min<PacketXl>(const PacketXl& a) { + return svminv_s64(svptrue_b64(), a); +} + +template <> +EIGEN_STRONG_INLINE numext::int64_t predux_max<PacketXl>(const PacketXl& a) { + return svmaxv_s64(svptrue_b64(), a); +} + +template <int N> +EIGEN_DEVICE_FUNC inline void ptranspose(PacketBlock<PacketXl, N>& kernel) { + EIGEN_STATIC_ASSERT((N & (N - 1)) == 0, EIGEN_INTERNAL_ERROR_PLEASE_FILE_A_BUG_REPORT); + for (int stride = N / 2; stride > 0; stride >>= 1) { + for (int block = 0; block < N; block += 2 * stride) { + for (int k = 0; k < stride; ++k) { + PacketXl lo = svzip1_s64(kernel.packet[block + k], kernel.packet[block + k + stride]); + PacketXl hi = svzip2_s64(kernel.packet[block + k], kernel.packet[block + k + stride]); + kernel.packet[block + k] = lo; + kernel.packet[block + k + stride] = hi; + } + } + } +} + /********************************* float32 ************************************/ // Keep typedef: Doxygen 1.13.2 misparses an attributed using declaration. @@ -712,11 +998,6 @@ // Keep typedef: Doxygen 1.13.2 misparses an attributed using declaration. typedef svfloat64_t PacketXd __attribute__((arm_sve_vector_bits(EIGEN_ARM64_SVE_VL))); -// The transcendentals stay at default_packet_traits' 0. Their generic -// implementations reach pfrexp/pldexp, which for double need the 64-bit integer -// packet this backend does not define -- add svint64_t and its integer_packet -// typedef first, as the float section does with PacketXi, and the whole suite -// follows through EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_DOUBLE. template <> struct packet_traits<double> : default_packet_traits { typedef PacketXd type; @@ -744,7 +1025,25 @@ HasDiv = 1, HasCmp = 1, HasSqrt = 1, - HasRsqrt = 1 + HasRsqrt = 1, + // asin and acos are absent from EIGEN_INSTANTIATE_GENERIC_MATH_FUNCS_DOUBLE, + // so they stay scalar. + // + // sin, cos and tan stay off despite being available: psin, pcos and ptan + // return the magnitude with the sign dropped under -ffast-math with GCC. + // Turning them on here would extend that to double, where the scalar path + // is correct today. tanh, exp, log, atan and cbrt are unaffected -- checked + // against the scalar reference under -ffast-math -- so they are on. See + // https://gitlab.com/libeigen/eigen/-/issues/3132. + HasATan = 1, + HasATanh = 1, + HasLog = 1, + HasLog1p = 1, + HasExpm1 = 1, + HasExp = 1, + HasPow = 1, + HasCbrt = 1, + HasTanh = EIGEN_FAST_MATH }; }; @@ -752,6 +1051,7 @@ struct unpacket_traits<PacketXd> { typedef double type; typedef PacketXd half; // Half not yet implemented + typedef PacketXl integer_packet; enum { size = sve_packet_size_selector<double, EIGEN_ARM64_SVE_VL>::size, @@ -1044,6 +1344,16 @@ } template <> +EIGEN_STRONG_INLINE PacketXd pfrexp<PacketXd>(const PacketXd& a, PacketXd& exponent) { + return pfrexp_generic(a, exponent); +} + +template <> +EIGEN_STRONG_INLINE PacketXd pldexp<PacketXd>(const PacketXd& a, const PacketXd& exponent) { + return pldexp_generic(a, exponent); +} + +template <> EIGEN_STRONG_INLINE PacketXd psqrt<PacketXd>(const PacketXd& a) { return svsqrt_f64_x(svptrue_b64(), a); }
diff --git a/Eigen/src/Core/arch/SVE/TypeCasting.h b/Eigen/src/Core/arch/SVE/TypeCasting.h index ec266e7..323cb35 100644 --- a/Eigen/src/Core/arch/SVE/TypeCasting.h +++ b/Eigen/src/Core/arch/SVE/TypeCasting.h
@@ -47,6 +47,36 @@ return svreinterpret_s32_f32(a); } +template <> +struct type_casting_traits<double, numext::int64_t> { + enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 }; +}; + +template <> +struct type_casting_traits<numext::int64_t, double> { + enum { VectorizedCast = 1, SrcCoeffRatio = 1, TgtCoeffRatio = 1 }; +}; + +template <> +EIGEN_STRONG_INLINE PacketXd pcast<PacketXl, PacketXd>(const PacketXl& a) { + return svcvt_f64_s64_x(svptrue_b64(), a); +} + +template <> +EIGEN_STRONG_INLINE PacketXl pcast<PacketXd, PacketXl>(const PacketXd& a) { + return svcvt_s64_f64_x(svptrue_b64(), a); +} + +template <> +EIGEN_STRONG_INLINE PacketXd preinterpret<PacketXd, PacketXl>(const PacketXl& a) { + return svreinterpret_f64_s64(a); +} + +template <> +EIGEN_STRONG_INLINE PacketXl preinterpret<PacketXl, PacketXd>(const PacketXd& a) { + return svreinterpret_s64_f64(a); +} + } // namespace internal } // namespace Eigen