-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfixed_point.hpp
More file actions
384 lines (308 loc) · 14.3 KB
/
fixed_point.hpp
File metadata and controls
384 lines (308 loc) · 14.3 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#ifndef FIXED_POINT_H__
#define FIXED_POINT_H__
#include <ostream>
#include <iomanip>
#include "template_utils.hpp"
/// A fixed-point integer type
/** \tparam INT_BITS The number of bits before the radix point
* \tparam FRAC_BITS The number of bits after the radix point
* \warning INT_BITS and FRAC_BITS must be non-negative, and their sum cannot exceed 64
*
* Fixed point numbers are signed, so FixedPoint<5,2>, for example, has a
* range of -16.00 to +15.75
*
* The internal storage (FixedPoint::raw_) is rounded up to the next highest
* power of 2, so a 17 bit number occupies 32 physical bits. Consider that the
* multiplication of a 5 bit fixed point (which occupies 8 bits of space) with
* an 10 bit fixed point (which occupies 16 bits of space) results in a 15 bit
* fixed point which occupies 16 bits of space.
*/
template <int INT_BITS, int FRAC_BITS>
struct FixedPoint
{
public:
/// The integer type used internally to store the value
typedef typename GET_INT_WITH_LENGTH<INT_BITS + FRAC_BITS>::RESULT RawType;
/// This is a type big enough to hold the largest integer value
typedef typename GET_INT_WITH_LENGTH<INT_BITS>::RESULT IntType;
/// This is the type that can handle the largest rounded integer value
/** Note that this is slightly different to IntType because FixedPoint<3,1>(3.5).round() == 4 */
typedef typename GET_INT_WITH_LENGTH<INT_BITS + 1>::RESULT RoundType;
typedef FixedPoint<INT_BITS, FRAC_BITS> ThisType;
struct RawValue
{
RawValue(RawType aValue) : value(aValue) { }
RawType value;
};
///Aiding Functions for conflicting int intialization
static ThisType fromInt(IntType value) { return ThisType(RawValue(value << FRAC_BITS));}
static ThisType fromFloat(float value) { return ThisType(RawType(value * (1 << FRAC_BITS)));}
/// Create a fixed-point with equivalent integer value
/** For example in 4.12 fixed-point, the number "2" is 0010.000000000000 */
FixedPoint(int value) : raw_(value << FRAC_BITS) { }
FixedPoint(double value) : raw_((RawType)(value * (1 << FRAC_BITS))) { }
FixedPoint(float value) : raw_((RawType)(value * (1 << FRAC_BITS))) { }
/// Default constructor
FixedPoint() : raw_(0) { }
FixedPoint(RawValue value): raw_(value.value) { }
/// Create a fixed-point with a predefined raw_ value, with no manipulation
static FixedPoint<INT_BITS, FRAC_BITS> createRaw(RawType aData) { return FixedPoint<INT_BITS, FRAC_BITS>(RawValue(aData)); }
/// Returns a new fixed-point that reinterprets the binary raw_.
/** \warning This should be used sparingly since returns a number whos
* value is not the necessarily same.
* \note To just move the radix point, rather use LeftShift or RightShift */
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW> reinterpret()
{
return FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW>::createRaw(raw_);
}
/// Make the integral part larger
template <int INT_BITS_NEW>
FixedPoint<INT_BITS_NEW, FRAC_BITS> extend()
{
return FixedPoint<INT_BITS_NEW, FRAC_BITS>::createRaw(raw_);
}
/// Returns a new fixed-point in a new format which is similar in value to the original
/** This may result in loss of raw_ if the number of bits for either the
* integer or fractional part are less than the original. */
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW> convert() const
{
typedef typename FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW>::RawType TargetRawType;
return FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW>::createRaw(
CONVERT_FIXED_POINT<
RawType,
TargetRawType,
(FRAC_BITS_NEW - FRAC_BITS),
(FRAC_BITS_NEW > FRAC_BITS)
>:: exec(raw_));
}
/// Multiplication with another fixed-point
template <int INT_BITS2, int FRAC_BITS2>
FixedPoint<INT_BITS + INT_BITS2, FRAC_BITS + FRAC_BITS2>
operator *(FixedPoint<INT_BITS2, FRAC_BITS2> value) const
{
return FixedPoint<INT_BITS + INT_BITS2, FRAC_BITS + FRAC_BITS2>::createRaw(raw_ * value.getRaw());
}
/// Addition with an integer
FixedPoint<INT_BITS, FRAC_BITS> operator +(IntType value) const
{
return *this + FixedPoint<INT_BITS, FRAC_BITS>(value);
}
/// Addition with another fixed point
template <int INT_BITS2, int FRAC_BITS2>
FixedPoint<GET_MAX<INT_BITS, INT_BITS2>::RESULT, GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT>
operator+(FixedPoint<INT_BITS2, FRAC_BITS2> value) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(value.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the addition is trivial
return ResultType::createRaw(op1.getRaw() + op2.getRaw());
}
template <int INT_BITS2, int FRAC_BITS2>
ThisType& operator+=(FixedPoint<INT_BITS2, FRAC_BITS2> value)
{
raw_ += value.convert<INT_BITS, FRAC_BITS>().getRaw();
return *this;
}
ThisType& operator+=(IntType value)
{
raw_ += ThisType(value).getRaw();
return *this;
}
ThisType& operator-=(IntType value)
{
raw_ -= ThisType(value).getRaw();
return *this;
}
template <int INT_BITS2, int FRAC_BITS2>
ThisType& operator-=(FixedPoint<INT_BITS2, FRAC_BITS2> value)
{
raw_ -= value.convert<INT_BITS, FRAC_BITS>().getRaw();
return *this;
}
/// Subtraction operator
template <int INT_BITS2, int FRAC_BITS2>
FixedPoint<GET_MAX<INT_BITS, INT_BITS2>::RESULT, GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT>
operator-(FixedPoint<INT_BITS2, FRAC_BITS2> value) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(value.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the addition is trivial
return ResultType::createRaw(op1.getRaw() - op2.getRaw());
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator < (FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() < op2.getRaw();
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator > (FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() > op2.getRaw();
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator >= (FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() >= op2.getRaw();
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator <= (FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() <= op2.getRaw();
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator ==(FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() == op2.getRaw();
}
template <int INT_BITS2, int FRAC_BITS2>
bool operator != (FixedPoint<INT_BITS2, FRAC_BITS2> other) const
{
// How many bits are in the result?
static const int INT_BITS_RES = GET_MAX<INT_BITS, INT_BITS2>::RESULT;
static const int FRAC_BITS_RES = GET_MAX<FRAC_BITS, FRAC_BITS2>::RESULT;
// This is the type of the result
typedef FixedPoint<INT_BITS_RES, FRAC_BITS_RES> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS_RES, FRAC_BITS_RES>());
ResultType op2(other.convert<INT_BITS_RES, FRAC_BITS_RES>());
// Then the operation is trivial
return op1.getRaw() != op2.getRaw();
}
/// Divide operator
template <int INT_BITS2, int FRAC_BITS2>
FixedPoint<INT_BITS + FRAC_BITS2, FRAC_BITS + INT_BITS2>
operator/(FixedPoint<INT_BITS2, FRAC_BITS2> divisor) const
{
// (A/2^B)/(C/2^D) = (A/C)/2^(B-D);
typedef FixedPoint<INT_BITS + FRAC_BITS2, FRAC_BITS + INT_BITS2> ResultType;
typedef typename GET_INT_WITH_LENGTH<INT_BITS + FRAC_BITS + FRAC_BITS2 + INT_BITS2>::RESULT IntermediateType;
// Expand the dividend so we don't lose resolution
IntermediateType intermediate(raw_);
// Shift the dividend. FRAC_BITS2 cancels with the fractional bits in
// divisor, and INT_BITS2 adds the required resolution.
intermediate <<= FRAC_BITS2 + INT_BITS2;
intermediate /= divisor.getRaw();
return ResultType::createRaw(intermediate);
}
FixedPoint<INT_BITS, FRAC_BITS>& operator=(const FixedPoint<INT_BITS, FRAC_BITS> value)
{
raw_ = value.getRaw();
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS>& operator=(const IntType value)
{
raw_ = FixedPoint<INT_BITS, FRAC_BITS>(value).getRaw();
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS>& operator=(const float value)
{
raw_ = FixedPoint<INT_BITS, FRAC_BITS>(value).getRaw();
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS>& operator=(const double value)
{
raw_ = FixedPoint<INT_BITS, FRAC_BITS>(value).getRaw();
return *this;
}
/// Shift left by literal amount (just moves radix right)
template <int AMT>
FixedPoint<INT_BITS + AMT, FRAC_BITS - AMT> leftShift() const
{
return FixedPoint<INT_BITS + AMT, FRAC_BITS - AMT>::createRaw(raw_);
}
/// Shift right by literal amount (just moves radix left)
template <int AMT>
FixedPoint<INT_BITS - AMT, FRAC_BITS + AMT> rightShift() const
{
/// \todo Check that this does the right thing arithmetically with negative numbers
return FixedPoint<INT_BITS - AMT, FRAC_BITS + AMT>::createRaw(raw_);
}
/// Write to an output stream
std::ostream& emit(std::ostream& os) const
{
return os << std::fixed << std::setprecision((FRAC_BITS * 3 + 9) / 10) << getValueF();
}
/// For debugging: return the number of bits before the radix
int getIntegralLength() const { return INT_BITS; }
/// For debugging: return the number of bits after the radix
int getFractionalLength() const { return FRAC_BITS; }
/// Get the value as a floating point
double getValueF() const { return ((double)raw_)/(1 << FRAC_BITS); }
/// Get the value truncated to an integer
IntType getValue() const { return (IntType)(raw_ >> FRAC_BITS); }
/// Get the value rounded to an integer (only works if FRAC_BITS > 0)
RoundType round() const { return (RoundType)((raw_ + ((1 << FRAC_BITS) - 1)) >> FRAC_BITS); }
/// Returns the raw internal binary contents
RawType getRaw() const { return raw_; }
static const int BIT_LENGTH = INT_BITS + FRAC_BITS;
static const int FRAC_BITS_LENGTH = FRAC_BITS;
static const int EXP_BITS_LENGTH = FRAC_BITS;
private:
RawType raw_;
};
// Make the fixed-point struct ostream outputtable
template <int INT_BITS, int FRAC_BITS>
std::ostream& operator<< (std::ostream &stream, const FixedPoint<INT_BITS, FRAC_BITS> &fixedPoint)
{
return fixedPoint.emit(stream);
}
#endif /* end of include guard: FIXED_POINT_H__ */