#ifndef __LC3_FASTMATH_H
#define __LC3_FASTMATH_H
#include <stdint.h>
#include <math.h>
static inline float lc3_exp2f(float x)
{
float y;
static const float c[] = { 1.27191277e-09, 1.47415221e-07,
1.35510312e-05, 9.38375815e-04, 4.33216946e-02 };
y = ( c[0]) * x;
y = (y + c[1]) * x;
y = (y + c[2]) * x;
y = (y + c[3]) * x;
y = (y + c[4]) * x;
y = (y + 1.f);
y = y*y;
y = y*y;
y = y*y;
y = y*y;
return y;
}
static inline float lc3_log2f(float x)
{
float y;
int e;
static const float c[] = {
-1.29479677, 5.11769018, -8.42295281, 8.10557963, -3.50567360 };
x = frexpf(x, &e);
y = ( c[0]) * x;
y = (y + c[1]) * x;
y = (y + c[2]) * x;
y = (y + c[3]) * x;
y = (y + c[4]);
return e + y;
}
static inline float lc3_log10f(float x)
{
return log10f(2) * lc3_log2f(x);
}
static inline int32_t lc3_db_q16(float x)
{
static const uint16_t t[][2] = {
{ 0, 4379 }, { 4379, 4248 }, { 8627, 4125 }, { 12753, 4009 },
{ 16762, 3899 }, { 20661, 3795 }, { 24456, 3697 }, { 28153, 3603 },
{ 31755, 3514 }, { 35269, 3429 }, { 38699, 3349 }, { 42047, 3272 },
{ 45319, 3198 }, { 48517, 3128 }, { 51645, 3061 }, { 54705, 2996 },
{ 8381, 2934 }, { 11315, 2875 }, { 14190, 2818 }, { 17008, 2763 },
{ 19772, 2711 }, { 22482, 2660 }, { 25142, 2611 }, { 27754, 2564 },
{ 30318, 2519 }, { 32837, 2475 }, { 35312, 2433 }, { 37744, 2392 },
{ 40136, 2352 }, { 42489, 2314 }, { 44803, 2277 }, { 47080, 2241 },
};
union { float f; uint32_t u; } x2 = { .f = x*x };
int e2 = (int)(x2.u >> 22) - 2*127;
int hi = (x2.u >> 18) & 0x1f;
int lo = (x2.u >> 2) & 0xffff;
return e2 * 49321 + t[hi][0] + ((t[hi][1] * lo) >> 16);
}
#endif