It's possibly to compute a good approximation to the cosine of a number using a single float multiplication and 1 integer multiplication.
The implementation from this video for example
https://www.youtube.com/watch?v=hffgNRfL1XY
(see e.g. 8:02 )
can be modified to use only integers except for the very last step.
Example:
#define SECOND_ORDER_COEFFICIENT 0.0000000010911122665310369f
uint32 a=3667592205
float b=1.f/a ( can be evaluated at compile time)
sint32 c= (sint32) (SECOND_ORDER_COEFFICIENT*a) (also compile time)
uint32 x=16382 (0 is 0 angle and 16382 is pi/4 ), values lower than -pi/4 or greater than pi/4 need to be done with symmetry transforms or the squareroot operation.
float result=b * (a-c*x*x)
the compiler optimizes this a bit to turn 2 integer multiplies +1 float into 1 integer multiplications and 1 float multiplication.
There are other possible values for a that can be chosen, potentially with less error than the one I used for this example, especially if the range of x is changed. The float multiply could also be replaced with a hardware divide if the angle doesnt have to be a float.
The current implementation seems to use a 8th order polynomial with doubles.
I'm also a bit confused as to why p[0] seems to be unused here.
It's possibly to compute a good approximation to the cosine of a number using a single float multiplication and 1 integer multiplication.
The implementation from this video for example
https://www.youtube.com/watch?v=hffgNRfL1XY
(see e.g. 8:02 )
can be modified to use only integers except for the very last step.
Example:
the compiler optimizes this a bit to turn 2 integer multiplies +1 float into 1 integer multiplications and 1 float multiplication.
There are other possible values for a that can be chosen, potentially with less error than the one I used for this example, especially if the range of x is changed. The float multiply could also be replaced with a hardware divide if the angle doesnt have to be a float.
The current implementation seems to use a 8th order polynomial with doubles.
sm64/lib/src/math/cosf.c
Line 36 in a4b5e94
I'm also a bit confused as to why p[0] seems to be unused here.