forked from RangeNetworks/CommonLibs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
F16.h
211 lines (161 loc) · 3.55 KB
/
F16.h
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* Copyright 2009 Free Software Foundation, Inc.
* Copyright 2014 Range Networks, Inc.
*
* This software is distributed under the terms of the GNU Affero Public License.
* See the COPYING file in the main directory for details.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef F16_H
#define F16_H
#include <stdint.h>
#include <ostream>
/** Round a float to the appropriate F16 value. */
inline int32_t _f16_round(float f)
{
if (f>0.0F) return (int32_t)(f+0.5F);
if (f<0.0F) return (int32_t)(f-0.5F);
return 0;
}
/** A class for F15.16 fixed point arithmetic with saturation. */
class F16 {
private:
int32_t mV;
public:
F16() {}
F16(int i) { mV = i<<16; }
F16(float f) { mV = _f16_round(f*65536.0F); }
F16(double f) { mV = _f16_round((float)f*65536.0F); }
int32_t& raw() { return mV; }
const int32_t& raw() const { return mV; }
float f() const { return mV/65536.0F; }
//operator float() const { return mV/65536.0F; }
//operator int() const { return mV>>16; }
F16 operator=(float f)
{
mV = _f16_round(f*65536.0F);
return *this;
}
F16 operator=(int i)
{
mV = i<<16;
return *this;
}
F16 operator=(const F16& other)
{
mV = other.mV;
return mV;
}
F16 operator+(const F16& other) const
{
F16 retVal;
retVal.mV = mV + other.mV;
return retVal;
}
F16& operator+=(const F16& other)
{
mV += other.mV;
return *this;
}
F16 operator-(const F16& other) const
{
F16 retVal;
retVal.mV = mV - other.mV;
return retVal;
}
F16& operator-=(const F16& other)
{
mV -= other.mV;
return *this;
}
F16 operator*(const F16& other) const
{
F16 retVal;
int64_t p = (int64_t)mV * (int64_t)other.mV;
retVal.mV = p>>16;
return retVal;
}
F16& operator*=(const F16& other)
{
int64_t p = (int64_t)mV * (int64_t)other.mV;
mV = p>>16;
return *this;
}
F16 operator*(float f) const
{
F16 retVal;
retVal.mV = mV * f;
return retVal;
}
F16& operator*=(float f)
{
mV *= f;
return *this;
}
F16 operator/(const F16& other) const
{
F16 retVal;
int64_t pV = (int64_t)mV << 16;
retVal.mV = pV / other.mV;
return retVal;
}
F16& operator/=(const F16& other)
{
int64_t pV = (int64_t)mV << 16;
mV = pV / other.mV;
return *this;
}
F16 operator/(float f) const
{
F16 retVal;
retVal.mV = mV / f;
return retVal;
}
F16& operator/=(float f)
{
mV /= f;
return *this;
}
bool operator>(const F16& other) const
{
return mV>other.mV;
}
bool operator<(const F16& other) const
{
return mV<other.mV;
}
bool operator==(const F16& other) const
{
return mV==other.mV;
}
bool operator>(float f) const
{
return (mV/65536.0F) > f;
}
bool operator<(float f) const
{
return (mV/65536.0F) < f;
}
bool operator==(float f) const
{
return (mV/65536.0F) == f;
}
};
inline std::ostream& operator<<(std::ostream& os, const F16& v)
{
os << v.f();
return os;
}
#endif