From 56d0910533efbcf8589a1e1fca967dbeefbba026 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Mon, 8 Dec 2014 12:20:47 -0500 Subject: [PATCH 1/6] Cleanup fmaf() usage, move to lmms_math.h --- include/interpolation.h | 24 +++--------------------- include/lmms_math.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/include/interpolation.h b/include/interpolation.h index 5dd98ea3f..cbe274d42 100644 --- a/include/interpolation.h +++ b/include/interpolation.h @@ -32,6 +32,7 @@ #include #include "lmms_constants.h" +#include "lmms_math.h" inline float hermiteInterpolate( float x0, float x1, float x2, float x3, float frac_pos ) @@ -80,32 +81,13 @@ inline float cubicInterpolate( float v0, float v1, float v2, float v3, float x ) inline float cosinusInterpolate( float v0, float v1, float x ) { const float f = ( 1.0f - cosf( x * F_PI ) ) * 0.5f; -#ifdef FP_FAST_FMAF - #ifndef __clang__ - return fmaf( f, v1-v0, v0 ); - #else - return fma( f, v1-v0, v0 ); - #endif -#else - return f * (v1-v0) + v0; -#endif -// return( v0*f + v1*( 1.0f-f ) ); + return fastFmaf( f, v1-v0, v0 ); } inline float linearInterpolate( float v0, float v1, float x ) { -// take advantage of fma function if present in hardware - -#ifdef FP_FAST_FMAF - #ifndef __clang__ - return fmaf( x, v1-v0, v0 ); - #else - return fma( x, v1-v0, v0 ); - #endif -#else - return x * (v1-v0) + v0; -#endif + return fastFmaf( x, v1-v0, v0 ); } diff --git a/include/lmms_math.h b/include/lmms_math.h index 47136ae94..c82d32ac5 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -130,7 +130,40 @@ static inline int fast_rand() return( (unsigned)( next / 65536 ) % 32768 ); } +//! @brief Takes advantage of fmal() function if present in hardware +static inline long fastFmal( long a, long b, long c) { +#ifdef FP_FAST_FMAF + #ifdef __clang__ + return fma(a, b, c); + #else + return fmal(a, b, c); + #endif +#else + return a * ( b - c ) + c; +#endif +} +//! @brief Takes advantage of fmaf() function if present in hardware +static inline float fastFmaf( float a, float b, float c) { +#ifdef FP_FAST_FMAF + #ifdef __clang__ + return fma(a, b, c); + #else + return fmaf(a, b, c); + #endif +#else + return a * ( b - c ) + c; +#endif +} + +//! @brief Takes advantage of fma() function if present in hardware +static inline int fastFma( int a, int b, int c) { +#ifdef FP_FAST_FMAF + return fma(a, b, c); +#else + return a * ( b - c ) + c; +#endif +} // source: http://martin.ankerl.com/2007/10/04/optimized-pow-approximation-for-java-and-c-c/ static inline double fastPow( double a, double b ) From 591acbf73264620de26bb90ec32a5c183be98c0d Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Mon, 8 Dec 2014 12:22:33 -0500 Subject: [PATCH 2/6] fastFmaf() formatting fixes --- include/lmms_math.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index c82d32ac5..8c7d07cb0 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -136,7 +136,7 @@ static inline long fastFmal( long a, long b, long c) { #ifdef __clang__ return fma(a, b, c); #else - return fmal(a, b, c); + return fmal( a, b, c ); #endif #else return a * ( b - c ) + c; @@ -147,9 +147,9 @@ static inline long fastFmal( long a, long b, long c) { static inline float fastFmaf( float a, float b, float c) { #ifdef FP_FAST_FMAF #ifdef __clang__ - return fma(a, b, c); + return fma( a, b, c ); #else - return fmaf(a, b, c); + return fmaf( a, b, c ); #endif #else return a * ( b - c ) + c; @@ -159,7 +159,7 @@ static inline float fastFmaf( float a, float b, float c) { //! @brief Takes advantage of fma() function if present in hardware static inline int fastFma( int a, int b, int c) { #ifdef FP_FAST_FMAF - return fma(a, b, c); + return fma( a, b, c ); #else return a * ( b - c ) + c; #endif From 76d766fe252f55668ab7d1a21bb889d2de8e110c Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Mon, 8 Dec 2014 12:30:16 -0500 Subject: [PATCH 3/6] fmaf() code cleanup, typos --- include/lmms_math.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 8c7d07cb0..393a16d8b 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -131,7 +131,7 @@ static inline int fast_rand() } //! @brief Takes advantage of fmal() function if present in hardware -static inline long fastFmal( long a, long b, long c) { +static inline long fastFmal( long a, long b, long c ) { #ifdef FP_FAST_FMAF #ifdef __clang__ return fma(a, b, c); @@ -139,12 +139,12 @@ static inline long fastFmal( long a, long b, long c) { return fmal( a, b, c ); #endif #else - return a * ( b - c ) + c; + return a * b + c; #endif } //! @brief Takes advantage of fmaf() function if present in hardware -static inline float fastFmaf( float a, float b, float c) { +static inline float fastFmaf( float a, float b, float c ) { #ifdef FP_FAST_FMAF #ifdef __clang__ return fma( a, b, c ); @@ -152,16 +152,16 @@ static inline float fastFmaf( float a, float b, float c) { return fmaf( a, b, c ); #endif #else - return a * ( b - c ) + c; + return a * b + c; #endif } //! @brief Takes advantage of fma() function if present in hardware -static inline int fastFma( int a, int b, int c) { +static inline int fastFma( int a, int b, int c ) { #ifdef FP_FAST_FMAF return fma( a, b, c ); #else - return a * ( b - c ) + c; + return a * b + c; #endif } From 5a0dfdd3c0b63704130f6ed287ccd8e3521c73e6 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Mon, 8 Dec 2014 12:45:31 -0500 Subject: [PATCH 4/6] minor fmaf() formatting fix. --- include/lmms_math.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 393a16d8b..ac8ab6fdc 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -134,7 +134,7 @@ static inline int fast_rand() static inline long fastFmal( long a, long b, long c ) { #ifdef FP_FAST_FMAF #ifdef __clang__ - return fma(a, b, c); + return fma( a, b, c ); #else return fmal( a, b, c ); #endif From 7431e772294a6d5455cfd1a6d45750bd26ac4d4e Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Mon, 8 Dec 2014 14:42:19 -0500 Subject: [PATCH 5/6] More fmal() fixes --- include/lmms_math.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index ac8ab6fdc..156303136 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -131,7 +131,7 @@ static inline int fast_rand() } //! @brief Takes advantage of fmal() function if present in hardware -static inline long fastFmal( long a, long b, long c ) { +static inline long double fastFmal( long double a, long double b, long double c ) { #ifdef FP_FAST_FMAF #ifdef __clang__ return fma( a, b, c ); @@ -157,7 +157,7 @@ static inline float fastFmaf( float a, float b, float c ) { } //! @brief Takes advantage of fma() function if present in hardware -static inline int fastFma( int a, int b, int c ) { +static inline double fastFma( double a, double b, double c ) { #ifdef FP_FAST_FMAF return fma( a, b, c ); #else From bbe337bd03299a37b27320f3995f27739733aa60 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 9 Dec 2014 08:28:00 -0500 Subject: [PATCH 6/6] Use correct FMA_ macros --- include/lmms_math.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/lmms_math.h b/include/lmms_math.h index 156303136..3d23abcd3 100644 --- a/include/lmms_math.h +++ b/include/lmms_math.h @@ -132,7 +132,7 @@ static inline int fast_rand() //! @brief Takes advantage of fmal() function if present in hardware static inline long double fastFmal( long double a, long double b, long double c ) { -#ifdef FP_FAST_FMAF +#ifdef FP_FAST_FMAL #ifdef __clang__ return fma( a, b, c ); #else @@ -158,7 +158,7 @@ static inline float fastFmaf( float a, float b, float c ) { //! @brief Takes advantage of fma() function if present in hardware static inline double fastFma( double a, double b, double c ) { -#ifdef FP_FAST_FMAF +#ifdef FP_FAST_FMA return fma( a, b, c ); #else return a * b + c;