forked from coder-mike/FixedPoint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixed_point.hpp
More file actions
693 lines (584 loc) · 21.4 KB
/
fixed_point.hpp
File metadata and controls
693 lines (584 loc) · 21.4 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#ifndef FIXED_POINT_H__
#define FIXED_POINT_H__
#include <ostream>
#include <iomanip>
#include <iostream>
#include "template_utils.hpp"
enum OverflowMode
{
MASK,
CLAMP
};
extern OverflowMode OVERFLOW_MODE;
/// A fixed-point integer type
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;
typedef typename GET_INT_WITH_LENGTH<INT_BITS*2 + FRAC_BITS*2>::RESULT MultType;
/// 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;
};
/// Create a fixed-point with equivalent integer value
/** For example in 4.12 fixed-point, the number "2" is 0010.000000000000 */
FixedPoint(int value){
if(OVERFLOW_MODE == OverflowMode::MASK){
bool is_neg = value < 0;
if (is_neg){
value = -value;
}
raw_ = (int64_t)value << FRAC_BITS;
mask = ((int64_t)1 << (FRAC_BITS+INT_BITS)) - 1;
applyMask();
if (is_neg){
raw_ = -raw_;
}
}
else{
if(value > max_val){
raw_ = max_val;
}
else if(value < min_val){
raw_ = min_val;
}
else{
raw_ = (int64_t)value << FRAC_BITS;
}
}
}
// Terrible negative number handling, will fix it
FixedPoint(double value){
if(OVERFLOW_MODE == OverflowMode::MASK){
bool is_neg = value < 0;
if (is_neg){
value = -value;
}
raw_ = value * ((int64_t)1 << FRAC_BITS);
mask = ((int64_t)1 << (FRAC_BITS+INT_BITS)) - 1;
applyMask();
if (is_neg){
raw_ = -raw_;
}
}
else{
if(value > max_val_f){
raw_ = max_val;
}
else if(value < min_val_f){
raw_ = min_val;
}
else{
raw_ = value * ((int64_t)1 << FRAC_BITS);
}
}
}
// Terrible negative number handling, will fix it
// MASK MAY BE BUGGY TODO:
FixedPoint(float value){
if(OVERFLOW_MODE == OverflowMode::MASK){
bool is_neg = value < 0;
if (is_neg){
value = -value;
}
raw_ = value * ((int64_t)1 << FRAC_BITS);
mask = ((int64_t)1 << (FRAC_BITS+INT_BITS)) - 1;
applyMask();
if (is_neg){
raw_ = -raw_;
}
}
else{
if(value > max_val_f){
raw_ = max_val;
}
else if(value < min_val_f){
raw_ = min_val;
}
else{
raw_ = value * ((int64_t)1 << FRAC_BITS);
}
}
}
/// Default constructor
FixedPoint(){
raw_ = 0;
mask = ((int64_t)1 << (FRAC_BITS+INT_BITS)) - 1;
//applyMask();
}
FixedPoint(RawValue value): raw_(value.value) {
mask = ((int64_t)1 << (FRAC_BITS+INT_BITS)) - ((int64_t)1);
//applyMask();
}
void applyMask(){
raw_ &= mask;
}
/// 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)); }
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_);
}
template <int INT_BITS_NEW, int FRAC_BITS_NEW>
FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW> cast()
{
// use getValueF() to convert to double
return FixedPoint<INT_BITS_NEW, FRAC_BITS_NEW>(getValueF());
}
// cast to a new type
template <typename T>
T cast()
{
return T(getValueF());
}
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_));
}
// https://vanhunteradams.com/FixedPoint/FixedPoint.html
FixedPoint<INT_BITS, FRAC_BITS> operator*(FixedPoint<INT_BITS, FRAC_BITS> value) const
{
int256_t raw_shifted = raw_;
int256_t value_shifted = value.getRaw();
int256_t mult = raw_shifted * value_shifted;
// mask the last INT_BITS bits
int256_t tmp1 = mult << INT_BITS;
int256_t tmp2 = tmp1 >> INT_BITS;
mult = tmp2;
// get rid of underflow
mult >>= FRAC_BITS;
if(OVERFLOW_MODE == OverflowMode::MASK){
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(mult.convert_to<RawType>());
}
else{
if(mult > max_val){
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(max_val);
}
else if(mult < min_val){
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(min_val);
}
else{
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(mult.convert_to<RawType>());
}
}
}
// The intermediate type is set to some arbitrary value, there may be a better way to do this
FixedPoint<INT_BITS, FRAC_BITS> operator*(int value) const
{
auto temp = FixedPoint<INT_BITS*2, FRAC_BITS*2>::createRaw(raw_ * value);
return temp.template convert<INT_BITS, FRAC_BITS>();
}
// Multiplication with a double
FixedPoint<INT_BITS, FRAC_BITS> operator*(double value) const
{
auto temp = FixedPoint<INT_BITS*2, FRAC_BITS*2>::createRaw(raw_ * value);
return temp.template convert<INT_BITS, FRAC_BITS>();
}
// *= overload
FixedPoint<INT_BITS, FRAC_BITS>& operator*=(FixedPoint<INT_BITS, FRAC_BITS> value)
{
raw_ *= value.template convert<INT_BITS, FRAC_BITS>().getRaw();
if(OVERFLOW_MODE == OverflowMode::MASK){
applyMask();
}
else{
if(raw_ > max_val){
raw_ = max_val;
}
else if(raw_ < min_val){
raw_ = min_val;
}
}
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS>& operator*=(int value)
{
raw_ *= FixedPoint<INT_BITS, FRAC_BITS>(value).getRaw();
if(OVERFLOW_MODE == OverflowMode::MASK){
applyMask();
}
else{
if(raw_ > max_val){
raw_ = max_val;
}
else if(raw_ < min_val){
raw_ = min_val;
}
}
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS>& operator*=(double value)
{
raw_ *= FixedPoint<INT_BITS, FRAC_BITS>(value).getRaw();
if(OVERFLOW_MODE == OverflowMode::MASK){
applyMask();
}
else{
if(raw_ > max_val){
raw_ = max_val;
}
else if(raw_ < min_val){
raw_ = min_val;
}
}
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS> operator+(IntType value) const
{
return *this + FixedPoint<INT_BITS, FRAC_BITS>(value);
}
FixedPoint<INT_BITS, FRAC_BITS> operator+(FixedPoint<INT_BITS, FRAC_BITS> value) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(value.template convert<INT_BITS, FRAC_BITS>());
if(OVERFLOW_MODE == OverflowMode::MASK){
return ResultType::createRaw(op1.getRaw() + op2.getRaw());
}
else{
double val1 = op1.getValueF();
double val2 = op2.getValueF();
double result = val1 + val2;
if(result > max_val_f){
return ResultType::createRaw(max_val);
}
else if(result < min_val_f){
return ResultType::createRaw(min_val);
}
else{
return ResultType::createRaw(op1.getRaw() + op2.getRaw());
}
}
}
FixedPoint<INT_BITS, FRAC_BITS> operator+(float value) const
{
return *this + FixedPoint<INT_BITS, FRAC_BITS>(value);
}
FixedPoint<INT_BITS, FRAC_BITS> operator+(double value) const
{
return *this + FixedPoint<INT_BITS, FRAC_BITS>(value);
}
ThisType& operator+=(FixedPoint<INT_BITS, FRAC_BITS> value)
{
raw_ += value.template convert<INT_BITS, FRAC_BITS>().getRaw();
if(OVERFLOW_MODE == OverflowMode::MASK){
applyMask();
}
else{
if(raw_ > max_val){
raw_ = max_val;
}
else if(raw_ < min_val){
raw_ = min_val;
}
}
return *this;
}
ThisType& operator+=(double value)
{
raw_ += ThisType(value).getRaw();
return *this;
}
ThisType& operator+=(float value)
{
raw_ += ThisType(value).getRaw();
return *this;
}
ThisType& operator-=(FixedPoint<INT_BITS, FRAC_BITS> value)
{
raw_ -= value.template convert<INT_BITS, FRAC_BITS>().getRaw();
if(OVERFLOW_MODE == OverflowMode::MASK){
applyMask();
}
else{
if(raw_ > max_val){
raw_ = max_val;
}
else if(raw_ < min_val){
raw_ = min_val;
}
}
return *this;
}
FixedPoint<INT_BITS, FRAC_BITS> operator-(FixedPoint<INT_BITS, FRAC_BITS> value) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(value.template convert<INT_BITS, FRAC_BITS>());
if(OVERFLOW_MODE == OverflowMode::MASK){
return ResultType::createRaw(op1.getRaw() - op2.getRaw());
}
else{
double val1 = op1.getValueF();
double val2 = op2.getValueF();
double result = val1 - val2;
if(result > max_val_f){
return ResultType::createRaw(max_val);
}
else if(result < min_val_f){
return ResultType::createRaw(min_val);
}
else{
return ResultType::createRaw(op1.getRaw() - op2.getRaw());
}
}
}
FixedPoint<INT_BITS, FRAC_BITS> operator-(double value) const
{
return *this - FixedPoint<INT_BITS, FRAC_BITS>(value);
}
// Unary minus
FixedPoint<INT_BITS, FRAC_BITS> operator-() const
{
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(-raw_);
}
bool operator< (FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() < op2.getRaw();
}
bool operator> (FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() > op2.getRaw();
}
bool operator>= (FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() >= op2.getRaw();
}
bool operator<= (FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() <= op2.getRaw();
}
bool operator==(FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() == op2.getRaw();
}
bool operator==(double other) const
{
return getValueF() == other;
}
bool operator!= (FixedPoint<INT_BITS, FRAC_BITS> other) const
{
// This is the type of the result
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
// Convert both operands to result type
ResultType op1(this->convert<INT_BITS, FRAC_BITS>());
ResultType op2(other.template convert<INT_BITS, FRAC_BITS>());
// Then the operation is trivial
return op1.getRaw() != op2.getRaw();
}
FixedPoint<INT_BITS, FRAC_BITS> operator/(FixedPoint<INT_BITS, FRAC_BITS> divisor) const
{
// (A/2^B)/(C/2^D) = (A/C)/2^(B-D);
// fpm library says the normal fixed-point division is:
// x * 2**Frac_bits / y
typedef FixedPoint<INT_BITS, FRAC_BITS> ResultType;
typedef typename GET_INT_WITH_LENGTH<INT_BITS*2 + FRAC_BITS*2>::RESULT IntermediateType;
// If the divisor is zero, return the maximum value
if (divisor.getRaw() == 0)
{
std::cerr << "Division by zero happened" << std::endl;
return ResultType::createRaw(max_val);
}
IntermediateType int_frac((int64_t)1 << FRAC_BITS);
// Expand the dividend so we don't lose resolution
IntermediateType intermediate(raw_ * int_frac);
// Shift the dividend. FRAC_BITS2 cancels with the fractional bits in
// divisor, and INT_BITS2 adds the required resolution.
//intermediate <<= FRAC_BITS + INT_BITS;
//std::cout << "divisor: " << divisor.getRaw() << std::endl;
//divisor += 0.001; // just so it doesn't divide by zero
//std::cout << "intermediate: " << intermediate << std::endl;
//std::cout << "divisor: " << divisor.getRaw() << std::endl;
intermediate /= divisor.getRaw();
//std::cout << "intermediate: " << intermediate << std::endl;
return ResultType::createRaw(intermediate);
}
FixedPoint<INT_BITS, FRAC_BITS> operator/(double divisor) const
{
typedef typename GET_INT_WITH_LENGTH<INT_BITS*2 + FRAC_BITS*2>::RESULT IntermediateType;
// If the divisor is zero, return the maximum value
if (divisor == 0)
{
std::cerr << "Division by zero happened" << std::endl;
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(max_val);
}
IntermediateType int_frac(1LL << FRAC_BITS);
// Expand the dividend so we don't lose resolution
IntermediateType intermediate(raw_ * int_frac);
// Shift the dividend. FRAC_BITS2 cancels with the fractional bits in
// divisor, and INT_BITS2 adds the required resolution.
//intermediate <<= FRAC_BITS + INT_BITS;
intermediate /= divisor;
return FixedPoint<INT_BITS, FRAC_BITS>::createRaw(intermediate);
}
FixedPoint<INT_BITS, FRAC_BITS>& operator/=(FixedPoint<INT_BITS, FRAC_BITS> divisor)
{
typedef typename GET_INT_WITH_LENGTH<INT_BITS*2 + FRAC_BITS*2>::RESULT IntermediateType;
// If the divisor is zero, return the maximum value
if (divisor.getRaw() == 0)
{
std::cerr << "Division by zero happened" << std::endl;
raw_ = max_val;
return *this;
}
IntermediateType int_frac(1LL << FRAC_BITS);
// Expand the dividend so we don't lose resolution
IntermediateType intermediate(raw_ * int_frac);
// Shift the dividend. FRAC_BITS2 cancels with the fractional bits in
// divisor, and INT_BITS2 adds the required resolution.
//intermediate <<= FRAC_BITS + INT_BITS;
intermediate /= divisor;
return *this;
}
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();
}
bool isfinite() const
{
return true;
}
// an overload to be used when this type is converted to a double
operator double() const
{
return getValueF();
}
FixedPoint<INT_BITS, FRAC_BITS> sin() const
{
// Take the raw value, calculate sin and return it
return FixedPoint<INT_BITS, FRAC_BITS>(std::sin(getValueF()));
}
FixedPoint<INT_BITS, FRAC_BITS> cos() const
{
// Take the raw value, calculate cos and return it
return FixedPoint<INT_BITS, FRAC_BITS>(std::cos(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 {
//std::cout << "raw_ is " << raw_ << std::endl;
//std::cout << "1LL << FRAC_BITS is " << (1LL << FRAC_BITS) << std::endl;
//std::cout << "double val is " << (raw_)/(double)(1LL << FRAC_BITS) << std::endl;
return (raw_)/(double)(1LL << 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_ + ((1LL << 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_;
int64_t mask;
//int64_t max_val = (int64_t)(((int256_t)1 << (FRAC_BITS+INT_BITS-1)) - 1);
// int256_t is not working for some systems as expected
int64_t max_val = (int64_t)(((int64_t)1 << (FRAC_BITS+INT_BITS-1)) - 1);
int64_t min_val = -(int64_t)(((int64_t)1 << (FRAC_BITS+INT_BITS-1)));
double max_val_f = (double)max_val/(1LL << FRAC_BITS);
double min_val_f = (double)min_val/(1LL << FRAC_BITS);
};
// 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__ */