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 --- valverect~.c | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'valverect~.c') 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