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.
This commit is contained in:
Flatmush 2011-02-24 20:04:55 +00:00
parent d829f83d31
commit 7c2852e7f6
3 changed files with 39 additions and 47 deletions

View File

@ -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;

View File

@ -8,6 +8,8 @@ extern "C"
#include <stdint.h>
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. */
#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))
/*! Coverts a fix16_t to a double and returns the result.
*/
/* 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);
*/

View File

@ -16,7 +16,6 @@
<Compiler>
<Add option="-Wall" />
<Add option="-g" />
<Add option="-DFIXMATH_NO_CACHE" />
</Compiler>
</Target>
<Target title="rel">
@ -27,8 +26,10 @@
<Option compiler="gcc" />
<Option createDefFile="1" />
<Compiler>
<Add option="-fomit-frame-pointer" />
<Add option="-fexpensive-optimizations" />
<Add option="-O3" />
<Add option="-Wall" />
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />