Swadge ESP32-S2/S3
APIs to develop for the Magfest Swadge
Loading...
Searching...
No Matches
fp_math.h
Go to the documentation of this file.
1
28
29#ifndef _FP_MATH_H_
30#define _FP_MATH_H_
31
32#include <stdint.h>
33#include <stdbool.h>
34
35typedef int16_t q8_8;
36typedef int32_t q24_8;
37typedef int32_t q16_16;
38typedef int32_t q8_24;
39
40typedef uint16_t uq8_8;
41typedef uint32_t uq24_8;
42typedef uint32_t uq16_16;
43typedef uint32_t uq8_24;
44
45#define FRAC_BITS 8
46#define Q24_8_DECI_MASK ((1 << FRAC_BITS) - 1)
47#define Q24_8_WHOLE_MASK (~Q24_8_DECI_MASK)
48
49#define Q16_16_FRAC_BITS 16
50#define Q16_16_DECI_MASK ((1 << Q16_16_FRAC_BITS) - 1)
51#define Q16_16_WHOLE_MASK (~Q16_16_DECI_MASK)
52
53#define Q8_24_FRAC_BITS 24
54#define Q8_24_DECI_MASK ((1 << Q8_24_FRAC_BITS) - 1)
55#define Q8_24_WHOLE_MASK (~Q8_24_DECI_MASK)
56
58typedef struct
59{
62} vec_q24_8;
63
64//==============================================================================
65// Fixed Point Math Functions
66//==============================================================================
67
68void fastNormVec(q24_8* xp, q24_8* yp);
69
74
78
79float fixToFloat(q24_8 fx);
80
81// Switch to use macros or inline functions
82#define FP_MATH_DEFINES
83
84#ifdef FP_MATH_DEFINES
85
91 #define TO_FX_QN(in, fracBits) ((in) * (1 << (fracBits)))
92
98 #define FROM_FX_QN(in, fracBits) ((in) >> (fracBits))
99
106 #define ADD_FX_QN(a, b, fracBits) ((a) + (b))
107
114 #define SUB_FX_QN(a, b, fracBits) ((a) - (b))
115
122 #define MUL_FX_QN(a, b, fracBits) (((a) * (b)) >> (fracBits))
123
130 #define DIV_FX_QN(a, b, fracBits) (((a) << (fracBits)) / (b))
131
137 #define FLOOR_FX_QN(a, fracBits) ((a) & (~((1 << (fracBits)) - 1)))
138
144 #define ROUND_FX_QN(a, fracBits) \
145 ((a) >= 0 ? (((a) + (1 << ((fracBits) - 1))) >> (fracBits)) \
146 : -(((-(a)) + (1 << ((fracBits) - 1))) >> (fracBits)))
147
154 #define TO_FX_FRAC_QN(num, denom, fracBits) DIV_FX(num, denom)
155
161 #define TO_FX(in) TO_FX_QN(in, FRAC_BITS)
162
168 #define FROM_FX(in) FROM_FX_QN(in, FRAC_BITS)
169
176 #define ADD_FX(a, b) ADD_FX_QN(a, b, FRAC_BITS)
177
184 #define SUB_FX(a, b) SUB_FX_QN(a, b, FRAC_BITS)
185
192 #define MUL_FX(a, b) MUL_FX_QN(a, b, FRAC_BITS)
193
200 #define DIV_FX(a, b) DIV_FX_QN(a, b, FRAC_BITS)
201
207 #define FLOOR_FX(a) FLOOR_FX_QN(a, FRAC_BITS)
208
214 #define ROUND_FX(a) ROUND_FX_QN(a, FRAC_BITS)
215
222 #define TO_FX_FRAC(num, denom) TO_FX_FRAC_QN(num, denom, FRAC_BITS)
223
224#else
225
226static inline q24_8 TO_FX(uint32_t in)
227{
228 return (q24_8)(in * (1 << FRAC_BITS));
229}
230
231static inline int32_t FROM_FX(q24_8 in)
232{
233 return in / (1 << FRAC_BITS);
234}
235
236static inline q24_8 ADD_FX(q24_8 a, q24_8 b)
237{
238 return a + b;
239}
240
241static inline q24_8 SUB_FX(q24_8 a, q24_8 b)
242{
243 return a - b;
244}
245
246static inline q24_8 MUL_FX(q24_8 a, q24_8 b)
247{
248 // could be simpler without rounding
249 return ((a * b) + (1 << (FRAC_BITS - 1))) / (1 << FRAC_BITS);
250}
251
252static inline q24_8 DIV_FX(q24_8 a, q24_8 b)
253{
254 return ((a * (1 << FRAC_BITS)) / b);
255}
256
257static inline q24_8 FLOOR_FX(q24_8 a)
258{
259 return a & (~((1 << FRAC_BITS) - 1));
260}
261
262static inline int32_t ROUND_FX(q24_8 a)
263{
264 if (a >= 0)
265 {
266 return (int32_t)((a + (1 << (FRAC_BITS - 1))) >> FRAC_BITS);
267 }
268 return -(int32_t)(((-a) + (1 << (FRAC_BITS - 1))) >> FRAC_BITS);
269}
270
271// #define FMT_FX "%s%d.%03d"
272// #define STR_FX(x) ((x) < 0 ? "-" : ""), ABS(FROM_FX(x)), DEC_PART(x)
273
274// static inline int32_t DEC_PART(q24_8 in)
275// {
276// return (1000 * (int32_t)(ABS(in) & ((1 << FRAC_BITS) - 1))) / (1 << FRAC_BITS);
277// }
278#endif
279
280#endif
uint32_t uq24_8
unsigned 24 bits integer, 8 bits fraction
Definition fp_math.h:41
#define FRAC_BITS
8 fractional bits for q24_8
Definition fp_math.h:45
#define ADD_FX(a, b)
Add two q24_8 numbers together.
Definition fp_math.h:176
q24_8 y
The Y component of the vector.
Definition fp_math.h:61
uint32_t uq8_24
unsigned 8 bits integer, 24 bits fraction
Definition fp_math.h:43
uint16_t uq8_8
unsigned 8 bits integer, 8 bits fraction
Definition fp_math.h:40
vec_q24_8 fpvDivSc(vec_q24_8 vec, q24_8 scalar)
Divide a vector by a scalar.
Definition fp_math.c:248
vec_q24_8 fpvNorm(vec_q24_8 vec)
Normalize and return a vector.
Definition fp_math.c:163
#define FROM_FX(in)
Convert a q24_8 to an integer.
Definition fp_math.h:168
q24_8 fpvSqMag(vec_q24_8 a)
Compute the squared magnitude of a vector.
Definition fp_math.c:188
vec_q24_8 fpvSub(vec_q24_8 a, vec_q24_8 b)
Subtract two vectors.
Definition fp_math.c:216
#define DIV_FX(a, b)
Divide a q24_8 by another.
Definition fp_math.h:200
q24_8 x
The X component of the vector.
Definition fp_math.h:60
#define MUL_FX(a, b)
Multiply two q24_8.
Definition fp_math.h:192
int32_t q16_16
16 bits integer, 16 bits fraction
Definition fp_math.h:37
#define FLOOR_FX(a)
Find the floor of a q24_8.
Definition fp_math.h:207
void fastNormVec(q24_8 *xp, q24_8 *yp)
Quickly normalize a q24_8 vector, in-place.
Definition fp_math.c:67
#define ROUND_FX(a)
Round a q24_8 to the nearest integer.
Definition fp_math.h:214
vec_q24_8 fpvAdd(vec_q24_8 a, vec_q24_8 b)
Add two vectors.
Definition fp_math.c:200
int16_t q8_8
8 bits integer, 8 bits fraction
Definition fp_math.h:35
#define SUB_FX(a, b)
Subtract a q24_8 from another.
Definition fp_math.h:184
q24_8 fpvDot(vec_q24_8 a, vec_q24_8 b)
Compute the dot product of two vectors.
Definition fp_math.c:177
float fixToFloat(q24_8 fx)
Convert a q24_8 to floating point.
Definition fp_math.c:263
int32_t q24_8
24 bits integer, 8 bits fraction
Definition fp_math.h:36
int32_t q8_24
8 bits integer, 24 bits fraction
Definition fp_math.h:38
vec_q24_8 fpvMulSc(vec_q24_8 vec, q24_8 scalar)
Multiply a vector by a scalar.
Definition fp_math.c:232
#define TO_FX(in)
Convert an integer to a q24_8.
Definition fp_math.h:161
uint32_t uq16_16
unsigned 16 bits integer, 16 bits fraction
Definition fp_math.h:42
A 2D vector with q24_8 numbers.
Definition fp_math.h:59