]> git.mxchange.org Git - simgear.git/commitdiff
Add some fast math functions
authorehofman <ehofman>
Sat, 28 Jun 2003 12:06:09 +0000 (12:06 +0000)
committerehofman <ehofman>
Sat, 28 Jun 2003 12:06:09 +0000 (12:06 +0000)
simgear/math/Makefile.am
simgear/math/fastmath.cxx [new file with mode: 0644]
simgear/math/fastmath.hxx [new file with mode: 0644]
simgear/sound/sound.cxx

index c319213c0da6fdbac49128f3251f74d24745a5b3..36747b2d883b03d57250d12a94e92f3d26b2f000 100644 (file)
@@ -12,7 +12,8 @@ include_HEADERS = \
        sg_memory.h \
        sg_random.h \
        sg_types.hxx \
-       vector.hxx
+       vector.hxx \
+       fastmath.hxx
 
 EXTRA_DIST = linintp2.h linintp2.inl sphrintp.h sphrintp.inl
 
@@ -22,6 +23,7 @@ libsgmath_a_SOURCES = \
        polar3d.cxx \
        sg_geodesy.cxx \
        sg_random.c \
-       vector.cxx
+       vector.cxx \
+       fastmath.cxx
 
 INCLUDES = -I$(top_srcdir)
diff --git a/simgear/math/fastmath.cxx b/simgear/math/fastmath.cxx
new file mode 100644 (file)
index 0000000..ce8e253
--- /dev/null
@@ -0,0 +1,94 @@
+/*
+ * \file fastmath.cxx
+ * fast mathematics routines.
+ *
+ * Refferences:
+ *
+ * A Fast, Compact Approximation of the Exponential Function
+ * Nicol N. Schraudolph
+ * IDSIA, Lugano, Switzerland
+ * http://www.inf.ethz.ch/~schraudo/pubs/exp.pdf
+ *
+ * Fast log() Function, by Laurent de Soras:
+ * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-Fastlogfunction&forum=totd&id=-1
+ *
+ */
+
+/*
+ * $Id$
+ */
+
+
+
+#include <fastmath.hxx>
+
+/**
+ * This function is on avarage 9 times faster than the system exp() function
+ * and has an error of about 1.5%
+ */
+static union {
+    double d;
+    struct {
+#if BYTE_ORDER == BIG_ENDIAN
+        int i, j;
+#else
+        int j, i;
+#endif
+    } n;
+} _eco;
+
+double fast_exp(double val) {
+    const double a = 1048576/M_LN2;
+    const double b_c = 1072632447; /* 1072693248 - 60801 */
+
+    _eco.n.i = a*val + b_c;
+
+    return _eco.d;
+}
+
+
+double fast_log2 (double val)
+{
+   int * const    exp_ptr = reinterpret_cast <int *> (&val);
+   int            x = *exp_ptr;
+   const int      log_2 = ((x >> 23) & 255) - 128;
+   x &= ~(255 << 23);
+   x += 127 << 23;
+   *exp_ptr = x;
+
+   val = ((-1.0f/3) * val + 2) * val - 2.0f/3;   // (1)
+
+   return (val + log_2);
+}
+
+/**
+ * This function is about 3 times faster than the system log() function
+ * and has an error of about 0.01%
+ */
+double fast_log (double val)
+{
+   return (fast_log2 (val) * 0.69314718f);
+}
+
+double fast_log10 (double val)
+{
+   return (fast_log (val) / fast_log (10));
+}
+
+
+/**
+ * While we're on the subject, someone might have use for these as well?
+ * Float Shift Left and Float Shift Right. Do what you want with this.
+ */
+void fast_BSL(double &x, register unsigned long shiftAmount) {
+
+       *(unsigned long*)&x+=shiftAmount<<23;
+
+}
+
+void fast_BSR(double &x, register unsigned long shiftAmount) {
+
+       *(unsigned long*)&x-=shiftAmount<<23;
+
+}
+
diff --git a/simgear/math/fastmath.hxx b/simgear/math/fastmath.hxx
new file mode 100644 (file)
index 0000000..b2b5595
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * \file fastmath.hxx
+ * fast mathematics routines.
+ *
+ * Refferences:
+ *
+ * A Fast, Compact Approximation of the Exponential Function
+ * Nicol N. Schraudolph
+ * IDSIA, Lugano, Switzerland
+ * http://www.inf.ethz.ch/~schraudo/pubs/exp.pdf
+ *
+ * Fast log() Function, by Laurent de Soras:
+ * http://www.flipcode.com/cgi-bin/msg.cgi?showThread=Tip-Fastlogfunction&forum=totd&id=-1
+ *
+ */
+
+/*
+ * $Id$
+ */
+
+#ifndef _SG_FMATH_HXX
+#define _SG_FMATH_HXX 1
+
+#ifndef __cplusplus
+# error This library requires C++
+#endif
+
+
+#include <math.h>
+
+double fast_exp(double y);
+double fast_log(double val);
+double fast_log2 (double val);
+double fast_log10(double val);
+void fast_BSL(double &x, register unsigned long shiftAmount);
+void fast_BSR(double &x, register unsigned long shiftAmount);
+
+#endif // !_SG_FMATH_HXX
+
index 366016cde86cbc0a6d2766e349d47248e48e837e..8ceaf18251de063a862b9ff71d25e7d44c9e1944 100644 (file)
@@ -32,6 +32,7 @@
 
 #include <simgear/debug/logstream.hxx>
 #include <simgear/props/condition.hxx>
+#include <simgear/math/fastmath.hxx>
 
 
 #include "sound.hxx"
 static double _snd_inv(double v)   { return (v == 0) ? 1e99 : 1/v; }
 static double _snd_abs(double v)   { return (v >= 0) ? v : -v; }
 static double _snd_sqrt(double v)  { return (v < 0) ? sqrt(-v) : sqrt(v); }
-static double _snd_log10(double v) { return (v < 1) ? 0 : log10(v); }
-static double _snd_log(double v)   { return (v < 1) ? 0 : log(v); }
-// static double _snd_sqr(double v)   { return pow(v, 2); }
-// static double _snd_pow3(double v)  { return pow(v, 3); }
+static double _snd_log10(double v) { return (v < 1) ? 0 : fast_log10(v); }
+static double _snd_log(double v)   { return (v < 1) ? 0 : fast_log(v); }
+// static double _snd_sqr(double v)   { return v*v; }
+// static double _snd_pow3(double v)  { return v*v*v; }
 
 static const struct {
        char *name;