-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixedpoint.h
819 lines (663 loc) · 35.7 KB
/
fixedpoint.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
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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/**
* Copyright (c) 2006 Manuel Bua
*
* THIS SOFTWARE IS PROVIDED 'AS-IS', WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTY. IN NO EVENT WILL THE AUTHORS BE HELD LIABLE FOR ANY DAMAGES
* ARISING FROM THE USE OF THIS SOFTWARE.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*
*/
#ifndef FIXEDPOINT_H
#define FIXEDPOINT_H
#include <stdint.h>
namespace fastmath
{
/**
* Mantains precalculated basic information such
* as bit-masks and other constants.
*/
template<int32_t precision_bits>
class FixedPointInfo
{
protected:
enum { FRACTION_MASK = ( 1 << precision_bits ) - 1 };
enum { ONE = ( 1 << precision_bits ) };
enum { ROUND = ( 1 << ( precision_bits - 1 ) ) };
enum { HALF_BITS = ( precision_bits / 2 ) };
enum { SIGN_BIT = ( 1 << 31 ) };
};
/**
* More comments for me ...
*/
template<int32_t precision_bits,
template <int32_t> class MulPrecisionPolicy,
template <int32_t> class DivPrecisionPolicy>
class FixedPoint : public FixedPointInfo<precision_bits>
{
public:
/**
* Construction w/ precision conversion
*/
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
explicit FixedPoint( const FixedPoint<bits, mulP, divP>& rhs )
{
v = translate( rhs.v, bits );
}
/**
* Assignment w/ precision conversion
*
* \think
* remove because subtle ambiguities?
*/
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint& operator=( const FixedPoint<bits, mulP, divP>& rhs )
{
v = translate( rhs.v, bits );
return *this;
}
/** Construction */
inline static FixedPoint fromRaw( int32_t raw ) { FixedPoint tmp; tmp.v = raw; return tmp; }
FixedPoint() : v( 0 ) {}
FixedPoint( const FixedPoint& rhs ) : v( rhs.v ) {}
explicit FixedPoint( float_t rhs ) : v( (int32_t)( rhs * (float_t)ONE + ( rhs < 0 ? -0.5f : 0.5f ) ) ) {}
explicit FixedPoint( double_t rhs ) : v( (int32_t)( rhs * (double_t)ONE + ( rhs < 0 ? -0.5f : 0.5f ) ) ) {}
explicit FixedPoint( int32_t rhs ) : v( rhs << precision_bits ) {}
/** FixedPoint assignment */
inline FixedPoint& operator=( const FixedPoint& rhs ) { v = rhs.v; return *this; }
inline FixedPoint& operator+=( const FixedPoint& rhs ) { v += rhs.v; return *this; }
inline FixedPoint& operator-=( const FixedPoint& rhs ) { v -= rhs.v; return *this; }
//inline FixedPoint& operator*=( const FixedPoint& rhs ) { v = mul_t::mul( v, rhs.v ); return *this; }
//inline FixedPoint& operator/=( const FixedPoint& rhs ) { v = div_t::div( v, rhs.v ); return *this; }
inline FixedPoint& operator*=( const FixedPoint& rhs ) { v = MulPrecisionPolicy<precision_bits>::mul( v, rhs.v ); return *this; }
inline FixedPoint& operator/=( const FixedPoint& rhs ) { v = DivPrecisionPolicy<precision_bits>::div( v, rhs.v ); return *this; }
/** int32_t assignment */
inline FixedPoint& operator=( int32_t rhs ) { v = FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator+=( int32_t rhs ) { v += rhs << precision_bits; return *this; }
inline FixedPoint& operator-=( int32_t rhs ) { v -= rhs << precision_bits; return *this; }
inline FixedPoint& operator*=( int32_t rhs ) { v *= rhs; return *this; }
inline FixedPoint& operator/=( int32_t rhs ) { v /= rhs; return *this; }
/** float_t assignment */
inline FixedPoint& operator=( float_t rhs ) { v = FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator+=( float_t rhs ) { v += FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator-=( float_t rhs ) { v -= FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator*=( float_t rhs ) { *this *= FixedPoint( rhs ); return *this; }
inline FixedPoint& operator/=( float_t rhs ) { *this /= FixedPoint( rhs ); return *this; }
/** double_t assignment */
inline FixedPoint& operator=( double_t rhs ) { v = FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator+=( double_t rhs ) { v += FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator-=( double_t rhs ) { v -= FixedPoint( rhs ).v; return *this; }
inline FixedPoint& operator*=( double_t rhs ) { *this *= FixedPoint( rhs ); return *this; }
inline FixedPoint& operator/=( double_t rhs ) { *this /= FixedPoint( rhs ); return *this; }
/** shift assignment */
inline FixedPoint& operator<<=( int32_t shift ) { v <<= shift; return *this; }
inline FixedPoint& operator>>=( int32_t shift ) { v >>= shift; return *this; }
/** converters */
inline operator int32_t() { return v >> precision_bits; }
inline operator float_t() { return v * ( 1.0f / static_cast<float_t>( ONE ) ); }
inline operator double_t() { return v * ( 1.0f / static_cast<double_t>( ONE ) ); }
/**
* Gives access to the integer part of the number.
*
* An use of the integer portion could be for lerping fixed-point
* quantities.
*/
inline int32_t getInteger() const { return( v >> precision_bits ); }
/**
* Gives access to the fractional part of the number.
*/
inline uint32_t getFraction() const { return( v & FRACTION_MASK ); }
inline int32_t getRaw() const { return v; }
/** Unary operators */
inline FixedPoint operator+() const { return FixedPoint::fromRaw( v ); }
inline FixedPoint operator-() const { return FixedPoint::fromRaw( -v ); }
/** Comparison operators */
// FixedPoint
inline bool_t operator==( const FixedPoint& other ) { return v == other.v; }
inline bool_t operator!=( const FixedPoint& other ) { return v != other.v; }
inline bool_t operator<( const FixedPoint& other ) { return v < other.v; }
inline bool_t operator>( const FixedPoint& other ) { return v > other.v; }
inline bool_t operator<=( const FixedPoint& other ) { return v <= other.v; }
inline bool_t operator>=( const FixedPoint& other ) { return v >= other.v; }
// int32_t
inline bool_t operator==( int32_t other ) { return v == FixedPoint( other ).v; }
inline bool_t operator!=( int32_t other ) { return v != FixedPoint( other ).v; }
inline bool_t operator<( int32_t other ) { return v < FixedPoint( other ).v; }
inline bool_t operator>( int32_t other ) { return v > FixedPoint( other ).v; }
inline bool_t operator<=( int32_t other ) { return v <= FixedPoint( other ).v; }
inline bool_t operator>=( int32_t other ) { return v >= FixedPoint( other ).v; }
// float
inline bool_t operator==( float_t other ) { return v == FixedPoint( other ).v; }
inline bool_t operator!=( float_t other ) { return v != FixedPoint( other ).v; }
inline bool_t operator<( float_t other ) { return v < FixedPoint( other ).v; }
inline bool_t operator>( float_t other ) { return v > FixedPoint( other ).v; }
inline bool_t operator<=( float_t other ) { return v <= FixedPoint( other ).v; }
inline bool_t operator>=( float_t other ) { return v >= FixedPoint( other ).v; }
// double
inline bool_t operator==( double_t other ) { return v == FixedPoint( other ).v; }
inline bool_t operator!=( double_t other ) { return v != FixedPoint( other ).v; }
inline bool_t operator<( double_t other ) { return v < FixedPoint( other ).v; }
inline bool_t operator>( double_t other ) { return v > FixedPoint( other ).v; }
inline bool_t operator<=( double_t other ) { return v <= FixedPoint( other ).v; }
inline bool_t operator>=( double_t other ) { return v >= FixedPoint( other ).v; }
/** Utilities */
/*
inline FixedPoint mulDiv( const FixedPoint& m1, const FixedPoint& m2, const FixedPoint& d )
{
int64_t t = int64_t( m1 ) * int64_t( m2 );
t += static_cast<int32_t>( ROUND );
t /= d;
return int32_t(t);
}
*/
inline FixedPoint abs()
{
return FixedPoint::fromRaw( ( v & ( 1 << 31 ) ) ? -v : v );
}
inline FixedPoint sign()
{
return FixedPoint::fromRaw( ( v & ( 1 << 31 ) ) ? -static_cast<int32_t>( ONE ): static_cast<int32_t>( ONE ) );
}
inline FixedPoint floor()
{
return FixedPoint( (int32_t)( v >> precision_bits ) );
}
inline FixedPoint ceil()
{
int32_t tmp = -( -v >> precision_bits );
return FixedPoint( tmp );
}
inline FixedPoint round()
{
int64_t tmp = int64_t( v );
if( tmp > 0 ) tmp += static_cast<int32_t>( ROUND ); else tmp-= static_cast<int32_t>( ROUND );
tmp /= static_cast<int32_t>( ONE );
return FixedPoint( int32_t( tmp ) );
}
private:
int32_t v;
inline static int32_t translate( int32_t src, int32_t bits )
{
if( bits < precision_bits )
{
return( src << ( precision_bits - bits ) );
}
else
{
return( src >> ( bits - precision_bits ) );
}
}
//friend FixedPoint<0>; friend FixedPoint<1>; friend FixedPoint<2>; friend FixedPoint<3>;
//friend FixedPoint<4>; friend FixedPoint<5>; friend FixedPoint<6>; friend FixedPoint<7>;
//friend FixedPoint<8>; friend FixedPoint<9>; friend FixedPoint<10>; friend FixedPoint<11>;
//friend FixedPoint<12>; friend FixedPoint<13>; friend FixedPoint<14>; friend FixedPoint<15>;
//friend FixedPoint<16>; friend FixedPoint<17>; friend FixedPoint<18>; friend FixedPoint<19>;
//friend FixedPoint<20>; friend FixedPoint<21>; friend FixedPoint<22>; friend FixedPoint<23>;
//friend FixedPoint<24>; friend FixedPoint<25>; friend FixedPoint<26>; friend FixedPoint<27>;
//friend FixedPoint<28>; friend FixedPoint<29>; friend FixedPoint<30>; friend FixedPoint<31>;
};
//////////////////////////////////////////////////////////////////////////
// "Add" operation on FixedPoint (global)
//////////////////////////////////////////////////////////////////////////
// op: FixedPoint + FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result += rhs;
return result;
}
/**
* int32_t
*/
// op: FixedPoint + int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result += rhs;
return result;
}
// op: int32_t + FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs + lhs;
}
/**
* float_t
*/
// op: FixedPoint + float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result += rhs;
return result;
}
// op: float_t + FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs + lhs;
}
/**
* double_t
*/
// op: FixedPoint + double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result += rhs;
return result;
}
// op: double_t + FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator+( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs + lhs;
}
//////////////////////////////////////////////////////////////////////////
// "Subtract" operation on FixedPoint (global)
//////////////////////////////////////////////////////////////////////////
// op: FixedPoint - FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result -= rhs;
return result;
}
/**
* int32_t
*/
// op: FixedPoint - int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result -= rhs;
return result;
}
// op: int32_t - FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) - rhs;
}
/**
* float_t
*/
// op: FixedPoint - float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result -= rhs;
return result;
}
// op: float_t - FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) - rhs;
}
/**
* double_t
*/
// op: FixedPoint - double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result -= rhs;
return result;
}
// op: double_t - FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator-( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) - rhs;
}
//////////////////////////////////////////////////////////////////////////
// "Multiply" operation on FixedPoint (global)
//////////////////////////////////////////////////////////////////////////
// op: FixedPoint * FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result *= rhs;
return result;
}
/**
* int32_t
*/
// op: FixedPoint * int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result *= rhs;
return result;
}
// op: int32_t * FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs * lhs;
}
/**
* float_t
*/
// op: FixedPoint * float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result *= rhs;
return result;
}
// op: float_t * FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs * lhs;
}
/**
* double_t
*/
// op: FixedPoint * double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result *= rhs;
return result;
}
// op: double_t * FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator*( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return rhs * lhs;
}
//////////////////////////////////////////////////////////////////////////
// "Division" operation on FixedPoint (global)
//////////////////////////////////////////////////////////////////////////
// op: FixedPoint / FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result /= rhs;
return result;
}
/**
* int32_t
*/
// op: FixedPoint / int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result /= rhs;
return result;
}
// op: int32_t / FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) / rhs;
}
/**
* float_t
*/
// op: FixedPoint / float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result /= rhs;
return result;
}
// op: float_t / FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) / rhs;
}
/**
* double_t
*/
// op: FixedPoint / double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs )
{
FixedPoint<bits, mulP, divP> result( lhs );
result /= rhs;
return result;
}
// op: double_t / FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator/( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs )
{
return FixedPoint<bits, mulP, divP>( lhs ) / rhs;
}
//////////////////////////////////////////////////////////////////////////
// Shift operators
//////////////////////////////////////////////////////////////////////////
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator<<( const FixedPoint<bits, mulP, divP>& x, int32_t shift )
{
FixedPoint<bits, mulP, divP> result( x );
result <<= shift;
return result;
}
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP>
inline FixedPoint<bits, mulP, divP> operator>>( const FixedPoint<bits, mulP, divP>& x, int32_t shift )
{
FixedPoint<bits, mulP, divP> result( x );
result >>= shift;
return result;
}
//////////////////////////////////////////////////////////////////////////
// Comparison operators
//////////////////////////////////////////////////////////////////////////
// FixedPoint
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs == rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs != rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs < rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs <= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs > rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( const FixedPoint<bits, mulP, divP>& lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return lhs >= rhs; }
// int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) == rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) != rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) < rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) <= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) > rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( int32_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) >= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs == FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs != FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs < FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs <= FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs > FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( const FixedPoint<bits, mulP, divP>& lhs, int32_t rhs ) { return lhs >= FixedPoint<bits, mulP, divP>( rhs ); }
// float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) == rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) != rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) < rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) <= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) > rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( float_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) >= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs == FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs != FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs < FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs <= FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs > FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( const FixedPoint<bits, mulP, divP>& lhs, float_t rhs ) { return lhs >= FixedPoint<bits, mulP, divP>( rhs ); }
// double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) == rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) != rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) < rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) <= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) > rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( double_t lhs, const FixedPoint<bits, mulP, divP>& rhs ) { return FixedPoint<bits, mulP, divP>( lhs ) >= rhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator==( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs == FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator!=( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs != FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs < FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator<=( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs <= FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs > FixedPoint<bits, mulP, divP>( rhs ); }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline bool_t operator>=( const FixedPoint<bits, mulP, divP>& lhs, double_t rhs ) { return lhs >= FixedPoint<bits, mulP, divP>( rhs ); }
//////////////////////////////////////////////////////////////////////////
// Other assignments
//////////////////////////////////////////////////////////////////////////
// int32_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline int32_t& operator+=( int32_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs + rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline int32_t& operator-=( int32_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs - rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline int32_t& operator*=( int32_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs * rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline int32_t& operator/=( int32_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs / rhs; return lhs; }
// float_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline float_t& operator+=( float_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs + rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline float_t& operator-=( float_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs - rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline float_t& operator*=( float_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs * rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline float_t& operator/=( float_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs / rhs; return lhs; }
// double_t
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline double_t& operator+=( double_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs + rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline double_t& operator-=( double_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs - rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline double_t& operator*=( double_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs * rhs; return lhs; }
template<int32_t bits, template <int32_t> class mulP, template <int32_t> class divP> inline double_t& operator/=( double_t& lhs, const FixedPoint<bits, mulP, divP>& rhs) { lhs = (FixedPoint<bits, mulP, divP>)lhs / rhs; return lhs; }
// local helpers
inline int32_t neg( int32_t r ) { return (r >> 31); }
///////////////////////////////////////////////////////////////////////
// High-precision policies (int64_t)
///////////////////////////////////////////////////////////////////////
template<int32_t bits>
class HighPrecision : public FixedPointInfo<bits>
{
public:
inline static int32_t mul( int32_t l, int32_t r )
{
int64_t t = int64_t(l) * int64_t(r);
t += static_cast<int32_t>( ROUND );
t >>= bits;
return int32_t(t);
/*
int64_t tmp32_32;
int32_t res16_16;
tmp32_32 = l;
tmp32_32 *= r;
// result is now 32:32
tmp32_32 >>= 16; // chop off the lower 16 bits
res16_16 = ( int ) tmp32_32; // chop off the upper 16bits.
// result is now back at 16:16
return res16_16;
*/
}
inline static int32_t div( int32_t l, int32_t r )
{
int64_t t = int64_t(l) << bits;
int32_t q = int32_t( t / r );
int32_t rem = int32_t( t % r );
q += 1 + neg( (int32_t)( (rem << 1 ) - r ) );
return q;
}
/*
inline static int32_t mul( int32_t l, int32_t r )
{
int64_t t = int64_t(l) * int64_t(r);
t += static_cast<int32_t>( ROUND );
t >>= bits;
return int32_t(t);
}
inline static int32_t div( int32_t l, int32_t r )
{
int64_t t = int64_t(l) << bits;
t /= r;
return int32_t(t);
}
*/
};
///////////////////////////////////////////////////////////////////////
// Mid-precision policies (int32_t) *UNSAFE*
///////////////////////////////////////////////////////////////////////
// at now this is UNSAFE if going to overflow
template<int32_t bits>
class MidPrecision : public FixedPointInfo<bits>
{
public:
inline static int32_t mul( int32_t l, int32_t r )
{
register unsigned int a,b;
register bool sign;
int32_t a1 = l, b1 = r;
sign = (a1 ^ b1) < 0;
if (a1 < 0) {a1 = -a1;}
if (b1 < 0) {b1 = -b1;}
// a and b contain integer part
// a1 and b1 contain fractional part.
a = (((unsigned int)a1) >> bits); a1 &= ~(a << bits);
b = (((unsigned int)b1) >> bits); b1 &= ~(b << bits);
a1 = ((a*b) << bits) + (a*b1 + b*a1) +
((unsigned int)((a1*b1) + (1 << (bits-1))) >> bits);
if (a1 < 0) {a1 ^= static_cast<int32_t>( SIGN_BIT );}
if (sign) {a1 = -a1;}
return a1;
}
inline static int32_t div( int32_t l, int32_t r )
{
register int res, mask;
register bool sign;
int32_t a = l, b = r;
sign = (a ^ b) < 0;
if (a < 0) {a = -a;}
if (b < 0) {b = -b;}
mask = (1<<bits); res = 0;
while (a > b) {b <<= 1; mask <<= 1;}
//while (mask != 0)
while (mask > 0)
{
if (a >= b) {res |= mask; a -= b;}
mask >>= 1; b >>= 1;
}
if (res < 0) {res ^= static_cast<int32_t>( SIGN_BIT );}
if (sign) {res = -res;}
return res;
}
};
///////////////////////////////////////////////////////////////////////
// Low-precision policies (int32_t)
///////////////////////////////////////////////////////////////////////
template<int32_t bits>
class LowPrecision : public FixedPointInfo<bits>
{
public:
inline static int32_t mul( int32_t l, int32_t r )
{
return ( ( l * r ) >> bits );
}
inline static int32_t div( int32_t l, int32_t r )
{
return( ( l << bits ) / r );
}
};
// Predefines
typedef FixedPoint< 8, LowPrecision, LowPrecision > fixed8_t;
typedef FixedPoint< 16, HighPrecision, HighPrecision > fixed16_t;
typedef fixed16_t fixed_t;
} // end of namespace fastmath
#endif // FIXEDPOINT_H