make wled_math functions IRAM_ATTR (faster)

This commit is contained in:
Frank
2026-01-17 13:56:29 +01:00
parent 1f065f69c2
commit d987206d3b
2 changed files with 7 additions and 5 deletions

View File

@@ -588,7 +588,7 @@ float fmod_t(float num, float denom);
#define cos_t cosf #define cos_t cosf
#define tan_t tanf #define tan_t tanf
*/ */
uint32_t sqrt32_bw(uint32_t x); uint32_t __attribute__((const)) sqrt32_bw(uint32_t x);
//wled_serial.cpp //wled_serial.cpp
void handleSerial(); void handleSerial();

View File

@@ -8,6 +8,8 @@
#include <Arduino.h> //PI constant #include <Arduino.h> //PI constant
#include "const.h" // WLED constantsr
//#define WLED_DEBUG_MATH //#define WLED_DEBUG_MATH
// Note: cos_t, sin_t and tan_t are very accurate but slow // Note: cos_t, sin_t and tan_t are very accurate but slow
@@ -101,21 +103,21 @@ void init_math(void) {
void init_math(void) { return;} // dummy for 8266 void init_math(void) { return;} // dummy for 8266
#endif #endif
float sin_approx(float theta) { float IRAM_ATTR_YN sin_approx(float theta) {
uint16_t scaled_theta = (int)(theta * (float)(0xFFFF / M_TWOPI)); // note: do not cast negative float to uint! cast to int first (undefined on C3) uint16_t scaled_theta = (int)(theta * (float)(0xFFFF / M_TWOPI)); // note: do not cast negative float to uint! cast to int first (undefined on C3)
int32_t result = sin16_calc(scaled_theta); int32_t result = sin16_calc(scaled_theta);
float sin = float(result) / 0x7FFF; float sin = float(result) / 0x7FFF;
return sin; return sin;
} }
float cos_approx(float theta) { float IRAM_ATTR_YN cos_approx(float theta) {
uint16_t scaled_theta = (int)(theta * (float)(0xFFFF / M_TWOPI)); // note: do not cast negative float to uint! cast to int first (undefined on C3) uint16_t scaled_theta = (int)(theta * (float)(0xFFFF / M_TWOPI)); // note: do not cast negative float to uint! cast to int first (undefined on C3)
int32_t result = sin16_calc(scaled_theta + 0x4000); int32_t result = sin16_calc(scaled_theta + 0x4000);
float cos = float(result) / 0x7FFF; float cos = float(result) / 0x7FFF;
return cos; return cos;
} }
float tan_approx(float x) { float IRAM_ATTR_YN tan_approx(float x) {
float c = cos_approx(x); float c = cos_approx(x);
if (c==0.0f) return 0; if (c==0.0f) return 0;
float res = sin_approx(x) / c; float res = sin_approx(x) / c;
@@ -237,7 +239,7 @@ float fmod_t(float num, float denom) {
#endif // WLEDMM #endif // WLEDMM
// bit-wise integer square root calculation (exact) // bit-wise integer square root calculation (exact)
uint32_t sqrt32_bw(uint32_t x) { uint32_t IRAM_ATTR_YN sqrt32_bw(uint32_t x) {
uint32_t res = 0; uint32_t res = 0;
uint32_t bit; uint32_t bit;
uint32_t num = x; // use 32bit for faster calculation uint32_t num = x; // use 32bit for faster calculation