GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 93.8% 45 / 0 / 48
Functions: 100.0% 10 / 0 / 10
Branches: 85.0% 17 / 0 / 20
Decisions: 70.0% 7 / - / 10

firmware/libfirmware/util/src/rusefi_math.cpp
Line Branch Decision Exec Source
1 #include <rusefi/rusefi_math.h>
2
3 #include <cstdint>
4
5 1221107 int maxI(int i1, int i2) {
6
2/2
✓ Branch 0 taken 1117129 times.
✓ Branch 1 taken 103978 times.
1221107 return i1 > i2 ? i1 : i2;
7 }
8
9 7859 int minI(int i1, int i2) {
10
2/2
✓ Branch 0 taken 570 times.
✓ Branch 1 taken 7289 times.
7859 return i1 < i2 ? i1 : i2;
11 }
12
13 850574 float maxF(float i1, float i2) {
14
2/2
✓ Branch 0 taken 155765 times.
✓ Branch 1 taken 694809 times.
850574 return i1 > i2 ? i1 : i2;
15 }
16
17 1381644 float minF(float i1, float i2) {
18
2/2
✓ Branch 0 taken 767301 times.
✓ Branch 1 taken 614343 times.
1381644 return i1 < i2 ? i1 : i2;
19 }
20
21 730566 float clampF(float min, float clamp, float max) {
22 730566 return maxF(min, minF(clamp, max));
23 }
24
25 487799 bool isSameF(float a, float b) {
26 487799 return absF(a - b) < 0.0001;
27 }
28
29 41 constexpr float expf_taylor_impl(float x, uint8_t n)
30 {
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 41 times.
41 if (x < -2)
32 {
33 return 0.818f;
34 }
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 41 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 41 times.
41 else if (x > 0)
36 {
37 return 1;
38 }
39
40 41 x = x + 1;
41
42 41 float x_power = x;
43 41 int fac = 1;
44 41 float sum = 1;
45
46
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 41 times.
2/2
✓ Decision 'true' taken 164 times.
✓ Decision 'false' taken 41 times.
205 for (int i = 1; i <= n; i++)
47 {
48 164 fac *= i;
49 164 sum += x_power / fac;
50
51 164 x_power *= x;
52 }
53
54 41 constexpr const float constant_e = 2.71828f;
55 41 return sum / constant_e;
56 }
57
58 41 float expf_taylor(float x)
59 {
60 41 return expf_taylor_impl(x, 4);
61 }
62
63 635 float tanf_taylor(float x) {
64 // This exists because the "normal" implementation, tanf, pulls in like 6kb of
65 // code and loookup tables
66
67 // This is only specified from [0, pi/2 - 0.01)
68 // Inside that range it has an error of less than 0.1%, and it gets worse as theta -> pi/2
69
70 // Precompute some exponents of x
71 635 float x2 = x * x;
72 635 float x3 = x2 * x;
73 635 float x4 = x3 * x;
74 635 float x5 = x4 * x;
75 635 float x6 = x5 * x;
76 // x7 not used
77 635 float x8 = x6 * x2;
78
79 // 3-term Taylor Series for sin(theta)
80 635 float sin_val = x - (x3 / 6) + (x5 / 120);
81
82 // 5-term Taylor Series for cos(theta)
83 635 float cos_val = 1 - (x2 / 2) + (x4 / 24) - (x6 / 720) + (x8 / 40320);
84
85 // tan = sin / cos
86 635 return sin_val / cos_val;
87 }
88
89 855 expected<float> NewtonsMethodSolver::solve(float x0, float deltaX, size_t maxIteration) {
90 855 float Xcur, Xnext;
91 855 Xnext = x0;
92
93 do {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 926 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 926 times.
926 if (maxIteration-- == 0) {
95 return unexpected;
96 }
97
98 926 Xcur = Xnext;
99
2/2
✓ Branch 1 taken 926 times.
✓ Branch 4 taken 926 times.
926 Xnext = Xcur - fx(Xcur) / dfx(Xcur);
100
2/2
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 855 times.
2/2
✓ Decision 'true' taken 71 times.
✓ Decision 'false' taken 855 times.
926 } while (absF(Xnext - Xcur) > deltaX);
101
102 855 return Xnext;
103 }
104