[eigen] fix double-promotion in 3.3 Core/SpecialFunctions.h

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Hi,

I'm trying out the current Eigen default branch ("almost-3.3"), compiling code that has been using Eigen 3.2 until now. 

I'm using the GCC warnings -Wdouble-promotion and -Wfloat-conversion as these are really helpful to uncover performance bugs on some of the ARM CPUs I am using (only single-precision FPU, use of double is very slow). 

This uncovered some slight bugs in Core/SpecialFunctions.h where some expressions were using double-precision when they should use floats, such as

extern/eigen3/Eigen/src/Core/SpecialFunctions.h:762:13: error: implicit conversion from 'float' to 'double' to match other operand of binary _expression_ [-Werror=double-promotion]
       c *= x/r;

extern/eigen3/Eigen/src/Core/SpecialFunctions.h:767:25: error: conversion to 'float' from 'double' may alter its value [-Werror=float-conversion]
     return (ans * ax / a);

Attached here is a small patch that fixes the issues I found. It was created against the default branch as of this morning. I'm not experienced with Mercurial so I hope i made the file correctly - please let me know if it should be done differently. 

Best regards,
Ola

# HG changeset patch
# User Ola Røer Thorsen <ola@xxxxxxxxxxxxxx>
# Date 1462351928 -7200
#      Wed May 04 10:52:08 2016 +0200
# Node ID 2d4ef1071d05a4c5af862abfd5bf0f0a7256f7fe
# Parent  6f65019205f194907df67118bb8a3477baf03fcc
fix double-promotion/float-conversion in Core/SpecialFunctions.h

diff -r 6f65019205f1 -r 2d4ef1071d05 Eigen/src/Core/SpecialFunctions.h
--- a/Eigen/src/Core/SpecialFunctions.h	Tue May 03 19:56:40 2016 -0700
+++ b/Eigen/src/Core/SpecialFunctions.h	Wed May 04 10:52:08 2016 +0200
@@ -284,7 +284,7 @@
     bool negative = false;
 
     const Scalar maxnum = NumTraits<Scalar>::infinity();
-    const Scalar m_pi(EIGEN_PI);
+    const Scalar m_pi = Scalar(EIGEN_PI);
 
     const Scalar zero = Scalar(0);
     const Scalar one = Scalar(1);
@@ -441,7 +441,7 @@
   EIGEN_DEVICE_FUNC
   static EIGEN_STRONG_INLINE float big() {
     // use epsneg (1.0 - epsneg == 1.0)
-    return 1.0 / (NumTraits<float>::epsilon() / 2);
+    return 1.0f / (NumTraits<float>::epsilon() / 2);
   }
 };
 
@@ -742,7 +742,7 @@
     const Scalar machep = igamma_helper<Scalar>::machep();
     const Scalar maxlog = numext::log(NumTraits<Scalar>::highest());
 
-    double ans, ax, c, r;
+    Scalar ans, ax, c, r;
 
     /* Compute  x**a * exp(-x) / gamma(a)  */
     ax = a * numext::log(x) - x - lgamma_impl<Scalar>::run(a);


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/