From 2dd87289b932baccc6e382e17813a7d78f724a9a Mon Sep 17 00:00:00 2001
From: Xavier Valls <xaviervallspla@gmail.com>
Date: Thu, 19 Jul 2018 17:14:37 +0200
Subject: [PATCH] Discard unnecessary operations

---
 math/mathcore/inc/Fit/EvaluateChi2.hxx | 23 ++++-------------------
 math/mathcore/src/EvaluateChi2.cxx     |  4 ++--
 2 files changed, 6 insertions(+), 21 deletions(-)

diff --git a/math/mathcore/inc/Fit/EvaluateChi2.hxx b/math/mathcore/inc/Fit/EvaluateChi2.hxx
index c4ebd497674..61efb57de22 100644
--- a/math/mathcore/inc/Fit/EvaluateChi2.hxx
+++ b/math/mathcore/inc/Fit/EvaluateChi2.hxx
@@ -270,7 +270,7 @@ namespace FitUtil {
             fval = func(x, p);
             func.ParameterGradient(x, p, &gradFunc[0]);
 
-            validPointsMasks[i] = CheckInfNaNValues(fval);
+            validPointsMasks[i] = isFinite(fval);
             if (vecCore::MaskEmpty(validPointsMasks[i])) {
                // Return a zero contribution to all partial derivatives on behalf of the current points
                return pointContributionVec;
@@ -280,7 +280,7 @@ namespace FitUtil {
             for (unsigned int ipar = 0; ipar < npar; ++ipar) {
                // avoid singularity in the function (infinity and nan ) in the chi2 sum
                // eventually add possibility of excluding some points (like singularity)
-               validPointsMasks[i] = CheckInfNaNValues(gradFunc[ipar]);
+               validPointsMasks[i] = isFinite(gradFunc[ipar]);
 
                if (vecCore::MaskEmpty(validPointsMasks[i])) {
                   break; // exit loop on parameters
@@ -386,17 +386,9 @@ namespace FitUtil {
        // Compute a mask to filter out infinite numbers and NaN values.
       // The argument rval is updated so infinite numbers and NaN values are replaced by
       // maximum finite values (preserving the original sign).
-      static vecCore::Mask<T> CheckInfNaNValues(T &rval)
+      static vecCore::Mask<T> isFinite(T &rval)
       {
-         auto mask = rval > -vecCore::NumericLimits<T>::Max() && rval < vecCore::NumericLimits<T>::Max();
-
-         // Case +inf or nan
-         vecCore::MaskedAssign(rval, mask, +vecCore::NumericLimits<T>::Max());
-
-         // Case -inf
-         vecCore::MaskedAssign(rval, mask && rval < 0, -vecCore::NumericLimits<T>::Max());
-
-         return mask;
+         return rval > -vecCore::NumericLimits<T>::Max() && rval < vecCore::NumericLimits<T>::Max();
       }
 
    };
@@ -435,13 +427,6 @@ namespace FitUtil {
          return FitUtil::EvaluateChi2Residual(func, data, p, i, g);
       }
 
-        // Check if the value is a finite number. The argument rval is updated if it is infinite or NaN,
-        // setting it to the maximum finite value (preserving the sign).
-        inline bool CheckInfNaNValue(double &rval)
-        {
-            return std::isfinite(rval) ? rval : std::copysign(std::numeric_limits<double>::max(), rval);
-        }
-
    };
 
 } // end namespace FitUtil
diff --git a/math/mathcore/src/EvaluateChi2.cxx b/math/mathcore/src/EvaluateChi2.cxx
index f1be643c56c..ccfef9e4268 100644
--- a/math/mathcore/src/EvaluateChi2.cxx
+++ b/math/mathcore/src/EvaluateChi2.cxx
@@ -558,7 +558,7 @@ void FitUtil::EvaluateChi2Gradient(const IModelFunction &f, const BinData &data,
          std::cout << p[ipar] << "\t";
       std::cout << "\tfval = " << fval << std::endl;
 #endif
-      if (!CheckInfNaNValue(fval)) {
+      if (!std::isfinite(fval)) {
          isPointRejected[i] = true;
          // Return a zero contribution to all partial derivatives on behalf of the current point
          return pointContribution;
@@ -575,7 +575,7 @@ void FitUtil::EvaluateChi2Gradient(const IModelFunction &f, const BinData &data,
          // avoid singularity in the function (infinity and nan ) in the chi2 sum
          // eventually add possibility of excluding some points (like singularity)
          double dfval = gradFunc[ipar];
-         if (!CheckInfNaNValue(dfval)) {
+         if (!std::isfinite(dfval)) {
             break; // exit loop on parameters
          }
 
-- 
GitLab