summaryrefslogtreecommitdiff
path: root/libfixmath/fixmath/fix16.hpp
blob: bd97c7c323f26e7a948f5408d0a63c072d91cc3a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#ifndef __libfixmath_fix16_hpp__
#define __libfixmath_fix16_hpp__

#include "fix16.h"

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 fix16_t() const { return value;                 }
        operator double()  const { return fix16_to_dbl(value);   }
        operator float()   const { return fix16_to_float(value); }
        operator int16_t() const { 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_smul(value, rhs.value); return *this; }
        Fix16 & operator*=(const fix16_t rhs) { value = fix16_smul(value, rhs); return *this; }
        Fix16 & operator*=(const double rhs)  { value = fix16_smul(value, fix16_from_dbl(rhs)); return *this; }
        Fix16 & operator*=(const float rhs)   { value = fix16_smul(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; }

#ifndef FIXMATH_NO_OVERFLOW
        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; }
#endif

        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; }

#ifndef FIXMATH_NO_OVERFLOW
        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; }
#endif

        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; }

#ifndef FIXMATH_NO_OVERFLOW
        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; }
#endif

        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; }

#ifndef FIXMATH_NO_OVERFLOW
        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; }
#endif

        int operator==(const Fix16 &other)  const { return (value == other.value);             }
        int operator==(const fix16_t other) const { return (value == other);                   }
        int operator==(const double other)  const { return (value == fix16_from_dbl(other));   }
        int operator==(const float other)   const { return (value == fix16_from_float(other)); }
        int operator==(const int16_t other) const { return (value == fix16_from_int(other));   }

        int operator!=(const Fix16 &other)  const { return (value != other.value);             }
        int operator!=(const fix16_t other) const { return (value != other);                   }
        int operator!=(const double other)  const { return (value != fix16_from_dbl(other));   }
        int operator!=(const float other)   const { return (value != fix16_from_float(other)); }
        int operator!=(const int16_t other) const { return (value != fix16_from_int(other));   }

        int operator<=(const Fix16 &other)  const { return (value <= other.value);             }
        int operator<=(const fix16_t other) const { return (value <= other);                   }
        int operator<=(const double other)  const { return (value <= fix16_from_dbl(other));   }
        int operator<=(const float other)   const { return (value <= fix16_from_float(other)); }
        int operator<=(const int16_t other) const { return (value <= fix16_from_int(other));   }

        int operator>=(const Fix16 &other)  const { return (value >= other.value);             }
        int operator>=(const fix16_t other) const { return (value >= other);                   }
        int operator>=(const double other)  const { return (value >= fix16_from_dbl(other));   }
        int operator>=(const float other)   const { return (value >= fix16_from_float(other)); }
        int operator>=(const int16_t other) const { return (value >= fix16_from_int(other));   }

        int operator< (const Fix16 &other)  const { return (value <  other.value);             }
        int operator< (const fix16_t other) const { return (value <  other);                   }
        int operator< (const double other)  const { return (value <  fix16_from_dbl(other));   }
        int operator< (const float other)   const { return (value <  fix16_from_float(other)); }
        int operator< (const int16_t other) const { return (value <  fix16_from_int(other));   }

        int operator> (const Fix16 &other)  const { return (value >  other.value);             }
        int operator> (const fix16_t other) const { return (value >  other);                   }
        int operator> (const double other)  const { return (value >  fix16_from_dbl(other));   }
        int operator> (const float other)   const { return (value >  fix16_from_float(other)); }
        int operator> (const int16_t other) const { return (value >  fix16_from_int(other));   }

        Fix16  sin() const { return Fix16(fix16_sin(value));  }
        Fix16  cos() const { return Fix16(fix16_cos(value));  }
        Fix16  tan() const { return Fix16(fix16_tan(value));  }
        Fix16 asin() const { return Fix16(fix16_asin(value)); }
        Fix16 acos() const { return Fix16(fix16_acos(value)); }
        Fix16 atan() const { return Fix16(fix16_atan(value)); }
        Fix16 atan2(const Fix16 &inY) const { return Fix16(fix16_atan2(value, inY.value)); }
        Fix16 sqrt() const { return Fix16(fix16_sqrt(value)); }
};

#endif