Skip to content

Commit 5f1f3fe

Browse files
committed
Code review fixes for float math flash savings
Follow-up to the single-precision math change that saves ~17 KB of flash on STM32F722 targets: - maths.c: Replace powf(x, 2.0f) with explicit multiplies in gaussian(). Clearer intent and avoids any dependency on libm reducing integer-exponent powf to a multiply. - vtx_smartaudio.c: Add roundf() before uint16_t cast in saDbiToMw(). powf(10.0f, 1.0f) may return 9.999...f on some libm implementations, which would truncate to 9 mW instead of the correct 10 mW. roundf() closes the hazard at the cost of one FPU instruction.
1 parent 6c1d527 commit 5f1f3fe

2 files changed

Lines changed: 2 additions & 2 deletions

File tree

‎src/main/common/maths.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ bool sensorCalibrationSolveForScale(sensorCalibrationState_t * state, float resu
526526
}
527527

528528
float gaussian(const float x, const float mu, const float sigma) {
529-
return expf(-powf(x - mu, 2.0f) / (2.0f * powf(sigma, 2.0f)));
529+
return expf(-((x - mu) * (x - mu)) / (2.0f * sigma * sigma));
530530
}
531531

532532
float bellCurve(const float x, const float curveWidth)

‎src/main/io/vtx_smartaudio.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ int saDacToPowerIndex(int dac)
212212

213213
int saDbiToMw(uint16_t dbi) {
214214

215-
uint16_t mw = (uint16_t)powf(10.0f, (float)dbi / 10.0f);
215+
uint16_t mw = (uint16_t)roundf(powf(10.0f, (float)dbi / 10.0f));
216216

217217
if (dbi > 14) {
218218
// For powers greater than 25mW round up to a multiple of 50 to match expectations

0 commit comments

Comments
 (0)