BEEP-8 Helper Lib 1.0.0
Loading...
Searching...
No Matches
submath.h File Reference

Provides mathematical functions and utilities, primarily focused on fixed-point arithmetic. More...

#include <submath.h>
#include <fixed.h>
#include <fxmath.h>
#include <b8/type.h>

Go to the source code of this file.

Classes

class  Vec
 
struct  Line
 Represents a line segment in 2D space defined by two points. More...
 
struct  Poly
 Represents a triangle in 2D space using three points. More...
 
struct  Rect
 A structure for representing rectangles with fixed-point coordinates. More...
 
struct  Xorshift32
 

Macros

#define M_E   2.71828182845904523536028747135266250 /* e */
 
#define M_LOG2E   1.44269504088896340735992468100189214 /* log2(e) */
 
#define M_LOG10E   0.434294481903251827651128918916605082 /* log10(e) */
 
#define M_LN2   0.693147180559945309417232121458176568 /* loge(2) */
 
#define M_LN10   2.30258509299404568401799145468436421 /* loge(10) */
 
#define M_PI   3.14159265358979323846264338327950288 /* pi */
 
#define M_PI_2   1.57079632679489661923132169163975144 /* pi/2 */
 
#define M_PI_4   0.785398163397448309615660845819875721 /* pi/4 */
 
#define M_1_PI   0.318309886183790671537767526745028724 /* 1/pi */
 
#define M_2_PI   0.636619772367581343075535053490057448 /* 2/pi */
 
#define M_2_SQRTPI   1.12837916709551257389615890312154517 /* 2/sqrt(pi) */
 
#define M_SQRT2   1.41421356237309504880168872420969808 /* sqrt(2) */
 
#define M_SQRT1_2   0.707106781186547524400844362104849039 /* 1/sqrt(2) */
 
#define MAXFLOAT   0x1.fffffep+127f
 

Typedefs

using fx8 = fpm::fixed<std::int32_t, std::int64_t, 8>
 Alias for fixed-point type with 8 fractional bits.
 
using fx12 = fpm::fixed<std::int32_t, std::int64_t, 12>
 Alias for fixed-point type with 12 fractional bits.
 

Functions

std::string tostr (const fx8 &val)
 
std::string tostr (const Vec &val)
 
std::string tostr (const Rect &val)
 
const fx8_min (const fx8 &lhs_, const fx8 &rhs_)
 Get the minimum of two fixed-point values.
 
const fx8_max (const fx8 &lhs_, const fx8 &rhs_)
 Get the maximum of two fixed-point values.
 
const fx8_lim (const fx8 &x_, const fx8 &lhs_, const fx8 &rhs_)
 Limit a fixed-point value within a specified range.
 
const fx8 _abs (const fx8 &x_)
 Get the absolute value of a fixed-point number.
 
template<typename T , typename U , typename V >
lim (const T &x_, const U &lhs_, const V &rhs_)
 Limit a value within a specified range.
 
s16 sin_12 (u32 th)
 Calculate the sine of an angle using a 12-bit angle representation.
 
s16 cos_12 (u32 th)
 Calculate the cosine of an angle using a 12-bit angle representation.
 
fx8 rad_cos_12 (fx8 rad, u32 th)
 Calculate the cosine of an angle and scale a fixed-point value by it.
 
fx8 rad_sin_12 (fx8 rad, u32 th)
 Calculate the sine of an angle and scale a fixed-point value by it.
 
fx8 genrand_min_max_fx8 (fx8 min_, fx8 max_)
 Generate a random fixed-point number within a specified range.
 
uint32_t qmod (uint32_t x, uint32_t N)
 Computes the remainder of x divided by N using an optimized method.
 
uint32_t qdiv (uint32_t x, uint32_t N)
 Computes the quotient of x divided by N using an optimized method.
 

Variables

constexpr fx8 FX8_E = fx8(696, 256)
 
constexpr fx8 FX8_LOG2E = fx8(369, 256)
 
constexpr fx8 FX8_LOG10E = fx8(111, 256)
 
constexpr fx8 FX8_LN2 = fx8(178, 256)
 
constexpr fx8 FX8_LN10 = fx8(590, 256)
 
constexpr fx8 FX8_1_PI = fx8(82, 256)
 
constexpr fx8 FX8_2_PI = fx8(163, 256)
 
constexpr fx8 FX8_2_SQRTPI = fx8(289, 256)
 
constexpr fx8 FX8_SQRT2 = fx8(362, 256)
 
constexpr fx8 FX8_SQRT1_2 = fx8(181, 256)
 
constexpr fx8 FX8_2PI = fx8(1608, 256)
 
constexpr fx8 FX8_PI = fx8(804, 256)
 
constexpr fx8 FX8_PI_2 = fx8(402, 256)
 
constexpr fx8 FX8_PI_3 = fx8(268, 256)
 
constexpr fx8 FX8_PI_4 = fx8(201, 256)
 
constexpr fx8 FX8_PI_5 = fx8(161, 256)
 
constexpr fx8 FX8_PI_6 = fx8(134, 256)
 
constexpr fx8 FX8_PI_7 = fx8(115, 256)
 
constexpr fx8 FX8_PI_8 = fx8(100, 256)
 
constexpr fx8 FX8_PI_9 = fx8(89, 256)
 
constexpr fx8 FX8_PI_10 = fx8(80, 256)
 
constexpr fx8 FX8_PI_11 = fx8(73, 256)
 
constexpr fx8 FX8_PI_12 = fx8(67, 256)
 
constexpr fx8 FX8_PI_13 = fx8(62, 256)
 
constexpr fx8 FX8_PI_14 = fx8(57, 256)
 
constexpr fx8 FX8_PI_15 = fx8(54, 256)
 
constexpr fx8 FX8_PI_16 = fx8(50, 256)
 
constexpr fx12 FX12_E = fx12(11135, 4096)
 
constexpr fx12 FX12_LOG2E = fx12(5912, 4096)
 
constexpr fx12 FX12_LOG10E = fx12(1785, 4096)
 
constexpr fx12 FX12_LN2 = fx12(2831, 4096)
 
constexpr fx12 FX12_LN10 = fx12(9438, 4096)
 
constexpr fx12 FX12_1_PI = fx12(1317, 4096)
 
constexpr fx12 FX12_2_PI = fx12(2634, 4096)
 
constexpr fx12 FX12_2_SQRTPI = fx12(4613, 4096)
 
constexpr fx12 FX12_SQRT2 = fx12(5793, 4096)
 
constexpr fx12 FX12_SQRT1_2 = fx12(2896, 4096)
 
constexpr fx12 FX12_PI = fx12(12868, 4096)
 
constexpr fx12 FX12_PI_2 = fx12(6434, 4096)
 
constexpr fx12 FX12_PI_3 = fx12(4290, 4096)
 
constexpr fx12 FX12_PI_4 = fx12(3217, 4096)
 
constexpr fx12 FX12_PI_5 = fx12(2574, 4096)
 
constexpr fx12 FX12_PI_6 = fx12(2145, 4096)
 
constexpr fx12 FX12_PI_7 = fx12(1838, 4096)
 
constexpr fx12 FX12_PI_8 = fx12(1609, 4096)
 
constexpr fx12 FX12_PI_9 = fx12(1432, 4096)
 
constexpr fx12 FX12_PI_10 = fx12(1287, 4096)
 
constexpr fx12 FX12_PI_11 = fx12(1171, 4096)
 
constexpr fx12 FX12_PI_12 = fx12(1072, 4096)
 
constexpr fx12 FX12_PI_13 = fx12(989, 4096)
 
constexpr fx12 FX12_PI_14 = fx12(919, 4096)
 
constexpr fx12 FX12_PI_15 = fx12(858, 4096)
 
constexpr fx12 FX12_PI_16 = fx12(804, 4096)
 

Detailed Description

Provides mathematical functions and utilities, primarily focused on fixed-point arithmetic.

This module offers a set of mathematical functions and utilities, including trigonometric calculations, fixed-point number operations, and random number generation using the Mersenne Twister algorithm. It is particularly useful in resource-constrained environments, such as embedded systems or game development.

The module includes:

  • Trigonometric functions using a 12-bit angle representation
  • Random number generation within a specified range for fixed-point numbers
  • Mathematical constants like M_PI, M_E, etc.
  • Utility functions for fixed-point arithmetic
  • A structure for representing rectangles with fixed-point coordinates

Note: This module relies on fixed-point number representations using the fx8 and fx12 types.

Example usage:

#include <submath.h>
#include <iostream>
int main() {
// Generate a random fixed-point number within a specified range
fx8 min_value(1.0);
fx8 max_value(10.0);
fx8 random_value = genrand_min_max_fx8(min_value, max_value);
std::cout << "Random value: " << random_value << std::endl;
// Calculate sine and cosine for a given angle
u32 angle = 1024; // 90 degrees
fx8 radius(5.0);
fx8 sin_result = rad_sin_12(radius, angle);
fx8 cos_result = rad_cos_12(radius, angle);
std::cout << "sin(90 degrees): " << sin_result << std::endl;
std::cout << "cos(90 degrees): " << cos_result << std::endl;
return 0;
}
Provides mathematical functions and utilities, primarily focused on fixed-point arithmetic.
fx8 rad_sin_12(fx8 rad, u32 th)
Calculate the sine of an angle and scale a fixed-point value by it.
Definition submath.cpp:1052
fx8 genrand_min_max_fx8(fx8 min_, fx8 max_)
Generate a random fixed-point number within a specified range.
Definition submath.cpp:1060
fx8 rad_cos_12(fx8 rad, u32 th)
Calculate the cosine of an angle and scale a fixed-point value by it.
Definition submath.cpp:1044

Typedef Documentation

◆ fx12

using fx12 = fpm::fixed<std::int32_t, std::int64_t, 12>

Alias for fixed-point type with 12 fractional bits.

This type represents a fixed-point number with 12 bits for the fractional part, using a 32-bit integer for storage and a 64-bit integer for intermediate calculations.

◆ fx8

using fx8 = fpm::fixed<std::int32_t, std::int64_t, 8>

Alias for fixed-point type with 8 fractional bits.

This type represents a fixed-point number with 8 bits for the fractional part, using a 32-bit integer for storage and a 64-bit integer for intermediate calculations.

Function Documentation

◆ _abs()

const fx8 _abs ( const fx8 & x_)
inline

Get the absolute value of a fixed-point number.

Parameters
x_The value.
Returns
The absolute value.

◆ _lim()

const fx8 & _lim ( const fx8 & x_,
const fx8 & lhs_,
const fx8 & rhs_ )
inline

Limit a fixed-point value within a specified range.

Parameters
x_The value to limit.
lhs_The lower bound.
rhs_The upper bound.
Returns
The limited value.

◆ _max()

const fx8 & _max ( const fx8 & lhs_,
const fx8 & rhs_ )
inline

Get the maximum of two fixed-point values.

Parameters
lhs_The first value.
rhs_The second value.
Returns
The maximum value.

◆ _min()

const fx8 & _min ( const fx8 & lhs_,
const fx8 & rhs_ )
inline

Get the minimum of two fixed-point values.

Parameters
lhs_The first value.
rhs_The second value.
Returns
The minimum value.

◆ cos_12()

s16 cos_12 ( u32 th)
extern

Calculate the cosine of an angle using a 12-bit angle representation.

Parameters
thThe angle in 12-bit representation.
Returns
The cosine of the angle.

◆ genrand_min_max_fx8()

fx8 genrand_min_max_fx8 ( fx8 min_,
fx8 max_ )
extern

Generate a random fixed-point number within a specified range.

Parameters
min_The minimum value.
max_The maximum value.
Returns
The random fixed-point number within the range.

◆ lim()

template<typename T , typename U , typename V >
T lim ( const T & x_,
const U & lhs_,
const V & rhs_ )
inline

Limit a value within a specified range.

Template Parameters
TThe type of the value.
UThe type of the lower bound.
VThe type of the upper bound.
Parameters
x_The value to limit.
lhs_The lower bound.
rhs_The upper bound.
Returns
The limited value.

◆ qdiv()

uint32_t qdiv ( uint32_t x,
uint32_t N )
extern

Computes the quotient of x divided by N using an optimized method.

This function calculates the quotient (integer division result) of x / N efficiently using different strategies based on the value of the divisor N:

  • If N is zero, the function returns zero to avoid division by zero errors.
  • If N is a power of two, the quotient is computed directly using x / N.
  • If N is less than 128, a precomputed reciprocal table (inv_tbl) is employed to approximate the division through fixed-point multiplication.
  • If N is 128 or greater, the function falls back to the standard division operator.
Parameters
xThe dividend.
NThe divisor (should be greater than zero).
Returns
The quotient of x divided by N.
Note
This function is designed to optimize division by avoiding slow division instructions whenever possible, leveraging bitwise and multiplication-based methods.

◆ qmod()

uint32_t qmod ( uint32_t x,
uint32_t N )
extern

Computes the remainder of x divided by N using an optimized method.

This function calculates x % N efficiently using different methods based on the divisor N:

  • If N is zero, the function returns zero to prevent division by zero errors.
  • If N is a power of two, the remainder is computed using x & (N - 1), which is significantly faster than division.
  • If N < 128, a precomputed reciprocal table (inv_tbl) is used to approximate the division, leveraging fixed-point multiplication.
  • If N >= 128, the function directly falls back to the standard % operator.
Parameters
xThe dividend.
NThe divisor (must be greater than zero).
Returns
The remainder of x divided by N (x % N).
Note
This function optimizes the modulo operation by avoiding slow division instructions whenever possible, making use of bitwise and multiplication-based methods.

◆ rad_cos_12()

fx8 rad_cos_12 ( fx8 rad,
u32 th )
extern

Calculate the cosine of an angle and scale a fixed-point value by it.

Parameters
radThe fixed-point value to scale.
thThe angle in 12-bit representation.
Returns
The scaled fixed-point value.

◆ rad_sin_12()

fx8 rad_sin_12 ( fx8 rad,
u32 th )
extern

Calculate the sine of an angle and scale a fixed-point value by it.

Parameters
radThe fixed-point value to scale.
thThe angle in 12-bit representation.
Returns
The scaled fixed-point value.

◆ sin_12()

s16 sin_12 ( u32 th)
extern

Calculate the sine of an angle using a 12-bit angle representation.

Parameters
thThe angle in 12-bit representation.
Returns
The sine of the angle.