-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixed_point.hpp
1470 lines (1237 loc) · 47.9 KB
/
fixed_point.hpp
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
--------------------------------------------------------------------------------
Fixed point C++ template class
Copyright (c) 2012, Pierre-Marc Jobin, Oleg Endo
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the names of any
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--------------------------------------------------------------------------------
This source code uses C++11 language and library features and thus requires
a compiler and an STL implementation that supports C++11.
Known to work:
GCC 4.7, libstdc++-v3
Clang 3.1, libc++
--------------------------------------------------------------------------------
This fixed point template class is supposed to serve as a drop-in replacement
for floating point numbers in situations where floating point usage is not
desired for whatever reason. It allows one to define custom fixed point types
with varying bit counts for the integral and fractional parts of the fixed
point number.
For example, one common 32-bit fixed point format is the 16.16 format, which
defines 16 bits for the integral and fractional parts.
An example how to define a signed 16.16 fixed point type (old syntax):
typedef fixed_point<int32_t, 16, 16> fxpt_16_16;
An example how to define a signed 16.16 fixed point type (new syntax):
fxpt_16_16 = using fixed_point<int32_t, 16, 16>;
The implementation of fixed point operations emphasizes the usage of widening
multiplications, which allows utilization of widening integer
multiply-accumulate instructions on various processors.
An example of a widening multiplication would be:
int64_t func (int32_t a, int32_t b)
{
return static_cast<int64_t> (a) * static_cast<int64_t> (b);
}
On many processors this is a single instruction operation, which takes
two 32-bit values and outputs one 64-bit value.
An example of a widening multiply-accumulate would be:
int64_t func (int32_t a, int32_t b, int64_t c)
{
return static_cast<int64_t> (a) * static_cast<int64_t> (b) + c;
}
This operation is also often implemented in a single instruction on
processors, especially those that are designed for DSP-style applications.
Once the desired integral and fractional precision has been defined, fixed
point numbers can be used just as regular numbers. Notice that it will
automatically use widening multiplication operations behind the scenes.
An example how to multiply two signed 16.16 fixed point numbers:
fxpt_16_16 func (fxpt_16_16 a, fxpt_16_16 b)
{
// expanded operations:
return a * b; // 32-bit * 32-bit -> 64-bit intermediate
// 64-bit -> 32-bit final result
}
An example how to multiply-accumulate signed 16.16 fixed point numbers:
fxpt_16_16 func (fxpt_16_16 a, fxpt_16_16 b, fxpt_16_16 c, fxpt_16_16 d)
{
// expanded operations:
return a * b + c * d; // 32-bit * 32-bit -> 64-bit intermediate
// 32-bit * 32-bit -> 64-bit intermediate
// 64-bit + 64-bit -> 64-bit intermediate
// 64-bit -> 32-bit final result
}
By default the fixed_point template class is not placed in a namespace.
The enclosing namespace can be customized as follows:
#define __FIXED_POINT_BEGIN_NAMESPACE__ namespace test { namespace math {
#define __FIXED_POINT_END_NAMESPACE__ } }
#define __FIXED_POINT_USE_NAMESPACE__ test::math::
#include "fixed_point.hpp"
This will put the fixed_point template class into the namespace test::math.
The following standard library functions are implemented for
fixed point types so far:
std::numeric_limits
std::is_arithmetic (-> true)
std::is_signed
std::is_unsigned
std::is_pod
std::is_integral (-> false)
std::is_floating_point (-> false)
(std::is_trivially_copyable - disabled due to missing libstdc++-v3 support)
std::is_standard_layout
std::is_literal_type
std::make_signed
std::make_unsigned
std::abs, std::fabs
std::min, std::fmin
std::max, std::fmax
std::fma
std::fdim
std::fpclassify (-> FP_ZERO, FP_NORMAL)
std::isfinite (-> true)
std::isinf (-> false)
std::isnan (-> false)
std::isnormal
std::signbit
std::copysign
std::trunc
*/
#ifndef __FIXED_POINT_HPP_INCLUDED__
#define __FIXED_POINT_HPP_INCLUDED__
#include <type_traits>
#include <limits>
#include <cstdint>
#include <cmath>
#ifndef __FIXED_POINT_BEGIN_NAMESPACE__
#define __FIXED_POINT_BEGIN_NAMESPACE_DO_UNDEF_AFTER__
#define __FIXED_POINT_BEGIN_NAMESPACE__
#endif
#ifndef __FIXED_POINT_END_NAMESPACE__
#define __FIXED_POINT_END_NAMESPACE__
#define __FIXED_POINT_END_NAMESPACE_DO_UNDEF_AFTER__
#endif
__FIXED_POINT_BEGIN_NAMESPACE__
template <typename U> struct fixed_point_widened_raw_type;
template <> struct fixed_point_widened_raw_type<std::int8_t> { typedef std::int16_t type; };
template <> struct fixed_point_widened_raw_type<std::uint8_t> { typedef std::uint16_t type; };
template <> struct fixed_point_widened_raw_type<std::int16_t> { typedef std::int32_t type; };
template <> struct fixed_point_widened_raw_type<std::uint16_t> { typedef std::uint32_t type; };
template <> struct fixed_point_widened_raw_type<std::int32_t> { typedef std::int64_t type; };
template <> struct fixed_point_widened_raw_type<std::uint32_t> { typedef std::uint64_t type; };
template <> struct fixed_point_widened_raw_type<std::int64_t> { typedef void type; };
template <> struct fixed_point_widened_raw_type<std::uint64_t> { typedef void type; };
template <typename U> struct fixed_point_narrowed_raw_type;
template <> struct fixed_point_narrowed_raw_type<std::int8_t> { typedef void type; };
template <> struct fixed_point_narrowed_raw_type<std::uint8_t> { typedef void type; };
template <> struct fixed_point_narrowed_raw_type<std::int16_t> { typedef std::int8_t type; };
template <> struct fixed_point_narrowed_raw_type<std::uint16_t> { typedef std::uint8_t type; };
template <> struct fixed_point_narrowed_raw_type<std::int32_t> { typedef std::int16_t type; };
template <> struct fixed_point_narrowed_raw_type<std::uint32_t> { typedef std::uint16_t type; };
template <> struct fixed_point_narrowed_raw_type<std::int64_t> { typedef std::int32_t type; };
template <> struct fixed_point_narrowed_raw_type<std::uint64_t> { typedef std::uint32_t type; };
enum fixed_point_raw_init_tag
{
FIXED_POINT_RAW
};
template <typename T, unsigned I, unsigned F, bool W> class fixed_point;
// empty partial specialization to avoid problems when instantiating
// impossible narrowed / widened nested type
template <unsigned I, unsigned F, bool W> class fixed_point <void, I, F, W> { };
template <typename T, unsigned I,
unsigned F = std::is_signed<T>::value + std::numeric_limits<T>::digits - I,
bool W = false>
class fixed_point
{
public:
typedef T raw_type;
static constexpr unsigned integral_bits = I;
static constexpr unsigned fractional_bits = F;
static constexpr raw_type fractional_mask = (raw_type (1) << fractional_bits) - 1;
static constexpr raw_type integral_mask = ~fractional_mask;
static constexpr bool is_widened = W;
protected:
T value;
typedef typename fixed_point_widened_raw_type<raw_type>::type widened_raw_type;
typedef typename fixed_point_narrowed_raw_type<raw_type>::type narrowed_raw_type;
typedef fixed_point<widened_raw_type, integral_bits*2, fractional_bits*2, true> widened_fixed_type;
typedef fixed_point<narrowed_raw_type, integral_bits/2, fractional_bits/2, false> narrowed_fixed_type;
static_assert (std::is_integral<raw_type>::value
, "fixed_point requires integral raw type");
static_assert (std::is_signed<T>::value + std::numeric_limits<T>::digits
== integral_bits + fractional_bits
, "fixed_point integral and fractional bits do not match the raw type width");
static_assert (integral_bits > 0
, "fixed_point integral bit count cannot be zero");
template <typename otherT, typename Enable = void> struct cast;
template <typename otherT>
struct cast<otherT, typename std::enable_if<std::is_integral<otherT>::value>::type>
{
static constexpr raw_type from (const otherT& value) noexcept
{
return static_cast<raw_type> (value) << fractional_bits;
}
static constexpr otherT to (const raw_type& value) noexcept
{
return static_cast<otherT> (value >> fractional_bits);
}
};
template <typename otherT>
struct cast<otherT, typename std::enable_if<std::is_floating_point<otherT>::value>::type>
{
static constexpr otherT one (void) noexcept
{
return static_cast<otherT> (raw_type (1) << fractional_bits);
}
static constexpr raw_type from (const otherT& value) noexcept
{
return static_cast<raw_type> (value * one ());
}
static constexpr otherT to (const raw_type& value) noexcept
{
return static_cast<otherT> (value) / one ();
}
};
public:
constexpr fixed_point (void) noexcept = default;
constexpr fixed_point (const fixed_point&) noexcept = default;
fixed_point& operator = (const fixed_point&) = default;
constexpr explicit fixed_point (const raw_type& _raw_value, fixed_point_raw_init_tag) noexcept
: value (_raw_value)
{ }
// construction from other fixed_point type is explicit
// ???: explicit if truncating (otherI > I || otherF > F)
// implicit if promoting (otherI <= I && otherF <= F)
template <typename otherT, unsigned otherI, unsigned otherF, bool otherW>
constexpr explicit fixed_point (const fixed_point<otherT, otherI, otherF, otherW>& other) noexcept
//: value ( ((fixed_point)other).raw () ) // this causes an infinite loop
: value (other.convert_to<raw_type, integral_bits, fractional_bits, is_widened> ().raw ())
{ }
// construction from integral or floating point type is implicit
template <typename otherT>
constexpr fixed_point (const otherT& other_value) noexcept
: value (cast<otherT>::from(other_value))
{ }
// conversion to another fixed_point type must be explicit
// ???: explicit if truncating (I > otherI || F > otherF)
// implicit if promoting (I <= otherI && F <= otherF)
template<typename otherT, unsigned otherI, unsigned otherF, bool otherW>
constexpr explicit operator fixed_point<otherT, otherI, otherF, otherW> () const noexcept
{
return convert_to<otherT, otherI, otherF, otherW> ();
}
// conversion to a narrowed type is implicit
constexpr operator narrowed_fixed_type (void) const noexcept
{
return convert_to<typename narrowed_fixed_type::raw_type,
narrowed_fixed_type::integral_bits,
narrowed_fixed_type::fractional_bits, false> ();
}
// conversion to a widened type is implicit
constexpr operator widened_fixed_type (void) const noexcept
{
return convert_to<typename widened_fixed_type::raw_type,
widened_fixed_type::integral_bits,
widened_fixed_type::fractional_bits, true> ();
}
// conversion to bool is explicit
constexpr explicit operator bool (void) const noexcept
{
return value != 0;
}
// conversion to integral or floating point type is explicit
template <typename otherT>
constexpr explicit operator otherT (void) const noexcept
{
return cast<otherT>::to (value);
}
// pre-increment
fixed_point& operator ++ (void) noexcept
{
*this += fixed_point (1);
return *this;
}
// post-increment
const fixed_point operator ++ (int) noexcept
{
fixed_point prev = *this;
*this += fixed_point (1);
return prev;
}
// pre-decrement
fixed_point& operator -- (void) noexcept
{
*this -= fixed_point (1);
return *this;
}
// post-decrement
const fixed_point operator -- (int) noexcept
{
fixed_point prev = *this;
*this -= fixed_point (1);
return prev;
}
// unary plus
const fixed_point operator + (void) const noexcept
{
return *this;
}
// fixed_point + fixed_point -> fixed_point
constexpr friend const fixed_point operator + (const fixed_point& lhs, const fixed_point& rhs) noexcept
{
return fixed_point (lhs.value + rhs.value, FIXED_POINT_RAW);
}
// widened_fixed + (widened_fixed)fixed_point -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, narrowed_fixed_type>::value
, const fixed_point>::type
operator + (const otherT& lhs, const otherR& rhs) noexcept
{
return lhs + (fixed_point)rhs;
}
// (widened_fixed)fixed + widened_fixed -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, narrowed_fixed_type>::value
&& std::is_same<typename std::remove_cv<otherR>::type, fixed_point>::value
, const fixed_point>::type
operator + (const otherT& lhs, const otherR& rhs) noexcept
{
return rhs + lhs;
}
fixed_point& operator += (const fixed_point& rhs) noexcept
{
*this = *this + rhs;
return *this;
}
/*
let the compiler figure it out, it can do a better job at it.
// fixed += widened_fixed
// do the addition on the widened_fixed and narrow to fixed.
// this can increase MAC opportunities.
fixed_point& operator += (const widened_fixed_type& rhs) noexcept
{
*this = rhs + *this;
return *this;
}
*/
// unary minus
const fixed_point operator - (void) const noexcept
{
return fixed_point (-value, FIXED_POINT_RAW);
}
// fixed - fixed -> fixed
constexpr friend const fixed_point operator - (const fixed_point& lhs, const fixed_point& rhs) noexcept
{
return fixed_point (lhs.value - rhs.value, FIXED_POINT_RAW);
}
// widened_fixed - (widened_fixed)fixed -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, narrowed_fixed_type>::value
, const fixed_point>::type
operator - (const otherT& lhs, const otherR& rhs) noexcept
{
return lhs - (fixed_point)rhs;
}
// (widened_fixed)fixed - widened_fixed -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, narrowed_fixed_type>::value
&& std::is_same<typename std::remove_cv<otherR>::type, fixed_point>::value
, const fixed_point >::type
operator - (const otherT& lhs, const otherR& rhs) noexcept
{
return (fixed_point)lhs - rhs;
}
fixed_point& operator -= (const fixed_point& rhs) noexcept
{
value -= rhs.value;
return *this;
}
/*
let the compiler figure it out, it can do a better job at it.
// fixed -= widened_fixed
// do the subtraction on the widened_fixed and narrow to fixed.
// this can increase MAC opportunities.
fixed_point& operator -= (const widened_fixed_type& rhs) noexcept
{
*this = *this - rhs;
return *this;
}
*/
// fixed * fixed -> widened_fixed
// ??? this might be not so optimal when multiplying fixed point types with
// different fraction bit counts.
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
, const widened_fixed_type>::type
operator * (const otherT& lhs, const otherT& rhs) noexcept
{
static_assert (!std::is_void <widened_raw_type>::value
, "widened type for multiplication result is not available");
return widened_fixed_type (static_cast<widened_raw_type>(lhs.raw ())
* static_cast<widened_raw_type>(rhs.raw ()), FIXED_POINT_RAW);
}
fixed_point& operator *= (const fixed_point& rhs) noexcept
{
*this = (fixed_point)(*this * rhs); // re-use mul definition above
return *this;
}
// (fixed_point)widened_fixed * fixed -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, narrowed_fixed_type>::value
, const fixed_point>::type
operator * (const otherT& lhs, const otherR& rhs) noexcept
{
return (narrowed_fixed_type)lhs * rhs;
}
// (fixed_point)widened_fixed * (fixed_point)widened_fixed -> widened_fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, fixed_point>::value
, const fixed_point>::type
operator * (const otherT& lhs, const otherR& rhs) noexcept
{
return (narrowed_fixed_type)lhs * (narrowed_fixed_type)rhs;
}
// multiplication with integral type does not need a conversion of the
// integral value to fixed point but results in a widened type.
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened && std::is_integral<otherT>::value, const widened_fixed_type>::type
operator * (const fixed_point& lhs, const otherT& rhs) noexcept
{
return widened_fixed_type (static_cast<widened_raw_type>(lhs.raw ())
* static_cast<widened_raw_type>(rhs), FIXED_POINT_RAW);
}
template <typename otherT>
constexpr friend typename
std::enable_if<is_widened && std::is_integral<otherT>::value, const fixed_point>::type
operator * (const fixed_point& lhs, const otherT& rhs) noexcept
{
return narrowed_fixed_type (lhs) * rhs;
}
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened && std::is_integral<otherT>::value, const widened_fixed_type>::type
operator * (const otherT& lhs, const fixed_point& rhs) noexcept
{
return rhs * lhs;
}
template <typename otherT>
constexpr friend typename
std::enable_if<is_widened && std::is_integral<otherT>::value, const fixed_point>::type
operator * (const otherT& lhs, const fixed_point& rhs) noexcept
{
return rhs * lhs;
}
template <typename otherT>
typename std::enable_if<std::is_integral<otherT>::value, fixed_point&>::type
operator *= (const otherT& rhs) noexcept
{
*this = *this * rhs;
return *this;
}
// multiplications with not fixed and non-integral types (e.g. floats)
// this requires conversion of the other type to fixed type first, to be on
// the safe side.
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened && !std::is_integral<otherT>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
, const widened_fixed_type>::type
operator * (const fixed_point& lhs, const otherT& rhs) noexcept
{
return lhs * fixed_point (rhs);
}
template <typename otherT>
constexpr friend typename
std::enable_if<is_widened
&& !std::is_integral<otherT>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, narrowed_fixed_type>::value
, const fixed_point>::type
operator * (const fixed_point& lhs, const otherT& rhs) noexcept
{
return narrowed_fixed_type (lhs) * narrowed_fixed_type (rhs);
}
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened
&& !std::is_integral<otherT>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
, const widened_fixed_type>::type
operator * (const otherT& lhs, const fixed_point& rhs) noexcept
{
return rhs * lhs;
}
template <typename otherT>
constexpr friend typename
std::enable_if<is_widened
&& !std::is_integral<otherT>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& !std::is_same<typename std::remove_cv<otherT>::type, narrowed_fixed_type>::value
, const fixed_point>::type
operator * (const otherT& lhs, const fixed_point& rhs) noexcept
{
return rhs * lhs;
}
// (widened_fixed)fixed / fixed -> fixed
template <typename otherT>
constexpr friend typename
std::enable_if<!is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
, const fixed_point>::type
operator / (const otherT& lhs, const otherT& rhs) noexcept
{
return fixed_point ((static_cast<widened_raw_type>(lhs.raw ()) << fractional_bits) / rhs.raw (), FIXED_POINT_RAW);
}
fixed_point& operator /= (const fixed_point& rhs) noexcept
{
*this = *this / rhs; // re-use div definition above
return *this;
}
// widened / fixed -> fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, narrowed_fixed_type>::value
, const narrowed_fixed_type>::type
operator / (const otherT& lhs, const otherR& rhs) noexcept
{
return narrowed_fixed_type (lhs.raw () / rhs.raw (), FIXED_POINT_RAW);
}
// fixed / (fixed)widened -> fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, narrowed_fixed_type>::value
&& std::is_same<typename std::remove_cv<otherR>::type, fixed_point>::value
, const narrowed_fixed_type>::type
operator / (const otherT& lhs, const otherR& rhs) noexcept
{
return lhs / (narrowed_fixed_type)rhs;
}
// widened / (fixed)widened -> fixed
template <typename otherT, typename otherR>
constexpr friend typename
std::enable_if<is_widened
&& std::is_same<typename std::remove_cv<otherT>::type, fixed_point>::value
&& std::is_same<typename std::remove_cv<otherR>::type, fixed_point>::value
, const narrowed_fixed_type>::type
operator / (const otherT& lhs, const otherR& rhs) noexcept
{
return lhs / (narrowed_fixed_type)rhs;
}
// fixed,widened / int -> fixed,widened
template <typename otherT>
constexpr friend typename std::enable_if <std::is_integral<otherT>::value, const fixed_point>::type
operator / (const fixed_point& lhs, const otherT& rhs) noexcept
{
return fixed_point (lhs.raw () / rhs, FIXED_POINT_RAW);
}
template <typename otherT>
typename std::enable_if <std::is_integral<otherT>::value, fixed_point&>::type
operator /= (const otherT& rhs) noexcept
{
*this = *this / rhs;
return *this;
}
// relationals
constexpr bool operator == (const fixed_point& rhs) const noexcept
{
return value == rhs.value;
}
constexpr bool operator != (const fixed_point& rhs) const noexcept
{
return value != rhs.value;
}
constexpr bool operator < (const fixed_point& rhs) const noexcept
{
return value < rhs.value;
}
constexpr bool operator <= (const fixed_point& rhs) const noexcept
{
return value <= rhs.value;
}
constexpr bool operator > (const fixed_point& rhs) const noexcept
{
return value > rhs.value;
}
constexpr bool operator >= (const fixed_point& rhs) const noexcept
{
return value >= rhs.value;
}
constexpr bool operator ! (void) const noexcept
{
return value == 0;
}
constexpr const raw_type& raw (void) const noexcept { return value; }
// have to leave the convert_to public although it is supposed
// to be used privately.
// same number of fractional bits -> convert raw_type only
template<typename destT, unsigned destI, unsigned destF, bool destW = false>
constexpr typename
std::enable_if<(destF == fractional_bits), fixed_point<destT, destI, destF, destW>>::type
convert_to (void) const noexcept
{
return fixed_point<destT, destI, destF, destW> (static_cast<destT> (value), FIXED_POINT_RAW);
}
// increase number of fractional bits -> left shift
// do the shift after the base type cast, so that if we're converting
// to a type with more total bits the bits are not shifted out.
// if converting to a dest type with fewer total bits, the bits will
// be shifted out anyway.
template<typename destT, unsigned destI, unsigned destF, bool destW = false>
constexpr typename std::enable_if<(destF > fractional_bits), fixed_point<destT, destI, destF, destW>>::type
convert_to (void) const noexcept
{
return fixed_point<destT, destI, destF, destW> (static_cast<destT> (value) << (destF - fractional_bits), FIXED_POINT_RAW);
}
// decrease number of fractional bits -> right shift
// do the shift before the type cast, so that we don't cut off integral
// bits if converting to a type with fewer total bits.
// if converting to a type with more total bits, it will be just extended.
template<typename destT, unsigned destI, unsigned destF, bool destW = false>
constexpr typename std::enable_if<(destF < fractional_bits), fixed_point<destT, destI, destF, destW>>::type
convert_to (void) const noexcept
{
return fixed_point<destT, destI, destF, destW> (static_cast<destT> (value >> (fractional_bits - destF)), FIXED_POINT_RAW);
}
};
__FIXED_POINT_END_NAMESPACE__
#ifdef __FIXED_POINT_BEGIN_NAMESPACE_DO_UNDEF_AFTER__
#undef __FIXED_POINT_BEGIN_NAMESPACE__
#undef __FIXED_POINT_BEGIN_NAMESPACE_DO_UNDEF_AFTER__
#endif
#ifdef __FIXED_POINT_END_NAMESPACE_DO_UNDEF_AFTER__
#undef __FIXED_POINT_END_NAMESPACE__
#undef __FIXED_POINT_END_NAMESPACE_DO_UNDEF_AFTER__
#endif
// =============================================================================
#ifndef __FIXED_POINT_USE_NAMESPACE__
#define __FIXED_POINT_USE_NAMESPACE__
#define __FIXED_POINT_USE_NAMESPACE_DO_UNDEF_AFTER__
#endif
namespace std
{
template <typename T, unsigned I, unsigned F, bool W>
class numeric_limits<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
{
typedef __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> fixed_type;
public:
static constexpr float_denorm_style has_denorm = denorm_absent;
static constexpr bool has_denorm_loss = false;
static constexpr bool has_infinity = false;
static constexpr bool has_quiet_NaN = false;
static constexpr bool has_signaling_NaN = false;
static constexpr bool is_bounded = true;
static constexpr bool is_exact = true;
static constexpr bool is_iec559 = false;
static constexpr bool is_integer = false;
static constexpr bool is_modulo = false;
static constexpr bool is_signed = std::numeric_limits<typename fixed_type::raw_type>::is_signed;
static constexpr bool is_specialized = true;
static constexpr bool tinyness_before = false;
static constexpr bool traps = false;
static constexpr float_round_style round_style = round_toward_zero;
static constexpr int digits = fixed_type::integral_bits;
static constexpr int digits10 = digits * 301. / 1000. + .5;
static constexpr int max_exponent = 0;
static constexpr int max_exponent10 = 0;
static constexpr int min_exponent = 0;
static constexpr int min_exponent10 = 0;
static constexpr int radix = std::numeric_limits<typename fixed_type::raw_type>::radix;
static constexpr fixed_type min (void) noexcept
{
return fixed_type (std::numeric_limits<typename fixed_type::raw_type>::min (),
__FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type lowest (void) noexcept
{
return min ();
}
static constexpr fixed_type max (void) noexcept
{
return fixed_type (std::numeric_limits<typename fixed_type::raw_type>::max (),
__FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type epsilon (void) noexcept
{
return fixed_type (1, __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type round_error (void) noexcept
{
return fixed_type (fixed_type::fractional_mask,
__FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type denorm_min (void) noexcept
{
return fixed_type (0, __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type infinity (void) noexcept
{
return fixed_type (0, __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type quiet_NaN (void) noexcept
{
return fixed_type (0, __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
static constexpr fixed_type signaling_NaN (void) noexcept
{
return fixed_type (0, __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
};
template <typename T, unsigned I, unsigned F, bool W>
struct is_arithmetic<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public true_type
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_signed<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_signed<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_unsigned<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_unsigned<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_pod <__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_pod<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_integral<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public false_type
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_floating_point<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public false_type
{};
/*
template <typename T, unsigned I, unsigned F, bool W>
struct is_trivially_copyable<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_trivially_copyable<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
*/
template <typename T, unsigned I, unsigned F, bool W>
struct is_standard_layout<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_standard_layout<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
template <typename T, unsigned I, unsigned F, bool W>
struct is_literal_type<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
: public is_literal_type<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>
{};
template <typename T, unsigned I, unsigned F, bool W>
struct make_signed<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
{
typedef __FIXED_POINT_USE_NAMESPACE__ fixed_point<typename make_signed<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type>::type, I, F, W> type;
};
template <typename T, unsigned I, unsigned F, bool W>
struct make_unsigned<__FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>>
{
typedef __FIXED_POINT_USE_NAMESPACE__ fixed_point<typename make_unsigned<typename __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>::raw_type >::type, I, F, W> type;
};
// floating point code might also be using std::abs instead of std::fabs
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> abs (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& val) noexcept
{
return val < 0 ? -val : val;
}
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> fabs (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& val) noexcept
{
return val < 0 ? -val : val;
}
// this one is weird. while std::abs just works fine with the templated type,
// std::min and std::max will force the int variable onto the stack. working with the raw_value
// eliminates this problem. probably should do the same in std::abs, just to be on the safe side.
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
min (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a, const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
return __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> (a.raw () < b.raw () ? a.raw () : b.raw (), __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
fmin (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a, const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
return __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> (a.raw () < b.raw () ? a.raw () : b.raw (), __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
max (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a, const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
return __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> (a.raw () > b.raw () ? a.raw () : b.raw (), __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
fmax (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a, const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
return __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W> (a.raw () > b.raw () ? a.raw () : b.raw (), __FIXED_POINT_USE_NAMESPACE__ FIXED_POINT_RAW);
}
/*
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
remainder (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a,
const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
}
*/
/*
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
remquo (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& a,
const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& b) noexcept
{
}
*/
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>
fma (const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& x,
const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& y,
const __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>& z) noexcept
{
return x * y + z;
}
template <typename T, unsigned I, unsigned F, bool W>
inline constexpr __FIXED_POINT_USE_NAMESPACE__ fixed_point<T, I, F, W>