Skip to content

Commit

Permalink
refactor inf/nan values
Browse files Browse the repository at this point in the history
  • Loading branch information
camilo committed Nov 22, 2024
1 parent ff141e1 commit 2bcc907
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 26 deletions.
16 changes: 9 additions & 7 deletions include/qffmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern "C" {
#define QLIB_ISINF qFFMath_IsInf
#define QLIB_MAX qFFMath_Max
#define QLIB_MIN qFFMath_Min
#define QLIB_NAN QFFM_NAN
#define QLIB_NAN ( qFFMath_NotANumber[ 0 ] )
#define QLIB_FMOD qFFMath_Mod
#define QLIB_ROUND qFFMath_Round
/*! @endcond */
Expand All @@ -58,10 +58,6 @@ extern "C" {
* @{
*/

/*! @cond */
float _qFFMath_GetAbnormal( const int i ); //skipcq: CXX-E2000
/*! @endcond */

/** @brief The base of natural logarithms ( e ) given as a single-precision floating-point number*/
#define QFFM_E ( 2.7182818284590452354F )
/** @brief The base 2 logarithm of e ( log_2 e ) given as a single-precision floating-point number */
Expand Down Expand Up @@ -101,9 +97,9 @@ extern "C" {
/** @brief The maximum value of a non-infinite single-precision floating-point number */
#define QFFM_MAXFLOAT ( 3.40282347e+38F )
/** @brief Positive infinity given as a single-precision floating-point number */
#define QFFM_INFINITY _qFFMath_GetAbnormal( 0 )
#define QFFM_INFINITY ( qFFMath_Infinity[ 0 ] )
/** @brief Not a Number (NaN) given as a single-precision floating-point number */
#define QFFM_NAN _qFFMath_GetAbnormal( 1 )
#define QFFM_NAN ( qFFMath_NotANumber[ 0 ] )

/** @brief Indicates that the value is positive or negative zero */
#define QFFM_FP_ZERO ( 0 )
Expand Down Expand Up @@ -1010,6 +1006,12 @@ extern "C" {
float qFFMath_Sph_legendre( size_t l,
size_t m,
float theta );

/*! @cond */
extern const float * const qFFMath_Infinity;
extern const float * const qFFMath_NotANumber;
/*! @endcond */

#endif /*#ifdef QLIBS_USE_STD_MATH*/

/** @}*/
Expand Down
2 changes: 2 additions & 0 deletions include/qinterp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ extern "C" {
QINTERP1_HERMITE, /*!< Piecewise cubic Hermite interpolation.*/
QINTERP1_SPLINE, /*!< Catmull spline interpolation.*/
QINTERP1_CONSTRAINED_SPLINE, /*!< A special kind of spline that doesn't overshoot.*/
/*! @cond */
QINTERP1_MAX,
/*! @endcond */
} qInterp1Method_t;


Expand Down
30 changes: 11 additions & 19 deletions qffmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
#include <string.h>
#include <float.h>

static const union {
uint32_t u[ 2 ];
float f[ 2 ];
} s_values = { { 0x7F800000U, 0x7FBFFFFFU } };
const float * const qFFMath_Infinity = &s_values.f[ 0 ];
const float * const qFFMath_NotANumber = &s_values.f[ 1 ];

/*cppcheck-suppress misra-c2012-20.7 */
#define cast_reinterpret( dst, src, dst_type ) \
(void)memcpy( &dst, &src, sizeof(dst_type) ) \
Expand Down Expand Up @@ -78,21 +85,6 @@ static float cyl_bessel_ij_series( float nu,
float sgn,
size_t max_iter );

/*============================================================================*/
float _qFFMath_GetAbnormal( const int i )
{
static const uint32_t u_ab[ 2 ] = { 0x7F800000U, 0x7FBFFFFFU };
static float f_ab[ 2 ] = { 0.0F, 0.0F };
static bool init = true;

if ( init ) {
/*cppcheck-suppress misra-c2012-21.15 */
(void)memcpy( f_ab, u_ab, sizeof(f_ab) );
init = false;
}

return f_ab[ i ];
}
/*============================================================================*/
int qFFMath_FPClassify( const float f )
{
Expand Down Expand Up @@ -1511,7 +1503,7 @@ float qFFMath_Beta( float x,
{
float result;
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsNaN( x ) || qFFMath_IsNaN( y ) ) { //no side effects here
if ( qFFMath_IsNaN( x ) || qFFMath_IsNaN( y ) ) { /*no side effects*/
result = QFFM_NAN;
}
else {
Expand Down Expand Up @@ -1808,7 +1800,7 @@ float qFFMath_Comp_ellint_3( float k,
{
float y;
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( nu ) || ( qFFMath_Abs( k ) > 1.0F ) ) { //no side effects here
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( nu ) || ( qFFMath_Abs( k ) > 1.0F ) ) { /*no side effects*/
y = QFFM_NAN;
}
else if ( qFFMath_IsEqual( 1.0F, nu ) ) {
Expand All @@ -1829,7 +1821,7 @@ float qFFMath_Ellint_1( float k,
{
float y;
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( phi ) || ( qFFMath_Abs( k ) > 1.0F ) ) { //no side effects here
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( phi ) || ( qFFMath_Abs( k ) > 1.0F ) ) { /*no side effects*/
y = QFFM_NAN;
}
else {
Expand All @@ -1854,7 +1846,7 @@ float qFFMath_Ellint_2( float k,
{
float y;
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( phi ) || ( qFFMath_Abs( k ) > 1.0F ) ) { //no side effects here
if ( qFFMath_IsNaN( k ) || qFFMath_IsNaN( phi ) || ( qFFMath_Abs( k ) > 1.0F ) ) { /*no side effects*/
y = QFFM_NAN;
}
else {
Expand Down

0 comments on commit 2bcc907

Please sign in to comment.