GCC Code Coverage Report


Directory: ./
File: firmware/libfirmware/util/src/rusefi_math.cpp
Date: 2025-10-03 00:57:22
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

Line Branch Decision Exec Source
1 #include <rusefi/rusefi_math.h>
2
3 #include <cstdint>
4
5 1200477 int maxI(int i1, int i2) {
6
2/2
✓ Branch 0 taken 1098965 times.
✓ Branch 1 taken 101512 times.
1200477 return i1 > i2 ? i1 : i2;
7 }
8
9 1537 int minI(int i1, int i2) {
10
2/2
✓ Branch 0 taken 567 times.
✓ Branch 1 taken 970 times.
1537 return i1 < i2 ? i1 : i2;
11 }
12
13 413476 float maxF(float i1, float i2) {
14
2/2
✓ Branch 0 taken 155765 times.
✓ Branch 1 taken 257711 times.
413476 return i1 > i2 ? i1 : i2;
15 }
16
17 936422 float minF(float i1, float i2) {
18
2/2
✓ Branch 0 taken 372507 times.
✓ Branch 1 taken 563915 times.
936422 return i1 < i2 ? i1 : i2;
19 }
20
21 293468 float clampF(float min, float clamp, float max) {
22 293468 return maxF(min, minF(clamp, max));
23 }
24
25 474148 bool isSameF(float a, float b) {
26 474148 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 616 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 616 float x2 = x * x;
72 616 float x3 = x2 * x;
73 616 float x4 = x3 * x;
74 616 float x5 = x4 * x;
75 616 float x6 = x5 * x;
76 // x7 not used
77 616 float x8 = x6 * x2;
78
79 // 3-term Taylor Series for sin(theta)
80 616 float sin_val = x - (x3 / 6) + (x5 / 120);
81
82 // 5-term Taylor Series for cos(theta)
83 616 float cos_val = 1 - (x2 / 2) + (x4 / 24) - (x6 / 720) + (x8 / 40320);
84
85 // tan = sin / cos
86 616 return sin_val / cos_val;
87 }
88
89 836 expected<float> NewtonsMethodSolver::solve(float x0, float deltaX, size_t maxIteration) {
90 836 float Xcur, Xnext;
91 836 Xnext = x0;
92
93 do {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 907 times.
1/2
✗ Decision 'true' not taken.
✓ Decision 'false' taken 907 times.
907 if (maxIteration-- == 0) {
95 return unexpected;
96 }
97
98 907 Xcur = Xnext;
99
2/2
✓ Branch 1 taken 907 times.
✓ Branch 4 taken 907 times.
907 Xnext = Xcur - fx(Xcur) / dfx(Xcur);
100
2/2
✓ Branch 1 taken 71 times.
✓ Branch 2 taken 836 times.
2/2
✓ Decision 'true' taken 71 times.
✓ Decision 'false' taken 836 times.
907 } while (absF(Xnext - Xcur) > deltaX);
101
102 836 return Xnext;
103 }
104