Added C++ class interface.

Optimized rounding on 32-bit divide function.
This commit is contained in:
flatmush 2011-03-02 12:58:08 +00:00
parent 359ed66670
commit 46cb5cd8df
3 changed files with 121 additions and 3 deletions

View File

@ -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);

View File

@ -118,6 +118,7 @@ extern fix16_t fix16_exp(fix16_t inValue);
#ifdef __cplusplus
}
#include "fix16.hpp"
#endif
#endif

118
libfixmath/fix16.hpp Normal file
View File

@ -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