summaryrefslogtreecommitdiff
path: root/n/avr/modules/math/fixed/test/test_fixed.c
blob: d19f63a5f44c8a80ff0de5c62551169c8e06a27f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* 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"

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);
	    rl[1] = fixed_mul_f824 (-al, bl);
	    rl[2] = fixed_mul_f824 (al, -bl);
	    rl[3] = fixed_mul_f824 (-al, -bl);
	    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);
	    rl[1] = fixed_mul_f824 (-al, bl);
	    rl[2] = fixed_mul_f824 (al, -bl);
	    rl[3] = fixed_mul_f824 (-al, -bl);
	    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 < 32; bs++)
	  {
	    al = patl[ap] >> as;
	    bl = patl[bp] >> bs;
	    proto_send2d ('a', al, bl);
	    rl[0] = fixed_div_f824 (al, bl);
	    rl[1] = fixed_div_f824 (-al, bl);
	    rl[2] = fixed_div_f824 (al, -bl);
	    rl[3] = fixed_div_f824 (-al, -bl);
	    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_div_f824 (al, bl);
	    rl[1] = fixed_div_f824 (-al, bl);
	    rl[2] = fixed_div_f824 (al, -bl);
	    rl[3] = fixed_div_f824 (-al, -bl);
	    proto_send4d ('r', rl[0], rl[1], rl[2], rl[3]);
	  }
	break;
      default:
	proto_send0 ('?');
	return;
      }
    /* When no error acknoledge. */
    proto_send (cmd, size, args);
#undef c
}

int
main (void)
{
    sei ();
    uart0_init ();
    proto_send0 ('z');
    while (1)
      {
	uint8_t c = uart0_getc ();
	proto_accept (c);
      }
}