aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core
diff options
context:
space:
mode:
authorGravatar Rasmus Munk Larsen <rmlarsen@google.com>2020-10-07 19:05:18 +0000
committerGravatar Rasmus Munk Larsen <rmlarsen@google.com>2020-10-07 19:05:18 +0000
commitb43102440489df9d0175c88e602dfa425b574a94 (patch)
tree9325c3401de7047451d4a59ad343cdf1c5a83679 /Eigen/src/Core
parentf66f3393e3d567e5c8b138fbad69b316214a4ce9 (diff)
Don't make assumptions about NaN-propagation for pmin/pmax - it various across platforms.
Change test to only test for NaN-propagation for pfmin/pfmax.
Diffstat (limited to 'Eigen/src/Core')
-rw-r--r--Eigen/src/Core/GenericPacketMath.h55
-rw-r--r--Eigen/src/Core/functors/BinaryFunctors.h58
-rw-r--r--Eigen/src/Core/util/Constants.h15
-rw-r--r--Eigen/src/Core/util/ForwardDeclarations.h4
4 files changed, 104 insertions, 28 deletions
diff --git a/Eigen/src/Core/GenericPacketMath.h b/Eigen/src/Core/GenericPacketMath.h
index 075d18aa6..b5eb1cf99 100644
--- a/Eigen/src/Core/GenericPacketMath.h
+++ b/Eigen/src/Core/GenericPacketMath.h
@@ -216,12 +216,12 @@ template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
pdiv(const Packet& a, const Packet& b) { return a/b; }
/** \internal \returns the min of \a a and \a b (coeff-wise).
-Equivalent to std::min(a, b), so if either a or b is NaN, a is returned. */
+ If \a a or \b b is NaN, the return value is implementation defined. */
template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
pmin(const Packet& a, const Packet& b) { return numext::mini(a, b); }
/** \internal \returns the max of \a a and \a b (coeff-wise)
-Equivalent to std::max(a, b), so if either a or b is NaN, a is returned.*/
+ If \a a or \b b is NaN, the return value is implementation defined. */
template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
pmax(const Packet& a, const Packet& b) { return numext::maxi(a, b); }
@@ -635,23 +635,54 @@ Packet print(const Packet& a) { using numext::rint; return rint(a); }
template<typename Packet> EIGEN_DECLARE_FUNCTION_ALLOWING_MULTIPLE_DEFINITIONS
Packet pceil(const Packet& a) { using numext::ceil; return ceil(a); }
-/** \internal \returns the min of \a a and \a b (coeff-wise)
- Equivalent to std::fmin(a, b). Only if both a and b are NaN is NaN returned.
-*/
+
+/** \internal \returns the max of \a a and \a b (coeff-wise)
+ If both \a a and \a b are NaN, NaN is returned.
+ Equivalent to std::fmax(a, b). */
+template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
+pfmax(const Packet& a, const Packet& b) {
+ Packet not_nan_mask_a = pcmp_eq(a, a);
+ Packet not_nan_mask_b = pcmp_eq(b, b);
+ return pselect(not_nan_mask_a,
+ pselect(not_nan_mask_b, pmax(a, b), a),
+ b);
+}
+
+/** \internal \returns the min of \a a and \a b (coeff-wise)
+ If both \a a and \a b are NaN, NaN is returned.
+ Equivalent to std::fmin(a, b). */
template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
pfmin(const Packet& a, const Packet& b) {
- Packet not_nan_mask = pcmp_eq(a, a);
- return pselect(not_nan_mask, pmin(a, b), b);
+ Packet not_nan_mask_a = pcmp_eq(a, a);
+ Packet not_nan_mask_b = pcmp_eq(b, b);
+ return pselect(not_nan_mask_a,
+ pselect(not_nan_mask_b, pmin(a, b), a),
+ b);
}
/** \internal \returns the max of \a a and \a b (coeff-wise)
- Equivalent to std::fmax(a, b). Only if both a and b are NaN is NaN returned.*/
-template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
-pfmax(const Packet& a, const Packet& b) {
- Packet not_nan_mask = pcmp_eq(a, a);
- return pselect(not_nan_mask, pmax(a, b), b);
+ If either \a a or \a b are NaN, NaN is returned. */
+template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
+pfmax_nan(const Packet& a, const Packet& b) {
+ Packet not_nan_mask_a = pcmp_eq(a, a);
+ Packet not_nan_mask_b = pcmp_eq(b, b);
+ return pselect(not_nan_mask_a,
+ pselect(not_nan_mask_b, pmax(a, b), b),
+ a);
+}
+
+/** \internal \returns the min of \a a and \a b (coeff-wise)
+ If either \a a or \a b are NaN, NaN is returned. */
+template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
+pfmin_nan(const Packet& a, const Packet& b) {
+ Packet not_nan_mask_a = pcmp_eq(a, a);
+ Packet not_nan_mask_b = pcmp_eq(b, b);
+ return pselect(not_nan_mask_a,
+ pselect(not_nan_mask_b, pmin(a, b), b),
+ a);
}
+
/***************************************************************************
* The following functions might not have to be overwritten for vectorized types
***************************************************************************/
diff --git a/Eigen/src/Core/functors/BinaryFunctors.h b/Eigen/src/Core/functors/BinaryFunctors.h
index d8b7b1eba..55650bb8d 100644
--- a/Eigen/src/Core/functors/BinaryFunctors.h
+++ b/Eigen/src/Core/functors/BinaryFunctors.h
@@ -134,21 +134,39 @@ struct functor_traits<scalar_conj_product_op<LhsScalar,RhsScalar> > {
*
* \sa class CwiseBinaryOp, MatrixBase::cwiseMin, class VectorwiseOp, MatrixBase::minCoeff()
*/
-template<typename LhsScalar,typename RhsScalar>
+template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
struct scalar_min_op : binary_op_base<LhsScalar,RhsScalar>
{
typedef typename ScalarBinaryOpTraits<LhsScalar,RhsScalar,scalar_min_op>::ReturnType result_type;
EIGEN_EMPTY_STRUCT_CTOR(scalar_min_op)
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return numext::mini(a, b); }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const {
+ if (NaNPropagation == PropagateFast) {
+ return numext::mini(a, b);
+ } else if (NaNPropagation == PropagateNumbers) {
+ return internal::pfmin(a,b);
+ } else if (NaNPropagation == PropagateNaN) {
+ return internal::pfmin_nan(a,b);
+ }
+ }
template<typename Packet>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const
- { return internal::pmin(a,b); }
+ {
+ if (NaNPropagation == PropagateFast) {
+ return internal::pmin(a,b);
+ } else if (NaNPropagation == PropagateNumbers) {
+ return internal::pfmin(a,b);
+ } else if (NaNPropagation == PropagateNaN) {
+ return internal::pfmin_nan(a,b);
+ }
+ }
+ // TODO(rmlarsen): Handle all NaN propagation semantics reductions.
template<typename Packet>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const
{ return internal::predux_min(a); }
};
-template<typename LhsScalar,typename RhsScalar>
-struct functor_traits<scalar_min_op<LhsScalar,RhsScalar> > {
+
+template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
+struct functor_traits<scalar_min_op<LhsScalar,RhsScalar, NaNPropagation> > {
enum {
Cost = (NumTraits<LhsScalar>::AddCost+NumTraits<RhsScalar>::AddCost)/2,
PacketAccess = internal::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMin
@@ -160,21 +178,39 @@ struct functor_traits<scalar_min_op<LhsScalar,RhsScalar> > {
*
* \sa class CwiseBinaryOp, MatrixBase::cwiseMax, class VectorwiseOp, MatrixBase::maxCoeff()
*/
-template<typename LhsScalar,typename RhsScalar>
-struct scalar_max_op : binary_op_base<LhsScalar,RhsScalar>
+template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
+struct scalar_max_op : binary_op_base<LhsScalar,RhsScalar>
{
typedef typename ScalarBinaryOpTraits<LhsScalar,RhsScalar,scalar_max_op>::ReturnType result_type;
EIGEN_EMPTY_STRUCT_CTOR(scalar_max_op)
- EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const { return numext::maxi(a, b); }
+ EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type operator() (const LhsScalar& a, const RhsScalar& b) const {
+ if (NaNPropagation == PropagateFast) {
+ return numext::maxi(a, b);
+ } else if (NaNPropagation == PropagateNumbers) {
+ return internal::pfmax(a,b);
+ } else if (NaNPropagation == PropagateNaN) {
+ return internal::pfmax_nan(a,b);
+ }
+ }
template<typename Packet>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(const Packet& a, const Packet& b) const
- { return internal::pmax(a,b); }
+ {
+ if (NaNPropagation == PropagateFast) {
+ return internal::pmax(a,b);
+ } else if (NaNPropagation == PropagateNumbers) {
+ return internal::pfmax(a,b);
+ } else if (NaNPropagation == PropagateNaN) {
+ return internal::pfmax_nan(a,b);
+ }
+ }
+ // TODO(rmlarsen): Handle all NaN propagation semantics reductions.
template<typename Packet>
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE result_type predux(const Packet& a) const
{ return internal::predux_max(a); }
};
-template<typename LhsScalar,typename RhsScalar>
-struct functor_traits<scalar_max_op<LhsScalar,RhsScalar> > {
+
+template<typename LhsScalar,typename RhsScalar, int NaNPropagation>
+struct functor_traits<scalar_max_op<LhsScalar,RhsScalar, NaNPropagation> > {
enum {
Cost = (NumTraits<LhsScalar>::AddCost+NumTraits<RhsScalar>::AddCost)/2,
PacketAccess = internal::is_same<LhsScalar, RhsScalar>::value && packet_traits<LhsScalar>::HasMax
diff --git a/Eigen/src/Core/util/Constants.h b/Eigen/src/Core/util/Constants.h
index 7ada82195..ad9af5727 100644
--- a/Eigen/src/Core/util/Constants.h
+++ b/Eigen/src/Core/util/Constants.h
@@ -328,12 +328,21 @@ enum StorageOptions {
* Enum for specifying whether to apply or solve on the left or right. */
enum SideType {
/** Apply transformation on the left. */
- OnTheLeft = 1,
+ OnTheLeft = 1,
/** Apply transformation on the right. */
- OnTheRight = 2
+ OnTheRight = 2
};
-
+/** \ingroup enums
+ * Enum for specifying NaN-propagation behavior, e.g. for coeff-wise min/max. */
+enum NaNPropagationOptions {
+ /** Implementation defined behavior if NaNs are present. */
+ PropagateFast = 0,
+ /** Always propagate NaNs. */
+ PropagateNaN,
+ /** Always propagate not-NaNs. */
+ PropagateNumbers
+};
/* the following used to be written as:
*
diff --git a/Eigen/src/Core/util/ForwardDeclarations.h b/Eigen/src/Core/util/ForwardDeclarations.h
index 208b96c9c..2f9cc4491 100644
--- a/Eigen/src/Core/util/ForwardDeclarations.h
+++ b/Eigen/src/Core/util/ForwardDeclarations.h
@@ -180,8 +180,8 @@ template<typename LhsScalar, typename RhsScalar, bool ConjLhs=false, bool ConjRh
template<typename LhsScalar,typename RhsScalar=LhsScalar> struct scalar_sum_op;
template<typename LhsScalar,typename RhsScalar=LhsScalar> struct scalar_difference_op;
template<typename LhsScalar,typename RhsScalar=LhsScalar> struct scalar_conj_product_op;
-template<typename LhsScalar,typename RhsScalar=LhsScalar> struct scalar_min_op;
-template<typename LhsScalar,typename RhsScalar=LhsScalar> struct scalar_max_op;
+template<typename LhsScalar,typename RhsScalar=LhsScalar, int NaNPropagation=PropagateFast> struct scalar_min_op;
+template<typename LhsScalar,typename RhsScalar=LhsScalar, int NaNPropagation=PropagateFast> struct scalar_max_op;
template<typename Scalar> struct scalar_opposite_op;
template<typename Scalar> struct scalar_conjugate_op;
template<typename Scalar> struct scalar_real_op;