PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Fixed point class & Intels GPP

Currently, I am trying to develope an efficient application for a Pocket PC (PXA27x Bulverde).
Inspiried from these two posts: http://www.gamedev.net/community/forums/topic.asp?topic_id=328183 and http://www.pocketmatrix.com/forums/viewtopic.php?t=20700&highlight=fixed16,
I created my own Fixed point class, in which I called functions of the Intel(R) Graphics Performance Primitives Library Version. (see below)

I am not an expert in operator overloading, inline function etc...
So, I have three questions:

1. Is the design efficient, or are there serious performance (or semantical) problems?

2. How to deal with overflows (multiplication)? Is there a save and efficient way? I am thinking of multiplication with saturation, but I dont know how to implement it...

3. If I call Fixed(2.345f) several times (e.g. in a loop), will the compiler calculate it once or is it calculated over and over again?

Thanks a lot!

Best,
TorbenT


P.S.: If the app runs on the PocketPC, "defined PPC" is true. On the PC, it has not to be efficient.

Code:

#include "gpp/gpp.h"

#define FXD_RES 16
#define POWER_FXD_RES 65536 //2^FXD_RES

#define int2fxd(x) (x << FXD_RES)
#define fxd2int(x) ((x + (1<<(FXD_RES-1))) >> FXD_RES)
#define fld2fxd(x) int(POWER_FXD_RES * x)
#define fxd2fld(x) float(x)/POWER_FXD_RES



class Fixed{
int value;

public:

Fixed() {}

explicit Fixed(int i): value(int2fxd(i)) {}

explicit Fixed(float fld): value(fld2fxd(fld)) {}

static Fixed fromRaw( int raw ) {
Fixed r;
r.value = raw;
return r;
}

long raw() const {
return value;
}

Fixed operator-() const {
return fromRaw(-value);
}

Fixed operator+(const Fixed& other) const {
return fromRaw(value + other.value);
}

Fixed operator-(const Fixed& other) const {
return fromRaw(value - other.value);
}

Fixed& operator+=(Fixed& other){
value += other.value;
return *this;
}

Fixed& operator-=(Fixed& other){
value -= other.value;
return *this;
}

Fixed operator*(const int i) const {
return fromRaw(value * i);
}

Fixed operator*(Fixed b ) {
#ifdef PPC
int dst;
gppMul_16_32s(value, b.raw(), &dst);
return fromRaw(dst);
#else
return Fixed( toFlt()*b.toFlt() );
#endif
}

Fixed& operator*=(Fixed& other){
*this = *this * other;
return *this;
}

Fixed operator/(Fixed b ) {
#ifdef PPC
int dst;
gppDivLP_16_32s(value, b.raw(), &dst);
return fromRaw(dst);
#else
return Fixed( toFlt()/b.toFlt() );
#endif
}


Fixed operator>>(const int i) const {
return fromRaw(value >> i);
}

Fixed operator<<(const int i) const {
return fromRaw(value << i);
}

bool operator==(const Fixed& other) const {
return value == other.value;
}

bool operator!=(const Fixed& other) const {
return value != other.value;
}

bool operator<(const Fixed& other) const {
return value < other.value;
}

bool operator>(const Fixed& other) const {
return value > other.value;
}

bool operator<=(const Fixed& other) const {
return value <= other.value;
}

bool operator>=(const Fixed& other) const {
return value >= other.value;
}

Fixed inv(){
#ifdef PPC
int dst;
gppInvLP_16_32s(value,&dst);
return fromRaw(dst);
#else
return Fixed(1.f/this->toFlt());
#endif
}

static Fixed abs(Fixed f){
return fromRaw(std::abs(f.raw()));
}

static Fixed sqrt(Fixed f){
#ifdef PPC
unsigned int dst;
gppSqrtLP_16_32s(f.raw(),&dst);
return fromRaw(int(dst));
#else
return Fixed(std::sqrt(f.toFlt()));
#endif
}

int toInt (){
return fxd2int(value);
}

float toFlt (){
return fxd2fld(value);
}
};
If you saturate on multiply you might get some odd behaviour depending on what the calculation is. In some cases this might not be too bad (graphical glitch) but in something like the collision this could mean you fall through the floor. Imo, its better to make sure your inputs never get that large and do an assert on your multiply function to check for overflow (check if the sign has changed, or if the top bits in your temporary (if ur doing 32x32->64) aren't all 1 or 0), you could use that check to saturate if you wanted.

Passing that float into the constructor should just compile to a constant load, might be worth checking the assembler.