From 7c2852e7f647ffad35eeda527d4c9d6bff96f226 Mon Sep 17 00:00:00 2001 From: Flatmush Date: Thu, 24 Feb 2011 20:04:55 +0000 Subject: Changed conversion functions to macros so that they can be optimized out by the compiler which should make optimized functions much more readable. Implemented FIXMATH_NO_ROUNDING in all multiplication and division functions too, it should now work for all functions in the library and allow for a choice between speed and accuracy on different projects. --- libfixmath/fix16.c | 45 ++++++++++++++++++++------------------------- libfixmath/fix16.h | 38 +++++++++++++++++--------------------- libfixmath/libfixmath.cbp | 5 +++-- 3 files changed, 40 insertions(+), 48 deletions(-) diff --git a/libfixmath/fix16.c b/libfixmath/fix16.c index 32fc3d1..a11e9b7 100644 --- a/libfixmath/fix16.c +++ b/libfixmath/fix16.c @@ -9,29 +9,14 @@ const fix16_t fix16_one = 0x00010000; -double fix16_to_dbl(const fix16_t inVal) { - return ((double)inVal / 65536.0); -} - -fix16_t fix16_from_dbl(const double inVal) { - return (fix16_t)(inVal * 65536.0); -} - -float fix16_to_float(const fix16_t inVal) { - return ((float)inVal / 65536.0f); -} - -fix16_t fix16_from_float(const float inVal) { - return (fix16_t)(inVal * 65536.0f); -} - -int32_t fix16_to_int(const fix16_t inVal) { - return ((inVal + 0x00008000) >> 16); -} - -fix16_t fix16_from_int(const int32_t inVal) { - return (inVal << 16); -} +/* See header as to why these are commented out. +double fix16_to_dbl(const fix16_t inVal) { return ((double)inVal / 65536.0); } +fix16_t fix16_from_dbl(const double inVal) { return (fix16_t)(inVal * 65536.0); } +float fix16_to_float(const fix16_t inVal) { return ((float)inVal / 65536.0f); } +fix16_t fix16_from_float(const float inVal) { return (fix16_t)(inVal * 65536.0f); } +int32_t fix16_to_int(const fix16_t inVal) { return ((inVal + 0x00008000) >> 16); } +fix16_t fix16_from_int(const int32_t inVal) { return (inVal << 16); } +*/ @@ -47,13 +32,19 @@ fix16_t fix16_sadd(fix16_t inArg0, fix16_t inArg1) { fix16_t fix16_mul(fix16_t inArg0, fix16_t inArg1) { - int64_t tempResult = ((int64_t)inArg0 * (int64_t)inArg1) + 0x00008000; + int64_t tempResult = ((int64_t)inArg0 * (int64_t)inArg1); + #ifndef FIXMATH_NO_ROUNDING + tempResult += (fix16_one >> 1); + #endif tempResult >>= 16; return tempResult; } fix16_t fix16_smul(fix16_t inArg0, fix16_t inArg1) { - int64_t tempResult = ((int64_t)inArg0 * (int64_t)inArg1) + 0x00008000; + int64_t tempResult = ((int64_t)inArg0 * (int64_t)inArg1); + #ifndef FIXMATH_NO_ROUNDING + tempResult += (fix16_one >> 1); + #endif tempResult >>= 16; if(tempResult < fix16_MIN) return fix16_MIN; @@ -67,7 +58,9 @@ fix16_t fix16_smul(fix16_t inArg0, fix16_t inArg1) { fix16_t fix16_div(fix16_t inArg0, fix16_t inArg1) { int64_t tempResult = inArg0; tempResult <<= 16; + #ifndef FIXMATH_NO_ROUNDING tempResult += (inArg1 >> 1); + #endif tempResult /= inArg1; return tempResult; } @@ -80,7 +73,9 @@ fix16_t fix16_sdiv(fix16_t inArg0, fix16_t inArg1) { } int64_t tempResult = inArg0; tempResult <<= 16; + #ifndef FIXMATH_NO_ROUNDING tempResult += (inArg1 >> 1); + #endif tempResult /= inArg1; if(tempResult < fix16_MIN) return fix16_MIN; diff --git a/libfixmath/fix16.h b/libfixmath/fix16.h index 7913b9c..27e4bc6 100644 --- a/libfixmath/fix16.h +++ b/libfixmath/fix16.h @@ -8,6 +8,8 @@ extern "C" #include +typedef int32_t fix16_t; + #define fix16_MAX (fix16_t)0x7FFFFFFF /*!< the maximum value of fix16_t */ #define fix16_MIN (fix16_t)0x80000000 /*!< the minimum value of fix16_t */ @@ -15,33 +17,27 @@ extern "C" #define fix16_e 178145 /*!< fix16_t value of e */ #define fix16_one 0x00010000 /*!< fix16_t value of 1 */ -typedef int32_t fix16_t; - - - -/*! Coverts a fix16_t to a double and returns the result. -*/ +/*! Coverts a fix16_t to a double and returns the result. */ +#define fix16_to_dbl(inVal) ((double)((inVal) / 65536.0)) +/*! Converts a double to a fix16_t and returns the result. */ +#define fix16_from_dbl(inVal) ((fix16_t)((inVal) * 65536.0)) +/*! Converts a fix16_t to a float and returns the result. */ +#define fix16_to_float(inVal) ((float)((inVal) / 65536.0f)) +/*! Converts a float to a fix16_t and returns the result. */ +#define fix16_from_float(inVal) ((fix16_t)((inVal) * 65536.0f)) +/*! Converts a fix16_t to a signed integer and returns the result. */ +#define fix16_to_int(inVal) ((int32_t)(((inVal) + (fix16_one >> 1)) >> 16)) +/*! Converts a signed integer to a fix16_t and returns the result. */ +#define fix16_from_int(inVal) ((fix16_t)((inVal) << 16)) + +/* Replaced with macros, uncomment if symbols are needed by old code. extern double fix16_to_dbl(const fix16_t inVal); - -/*! Converts a double to a fix16_t and returns the result. -*/ extern fix16_t fix16_from_dbl(const double inVal); - -/*! Converts a fix16_t to a float and returns the result. -*/ extern float fix16_to_float(const fix16_t inVal); - -/*! Converts a float to a fix16_t and returns the result. -*/ extern fix16_t fix16_from_float(const float inVal); - -/*! Converts a fix16_t to a signed integer and returns the result. -*/ extern int32_t fix16_to_int(const fix16_t inVal); - -/*! Converts a signed integer to a fix16_t and returns the result. -*/ extern fix16_t fix16_from_int(const int32_t inVal); +*/ diff --git a/libfixmath/libfixmath.cbp b/libfixmath/libfixmath.cbp index d9264ea..013625e 100644 --- a/libfixmath/libfixmath.cbp +++ b/libfixmath/libfixmath.cbp @@ -16,7 +16,6 @@ - @@ -27,8 +26,10 @@