Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
fp_math.h
Go to the documentation of this file.
1
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(in) ((in) * (1 << FRAC_BITS))
92
98 #define FROM_FX(in) ((in) >> FRAC_BITS)
99
106 #define ADD_FX(a, b) ((a) + (b))
107
114 #define SUB_FX(a, b) ((a) - (b))
115
122 #define MUL_FX(a, b) (((a) * (b)) >> FRAC_BITS)
123
130 #define DIV_FX(a, b) (((a) << FRAC_BITS) / (b))
131
137 #define FLOOR_FX(a) ((a) & (~((1 << FRAC_BITS) - 1)))
138
145 #define TO_FX_FRAC(num, denom) DIV_FX(num, denom)
146
147#else
148
149static inline q24_8 TO_FX(uint32_t in)
150{
151 return (q24_8)(in * (1 << FRAC_BITS));
152}
153
154static inline int32_t FROM_FX(q24_8 in)
155{
156 return in / (1 << FRAC_BITS);
157}
158
159static inline q24_8 ADD_FX(q24_8 a, q24_8 b)
160{
161 return a + b;
162}
163
164static inline q24_8 SUB_FX(q24_8 a, q24_8 b)
165{
166 return a - b;
167}
168
169static inline q24_8 MUL_FX(q24_8 a, q24_8 b)
170{
171 // could be simpler without rounding
172 return ((a * b) + (1 << (FRAC_BITS - 1))) / (1 << FRAC_BITS);
173}
174
175static inline q24_8 DIV_FX(q24_8 a, q24_8 b)
176{
177 return ((a * (1 << FRAC_BITS)) / b);
178}
179
180static inline q24_8 FLOOR_FX(q24_8 a)
181{
182 return a & (~((1 << FRAC_BITS) - 1));
183}
184
185// #define FMT_FX "%s%d.%03d"
186// #define STR_FX(x) ((x) < 0 ? "-" : ""), ABS(FROM_FX(x)), DEC_PART(x)
187
188// static inline int32_t DEC_PART(q24_8 in)
189// {
190// return (1000 * (int32_t)(ABS(in) & ((1 << FRAC_BITS) - 1))) / (1 << FRAC_BITS);
191// }
192#endif
193
194#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:106
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:240
vec_q24_8 fpvNorm(vec_q24_8 vec)
Normalize and return a vector.
Definition fp_math.c:155
#define FROM_FX(in)
Convert a q24_8 to an integer.
Definition fp_math.h:98
q24_8 fpvSqMag(vec_q24_8 a)
Compute the squared magnitude of a vector.
Definition fp_math.c:180
vec_q24_8 fpvSub(vec_q24_8 a, vec_q24_8 b)
Subtract two vectors.
Definition fp_math.c:208
#define DIV_FX(a, b)
Divide a q24_8 by another.
Definition fp_math.h:130
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:122
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:137
void fastNormVec(q24_8 *xp, q24_8 *yp)
Quickly normalize a q24_8 vector, in-place.
Definition fp_math.c:67
vec_q24_8 fpvAdd(vec_q24_8 a, vec_q24_8 b)
Add two vectors.
Definition fp_math.c:192
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:114
q24_8 fpvDot(vec_q24_8 a, vec_q24_8 b)
Compute the dot product of two vectors.
Definition fp_math.c:169
float fixToFloat(q24_8 fx)
Convert a q24_8 to floating point.
Definition fp_math.c:255
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:224
#define TO_FX(in)
Convert an integer to a q24_8.
Definition fp_math.h:91
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