GNU Radio's M17 Package
util.h
Go to the documentation of this file.
1#ifndef CODEC2_MOD_UTIL_H
2#define CODEC2_MOD_UTIL_H
3
4#include <stdint.h>
5#include <math.h>
6
7float fast_atan2f(float y, float x);
8float fast_acosf(float x);
9float fast_cosf(float x);
10
11#if defined(HAVE_SINCOSF)
12#define codec2_sincosf(x, s, c) sincosf((x), (s), (c))
13#else
14static inline void codec2_sincosf(float x, float *s, float *c)
15{
16 *s = sinf(x);
17 *c = cosf(x);
18}
19#endif
20
21static inline int ceilf_fast(float x)
22{
23 int i = (int)x;
24 return i + (i < x);
25}
26
27static inline int codec2_rand(uint32_t *prng_state)
28{
29 *prng_state = *prng_state * 1103515245U + 12345U;
30 return ((unsigned)(*prng_state >> 16) & 0x7FFF);
31}
32
33/* x^y for x > 0 and |y*log2(x)| < 126
34 ~2e-5 relative error, no libm calls, only mul/add + int operations */
35static inline float fast_powf_pos(float x, float y)
36{
37 union { float f; uint32_t u; } v = {x};
38
39 /* log2(x) = exponent + log2(1 + mantissa) */
40 float e = (float)((int)(v.u >> 23) - 127);
41 v.u = (v.u & 0x007FFFFFu) | 0x3F800000u;
42 float m = v.f - 1.0f;
43 float l2 = e + m * (1.441879776f + m * (-0.708863876f + m * (0.4152409602f + m * (-0.193510391f + m * 0.04526550458f))));
44
45 /* 2^t = 2^floor(t) * 2^frac(t) */
46 float t = y * l2;
47 float fl = floorf(t);
48 float f = t - fl;
49 float p = 0.999999896f + f * (0.6931546159f + f * (0.2401407868f + f * (0.05586325839f + f * (0.008946224944f + f * 0.001895108641f))));
50 v.f = p;
51 v.u += (uint32_t)((int32_t)fl << 23);
52 return v.f;
53}
54
55#endif /* CODEC2_MOD_UTIL_H */
static float fast_powf_pos(float x, float y)
Definition util.h:35
float fast_acosf(float x)
static int ceilf_fast(float x)
Definition util.h:21
float fast_atan2f(float y, float x)
float fast_cosf(float x)
static void codec2_sincosf(float x, float *s, float *c)
Definition util.h:14
static int codec2_rand(uint32_t *prng_state)
Definition util.h:27