aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/GenericPacketMath.h
diff options
context:
space:
mode:
authorGravatar Rasmus Munk Larsen <rmlarsen@google.com>2020-10-01 16:54:31 +0000
committerGravatar Rasmus Munk Larsen <rmlarsen@google.com>2020-10-01 16:54:31 +0000
commit3b445d9bf2df77e7742f808125e826c7955e0b8b (patch)
tree04e9b42846d06cf4c754051cf713c608bd033a9e /Eigen/src/Core/GenericPacketMath.h
parent44b9d4e412b1afe110998aab3dff64d1c0ff0710 (diff)
Add a generic packet ops corresponding to {std}::fmin and {std}::fmax. The non-sensical NaN-propagation rules for std::min std::max implemented by pmin and pmax in Eigen is a longstanding source og confusion and bug report. This change is a first step towards addressing it, as discussing in issue #564.
Diffstat (limited to 'Eigen/src/Core/GenericPacketMath.h')
-rw-r--r--Eigen/src/Core/GenericPacketMath.h23
1 files changed, 21 insertions, 2 deletions
diff --git a/Eigen/src/Core/GenericPacketMath.h b/Eigen/src/Core/GenericPacketMath.h
index d63c8aaf4..075d18aa6 100644
--- a/Eigen/src/Core/GenericPacketMath.h
+++ b/Eigen/src/Core/GenericPacketMath.h
@@ -215,11 +215,13 @@ pmul(const bool& a, const bool& b) { return a && b; }
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) */
+/** \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. */
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) */
+/** \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.*/
template<typename Packet> EIGEN_DEVICE_FUNC inline Packet
pmax(const Packet& a, const Packet& b) { return numext::maxi(a, b); }
@@ -633,6 +635,23 @@ 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.
+*/
+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);
+}
+
+/** \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);
+}
+
/***************************************************************************
* The following functions might not have to be overwritten for vectorized types
***************************************************************************/