Chirp SonicLib  4.5.2
ch_math_utils.h
Go to the documentation of this file.
1 
11 #ifndef CH_MATH_UTILS_H_
12 #define CH_MATH_UTILS_H_
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include <stdio.h>
19 #include <math.h>
20 #include <stdint.h>
21 
22 #define FRACT_BITS 16
23 
24 #define INT2FIXED(x) ((x) << FRACT_BITS)
25 #define FLOAT2FIXED(x) ((fixed_t)((x) * (1 << FRACT_BITS)))
26 
27 #define FIXED2INT(x) ((x) >> FRACT_BITS)
28 #define FIXED2FLOAT(x) (((float)(x)) / (1 << FRACT_BITS))
29 
30 #define FIXEDDIV(x, y) ((fixed_t)(((uint64_t)(x) << FRACT_BITS) / (y)))
31 #define FIXEDMUL(x, y) ((fixed_t)(((x) >> (FRACT_BITS / 2)) * ((y) >> (FRACT_BITS / 2))))
32 
33 #define FIXED_PI 0x3243FU
34 
35 #define INV_LOG2_E_Q1DOT31 0x58b90bfcU // Inverse log base 2 of e, Q1.31 format
36 
37 #define Q31_TO_Q16_SHIFT_BITS 15 // Shift Q31 format by 15 bits to give Q16
38 #define Q31_TO_Q16_SHIFT_1 10 // Number of bits to shift in first step
39 #define Q31_TO_Q16_SHIFT_2 (Q31_TO_Q16_SHIFT_BITS - Q31_TO_Q16_SHIFT_1)
40 // Number of bits to shift in second step
41 
42 typedef uint32_t fixed_t;
43 
44 fixed_t FP_sqrt(fixed_t x);
45 
59 fixed_t FP_log2(fixed_t x);
60 
74 fixed_t FP_log(fixed_t x);
75 
88 int32_t sqrt_int32(int32_t v);
89 
90 #ifdef __cplusplus
91 }
92 #endif
93 
94 #endif /* CH_MATH_UTILS_H_ */
int32_t sqrt_int32(int32_t v)
Compute the square root of a 32-bit integer.
Definition: ch_math_utils.c:84
fixed_t FP_log(fixed_t x)
Find the natural logarithm in UQ16.16 fixed-point format.
Definition: ch_math_utils.c:64
fixed_t FP_log2(fixed_t x)
Find the base 2 logarithm in UQ16.16 fixed-point format.
Definition: ch_math_utils.c:34