Switch from GPLv2 to GPLv2+
[fw/altos] / src / test / ao_int64_test.c
1 /*
2  * Copyright © 2013 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #define __data
20 #define __pdata
21 #define __xdata
22 #define __reentrant
23
24 #include <ao_int64.h>
25 #include <ao_int64.c>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 int     errors;
30
31 #define test_o(op,func,mod,a,b,ao_a,ao_b) do {                          \
32                 r = (a) op (b);                                         \
33                 func(&ao_r, ao_a, ao_b);                                \
34                 c = ao_cast64(&ao_r);                                   \
35                 if (c != r) {                                           \
36                         printf ("trial %4d: %lld " #func mod " %lld = %lld (should be %lld)\n", \
37                                 trial, (int64_t) (a), (int64_t) b, c, r); \
38                         ++errors;                                       \
39                 }                                                       \
40         } while (0)
41
42 #define test(op,func,a,b,ao_a,ao_b) test_o(op,func,"",a,b,ao_a,ao_b)
43
44 #define test_a(op,func,a,b,ao_a,ao_b) do {      \
45                 ao_r = *ao_a;                   \
46                 test_o(op,func,"_a",a,b,&ao_r,ao_b);    \
47         } while (0)
48
49 #define test_b(op,func,a,b,ao_a,ao_b) do {      \
50                 ao_r = *ao_b;                   \
51                 test_o(op,func,"_b",a,b,ao_a,&ao_r);    \
52         } while (0)
53
54 #define test_x(op,func,a,b,ao_a,ao_b) do {      \
55                 ao_r = *ao_a;                   \
56                 test_o(op,func,"_xa",a,a,&ao_r,&ao_r);  \
57                 ao_r = *ao_b;                   \
58                 test_o(op,func,"_xb",b,b,&ao_r,&ao_r);  \
59         } while (0)
60
61 void
62 do_test(int trial, int64_t a, int64_t b)
63 {
64         int64_t r, c;
65         ao_int64_t      ao_a, ao_b, ao_r;
66
67         ao_int64_init64(&ao_a, a >> 32, a);
68         ao_int64_init64(&ao_b, b >> 32, b);
69
70         test(+, ao_plus64, a, b, &ao_a, &ao_b);
71         test_a(+, ao_plus64, a, b, &ao_a, &ao_b);
72         test_b(+, ao_plus64, a, b, &ao_a, &ao_b);
73         test_x(+, ao_plus64, a, b, &ao_a, &ao_b);
74         test(-, ao_minus64, a, b, &ao_a, &ao_b);
75         test_a(-, ao_minus64, a, b, &ao_a, &ao_b);
76         test_b(-, ao_minus64, a, b, &ao_a, &ao_b);
77         test_x(-, ao_minus64, a, b, &ao_a, &ao_b);
78         test(*, ao_mul64_32_32,(int64_t) (int32_t) a, (int32_t) b, (int32_t) a, (int32_t) b);
79         test(*, ao_mul64, a, b, &ao_a, &ao_b);
80         test_a(*, ao_mul64, a, b, &ao_a, &ao_b);
81         test_b(*, ao_mul64, a, b, &ao_a, &ao_b);
82         test_x(*, ao_mul64, a, b, &ao_a, &ao_b);
83         test(*, ao_mul64_64_16, a, (uint16_t) b, &ao_a, (uint16_t) b);
84         test_a(*, ao_mul64_64_16, a, (uint16_t) b, &ao_a, (uint16_t) b);
85         test(>>, ao_rshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
86         test_a(>>, ao_rshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
87         test(<<, ao_lshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
88         test_a(<<, ao_lshift64, a, (uint8_t) b & 0x3f, &ao_a, (uint8_t) b & 0x3f);
89 }
90
91 #define TESTS   10000000
92
93 static int64_t
94 random64(void)
95 {
96         return (int64_t) random() + ((int64_t) random() << 31) /* + ((int64_t) random() << 33) */;
97 }
98
99 int
100 main (int argc, char **argv)
101 {
102         int     i, start;
103
104         if (argv[1])
105                 start = atoi(argv[1]);
106         else
107                 start = 0;
108         srandom(1000);
109         for (i = 0; i < TESTS; i++) {
110                 int64_t a = random64();
111                 int64_t b = random64();
112                 if (i >= start)
113                         do_test(i, a, b);
114         }
115         return errors;
116 }