summaryrefslogtreecommitdiff
path: root/n/avr/modules/math/fixed
diff options
context:
space:
mode:
authorschodet2005-08-18 19:57:13 +0000
committerschodet2005-08-18 19:57:13 +0000
commit0267a146f28e7f43d6499159911ffcbb4cb69155 (patch)
treee08d165f7f585bb13fc74ecbbd74c49558d4acd1 /n/avr/modules/math/fixed
parenta9573e8c2aee1aeff25c1a49a030a99e939acaff (diff)
Ajout du calcul de racine carré.
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.h20
-rw-r--r--n/avr/modules/math/fixed/fixed_sqrt_uf248.c48
-rw-r--r--n/avr/modules/math/fixed/fixed_sqrt_ui32.c48
-rw-r--r--n/avr/modules/math/fixed/test/Makefile2
-rw-r--r--n/avr/modules/math/fixed/test/test_fixed.c19
6 files changed, 138 insertions, 2 deletions
diff --git a/n/avr/modules/math/fixed/Makefile.module b/n/avr/modules/math/fixed/Makefile.module
index f32d94a..ed7e89f 100644
--- a/n/avr/modules/math/fixed/Makefile.module
+++ b/n/avr/modules/math/fixed/Makefile.module
@@ -1,2 +1,3 @@
math_fixed_SOURCES = fixed_mul_f824.avr.S fixed_div_f824.avr.S \
- fixed_cosin_f824.c
+ fixed_cosin_f824.c \
+ fixed_sqrt_uf248.c fixed_sqrt_ui32.c
diff --git a/n/avr/modules/math/fixed/fixed.h b/n/avr/modules/math/fixed/fixed.h
index ce3138f..9c031dc 100644
--- a/n/avr/modules/math/fixed/fixed.h
+++ b/n/avr/modules/math/fixed/fixed.h
@@ -25,6 +25,18 @@
*
* }}} */
+/** Numbers notation:
+ * [u]{i|f}x[.y]
+ * u: unsigned
+ * i: integer
+ * f: fixed point
+ * x: integral part size in bits
+ * y: fractionnal part size in bits
+ *
+ * Ex: i16: signed 16 bit word, uf8.8: unsigned fixed 8.8.
+ *
+ * Angles are mapped from [0, 2pi) to [0,1). */
+
#ifndef HOST
/** Multiply f8.24 by f8.24, return f8.24. */
@@ -57,4 +69,12 @@ fixed_cos_f824 (int32_t a);
int32_t
fixed_sin_f824 (int32_t a);
+/** Compute square root, uf24.8. */
+uint32_t
+fixed_sqrt_uf248 (uint32_t x);
+
+/** Compute square root, ui32 -> ui16. */
+uint16_t
+fixed_sqrt_ui32 (uint32_t x);
+
#endif /* fixed_h */
diff --git a/n/avr/modules/math/fixed/fixed_sqrt_uf248.c b/n/avr/modules/math/fixed/fixed_sqrt_uf248.c
new file mode 100644
index 0000000..6d67312
--- /dev/null
+++ b/n/avr/modules/math/fixed/fixed_sqrt_uf248.c
@@ -0,0 +1,48 @@
+/* fixed_sqrt_uf248.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 square root, uf24.8. */
+uint32_t
+fixed_sqrt_uf248 (uint32_t x)
+{
+ uint32_t root, bit, test;
+ root = 0;
+ bit = 1L << 30;
+ do
+ {
+ test = root + bit;
+ if (x >= test)
+ {
+ x -= test;
+ root = test + bit;
+ }
+ root >>= 1;
+ bit >>= 2;
+ } while (bit);
+ return root << 4;
+}
+
diff --git a/n/avr/modules/math/fixed/fixed_sqrt_ui32.c b/n/avr/modules/math/fixed/fixed_sqrt_ui32.c
new file mode 100644
index 0000000..88de584
--- /dev/null
+++ b/n/avr/modules/math/fixed/fixed_sqrt_ui32.c
@@ -0,0 +1,48 @@
+/* fixed_sqrt_ui32.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 square root, ui32 -> ui16. */
+uint16_t
+fixed_sqrt_ui32 (uint32_t x)
+{
+ uint32_t root, bit, test;
+ root = 0;
+ bit = 1L << 30;
+ do
+ {
+ test = root + bit;
+ if (x >= test)
+ {
+ x -= test;
+ root = test + bit;
+ }
+ root >>= 1;
+ bit >>= 2;
+ } while (bit);
+ return root;
+}
+
diff --git a/n/avr/modules/math/fixed/test/Makefile b/n/avr/modules/math/fixed/test/Makefile
index 23be911..38517e5 100644
--- a/n/avr/modules/math/fixed/test/Makefile
+++ b/n/avr/modules/math/fixed/test/Makefile
@@ -6,7 +6,7 @@ EXTRACTDOC =
MODULES = uart proto utils math/fixed math/random
CONFIGFILE = avrconfig.h
# atmega8, atmega8535, atmega128...
-AVR_MCU = atmega8
+AVR_MCU = atmega128
# -O2 : speed
# -Os : size
OPTIMIZE = -O2
diff --git a/n/avr/modules/math/fixed/test/test_fixed.c b/n/avr/modules/math/fixed/test/test_fixed.c
index 9f04880..35ea573 100644
--- a/n/avr/modules/math/fixed/test/test_fixed.c
+++ b/n/avr/modules/math/fixed/test/test_fixed.c
@@ -115,6 +115,25 @@ proto_callback (uint8_t cmd, uint8_t size, uint8_t *args)
proto_send2d ('r', rl[0], rl[1]);
}
break;
+ case c ('s', 0):
+ for (ap = 0; ap < patn; ap++)
+ for (as = 0; as < 32; as++)
+ {
+ al = patl[ap] >> as;
+ proto_send1d ('a', al);
+ rl[0] = fixed_sqrt_uf248 (al);
+ rl[1] = fixed_sqrt_ui32 (al);
+ proto_send2d ('r', rl[0], rl[1]);
+ }
+ for (i = 0; i < 64000; i++)
+ {
+ al = random_u32 ();
+ proto_send1d ('a', al);
+ rl[0] = fixed_sqrt_uf248 (al);
+ rl[1] = fixed_sqrt_ui32 (al);
+ proto_send2d ('r', rl[0], rl[1]);
+ }
+ break;
default:
proto_send0 ('?');
return;