summaryrefslogtreecommitdiff
path: root/apps
diff options
context:
space:
mode:
authorFranklin Wei <git@fwei.tk>2017-01-16 11:38:18 -0500
committerFranklin Wei <me@fwei.tk>2017-01-16 20:54:56 +0100
commit0056ea8a256af0e2daaf451af43c00708a31d4df (patch)
treed667180ccccef14eccdbe263e518aad8d8bfdb23 /apps
parent1b882cb156eac8bdfa132baa5226e3f8038841b7 (diff)
downloadrockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.tar.gz
rockbox-0056ea8a256af0e2daaf451af43c00708a31d4df.zip
puzzles: more accurate sin() and cos()
- now uses fp_sincos() Change-Id: I20c8224cac98fc677097161737d25dd9038bede2
Diffstat (limited to 'apps')
-rw-r--r--apps/plugins/puzzles/rbwrappers.c43
1 files changed, 36 insertions, 7 deletions
diff --git a/apps/plugins/puzzles/rbwrappers.c b/apps/plugins/puzzles/rbwrappers.c
index ae249a0c93..e595cf93d1 100644
--- a/apps/plugins/puzzles/rbwrappers.c
+++ b/apps/plugins/puzzles/rbwrappers.c
@@ -22,24 +22,53 @@ int puts_wrapper(const char *s)
22} 22}
23 23
24/* fixed-point wrappers */ 24/* fixed-point wrappers */
25static long lastphase = 0, lastsin = 0, lastcos = 0x7fffffff;
26
25double sin_wrapper(double rads) 27double sin_wrapper(double rads)
26{ 28{
27 int degs = rads * 180/PI; 29 /* we want [0, 2*PI) */
28 long fixed = fp14_sin(degs); 30 while(rads >= 2*PI)
29 return fixed / (16384.0); 31 rads -= 2*PI;
32 while(rads < 0)
33 rads += 2*PI;
34
35 unsigned long phase = rads/(2*PI) * 4294967296.0;
36
37 /* caching */
38 if(phase == lastphase)
39 {
40 return lastsin/(lastsin < 0 ? 2147483648.0 : 2147483647.0);
41 }
42
43 lastphase = phase;
44 lastsin = fp_sincos(phase, &lastcos);
45 return lastsin/(lastsin < 0 ? 2147483648.0 : 2147483647.0);
30} 46}
31 47
32double cos_wrapper(double rads) 48double cos_wrapper(double rads)
33{ 49{
34 int degs = rads * 180/PI; 50 /* we want [0, 2*PI) */
35 long fixed = fp14_cos(degs); 51 while(rads >= 2*PI)
36 return fixed / (16384.0); 52 rads -= 2*PI;
53 while(rads < 0)
54 rads += 2*PI;
55
56 unsigned long phase = rads/(2*PI) * 4294967296.0;
57
58 /* caching */
59 if(phase == lastphase)
60 {
61 return lastcos/(lastcos < 0 ? 2147483648.0 : 2147483647.0);
62 }
63
64 lastphase = phase;
65 lastsin = fp_sincos(phase, &lastcos);
66 return lastcos/(lastcos < 0 ? 2147483648.0 : 2147483647.0);
37} 67}
38 68
39int vsprintf_wrapper(char *s, const char *fmt, va_list ap) 69int vsprintf_wrapper(char *s, const char *fmt, va_list ap)
40{ 70{
41 return rb->vsnprintf(s, 9999, fmt, ap); 71 return rb->vsnprintf(s, 9999, fmt, ap);
42
43} 72}
44 73
45/* Absolute value, simple calculus */ 74/* Absolute value, simple calculus */