This commit is contained in:
Matthew Kennedy 2022-07-16 21:16:22 -07:00 committed by GitHub
parent 6a0a2ec9eb
commit acf18c7072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 5 additions and 169 deletions

@ -1 +1 @@
Subproject commit a8d6982d409a83fa7beb5c37d907d742b620ee2e
Subproject commit 8fcca4c0ce715a3c44ad13bb1a1a9316dbe63605

View File

@ -21,6 +21,7 @@
#include <rusefi/crc.h>
#include <rusefi/interpolation.h>
#include <rusefi/isnan.h>
#include <rusefi/math.h>
#include "efifeatures.h"
#include "rusefi_generated.h"

View File

@ -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;
}

View File

@ -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
}

View File

@ -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) \

View File

@ -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 \

View File

@ -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;
}
}