diff --git a/inc/math.h b/inc/math.h index 3830101..2b8255c 100644 --- a/inc/math.h +++ b/inc/math.h @@ -220,17 +220,8 @@ float sin(float x); float cos(float x); - -static inline float tan(float x) -{ - return sin(x) / cos(x); -} - - -static inline float cotan(float x) -{ - return cos(x) / sin(x); -} +float tan(float x); +float cotan(float x); float exp(float x); diff --git a/src/math.c b/src/math.c index 3c13b2a..848fadc 100644 --- a/src/math.c +++ b/src/math.c @@ -1,27 +1,45 @@ #include -static float cos_taylor(float x); +#if defined(ARM_MATH_CM7) || defined(ARM_MATH_CM4) +#include +#include -static float agm(float x, float y) +float sin(float x) { - float a = (x + y) / 2.0; - float g = sqrt(x * y); - - while(fabs(a - g) > 1E-7) - { - float an = (a + g) / 2.0; - float gn = sqrt(a * g); - - a = an; - g = gn; - } - - return a; + return arm_sin_f32(x); } +float cos(float x) +{ + return arm_cos_f32(x); +} + + +float tan(float x) +{ + float s, c; + + arm_sin_cos_f32(x, &s, &c); + + return s / c; +} + + +float cotan(float x) +{ + float s, c; + + arm_sin_cos_f32(x, &s, &c); + + return c / s; +} + +#else + +static float cos_taylor(float x); static float sin_taylor(float x) { @@ -78,8 +96,6 @@ return rv; } - - float sin(float x) { x = fmod(x, 2 * M_PI); @@ -112,6 +128,38 @@ } +float tan(float x) +{ + return sin(x) / cos(x) +} + + +float cotan(float x) +{ + return cos(x) / sin(x) +} + +#endif + + +static float agm(float x, float y) +{ + float a = (x + y) / 2.0; + float g = sqrt(x * y); + + while(fabs(a - g) > 1E-7) + { + float an = (a + g) / 2.0; + float gn = sqrt(a * g); + + a = an; + g = gn; + } + + return a; +} + + float exp(float x) { int32_t neg = 0;