aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans-Christoph Steiner <eighthave@users.sourceforge.net>2012-11-20 18:49:27 +0000
committerHans-Christoph Steiner <eighthave@users.sourceforge.net>2012-11-20 18:49:27 +0000
commitcb6781e80a8daebc0285ff3d1c8d8ab464182fca (patch)
tree6db2913dd9c510ef4a15d3890b10ad2b77a2d079
parent97c581bb050d83b7f1af814cb813ddb46d2fd137 (diff)
fix type-punning issues
svn path=/trunk/externals/sigpack/; revision=16564
-rw-r--r--freqshift~.c11
-rw-r--r--valverect~.c32
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 <math.h>
#include <string.h>
+#include <stdint.h>
#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)