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.
This commit is contained in:
Rasmus Munk Larsen
2020-10-07 19:05:18 +00:00
parent f66f3393e3
commit b431024404
7 changed files with 196 additions and 65 deletions

View File

@@ -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.*/
If either \a a or \a b are NaN, NaN is 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);
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
***************************************************************************/

View File

@@ -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

View File

@@ -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:
*

View File

@@ -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;