From 5d24be8c51c282cee28cc8329d4305f872217d79 Mon Sep 17 00:00:00 2001 From: ehofman Date: Sat, 28 Jun 2003 12:06:09 +0000 Subject: [PATCH] Add some fast math functions --- simgear/math/Makefile.am | 6 ++- simgear/math/fastmath.cxx | 94 +++++++++++++++++++++++++++++++++++++++ simgear/math/fastmath.hxx | 39 ++++++++++++++++ simgear/sound/sound.cxx | 9 ++-- 4 files changed, 142 insertions(+), 6 deletions(-) create mode 100644 simgear/math/fastmath.cxx create mode 100644 simgear/math/fastmath.hxx diff --git a/simgear/math/Makefile.am b/simgear/math/Makefile.am index c319213c..36747b2d 100644 --- a/simgear/math/Makefile.am +++ b/simgear/math/Makefile.am @@ -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 index 00000000..ce8e253b --- /dev/null +++ b/simgear/math/fastmath.cxx @@ -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 + +/** + * 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 (&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 index 00000000..b2b5595a --- /dev/null +++ b/simgear/math/fastmath.hxx @@ -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 + +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 + diff --git a/simgear/sound/sound.cxx b/simgear/sound/sound.cxx index 366016cd..8ceaf182 100644 --- a/simgear/sound/sound.cxx +++ b/simgear/sound/sound.cxx @@ -32,6 +32,7 @@ #include #include +#include #include "sound.hxx" @@ -41,10 +42,10 @@ 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; -- 2.39.5