Index: trunk/firmware/sources/fixpoint/.cproject =================================================================== diff --git a/trunk/firmware/sources/fixpoint/.cproject b/trunk/firmware/sources/fixpoint/.cproject new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/firmware/sources/fixpoint/.cproject (revision 203) @@ -0,0 +1,127 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Index: trunk/firmware/sources/fixpoint/.project =================================================================== diff --git a/trunk/firmware/sources/fixpoint/.project b/trunk/firmware/sources/fixpoint/.project new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/firmware/sources/fixpoint/.project (revision 203) @@ -0,0 +1,27 @@ + + + fixpoint + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + Index: trunk/firmware/sources/fixpoint/fixpoint.h =================================================================== diff --git a/trunk/firmware/sources/fixpoint/fixpoint.h b/trunk/firmware/sources/fixpoint/fixpoint.h new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/firmware/sources/fixpoint/fixpoint.h (revision 203) @@ -0,0 +1,121 @@ +/* + * fixpoint.h + * + * Created on: Feb 24, 2015 + * Author: aras + */ + +#ifndef FIXPOINT_H_ +#define FIXPOINT_H_ + +#include +#include +#include +#include + + + + +template +class fixpoint { + +private: + uint8_t factor; + T value; + +public: + fixpoint(const fixpoint& fp){factor=fp.factor;value=fp.value;}; + fixpoint(uint8_t dec); + fixpoint(uint8_t dec,T num); + + void set_from_float(float num){value=T(num*(1< operator-(const fixpoint &num1, const fixpoint &num2){ + return fixpoint(num1.factor,num1.value-num2.value); + } + friend inline fixpoint operator+(const fixpoint &num1, const fixpoint &num2){ + return fixpoint(num1.factor,num1.value+num2.value); + } + friend inline fixpoint operator*(const fixpoint &num1, const fixpoint &num2){ + int64_t aux = (int64_t(num1.value)*int64_t(num2.value))>>num1.factor; + return fixpoint(num1.factor,T(aux)); + } + friend inline fixpoint operator/(const fixpoint &num1, const fixpoint &num2){ + int64_t aux = (int64_t(num1.value)<(num1.factor,aux/num2.value); + } + + + //fixpoint& operator= ( const fixpoint& ); + + inline fixpoint power(); + inline fixpoint sqroot(); +}; + + +//template +//fixpoint & fixpoint::operator= ( fixpoint ){ +// +//} + + + + +template +fixpoint::fixpoint(uint8_t dec){ + factor=dec; + value=0; +} + +template +fixpoint::fixpoint(uint8_t dec,T num){ + factor=dec; + value=num; +} + + +template +float fixpoint::tofloat(){ + return float(value)/(1< +fixpoint fixpoint::power(){ + int64_t aux = (int64_t(value)*int64_t(value))>>factor; + return fixpoint(aux); +} + +template +fixpoint fixpoint::sqroot(){ + T aux = sqrt(value); + return fixpoint(aux<<(factor/2)); +} + + + +typedef fixpoint num32; +typedef fixpoint num64; + + +class fix1912:public num32{ +public: + fix1912():num32(12){}; + fix1912(int32_t num):num32(12,num){}; +}; + +class fix0922:public num32{ +public: + fix0922():num32(22){}; + fix0922(int32_t num):num32(22,num){}; +}; + +class fix4320:public num64{ +public: + fix4320():num64(20){}; + fix4320(int64_t num):num64(20,num){}; +}; + +#endif /* FIXPOINT_H_ */ Index: trunk/firmware/sources/fixpoint/fixpoint_test.cpp =================================================================== diff --git a/trunk/firmware/sources/fixpoint/fixpoint_test.cpp b/trunk/firmware/sources/fixpoint/fixpoint_test.cpp new file mode 10644 --- /dev/null (revision 0) +++ b/trunk/firmware/sources/fixpoint/fixpoint_test.cpp (revision 203) @@ -0,0 +1,31 @@ +/* + * fixpoint_test.cpp + * + * Created on: Feb 24, 2015 + * Author: aras + */ + +#include "fixpoint.h" + + +int main(){ + + fix1912 A; + fix1912 B; + A.set_from_float(2); + B.set_from_float(5); + fix1912 C; + C=A*B; + C.power(); +// N44_20 A1; +// N44_20 B1; +// A1.set_from_float(2); +// B1.set_from_float(5); +// +// N44_20 C1 = (A1*B1).power(); + + printf("float valout: %f\n",-2.0*500.0); + printf("C valout: %f\n",C.tofloat()); +// printf("C1 valout: %f",C1.tofloat()); + return 0; +}