slightly smaller fixSin() function (just 14 bytes, maybe they make a difference?)

This commit is contained in:
Christian Kroll 2012-05-05 03:44:58 +00:00
parent af8a928123
commit 0705564d6d
1 changed files with 31 additions and 12 deletions

View File

@ -211,23 +211,42 @@ inline static fixp_t fixDiv(fixp_interim_t const a, fixp_interim_t const b)
* @param angle fixed point radian value * @param angle fixed point radian value
* @return Result of the sine function normalized to a range from -FIX to FIX. * @return Result of the sine function normalized to a range from -FIX to FIX.
*/ */
static fixp_t fixSin(fixp_t const fAngle) static fixp_t fixSin(fixp_t fAngle)
{ {
// convert given angle to its corresponding lookup table quantization step // convert given fixed-point angle to its corresponding quantization step
ordinary_int_t nNormAng = fAngle / FIX_SIN_DIVIDER; int8_t nSign = 1;
// trim that value so that it fits into a range between [0, FIX_SIN_COUNT] if (fAngle < 0)
nNormAng = (nNormAng - (nNormAng / FIX_SIN_COUNT * FIX_SIN_COUNT) +
FIX_SIN_COUNT) % FIX_SIN_COUNT;
uint8_t nIndex = nNormAng % (FIX_SIN_COUNT / 2);
if (nIndex >= (FIX_SIN_COUNT / 4))
{ {
nIndex = (FIX_SIN_COUNT / 2 - 1) - nIndex; // take advantage of sin(-x) == -sin(x) to avoid neg. operands for "%"
fAngle = -fAngle;
nSign = -1;
}
uint8_t nIndex = (fAngle / FIX_SIN_DIVIDER) % FIX_SIN_COUNT;
// now convert that quantization step to an index of our quartered array
if ((nIndex >= (FIX_SIN_COUNT / 4)))
{
if (nIndex < (FIX_SIN_COUNT / 2))
{
nIndex = (FIX_SIN_COUNT / 2 - 1) - nIndex;
}
else
{
// an angle > PI means that we have to toggle the sign of the result
nSign *= -1;
if (nIndex < (FIX_SIN_COUNT - (FIX_SIN_COUNT / 4)))
{
nIndex = nIndex - (FIX_SIN_COUNT / 2);
}
else
{
nIndex = (FIX_SIN_COUNT - 1) - nIndex;
}
}
} }
assert(nIndex < (FIX_SIN_COUNT / 4)); assert(nIndex < (FIX_SIN_COUNT / 4));
return ((fixp_t)fix_sine_lut[nIndex]) * return ((fixp_t)fix_sine_lut[nIndex]) * nSign;
(nNormAng < (FIX_SIN_COUNT / 2) ? 1 : -1);
} }