-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathqffmath.c
2966 lines (2795 loc) · 94.6 KB
/
qffmath.c
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
/*!
* @file qffmath.c
* @author J. Camilo Gomez C.
* @note This file is part of the qLibs distribution.
**/
#include "qffmath.h"
#ifndef QLIBS_USE_STD_MATH
#include <stdint.h>
#include <string.h>
#include <float.h>
static const union {
uint32_t u[ 2 ];
float f[ 2 ];
} s_values = { { 0x7F800000U, 0x7FBFFFFFU } };
const float * const qFFMath_Infinity = &s_values.f[ 0 ];
const float * const qFFMath_NotANumber = &s_values.f[ 1 ];
/*cppcheck-suppress misra-c2012-20.7 */
#define cast_reinterpret( dst, src, dst_type ) \
(void)memcpy( &dst, &src, sizeof(dst_type) ) \
static float qFFMath_CalcCbrt( float x , bool r );
static float lgamma_positive( float x );
static float poly_laguerre_recursion( size_t n,
float alpha,
float x );
static float poly_laguerre_large_n( size_t n,
float alpha,
float x );
static float poly_laguerre_hyperg( size_t n,
float alpha,
float x );
static float poly_legendre_p( size_t l,
float x );
static float ellint_rf( float x,
float y,
float z );
static float ellint_rd( float x,
float y,
float z );
static float ellint_rc( float x,
float y );
static float ellint_rj( float x,
float y,
float z,
float p );
static float expint_E1_series( float x );
static float expint_E1_asymp( float x );
static float expint_Ei_asymp( float x );
static float expint_En_cont_frac( size_t n,
float x );
static float expint_Ei_series( float x );
static float expint_Ei( float x );
static float riemann_zeta_glob( float s );
static float riemann_zeta_product( float s );
static void gamma_temme( float mu,
float* gam1,
float* gam2,
float* gam_pl,
float* gam_mi);
static void bessel_jn( float nu,
float x,
float* j_nu,
float* n_nu,
float* j_pnu,
float* n_pnu );
static void sph_bessel_jn( size_t n,
float x,
float* j_n,
float* n_n,
float* jp_n,
float* np_n );
static void bessel_ik( float nu,
float x,
float* i_nu,
float* k_nu,
float* i_pnu,
float* k_pnu );
static float cyl_bessel_ij_series( float nu,
float x,
float sgn,
size_t max_iter );
/*============================================================================*/
int qFFMath_FPClassify( const float f )
{
uint32_t u = 0U;
int retVal;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( u, f, uint32_t );
u &= 0x7FFFFFFFU;
if ( 0U == u ) {
retVal = QFFM_FP_ZERO;
}
else if ( u < 0x00800000U ) {
retVal = QFFM_FP_SUBNORMAL;
}
else if ( u < 0x7F800000U ) {
retVal = QFFM_FP_NORMAL;
}
else if ( 0x7F800000U == u ) {
retVal = QFFM_FP_INFINITE;
}
else {
retVal = QFFM_FP_NAN;
}
return retVal;
}
/*============================================================================*/
bool qFFMath_IsNaN( const float x )
{
return ( QFFM_FP_NAN == qFFMath_FPClassify( x ) );
}
/*============================================================================*/
bool qFFMath_IsInf( const float x )
{
return ( QFFM_FP_INFINITE == qFFMath_FPClassify( x ) );
}
/*============================================================================*/
bool qFFMath_IsFinite( const float x )
{
return ( qFFMath_FPClassify( x ) < QFFM_FP_INFINITE );
}
/*============================================================================*/
bool qFFMath_IsNormal( const float x )
{
return ( qFFMath_FPClassify( x ) == QFFM_FP_NORMAL );
}
/*============================================================================*/
bool qFFMath_IsAlmostEqual( const float a,
const float b,
const float tol )
{
return ( qFFMath_Abs( a - b ) <= qFFMath_Abs( tol ) );
}
/*============================================================================*/
bool qFFMath_IsEqual( const float a,
const float b )
{
return ( qFFMath_Abs( a - b ) <= FLT_MIN );
}
/*============================================================================*/
float qFFMath_Abs( float x )
{
return ( x < 0.0F ) ? -x : x;
}
/*============================================================================*/
float qFFMath_Recip( float x )
{
uint32_t y = 0U;
float z = 0.0F;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( y, x, uint32_t );
y = 0x7EF311C7U - y;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( z, y, float );
return z*( 2.0F - ( x*z ) );
}
/*============================================================================*/
float qFFMath_Sqrt( float x )
{
float retVal;
if ( x < 0.0F ) {
retVal = QFFM_NAN;
}
else if ( QFFM_FP_ZERO == qFFMath_FPClassify( x ) ) {
retVal = 0.0F;
}
else {
uint32_t y = 0U;
float z = 0.0F;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( y, x, uint32_t );
y = ( ( y - 0x00800000U ) >> 1U ) + 0x20000000U;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( z, y, float );
z = ( ( x/z ) + z ) * 0.5F;
retVal = 0.5F*( ( x/z ) + z );
}
return retVal;
}
/*============================================================================*/
float qFFMath_RSqrt( float x )
{
float retVal;
if ( x < 0.0F ) {
retVal = QFFM_NAN;
}
else if ( QFFM_FP_ZERO == qFFMath_FPClassify( x ) ) {
retVal = QFFM_INFINITY;
}
else {
uint32_t y = 0U;
float z = 0.5F*x;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( y, x, uint32_t );
y = 0x5F375A86U - ( y >> 1U );
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( x, y, float );
retVal = x*( 1.5F - ( z*x*x ) );
}
return retVal;
}
/*============================================================================*/
static float qFFMath_CalcCbrt( float x , bool r )
{
float retVal, y = 0.0F, c, d;
const float k[ 3 ] = { 1.752319676F, 1.2509524245F, 0.5093818292F };
uint32_t i = 0U;
bool neg = false;
if ( x < 0.0F ) {
x = -x;
neg = true;
}
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( i, x, uint32_t );
i = 0x548C2B4BU - ( i/3U );
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( y, i, float );
c = x*y*y*y;
y = y*( k[ 0 ] - ( c*( k[ 1 ] - ( k[ 2 ]*c ) ) ) );
d = x*y*y;
c = 1.0F - ( d*y );
retVal = 1.0F + ( 0.333333333333F*c );
retVal *= ( r ) ? y : d;
return ( neg )? -retVal : retVal;
}
/*============================================================================*/
float qFFMath_Cbrt( float x )
{
return qFFMath_CalcCbrt( x, false );
}
/*============================================================================*/
float qFFMath_RCbrt( float x )
{
return ( QFFM_FP_ZERO == qFFMath_FPClassify( x ) ) ? QFFM_INFINITY
: qFFMath_CalcCbrt( x, true );
}
/*============================================================================*/
float qFFMath_Round( float x )
{
int32_t i0 = 0, j0;
float ret = x;
cast_reinterpret( i0, x, int32_t );
/*cstat -MISRAC2012-Rule-10.1_R7 -MISRAC2012-Rule-10.3 */
/*cstat -MISRAC2012-Rule-10.1_R6 -ATH-shift-neg -CERT-INT34-C_c */
/*cstat -MISRAC2012-Rule-1.3_n -MISRAC2012-Rule-10.4_a*/
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
if ( j0 < 23 ) {
if ( j0 < 0 ) {
i0 &= (int32_t)0x80000000U;
if ( -1 == j0 ) {
i0 |= 0x3F800000;
}
cast_reinterpret( ret, i0, float );
}
else {
const int32_t i = 0x007FFFFF >> j0;
if ( 0 != ( i0 & i ) ) {
i0 += 0x00400000 >> j0;
i0 &= ~i;
cast_reinterpret( ret, i0, float );
}
}
}
/*cstat +MISRAC2012-Rule-10.1_R7 +MISRAC2012-Rule-10.3 */
/*cstat +MISRAC2012-Rule-10.1_R6 +ATH-shift-neg +CERT-INT34-C_c */
/*cstat +MISRAC2012-Rule-1.3_n +MISRAC2012-Rule-10.4_a */
return ret;
}
/*============================================================================*/
float qFFMath_Floor( float x )
{
int32_t i0 = 0, j0;
float ret = x;
cast_reinterpret( i0, x, int32_t );
/*cstat -MISRAC2012-Rule-10.1_R7 -MISRAC2012-Rule-10.3 */
/*cstat -MISRAC2012-Rule-10.1_R6 -ATH-shift-neg -CERT-INT34-C_c */
/*cstat -MISRAC2012-Rule-1.3_n -MISRAC2012-Rule-10.4_a*/
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
if ( j0 < 23 ) {
if ( j0 < 0 ) {
/*cstat -ATH-neg-check-nonneg*/
if ( i0 >= 0 ) {
i0 = 0;
}
else if ( 0 != ( i0 & 0x7FFFFFFF ) ) {
i0 = (int32_t)0xBF800000U;
}
else {
/*nothing to do here*/
}
/*cstat +ATH-neg-check-nonneg*/
cast_reinterpret( ret, i0, float);
}
else {
const int32_t i = ( 0x007FFFFF ) >> j0;
if ( 0 != ( i0 & i ) ) {
/*cstat -ATH-neg-check-nonneg*/
if ( i0 < 0 ) {
i0 += 0x00800000 >> j0;
}
/*cstat +ATH-neg-check-nonneg*/
i0 &= (~i);
cast_reinterpret( ret, i0, float);
}
}
}
/*cstat +MISRAC2012-Rule-10.1_R7 +MISRAC2012-Rule-10.3 */
/*cstat +MISRAC2012-Rule-10.1_R6 +ATH-shift-neg +CERT-INT34-C_c */
/*cstat +MISRAC2012-Rule-1.3_n +MISRAC2012-Rule-10.4_a */
return ret;
}
/*============================================================================*/
float qFFMath_Ceil( float x )
{
int32_t i0 = 0, j0;
float ret = x;
cast_reinterpret( i0, x, int32_t );
/*cstat -MISRAC2012-Rule-10.1_R7 -MISRAC2012-Rule-10.3 */
/*cstat -MISRAC2012-Rule-10.1_R6 -ATH-shift-neg -CERT-INT34-C_c */
/*cstat -MISRAC2012-Rule-1.3_n -MISRAC2012-Rule-10.4_a*/
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
if ( j0 < 23 ) {
if ( j0 < 0 ) {
/*cstat -ATH-neg-check-nonneg*/
if ( i0 < 0 ) {
i0 = (int32_t)0x80000000U;
}
else if ( 0 != i0 ) {
i0 = (int32_t)0x3F800000U;
}
else {
/*nothing to do here*/
}
/*cstat +ATH-neg-check-nonneg*/
cast_reinterpret( ret, i0, float);
}
else {
const int32_t i = ( 0x007FFFFF ) >> j0;
if ( 0 != ( i0 & i ) ) {
if ( i0 > 0 ) {
i0 += 0x00800000 >> j0;
}
i0 &= (~i);
cast_reinterpret( ret, i0, float);
}
}
}
/*cstat +MISRAC2012-Rule-10.1_R7 +MISRAC2012-Rule-10.3 */
/*cstat +MISRAC2012-Rule-10.1_R6 +ATH-shift-neg +CERT-INT34-C_c */
/*cstat +MISRAC2012-Rule-1.3_n +MISRAC2012-Rule-10.4_a */
return ret;
}
/*============================================================================*/
float qFFMath_Trunc( float x )
{
int32_t i0 = 0, sx, j0;
float ret = x;
cast_reinterpret( i0, x, int32_t );
/*cstat -MISRAC2012-Rule-10.1_R7 -MISRAC2012-Rule-10.3 */
/*cstat -MISRAC2012-Rule-10.1_R6 -ATH-shift-neg -CERT-INT34-C_c */
/*cstat -MISRAC2012-Rule-1.3_n -MISRAC2012-Rule-10.4_a*/
sx = i0 & (int32_t)0x80000000U;
j0 = ( ( i0 >> 23 ) & 0xFF ) - 0x7F;
if ( j0 < 23 ) {
const int32_t tmp = ( j0 < 0 ) ? sx : ( sx | ( i0 & ~( 0x007FFFFF >> j0 ) ) );
cast_reinterpret( ret, tmp, float );
}
/*cstat +MISRAC2012-Rule-10.1_R7 +MISRAC2012-Rule-10.3 */
/*cstat +MISRAC2012-Rule-10.1_R6 +ATH-shift-neg +CERT-INT34-C_c */
/*cstat +MISRAC2012-Rule-1.3_n +MISRAC2012-Rule-10.4_a */
return ret;
}
/*============================================================================*/
float qFFMath_Frac( float x )
{
return x - qFFMath_Trunc( x );
}
/*============================================================================*/
float qFFMath_Remainder( float x,
float y )
{
return x - ( y*qFFMath_Floor( x/y ) );
}
/*============================================================================*/
float qFFMath_Mod( float x,
float y )
{
return ( QFFM_FP_ZERO == qFFMath_FPClassify( x ) ) ? QFFM_NAN
: ( x - ( y*qFFMath_Trunc( x/y ) ) );
}
/*============================================================================*/
float qFFMath_Sin( float x )
{
float y;
if ( qFFMath_Abs( x ) <= 0.0066F ) {
y = x;
}
else {
x *= -QFFM_1_PI;
y = x + 25165824.0F;
x -= y - 25165824.0F;
x *= qFFMath_Abs( x ) - 1.0F;
y = x*( ( 3.5841304553896F*qFFMath_Abs( x ) ) + 3.1039673861526F );
}
return y;
}
/*============================================================================*/
float qFFMath_Cos( float x )
{
float y;
const float abs_x = qFFMath_Abs( x );
if ( qFFMath_IsEqual( abs_x, QFFM_PI_2 ) ) {
y = 1.0e-12F;
}
else {
y = qFFMath_Sin( x + QFFM_PI_2 );
}
return y;
}
/*============================================================================*/
float qFFMath_Tan( float x )
{
return qFFMath_Sin( x )/qFFMath_Cos( x );
}
/*============================================================================*/
float qFFMath_ASin( float x )
{
x = qFFMath_Sqrt( 1.0F + x ) - qFFMath_Sqrt( 1.0F - x );
return x*( ( 0.131754508171F*qFFMath_Abs( x ) ) + 0.924391722181F );
}
/*============================================================================*/
float qFFMath_ACos( float x )
{
return QFFM_PI_2 - qFFMath_ASin( x );
}
/*============================================================================*/
float qFFMath_ATan( float x )
{
float abs_x;
x /= qFFMath_Abs( x ) + 1.0F;
abs_x = qFFMath_Abs( x );
return x*( ( abs_x*( ( -1.45667498914F*abs_x ) + 2.18501248371F ) ) + 0.842458832225F );
}
/*============================================================================*/
float qFFMath_ATan2( float y, float x )
{
float t, f;
t = QFFM_PI - ( ( y < 0.0F ) ? 6.283185307F : 0.0F );
f = ( qFFMath_Abs( x ) <= FLT_MIN ) ? 1.0F : 0.0F;
y = qFFMath_ATan( y/( x + f ) ) + ( ( x < 0.0F ) ? t : 0.0F );
return y + ( f*( ( 0.5F*t ) - y ) );
}
/*============================================================================*/
float qFFMath_Exp2( float x )
{
float retVal;
if ( x <= -126.0F ) {
retVal = 0.0F;
}
else if ( x > 128.0F ) {
retVal = QFFM_INFINITY;
}
else {
float ip, fp;
float ep_f = 0.0F;
int32_t ep_i;
ip = qFFMath_Floor( x + 0.5F );
fp = x - ip;
/*cstat -MISRAC2012-Rule-10.1_R6 -CERT-FLP34-C*/
ep_i = ( (int32_t)( ip ) + 127 ) << 23;
/*cstat +MISRAC2012-Rule-10.1_R6 +CERT-FLP34-C*/
x = 1.535336188319500e-4F;
x = ( x*fp ) + 1.339887440266574e-3F;
x = ( x*fp ) + 9.618437357674640e-3F;
x = ( x*fp ) + 5.550332471162809e-2F;
x = ( x*fp ) + 2.402264791363012e-1F;
x = ( x*fp ) + 6.931472028550421e-1F;
x = ( x*fp ) + 1.0F;
cast_reinterpret( ep_f, ep_i, float );
retVal = ep_f*x;
}
return retVal;
}
/*============================================================================*/
float qFFMath_Log2( float x )
{
float retVal;
if ( x < 0.0F ) {
retVal = QFFM_NAN;
}
else if ( QFFM_FP_ZERO == qFFMath_FPClassify( x ) ) {
retVal = -QFFM_INFINITY;
}
else {
float z, px;
int32_t ip, fp;
int32_t val_i = 0;
cast_reinterpret( val_i, x, int32_t );
/*cstat -MISRAC2012-Rule-10.1_R6*/
fp = val_i & 8388607;
ip = val_i & 2139095040;
fp |= 1065353216;
cast_reinterpret( x, fp, float );
ip >>= 23;
ip -= 127;
/*cstat +MISRAC2012-Rule-10.1_R6*/
if ( x > QFFM_SQRT2 ) {
x *= 0.5F;
++ip;
}
x -= 1.0F;
px = 7.0376836292e-2F;
px = ( px*x ) - 1.1514610310e-1F;
px = ( px*x ) + 1.1676998740e-1F;
px = ( px*x ) - 1.2420140846e-1F;
px = ( px*x ) + 1.4249322787e-1F;
px = ( px*x ) - 1.6668057665e-1F;
px = ( px*x ) + 2.0000714765e-1F;
px = ( px*x ) - 2.4999993993e-1F;
px = ( px*x ) + 3.3333331174e-1F;
z = x*x;
z = ( x*z*px ) - ( 0.5F*z ) + x;
z *= QFFM_LOG2E;
/*cstat -CERT-FLP36-C*/
retVal = ( (float)ip ) + z;
/*cstat +CERT-FLP36-C*/
}
return retVal;
}
/*============================================================================*/
float qFFMath_Exp( float x )
{
return qFFMath_Exp2( QFFM_LOG2E*x );
}
/*============================================================================*/
float qFFMath_Exp10( float x )
{
return qFFMath_Exp2( 3.32192809F*x );
}
/*============================================================================*/
float qFFMath_Log( float x )
{
return QFFM_LN2*qFFMath_Log2(x);
}
/*============================================================================*/
float qFFMath_Log10( float x )
{
return 0.301029996F*qFFMath_Log2(x);
}
/*============================================================================*/
float qFFMath_Pow( float b,
float e )
{
return qFFMath_Exp2( e*qFFMath_Log2( b ) );
}
/*============================================================================*/
float qFFMath_Sinh( float x )
{
const float epx = qFFMath_Exp( x );
const float enx = 1.0F/epx;
return 0.5F*( epx - enx );
}
/*============================================================================*/
float qFFMath_Cosh( float x )
{
const float epx = qFFMath_Exp( x );
const float enx = 1.0F/epx;
return 0.5F*( epx + enx );
}
/*============================================================================*/
float qFFMath_Tanh( float x )
{
x = qFFMath_Exp( -2.0F*x );
return ( 1.0F - x )/( 1.0F + x );
}
/*============================================================================*/
float qFFMath_ASinh( float x )
{
return qFFMath_Log( x + qFFMath_Sqrt( ( x*x ) + 1.0F ) );
}
/*============================================================================*/
float qFFMath_ACosh( float x )
{
return ( x < 1.0F ) ? QFFM_NAN
: qFFMath_Log( x + qFFMath_Sqrt( ( x*x ) - 1.0F ) );
}
/*============================================================================*/
float qFFMath_ATanh( float x )
{
return qFFMath_Log( ( 1.0F + x )/( 1.0F - x ) )*0.5F;
}
/*============================================================================*/
float qFFMath_WrapToPi( float x )
{
return qFFMath_Mod( x + QFFM_PI, QFFM_2PI ) - QFFM_PI;
}
/*============================================================================*/
float qFFMath_WrapTo2Pi( float x )
{
return qFFMath_Mod( x, QFFM_2PI );
}
/*============================================================================*/
float qFFMath_WrapTo180( float x )
{
return qFFMath_Mod( x + 180.0F, 360.0F ) - 180.0F;
}
/*============================================================================*/
float qFFMath_WrapTo360( float x )
{
return qFFMath_Mod( x, 360.0F );
}
/*============================================================================*/
float qFFMath_Erf( float x )
{
float retVal;
if ( x >= 6.912F ) {
retVal = 1.0F;
}
else {
x = qFFMath_Exp( 3.472034176F*x );
retVal = ( x/( ( qFFMath_Abs( x ) + 1.0F )*2.0F ) ) - 1.0F;
}
return retVal;
}
/*============================================================================*/
float qFFMath_Erfc( float x )
{
return 1.0F - qFFMath_Erf( x );
}
/*============================================================================*/
float qFFMath_Max( float x,
float y )
{
return ( x > y ) ? x : y;
}
/*============================================================================*/
float qFFMath_Min( float x,
float y )
{
return ( x < y ) ? x : y;
}
/*============================================================================*/
float qFFMath_RExp( float x,
int32_t *pw2 )
{
uint32_t lu = 0U, iu;
int32_t i = 0;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( lu, x, uint32_t );
iu = ( lu >> 23U ) & 0x000000FFU; /* Find the exponent (power of 2) */
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( i, iu, int32_t );
i -= 0x7E;
pw2[ 0 ] = (int)i;
lu &= 0x807FFFFFU; /* strip all exponent bits */
lu |= 0x3F000000U; /* mantissa between 0.5 and 1 */
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( x, lu, float );
return x;
}
/*============================================================================*/
float qFFMath_LDExp( float x,
int32_t pw2 )
{
uint32_t lu = 0U, eu;
int32_t e = 0;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( lu, x, uint32_t );
eu = ( lu >> 23U ) & 0x000000FFU;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( e, eu, int32_t );
e += pw2;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( eu, e, uint32_t );
lu = ( ( eu & 0xFFU ) << 23U ) | ( lu & 0x807FFFFFU );
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( x, lu, float );
return x;
}
/*============================================================================*/
float qFFMath_Hypot( float x,
float y )
{
float retVal;
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsFinite( x ) && qFFMath_IsFinite( y ) ) {
float a, b, an, bn;;
int32_t e = 0;
if ( x >= y ) {
a = x;
b = y;
}
else {
a = y;
b = x;
}
/* Write a = an * 2^e, b = bn * 2^e with 0 <= bn <= an < 1.*/
an = qFFMath_RExp( a, &e );
bn = qFFMath_LDExp( b, -e );
retVal = qFFMath_Sqrt( ( an*an ) + ( bn*bn ) );
retVal = qFFMath_LDExp( retVal, e );
}
else {
retVal = ( qFFMath_IsInf( x ) || qFFMath_IsInf( y ) ) ? QFFM_INFINITY
: QFFM_NAN;
}
/*cstat +MISRAC2012-Rule-13.5*/
return retVal;
}
/*============================================================================*/
float qFFMath_NextAfter( float x,
float y )
{
float retVal;
uint32_t ax, ay, uxi = 0U, uyi = 0U;
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( uxi, x, uint32_t );
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( uyi, y, uint32_t );
/*cstat -MISRAC2012-Rule-13.5*/
if ( qFFMath_IsNaN( x ) || qFFMath_IsNaN( y ) ) {
/*cstat +MISRAC2012-Rule-13.5*/
retVal = QFFM_NAN;
}
else if ( uxi == uyi ) {
retVal = y;
}
else {
ax = uxi & 0x7FFFFFFFU;
ay = uyi & 0x7FFFFFFFU;
if ( 0U == ax ) {
uxi = ( 0U == ay ) ? uyi : ( ( uyi & 0x80000000U ) | 1U );
}
else if ( ( ax > ay ) || ( 0U != ( ( uxi^uyi ) & 0x80000000U ) ) ) {
uxi--;
}
else {
uxi++;
}
/*cppcheck-suppress misra-c2012-21.15 */
cast_reinterpret( retVal, uxi, float );
}
return retVal;
}
/*============================================================================*/
float qFFMath_Midpoint( float a,
float b )
{
float y;
const float lo = 2.0F*FLT_MIN;
const float hi = 0.5F*FLT_MAX;
const float abs_a = qFFMath_Abs( a );
const float abs_b = qFFMath_Abs( b );
if ( ( abs_a <= hi ) && ( abs_b <= hi ) ) {
y = 0.5F*( a + b );
}
else if ( abs_a < lo ) {
y = a + ( 0.5F*b );
}
else if ( abs_b < lo) {
y = ( 0.5F*a ) + b;
}
else {
y = ( 0.5F*a ) + ( 0.5F*b );
}
return y;
}
/*============================================================================*/
float qFFMath_Lerp( float a,
float b,
float t )
{
float y;
if ( ( ( a <= 0.0F ) && ( b >= 0.0F ) ) || ( ( a >= 0.0F ) && ( b <= 0.0F ) ) ) {
y = ( t*b ) + ( a*( 1.0F - t ) );
}
else if ( qFFMath_IsEqual( t, 1.0F ) ) {
y = b;
}
else {
const float x = a + ( t*( b - a ) );
y = ( ( t > 1.0F ) == ( b > a ) ) ? ( ( b < x ) ? x : b )
: ( ( b > x ) ? x : b );
}
return y;
}
/*============================================================================*/
float qFFMath_Normalize( const float x,
const float xMin,
const float xMax )
{
return ( x - xMin )/( xMax - xMin );
}
/*============================================================================*/
float qFFMath_Map( const float x,
const float xMin,
const float xMax,
const float yMin,
const float yMax )
{
return ( ( yMax - yMin )*qFFMath_Normalize( x, xMin, xMax ) ) + yMin;
}
/*============================================================================*/
bool qFFMath_InRangeCoerce( float * const x,
const float lowerL,
const float upperL )
{
bool retVal = false;
if ( qFFMath_IsNaN( x[ 0 ] ) ) {
x[ 0 ] = lowerL;
}
else {
if ( x[ 0 ] < lowerL ) {
x[ 0 ] = lowerL;
}
else if ( x[ 0 ] > upperL ) {
x[ 0 ] = upperL;
}
else {
retVal = true;
}
}
return retVal;
}
/*============================================================================*/
bool qFFMath_InPolygon( const float x,
const float y,
const float * const px,
const float * const py,
const size_t p )
{
size_t i;
bool retVal = false;
float max_y = py[ 0 ], max_x = px[ 0 ], min_y = py[ 0 ], min_x = px[ 0 ];
for ( i = 0U ; i < p ; ++i ) {
max_y = ( py[ i ] > max_y ) ? py[ i ] : max_y;
max_x = ( px[ i ] > max_x ) ? px[ i ] : max_x;
min_y = ( py[ i ] < min_y ) ? py[ i ] : min_y;
min_x = ( px[ i ] < min_x ) ? px[ i ] : min_x;
}
if ( ( y >= min_y ) && ( y <= max_y ) && ( x >= min_x ) && ( x <= max_x ) ) {
size_t j = p - 1U;
for ( i = 0U ; i < p ; ++i ) {
if ( ( px[ i ] > x ) != ( px[ j ] > x ) ) {
const float dx = px[ j ] - px[ i ];
const float dy = py[ j ] - py[ i ];
if ( y < ( ( dy*( x - px[ i ] ) )/( dx + py[ i ] ) ) ) {
retVal = !retVal;
}
}
j = i;
}
}
return retVal;
}
/*============================================================================*/
bool qFFMath_InCircle( const float x,
const float y,
const float cx,
const float cy,
const float r )
{
const float d = ( ( x - cx )*( x - cx ) ) + ( ( y - cy )*( y - cy ) );
return ( d <= ( r*r ) );
}
/*============================================================================*/
float qFFMath_TGamma( float x )
{
float result;
const int fClass = qFFMath_FPClassify( x );
if ( QFFM_FP_NAN == fClass ) {
result = QFFM_NAN;
}
else if ( QFFM_FP_ZERO == fClass ) {
result = QFFM_INFINITY; /* a huge value */
}
else if ( QFFM_FP_INFINITE == fClass ) {
result = ( x > 0.0F ) ? QFFM_INFINITY : QFFM_NAN;
}
else {
bool parity = false;
float fact = 1.0F;
float y = x;
float y1;
if ( y <= 0.0F ) {
float isItAnInt;
y = -x;
y1 = qFFMath_Trunc( y );
isItAnInt = y - y1;
if ( !qFFMath_IsEqual( 0.0F, isItAnInt ) ) {
const float tmp = 2.0F*qFFMath_Trunc( y1*0.5F );
if ( !qFFMath_IsEqual( y1, tmp ) ) {
parity = true;
}
fact = -QFFM_PI/qFFMath_Sin( QFFM_PI*isItAnInt );
y += 1.0F;
}
else {
result = QFFM_NAN;
goto EXIT_TGAMMA;
}
}
if ( y < FLT_EPSILON ) {
if ( y >= FLT_MIN ) {
result = 1.0F/y;
}
else {
result = QFFM_INFINITY;
}
}
else if ( y < 12.0F ) {
float num = 0.0F, den = 1.0F, z;
int n = 0;
y1 = y;
if ( y < 1.0F ) {
z = y;
y += 1.0F;
}
else {
n = (int)y - 1;
/*cstat -CERT-FLP36-C */
y -= (float)n;
/*cstat +CERT-FLP36-C */
z = y - 1.0F;
}
num = z*( num + -1.71618513886549492533811e+0F );
den = ( den*z ) -3.08402300119738975254353e+1F;
num = z*( num + 2.47656508055759199108314e+1F );
den = ( den*z ) + 3.15350626979604161529144e+2F;
num = z*( num - 3.79804256470945635097577e+2F );
den = ( den*z ) - 1.01515636749021914166146e+3F;
num = z*( num + 6.29331155312818442661052e+2F );
den = ( den*z ) - 3.10777167157231109440444e+3F;
num = z*( num + 8.66966202790413211295064e+2F );
den = ( den*z ) + 2.25381184209801510330112e+4F;
num = z*( num - 3.14512729688483675254357e+4F );
den = ( den*z ) + 4.75584627752788110767815e+3F;
num = z*( num - 3.61444134186911729807069e+4F );
den = ( den*z ) - 1.34659959864969306392456e+5F;
num = z*( num + 6.64561438202405440627855e+4F );
den = ( den*z ) - 1.15132259675553483497211e+5F;
result = ( num/den ) + 1.0F;
if ( y1 < y ) {