-
Notifications
You must be signed in to change notification settings - Fork 5
/
Cordic.h
executable file
·4569 lines (4124 loc) · 173 KB
/
Cordic.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.
//
#ifndef _Cordic_h
#define _Cordic_h
#include <cmath>
#include <cfenv>
#include <iostream>
#include <iomanip>
#include <cstring>
#include "Logger.h"
#ifdef DEBUG_LEVEL
static constexpr uint32_t debug = DEBUG_LEVEL;
#else
static constexpr uint32_t debug = 0;
#endif
#ifdef NO_ASSERT
static constexpr bool do_asserts = false;
#else
static constexpr bool do_asserts = true;
#endif
#define cassert(expr, msg) if ( do_asserts && !(expr) ) \
{ std::cout << "ERROR: assertion failure: " << (msg) << " at " << __FILE__ << ":" << __LINE__ << "\n"; exit( 1 ); }
// extended rounding modes beyond FE_TONEAREST etc.
//
static constexpr int FE_NOROUND = 0x1000; // perform no rounding at all; leave guard bits alone
static constexpr int FE_AWAYFROMZERO = 0x2000; // signbit(x) ? trunc(x) : ceil(x); and clear guard bits
// T = some signed integer type that can hold fixed-point values (default is int64_t)
// FLT = some floating-point type that can hold constants of the desired precision (default is double)
//
template< typename T=int64_t, typename FLT=double >
class Cordic
{
public:
//-----------------------------------------------------
// Constructor
//
// int_exp_w = fixed-point integer width OR floating-point exponent width
// frac_w = fixed-point fraction width OR floating-point mantissa width
// is_float = true=floating-point, false=fixed-point
// guard_w = fixed-point guard bits width (default is calculated as log2(frac_w)
// 1+int_exp_w+frac_w+guard_w must fit in T
//
// So the most significant bit is the sign, followed by int_exp_w bits of integer/exponent, followed by frac_w+guard_w bits of fraction.
//-----------------------------------------------------
Cordic( uint32_t int_exp_w, // fixed-point integer width OR floating-point exponent width
uint32_t frac_w, // fixed-point fraction width OR floating-point mantissa width
bool is_float=true, // true=floating-point, false=fixed-point
uint32_t guard_w=-1, // number of guard bits used for CORDIC proper (-1 == default == log2(frac_w))
uint32_t n=-1 ); // number of iterations used for CORDIC proper (-1 == default == frac_w)
~Cordic();
void log_constructed( void ); // for when Cordic constructed before logging enabled
//-----------------------------------------------------
// Construction
//-----------------------------------------------------
T make_fixed( bool sign, const T& i, const T& f ) const; // fixed-point value with sign, integer part i, and fractional part f
T make_float( bool sign, const T& e, const T& f ) const; // floating-point value using sign, exponent part 3, and mantissa part f
//-----------------------------------------------------
// Explicit Conversions
//-----------------------------------------------------
T to_t( FLT x, bool is_final=false, bool to_fixed=false ) const; // FLT to T encoded value; lsb is never rounded
FLT to_flt( const T& x ) const; // T encoded value to FLT
std::string to_string( const T& x, bool from_fixed=false ) const; // T to std::string in decimal floating-point format
std::string to_rstring( const T& x, bool from_fixed=false ) const; // T to std::string in raw decimal integer format
std::string to_bstring( const T& x, bool from_fixed=false ) const; // T to std::string in binary format, like "1 001 101101011010"
//-----------------------------------------------------
// Constants (T 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
uint32_t exp_w( void ) const; // exp_w from above
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_exp_w + frac_w + guard_w (i.e., overall width)
uint32_t n( void ) const; // n from above
T maxint( void ) const; // largest positive integer (just integer part)
T max( void ) const; // encoded maximum positive value
T min( void ) const; // encoded minimum positive value
T denorm_min( void ) const; // fixed-point: min()
T lowest( void ) const; // encoded most negative value
T epsilon( void ) const; // difference between 1 and least value greater than 1
T round_error( void ) const; // maximum rounding error
T zero( void ) const; // encoded 0.0
T one( void ) const; // encoded 1.0
T neg_one( void ) const; // encoded -1.0
T two( void ) const; // encoded 2.0
T half( void ) const; // encoded 0.5
T quarter( void ) const; // encoded 0.25
T sqrt2( void ) const; // encoded sqrt(2)
T sqrt2_div_2( void ) const; // encoded sqrt(2)/2
T pi( void ) const; // encoded PI
T tau( void ) const; // encoded 2*PI
T pi_div_2( void ) const; // encoded PI/2
T pi_div_4( void ) const; // encoded PI/4
T one_div_pi( void ) const; // encoded 1/PI
T two_div_pi( void ) const; // encoded 2/PI
T four_div_pi( void ) const; // encoded PI/4
T e( void ) const; // encoded natural exponent
T nan( const char * arg ) const; // to_t( std::strtod( arg, nullptr )
T quiet_NaN( void ) const; // to_t( std::numeric_limits<FLT>::quiet_NaN() )
T signaling_NaN( void ) const; // to_t( std::numeric_limits<FLT>::signaling_NaN() )
T infinity( void ) const; // to_t( std::numeric_limits<FLT>::infinity() )
T ninfinity( void ) const; // to_t( -std::numeric_limits<FLT>::infinity() )
//-----------------------------------------------------
// Well-Known Math Functions Implemented Using CORDIC
//
// (2) means requires 2 applications of a CORDIC algorithm. functionality
//----------------------------------------------------- ---------------------------
// deconstruction
bool signbit( const T& x ) const; // x < 0
T frexp( const T& x, int * e ) const; // return normalized fraction and exponent
T modf( const T& x, T * i ) const; // decompose into fraction and integer (both with sign of x), still encoded
int ilogb( const T& x ) const; // unbiased exponent as int
T logb( const T& x ) const; // unbiased exponent, still encoded
int fpclassify( const T& x ) const; // FP_ZERO, FP_NORMAL, FP_SUBNORMAL, FP_INFINITE, FP_NAN
bool iszero( const T& x ) const; // fpclassify() is FP_ZERO
bool isfinite( const T& x ) const; // fpclassify() is FP_ZERO or FP_NORMAL or FP_SUBNORMAL
bool isinf( const T& x ) const; // fpclassify() is FP_INFINITE
bool isnan( const T& x ) const; // fpclassify() is FP_NAN
bool isnormal( const T& x ) const; // fpclassify() is FP_NORMAL
bool issubnormal( const T& x ) const; // fpclassify() is FP_SUBNORMAL
// rounding
int fesetround( int round ); // set rounding mode to FE_{DOWNWARD,UPWARD,TOWARDZDERO,TONEAREST}; return 0
int fegetround( void ) const; // returns current rounding mode
T nextafter( const T& from, const T& to ) const; // (from == to) ? to : (from +/- min toward to)
T nexttoward( const T& from, long double to ) const; // (from == to) ? to_t(to) : (from +/- min toward to)
T floor( const T& x ) const; // largest integral value <= x
T ceil( const T& x ) const; // smallest integral value >= x
T trunc( const T& x ) const; // nearest integral value toward 0
T extend( const T& x ) const; // nearest integral value away from 0
T round( const T& x ) const; // nearest integral value; halfway cases away from 0
long lround( const T& x ) const; // same as round() except returns just the raw integer part as long
long long llround( const T& x ) const; // same as round() except returns just the raw integer part as long long
T iround( const T& x ) const; // same as round() except returns just the raw integer part as T
T rint( const T& x, int rmode=-1 ) const; // nearest integral value according to rounding mode (-1 means fegetround()):
// FE_NOROUND: x (extension)
// FE_DOWNWARD: floor(x)
// FE_UPWARD: ceil(x)
// FE_TOWWARDZERO: trunc(x)
// FE_AWAYFROMZERO: extend(x) (extension)
// FE_TONEAREST: round(x)
long lrint( const T& x ) const; // same as rint() except returns just the raw integer part as long
long long llrint( const T& x ) const; // same as rint() except returns just the raw integer part as long long
T irint( const T& x ) const; // same as rint() except returns just the raw integer part as T
T nearbyint( const T& x ) const; // same as rint() but never raises FE_INEXACT
T floorfrac( const T& x ) const; // largest value <= x (and clear guard bits)
T ceilfrac( const T& x ) const; // smallest value >= x (and clear guard bits)
T truncfrac( const T& x ) const; // nearest value toward 0 (and clear guard bits)
T extendfrac( const T& x ) const; // nearest value away from 0 (and clear guard bits)
T roundfrac( const T& x ) const; // nearest value; halfway cases away from 0 (and clear guard bits)
T rfrac( const T& x, int rmode=-1 ) const; // use guard bits to round value according to rounding mode (-1 means fegetround()):
// FE_NOROUND: x (extension)
// FE_DOWNWARD: floorfrac(x)
// FE_UPWARD: ceilfrac(x)
// FE_TOWWARDZERO: truncfrac(x)
// FE_AWAYFROMZERO: extendfrac(x) (extension)
// FE_TONEAREST: roundfrac(x)
// basic arithmetic
T abs( const T& x ) const; // |x|
T neg( const T& x ) const; // -x
T copysign( const T& x, const T& y ) const; // |x| with sign of y
T add( const T& x, const T& y ) const; // x+y
T sub( const T& x, const T& y ) const; // x-y
T scalbn( const T& x, int y ) const; // x * 2^y (i.e., left-shift)
T scalbnn( const T& x, int y ) const; // x * 2^(-y) (i.e., right-shift)
T ldexp( const T& x, int y ) const; // x * 2^y (same as scalbn)
T fma( const T& x, const T& y, const T& addend ) const; // x*y + addend
T mul( const T& x, const T& y ) const; // x*y
T mulc( const T& x, const T& c ) const; // x*c where c is known to be a constant
T sqr( const T& x ) const; // x*x
T fda( const T& y, const T& x, const T& addend ) const; // y/x + addend
T div( const T& y, const T& x ) const; // y/x
T remainder( const T& y, const T& x ) const; // IEEE 754-style remainder: y - n*x (where n is nearest int)
T fmod( const T& y, const T& x ) const; // rem = remainder( |y|, |x| ); if (rem < 0) rem += x; rem = copysign(rem, x)
T remquo( const T& y, const T& x, int * quo ) const; // remainder(), and *quo receives sign and at least 3 LSBs of y/x
T rcp( const T& x ) const; // 1/x
// comparisons
int compare( const T& _x, const T& _y ) const; // -2 if one NaN, -4 if two NaNs, -1 if x < y, 0 if x == y, 1 if x > y
bool isgreater( const T& x, const T& y ) const; // x > y
bool isgreaterequal( const T& x, const T& y ) const; // x >= y
bool isless( const T& x, const T& y ) const; // x > y
bool islessequal( const T& x, const T& y ) const; // x >= y
bool islessgreater( const T& x, const T& y ) const; // x != y (but returns false if at least one is NaN)
bool isunordered( const T& x, const T& y ) const; // returns true if x is a NaN OR y is a NaN
bool isunequal( const T& x, const T& y ) const; // x != y (returns true if only one is NaN)
bool isequal( const T& x, const T& y ) const; // x == y (returns true if both are NaN)
T fdim( const T& x, const T& y ) const; // (x >= y) ? (x-y) : 0
T fmax( const T& x, const T& y ) const; // max(x, y)
T fmin( const T& x, const T& y ) const; // min(x, y)
// elementary functions
T sqrt( const T& x ) const; // hypoth( x+1, x-1 ) / 2
T rsqrt( const T& x ) const; // 1.0 / sqrt( x )
T rsqrt_orig( const T& x ) const; // x^(-1/2) = exp(log(x)/-2)
T cbrt( const T& x ) const; // x^(1/3) = exp(log(x)/3)
T rcbrt( const T& x ) const; // x^(-1/3) = exp(log(x)/-3)
T exp( const T& x ) const; // e^x
T expm1( const T& x ) const; // e^x - 1
T expc( const FLT& b, const T& x ) const; // b^x = exp(x * log(b)) b=const (2)
T exp2( const T& x ) const; // 2^x
T exp10( const T& x ) const; // 10^x
T pow( const T& b, const T& x ) const; // b^x = exp(x * log(b)) (3)
T log( const T& x ) const; // 2*atanh2(x-1, x+1)
T log( const T& x, const T& b ) const; // log(x)/log(b) (3)
T log1p( const T& x ) const; // 2*atanh2(x, x+2) = log(x+1)
T logc( const T& x, const FLT& b ) const; // log(x)/log(b) b=const (2)
T log2( const T& x ) const; // log(x)/log(2) (2)
T log10( const T& x ) const; // log(x)/log(10) (2)
T deg2rad( const T& x ) const; // x * PI / 180
T rad2deg( const T& x ) const; // x * 180 / PI
T sin( const T& x, const T * r=nullptr ) const; // r*sin(x) (default r is 1)
T sinpi( const T& x, const T * r=nullptr ) const; // r*sin(x*PI) (defualt r is 1
T cos( const T& x, const T * r=nullptr ) const; // r*cos(x) (default r is 1)
T cospi( const T& x, const T * r=nullptr ) const; // r*cos(x*PI) (defualt r is 1
void sincos( const T& x, T& si, T& co, const T * r=nullptr ) const; // si=r*sin(x), co=r*cos(x) (default r is 1)
void sinpicospi( const T& x, T& si, T& co, const T * r=nullptr ) const;// si=r*sin(x*PI), co=r*cos(x*PI) (default r is 1)
T tan( const T& x ) const; // sin(x) / cos(x) (2)
T tanpi( const T& x ) const; // sin(x*PI) / cos(x*PI) (2)
T asin( const T& x ) const; // atan2(x, sqrt(1 - x^2)) (2)
T acos( const T& x ) const; // atan2(sqrt(1 - x^2), x) (2)
T atan( const T& x ) const; // atan(x)
T atan2( const T& y, const T& x ) const; // atan2(y, x)
void polar_to_rect( const T& r, const T& a, T& x, T& y ) const; // sincos(a, x, y, &r)
void rect_to_polar( const T& x, const T& y, T& r, T& a ) const; // r=sqrt(x^2 + y^2), a=atan2(y, x)
T hypot( const T& x, const T& y ) const; // sqrt(x^2 + y^2) (Euclidean hypot)
T hypoth( const T& x, const T& y ) const; // sqrt(x^2 - y^2) ("hyperbolic hypot")
T sinh( const T& x, const T * r=nullptr ) const; // r*sinh(x), also r*(e^x - e^-x)/2 (default r is 1)
T cosh( const T& x, const T * r=nullptr ) const; // r*cosh(x), also r*(e^x + e^-x)/2 (default r is 1)
void sinhcosh( const T& x, T& sih, T& coh, const T * r=nullptr ) const;// sih=r*sinh(x), coh=r*cosh(x) (default r is 1)
T tanh( const T& x ) const; // sinh(x) / cosh(x) (2)
T asinh( const T& x ) const; // log(x + sqrt(x^2 + 1)) (2)
T acosh( const T& x ) const; // log(x + sqrt(x^2 - 1)) (2)
T atanh( const T& x ) const; // atanh(x)
T atanh2( const T& y, const T& x ) const; // atanh(y/x)
//-----------------------------------------------------
// Bob's Collection of Math Identities (some are used in the implementation, most are not)
//
// sqrt(x) = sqrt((x+0.25)^2 - (x-0.25)^2) allows use of hyperbolic_vectoring
// sqrt(x) = sqrt((x+1)^2 - (x-1)^2) / 2 ditto, but easier to make sure |atanh((x-1)/(x+1))| is small enough
// sqrt(x*y) = sqrt((x+y)^2 - (x-y)^2) / 2 ditto, y doesn't matter
// sqrt(x*y) = sqrt(|x|) * sqrt(|y|) assuming x*y >= 0
// sqrt(x^2 - y^2) = sqrt((m+d)^2 - (m-d)^2) = 2*sqrt(m*d) where m=(x+y)/2, d=(x-y)/2
// factor m*d=p*s so that p is a power-of-2 and s is within -1 .. 1
// 2*sqrt(m*d) = 2*sqrt(p) * sqrt(s) = sqrt(p) * sqrt((s+1)^2 - (s-1)^2)
// if log2(p) is even and >= 2, then sqrt(p) = 2^(log2(p)/2) = some integer
// sqrt(1+x) - 1 = x / (sqrt(1+x)+1)
// 1-sqrt(1-x) = x / (sqrt(1-x)+1)
// 1-(1-x)^2 = x * (2-x)
//
// pow(b,x) = exp(log(b) * x)
// exp2(x) = 2^x = exp(log(2) * x)
// exp(x) = exp2(log2(e) * x)
// exp2(i+f) = exp2(i) * exp2(f) i = integer part, f = fractional remainder
// = exp2(f) << i
// = exp(log(2)*f) << i
// exp(x) = sinh(x) + cosh(x) if x is already reduced, use hyperbolic CORDIC directly to get this sum
// exp(-x) = 1/exp(x)
// exp(x+y) = exp(x) * exp(y)
// exp(ix) = cos(x) + i*sin(x) Euler's Formula, i = sqrt(-1)
// exp(i*pi) + 1 = 0 Euler's Identity
// exp(x)-1 = tanh(x/2)*(exp(x)+1) expm1(x)
// exp(x)-1 = u = exp(x); (u == 1) ? x : ((u-1) == -1) ? -1 : (u-1)*x/log(u)
//
// log(x) = 2*atanh2(x-1, x+1)
// log(x) = atanh2((x^2 - 1, x^2 + 1)
// log(x*y) = log(x) + log(y)
// log(b^i) = i*log(b)
// log(2^i + f) = i*log(2) + log(f) i=integer f=remainder
// log(x/4) = atanh((x-0.25)/(x+0.25))
// log(x/y) = log(x) - log(y)
// log(x/y) = 2*atanh((x-y)/(x+y))
// log(1+x) = 2*atanh(x/(x+2)) log1p()
//
// sin(-x) = -sin(x)
// sin(x) = cos(x - pi/2)
// sin(x) = sin(i*pi/2 + f) where i is an integer and |f| <= pi/4
// sin(x+y) = sin(x)*cos(y) + cos(x)*sin(y)
// sin(x-y) = sin(x)*cos(y) - cos(x)*sin(y)
// sin(x+y) = 2*sin(x)*cos(y) - sin(x-y)
// sin(2x) = 2*sin(x)*cos(x) but it's cheaper to just compute sin(x+x)
// sin(2x) = sin(x) * (2*cos(x))
// sin^2(x) = (1 - cos(2x)) / 2
// sin(3x) = sin(x) * (4*cos^2(x) - 1)
// sin(nx) = 2*sin((n-1)*x)*cos(x) - sin((n-2)*x)
// sin(x+pi/4) = sqrt(2)/2 * (sin(x) + cos(x))
// sin(i*pi/2 + f) = +/-sin(f) or +/-cos(f)
// sin(x*pi) = sin(2x * pi/2) = sin((i+f)*pi/2) = +/-sin(f*pi/2) or +/-cos(f*pi/2)
// sin(x) = Im(e^(ix)) = (e^(ix) + e^(-ix)) / 2
// sin(ix) = i*sinh(x)
// sin(x)/x = (1 + x*x*(1/6)) == 1) ? 1 : sin(x)/x
// sin(x)*sin(y) = (cos(x-y) - cos(x+y)) / 2
//
// cos(-x) = cos(x)
// cos(x) = sin(x + pi/2)
// cos(x+y) = cos(x)*cos(y) - sin(x)*sin(y)
// cos(x+pi/4) = sqrt(2)/2 * (cos(x) - sin(x))
// cos(i*pi/2 + f) = +/-cos(f) or +/-sin(f) i=integer, f=frac
// cos(x*pi) = cos(2x * pi/2) = cos((i+f)*pi/2) = +/-cos(f*pi/2) or +/-sin(f*pi/2)
// cos(2x) = -sin^2(x) + cos^2(x)
// cos(2x) = 2*cos^2(x) - 1 but it's cheaper to just compute cos(x+x)
// cos^2(x) = (cos(2x) + 1) / 2 this could come in handy
// cos(3x) = -3*cos(x) + 4*cos^3(x) but it's cheaper to just compute cos(x+x+x)
// cos(nx) = 2*cos((n-1)*x)*cos(x) - cos((n-2)x)
// cos(x) = Re(e^(ix)) = (e^(ix) - e^(-ix)) / 2i i=sqrt(-1)
// cos(ix) = cosh(x) i=sqrt(-1)
// 1-cos(x) = 2*sin(x/2)^2
// (1-cos(x))/x = (1 + x*x) == 1) ? 0.5*x : cosf1(x)/x
// cos(x)*cos(y) = (cos(x-y) + cos(x+y)) / 2
// sin(x)*cos(v) = (sin(x+y) + sin(x-y)) / 2
// cos(x)*sin(v) = (sin(x+y) - sin(x-y)) / 2
//
// tan(-x) = -tan(x)
// tan(x+y) = (tan(x) + tan(y)) / (1 - tan(x)*tan(y))
// tan(x/2) = sin(x) / (1 + cos(x))
// tan(x/2 + PI/4) = cos(x) / (1 - sin(x))
// tan^2(x) = (1 - cos(2x)) / (1 + cos(2x))
//
// asin(-x) = -asin(x)
// asin(x) = atan2(x, sqrt(1 - x^2))
//
// acos(-x) = acos(-x)
// acos(x) = atan2(sqrt(1 - x^2), x)
// acos(x+y) = pi/2 - asin(x+y)
// acos(1-x) = 2*asin(x/2)
// acos(dot(u, v)) = 2*atan2( |u-v|, |u+v| )
// acos(dot(u, v)) = (dot(u,v) < 0) ? (pi - 2*asin(|-v-u|/2)) : 2*asin(|v-u|/2)
//
// atan(x) = atan2(x, 1) this is true only when second argument is > 0 (see below)
// atan(-x) = -atan(x)
// atan(1/x) = pi/2 - atan(x) if x > 0
// atan(x) = asin(x / sqrt(1 + x^2))
// atan(x) = 2*atan(x / (1 + sqrt(1 + x^2)))
// atan2(y,x) = 2*atan(y / (sqrt(x^2 + y^2) + x)) if x > 0 (x < 0 can have inflated rounding errors, so...)
// atan2(y,x) = 2*atan((sqrt(x^2 + y^2) - x) / y) if x <= 0 && y != 0
// atan2(y,x) = pi if x < 0 && y == 0
// atan2(y,x) = undefined if x == 0 && y == 0
//
// pi = 4*atan(1) but low 2 bits will end up as 0 for a fixed-point number
// pi = acos(-1) but that just uses atan
// pi = pi() function in this class to return a precomputed high-precision version
// -3*pi/4 = atan(-1,-1)
//
// sinh(-x) = -sinh(x)
// sinh(x) = (e^x - e^-x)/2
// = (e^x - 1/e^x)/2
// sinh(x+y) = sinh(x)*cosh(y) + cosh(x)*sinh(y)
// sinh(2^i + f) = sinh(2^i)*cosh(f) + cosh(2^i)*sinh(f)
// sinh(2^i) = (e^(2^i) - e^(-2^i))/2
// = (2^(log2(e) << i) - 2^(-log2(e) << i))/2 let j = integer part of (log2(e) << i), g = fractional part
// = ((2^g << j) - (2^(-g) >> j))/2
// = (exp(log(2)*g) << (j-1)) - (1/exp(log(2)*g) >> (j+1))
// note: exp(log(2)*g) == hyperbolic_rotation()
// note: this whole method doesn't look more efficient than just using (e^x - 1/e^x)/2
//
// cosh(-x) = cosh(x)
// cosh(x) = (e^x + e^-x)/2
// = (e^x + 1/e^x)/2
// cosh(x+y) = cosh(x)*cosh(y) + sinh(x)*sinh(y)
// cosh(2^i + f) = cosh(2^i)*cosh(f) + sinh(2^i)*sinh(f)
// cosh(2^i) = (e^(2^i) + e^(-2^i))/2
// = (2^(log2(e) << i) + 2^(-log2(e) << i))/2 let j = integer part of (log2(e) << i), g = fractional part
// = ((2^g << j) + (2^(-g) >> j))/2
// = (exp(log(2)*g) << (j-1)) + (1/exp(log(2)*g) >> (j+1))
// note: exp(log(2)*g) == hyperbolic_rotation()
// note: this whole method doesn't look more efficient than just using (e^x + 1/e^x)/2
//
// tanh(-x) = -tanh(x)
// tanh(x) = (e^x - e^-x) / (e^x + e^-x)
// tanh(x) = expm1(2*x) / (expm1(2*x) + 2)
//
// asinh(-x) = -asinh(x)
// asinh(x) = log(x + sqrt(x^2 + 1))
// asinh(x) = atanh(x / sqrt(1 + x^2))
// asinh(x) = log1p(x * (1 + x/(sqrt(x^2 + 1)+1))
//
// acosh(-x) = [illegal, x must be >= 1]
// acosh(x) = log(x + sqrt(x^2 - 1))
// acosh(x) = abs( asinh(sqrt(x^2 - 1)) )
//
// atanh(-x) = -atanh(x)
// atanh(x) = log((1+x)/(1-x))/2 = log(1+x)/2 - log(1-x)/2 note: x must be between -1 and 1
// atanh(x) = asinh(x / sqrt(1 - x^2))
// atanh(x) = acosh(1 / sqrt(1 - x^2)) (+/-)
// atanh(x) = log1p(2*x/(1-x)) / 2
//
// slerp(v0,v1,t) = sin((1-t)*Ang)/sin(Ang)*v0 + sin(t*Ang)/sin(Ang)*v1 (Ang = angle between v0 and v1
// [see other equations when Ang ~= 0 or Ang ~= 180]
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//
// WHAT FOLLOWS ARE ADVANCED ROUTINES THAT MOST PEOPLE WON'T EVER NEED
//
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
// The basic CORDIC functions that all the above math functions ultimately use.
//-----------------------------------------------------
// circular rotation mode results after step n:
// x = gain*(x0*cos(z0) - y0*sin(z0)) gain=1.64676...
// y = gain*(y0*cos(z0) + x0*sin(z0))
// z = 0
//
// input ranges allowed:
// -1 <= x0 <= 1
// -1 <= y0 <= 1
// -PI/4 <= z0 <= PI/4
//
// output ranges:
// -sqrt(2) <= x <= sqrt(2)
// -sqrt(2) <= y <= sqrt(2)
// 0 <= z <= 0
//
void circular_rotation( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
// circular vectoring mode results after step n:
// x = gain*sqrt(x0^2 + y0^2) gain=1.64676...
// y = 0
// z = z0 + atan( y0/x0 )
//
// input ranges allowed:
// -3 <= x0 <= 3
// -1 <= y0 <= 1
// -PI/4 <= z0 <= PI/4
// |atan(y/x)| <= PI/4
//
// output ranges:
// 0 <= x <= sqrt(2)
// 0 <= y <= 0
// -PI <= z <= PI (if z0 == 0)
//
void circular_vectoring( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
void circular_vectoring_xy( const T& x0, const T& y0, T& x, T& y ) const; // if z not needed
// hyperbolic rotation mode results after step n:
// x = gain*(x0*cosh(z0) + y0*sinh(z0)) gain=0.828159...
// y = gain*(y0*cosh(z0) + x0*sinh(z0))
// z = 0
//
// input ranges allowed:
// -2 <= x0 <= 2
// -2 <= y0 <= 2
// |z0| <= 1.1182...
//
// output ranges:
// 1 <= x <= 2
// -2 <= y <= 2
// 0 <= z <= 0
//
void hyperbolic_rotation( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
// hyperbolic vectoring mode results after step n:
// x = gain*sqrt(x0^2 - y0^2) gain=0.828159...
// y = 0
// z = z0 + atanh( y0/x0 )
//
// input ranges allowed:
// -2 <= x0 <= 2
// -2 <= y0 <= 2
// -PI <= z0 <= PI
// |atanh(y0/x0)| <= 1.1182...
//
// output ranges:
// -2 <= x <= 2
// 0 <= y <= 0
// -PI/2 <= z <= PI/2 (if z0 == 0)
//
void hyperbolic_vectoring( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
void hyperbolic_vectoring_xy( const T& x0, const T& y0, T& x, T& y ) const; // if z not needed
// linear rotation mode results after step n:
// x = x0
// y = y0 + x0*z0
// z = 0
//
// input ranges allowed:
// -2 <= x0 <= 2
// -2 <= y0 <= 2
// |z0| <= 1
//
// output ranges:
// -2 <= x <= 2
// -2 <= y <= 2
// 0 <= z <= 0
//
void linear_rotation( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
// linear vectoring mode results after step n:
// x = x0
// y = 0
// z = z0 + y0/x0
//
// input ranges allowed:
// -2 <= x0 <= 2
// -2 <= y0 <= 2
// |y0/x0| <= 1
//
// output ranges:
// -2 <= x <= 2
// 0 <= y <= 0
// -PI/2 <= z <= PI/2 (if z0 == 0)
//
void linear_vectoring( const T& x0, const T& y0, const T& z0, T& x, T& y, T& z ) const;
//-----------------------------------------------------
// These version are used internally, but making them available publically.
// In general, you should only call the earlier routines.
//-----------------------------------------------------
T neg( const T& x, bool is_final ) const;
T add( const T& x, const T& y, bool is_final ) const;
T sub( const T& x, const T& y, bool is_final ) const;
T scalbn( const T& x, int y, bool is_final ) const;
T fma_fda( bool is_fma, const T& x, const T& y, const T& addend, bool is_final ) const;
T mul( const T& x, const T& y, bool is_final ) const;
T mulc( const T& x, const T& c, bool is_final ) const;
T sqr( const T& x, bool is_final ) const;
T div( const T& y, const T& x, bool is_final ) const;
T sqrt( const T& x, bool is_final ) const;
T exp( const T& x, bool is_final, FLT b=M_E ) const;
T log( const T& x, bool is_final ) const;
T log1p( const T& x, bool is_final ) const;
T hypot( const T& x, const T& y, bool is_final ) const;
T hypoth( const T& x, const T& y, bool is_final ) const;
T atan2( const T& y, const T& x, bool is_final, bool x_is_one, T * r ) const;
T atanh2( const T& y, const T& x, bool is_final, bool x_is_one ) const;
void sincos( bool times_pi, const T& x, T& si, T& co, bool is_final, bool need_si, bool need_co, const T * r ) const;
void sinhcosh( const T& x, T& sih, T& coh, bool is_final, bool need_sih, bool need_coh, const T * r ) const;
//-----------------------------------------------------
// Argument Range Reduction Routines
//
// These are called to reduce arguments from normal fixed/float form to
// reduced fixed form appropriate for core CORDIC routins.
//-----------------------------------------------------
enum class EXP_CLASS
{
ZERO, // can be used to save power
NORMAL, // for fixed-point CORDIC, this is always the class
SUBNORMAL, // these others apply to floating-point (is_float=true)
INFINITE,
NOT_A_NUMBER,
};
std::string to_str( const EXP_CLASS& c ) const
{
switch( c )
{
case EXP_CLASS::ZERO: return "ZERO";
case EXP_CLASS::NORMAL: return "NORMAL";
case EXP_CLASS::SUBNORMAL: return "SUBNORMAL";
case EXP_CLASS::INFINITE: return "INFINITY";
case EXP_CLASS::NOT_A_NUMBER: return "NAN";
default: return "<unknown>";
}
}
FLT _to_flt( const T& x, bool is_final=false, bool from_fixed=false, bool allow_debug=false ) const; // T encoded value to FLT
EXP_CLASS classify( const T& x ) const; // returns exp class only
void deconstruct( T& x, EXP_CLASS& x_exp_class, int32_t& x_exp, bool& sign, bool allow_debug=true ) const; // x will end up as fixed-point for _is_float=true
void reconstruct( T& x, EXP_CLASS x_exp_class, int32_t x_exp, bool sign ) const; // x will end up as float value for _is_float=true
void reduce_add_args( T& x, T& y, EXP_CLASS& x_exp_class, int32_t& x_exp, bool& x_sign, EXP_CLASS& y_exp_class, int32_t& y_exp, bool& y_sign ) const;
void reduce_mul_div_args( bool is_fma, T& x, T& y, EXP_CLASS& x_exp_class, int32_t& x_exp, EXP_CLASS& y_exp_class, int32_t& y_exp, bool& sign ) const;
void reduce_sqrt_arg( T& x, EXP_CLASS& x_exp_class, int32_t& x_exp, bool& x_sign ) const;
void reduce_exp_arg( FLT b, T& x, int32_t& i, EXP_CLASS& x_exp_class, bool& x_sign ) const;
void reduce_log_arg( T& x, EXP_CLASS& x_exp_class, bool& x_sign, T& addend ) const;
void reduce_hypot_args( T& x, T& y, EXP_CLASS& exp_class, int32_t& exp, bool& swapped ) const;
void reduce_sincos_arg( bool times_pi, T& a, uint32_t& quadrant, EXP_CLASS& exp_class, bool& sign, bool& did_minus_pi_div_4 ) const;
//-----------------------------------------------------
// Logging Support
//
// There are many reasons you might want to log Cordic operations.
// You can use the default Logger (new Logger<T,FLT>( ... )) or supply your
// own Logger subclass.
//
// NOTE: Logging is done globally, not just for one Cordic.
// Thus the static methods here.
//-----------------------------------------------------
static void logger_set( Logger<T,FLT> * logger ); // null means use the default logger
static Logger<T,FLT> * logger_get( void ); // returns current logger
static std::string op_to_str( uint16_t op ); // supply this to Logger constructor
void constructed( const T& x ) const; // so we can log creation of x
void destructed( const T& x ) const; // so we can log destruction of x
T& assign( T& x, const T& y ) const; // x = y (this exists so we can log assignments)
T& pop_value( T& x, const T& y ) const; // x = y (where y is top of stack for logging)
bool pop_bool( bool ) const; // so we can log consumption of bool
enum class OP
{
push_constant,
to_flt,
assign,
pop_value,
pop_bool,
frexp,
modf,
ilogb,
logb,
nextafter,
nexttoward,
floor,
ceil,
trunc,
extend,
round,
lround,
llround,
iround,
rint,
lrint,
llrint,
irint,
nearbyint,
floorfrac,
ceilfrac,
truncfrac,
extendfrac,
roundfrac,
rfrac,
abs,
neg,
copysign,
add,
sub,
fma,
mul,
mulc,
sqr,
scalbn,
ldexp,
fda,
div,
rcp,
sqrt,
rsqrt,
cbrt,
rcbrt,
iszero,
isgreater,
isgreaterequal,
isless,
islessequal,
islessgreater,
isunordered,
isunequal,
isequal,
fdim,
fmax,
fmin,
exp,
expm1,
expc,
exp2,
exp10,
pow,
log,
log1p,
logn,
logc,
log2,
log10,
deg2rad,
rad2deg,
sin,
sinpi,
cos,
cospi,
sincos,
sinpicospi,
tan,
tanpi,
asin,
acos,
atan,
atan2,
polar_to_rect,
rect_to_polar,
hypot,
hypoth,
sinh,
cosh,
sinhcosh,
tanh,
asinh,
acosh,
atanh,
atanh2,
// these are here for convenience, but have nothing to do with CORDIC
sram_rd,
sram_wr,
dram_rd,
dram_wr,
};
static constexpr uint32_t OP_cnt = uint32_t(OP::dram_wr) + 1;
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//
//
// ___ _ _ _ _
// |_ _|_ __ ___ _ __ | | ___ _ __ ___ ___ _ __ | |_ __ _| |_(_) ___ _ __
// | || '_ ` _ \| '_ \| |/ _ \ '_ ` _ \ / _ \ '_ \| __/ _` | __| |/ _ \| '_ \
// | || | | | | | |_) | | __/ | | | | | __/ | | | || (_| | |_| | (_) | | | |
// |___|_| |_| |_| .__/|_|\___|_| |_| |_|\___|_| |_|\__\__,_|\__|_|\___/|_| |_|
// |_|
//
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
private:
bool _is_float;
uint32_t _int_w;
uint32_t _exp_w;
uint32_t _frac_w;
uint32_t _guard_w;
uint32_t _frac_guard_w; // sum of the two
T _frac_guard_mask;
T _guard_mask;
uint32_t _exp_mask;
int32_t _exp_bias;
int32_t _exp_unbiased_min;
int32_t _exp_unbiased_max;
uint32_t _w;
uint32_t _n;
int _rounding_mode;
T _quiet_NaN_fxd;
T _maxint;
T _max;
T _min;
T _min_fxd;
T _lowest;
T _zero;
T _zero_fxd;
T _neg_zero;
T _quarter;
T _third;
T _neg_third;
T _half;
T _half_fxd;
T _one;
T _one_fxd;
T _neg_one;
T _two;
T _two_fxd;
T _four;
T _four_fxd;
T _sqrt2;
T _sqrt2_div_2;
T _pi;
T _neg_pi;
T _pi_fxd;
T _tau;
T _pi_div_2;
T _pi_div_4;
T _one_div_pi;
T _two_div_pi;
T _four_div_pi;
T _e;
T _log2;
T _log10;
T * _circular_atan_fxd; // circular atan values
T _circular_rotation_gain_fxd; // circular rotation gain
T _circular_rotation_one_over_gain_fxd; // circular rotation 1/gain
T _circular_rotation_one_over_gain; // circular rotation 1/gain
T _circular_vectoring_gain_fxd; // circular vectoring gain
T _circular_vectoring_one_over_gain_fxd; // circular vectoring 1/gain
T _circular_vectoring_one_over_gain; // circular vectoring 1/gain
T _circular_angle_max_fxd; // circular vectoring |z0| max value
T * _hyperbolic_atanh_fxd; // hyperbolic atanh values
T _hyperbolic_rotation_gain_fxd; // hyperbolic rotation gain
T _hyperbolic_rotation_one_over_gain_fxd; // hyperbolic rotation 1/gain
T _hyperbolic_rotation_one_over_gain; // hyperbolic rotation 1/gain
T _hyperbolic_vectoring_gain_fxd; // hyperbolic vectoring gain
T _hyperbolic_vectoring_one_over_gain_fxd; // hyperbolic vectoring 1/gain
T _hyperbolic_vectoring_one_over_gain; // hyperbolic vectoring 1/gain
T _hyperbolic_angle_max_fxd; // hyperbolic vectoring |z0| max value
static Logger<T,FLT> * logger;
};
//-----------------------------------------------------
// Logging
//-----------------------------------------------------
template< typename T, typename FLT >
Logger<T,FLT> * Cordic<T,FLT>::logger = nullptr;
template< typename T, typename FLT >
void Cordic<T,FLT>::logger_set( Logger<T,FLT> * _logger )
{
logger = _logger;
}
template< typename T, typename FLT >
Logger<T,FLT> * Cordic<T,FLT>::logger_get( void )
{
return logger;
}
template< typename T, typename FLT >
std::string Cordic<T,FLT>::op_to_str( uint16_t op )
{
#define _ocase( op ) case OP::op: return #op;
switch( OP( op ) )
{
_ocase( push_constant )
_ocase( to_flt )
_ocase( assign )
_ocase( pop_value )
_ocase( pop_bool )
_ocase( frexp )
_ocase( modf )
_ocase( ilogb )
_ocase( logb )
_ocase( nextafter )
_ocase( nexttoward )
_ocase( floor )
_ocase( ceil )
_ocase( trunc )
_ocase( extend )
_ocase( round )
_ocase( lround )
_ocase( llround )
_ocase( iround )
_ocase( rint )
_ocase( lrint )
_ocase( llrint )
_ocase( irint )
_ocase( nearbyint )
_ocase( floorfrac )
_ocase( ceilfrac )
_ocase( truncfrac )
_ocase( extendfrac )
_ocase( roundfrac )
_ocase( rfrac )
_ocase( abs )
_ocase( neg )
_ocase( copysign )
_ocase( add )
_ocase( sub )
_ocase( fma )
_ocase( mul )
_ocase( mulc )
_ocase( sqr )
_ocase( scalbn )
_ocase( ldexp )
_ocase( fda )
_ocase( div )
_ocase( rcp )
_ocase( sqrt )
_ocase( rsqrt )
_ocase( iszero )
_ocase( isgreater )
_ocase( isgreaterequal )
_ocase( isless )
_ocase( islessequal )
_ocase( islessgreater )
_ocase( isunordered )
_ocase( isunequal )
_ocase( isequal )
_ocase( exp )
_ocase( expm1 )
_ocase( expc )
_ocase( exp2 )
_ocase( exp10 )
_ocase( pow )
_ocase( log )
_ocase( logn )
_ocase( log1p )
_ocase( logc )
_ocase( log2 )
_ocase( log10 )
_ocase( deg2rad )
_ocase( rad2deg )
_ocase( sin )
_ocase( sinpi )
_ocase( cos )
_ocase( cospi )
_ocase( sincos )
_ocase( sinpicospi )
_ocase( tanpi )
_ocase( asin )
_ocase( acos )
_ocase( atan )
_ocase( atan2 )
_ocase( polar_to_rect )
_ocase( rect_to_polar )
_ocase( hypot )
_ocase( hypoth )
_ocase( sinh )
_ocase( cosh )
_ocase( sinhcosh )
_ocase( tanh )
_ocase( asinh )
_ocase( acosh )
_ocase( atanh )
_ocase( atanh2 )
_ocase( sram_rd )
_ocase( sram_wr )
_ocase( dram_rd )
_ocase( dram_wr )
default: return "<unknown OP>";
}
}