From e8ab51042ba2f29feb98f39fb09f93297320060c Mon Sep 17 00:00:00 2001 From: flatmush Date: Wed, 2 Mar 2011 16:33:28 +0000 Subject: Added fix16_add, fix16_sub, fix16_ssub as static inline functions. Updated C++ interface to include saturated operators. --- libfixmath/fix16.h | 6 ++++++ libfixmath/fix16.hpp | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) 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 -- cgit v1.2.3