diff --git a/src/math.c b/src/math.c index 47f6a77..b9975fa 100644 --- a/src/math.c +++ b/src/math.c @@ -1,5 +1,7 @@ #include +#include + #if defined(ARM_MATH_CM7) || defined(ARM_MATH_CM4) @@ -162,21 +164,42 @@ float exp(float x) { - int32_t neg = 0; + const float int_exps[] = + { + M_E, + M_E * M_E, + M_E * M_E * M_E, + M_E * M_E * M_E * M_E, + M_E * M_E * M_E * M_E * M_E, + M_E * M_E * M_E * M_E * M_E * M_E + }; + + + bool neg = false; if (x < 0) { neg = 1; x = -x; } + + float rv; - float rv = 1; - - int32_t x_int = x; + uint32_t x_int = x; x -= x_int; - - while(x_int--) - rv *= M_E; + + if (x_int > 127) + { + rv = INFINITY; + } + else + { + rv = 1; + + for(uint32_t i = 1; (i < 7) && x_int; i++, x_int >>= 1) + if (x_int & 0x01) + rv *= int_exps[i]; + } if (x > 0) { @@ -189,7 +212,6 @@ return 1 / (rv * rv_frac); else return rv * rv_frac; - } if (neg)