summaryrefslogtreecommitdiff
path: root/n/avr/modules/math/fixed
diff options
context:
space:
mode:
Diffstat (limited to 'n/avr/modules/math/fixed')
-rw-r--r--n/avr/modules/math/fixed/Makefile.module3
-rw-r--r--n/avr/modules/math/fixed/fixed.h8
-rw-r--r--n/avr/modules/math/fixed/fixed_cosin_f824.c81
-rw-r--r--n/avr/modules/math/fixed/test/test_fixed.c17
4 files changed, 108 insertions, 1 deletions
diff --git a/n/avr/modules/math/fixed/Makefile.module b/n/avr/modules/math/fixed/Makefile.module
index 91b409b..f32d94a 100644
--- a/n/avr/modules/math/fixed/Makefile.module
+++ b/n/avr/modules/math/fixed/Makefile.module
@@ -1 +1,2 @@
-math_fixed_SOURCES = fixed_mul_f824.avr.S fixed_div_f824.avr.S
+math_fixed_SOURCES = fixed_mul_f824.avr.S fixed_div_f824.avr.S \
+ fixed_cosin_f824.c
diff --git a/n/avr/modules/math/fixed/fixed.h b/n/avr/modules/math/fixed/fixed.h
index 95b1766..ce3138f 100644
--- a/n/avr/modules/math/fixed/fixed.h
+++ b/n/avr/modules/math/fixed/fixed.h
@@ -49,4 +49,12 @@ fixed_div_f824 (int32_t a, int32_t b);
#endif
+/** Compute cosinus, angle f8.24, result f8.24. */
+int32_t
+fixed_cos_f824 (int32_t a);
+
+/** Compute sinus, angle f8.24, result f8.24. */
+int32_t
+fixed_sin_f824 (int32_t a);
+
#endif /* fixed_h */
diff --git a/n/avr/modules/math/fixed/fixed_cosin_f824.c b/n/avr/modules/math/fixed/fixed_cosin_f824.c
new file mode 100644
index 0000000..d5148f4
--- /dev/null
+++ b/n/avr/modules/math/fixed/fixed_cosin_f824.c
@@ -0,0 +1,81 @@
+/* fixed_cosin_f824.c */
+/* avr.math.fixed - Fixed point math module. {{{
+ *
+ * Copyright (C) 2005 Nicolas Schodet
+ *
+ * Robot APB Team/Efrei 2006.
+ * Web: http://assos.efrei.fr/robot/
+ * Email: robot AT efrei DOT fr
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ * }}} */
+#include "common.h"
+#include "fixed.h"
+
+/** Compute cosinus for angles between [0,pi/2]. */
+static int32_t
+fixed_cos_dli (int32_t a)
+{
+ static const int32_t f[] = {
+ (1L << 24) * -26.42625678337439745096,
+ (1L << 24) * 60.24464137187666035919,
+ (1L << 24) * -85.45681720669372773226,
+ (1L << 24) * 64.93939402266829148905,
+ (1L << 24) * -19.73920880217871723738,
+ (1L << 24) * 1
+ };
+ int32_t r;
+ int32_t a2;
+ uint8_t i;
+ a2 = fixed_mul_f824 (a, a);
+ r = f[0];
+ for (i = 1; i < sizeof (f) / sizeof (f[0]); i++)
+ r = fixed_mul_f824 (r, a2) + f[i];
+ return r;
+}
+
+/** Compute cosinus, angle f8.24, result f8.24. */
+int32_t
+fixed_cos_f824 (int32_t a)
+{
+ a &= (1L << 24) - 1;
+ uint8_t z = ((uint32_t) a >> 16) & 0xc0;
+ if (z == 0)
+ return fixed_cos_dli (a);
+ else if (z == 1 << 6)
+ return -fixed_cos_dli ((1L << 23) - a);
+ else if (z == 2 << 6)
+ return -fixed_cos_dli (a & 0xff7fffff);
+ else
+ return fixed_cos_dli ((1L << 24) - a);
+}
+
+/** Compute sinus, angle f8.24, result f8.24. */
+int32_t
+fixed_sin_f824 (int32_t a)
+{
+ a &= (1L << 24) - 1;
+ uint8_t z = ((uint32_t) a >> 16) & 0xc0;
+ if (z == 0)
+ return fixed_cos_dli ((1L << 22) - a);
+ else if (z == 1 << 6)
+ return fixed_cos_dli (a - (1L << 22));
+ else if (z == 2 << 6)
+ return -fixed_cos_dli ((1L << 23) + (1L << 22) - a);
+ else
+ return -fixed_cos_dli (a - (1L << 23) - (1L << 22));
+}
+
diff --git a/n/avr/modules/math/fixed/test/test_fixed.c b/n/avr/modules/math/fixed/test/test_fixed.c
index d19f63a..9f04880 100644
--- a/n/avr/modules/math/fixed/test/test_fixed.c
+++ b/n/avr/modules/math/fixed/test/test_fixed.c
@@ -98,6 +98,23 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]);
}
break;
+ case c ('c', 0):
+ for (al = 0; al < (1L << 24); al += 1 << 8)
+ {
+ proto_send1d ('a', al);
+ rl[0] = fixed_cos_f824 (al);
+ rl[1] = fixed_sin_f824 (al);
+ proto_send2d ('r', rl[0], rl[1]);
+ }
+ for (i = 0; i < 64000; i++)
+ {
+ al = random_u32 () & 0xffffff;
+ proto_send1d ('a', al);
+ rl[0] = fixed_cos_f824 (al);
+ rl[1] = fixed_sin_f824 (al);
+ proto_send2d ('r', rl[0], rl[1]);
+ }
+ break;
default:
proto_send0 ('?');
return;