From a20fc40e4ee2c50f635716ae844f44c5fdeb699a Mon Sep 17 00:00:00 2001 From: Rasmus Munk Larsen <4643818-rmlarsen1@users.noreply.gitlab.com> Date: Mon, 3 Nov 2025 18:59:51 +0000 Subject: [PATCH] Revert "simplify squaredNorm" This causes some subtle alignment-related bugs, which @chuckyschluz is currently investigating. See merge request libeigen/eigen!2055 --- Eigen/src/Core/Dot.h | 5 ++++- Eigen/src/Core/functors/UnaryFunctors.h | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/Eigen/src/Core/Dot.h b/Eigen/src/Core/Dot.h index a173306b4..059527c85 100644 --- a/Eigen/src/Core/Dot.h +++ b/Eigen/src/Core/Dot.h @@ -20,7 +20,10 @@ namespace internal { template ::Scalar> struct squared_norm_impl { using Real = typename NumTraits::Real; - static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Real run(const Derived& a) { return a.realView().cwiseAbs2().sum(); } + static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Real run(const Derived& a) { + Scalar result = a.unaryExpr(squared_norm_functor()).sum(); + return numext::real(result) + numext::imag(result); + } }; template diff --git a/Eigen/src/Core/functors/UnaryFunctors.h b/Eigen/src/Core/functors/UnaryFunctors.h index d7fc7bb4d..202995ff0 100644 --- a/Eigen/src/Core/functors/UnaryFunctors.h +++ b/Eigen/src/Core/functors/UnaryFunctors.h @@ -106,6 +106,26 @@ struct functor_traits> { }; }; +template ::IsComplex> +struct squared_norm_functor { + typedef Scalar result_type; + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(const Scalar& a) const { + return Scalar(numext::real(a) * numext::real(a), numext::imag(a) * numext::imag(a)); + } + template + EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(const Packet& a) const { + return Packet(pmul(a.v, a.v)); + } +}; +template +struct squared_norm_functor : scalar_abs2_op {}; + +template +struct functor_traits> { + using Real = typename NumTraits::Real; + enum { Cost = NumTraits::MulCost, PacketAccess = packet_traits::HasMul }; +}; + /** \internal * \brief Template functor to compute the conjugate of a complex value *