forked from balfieri/cordic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfreal.h
executable file
·1297 lines (1169 loc) · 60.1 KB
/
freal.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
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
// Copyright (c) 2014-2019 Robert A. Alfieri
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// freal.h - flexible real number
//
// This class supports typical C++ floating-point semantics found with float and double.
// It implements freals as a user-specified fixed-point.
// All functions are implemented using Cordic.h.
//
// In the near future, it will allow the internal format to change
// dynamically based on input and output ranges, and the
// format could be fixed-point or floating-point.
//
// Typical usage:
//
// #include freal.h
// freal::implicit_to_set( 8, 21, true ); // default is to convert FLT values to floating-point 1.8.21
// ...
// freal g; // g is undefined, no type
// freal f = 25.473822; // implicitly convert to fixed-point 1.8.21
// g = f; // g copies type and value of f
//
#ifndef _freal_h
#define _freal_h
#include "Cordic.h"
// #defines:
//
// FREAL_T = some signed integer type that can hold fixed-point values (default is int64_t)
// FREAL_FLT = some floating-point type that can hold constants of the desired precision (default is double)
//
// For the most generality, use -DFREAL_T=mpint -DFREAL_T=mpreal, but these types have not been tested yet.
// mpint is my own minimalistic "big integer" class.
// mpreal is implemented on top of libmpfr which must be installed on your system. Add -lmpfr to your link link.
//
#ifdef FREAL_T
using T = FREAL_T;
#else
using T = int64_t;
#endif
#ifdef FREAL_FLT
using FLT = FREAL_FLT;
#else
using FLT = double;
#endif
class freal
{
public:
//-----------------------------------------------------
// Constructors
//-----------------------------------------------------
freal( void ); // initializes value to undefined
freal( const freal& other ); // copy type and value of other
freal( const freal& other, FLT f ); // copy type of other, but value of f
freal( Cordic<T,FLT> * cordic, FLT f ); // use type from cordic, but value of f; fractional lsb is never rounded
static void logger_set( Logger<T,FLT> * logger ); // record logger for any implicit cordic
static freal make_fixed( uint32_t int_w, uint32_t frac_w, FLT init_f=FLT(0) ); // make a signed fixed-point number
static freal make_float( uint32_t exp_w, uint32_t frac_w, FLT init_f=FLT(0) ); // make a signed floating-point number
~freal();
//-----------------------------------------------------
// Explicit Conversions
//-----------------------------------------------------
FLT to_flt( void ) const; // freal to FLT
std::string to_string( void ) const; // freal to std::string
std::string to_bstring( void ) const; // freal binary to std::string
//-----------------------------------------------------
// Implicit Conversions
//
// IMPORTANT: implicit_to_set() must be called before
// using any implicit conversions TO freal.
//
// IMPORTANT: implicit_from_set() must be called before
// using any implicit conversions FROM freal.
//-----------------------------------------------------
static void implicit_to_set( uint32_t int_w, uint32_t frac_w, bool is_float=true );
static void implicit_to_set( Cordic<T,FLT> * cordic );
static Cordic<T,FLT> * implicit_to_get( void );
static void implicit_from_set( bool allow );
freal( FLT f );
freal( uint64_t i );
freal( int64_t i );
freal( uint32_t i );
freal( int32_t i );
operator FLT( void ) const;
operator float( void ) const;
operator uint64_t( void ) const;
operator int64_t( void ) const;
operator uint32_t( void ) const;
operator int32_t( void ) const;
//-----------------------------------------------------
// Constants (freal ones are never rounded, so call rfrac() if you want them rounded)
//-----------------------------------------------------
bool is_float( void ) const; // is_float from above
uint32_t int_w( void ) const; // int_w from above (fixed-point only)
uint32_t exp_w( void ) const; // exp_w from above (floating-point only)
uint32_t frac_w( void ) const; // frac_w from above
uint32_t guard_w( void ) const; // guard_w from above
uint32_t w( void ) const; // 1 + int_w + frac_w + guard_w (i.e., overall width)
uint32_t n( void ) const; // number of cordic iterations (default is frac_w)
T maxint( void ); // largest positive integer (just integer part, does not include fraction)
freal max( void ); // maximum positive value
freal min( void ); // minimum positive value
freal denorm_min( void ); // minimum positive denorm value
freal lowest( void ); // most negative value
freal epsilon( void ); // difference between 1 and first number above 1
freal round_error( void ); // maximum rounding error
freal zero( void ); // 0.0
freal one( void ); // 1.0
freal two( void ); // 2.0
freal half( void ); // 0.5
freal quarter( void ); // 0.25
freal sqrt2( void ); // sqrt(2)
freal sqrt2_div_2( void ); // sqrt(2)/2
freal pi( void ); // PI
freal tau( void ); // 2*PI
freal pi_div_2( void ); // PI/2
freal pi_div_4( void ); // PI/4
freal one_div_pi( void ); // 1/PI
freal two_div_pi( void ); // 2/PI
freal four_div_pi( void ); // 4/PI
freal e( void ); // natural exponent
freal nan( const char * arg ); // not-a-number (NaN)
freal quiet_NaN( void ); // quiet not-a-number (NaN)
freal signaling_NaN( void ); // signaling not-a-number (NaN)
freal infinity( void ); // +infinity
freal ninfinity( void ); // -infinity
//-----------------------------------------------------
// Standard Operators
//-----------------------------------------------------
freal operator - () const; // -x
freal operator + ( const freal& b ) const;
freal operator + ( const FLT& b ) const;
freal operator - ( const freal& b ) const;
freal operator - ( const FLT& b ) const;
freal operator * ( const freal& b ) const;
freal operator * ( const FLT& b ) const;
freal operator / ( const freal& b ) const;
freal operator / ( const FLT& b ) const;
freal operator << ( int b ) const;
freal operator >> ( int b ) const;
freal& operator = ( const freal& b );
freal& operator = ( const FLT& b );
freal& operator += ( const freal& b );
freal& operator += ( const FLT& b );
freal& operator -= ( const freal& b );
freal& operator -= ( const FLT& b );
freal& operator *= ( const freal& b );
freal& operator *= ( const FLT& b );
freal& operator /= ( const freal& b );
freal& operator /= ( const FLT& b );
freal& operator <<=( int b );
freal& operator >>=( int b );
bool operator > ( const freal& b ) const;
bool operator > ( const FLT& b ) const;
bool operator >= ( const freal& b ) const;
bool operator >= ( const FLT& b ) const;
bool operator < ( const freal& b ) const;
bool operator < ( const FLT& b ) const;
bool operator <= ( const freal& b ) const;
bool operator <= ( const FLT& b ) const;
bool operator != ( const freal& b ) const;
bool operator != ( const FLT& b ) const;
bool operator == ( const freal& b ) const;
bool operator == ( const FLT& b ) const;
//-----------------------------------------------------
// Well-Known Math Operators and Functions
//
// See Cordic.h for functionality of each of these.
//
// a = *this
//-----------------------------------------------------
freal& assign( const freal& b );
bool signbit( void ) const;
freal frexp( int * e ) const;
freal modf( freal * i ) const;
int ilogb( void ) const;
freal logb( void ) const;
int fpclassify( void ) const;
bool iszero( void ) const;
bool isfinite( void ) const;
bool isinf( void ) const;
bool isnan( void ) const;
bool isnormal( void ) const;
bool issubnormal( void ) const;
int fesetround( int round ) const;
int fegetround( void ) const;
freal nextafter( const freal& to ) const;
freal nextafter( const FLT& to ) const;
freal nexttoward( long double to ) const;
freal floor( void ) const;
freal ceil( void ) const;
freal trunc( void ) const;
freal extend( void ) const; // away from zero
freal round( void ) const;
long lround( void ) const;
long long llround( void ) const;
T iround( void ) const;
freal rint( void ) const;
long lrint( void ) const;
long long llrint( void ) const;
T irint( void ) const;
freal nearbyint( void ) const;
freal floorfrac( void ) const; // these round fractional lsb and clear guard bits
freal ceilfrac( void ) const;
freal truncfrac( void ) const;
freal extendfrac( void ) const; // away from zero
freal roundfrac( void ) const;
freal rfrac( void ) const;
freal abs( void ) const;
freal neg( void ) const;
freal copysign( const freal& b ) const;
freal copysign( const FLT& b ) const;
freal add( const freal& b ) const;
freal add( const FLT& b ) const;
freal sub( const freal& b ) const;
freal sub( const FLT& b ) const;
freal scalbn( int b ) const;
freal scalbnn( int b ) const; // scalbn( -b )
freal ldexp( int b ) const;
freal fma( const freal& b, const freal& c ) const;
freal fma( const freal& b, const FLT& c ) const;
freal fma( const FLT& b, const freal& c ) const;
freal fma( const FLT& b, const FLT& c ) const;
freal mul( const freal& b ) const;
freal mul( const FLT& b ) const;
freal sqr( void ) const;
freal fda( const freal& b, const freal& c ) const;
freal fda( const freal& b, const FLT& c ) const;
freal fda( const FLT& b, const freal& c ) const;
freal fda( const FLT& b, const FLT& c ) const;
freal div( const freal& b ) const;
freal div( const FLT& b ) const;
freal remainder( const freal& b ) const;
freal remainder( const FLT& b ) const;
freal fmod( const freal& b ) const;
freal fmod( const FLT& b ) const;
freal remquo( const freal& x, int * quo ) const;
freal remquo( const FLT& x, int * quo ) const;
freal rcp( void ) const;
int compare( const freal& b ) const;
int compare( const FLT& b ) const;
bool isgreater( const freal& b ) const;
bool isgreater( const FLT& b ) const;
bool isgreaterequal( const freal& b ) const;
bool isgreaterequal( const FLT& b ) const;
bool isless( const freal& b ) const;
bool isless( const FLT& b ) const;
bool islessequal( const freal& b ) const;
bool islessequal( const FLT& b ) const;
bool islessgreater( const freal& b ) const;
bool islessgreater( const FLT& b ) const;
bool isunordered( const freal& b ) const;
bool isunordered( const FLT& b ) const;
bool isunequal( const freal& b ) const;
bool isunequal( const FLT& b ) const;
bool isequal( const freal& b ) const;
bool isequal( const FLT& b ) const;
freal fdim( const freal& b ) const;
freal fdim( const FLT& b ) const;
freal fmax( const freal& b ) const;
freal fmax( const FLT& b ) const;
freal fmin( const freal& b ) const;
freal fmin( const FLT& b ) const;
freal sqrt( void ) const;
freal rsqrt( void ) const;
freal cbrt( void ) const;
freal rcbrt( void ) const;
freal exp( void ) const;
freal expm1( void ) const; // exp(x) - 1 (accurately)
freal expc( const FLT c ) const; // c^a
freal exp2( void ) const; // 2^x
freal exp10( void ) const; // 10^x
freal pow( const freal& e ) const; // a^e
freal pow( const FLT& e ) const; // a^e
freal log( void ) const; // log base-e
freal log( const freal& b ) const; // log base-b
freal log( const FLT& b ) const; // log base-b
freal log1p( void ) const; // log base-e (a+1)
freal logc( const FLT c ) const; // log base-c (c is a constant)
freal log2( void ) const; // log base-2
freal log10( void ) const; // log base-10
freal deg2rad( void ) const;
freal rad2deg( void ) const;
freal sin( void ) const;
freal sinpi( void ) const;
freal sin( const freal& r ) const; // multiply sin by r
freal sin( const FLT& r ) const; // multiply sin by r
freal sinpi( const freal& r ) const; // multiply sin by r
freal sinpi( const FLT& r ) const; // multiply sin by r
freal cos( void ) const;
freal cospi( void ) const;
freal cos( const freal& r ) const; // multiply cos by r
freal cos( const FLT& r ) const; // multiply cos by r
freal cospi( const freal& r ) const; // multiply cos by r
freal cospi( const FLT& r ) const; // multiply cos by r
void sincos( freal& si, freal& co ) const;
void sinpicospi( freal& si, freal& co ) const;
void sincos( freal& si, freal& co, const freal& r ) const; // multiply sin and cos by r
void sinpicospi( freal& si, freal& co, const freal& r ) const; // multiply sin and cos by r
freal tan( void ) const;
freal tanpi( void ) const;
freal asin( void ) const;
freal acos( void ) const;
freal atan( void ) const;
freal atan2( const freal& b ) const; // y=a, x=b
freal atan2( const FLT& b ) const; // y=a, x=b
void polar_to_rect( const freal& angle, freal& x, freal& y ) const; // a=radius
void polar_to_rect( const FLT& angle, freal& x, freal& y ) const; // a=radius
void rect_to_polar( const freal& b, freal& r, freal& angle ) const; // x=a, y=b
void rect_to_polar( const FLT& b, freal& r, freal& angle ) const; // x=a, y=b
freal hypot( const freal& b ) const;
freal hypot( const FLT& b ) const;
freal hypoth( const freal& b ) const;
freal hypoth( const FLT& b ) const;
freal sinh( void ) const;
freal sinh( const freal& r ) const; // multiply sinh by r
freal sinh( const FLT& r ) const; // multiply sinh by r
freal cosh( void ) const;
freal cosh( const freal& r ) const; // multiply cosh by r
freal cosh( const FLT& r ) const; // multiply cosh by r
void sinhcosh( freal& sih, freal& coh ) const;
void sinhcosh( freal& sih, freal& coh, const freal& r ) const; // multiply sinh and cosh by r
freal tanh( void ) const;
freal asinh( void ) const;
freal acosh( void ) const;
freal atanh( void ) const;
freal atanh2( const freal& b ) const; // atanh2( a, b )
freal atanh2( const FLT& b ) const; // atanh2( a, b )
//-----------------------------------------------------
// Introspection
//-----------------------------------------------------
const Cordic<T,FLT> * c( void ) const; // validates current cordic and returns it
Cordic<T,FLT> * cw( void ) const; // validates current cordic and returns it
const Cordic<T,FLT> * c( const freal& b ) const; // validates two cordics and returns one to use for operation
Cordic<T,FLT> * cw( const freal& b ) const; // validates two cordics and returns one to use for operation
const Cordic<T,FLT> * c( const freal& b, const freal& _c ) const; // validates three cordics and returns one to use for operation
Cordic<T,FLT> * cw( const freal& b, const freal& _c ) const; // validates three cordics and returns one to use for operation
const T * raw_ptr( void ) const; // useful for some gross things like manual logging by callers
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//
//
// ___ _ _ _ _
// |_ _|_ __ ___ _ __ | | ___ _ __ ___ ___ _ __ | |_ __ _| |_(_) ___ _ __
// | || '_ ` _ \| '_ \| |/ _ \ '_ ` _ \ / _ \ '_ \| __/ _` | __| |/ _ \| '_ \
// | || | | | | | |_) | | __/ | | | | | __/ | | | || (_| | |_| | (_) | | | |
// |___|_| |_| |_| .__/|_|\___|_| |_| |_|\___|_| |_|\__\__,_|\__|_|\___/|_| |_|
// |_|
//
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
private:
static Cordic<T,FLT> * implicit_to;
static bool implicit_from;
Cordic<T,FLT> * cordic; // defines the type and most operations
T v; // this value encoded in type T
static freal pop_value( Cordic<T,FLT> * cordic, const T& encoded ); // pop value associated with last operation
static bool pop_bool( Cordic<T,FLT> * cordic, bool ); // pop bool associated with last operation
};
// Well-Known std:xxx() Functions
//
namespace std
{
static inline std::istream& operator >> ( std::istream &in, freal& a )
{
FLT a_f;
in >> a_f;
Cordic<T,FLT> * cordic = a.cw();
if ( cordic != nullptr ) {
a = freal( cordic, a_f ); // use a's current format
} else {
a = a_f; // rely on implicit conversion, if it's currently allowed
}
return in;
}
static inline std::ostream& operator << ( std::ostream &out, const freal& a )
{
out << a.to_string();
return out;
}
// use macros to avoid redundancy
//
#define _freal freal
#define _FLT FLT
#define decl_std1( name ) \
static inline _freal name( const _freal& a ) \
{ return a.name(); } \
#define decl_std1_ret( name, ret_type ) \
static inline ret_type name( const _freal& a ) \
{ return a.name(); } \
#define decl_std1_ret2( name ) \
static inline void name( const _freal& a, _freal& r1, _freal& r2 ) \
{ a.name( r1, r2 ); } \
static inline void name( const _FLT& a, _freal& r1, _freal& r2 ) \
{ _freal(a).name( r1, r2 ); } \
#define decl_std2( name ) \
static inline _freal name( const _freal& a, const _freal& b ) \
{ return a.name( b ); } \
static inline _freal name( const _freal& a, const _FLT& b ) \
{ return a.name( b ); } \
static inline _freal name( const _FLT& a, const _freal& b ) \
{ return _freal(a).name( b ); } \
#define decl_std2_ret( name, ret_type ) \
static inline ret_type name( const _freal& a, const _freal& b ) \
{ return a.name( b ); } \
static inline ret_type name( const _freal& a, const _FLT& b ) \
{ return a.name( b ); } \
static inline ret_type name( const _FLT& a, const _freal& b ) \
{ return _freal(a).name( b ); } \
#define decl_std2_ret2( name ) \
static inline void name( const _freal& a, const _freal& b, _freal& r1, _freal& r2 ) \
{ a.name( b, r1, r2 ); } \
static inline void name( const _freal& a, const _FLT& b, _freal& r1, _freal& r2 ) \
{ a.name( b, r1, r2 ); } \
static inline void name( const _FLT& a, const _freal& b, _freal& r1, _freal& r2 ) \
{ _freal(a).name( b, r1, r2 ); } \
static inline void name( const _FLT& a, const _FLT& b, _freal& r1, _freal& r2 ) \
{ _freal(a).name( b, r1, r2 ); } \
#define decl_std2x( name, b_type ) \
static inline _freal name( const _freal& a, b_type b ) \
{ return a.name( b ); } \
#define decl_std2x_ret2( name, b_type ) \
static inline void name( const _freal& a, _freal& r1, _freal& r2, b_type b ) \
{ a.name( r1, r2, b ); } \
static inline void name( const _FLT& a, _freal& r1, _freal& r2, b_type b ) \
{ _freal(a).name( r1, r2, b ); } \
#define decl_std3( name ) \
static inline _freal name( const _freal& a, const _freal& b, const _freal& c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _freal& a, const _FLT& b, const _freal& c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _freal& a, const _freal& b, const _FLT& c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _freal& a, const _FLT& b, const _FLT& c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _FLT& a, const _freal& b, const _freal& c ) \
{ return _freal(a).name( b, c ); } \
static inline _freal name( const _FLT& a, const _FLT& b, const _freal& c ) \
{ return _freal(a).name( b, c ); } \
static inline _freal name( const _FLT& a, const _freal& b, const _FLT& c ) \
{ return _freal(a).name( b, c ); } \
#define decl_std3x( name, c_type ) \
static inline _freal name( const _freal& a, const _freal& b, c_type c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _freal& a, const _FLT& b, c_type c ) \
{ return a.name( b, c ); } \
static inline _freal name( const _FLT& a, const _freal& b, c_type c ) \
{ return _freal(a).name( b, c ); } \
static inline _freal name( const _FLT& a, const _FLT& b, c_type c ) \
{ return _freal(a).name( b, c ); } \
decl_std1_ret( signbit, bool )
decl_std2x( frexp, int * )
decl_std2x( modf, _freal * )
decl_std1_ret( ilogb, int )
decl_std1( logb )
decl_std1_ret( fpclassify, int )
decl_std1_ret( iszero, bool )
decl_std1_ret( isfinite, bool )
decl_std1_ret( isinf, bool )
decl_std1_ret( isnan, bool )
decl_std1_ret( isnormal, bool )
decl_std1_ret( issubnormal, bool )
decl_std1_ret( to_string, std::string )
decl_std1_ret( to_bstring, std::string )
decl_std2( nextafter )
decl_std2x( nexttoward, long double )
decl_std1( floor )
decl_std1( ceil )
decl_std1( trunc )
decl_std1( extend )
decl_std1( round )
decl_std1_ret( lround, long )
decl_std1_ret( llround, long long )
decl_std1_ret( iround, T )
decl_std1( rint )
decl_std1_ret( lrint, long )
decl_std1_ret( llrint, long long )
decl_std1_ret( irint, T )
decl_std1( nearbyint )
decl_std1( floorfrac )
decl_std1( ceilfrac )
decl_std1( truncfrac )
decl_std1( extendfrac )
decl_std1( roundfrac )
decl_std1( rfrac )
decl_std1( abs )
decl_std1( neg )
decl_std2( copysign )
decl_std2( add )
decl_std2( sub )
decl_std2x( scalbn, int )
decl_std2x( scalbnn, int )
decl_std2x( ldexp, int )
decl_std3( fma )
decl_std2( mul )
decl_std1( sqr )
decl_std3( fda )
decl_std2( div )
decl_std2( remainder )
decl_std2( fmod )
decl_std3x( remquo, int * )
decl_std1( rcp )
decl_std2_ret( compare, int )
decl_std2_ret( isgreater, bool )
decl_std2_ret( isgreaterequal, bool )
decl_std2_ret( isless, bool )
decl_std2_ret( islessequal, bool )
decl_std2_ret( islessgreater, bool )
decl_std2_ret( isunordered, bool )
decl_std2_ret( isunequal, bool )
decl_std2_ret( isequal, bool )
decl_std2( fdim )
decl_std2( fmin )
decl_std2( fmax )
decl_std1( sqrt )
decl_std1( rsqrt )
decl_std1( cbrt )
decl_std1( rcbrt )
decl_std1( exp )
decl_std1( expm1 )
decl_std2x( expc, FLT )
decl_std1( exp2 )
decl_std1( exp10 )
decl_std2( pow )
decl_std1( log )
decl_std2( log )
decl_std1( log1p )
decl_std2x( logc, FLT )
decl_std1( log2 )
decl_std1( log10 )
decl_std1( deg2rad )
decl_std1( rad2deg )
decl_std1( sin )
decl_std1( sinpi )
decl_std2( sin )
decl_std2( sinpi )
decl_std1( cos )
decl_std1( cospi )
decl_std2( cos )
decl_std2( cospi )
decl_std1_ret2(sincos )
decl_std1_ret2(sinpicospi )
decl_std2x_ret2(sincos, _freal )
decl_std2x_ret2(sinpicospi, _freal )
decl_std1( tan )
decl_std1( tanpi )
decl_std1( asin )
decl_std1( acos )
decl_std1( atan )
decl_std2( atan2 )
decl_std2_ret2(polar_to_rect )
decl_std2_ret2(rect_to_polar )
decl_std2( hypot )
decl_std2( hypoth )
decl_std1( sinh )
decl_std2( sinh )
decl_std1( cosh )
decl_std2( cosh )
decl_std1_ret2(sinhcosh )
decl_std2x_ret2(sinhcosh, _freal )
decl_std1( tanh )
decl_std1( asinh )
decl_std1( acosh )
decl_std1( atanh )
decl_std2( atanh2 )
}
// specialize for freal
//
template<> class std::numeric_limits<freal>
{
public:
// any static field that depends on the current implicit_to is (re)initialized
// when implicit_to_set() is called to change the implicit Cordic, therefore
// these static fields are not marked const and will change from these
// default values in the typical usage scenario
static bool is_specialized;
static freal min() throw() { return freal::implicit_to_get()->min(); }
static freal max() throw() { return freal::implicit_to_get()->max(); }
static int digits;
static int digits10;
static const bool is_signed = true;
static const bool is_integer = false;
static const bool is_exact = false;
static const int radix = 2;
static freal epsilon() throw() { return freal::implicit_to_get()->epsilon(); }
static freal round_error() throw() { return freal::implicit_to_get()->round_error(); }
static int min_exponent;
static int min_exponent10;
static int max_exponent;
static int max_exponent10;
static bool has_infinity;
static bool has_quiet_NaN;
static bool has_signaling_NaN;
static const float_denorm_style has_denorm = denorm_present;
static const bool has_denorm_loss = false;
static freal infinity() throw() { return freal::implicit_to_get()->infinity(); }
static freal quiet_NaN() throw() { return freal::implicit_to_get()->quiet_NaN(); }
static freal signaling_NaN() throw() { return freal::implicit_to_get()->signaling_NaN(); }
static freal denorm_min() throw() { return freal::implicit_to_get()->denorm_min(); }
static bool is_iec559;
static bool is_bounded;
static bool is_modulo;
static bool traps;
static bool tinyness_before;
static float_round_style round_style;
};
bool std::numeric_limits<freal>::is_specialized = false;
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//
// IMPLEMENTATION
//
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
// Static Globals
//-----------------------------------------------------
Cordic<T,FLT> * freal::implicit_to = nullptr; // disallow
bool freal::implicit_from = false; // disallow
void freal::logger_set( Logger<T,FLT> * logger )
{
Cordic<T,FLT>::logger_set( logger );
if ( implicit_to != nullptr ) implicit_to->log_constructed();
}
//-----------------------------------------------------
// Constructors
//-----------------------------------------------------
inline freal::freal( void )
{
cordic = nullptr;
v = T(666);
}
inline freal::~freal()
{
if ( cordic != nullptr ) {
cordic->destructed( v );
cordic = nullptr;
}
v = T(668);
}
inline freal::freal( Cordic<T,FLT> * _cordic, FLT f )
{
cassert( _cordic != nullptr, "freal(cordic, f) cordic argument must be non-null" );
cordic = _cordic;
cordic->constructed( v );
cordic->pop_value( v, cordic->to_t( f, true ) );
}
inline freal::freal( const freal& other )
{
cordic = other.cw();
v = other.v;
cordic->constructed( v );
cordic->assign( v, other.v );
}
inline freal::freal( const freal& other, FLT f )
{
freal( other.cordic, f );
}
inline freal freal::make_fixed( uint32_t int_w, uint32_t frac_w, FLT init_f )
{
return freal( new Cordic<T,FLT>( int_w, frac_w, false ), init_f );
}
inline freal freal::make_float( uint32_t exp_w, uint32_t frac_w, FLT init_f )
{
return freal( new Cordic<T,FLT>( exp_w, frac_w, true ), init_f );
}
inline freal freal::pop_value( Cordic<T,FLT> * cordic, const T& encoded )
{
cassert( cordic != nullptr, "pop_value(cordic, encoded) called with null cordic" );
freal r;
r.cordic = cordic;
cordic->constructed( r.v );
cordic->pop_value( r.v, encoded );
return r;
}
inline bool freal::pop_bool( Cordic<T,FLT> * cordic, bool b )
{
cassert( cordic != nullptr, "pop_bool(cordic, b) called with null cordic" );
cordic->pop_bool( b );
return b;
}
//-----------------------------------------------------
// Explicit Conversions
//-----------------------------------------------------
FLT freal::to_flt( void ) const
{ return c()->to_flt( v ); }
std::string freal::to_string( void ) const
{ return c()->to_string( v ); }
std::string freal::to_bstring( void ) const
{ return c()->to_bstring( v ); }
//-----------------------------------------------------
// Implicit Conversions
//-----------------------------------------------------
inline void freal::implicit_to_set( Cordic<T,FLT> * cordic )
{
implicit_to = cordic;
if ( cordic != nullptr ) {
std::numeric_limits<freal>::is_specialized = true;
std::numeric_limits<freal>::digits = cordic->int_w() + cordic->frac_w();
std::numeric_limits<freal>::digits10 = std::numeric_limits<freal>::digits / std::log2( 10 );
std::numeric_limits<freal>::min_exponent = 0;
std::numeric_limits<freal>::min_exponent10 = 0;
std::numeric_limits<freal>::max_exponent = 0;
std::numeric_limits<freal>::max_exponent10 = 0;
std::numeric_limits<freal>::has_infinity = false;
std::numeric_limits<freal>::has_quiet_NaN = false;
std::numeric_limits<freal>::has_signaling_NaN = false;
std::numeric_limits<freal>::is_iec559 = false;
std::numeric_limits<freal>::is_bounded = true;
std::numeric_limits<freal>::is_modulo = true;
std::numeric_limits<freal>::traps = false;
std::numeric_limits<freal>::tinyness_before = true;
std::numeric_limits<freal>::round_style = std::round_to_nearest;
} else {
std::numeric_limits<freal>::is_specialized = false;
}
}
inline Cordic<T,FLT> * freal::implicit_to_get( void )
{
return implicit_to;
}
inline void freal::implicit_to_set( uint32_t int_exp_w, uint32_t frac_w, bool is_float )
{
implicit_to = new Cordic<T,FLT>( int_exp_w, frac_w, is_float );
}
inline void freal::implicit_from_set( bool allow )
{
implicit_from = allow;
}
inline freal::freal( FLT f )
{
cassert( implicit_to != nullptr, "implicit_to_set() must be called before relying on any implicit from FLT to freal<>" );
*this = freal( implicit_to, f );
}
inline freal::freal( uint64_t i )
{
cassert( implicit_to != nullptr, "implicit_to_set() must be called before relying on any implicit from uint64_t to freal<>" );
*this = freal( implicit_to, FLT(i) );
}
inline freal::freal( int64_t i )
{
cassert( implicit_to != nullptr, "implicit_to_set() must be called before relying on any implicit from int64_t to freal<>" );
*this = freal( implicit_to, FLT(i) );
}
inline freal::freal( uint32_t i )
{
cassert( implicit_to != nullptr, "implicit_to_set() must be called before relying on any implicit from uint32_t to freal<>" );
*this = freal( implicit_to, FLT(i) );
}
inline freal::freal( int32_t i )
{
cassert( implicit_to != nullptr, "implicit_to_set() must be called before relying on any implicit from int32_t to freal<>" );
*this = freal( implicit_to, FLT(i) );
}
inline freal::operator FLT( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to FLT" );
return to_flt();
}
inline freal::operator float( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to FLT" );
return to_flt();
}
inline freal::operator uint64_t( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to uint64_t" );
return uint64_t( to_flt() );
}
inline freal::operator int64_t( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to int64_t" );
return int64_t( to_flt() );
}
inline freal::operator uint32_t( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to uint32_t" );
return uint32_t( to_flt() );
}
inline freal::operator int32_t( void ) const
{
cassert( implicit_from, "implicit_from_set( true ) must be called before relying on any implicit from freal<> to int32_t" );
return int32_t( to_flt() );
}
//-----------------------------------------------------
// Check that cordic(s) defined and return it.
//-----------------------------------------------------
inline const Cordic<T,FLT> * freal::c( void ) const
{
cassert( cordic != nullptr, "undefined type" );
return cordic;
}
inline Cordic<T,FLT> * freal::cw( void ) const
{
cassert( cordic != nullptr, "undefined type" );
return cordic;
}
inline const Cordic<T,FLT> * freal::c( const freal& b ) const
{
cassert( cordic != nullptr, "a has undefined type" );
cassert( b.cordic != nullptr, "b has undefined type" );
cassert( cordic == b.cordic, "a and b must have same type currently" );
return cordic;
}
inline Cordic<T,FLT> * freal::cw( const freal& b ) const
{
cassert( cordic != nullptr, "a has undefined type" );
cassert( b.cordic != nullptr, "b has undefined type" );
cassert( cordic == b.cordic, "a and b must have same type currently" );
return cordic;
}
inline const Cordic<T,FLT> * freal::c( const freal& b, const freal& _c ) const
{
cassert( cordic != nullptr, "a has undefined type" );
cassert( b.cordic != nullptr, "b has undefined type" );
cassert( _c.cordic != nullptr, "c has undefined type" );
cassert( cordic == b.cordic && cordic == _c.cordic, "a and b and c must have same type currently" );
return cordic;
}
inline Cordic<T,FLT> * freal::cw( const freal& b, const freal& _c ) const
{
cassert( cordic != nullptr, "a has undefined type" );
cassert( b.cordic != nullptr, "b has undefined type" );
cassert( _c.cordic != nullptr, "c has undefined type" );
cassert( cordic == b.cordic && cordic == _c.cordic, "a and b and c must have same type currently" );
return cordic;
}
inline const T * freal::raw_ptr( void ) const
{
return &v;
}
//-----------------------------------------------------
// Constants
//-----------------------------------------------------
#define decl_const( name ) \
inline _freal freal::name( void ) \
{ return( c(), pop_value( cordic, cordic->name() ) ); } \
#define decl_const1x( name, a_type ) \
inline _freal freal::name( a_type a ) \
{ return( c(), pop_value( cordic, cordic->name( a ) ) ); } \
decl_const( max )
decl_const( min )
decl_const( denorm_min )
decl_const( lowest )
decl_const( epsilon )
decl_const( round_error )
decl_const( zero )
decl_const( one )
decl_const( two )
decl_const( half )
decl_const( quarter )
decl_const( sqrt2 )
decl_const( sqrt2_div_2 )
decl_const( pi )
decl_const( tau )
decl_const( pi_div_2 )
decl_const( pi_div_4 )
decl_const( one_div_pi )
decl_const( two_div_pi )
decl_const( four_div_pi )
decl_const( e )
decl_const1x( nan, const char * )
decl_const( quiet_NaN )
decl_const( signaling_NaN )
decl_const( infinity )