From cb6781e80a8daebc0285ff3d1c8d8ab464182fca Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Tue, 20 Nov 2012 18:49:27 +0000 Subject: fix type-punning issues svn path=/trunk/externals/sigpack/; revision=16564 --- freqshift~.c | 11 +++++++++++ valverect~.c | 32 ++++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/freqshift~.c b/freqshift~.c index 026e4cb..fe99884 100644 --- a/freqshift~.c +++ b/freqshift~.c @@ -99,11 +99,22 @@ static float f_clamp(float x, float a, float b) return x; } +static int f_round(t_float f) { +#if PD_FLOAT_PRECISION == 64 + return (int)lround(f); +#else + return (int)lroundf(f); +#endif +} + +// this relies on type-punning, which is not allowed in C99 or 64-bit +#if 0 // Round float to int using IEEE int* hack static int f_round(float f) { f += (3<<22); return *((int*)&f) - 0x4b400000; } +#endif // Cubic interpolation function static float cube_interp(const float fr, const float inm1, const float diff --git a/valverect~.c b/valverect~.c index 246878c..37ac536 100644 --- a/valverect~.c +++ b/valverect~.c @@ -7,6 +7,7 @@ #include "m_pd.h" #include #include +#include #ifdef _MSC_VER #pragma warning( disable : 4244 ) #pragma warning( disable : 4305 ) @@ -65,19 +66,30 @@ static void *valverect_tilde_new(t_floatarg sag, t_floatarg dist) } /* Andrew Simper's pow(2, x) aproximation from the music-dsp list */ -static float f_pow2(float x) + +/* 32 bit "pointer cast" union */ +typedef union { + float f; + int32_t i; +} ls_pcast32; + +/* union version */ +static inline float f_pow2(float x) { - long *px = (long*)(&x); // store address of float as long pointer - const float tx = (x-0.5f) + (3<<22); // temporary value for truncation - const long lx = *((long*)&tx) - 0x4b400000; // integer power of 2 - const float dx = x-(float)(lx); // float remainder of power of 2 + ls_pcast32 *px, tx, lx; + float dx; + + px = (ls_pcast32 *)&x; // store address of float as long pointer + tx.f = (x-0.5f) + (3<<22); // temporary value for truncation + lx.i = tx.i - 0x4b400000; // integer power of 2 + dx = x - (float)lx.i; // float remainder of power of 2 - x = 1.0f + dx*(0.6960656421638072f + // cubic apporoximation of 2^x - dx*(0.224494337302845f + // for x in the range [0, 1] - dx*(0.07944023841053369f))); - *px += (lx<<23); // add integer power of 2 to exponent + x = 1.0f + dx * (0.6960656421638072f + // cubic apporoximation of 2^x + dx * (0.224494337302845f + // for x in the range [0, 1] + dx * (0.07944023841053369f))); + (*px).i += (lx.i << 23); // add integer power of 2 to exponent - return x; + return (*px).f; } static t_int *valverect_tilde_perform(t_int *w) -- cgit v1.2.1