Swadge 2024 2.0.0
APIs to develop games for the Magfest Swadge
Loading...
Searching...
No Matches
fp_math.h File Reference

Detailed Description

Design Philosophy

These utility functions are can be used for fixed point decimal math.

q24_8 has 24 bits of signed integer and 8 bits of decimal. The integer range is from -8388608 to 8388607 and the smallest decimal resolution is 1/256.

q16_16 and q8_24 typedefs are also provided, but function macros aren't. Use with caution if more decimal precision is required.

vec_q24_8 is a vector type for q24_8. Fixed point vector math functions are provided and start with 'fpv.'

Usage

No initialization or deinitialization is required.

Example

q24_8 a = TO_FX(6);
q24_8 b = TO_FX_FRAC(1, 2);
q24_8 c = ADD_FX(a, b);
printf("%f\n", fixToFloat(c)); // prints 6.500000
float fixToFloat(q24_8 fx)
Convert a q24_8 to floating point.
Definition fp_math.c:255
#define ADD_FX(a, b)
Add two q24_8 numbers together.
Definition fp_math.h:106
#define TO_FX_FRAC(num, denom)
Convert an integer fraction to q24_8.
Definition fp_math.h:145
int32_t q24_8
24 bits integer, 8 bits fraction
Definition fp_math.h:36
#define TO_FX(in)
Convert an integer to a q24_8.
Definition fp_math.h:91

Go to the source code of this file.

Data Structures

struct  vec_q24_8
 A 2D vector with q24_8 numbers. More...
 

Macros

#define FRAC_BITS   8
 8 fractional bits for q24_8
 
#define Q24_8_DECI_MASK   ((1 << FRAC_BITS) - 1)
 A mask for the fractional part of a q24_8.
 
#define Q24_8_WHOLE_MASK   (~Q24_8_DECI_MASK)
 A mask for the integer part of a q24_8.
 
#define Q16_16_FRAC_BITS   16
 8 fractional bits for q16_16
 
#define Q16_16_DECI_MASK   ((1 << Q16_16_FRAC_BITS) - 1)
 A mask for the fractional part of a q16_16.
 
#define Q16_16_WHOLE_MASK   (~Q16_16_DECI_MASK)
 A mask for the integer part of a q16_16.
 
#define Q8_24_FRAC_BITS   24
 8 fractional bits for q8_24
 
#define Q8_24_DECI_MASK   ((1 << Q8_24_FRAC_BITS) - 1)
 A mask for the fractional part of a q8_24.
 
#define Q8_24_WHOLE_MASK   (~Q8_24_DECI_MASK)
 A mask for the integer part of a q8_24.
 
#define FP_MATH_DEFINES
 
#define TO_FX(in)   ((in) * (1 << FRAC_BITS))
 Convert an integer to a q24_8.
 
#define FROM_FX(in)   ((in) >> FRAC_BITS)
 Convert a q24_8 to an integer.
 
#define ADD_FX(a, b)   ((a) + (b))
 Add two q24_8 numbers together.
 
#define SUB_FX(a, b)   ((a) - (b))
 Subtract a q24_8 from another.
 
#define MUL_FX(a, b)   (((a) * (b)) >> FRAC_BITS)
 Multiply two q24_8.
 
#define DIV_FX(a, b)   (((a) << FRAC_BITS) / (b))
 Divide a q24_8 by another.
 
#define FLOOR_FX(a)   ((a) & (~((1 << FRAC_BITS) - 1)))
 Find the floor of a q24_8.
 
#define TO_FX_FRAC(num, denom)   DIV_FX(num, denom)
 Convert an integer fraction to q24_8.
 

Typedefs

typedef int16_t q8_8
 8 bits integer, 8 bits fraction
 
typedef int32_t q24_8
 24 bits integer, 8 bits fraction
 
typedef int32_t q16_16
 16 bits integer, 16 bits fraction
 
typedef int32_t q8_24
 8 bits integer, 24 bits fraction
 
typedef uint16_t uq8_8
 unsigned 8 bits integer, 8 bits fraction
 
typedef uint32_t uq24_8
 unsigned 24 bits integer, 8 bits fraction
 
typedef uint32_t uq16_16
 unsigned 16 bits integer, 16 bits fraction
 
typedef uint32_t uq8_24
 unsigned 8 bits integer, 24 bits fraction
 

Functions

void fastNormVec (q24_8 *xp, q24_8 *yp)
 Quickly normalize a q24_8 vector, in-place.
 
vec_q24_8 fpvAdd (vec_q24_8 a, vec_q24_8 b)
 Add two vectors.
 
vec_q24_8 fpvSub (vec_q24_8 a, vec_q24_8 b)
 Subtract two vectors.
 
vec_q24_8 fpvMulSc (vec_q24_8 vec, q24_8 scalar)
 Multiply a vector by a scalar.
 
vec_q24_8 fpvDivSc (vec_q24_8 vec, q24_8 scalar)
 Divide a vector by a scalar.
 
q24_8 fpvDot (vec_q24_8 a, vec_q24_8 b)
 Compute the dot product of two vectors.
 
q24_8 fpvSqMag (vec_q24_8 a)
 Compute the squared magnitude of a vector.
 
vec_q24_8 fpvNorm (vec_q24_8 vec)
 Normalize and return a vector.
 
float fixToFloat (q24_8 fx)
 Convert a q24_8 to floating point.
 

Data Structure Documentation

◆ vec_q24_8

struct vec_q24_8
Data Fields
q24_8 x The X component of the vector.
q24_8 y The Y component of the vector.

Macro Definition Documentation

◆ FRAC_BITS

#define FRAC_BITS   8

8 fractional bits for q24_8

◆ Q24_8_DECI_MASK

#define Q24_8_DECI_MASK   ((1 << FRAC_BITS) - 1)

A mask for the fractional part of a q24_8.

◆ Q24_8_WHOLE_MASK

#define Q24_8_WHOLE_MASK   (~Q24_8_DECI_MASK)

A mask for the integer part of a q24_8.

◆ Q16_16_FRAC_BITS

#define Q16_16_FRAC_BITS   16

8 fractional bits for q16_16

◆ Q16_16_DECI_MASK

#define Q16_16_DECI_MASK   ((1 << Q16_16_FRAC_BITS) - 1)

A mask for the fractional part of a q16_16.

◆ Q16_16_WHOLE_MASK

#define Q16_16_WHOLE_MASK   (~Q16_16_DECI_MASK)

A mask for the integer part of a q16_16.

◆ Q8_24_FRAC_BITS

#define Q8_24_FRAC_BITS   24

8 fractional bits for q8_24

◆ Q8_24_DECI_MASK

#define Q8_24_DECI_MASK   ((1 << Q8_24_FRAC_BITS) - 1)

A mask for the fractional part of a q8_24.

◆ Q8_24_WHOLE_MASK

#define Q8_24_WHOLE_MASK   (~Q8_24_DECI_MASK)

A mask for the integer part of a q8_24.

◆ FP_MATH_DEFINES

#define FP_MATH_DEFINES

◆ TO_FX

#define TO_FX ( in)    ((in) * (1 << FRAC_BITS))

Convert an integer to a q24_8.

Parameters
inThe integer to convert to q24_8
Returns
A q24_8

◆ FROM_FX

#define FROM_FX ( in)    ((in) >> FRAC_BITS)

Convert a q24_8 to an integer.

Parameters
inThe q24_8 to convert to integer
Returns
An integer

◆ ADD_FX

#define ADD_FX ( a,
b )   ((a) + (b))

Add two q24_8 numbers together.

Parameters
aAn operand
bThe other operand
Returns
The sum

◆ SUB_FX

#define SUB_FX ( a,
b )   ((a) - (b))

Subtract a q24_8 from another.

Parameters
aThe number to subtract from
bThe number to subtract
Returns
The difference

◆ MUL_FX

#define MUL_FX ( a,
b )   (((a) * (b)) >> FRAC_BITS)

Multiply two q24_8.

Parameters
aAn operand
bThe other operand
Returns
The result

◆ DIV_FX

#define DIV_FX ( a,
b )   (((a) << FRAC_BITS) / (b))

Divide a q24_8 by another.

Parameters
aThe numerator
bThe denominator
Returns
The result

◆ FLOOR_FX

#define FLOOR_FX ( a)    ((a) & (~((1 << FRAC_BITS) - 1)))

Find the floor of a q24_8.

Parameters
aThe number to floor
Returns
The floor of the input

◆ TO_FX_FRAC

#define TO_FX_FRAC ( num,
denom )   DIV_FX(num, denom)

Convert an integer fraction to q24_8.

Parameters
numThe numerator
denomThe denominator
Returns
The fraction in q24_8

Typedef Documentation

◆ q8_8

typedef int16_t q8_8

8 bits integer, 8 bits fraction

◆ q24_8

typedef int32_t q24_8

24 bits integer, 8 bits fraction

◆ q16_16

typedef int32_t q16_16

16 bits integer, 16 bits fraction

◆ q8_24

typedef int32_t q8_24

8 bits integer, 24 bits fraction

◆ uq8_8

typedef uint16_t uq8_8

unsigned 8 bits integer, 8 bits fraction

◆ uq24_8

typedef uint32_t uq24_8

unsigned 24 bits integer, 8 bits fraction

◆ uq16_16

typedef uint32_t uq16_16

unsigned 16 bits integer, 16 bits fraction

◆ uq8_24

typedef uint32_t uq8_24

unsigned 8 bits integer, 24 bits fraction

Function Documentation

◆ fastNormVec()

void fastNormVec ( q24_8 * xp,
q24_8 * yp )

Quickly normalize a q24_8 vector, in-place.

Parameters
xpThe X component of the vector, normalized in-place
ypThe Y component of the vector, normalized in-place

◆ fpvAdd()

vec_q24_8 fpvAdd ( vec_q24_8 a,
vec_q24_8 b )

Add two vectors.

Parameters
aA vector to add
bThe other vector to add
Returns
The sum of the two input vectors

◆ fpvSub()

vec_q24_8 fpvSub ( vec_q24_8 a,
vec_q24_8 b )

Subtract two vectors.

Parameters
aA vector to subtract from
bThe other vector to subtract
Returns
The difference between the two input vectors

◆ fpvMulSc()

vec_q24_8 fpvMulSc ( vec_q24_8 vec,
q24_8 scalar )

Multiply a vector by a scalar.

Parameters
vecA vector multiply
scalarA scalar to multiply the vector by
Returns
The scaled vector

◆ fpvDivSc()

vec_q24_8 fpvDivSc ( vec_q24_8 vec,
q24_8 scalar )

Divide a vector by a scalar.

Parameters
vecA vector divide
scalarA scalar to divide the vector by
Returns
The scaled vector

◆ fpvDot()

q24_8 fpvDot ( vec_q24_8 a,
vec_q24_8 b )

Compute the dot product of two vectors.

Parameters
aA vector to dot
bThe other vector to dot
Returns
The dot product of the two vectors

◆ fpvSqMag()

q24_8 fpvSqMag ( vec_q24_8 a)

Compute the squared magnitude of a vector.

Parameters
aThe vector to compute the squared magnitude of
Returns
The squared magnitude of the vector

◆ fpvNorm()

vec_q24_8 fpvNorm ( vec_q24_8 vec)

Normalize and return a vector.

Parameters
vecThe vector to normalize
Returns
The normalized vector

◆ fixToFloat()

float fixToFloat ( q24_8 fx)

Convert a q24_8 to floating point.

Parameters
fxFixed point input
Returns
Floating point output