Skip to content

Commit

Permalink
fmath: add fm_sincosf
Browse files Browse the repository at this point in the history
  • Loading branch information
rasky committed Sep 11, 2023
1 parent ed902d1 commit 8de556e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 18 additions & 5 deletions include/fmath.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,23 @@ float fm_sinf_approx(float x, int approx);
/**
* @brief Faster version of cosf.
*
* @see dragon_sinf for considerations on why and how to use this functions
* @see #fm_sinf for considerations on why and how to use this functions
* instead of the standard sinf.
*/
float fm_cosf(float x);

/**
* @brief Faster version of sincosf.
*
* Similar to #fm_sinf and #fm_cosf, but calculates both sinf and cosf
* for the same angle.
*
* @param x The angle in radians
* @param sin The pointer to the variable that will contain the sine
* @param cos The pointer to the variable that will contain the cosine
*/
void fm_sincosf(float x, float *sin, float *cos);

/**
* @brief Faster version of atan2f.
*
Expand All @@ -199,10 +211,11 @@ float fm_atan2f(float y, float x);
// we expect that to be resolved at compile-time with the maximum accuracy,
// so it's important to generate code that calls the standard C function
// which is then intrinsified by the compiler.
#define fmodf(x, y) ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? fmodf(x,y) : fm_fmodf(x,y))
#define sinf(x) (__builtin_constant_p(x) ? sinf(x) : fm_sinf(x))
#define cosf(x) (__builtin_constant_p(x) ? cosf(x) : fm_cosf(x))
#define atan2f(y, x) ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? atan2f(y, x) : fm_atan2f(y, x))
#define fmodf(x, y) ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? fmodf(x,y) : fm_fmodf(x,y))
#define sinf(x) (__builtin_constant_p(x) ? sinf(x) : fm_sinf(x))
#define cosf(x) (__builtin_constant_p(x) ? cosf(x) : fm_cosf(x))
#define sincosf(x,s,c) (__builtin_constant_p(x) ? sincosf(x,s,c) : fm_sincosf(x,s,c))
#define atan2f(y, x) ((__builtin_constant_p(x) && __builtin_constant_p(y)) ? atan2f(y, x) : fm_atan2f(y, x))
#endif

#ifdef __cplusplus
Expand Down
6 changes: 6 additions & 0 deletions src/fmath.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ float fm_cosf(float x) {
return fm_sinf(half_pi_hi - x);
}

void fm_sincosf(float x, float *sin, float *cos) {
float y = fm_sinf_approx(x, 0);
*sin = y;
*cos = sqrtf(1.0f - y * y);
}

float fm_atan2f(float y, float x) {
// Approximation of atan2f using a polynomial minmax approximation in [0,1]
// calculated via the Remez algorithm (https://math.stackexchange.com/a/1105038).
Expand Down

0 comments on commit 8de556e

Please sign in to comment.