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.
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);
}
};