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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/mathematics.h"
#if defined(GATE_SYS_EFI)
//# define GATE_CORE_MATH_EFI_IMPL 1
# define GATE_CORE_MATH_STD_IMPL 1
#else
# define GATE_CORE_MATH_STD_IMPL 1
#endif
/*****************************
* Generic implementations *
*****************************/
#include <stdarg.h>
#include <float.h>
void gate_decimal_init(gate_decimal_t* deci, gate_int32_t int_num, gate_uint32_t micro_num)
{
deci->value = (gate_decimal_primitive_t)(int_num * GATE_DECIMAL_FACTOR);
deci->value += (gate_decimal_primitive_t)(micro_num % GATE_DECIMAL_FACTOR);
}
gate_int32_t gate_decimal_get_int(gate_decimal_t const* deci)
{
return (gate_int32_t)(deci->value / GATE_DECIMAL_FACTOR);
}
gate_uint32_t gate_decimal_get_micro(gate_decimal_t const* deci)
{
if (deci->value < 0)
{
return (gate_uint32_t)((-deci->value) % GATE_DECIMAL_FACTOR);
}
else
{
return (gate_uint32_t)(deci->value % GATE_DECIMAL_FACTOR);
}
}
gate_real32_t gate_decimal_get_float(gate_decimal_t const* micro)
{
return (gate_real32_t)micro->value / (gate_real32_t)GATE_DECIMAL_FACTOR;
}
int gate_decimal_compare(gate_decimal_t const* a, gate_decimal_t const* b)
{
if (a->value < b->value)
{
return -1;
}
else if (a->value > b->value)
{
return 1;
}
return 0;
}
void gate_decimal_add_int(gate_decimal_t* deci, gate_int32_t value)
{
deci->value += (gate_decimal_primitive_t)value * GATE_DECIMAL_FACTOR;
}
void gate_decimal_sub_int(gate_decimal_t* deci, gate_int32_t value)
{
deci->value -= (gate_decimal_primitive_t)value * GATE_DECIMAL_FACTOR;
}
void gate_decimal_mul_int(gate_decimal_t* deci, gate_int32_t value)
{
deci->value *= (gate_decimal_primitive_t)value;
}
void gate_decimal_div_int(gate_decimal_t* deci, gate_int32_t value)
{
deci->value /= (gate_decimal_primitive_t)value;
}
void gate_decimal_add_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
{
deci->value += ptr_value->value;
}
void gate_decimal_sub_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
{
deci->value -= ptr_value->value;
}
void gate_decimal_mul_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
{
deci->value *= ptr_value->value;
deci->value /= (gate_decimal_primitive_t)GATE_DECIMAL_FACTOR;
}
void gate_decimal_div_dec(gate_decimal_t* deci, gate_decimal_t const* ptr_value)
{
gate_int64_t r = deci->value % ptr_value->value;
deci->value /= ptr_value->value;
deci->value *= (gate_decimal_primitive_t)GATE_DECIMAL_FACTOR;
deci->value += (gate_decimal_primitive_t)(r * GATE_DECIMAL_FACTOR / ptr_value->value);
}
void gate_decimal_add_flt(gate_decimal_t* deci, gate_real32_t value)
{
deci->value += (gate_decimal_primitive_t)(value * (gate_real32_t)GATE_DECIMAL_FACTOR);
}
void gate_decimal_sub_flt(gate_decimal_t* deci, gate_real32_t value)
{
deci->value -= (gate_decimal_primitive_t)(value * (gate_real32_t)GATE_DECIMAL_FACTOR);
}
void gate_decimal_mul_flt(gate_decimal_t* deci, gate_real32_t value)
{
deci->value = (gate_decimal_primitive_t)((gate_real32_t)deci->value * value);
}
void gate_decimal_div_flt(gate_decimal_t* deci, gate_real32_t value)
{
deci->value = (gate_decimal_primitive_t)((gate_real32_t)deci->value / value);
}
void gate_decimal_sqrt(gate_decimal_t* deci)
{
static gate_decimal_t n2 = GATE_DECIMAL_INIT(2, 0);
gate_decimal_t a = GATE_DECIMAL_INIT(1255, 0);
gate_decimal_t b;
unsigned n;
if (gate_decimal_compare(deci, &n2) < 0)
{
return;
}
for (n = 0; n < 8; ++n)
{
b.value = (deci->value / a.value) * GATE_DECIMAL_FACTOR;
a.value = (a.value + b.value) / 2;
}
deci->value = a.value;
}
gate_bool_t gate_math_iszero(gate_real64_t num)
{
if (num < 0.0)
{
return (-num < DBL_EPSILON) ? true : false;
}
else
{
return (num < DBL_EPSILON) ? true : false;
}
}
gate_bool_t gate_math_iszerof(gate_real32_t num)
{
if (num < 0.0f)
{
return (-num < FLT_EPSILON) ? true : false;
}
else
{
return (num < FLT_EPSILON) ? true : false;
}
}
gate_bool_t gate_math_isnan(gate_real64_t num)
{
volatile gate_real64_t tmp = num;
return (tmp != num) ? true : false;
}
gate_bool_t gate_math_isnanf(gate_real32_t num)
{
volatile gate_real32_t tmp = num;
return (tmp != num) ? true : false;
}
int gate_math_isinfinite(gate_real64_t num)
{
#if defined(DBL_MAX)
if (num > DBL_MAX)<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
{
return 1;
}
else if (num < -DBL_MAX)<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
{
return -1;
}
return 0;
#else
static gate_real64_t const gate_math_infinite_pos = 1.0 / 0.0;
static gate_real64_t const gate_math_infinite_neg = -1.0 / 0.0;<--- Syntax Error: AST broken, binary operator '=' doesn't have two operands.
if (num == gate_math_infinite_pos)
{
return 1;
}
else if (num == gate_math_infinite_neg)
{
return -1;
}
return 0;
#endif
}
int gate_math_isinfinitef(gate_real32_t num)
{
#if defined(FLT_MAX)
if (num > FLT_MAX)<--- Skipping configuration 'FLT_MAX' since the value of 'FLT_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
{
return 1;
}
else if (num < -FLT_MAX)<--- Skipping configuration 'FLT_MAX' since the value of 'FLT_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
{
return -1;
}
return 0;
#else
static gate_real32_t const gate_math_infinite_pos = 1.0f / 0.0f;
static gate_real32_t const gate_math_infinite_neg = -1.0f / 0.0f;
if (num == gate_math_infinite_pos)
{
return 1;
}
else if (num == gate_math_infinite_neg)
{
return -1;
}
return 0;
#endif
}
int gate_math_signum_i8(gate_int8_t num)
{
return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
}
int gate_math_signum_i16(gate_int16_t num)
{
return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
}
int gate_math_signum_i32(gate_int32_t num)
{
return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
}
int gate_math_signum_i64(gate_int64_t num)
{
return (num < 0) ? -1 : ((num == 0) ? 0 : 1);
}
int gate_math_signum_r32(gate_real32_t num)
{
return gate_math_iszerof(num) ? 0 : ((num < 0.0f) ? -1 : 1);
}
int gate_math_signum_r64(gate_real64_t num)
{
return gate_math_iszero(num) ? 0 : ((num < 0.0) ? -1 : 1);
}
gate_int8_t gate_math_abs_i8(gate_int8_t num)
{
return num < 0 ? -num : num;
}
gate_int16_t gate_math_abs_i16(gate_int16_t num)
{
return num < 0 ? -num : num;
}
gate_int32_t gate_math_abs_i32(gate_int32_t num)
{
return num < 0 ? -num : num;
}
gate_int64_t gate_math_abs_i64(gate_int64_t num)
{
return num < 0 ? -num : num;
}
gate_real32_t gate_math_abs_r32(gate_real32_t num)
{
return num < 0.0f ? -num : num;
}
gate_real64_t gate_math_abs_r64(gate_real64_t num)
{
return num < 0.0 ? -num : num;
}
#define gate_math_min_type(type, va_type, count) \
type ret = 0; \
type tmp; \
va_list vl; \
if(count != 0) \
{ \
va_start(vl, count); \
ret = (type)va_arg(vl, va_type); \
while(--count) \
{ \
tmp = (type)va_arg(vl, va_type); \
if(tmp < ret) \
{ \
ret = tmp; \
} \
} \
va_end(vl); \
} \
return ret
#define gate_math_max_type(type, va_type, count) \
type ret = 0; \
type tmp; \
va_list vl; \
if(count != 0) \
{ \
va_start(vl, count); \
ret = (type)va_arg(vl, va_type); \
while(--count) \
{ \
tmp = (type)va_arg(vl, va_type); \
if(tmp > ret) \
{ \
ret = tmp; \
} \
} \
va_end(vl); \
} \
return ret
gate_int16_t gate_math_min_i16(gate_size_t count, ...)
{
gate_math_min_type(gate_int16_t, int, count);
}
gate_int32_t gate_math_min_i32(gate_size_t count, ...)
{
gate_math_min_type(gate_int32_t, gate_int32_t, count);
}
gate_int64_t gate_math_min_i64(gate_size_t count, ...)
{
gate_math_min_type(gate_int64_t, gate_int64_t, count);
}
gate_real32_t gate_math_min_r32(gate_size_t count, ...)
{
gate_math_min_type(gate_real32_t, gate_real64_t, count);
}
gate_real64_t gate_math_min_r64(gate_size_t count, ...)
{
gate_math_min_type(gate_real64_t, gate_real64_t, count);
}
gate_int16_t gate_math_max_i16(gate_size_t count, ...)
{
gate_math_max_type(gate_int16_t, int, count);
}
gate_int32_t gate_math_max_i32(gate_size_t count, ...)
{
gate_math_max_type(gate_int32_t, gate_int32_t, count);
}
gate_int64_t gate_math_max_i64(gate_size_t count, ...)
{
gate_math_max_type(gate_int64_t, gate_int64_t, count);
}
gate_real32_t gate_math_max_r32(gate_size_t count, ...)
{
gate_math_max_type(gate_real32_t, gate_real64_t, count);
}
gate_real64_t gate_math_max_r64(gate_size_t count, ...)
{
gate_math_max_type(gate_real64_t, gate_real64_t, count);
}
gate_uint8_t gate_math_decimal_length(gate_real64_t value)
{
gate_uint8_t len = 0;
gate_real64_t factor = 1.0;
gate_real64_t test_value;
gate_real64_t diff;
if (value < 0.0)
{
value = -value;
}
while (len < 20)
{
test_value = value * factor;
diff = (test_value - ((gate_real64_t)(gate_int64_t)test_value));
if (gate_math_iszero(diff))
{
break;
}
factor *= 10.0;
++len;
}
return len;
}
gate_real64_t gate_math_deg2rad(gate_real64_t value)
{
return value * GATE_MATH_CONST_PI / 180.0;
}
gate_real64_t gate_math_rad2deg(gate_real64_t value)
{
return value * 180.0 / GATE_MATH_CONST_PI;
}
gate_int64_t gate_math_pow_i64(gate_int64_t base, gate_uint16_t exp)
{
gate_uint8_t n;
gate_int64_t ret = 1;
for (n = 0; n != exp; ++n)
{
ret *= base;
}
return ret;
}
gate_real64_t gate_math_pow_iexp(gate_real64_t base, gate_uint16_t exp)
{
gate_uint16_t n;
gate_real64_t ret = 1.0;
for (n = 0; n != exp; ++n)
{
ret *= base;
}
return ret;
}
gate_real64_t gate_math_acosh(gate_real64_t value)
{
return gate_math_log(value + gate_math_sqrt(value * value - 1.0));
}
gate_real64_t gate_math_asinh(gate_real64_t value)
{
return gate_math_log(value + gate_math_sqrt(value * value + 1.0));
}
gate_real64_t gate_math_atanh(gate_real64_t value)
{
return gate_math_log((1.0 + value) / (1.0 - value)) / 2.0;
}
#if defined(GATE_CORE_MATH_STD_IMPL)
#include <math.h>
gate_real64_t gate_math_cos(gate_real64_t value)
{
return cos(value);
}
gate_real64_t gate_math_sin(gate_real64_t value)
{
return sin(value);
}
gate_real64_t gate_math_tan(gate_real64_t value)
{
return tan(value);
}
gate_real64_t gate_math_acos(gate_real64_t value)
{
return acos(value);
}
gate_real64_t gate_math_asin(gate_real64_t value)
{
return asin(value);
}
gate_real64_t gate_math_atan(gate_real64_t value)
{
return atan(value);
}
gate_real64_t gate_math_atan2(gate_real64_t y, gate_real64_t x)
{
return atan2(y, x);
}
gate_real64_t gate_math_cosh(gate_real64_t value)
{
return cosh(value);
}
gate_real64_t gate_math_sinh(gate_real64_t value)
{
return sinh(value);
}
gate_real64_t gate_math_tanh(gate_real64_t value)
{
return tanh(value);
}
gate_real64_t gate_math_exp(gate_real64_t value)
{
return exp(value);
}
gate_real64_t gate_math_frexp(gate_real64_t value, int* exp)
{
return frexp(value, exp);
}
gate_real64_t gate_math_ldexp(gate_real64_t value, int exp)
{
return ldexp(value, exp);
}
gate_real64_t gate_math_log(gate_real64_t value)
{
return log(value);
}
gate_real64_t gate_math_log10(gate_real64_t value)
{
return log10(value);
}
gate_real64_t gate_math_modf(gate_real64_t value, double* intpart)
{
return modf(value, intpart);
}
gate_real64_t gate_math_pow(gate_real64_t base, gate_real64_t exp)
{
return pow(base, exp);
}
gate_real64_t gate_math_sqrt(gate_real64_t value)
{
return sqrt(value);
}
gate_real64_t gate_math_ceil(gate_real64_t value)
{
return ceil(value);
}
gate_real64_t gate_math_floor(gate_real64_t value)
{
return floor(value);
}
#endif /* GATE_CORE_MATH_STD_IMPL */
#if defined(GATE_CORE_MATH_EFI_IMPL)
static gate_real64_t normalize_angle(gate_real64_t angle)
{
while (angle <= -GATE_MATH_CONST_PI)
{
angle += GATE_MATH_CONST_PI;
}
while (angle > GATE_MATH_CONST_PI)
{
angle -= GATE_MATH_CONST_PI;
}
return angle;
}
gate_real64_t gate_math_cos(gate_real64_t value)
{
static gate_real64_t const n2 = 2.0;
static gate_real64_t const n4 = 24.0;
static gate_real64_t const n6 = 720.0;
static gate_real64_t const n8 = 40320.0;
static gate_real64_t const n10 = 3628800.0;
static gate_real64_t const n12 = 479001600.0;
gate_real64_t const x = normalize_angle(value);
gate_real64_t const x2 = x * x;
gate_real64_t const x4 = x2 * x * x;
gate_real64_t const x6 = x4 * x * x;
gate_real64_t const x8 = x6 * x * x;
gate_real64_t const x10 = x8 * x * x;
gate_real64_t const x12 = x10 * x * x;
return 1 - x2 / n2 + x4 / n4 - x6 / n6 + x8 / n8 - x10 / n10 + x12 / n12;
}
gate_real64_t gate_math_sin(gate_real64_t value)
{
static gate_real64_t const n3 = 6.0;
static gate_real64_t const n5 = 120.0;
static gate_real64_t const n7 = 5040.0;
static gate_real64_t const n9 = 362880.0;
static gate_real64_t const n11 = 39916800.0;
static gate_real64_t const n13 = 6227020800.0;
gate_real64_t const x = normalize_angle(value);
gate_real64_t const x3 = x * x * x;
gate_real64_t const x5 = x3 * x * x;
gate_real64_t const x7 = x5 * x * x;
gate_real64_t const x9 = x7 * x * x;
gate_real64_t const x11 = x9 * x * x;
gate_real64_t const x13 = x11 * x * x;
return x - x3 / n3 + x5 / n5 - x7 / n7 + x9 / n9 - x11 / n11 + x13 / n13;
}
gate_real64_t gate_math_tan(gate_real64_t value)
{
return gate_math_sin(value) / gate_math_cos(value);
}
gate_real64_t gate_math_asin(gate_real64_t value)
{
/* https://de.wikipedia.org/wiki/Arkussinus_und_Arkuskosinus#Reihenentwicklungen */
static double const ret_0_96 = 1.2870022176;
/*static double const ret_1_00 = GATE_MATH_CONST_PI * 0.5;*/
static double const diff_1_00 = GATE_MATH_CONST_PI * 0.5 - 1.2870022176;
static unsigned const n_max = 107; /* quite OK up to 0.96 */
double factor;<--- The scope of the variable 'factor' can be reduced. [+]The scope of the variable 'factor' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned n;<--- The scope of the variable 'n' can be reduced. [+]The scope of the variable 'n' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
double x = value;
double ret = x;
double a = 1;<--- The scope of the variable 'a' can be reduced. [+]The scope of the variable 'a' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
double b = 1;<--- The scope of the variable 'b' can be reduced. [+]The scope of the variable 'b' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
if (value > 0.96)
{
/* iterative solution becomes unstable for value > 0.96
just apply a quick and dirty linear patch */
factor = (value - 0.96) / (1.00 - 0.96);
ret = ret_0_96 + diff_1_00 * factor;
}
else
{
for (n = 3; n != n_max; n += 2)
{
x *= value * value;
a *= ((double)n - 2.0);
b *= ((double)n - 1.0);
ret += a * x / (b * (double)n);
}
}
return ret;
}
gate_real64_t gate_math_acos(gate_real64_t value)
{
/* https://stackoverflow.com/questions/3380628/fast-arc-cos-algorithm/20914630 */
/* return (-0.69813170079773212 * value * value - 0.87266462599716477) * value + 1.5707963267948966; */
return GATE_MATH_CONST_PI * 0.5 - gate_math_asin(value);
}
gate_real64_t gate_math_atan(gate_real64_t value)
{
gate_real64_t ret;
ret = gate_math_asin(value / gate_math_sqrt(1 + value * value));
return ret;
}
gate_real64_t gate_math_atan2(gate_real64_t y, gate_real64_t x)
{
/* TODO */
return 0.0;
}
gate_real64_t gate_math_sinh(gate_real64_t value)
{
return 0.5 * (gate_math_exp(value) - gate_math_exp(-value));
}
gate_real64_t gate_math_cosh(gate_real64_t value)
{
return 0.5 * (gate_math_exp(value) + gate_math_exp(-value));
}
gate_real64_t gate_math_tanh(gate_real64_t value)
{
return gate_math_sinh(value) / gate_math_cosh(value);
}
gate_real64_t gate_math_exp(gate_real64_t value)
{
/* https://www.pseudorandom.com/implementing-exp */
gate_real64_t x0, tn;
gate_int64_t i, n;
if (gate_math_iszero(value))
{
return 1.0;
}
x0 = gate_math_abs_r64(value);
tn = 1.0;
n = 12;
n *= (gate_int64_t)gate_math_ceil(x0 * GATE_MATH_CONST_E);
for (i = n; i > 0; i--)
{
tn = tn * (x0 / i) + 1.0;
}
if (value < 0.0)
{
tn = 1.0 / tn;
}
return tn;
}
gate_real64_t gate_math_frexp(gate_real64_t value, int* exp)
{
/* TODO */
return 0.0;
}
gate_real64_t gate_math_ldexp(gate_real64_t value, int exp)
{
/* TODO */
return 0.0;
}
static unsigned msb(unsigned v)
{
unsigned ret = 0;
while (v >>= 1)
{
ret++;
}
return ret;
}
static float log_n(float value)
{
/* https://stackoverflow.com/questions/9799041/efficient-implementation-of-natural-logarithm-ln-and-exponentiation */
int log2;
float divisor, x, result;
log2 = msb((int)value); // See: https://stackoverflow.com/a/4970859/6630230
divisor = (float)(1 << log2);
x = value / divisor;
result = -1.7417939f + (2.8212026f + (-1.4699568f + (0.44717955f - 0.056570851f * x) * x) * x) * x;
result += ((float)log2) * 0.69314718f;
return result;
}
gate_real64_t gate_math_log(gate_real64_t value)
{
return (gate_real64_t)log_n((float)value);
}
gate_real64_t gate_math_log10(gate_real64_t value)
{
return gate_math_log(value) / gate_math_log(10.0);
}
gate_real64_t gate_math_modf(gate_real64_t value, gate_real64_t* intpart)
{
gate_real64_t ret;
gate_uint64_t ui;
if (value < 0.0)
{
ui = (gate_uint64_t)-value;
ret = -value - (gate_real64_t)ui;
if (intpart)
{
*intpart = -((gate_real64_t)ui);
}
}
else
{
ui = (gate_uint64_t)value;
ret = value - (gate_real64_t)ui;
if (intpart)
{
*intpart = (gate_real64_t)ui;
}
}
return ret;
}
gate_real64_t gate_math_pow(gate_real64_t base, gate_real64_t exp)
{
if (exp < 0.0)
{
return 1.0 / gate_math_pow_iexp(base, (gate_uint16_t)-exp);
}
else
{
return gate_math_pow_iexp(base, (gate_uint16_t)exp);
}
return 0.0;
}
union f_rsqrt_helper
{
float f;
gate_uint32_t i;
};
union d_rsqrt_helper
{
double d;
gate_uint64_t i;
};
/* https://en.wikipedia.org/wiki/Fast_inverse_square_root */
/* https://stackoverflow.com/questions/11644441/fast-inverse-square-root-on-x64/11644533 */
static float f_rsqrt(float number)
{
static const float threehalfs = 1.5F;
const float x2 = number * 0.5F;
union f_rsqrt_helper conv;
conv.f = number;
conv.i = 0x5f3759df - (conv.i >> 1);
conv.f *= threehalfs - (x2 * conv.f * conv.f);
return conv.f;
}
static double d_rsqrt(double number)
{
double y = number;
double x2 = y * 0.5;
union d_rsqrt_helper conv;
conv.d = y;
conv.i = 0x5fe6eb50c7b537a9 - (conv.i >> 1);
y = conv.d;
y = y * (1.5 - (x2 * y * y));
y = y * (1.5 - (x2 * y * y));
return y;
}
gate_real64_t gate_math_sqrt(gate_real64_t value)
{
double num = gate_math_abs_r64((double)value);
return (gate_real64_t)(1.0 / d_rsqrt(num));
}
gate_real64_t gate_math_ceil(gate_real64_t value)
{
gate_int64_t i = (gate_int64_t)value;
gate_real64_t diff = value - (gate_real64_t)i;
if (!gate_math_iszero(diff))
{
if (value >= 0.0)
{
i = (gate_int64_t)(value + 1.0);
}
else
{
i = -(gate_int64_t)(-value);
}
}
return (gate_real64_t)i;
}
gate_real64_t gate_math_floor(gate_real64_t value)
{
gate_int64_t i = (gate_int64_t)value;
gate_real64_t diff = value - (gate_real64_t)i;
if (!gate_math_iszero(diff))
{
if (value >= 0.0)
{
i = (gate_int64_t)(value);
}
else
{
i = -(gate_int64_t)(-value + 1.0);
}
}
return (gate_real64_t)i;
}
#endif /* GATE_CORE_MATH_EFI_IMPL */
|