diff options
| author | flatmush <flatmush@d3e1167c-abe1-51d5-8199-f9061ebe54e4> | 2011-03-02 12:58:08 +0000 |
|---|---|---|
| committer | flatmush <flatmush@d3e1167c-abe1-51d5-8199-f9061ebe54e4> | 2011-03-02 12:58:08 +0000 |
| commit | 46cb5cd8df43d3ca9f1877f1d6c810749f34867b (patch) | |
| tree | 8dbd6f467330be45da57acbb2fde6da473285dfd | |
| parent | 359ed6667023cc7df575a03571ba31f1f670ce30 (diff) | |
| download | libfixmath-46cb5cd8df43d3ca9f1877f1d6c810749f34867b.tar.gz | |
Added C++ class interface.
Optimized rounding on 32-bit divide function.
| -rw-r--r-- | libfixmath/fix16.c | 5 | ||||
| -rw-r--r-- | libfixmath/fix16.h | 1 | ||||
| -rw-r--r-- | libfixmath/fix16.hpp | 118 |
3 files changed, 121 insertions, 3 deletions
diff --git a/libfixmath/fix16.c b/libfixmath/fix16.c index 129541f..d162554 100644 --- a/libfixmath/fix16.c +++ b/libfixmath/fix16.c @@ -207,11 +207,10 @@ fix16_t fix16_sdiv(fix16_t inArg0, fix16_t inArg1) { } while(n_hi);
}
- res += (n_lo / inArg1);
#ifndef FIXMATH_NO_ROUNDING
- if((n_lo % inArg1) >= (inArg1 >> 1))
- res++;
+ n_lo += (inArg1 >> 1);
#endif
+ res += (n_lo / inArg1);
res += (r_hi << 16);
return (neg ? -res : res);
diff --git a/libfixmath/fix16.h b/libfixmath/fix16.h index b3ab70c..711a1d1 100644 --- a/libfixmath/fix16.h +++ b/libfixmath/fix16.h @@ -118,6 +118,7 @@ extern fix16_t fix16_exp(fix16_t inValue); #ifdef __cplusplus
}
+#include "fix16.hpp"
#endif
#endif
diff --git a/libfixmath/fix16.hpp b/libfixmath/fix16.hpp new file mode 100644 index 0000000..4b376d8 --- /dev/null +++ b/libfixmath/fix16.hpp @@ -0,0 +1,118 @@ +#ifndef __libfixmath_fix16_hpp__
+#define __libfixmath_fix16_hpp__
+
+class Fix16 {
+ public:
+ fix16_t value;
+
+ Fix16() { value = 0; }
+ Fix16(const Fix16 &inValue) { value = inValue.value; }
+ Fix16(const fix16_t inValue) { value = inValue; }
+ Fix16(const float inValue) { value = fix16_from_float(inValue); }
+ Fix16(const double inValue) { value = fix16_from_dbl(inValue); }
+ Fix16(const int16_t inValue) { value = fix16_from_int(inValue); }
+
+ operator double() { return fix16_to_dbl(value); }
+ operator float() { return fix16_to_float(value); }
+ operator int() { return fix16_to_int(value); }
+
+ Fix16 & operator=(const Fix16 &rhs) { value = rhs.value; return *this; }
+ Fix16 & operator=(const fix16_t rhs) { value = rhs; return *this; }
+ Fix16 & operator=(const double rhs) { value = fix16_from_dbl(rhs); return *this; }
+ Fix16 & operator=(const float rhs) { value = fix16_from_float(rhs); return *this; }
+ Fix16 & operator=(const int16_t rhs) { value = fix16_from_int(rhs); return *this; }
+
+ Fix16 & operator+=(const Fix16 &rhs) { value += rhs.value; return *this; }
+ Fix16 & operator+=(const fix16_t rhs) { value += rhs; return *this; }
+ Fix16 & operator+=(const double rhs) { value += fix16_from_dbl(rhs); return *this; }
+ Fix16 & operator+=(const float rhs) { value += fix16_from_float(rhs); return *this; }
+ Fix16 & operator+=(const int16_t rhs) { value += fix16_from_int(rhs); return *this; }
+
+ Fix16 & operator-=(const Fix16 &rhs) { value -= rhs.value; return *this; }
+ Fix16 & operator-=(const fix16_t rhs) { value -= rhs; return *this; }
+ Fix16 & operator-=(const double rhs) { value -= fix16_from_dbl(rhs); return *this; }
+ Fix16 & operator-=(const float rhs) { value -= fix16_from_float(rhs); return *this; }
+ Fix16 & operator-=(const int16_t rhs) { value -= fix16_from_int(rhs); return *this; }
+
+ Fix16 & operator*=(const Fix16 &rhs) { value = fix16_mul(value, rhs.value); return *this; }
+ Fix16 & operator*=(const fix16_t rhs) { value = fix16_mul(value, rhs); return *this; }
+ Fix16 & operator*=(const double rhs) { value = fix16_mul(value, fix16_from_dbl(rhs)); return *this; }
+ Fix16 & operator*=(const float rhs) { value = fix16_mul(value, fix16_from_float(rhs)); return *this; }
+ Fix16 & operator*=(const int16_t rhs) { value *= rhs; return *this; }
+
+ Fix16 & operator/=(const Fix16 &rhs) { value = fix16_div(value, rhs.value); return *this; }
+ Fix16 & operator/=(const fix16_t rhs) { value = fix16_div(value, rhs); return *this; }
+ Fix16 & operator/=(const double rhs) { value = fix16_div(value, fix16_from_dbl(rhs)); return *this; }
+ Fix16 & operator/=(const float rhs) { value = fix16_div(value, fix16_from_float(rhs)); return *this; }
+ Fix16 & operator/=(const int16_t rhs) { value /= rhs; return *this; }
+
+ const Fix16 operator+(const Fix16 &other) const { Fix16 ret = *this; ret += other; return ret; }
+ const Fix16 operator+(const fix16_t other) const { Fix16 ret = *this; ret += other; return ret; }
+ const Fix16 operator+(const double other) const { Fix16 ret = *this; ret += other; return ret; }
+ const Fix16 operator+(const float other) const { Fix16 ret = *this; ret += other; return ret; }
+ const Fix16 operator+(const int16_t other) const { Fix16 ret = *this; ret += other; return ret; }
+
+ const Fix16 operator-(const Fix16 &other) const { Fix16 ret = *this; ret -= other; return ret; }
+ const Fix16 operator-(const fix16_t other) const { Fix16 ret = *this; ret -= other; return ret; }
+ const Fix16 operator-(const double other) const { Fix16 ret = *this; ret -= other; return ret; }
+ const Fix16 operator-(const float other) const { Fix16 ret = *this; ret -= other; return ret; }
+ const Fix16 operator-(const int16_t other) const { Fix16 ret = *this; ret -= other; return ret; }
+
+ const Fix16 operator*(const Fix16 &other) const { Fix16 ret = *this; ret *= other; return ret; }
+ const Fix16 operator*(const fix16_t other) const { Fix16 ret = *this; ret *= other; return ret; }
+ const Fix16 operator*(const double other) const { Fix16 ret = *this; ret *= other; return ret; }
+ const Fix16 operator*(const float other) const { Fix16 ret = *this; ret *= other; return ret; }
+ const Fix16 operator*(const int16_t other) const { Fix16 ret = *this; ret *= other; return ret; }
+
+ const Fix16 operator/(const Fix16 &other) const { Fix16 ret = *this; ret /= other; return ret; }
+ const Fix16 operator/(const fix16_t other) const { Fix16 ret = *this; ret /= other; return ret; }
+ const Fix16 operator/(const double other) const { Fix16 ret = *this; ret /= other; return ret; }
+ const Fix16 operator/(const float other) const { Fix16 ret = *this; ret /= other; return ret; }
+ const Fix16 operator/(const int16_t other) const { Fix16 ret = *this; ret /= other; return ret; }
+
+ const int operator==(const Fix16 &other) const { return (value == other.value); }
+ const int operator==(const fix16_t other) const { return (value == other); }
+ const int operator==(const double other) const { return (value == fix16_from_dbl(other)); }
+ const int operator==(const float other) const { return (value == fix16_from_float(other)); }
+ const int operator==(const int16_t other) const { return (value == fix16_from_int(other)); }
+
+ const int operator!=(const Fix16 &other) const { return (value != other.value); }
+ const int operator!=(const fix16_t other) const { return (value != other); }
+ const int operator!=(const double other) const { return (value != fix16_from_dbl(other)); }
+ const int operator!=(const float other) const { return (value != fix16_from_float(other)); }
+ const int operator!=(const int16_t other) const { return (value != fix16_from_int(other)); }
+
+ const int operator<=(const Fix16 &other) const { return (value <= other.value); }
+ const int operator<=(const fix16_t other) const { return (value <= other); }
+ const int operator<=(const double other) const { return (value <= fix16_from_dbl(other)); }
+ const int operator<=(const float other) const { return (value <= fix16_from_float(other)); }
+ const int operator<=(const int16_t other) const { return (value <= fix16_from_int(other)); }
+
+ const int operator>=(const Fix16 &other) const { return (value >= other.value); }
+ const int operator>=(const fix16_t other) const { return (value >= other); }
+ const int operator>=(const double other) const { return (value >= fix16_from_dbl(other)); }
+ const int operator>=(const float other) const { return (value >= fix16_from_float(other)); }
+ const int operator>=(const int16_t other) const { return (value >= fix16_from_int(other)); }
+
+ const int operator< (const Fix16 &other) const { return (value < other.value); }
+ const int operator< (const fix16_t other) const { return (value < other); }
+ const int operator< (const double other) const { return (value < fix16_from_dbl(other)); }
+ const int operator< (const float other) const { return (value < fix16_from_float(other)); }
+ const int operator< (const int16_t other) const { return (value < fix16_from_int(other)); }
+
+ const int operator> (const Fix16 &other) const { return (value > other.value); }
+ const int operator> (const fix16_t other) const { return (value > other); }
+ const int operator> (const double other) const { return (value > fix16_from_dbl(other)); }
+ const int operator> (const float other) const { return (value > fix16_from_float(other)); }
+ const int operator> (const int16_t other) const { return (value > fix16_from_int(other)); }
+
+ Fix16 sin() { return Fix16(fix16_sin(value)); }
+ Fix16 cos() { return Fix16(fix16_cos(value)); }
+ Fix16 tan() { return Fix16(fix16_tan(value)); }
+ Fix16 asin() { return Fix16(fix16_asin(value)); }
+ Fix16 acos() { return Fix16(fix16_acos(value)); }
+ Fix16 atan() { return Fix16(fix16_atan(value)); }
+ Fix16 atan2(const Fix16 &inY) { return Fix16(fix16_atan2(value, inY.value)); }
+};
+
+#endif
|
