mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-27 15:56:18 -04:00
puzzles: more accurate sin() and cos()
- now uses fp_sincos() Change-Id: I20c8224cac98fc677097161737d25dd9038bede2
This commit is contained in:
parent
1b882cb156
commit
0056ea8a25
1 changed files with 36 additions and 7 deletions
|
|
@ -22,24 +22,53 @@ int puts_wrapper(const char *s)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fixed-point wrappers */
|
/* fixed-point wrappers */
|
||||||
|
static long lastphase = 0, lastsin = 0, lastcos = 0x7fffffff;
|
||||||
|
|
||||||
double sin_wrapper(double rads)
|
double sin_wrapper(double rads)
|
||||||
{
|
{
|
||||||
int degs = rads * 180/PI;
|
/* we want [0, 2*PI) */
|
||||||
long fixed = fp14_sin(degs);
|
while(rads >= 2*PI)
|
||||||
return fixed / (16384.0);
|
rads -= 2*PI;
|
||||||
|
while(rads < 0)
|
||||||
|
rads += 2*PI;
|
||||||
|
|
||||||
|
unsigned long phase = rads/(2*PI) * 4294967296.0;
|
||||||
|
|
||||||
|
/* caching */
|
||||||
|
if(phase == lastphase)
|
||||||
|
{
|
||||||
|
return lastsin/(lastsin < 0 ? 2147483648.0 : 2147483647.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastphase = phase;
|
||||||
|
lastsin = fp_sincos(phase, &lastcos);
|
||||||
|
return lastsin/(lastsin < 0 ? 2147483648.0 : 2147483647.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
double cos_wrapper(double rads)
|
double cos_wrapper(double rads)
|
||||||
{
|
{
|
||||||
int degs = rads * 180/PI;
|
/* we want [0, 2*PI) */
|
||||||
long fixed = fp14_cos(degs);
|
while(rads >= 2*PI)
|
||||||
return fixed / (16384.0);
|
rads -= 2*PI;
|
||||||
|
while(rads < 0)
|
||||||
|
rads += 2*PI;
|
||||||
|
|
||||||
|
unsigned long phase = rads/(2*PI) * 4294967296.0;
|
||||||
|
|
||||||
|
/* caching */
|
||||||
|
if(phase == lastphase)
|
||||||
|
{
|
||||||
|
return lastcos/(lastcos < 0 ? 2147483648.0 : 2147483647.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastphase = phase;
|
||||||
|
lastsin = fp_sincos(phase, &lastcos);
|
||||||
|
return lastcos/(lastcos < 0 ? 2147483648.0 : 2147483647.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int vsprintf_wrapper(char *s, const char *fmt, va_list ap)
|
int vsprintf_wrapper(char *s, const char *fmt, va_list ap)
|
||||||
{
|
{
|
||||||
return rb->vsnprintf(s, 9999, fmt, ap);
|
return rb->vsnprintf(s, 9999, fmt, ap);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Absolute value, simple calculus */
|
/* Absolute value, simple calculus */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue