From 583f00e0b8efe2832f63efb478a51d3ad35e92ed Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Sun, 7 Oct 2007 22:16:03 +0200 Subject: Included SI2E avr modules. Well, this need more works... --- digital/avr/modules/math/fixed/Makefile | 5 + digital/avr/modules/math/fixed/Makefile.module | 3 + digital/avr/modules/math/fixed/README | 25 +++ digital/avr/modules/math/fixed/fixed.h | 81 +++++++ digital/avr/modules/math/fixed/fixed.txt | 52 +++++ digital/avr/modules/math/fixed/fixed_cosin_f824.c | 81 +++++++ .../avr/modules/math/fixed/fixed_div_f824.avr.S | 166 ++++++++++++++ .../avr/modules/math/fixed/fixed_mul_f824.avr.S | 172 ++++++++++++++ digital/avr/modules/math/fixed/fixed_sqrt_uf248.c | 48 ++++ digital/avr/modules/math/fixed/fixed_sqrt_ui32.c | 48 ++++ digital/avr/modules/math/fixed/test/Makefile | 38 ++++ digital/avr/modules/math/fixed/test/avrconfig.h | 86 +++++++ digital/avr/modules/math/fixed/test/test_fixed.c | 247 +++++++++++++++++++++ digital/avr/modules/math/random/Makefile.module | 1 + digital/avr/modules/math/random/README | 26 +++ digital/avr/modules/math/random/random.h | 33 +++ digital/avr/modules/math/random/test/Makefile | 12 + digital/avr/modules/math/random/test/avrconfig.h | 86 +++++++ digital/avr/modules/math/random/test/test_random.c | 44 ++++ digital/avr/modules/math/random/tt800.c | 86 +++++++ 20 files changed, 1340 insertions(+) create mode 100644 digital/avr/modules/math/fixed/Makefile create mode 100644 digital/avr/modules/math/fixed/Makefile.module create mode 100644 digital/avr/modules/math/fixed/README create mode 100644 digital/avr/modules/math/fixed/fixed.h create mode 100644 digital/avr/modules/math/fixed/fixed.txt create mode 100644 digital/avr/modules/math/fixed/fixed_cosin_f824.c create mode 100644 digital/avr/modules/math/fixed/fixed_div_f824.avr.S create mode 100644 digital/avr/modules/math/fixed/fixed_mul_f824.avr.S create mode 100644 digital/avr/modules/math/fixed/fixed_sqrt_uf248.c create mode 100644 digital/avr/modules/math/fixed/fixed_sqrt_ui32.c create mode 100644 digital/avr/modules/math/fixed/test/Makefile create mode 100644 digital/avr/modules/math/fixed/test/avrconfig.h create mode 100644 digital/avr/modules/math/fixed/test/test_fixed.c create mode 100644 digital/avr/modules/math/random/Makefile.module create mode 100644 digital/avr/modules/math/random/README create mode 100644 digital/avr/modules/math/random/random.h create mode 100644 digital/avr/modules/math/random/test/Makefile create mode 100644 digital/avr/modules/math/random/test/avrconfig.h create mode 100644 digital/avr/modules/math/random/test/test_random.c create mode 100644 digital/avr/modules/math/random/tt800.c (limited to 'digital/avr/modules/math') diff --git a/digital/avr/modules/math/fixed/Makefile b/digital/avr/modules/math/fixed/Makefile new file mode 100644 index 00000000..44892c74 --- /dev/null +++ b/digital/avr/modules/math/fixed/Makefile @@ -0,0 +1,5 @@ +BASE = ../../.. +DOC = fixed.html +EXTRACTDOC = fixed.h + +include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/math/fixed/Makefile.module b/digital/avr/modules/math/fixed/Makefile.module new file mode 100644 index 00000000..ed7e89fe --- /dev/null +++ b/digital/avr/modules/math/fixed/Makefile.module @@ -0,0 +1,3 @@ +math_fixed_SOURCES = fixed_mul_f824.avr.S fixed_div_f824.avr.S \ + fixed_cosin_f824.c \ + fixed_sqrt_uf248.c fixed_sqrt_ui32.c diff --git a/digital/avr/modules/math/fixed/README b/digital/avr/modules/math/fixed/README new file mode 100644 index 00000000..639d4fca --- /dev/null +++ b/digital/avr/modules/math/fixed/README @@ -0,0 +1,25 @@ +avr.math.fixed - Fixed point math module. + +Provide fixed point math function for AVR, and host simulation. See modules +README for more details about AVR modules. + + +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. diff --git a/digital/avr/modules/math/fixed/fixed.h b/digital/avr/modules/math/fixed/fixed.h new file mode 100644 index 00000000..a3200c18 --- /dev/null +++ b/digital/avr/modules/math/fixed/fixed.h @@ -0,0 +1,81 @@ +#ifndef fixed_h +#define fixed_h +/* fixed.h */ +/* 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. + * + * }}} */ + +/* 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. */ +#define fixed_mul_f824(a, b) ({ \ + uint16_t dummy; \ + asm ("" : "=r" (dummy)); \ + fixed_mul_f824_asm (dummy, a, b); }) +int32_t +fixed_mul_f824_asm (uint16_t dummy, int32_t a, int32_t b); + +/* Divide f8.24 by f8.24, return f8.24. */ +int32_t +fixed_div_f824 (int32_t a, int32_t b); + +#else /* HOST */ + +/** Multiply f8.24 by f8.24, return f8.24. */ +#define fixed_mul_f824(a, b) (((int64_t) (a) * (int64_t) (b) \ + + 0x800000LL) >> 24) + +/** Divide f8.24 by f8.24, return f8.24. */ +#define fixed_div_f824(a, b) (((int64_t) (a) << 24) / (int64_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); + +/** 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/digital/avr/modules/math/fixed/fixed.txt b/digital/avr/modules/math/fixed/fixed.txt new file mode 100644 index 00000000..645d9cc6 --- /dev/null +++ b/digital/avr/modules/math/fixed/fixed.txt @@ -0,0 +1,52 @@ +*Title: Module AVR math à virgule fixe +*Author: Ni + +* La virgule fixe + +Le format à virgule fixe, à opposer à la virgule flottante, divise un mot +binaire en deux parties, la partie entière et la partie fractionnaire. + +L'inconvénient par rapport à la virgule flottante, c'est de pouvoir +représenter une plage plus petite de nombres. Il faut aussi faire très +attention de ne pas déborder lors des calculs intermédiaires. + +L'avantage, c'est la vitesse d'exécution, ça tombe bien, on est pressé ! + +Je vous laisse consulter internet pour plus d'informations. + +* Notation des nombres + +Pour décoder le nom des fonctions : + +^<< + [u]{i|f}x[.y] +^>> + + [u] non signé ; + [i] entier ; + [f] virgule fixe ; + [x] taille de la partie entière en bits ; + [y] taille de la partie fractionnaire en bits. + +Par exemple : + + [i16] mot signé 16 bits ; + [uf24.8] nombre en virgule fixe non signé, avec 24 bits pour la partie + entière et 8 bits pour la partie fractionnaire. + +* Format des angles + +Les angles utilisés sont dans l'intervalle [0 ; 1[. La valeur 1 correspond à 2 +pi radians, soit un tour complet. Le sens des angles orientés est le sens +trigonométrique bien sur. + +* Routines de tests + +Pour tester les algorithmes, on compile test_fixed pour hôte et pour AVR. On +lance les deux et l'on compare les résultats, ils doivent être identiques. + +La version hôte vérifie les calculs avec la bibliothèque mathématique du C. + +* Doc + +*File: fixed.exd diff --git a/digital/avr/modules/math/fixed/fixed_cosin_f824.c b/digital/avr/modules/math/fixed/fixed_cosin_f824.c new file mode 100644 index 00000000..d5148f42 --- /dev/null +++ b/digital/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/digital/avr/modules/math/fixed/fixed_div_f824.avr.S b/digital/avr/modules/math/fixed/fixed_div_f824.avr.S new file mode 100644 index 00000000..43f4e0c3 --- /dev/null +++ b/digital/avr/modules/math/fixed/fixed_div_f824.avr.S @@ -0,0 +1,166 @@ +; fixed_div_f824.avr.S +; 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. +; +; }}} + +; Perfs: +; 1186 cycles in worse case +; 843 per second, per MHz + +#define dd3 r25 +#define dd2 r24 +#define dd1 r23 +#define dd0 r22 + +#define dv3 r21 +#define dv2 r20 +#define dv1 r19 +#define dv0 r18 + +; r28-r29 are avoided because they should be saved. +#define rem3 r31 +#define rem2 r30 +#define rem1 r27 +#define rem0 r26 + +#define __tmp_reg__ r0 +#define __zero_reg__ r1 + +#define cnt r16 + + .text + .global fixed_div_f824 + .func fixed_div_f824 +fixed_div_f824: + push cnt +; Store sign. + mov __tmp_reg__, dd3 + eor __tmp_reg__, dv3 +; Change sign. + sbrs dd3, 7 + rjmp 1f + com dd3 + com dd2 + com dd1 + neg dd0 + sbci dd1, 0xff + sbci dd2, 0xff + sbci dd3, 0xff +1: sbrs dv3, 7 + rjmp 2f + com dv3 + com dv2 + com dv1 + neg dv0 + sbci dv1, 0xff + sbci dv2, 0xff + sbci dv3, 0xff +; Clear rem. +2: clr rem0 + clr rem1 + movw rem2, rem0 +; First loop, dropped bits. + ldi cnt, 24 +1: ;lsl dd0 ; shift out dd + lsl dd1 ; do not touch dd0 + rol dd2 + rol dd3 + rol rem0 ; shift in rem + rol rem1 ; 24 bits only + rol rem2 + ;rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 +2: dec cnt ; test for loop + brne 1b +; Second loop, stored bits. + ldi cnt, 8 +1: rol dd0 ; shift out dd, shift in result + rol rem0 ; shift in rem + rol rem1 + rol rem2 + rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 + clc ; result bit 0 + rjmp 3f +2: sec ; result bit 1 +3: dec cnt ; test for loop + brne 1b +; Last loop, stored bits, dd padding bits. + ldi cnt, 24 +1: rol dd0 ; shift out dd, shift in result + rol dd1 ; 0s come from the first loop + rol dd2 + rol dd3 + rol rem0 ; shift in rem + rol rem1 + rol rem2 + rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 + clc ; result bit 0 + rjmp 3f +2: sec ; result bit 1 +3: dec cnt ; test for loop + brne 1b +; Store last bit. + rol dd0 ; shift in result + rol dd1 + rol dd2 + rol dd3 +; Restore sign. + sbrs __tmp_reg__, 7 + rjmp 1f + com dd3 + com dd2 + com dd1 + neg dd0 + sbci dd1, 0xff + sbci dd2, 0xff + sbci dd3, 0xff +; Return. +1: pop r16 + ret + .endfunc diff --git a/digital/avr/modules/math/fixed/fixed_mul_f824.avr.S b/digital/avr/modules/math/fixed/fixed_mul_f824.avr.S new file mode 100644 index 00000000..9da807d7 --- /dev/null +++ b/digital/avr/modules/math/fixed/fixed_mul_f824.avr.S @@ -0,0 +1,172 @@ +; fixed_mul_f824.avr.S +; 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. +; +; }}} + +; a: A3.A2 A1 A0 +; b: B3.B2 B1 B0 +; m: M3.M2 M1 M0 +; +; . => fractional dot +; A3xB3. | +; |A3xB2 | +; | .A3xB1 | +; | . A3xB0| +; |A2xB3 | +; | .A2xB2 | +; | . A2xB1| +; | . A2xB0 +; | .A1xB3 | +; | . A1xB2| +; | . A1xB1 +; | . |A1xB0 +; | . A0xB3| +; | . A0xB2 +; | . |A0xB1 +; | . | A0xB0 +; . [- step 1 --] +; [- step 2 --] +; +; The operation is done in two steps. The three lsb multiplications can not be +; avoided. +; All multiplications results are added together. If the result is negative, +; do the sign extension (the sbc instructions). +; Result is rounded to the nearest value. +; +; Perfs: +; 96 cycles +; 10416 per second, per MHz + +#define dummy1 r25 +#define dummy0 r24 + +; mulsu are only allowed in r16-r23. +#define a3 r23 +#define a2 r22 +#define a1 r21 +#define a0 r20 + +#define b3 r19 +#define b2 r18 +#define b1 r17 +#define b0 r16 + +; r23 and r22 are used for a, registers will be moved before return. +#define m3 r25 +#define m2 r24 +#define m1 r27 +#define m0 r26 + +#define m1r r23 +#define m0r r22 + +#define z r30 + + .text + .global fixed_mul_f824_asm + .func fixed_mul_f824_asm +fixed_mul_f824_asm: + clr z +; Low dword (>> 8, with 8 guards). + mul a0, b1 + movw m0, r0 + clr m2 + clr m3 + mul a0, b0 + add m0, r1 + adc m1, z + adc m2, z + mul a1, b0 + add m0, r0 + adc m1, r1 + adc m2, z + mul a0, b2 + add m1, r0 + adc m2, r1 + adc m3, z + mul a1, b1 + add m1, r0 + adc m2, r1 + adc m3, z + mul a2, b0 + add m1, r0 + adc m2, r1 + adc m3, z +; Rounding, m0 is dropped. + ldi m0, 0x80 + add m1, m0 + adc m2, z + adc m3, z +; Shift. + movw m0, m2 +; Upper word. + mulsu b3, a2 + movw m2, r0 + mulsu b3, a0 + sbc m2, z + sbc m3, z + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mul a1, b2 + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mul a2, b1 + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mulsu a3, b0 + sbc m2, z + sbc m3, z + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mulsu b3, a1 + sbc m3, z + add m1, r0 + adc m2, r1 + adc m3, z + mul a2, b2 + add m1, r0 + adc m2, r1 + adc m3, z + mulsu a3, b1 + sbc m3, z + add m1, r0 + adc m2, r1 + adc m3, z + mulsu a3, b2 + add m2, r0 + adc m3, r1 + muls a3, b3 + add m3, r0 +; Restore r1 and return. + clr r1 + movw m0r, m0 + ret + .endfunc diff --git a/digital/avr/modules/math/fixed/fixed_sqrt_uf248.c b/digital/avr/modules/math/fixed/fixed_sqrt_uf248.c new file mode 100644 index 00000000..6d673120 --- /dev/null +++ b/digital/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/digital/avr/modules/math/fixed/fixed_sqrt_ui32.c b/digital/avr/modules/math/fixed/fixed_sqrt_ui32.c new file mode 100644 index 00000000..88de5841 --- /dev/null +++ b/digital/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/digital/avr/modules/math/fixed/test/Makefile b/digital/avr/modules/math/fixed/test/Makefile new file mode 100644 index 00000000..04aa4cff --- /dev/null +++ b/digital/avr/modules/math/fixed/test/Makefile @@ -0,0 +1,38 @@ +BASE = ../../../.. +PROGS = test_fixed +SIMU_PROGS = test_fixed +test_fixed_SOURCES = test_fixed.c +MODULES = uart proto utils math/fixed math/random +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega128 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +HOST_LIBS = -lm + +include $(BASE)/make/Makefile.gen + +# Automatic tests. + +TEST_FILES = test.mul test.div test.cos test.sqrt +TEST_FILES := $(TEST_FILES:%=%.host) $(TEST_FILES:%=%.simu) +code_mul = m +code_div = d +code_cos = c +code_sqrt = s +code = $(code_$(1:test.%.$(2)=%)) + +clean.test: + rm -f $(TEST_FILES) + +SIMULAVR = simulavr -d $(AVR_MCU) -W 0x22,- -R 0x20,- -T uart0_exit + +.PRECIOUS: $(TEST_FILES) + +test.%.host: test_fixed.host + echo '!$(call code,$@,host)' | ./$< > $@ + +test.%.simu: test_fixed.avr.simu.elf + echo '!$(call code,$@,simu)\r' | $(SIMULAVR) -f $< > $@ diff --git a/digital/avr/modules/math/fixed/test/avrconfig.h b/digital/avr/modules/math/fixed/test/avrconfig.h new file mode 100644 index 00000000..763c9185 --- /dev/null +++ b/digital/avr/modules/math/fixed/test/avrconfig.h @@ -0,0 +1,86 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* 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. + * + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* uart - UART module. */ +/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ +#define AC_UART0_PORT 0 +/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_UART0_BAUDRATE 115200 +/** Send mode: + * - POLLING: no interrupts. + * - RING: interrupts, ring buffer. */ +#define AC_UART0_SEND_MODE RING +/** Recv mode, same as send mode. */ +#define AC_UART0_RECV_MODE RING +/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ +#define AC_UART0_CHAR_SIZE 8 +/** Parity : ODD, EVEN, NONE. */ +#define AC_UART0_PARITY EVEN +/** Stop bits : 1, 2. */ +#define AC_UART0_STOP_BITS 1 +/** Send buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_SEND_BUFFER_SIZE 32 +/** Recv buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_RECV_BUFFER_SIZE 32 +/** If the send buffer is full when putc: + * - DROP: drop the new byte. + * - WAIT: wait until there is room in the send buffer. */ +#define AC_UART0_SEND_BUFFER_FULL WAIT +/** In HOST compilation: + * - STDIO: use stdin/out. + * - PTS: use pseudo terminal. */ +#define AC_UART0_HOST_DRIVER STDIO +/** Same thing for secondary port. */ +#define AC_UART1_PORT -1 +#define AC_UART1_BAUDRATE 115200 +#define AC_UART1_SEND_MODE RING +#define AC_UART1_RECV_MODE RING +#define AC_UART1_CHAR_SIZE 8 +#define AC_UART1_PARITY EVEN +#define AC_UART1_STOP_BITS 1 +#define AC_UART1_SEND_BUFFER_SIZE 32 +#define AC_UART1_RECV_BUFFER_SIZE 32 +#define AC_UART1_SEND_BUFFER_FULL WAIT +#define AC_UART1_HOST_DRIVER STDIO + +/* proto - Protocol module. */ +/** Maximum argument size. */ +#define AC_PROTO_ARGS_MAX_SIZE 8 +/** Callback function name. */ +#define AC_PROTO_CALLBACK proto_callback +/** Putchar function name. */ +#define AC_PROTO_PUTC uart0_putc +/** Support for quote parameter. */ +#define AC_PROTO_QUOTE 0 + +#endif /* avrconfig_h */ diff --git a/digital/avr/modules/math/fixed/test/test_fixed.c b/digital/avr/modules/math/fixed/test/test_fixed.c new file mode 100644 index 00000000..875aad4f --- /dev/null +++ b/digital/avr/modules/math/fixed/test/test_fixed.c @@ -0,0 +1,247 @@ +/* test_fixed.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 "modules/math/fixed/fixed.h" +#include "modules/math/random/random.h" +#include "modules/uart/uart.h" +#include "modules/proto/proto.h" +#include "modules/utils/utils.h" +#include "io.h" + +#ifdef HOST + +#include +#include +#include + +static void +check_mul (int32_t a, int32_t b, int32_t r) +{ + feclearexcept (FE_ALL_EXCEPT); + double af = (double) a / (1 << 24); + double bf = (double) b / (1 << 24); + int32_t ri = rint (af * bf * (1 << 24)); + if (r != ri && !fetestexcept (FE_INVALID)) + printf ("error: %08x * %08x != %08x (%08x)\n", a, b, r, ri); +} + +static void +check_div (int32_t a, int32_t b, int32_t r) +{ + feclearexcept (FE_ALL_EXCEPT); + double af = (double) a / (1 << 24); + double bf = (double) b / (1 << 24); + int32_t ri = af / bf * (1 << 24); + if (r != ri && !fetestexcept (FE_INVALID)) + printf ("error: %08x / %08x != %08x (%08x)\n", a, b, r, ri); +} + +static void +check_cos (int32_t a, int32_t rc, int32_t rs) +{ + double af = (double) a / (1 << 24) * 2 * M_PI; + int32_t rci = cos (af) * (1 << 24); + if (rc != rci) + printf ("error: cos (%08x) != %08x (%08x%+d)\n", + a, rc, rci, rc - rci); + int32_t rsi = sin (af) * (1 << 24); + if (rs != rsi) + printf ("error: sin (%08x) != %08x (%08x%+d)\n", + a, rs, rsi, rs - rsi); +} + +static void +check_sqrt (uint32_t a, uint32_t rf, uint16_t ri) +{ + double aff = (double) a / (1 << 8); + uint32_t rfi = sqrt (aff) * (1 << 8); + if (rf != rfi) + printf ("error: sqrt_f (%08x) != %08x (%08x%+d)\n", + a, rf, rfi, rf - rfi); + double aif = (double) a; + uint16_t rii = sqrt (aif); + if (ri != rii) + printf ("error: sqrt_i (%08x) != %04x (%04x%+d)\n", + a, ri, rii, ri - rii); +} + +#else + +# define check_mul(a, b, r) +# define check_div(a, b, r) +# define check_cos(a, rc, rs) +# define check_sqrt(a, rf, ri) + +#endif + +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ + uint8_t ap, bp, as, bs; + uint16_t i; + int32_t al, bl, rl[4]; + uint32_t patl[] = { 0xa66a6aa6, 0x5a5affff, 0xffcdffcd, 0xffffffff }; +#define patn (sizeof (patl) / sizeof (patl[0])) +#define c(cmd, size) (cmd << 8 | size) + switch (c (cmd, size)) + { + case c ('z', 0): + utils_reset (); + break; + case c ('m', 0): + for (ap = 0; ap < patn; ap++) + for (bp = 0; bp < patn; bp++) + for (as = 0; as < 32; as++) + for (bs = 0; bs < 32; bs++) + { + al = patl[ap] >> as; + bl = patl[bp] >> bs; + proto_send2d ('a', al, bl); + rl[0] = fixed_mul_f824 (al, bl); + check_mul (al, bl, rl[0]); + rl[1] = fixed_mul_f824 (-al, bl); + check_mul (-al, bl, rl[1]); + rl[2] = fixed_mul_f824 (al, -bl); + check_mul (al, -bl, rl[2]); + rl[3] = fixed_mul_f824 (-al, -bl); + check_mul (-al, -bl, rl[3]); + proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]); + } + for (i = 0; i < 64000; i++) + { + al = random_u32 (); + bl = random_u32 (); + proto_send2d ('a', al, bl); + rl[0] = fixed_mul_f824 (al, bl); + check_mul (al, bl, rl[0]); + rl[1] = fixed_mul_f824 (-al, bl); + check_mul (-al, bl, rl[1]); + rl[2] = fixed_mul_f824 (al, -bl); + check_mul (al, -bl, rl[2]); + rl[3] = fixed_mul_f824 (-al, -bl); + check_mul (-al, -bl, rl[3]); + proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]); + } + break; + case c ('d', 0): + for (ap = 0; ap < patn; ap++) + for (bp = 0; bp < patn; bp++) + for (as = 0; as < 32; as++) + for (bs = 0; bs < 31; bs++) + { + al = patl[ap] >> as; + bl = patl[bp] >> bs; + proto_send2d ('a', al, bl); + rl[0] = fixed_div_f824 (al, bl); + check_div (al, bl, rl[0]); + rl[1] = fixed_div_f824 (-al, bl); + check_div (-al, bl, rl[1]); + rl[2] = fixed_div_f824 (al, -bl); + check_div (al, -bl, rl[2]); + rl[3] = fixed_div_f824 (-al, -bl); + check_div (-al, -bl, rl[3]); + proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]); + } + for (i = 0; i < 64000; i++) + { + al = random_u32 (); + bl = random_u32 (); + if (bl != 0) + { + proto_send2d ('a', al, bl); + rl[0] = fixed_div_f824 (al, bl); + check_div (al, bl, rl[0]); + rl[1] = fixed_div_f824 (-al, bl); + check_div (-al, bl, rl[1]); + rl[2] = fixed_div_f824 (al, -bl); + check_div (al, -bl, rl[2]); + rl[3] = fixed_div_f824 (-al, -bl); + check_div (-al, -bl, rl[3]); + proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]); + } + } + break; + case c ('c', 0): + for (al = 0; al < (1L << 24); al += 257) + { + proto_send1d ('a', al); + rl[0] = fixed_cos_f824 (al); + rl[1] = fixed_sin_f824 (al); + check_cos (al, rl[0], rl[1]); + 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); + check_cos (al, rl[0], rl[1]); + 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); + check_sqrt (al, rl[0], rl[1]); + 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); + check_sqrt (al, rl[0], rl[1]); + proto_send2d ('r', rl[0], rl[1]); + } + break; + default: + proto_send0 ('?'); + return; + } + /* When no error acknoledge. */ + proto_send (cmd, size, args); +#undef c +} + +int +main (int argc, char **argv) +{ + avr_init (argc, argv); + sei (); + uart0_init (); + proto_send0 ('z'); + while (1) + { + uint8_t c = uart0_getc (); + proto_accept (c); + } +} diff --git a/digital/avr/modules/math/random/Makefile.module b/digital/avr/modules/math/random/Makefile.module new file mode 100644 index 00000000..491dd63b --- /dev/null +++ b/digital/avr/modules/math/random/Makefile.module @@ -0,0 +1 @@ +math_random_SOURCES = tt800.c diff --git a/digital/avr/modules/math/random/README b/digital/avr/modules/math/random/README new file mode 100644 index 00000000..1e8e07af --- /dev/null +++ b/digital/avr/modules/math/random/README @@ -0,0 +1,26 @@ +avr.math.random - Pseudo random numbers generator. + +Provide RNG for AVR, using the TT800 algorithm. See modules README for more +details about AVR modules. + +The TT800 has his own copyright notice, see tt800.c file. + +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. diff --git a/digital/avr/modules/math/random/random.h b/digital/avr/modules/math/random/random.h new file mode 100644 index 00000000..5822cfd2 --- /dev/null +++ b/digital/avr/modules/math/random/random.h @@ -0,0 +1,33 @@ +#ifndef random_h +#define random_h +/* random.h */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * 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. + * + * }}} */ + +/** Generate a 32 bit pseudo random number. Sequences are deterministic and + * same numbers are drawn on each runs as the seed is always the same. */ +uint32_t +random_u32 (void); + +#endif /* random_h */ diff --git a/digital/avr/modules/math/random/test/Makefile b/digital/avr/modules/math/random/test/Makefile new file mode 100644 index 00000000..0bc3c0e5 --- /dev/null +++ b/digital/avr/modules/math/random/test/Makefile @@ -0,0 +1,12 @@ +BASE = ../../../.. +PROGS = test_random +test_random_SOURCES = test_random.c +MODULES = uart proto math/random +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega8 +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +include $(BASE)/make/Makefile.gen diff --git a/digital/avr/modules/math/random/test/avrconfig.h b/digital/avr/modules/math/random/test/avrconfig.h new file mode 100644 index 00000000..5f25426e --- /dev/null +++ b/digital/avr/modules/math/random/test/avrconfig.h @@ -0,0 +1,86 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * 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. + * + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* uart - UART module. */ +/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ +#define AC_UART0_PORT 0 +/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_UART0_BAUDRATE 115200 +/** Send mode: + * - POLLING: no interrupts. + * - RING: interrupts, ring buffer. */ +#define AC_UART0_SEND_MODE RING +/** Recv mode, same as send mode. */ +#define AC_UART0_RECV_MODE RING +/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ +#define AC_UART0_CHAR_SIZE 8 +/** Parity : ODD, EVEN, NONE. */ +#define AC_UART0_PARITY EVEN +/** Stop bits : 1, 2. */ +#define AC_UART0_STOP_BITS 1 +/** Send buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_SEND_BUFFER_SIZE 32 +/** Recv buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_RECV_BUFFER_SIZE 32 +/** If the send buffer is full when putc: + * - DROP: drop the new byte. + * - WAIT: wait until there is room in the send buffer. */ +#define AC_UART0_SEND_BUFFER_FULL WAIT +/** In HOST compilation: + * - STDIO: use stdin/out. + * - PTS: use pseudo terminal. */ +#define AC_UART0_HOST_DRIVER STDIO +/** Same thing for secondary port. */ +#define AC_UART1_PORT -1 +#define AC_UART1_BAUDRATE 115200 +#define AC_UART1_SEND_MODE RING +#define AC_UART1_RECV_MODE RING +#define AC_UART1_CHAR_SIZE 8 +#define AC_UART1_PARITY EVEN +#define AC_UART1_STOP_BITS 1 +#define AC_UART1_SEND_BUFFER_SIZE 32 +#define AC_UART1_RECV_BUFFER_SIZE 32 +#define AC_UART1_SEND_BUFFER_FULL WAIT +#define AC_UART1_HOST_DRIVER STDIO + +/* proto - Protocol module. */ +/** Maximum argument size. */ +#define AC_PROTO_ARGS_MAX_SIZE 8 +/** Callback function name. */ +#define AC_PROTO_CALLBACK proto_callback +/** Putchar function name. */ +#define AC_PROTO_PUTC uart0_putc +/** Support for quote parameter. */ +#define AC_PROTO_QUOTE 0 + +#endif /* avrconfig_h */ diff --git a/digital/avr/modules/math/random/test/test_random.c b/digital/avr/modules/math/random/test/test_random.c new file mode 100644 index 00000000..c4d2b045 --- /dev/null +++ b/digital/avr/modules/math/random/test/test_random.c @@ -0,0 +1,44 @@ +/* test_random.c */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * 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 "modules/math/random/random.h" +#include "modules/uart/uart.h" +#include "modules/proto/proto.h" +#include "io.h" + +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ +} + +int +main (void) +{ + sei (); + uart0_init (); + proto_send0 ('z'); + while (42) + proto_send1d ('r', random_u32 ()); +} diff --git a/digital/avr/modules/math/random/tt800.c b/digital/avr/modules/math/random/tt800.c new file mode 100644 index 00000000..64675e8a --- /dev/null +++ b/digital/avr/modules/math/random/tt800.c @@ -0,0 +1,86 @@ +/* A C-program for TT800 : July 8th 1996 Version + by M. Matsumoto, email: m-mat @ math.sci.hiroshima-u.ac.jp + + Modified for AVR modules by Nicolas Schodet. + + genrand() generate one pseudorandom number with double precision + which is uniformly distributed on [0,1]-interval + for each call. One may choose any initial 25 seeds + except all zeros. + + Copyright (C) 1996, Makoto Matsumoto + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + See: ACM Transactions on Modelling and Computer Simulation, + Vol. 4, No. 3, 1994, pages 254-266. +*/ +#include "common.h" + +#define N 25 +#define M 7 + +uint32_t +random_u32 (void) +{ + uint32_t y; + static uint8_t k = 0; + static uint32_t x[N]={ /* initial 25 seeds, change as you wish */ + 0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23, + 0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825, + 0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f, + 0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9, + 0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb + }; + static uint32_t mag01[2]={ + 0x0, 0x8ebfd028 /* this is magic vector `a', don't change */ + }; + if (k==N) { /* generate N words at one time */ + uint8_t kk; + for (kk=0;kk> 1) ^ mag01[x[kk] % 2]; + } + for (; kk> 1) ^ mag01[x[kk] % 2]; + } + k=0; + } + y = x[k]; + y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */ + y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */ + /*y &= 0xffffffff;*/ /* you may delete this line if word size = 32 */ +/* + the following line was added by Makoto Matsumoto in the 1996 version + to improve lower bit's corellation. + Delete this line to o use the code published in 1994. +*/ + y ^= (y >> 16); /* added to the 1994 version */ + k++; + return y; +} -- cgit v1.2.3