Blame include/cppunit/portability/FloatingPoint.h

Packit Service e31359
#ifndef CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED
Packit Service e31359
#define CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED
Packit Service e31359
Packit Service e31359
#include <cppunit/Portability.h>
Packit Service e31359
#include <math.h>
Packit Service e31359
Packit Service e31359
#if defined(__sun) && !defined(CPPUNIT_HAVE_ISFINITE) && defined(CPPUNIT_HAVE_FINITE)
Packit Service e31359
#include <ieeefp.h>
Packit Service e31359
  // <math.h> is still needed for usage of fabs in TestAssert.cpp
Packit Service e31359
#endif
Packit Service e31359
Packit Service e31359
CPPUNIT_NS_BEGIN
Packit Service e31359
Packit Service e31359
/// \brief Tests if a floating-point is a NaN.
Packit Service e31359
// According to IEEE-754 floating point standard, 
Packit Service e31359
// (see e.g. page 8 of
Packit Service e31359
// http://www.cs.berkeley.edu/~wkahan/ieee754status/ieee754.ps) 
Packit Service e31359
// all comparisons with NaN are false except "x != x", which is true.
Packit Service e31359
//
Packit Service e31359
// At least Microsoft Visual Studio 6 is known not to implement this test correctly.
Packit Service e31359
// It emits the following code to test equality:
Packit Service e31359
//  fcomp       qword ptr [nan]
Packit Service e31359
//  fnstsw      ax                        // copie fp (floating-point) status register to ax
Packit Service e31359
//  test        ah,40h                    // test bit 14 of ax (0x4000) => C3 of fp status register
Packit Service e31359
// According to the following documentation on the x86 floating point status register,
Packit Service e31359
// the C2 bit should be tested to test for NaN value. 
Packit Service e31359
// http://webster.cs.ucr.edu/AoA/Windows/HTML/RealArithmetic.html#1000117
Packit Service e31359
// In Microsoft Visual Studio 2003 & 2005, the test is implemented with:
Packit Service e31359
//  test        ah,44h         // Visual Studio 2005 test both C2 & C3...
Packit Service e31359
//
Packit Service e31359
// To work around this, a NaN is assumed to be detected if no strict ordering is found.
Packit Service e31359
inline bool floatingPointIsUnordered( double x )
Packit Service e31359
{
Packit Service e31359
   // x != x will detect a NaN on conformant platform
Packit Service e31359
   // (2.0 < x  &&  x < 1.0) will detect a NaN on non conformant platform:
Packit Service e31359
   // => no ordering can be found for x.
Packit Service e31359
   return  (x != x) ||  (2.0 < x  &&  x < 1.0);
Packit Service e31359
}
Packit Service e31359
Packit Service e31359
Packit Service e31359
/// \brief Tests if a floating-point is finite.
Packit Service e31359
/// @return \c true if x is neither a NaN, nor +inf, nor -inf, \c false otherwise.
Packit Service e31359
inline int floatingPointIsFinite( double x )
Packit Service e31359
{
Packit Service e31359
#if defined(CPPUNIT_HAVE_ISFINITE)
Packit Service e31359
   return isfinite( x );
Packit Service e31359
#elif defined(CPPUNIT_HAVE_FINITE)
Packit Service e31359
   return finite( x );
Packit Service e31359
#elif defined(CPPUNIT_HAVE__FINITE)
Packit Service e31359
   return _finite(x);
Packit Service e31359
#else
Packit Service e31359
   double testInf = x * 0.0;  // Produce 0.0 if x is finite, a NaN otherwise.
Packit Service e31359
   return testInf == 0.0  &&  !floatingPointIsUnordered(testInf);
Packit Service e31359
#endif
Packit Service e31359
}
Packit Service e31359
Packit Service e31359
CPPUNIT_NS_END
Packit Service e31359
Packit Service e31359
#endif // CPPUNIT_PORTABILITY_FLOATINGPOINT_H_INCLUDED