libdragon
|
Fast math routines, optimized for 3D graphics calculations. More...
Macros | |
#define | LIKELY(x) __builtin_expect((x),1) |
Mark a branch as likely to be taken. | |
Functions | |
float | fm_sinf_approx (float x, int approx) |
Faster version of sinf, with tunable approximation level. | |
float | fm_sinf (float x) |
Faster version of sinf. | |
float | fm_cosf (float x) |
Faster version of cosf. | |
void | fm_sincosf (float x, float *sin, float *cos) |
Faster version of sincosf. | |
float | fm_atan2f (float y, float x) |
Faster version of atan2f. | |
Fast math routines, optimized for 3D graphics calculations.
float fm_sinf_approx | ( | float | x, |
int | approx | ||
) |
Faster version of sinf, with tunable approximation level.
This function is similar to fm_sinf, but allows to further speedup the approximation by reducing the number of calculated terms. fm_sinf in fact is pretty accurate (~ 5 ULP) but some times much less precision is required.
The approximation level is a number between 0 and 5, where 0 is the most accurate version (identical to fm_sinf) and 5 is the fastest one. We do not give mathematical guarantees on the accuracy of the approximation, and we suggest on holistic approach (try and see if it works for you).
This function is suggested in all cases in which you need to visually reproduce a "sinewave" effect, but you do not care about the exact numbers behind it. For trigonemetric formulas that includes a sine (eg: matrix rotations), it is suggested to use fm_sinf instead.
x | The angle in radians |
approx | The approximation level, between 0 and 5 |
float fm_sinf | ( | float | x | ) |
Faster version of sinf.
This function computes a very accurate approximation of the sine of a floating point number, as long as the argument as a small magnitude. Do not use this function with very large (positive or negative) numbers as the accuracy decreases. Normally, it is not necessary in graphics programming to compute trigonometric functions on angles of unbounded magnitude.
The functions runs in about ~50 ticks, versus ~800 ticks of the newlib version. The accuracy in the range [-π, +π] is within 5 ULP of the correct result, but the argument reduction to bring the argument in that range introduces errors which increase with the magnitude of the operand.
float fm_cosf | ( | float | x | ) |
Faster version of cosf.
void fm_sincosf | ( | float | x, |
float * | sin, | ||
float * | cos | ||
) |
float fm_atan2f | ( | float | y, |
float | x | ||
) |
Faster version of atan2f.
Given a point (x,y), return the angle in radians that the vector (x,y) forms with the X axis. This is the same of arctan(y/x).
This function runs in about ~XX ticks, versus ~YY ticks of the newlib version. The maximum measured error is ~6.14e-4, which is usually more than enough in the context of angles.