From acf18c7072a96eeff2af414fa36bdc57cb3a42ec Mon Sep 17 00:00:00 2001 From: Matthew Kennedy Date: Sat, 16 Jul 2022 21:16:22 -0700 Subject: [PATCH] move math (#4353) --- firmware/libfirmware | 2 +- firmware/pch/pch.h | 1 + firmware/util/efilib.cpp | 104 --------------------- firmware/util/efilib.h | 21 +---- unit_tests/Makefile | 1 + unit_tests/test.mk | 1 - unit_tests/test_basic_math/test_efilib.cpp | 44 --------- 7 files changed, 5 insertions(+), 169 deletions(-) delete mode 100644 unit_tests/test_basic_math/test_efilib.cpp diff --git a/firmware/libfirmware b/firmware/libfirmware index a8d6982d40..8fcca4c0ce 160000 --- a/firmware/libfirmware +++ b/firmware/libfirmware @@ -1 +1 @@ -Subproject commit a8d6982d409a83fa7beb5c37d907d742b620ee2e +Subproject commit 8fcca4c0ce715a3c44ad13bb1a1a9316dbe63605 diff --git a/firmware/pch/pch.h b/firmware/pch/pch.h index f32873ac7c..531404de21 100644 --- a/firmware/pch/pch.h +++ b/firmware/pch/pch.h @@ -21,6 +21,7 @@ #include #include #include +#include #include "efifeatures.h" #include "rusefi_generated.h" diff --git a/firmware/util/efilib.cpp b/firmware/util/efilib.cpp index 23f695193b..fd8f420bf8 100644 --- a/firmware/util/efilib.cpp +++ b/firmware/util/efilib.cpp @@ -19,10 +19,6 @@ const char * boolToString(bool value) { return value ? "Yes" : "No"; } -int minI(int i1, int i2) { - return i1 < i2 ? i1 : i2; -} - /* float efiFloor(float value, float precision) { int a = (int) (value / precision); @@ -40,34 +36,6 @@ float efiRound(float value, float precision) { return fixNegativeZero(a * precision); } -float absF(float value) { - return value > 0 ? value : -value; -} - -int absI(int32_t value) { - return value >= 0 ? value : -value; -} - -int maxI(int i1, int i2) { - return i1 > i2 ? i1 : i2; -} - -float maxF(float i1, float i2) { - return i1 > i2 ? i1 : i2; -} - -float minF(float i1, float i2) { - return i1 < i2 ? i1 : i2; -} - -int clampI(int min, int clamp, int max) { - return maxI(min, minI(clamp, max)); -} - -float clampF(float min, float clamp, float max) { - return maxF(min, minF(clamp, max)); -} - uint32_t efiStrlen(const char *param) { const char *s; for (s = param; *s; ++s) @@ -197,12 +165,6 @@ char* itoa10(char *p, int num) { return itoa_signed(p, num, 10); } -#define EPS 0.0001 - -bool isSameF(float v1, float v2) { - return absF(v1 - v2) < EPS; -} - int efiPow10(int param) { switch (param) { case 0: @@ -336,69 +298,3 @@ float limitRateOfChange(float newValue, float oldValue, float incrLimitPerSec, f return (incrLimitPerSec <= 0.0f) ? newValue : oldValue + minF(newValue - oldValue, incrLimitPerSec * secsPassed); return (decrLimitPerSec <= 0.0f) ? newValue : oldValue - minF(oldValue - newValue, decrLimitPerSec * secsPassed); } - -constexpr float constant_e = 2.71828f; - -// 'constexpr' is a keyword that tells the compiler -// "yes, this thing, it's a 'pure function' that only depends on its inputs and has no side effects" -// like how const is a constant value, constexpr is a constant expression -// so if somewhere you used it in a way that it could determine the exact arguments to the function at compile time, it will _run_ the function at compile time, and cook in the result as a constant -constexpr float expf_taylor_impl(float x, uint8_t n) -{ - if (x < -2) - { - return 0.818f; - } - else if (x > 0) - { - return 1; - } - - x = x + 1; - - float x_power = x; - int fac = 1; - float sum = 1; - - for (int i = 1; i <= n; i++) - { - fac *= i; - sum += x_power / fac; - - x_power *= x; - } - - return sum / constant_e; -} - -float expf_taylor(float x) -{ - return expf_taylor_impl(x, 4); -} - -float tanf_taylor(float x) { - // This exists because the "normal" implementation, tanf, pulls in like 6kb of - // code and loookup tables - - // This is only specified from [0, pi/2 - 0.01) - // Inside that range it has an error of less than 0.1%, and it gets worse as theta -> pi/2 - - // Precompute some exponents of x - float x2 = x * x; - float x3 = x2 * x; - float x4 = x3 * x; - float x5 = x4 * x; - float x6 = x5 * x; - // x7 not used - float x8 = x6 * x2; - - // 3-term Taylor Series for sin(theta) - float sin_val = x - (x3 / 6) + (x5 / 120); - - // 5-term Taylor Series for cos(theta) - float cos_val = 1 - (x2 / 2) + (x4 / 24) - (x6 / 720) + (x8 / 40320); - - // tan = sin / cos - return sin_val / cos_val; -} - diff --git a/firmware/util/efilib.h b/firmware/util/efilib.h index fa8377edbf..776c385231 100644 --- a/firmware/util/efilib.h +++ b/firmware/util/efilib.h @@ -62,24 +62,15 @@ float atoff(const char *string); int atoi(const char *string); #define UNUSED(x) (void)(x) - -int absI(int32_t value); -float absF(float value); + /** * Rounds value to specified precision. * @param precision some pow of 10 value - for example, 100 for two digit precision */ float efiRound(float value, float precision); -int maxI(int i1, int i2); -int minI(int i1, int i2); -float maxF(float i1, float i2); -float minF(float i1, float i2); + // sometimes known as 'itoa' char* itoa10(char *p, int num); -bool isSameF(float v1, float v2); - -int clampI(int min, int clamp, int max); -float clampF(float min, float clamp, float max); /** * clamps value into the [0, 100] range @@ -92,14 +83,6 @@ bool strEqual(const char *str1, const char *str2); // Currently used by air-interp. tCharge mode (see EngineState::updateTChargeK()). float limitRateOfChange(float newValue, float oldValue, float incrLimitPerSec, float decrLimitPerSec, float secsPassed); -// @brief Compute e^x using a 4th order taylor expansion centered at x=-1. Provides -// bogus results outside the range -2 < x < 0. -float expf_taylor(float x); - -// @brief Compute tan(theta) using a ratio of the Taylor series for sin and cos -// Valid for the range [0, pi/2 - 0.01] -float tanf_taylor(float theta); - #ifdef __cplusplus } diff --git a/unit_tests/Makefile b/unit_tests/Makefile index 82c4455ca2..9de574cc32 100644 --- a/unit_tests/Makefile +++ b/unit_tests/Makefile @@ -38,6 +38,7 @@ CPPSRC += $(ALLCPPSRC) \ $(PROJECT_DIR)/../unit_tests/global_mocks.cpp \ $(PROJECT_DIR)/../unit_tests/mocks.cpp \ $(RUSEFI_LIB_CPP) \ + $(RUSEFI_LIB_CPP_TEST) \ INCDIR += \ $(PCH_DIR) \ diff --git a/unit_tests/test.mk b/unit_tests/test.mk index 2de380e5e9..ca3395a7cc 100644 --- a/unit_tests/test.mk +++ b/unit_tests/test.mk @@ -5,5 +5,4 @@ FRAMEWORK_SRC_CPP = unit_test_framework.cpp \ global_execution_queue.cpp \ test_basic_math/test_find_index.cpp \ test_basic_math/test_interpolation_3d.cpp \ - test_basic_math/test_efilib.cpp \ diff --git a/unit_tests/test_basic_math/test_efilib.cpp b/unit_tests/test_basic_math/test_efilib.cpp deleted file mode 100644 index 0c9e4b3308..0000000000 --- a/unit_tests/test_basic_math/test_efilib.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * test_efilib.cpp - * - * Created on: Jan 6, 2019 - * @author Matthew Kennedy, (c) 2019 - */ - -#include "pch.h" - -TEST(EfiLibTest, ExpTaylor) -{ - float x = -2; - - // test from -2 < x < 0 - for(float x = -2; x < 0; x += 0.05) - { - // Compare taylor to libc implementation - EXPECT_NEAR(expf_taylor(x), expf(x), 0.01f); - } -} - -TEST(EfiLibTest, clampf) { - // off scale low - EXPECT_EQ(clampF(10, 5, 20), 10); - EXPECT_EQ(clampF(-10, -50, 10), -10); - - // in range (unclamped) - EXPECT_EQ(clampF(10, 15, 20), 15); - EXPECT_EQ(clampF(-10, -5, 10), -5); - - // off scale high - EXPECT_EQ(clampF(10, 25, 20), 20); - EXPECT_EQ(clampF(-10, 50, 10), 10); - -} - -TEST(EfiLibTest, tanf_taylor) { - // Function is only specified from [0, pi/2) ish, so test that range - for (float i = 0; i < 1.5; i += 0.1f) - { - // Compare to libc implementation - EXPECT_NEAR(tanf_taylor(i), tanf(i), 0.05f) << "I = " << i; - } -}