Skip to content

Commit b633694

Browse files
add compile optimizations (#55)
1 parent 76b195a commit b633694

4 files changed

Lines changed: 50 additions & 2 deletions

File tree

‎numerical/controllers/Pid.hpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef CONTROLLERS_PID_HPP
22
#define CONTROLLERS_PID_HPP
33

4+
#include "numerical/math/CompilerOptimizations.hpp"
45
#include "numerical/math/QNumber.hpp"
56
#include "numerical/math/RecursiveBuffer.hpp"
67
#include <optional>
@@ -109,7 +110,9 @@ namespace controllers
109110
}
110111

111112
template<class QNumberType>
112-
QNumberType Pid<QNumberType>::Clamp(QNumberType input)
113+
ALWAYS_INLINE
114+
QNumberType
115+
Pid<QNumberType>::Clamp(QNumberType input)
113116
{
114117
if (input > limits.max)
115118
return limits.max;
@@ -121,7 +124,9 @@ namespace controllers
121124
}
122125

123126
template<class QNumberType>
124-
QNumberType Pid<QNumberType>::Process(QNumberType measuredProcessVariable)
127+
ALWAYS_INLINE_HOT
128+
QNumberType
129+
Pid<QNumberType>::Process(QNumberType measuredProcessVariable)
125130
{
126131
if (!setPoint || !autoMode)
127132
return measuredProcessVariable;

‎numerical/controllers/SpaceVectorModulation.hpp‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CONTROLLERS_SPACE_VECTOR_MODULATION_HPP
33

44
#include "numerical/controllers/TransformsClarkePark.hpp"
5+
#include "numerical/math/CompilerOptimizations.hpp"
56
#include "numerical/math/QNumber.hpp"
67

78
namespace controllers
@@ -21,6 +22,7 @@ namespace controllers
2122
QNumberType c;
2223
};
2324

25+
ALWAYS_INLINE_HOT
2426
Output Generate(const TwoPhase<QNumberType>& voltagePhase)
2527
{
2628
auto pattern = CalculateSwitchingTimes(voltagePhase.alpha, voltagePhase.beta);
@@ -33,6 +35,7 @@ namespace controllers
3335
}
3436

3537
private:
38+
ALWAYS_INLINE
3639
QNumberType ClampDutyCycle(QNumberType duty)
3740
{
3841
if (duty < zero)
@@ -50,6 +53,7 @@ namespace controllers
5053
QNumberType tc;
5154
};
5255

56+
ALWAYS_INLINE
5357
SwitchingPattern AddCommonInjection(SwitchingPattern pattern)
5458
{
5559
static const QNumberType _half = QNumberType(0.5f);
@@ -64,48 +68,55 @@ namespace controllers
6468
return pattern;
6569
}
6670

71+
ALWAYS_INLINE
6772
SwitchingPattern Calculate60To120Degrees(QNumberType valpha, QNumberType vbeta)
6873
{
6974
QNumberType t1 = valpha + vbeta * invSqrt3;
7075
QNumberType t2 = -valpha + vbeta * invSqrt3;
7176
return AddCommonInjection({ t2, t1 + t2, zero });
7277
}
7378

79+
ALWAYS_INLINE
7480
SwitchingPattern Calculate120To180Degrees(QNumberType valpha, QNumberType vbeta)
7581
{
7682
QNumberType t1 = -valpha + vbeta * invSqrt3;
7783
QNumberType t2 = -valpha - vbeta * invSqrt3;
7884
return AddCommonInjection({ zero, t1 + t2, t2 });
7985
}
8086

87+
ALWAYS_INLINE
8188
SwitchingPattern Calculate180To240Degrees(QNumberType valpha, QNumberType vbeta)
8289
{
8390
QNumberType t1 = -valpha - vbeta * invSqrt3;
8491
QNumberType t2 = vbeta * twoDivSqrt3;
8592
return AddCommonInjection({ zero, t2, t1 + t2 });
8693
}
8794

95+
ALWAYS_INLINE
8896
SwitchingPattern Calculate240To300Degrees(QNumberType valpha, QNumberType vbeta)
8997
{
9098
QNumberType t1 = -valpha + vbeta * invSqrt3;
9199
QNumberType t2 = -vbeta * twoDivSqrt3;
92100
return AddCommonInjection({ t2, zero, t1 + t2 });
93101
}
94102

103+
ALWAYS_INLINE
95104
SwitchingPattern Calculate300To360Degrees(QNumberType valpha, QNumberType vbeta)
96105
{
97106
QNumberType t1 = valpha + vbeta * invSqrt3;
98107
QNumberType t2 = -valpha - vbeta * invSqrt3;
99108
return AddCommonInjection({ t1 + t2, zero, t2 });
100109
}
101110

111+
ALWAYS_INLINE
102112
SwitchingPattern Calculate0To60Degrees(QNumberType valpha, QNumberType vbeta)
103113
{
104114
QNumberType t1 = valpha - vbeta * invSqrt3;
105115
QNumberType t2 = vbeta * twoDivSqrt3;
106116
return AddCommonInjection({ t1 + t2, t2, zero });
107117
}
108118

119+
ALWAYS_INLINE_HOT
109120
SwitchingPattern CalculateSwitchingTimes(QNumberType valpha, QNumberType vbeta)
110121
{
111122
QNumberType v_ref_60 = (valpha * half - vbeta * sqrt3Div2);

‎numerical/controllers/TransformsClarkePark.hpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define TRANSFORMS_CLARKE_PARK_HPP
33

44
#include "numerical/math/AdvancedFunctions.hpp"
5+
#include "numerical/math/CompilerOptimizations.hpp"
56
#include "numerical/math/QNumber.hpp"
67
#include "numerical/math/TrigonometricFunctions.hpp"
78

@@ -37,13 +38,15 @@ namespace controllers
3738
"Clarke can only be instantiated with math::QNumber or floating point types.");
3839

3940
public:
41+
ALWAYS_INLINE_HOT
4042
TwoPhase<QNumberType> Forward(const ThreePhase<QNumberType>& input)
4143
{
4244
const QNumberType bc_sum = input.b + input.c;
4345

4446
return TwoPhase<QNumberType>{ twoThirds * (input.a - oneHalf * bc_sum), invSqrt3 * (input.b - input.c) };
4547
}
4648

49+
ALWAYS_INLINE_HOT
4750
ThreePhase<QNumberType> Inverse(const TwoPhase<QNumberType>& input)
4851
{
4952
const QNumberType alpha_half = oneHalf * input.alpha;
@@ -70,6 +73,7 @@ namespace controllers
7073
: trigFunctions(trigFunctions)
7174
{}
7275

76+
ALWAYS_INLINE_HOT
7377
RotatingFrame<QNumberType> Forward(const TwoPhase<QNumberType>& input, QNumberType scaledTheta)
7478
{
7579
really_assert(scaledTheta >= math::Lowest<QNumberType>() && scaledTheta <= math::Max<QNumberType>());
@@ -85,6 +89,7 @@ namespace controllers
8589
return RotatingFrame<QNumberType>{ alpha_cos + beta_sin, -alpha_sin + beta_cos };
8690
}
8791

92+
ALWAYS_INLINE_HOT
8893
TwoPhase<QNumberType> Inverse(const RotatingFrame<QNumberType>& input, QNumberType scaledTheta)
8994
{
9095
really_assert(scaledTheta >= math::Lowest<QNumberType>() && scaledTheta <= math::Max<QNumberType>());
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#if defined(__GNUC__) && !defined(__clang__)
4+
#define OPTIMIZE_FOR_SPEED __attribute__((hot, optimize("-O3"), optimize("-ffast-math"), flatten))
5+
#elif defined(__clang__)
6+
#define OPTIMIZE_FOR_SPEED __attribute__((hot, flatten))
7+
#elif defined(_MSC_VER)
8+
#define OPTIMIZE_FOR_SPEED
9+
#else
10+
#define OPTIMIZE_FOR_SPEED
11+
#endif
12+
13+
#if defined(__GNUC__) || defined(__clang__)
14+
#define ALWAYS_INLINE_HOT __attribute__((always_inline, hot)) inline
15+
#elif defined(_MSC_VER)
16+
#define ALWAYS_INLINE_HOT __forceinline
17+
#else
18+
#define ALWAYS_INLINE_HOT inline
19+
#endif
20+
21+
#if defined(__GNUC__) || defined(__clang__)
22+
#define ALWAYS_INLINE __attribute__((always_inline)) inline
23+
#elif defined(_MSC_VER)
24+
#define ALWAYS_INLINE __forceinline
25+
#else
26+
#define ALWAYS_INLINE inline
27+
#endif

0 commit comments

Comments
 (0)