diff options
| author | Franklin Wei <git@fwei.tk> | 2017-01-16 11:38:18 -0500 |
|---|---|---|
| committer | Franklin Wei <me@fwei.tk> | 2017-01-16 20:54:56 +0100 |
| commit | 0056ea8a256af0e2daaf451af43c00708a31d4df (patch) | |
| tree | d667180ccccef14eccdbe263e518aad8d8bfdb23 /apps/plugins/puzzles | |
| parent | 1b882cb156eac8bdfa132baa5226e3f8038841b7 (diff) | |
| download | rockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.zip rockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.tar.gz rockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.tar.bz2 rockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.tar.xz | |
puzzles: more accurate sin() and cos()
- now uses fp_sincos()
Change-Id: I20c8224cac98fc677097161737d25dd9038bede2
Diffstat (limited to 'apps/plugins/puzzles')
| -rw-r--r-- | apps/plugins/puzzles/rbwrappers.c | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/apps/plugins/puzzles/rbwrappers.c b/apps/plugins/puzzles/rbwrappers.c index ae249a0..e595cf9 100644 --- a/apps/plugins/puzzles/rbwrappers.c +++ b/apps/plugins/puzzles/rbwrappers.c @@ -22,24 +22,53 @@ int puts_wrapper(const char *s) } /* fixed-point wrappers */ +static long lastphase = 0, lastsin = 0, lastcos = 0x7fffffff; + double sin_wrapper(double rads) { - int degs = rads * 180/PI; - long fixed = fp14_sin(degs); - return fixed / (16384.0); + /* we want [0, 2*PI) */ + while(rads >= 2*PI) + 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) { - int degs = rads * 180/PI; - long fixed = fp14_cos(degs); - return fixed / (16384.0); + /* we want [0, 2*PI) */ + while(rads >= 2*PI) + 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) { return rb->vsnprintf(s, 9999, fmt, ap); - } /* Absolute value, simple calculus */ |