aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorflatmush <flatmush@d3e1167c-abe1-51d5-8199-f9061ebe54e4>2011-03-02 16:33:28 +0000
committerflatmush <flatmush@d3e1167c-abe1-51d5-8199-f9061ebe54e4>2011-03-02 16:33:28 +0000
commite8ab51042ba2f29feb98f39fb09f93297320060c (patch)
treecdc9a0fcd384481ff64a72be065f5aae54eb0933
parent62eb1059a5b15e3229c2504818440be4b511c1a1 (diff)
downloadlibfixmath-e8ab51042ba2f29feb98f39fb09f93297320060c.tar.gz
Added fix16_add, fix16_sub, fix16_ssub as static inline functions.
Updated C++ interface to include saturated operators.
-rw-r--r--libfixmath/fix16.h6
-rw-r--r--libfixmath/fix16.hpp25
2 files changed, 31 insertions, 0 deletions
diff --git a/libfixmath/fix16.h b/libfixmath/fix16.h
index 711a1d1..92573d6 100644
--- a/libfixmath/fix16.h
+++ b/libfixmath/fix16.h
@@ -40,10 +40,16 @@ static inline int32_t fix16_to_int(const fix16_t inVal) { return ((inVal + 0x0
+static inline fix16_t fix16_add(fix16_t inArg0, fix16_t inArg1) { return (inArg0 + inArg1); }
+
/*! Performs a saturated addition (overflow-protected) of the two given fix16_t's and returns the result.
*/
extern fix16_t fix16_sadd(fix16_t inArg0, fix16_t inArg1);
+static inline fix16_t fix16_sub(fix16_t inArg0, fix16_t inArg1) { return (inArg0 - inArg1); }
+
+static inline fix16_t fix16_ssub(fix16_t inArg0, fix16_t inArg1) { return fix16_sadd(inArg0, inArg1);}
+
/*! Multiplies the two given fix16_t's and returns the result.
diff --git a/libfixmath/fix16.hpp b/libfixmath/fix16.hpp
index 3cdec7c..26863fe 100644
--- a/libfixmath/fix16.hpp
+++ b/libfixmath/fix16.hpp
@@ -53,24 +53,48 @@ class Fix16 {
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 sadd(const Fix16 &other) const { Fix16 ret = fix16_sadd(value, other.value); return ret; }
+ const Fix16 sadd(const fix16_t other) const { Fix16 ret = fix16_sadd(value, other); return ret; }
+ const Fix16 sadd(const double other) const { Fix16 ret = fix16_sadd(value, fix16_from_dbl(other)); return ret; }
+ const Fix16 sadd(const float other) const { Fix16 ret = fix16_sadd(value, fix16_from_float(other)); return ret; }
+ const Fix16 sadd(const int16_t other) const { Fix16 ret = fix16_sadd(value, fix16_from_int(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 ssub(const Fix16 &other) const { Fix16 ret = fix16_sadd(value, -other.value); return ret; }
+ const Fix16 ssub(const fix16_t other) const { Fix16 ret = fix16_sadd(value, -other); return ret; }
+ const Fix16 ssub(const double other) const { Fix16 ret = fix16_sadd(value, -fix16_from_dbl(other)); return ret; }
+ const Fix16 ssub(const float other) const { Fix16 ret = fix16_sadd(value, -fix16_from_float(other)); return ret; }
+ const Fix16 ssub(const int16_t other) const { Fix16 ret = fix16_sadd(value, -fix16_from_int(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 smul(const Fix16 &other) const { Fix16 ret = fix16_smul(value, other.value); return ret; }
+ const Fix16 smul(const fix16_t other) const { Fix16 ret = fix16_smul(value, other); return ret; }
+ const Fix16 smul(const double other) const { Fix16 ret = fix16_smul(value, fix16_from_dbl(other)); return ret; }
+ const Fix16 smul(const float other) const { Fix16 ret = fix16_smul(value, fix16_from_float(other)); return ret; }
+ const Fix16 smul(const int16_t other) const { Fix16 ret = fix16_smul(value, fix16_from_int(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 sdiv(const Fix16 &other) const { Fix16 ret = fix16_sdiv(value, other.value); return ret; }
+ const Fix16 sdiv(const fix16_t other) const { Fix16 ret = fix16_sdiv(value, other); return ret; }
+ const Fix16 sdiv(const double other) const { Fix16 ret = fix16_sdiv(value, fix16_from_dbl(other)); return ret; }
+ const Fix16 sdiv(const float other) const { Fix16 ret = fix16_sdiv(value, fix16_from_float(other)); return ret; }
+ const Fix16 sdiv(const int16_t other) const { Fix16 ret = fix16_sdiv(value, fix16_from_int(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)); }
@@ -114,6 +138,7 @@ class Fix16 {
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)); }
+ Fix16 sqrt() { return Fix16(fix16_sqrt(value)); }
};
#endif