diff --git a/firmware/controllers/core/interpolation.cpp b/firmware/controllers/core/interpolation.cpp index 4c87a45074..f6c7802896 100644 --- a/firmware/controllers/core/interpolation.cpp +++ b/firmware/controllers/core/interpolation.cpp @@ -129,9 +129,12 @@ float interpolate(float x1, float y1, float x2, float y2, float x) { return interpolateMsg("", x1, y1, x2, y2, x); } +/** + * Another implementation, which one is faster? + */ int findIndex2(const float array[], unsigned size, float value) { - efiAssert(!cisnan(value), "NaN in findIndex", 0); - efiAssert(size > 1, "NaN in findIndex", 0); + efiAssert(!cisnan(value), "NaN in findIndex2", 0); + efiAssert(size > 1, "size in findIndex", 0); // if (size <= 1) // return size && *array <= value ? 0 : -1; @@ -157,8 +160,11 @@ int findIndex2(const float array[], unsigned size, float value) { * @returns the highest index within sorted array such that array[i] is greater than or equal to the parameter * @note If the parameter is smaller than the first element of the array, -1 is returned. */ -int findIndex(const float array[], int size, float value) { - efiAssert(!cisnan(value), "NaN in findIndex", 0); +int findIndexMsg(const char *msg, const float array[], int size, float value) { + if (cisnan(value)) { + firmwareError(CUSTOM_ERR_6530, "NaN in findIndex%s", msg); + return 0; + } if (value < array[0]) return -1; @@ -197,6 +203,10 @@ int findIndex(const float array[], int size, float value) { return middle; } +int findIndex(const float array[], int size, float value) { + return findIndexMsg("", array, size, value); +} + /** * @brief One-dimensional table lookup with linear interpolation */ diff --git a/firmware/controllers/core/interpolation.h b/firmware/controllers/core/interpolation.h index b348912aa6..ab470d7538 100644 --- a/firmware/controllers/core/interpolation.h +++ b/firmware/controllers/core/interpolation.h @@ -21,6 +21,7 @@ #define INTERPOLATION_A(x1, y1, x2, y2) ((y1 - y2) / (x1 - x2)) int findIndex(const float array[], int size, float value); +int findIndexMsg(const char *msg, const float array[], int size, float value); int findIndex2(const float array[], unsigned size, float value); float interpolate(float x1, float y1, float x2, float y2, float x); float interpolateMsg(const char *msg, float x1, float y1, float x2, float y2, float x); @@ -42,12 +43,12 @@ float interpolate3d(float x, float xBin[], int xBinSize, float y, float yBin[], return NAN; } - int xIndex = findIndex(xBin, xBinSize, x); + int xIndex = findIndexMsg("x", xBin, xBinSize, x); #if DEBUG_INTERPOLATION if (needInterpolationLogging()) printf("X index=%d\r\n", xIndex); #endif - int yIndex = findIndex(yBin, yBinSize, y); + int yIndex = findIndexMsg("y", yBin, yBinSize, y); if (xIndex < 0 && yIndex < 0) { #if DEBUG_INTERPOLATION if (needInterpolationLogging())