1 /*
2 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische
3 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for
4 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE.
5 */
6
7 /* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/add_test.c,v 1.2 1994/05/10 20:18:17 jutta Exp $ */
8
9 #ifndef _POSIX_C_SOURCE
10 #define _POSIX_C_SOURCE 200809L
11 #endif
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include "gsm.h"
19
20 #include "../src/add.c"
21
22 int interactive = 1;
23
24 char * opname;
25 longword L_op1, L_op2, L_expect;
26 word op1, op2, expect;
27 int do_expect;
28
29 word M_gsm_add P((word op1, word op2));
30 word M_gsm_sub P((word op1, word op2));
31 word M_gsm_mult P((word op1, word op2));
32 word M_gsm_mult_r P((word op1, word op2));
33 word M_gsm_abs P((word op1));
34 longword M_gsm_L_mult P((word op1, word op2));
35 longword M_gsm_L_add P((longword op1, longword op2));
36
help()37 void help()
38 {
39 puts( " add a b sub a b mult a b div a b" );
40 puts( "L_add A B L_sub A B L_mult A B mult_r a b" );
41 puts( "" );
42 puts( "abs a norm a >> a b << a b" );
43 puts( " L_>> A B L_<< A B" );
44 }
45
46 char * strtek P2((str, sep), char * str, char * sep) {
47
48 static char * S = (char *)0;
49 char * c, * base;
50
51 if (str) S = str;
52
53 if (!S || !*S) return (char *)0;
54
55 /* Skip delimiters.
56 */
57 while (*S) {
58 for (c = sep; *c && *c != *S; c++) ;
59 if (*c) *S++ = 0;
60 else break;
61 }
62
63 base = S;
64
65 /* Skip non-delimiters.
66 */
67 for (base = S; *S; S++) {
68
69 for (c = sep; *c; c++)
70 if (*c == *S) {
71 *S++ = 0;
72 return base;
73 }
74 }
75
76 return base == S ? (char *)0 : base;
77 }
78
79 long value P1((s), char * s)
80 {
81 switch (*s) {
82 case '-': switch (s[1]) {
83 case '\0': return MIN_WORD;
84 case '-': return MIN_LONGWORD;
85 default: break;
86 }
87 break;
88
89 case '+': switch (s[1]) {
90 case '\0': return MAX_WORD;
91 case '+': return MAX_LONGWORD;
92 default: break;
93 }
94 default: break;
95 }
96
97 return strtol(s, (char **)0, 0);
98 }
99
100 char * parse P1((buf), char * buf)
101 {
102 char * s, * a;
103
104 if ((a = strchr(buf, '=')) != 0) *a++ = 0;
105
106 opname = s = strtek(buf, " \t(");
107 if (!s) return (char *)0;
108
109 op1 = op2 = L_op1 = L_op2 = 0;
110
111 if ((s = strtek( (char *)0, "( \t,")) != 0) {
112 op1 = L_op1 = value(s);
113 if ((s = strtek( (char *)0, ", \t)")) != 0)
114 op2 = L_op2 = value(s);
115 }
116
117 if (a) {
118 do_expect = 1;
119 while (*a == ' ' || *a == '\t') a++;
120 expect = L_expect = value(a);
121 }
122
123 return opname;
124 }
125
126 void fprint_word P2((f, w), FILE * f, word w)
127 {
128 if (!w) putc('0', f);
129 else fprintf(f, "0x%4.4x (%d%s)",
130 (unsigned int)w,
131 (int)w,
132 w == MIN_WORD? "/-" : (w == MAX_WORD ? "/+" : ""));
133 }
134
135 void print_word P1((w), word w)
136 {
137 fprint_word( stdout, w );
138 }
139
140 void fprint_longword P2((f, w), FILE * f, longword w)
141 {
142 if (!w) putc('0', f);
143 else fprintf(f, "0x%8.8lx (%ld%s)",
144 (unsigned long)w, (long)w, w == MIN_WORD ? "/-"
145 : (w == MAX_WORD ? "/+"
146 : (w == MIN_LONGWORD ? "/--"
147 : (w == MAX_LONGWORD ? "/++" : ""))));
148 }
149
150 void print_longword P1((w),longword w)
151 {
152 fprint_longword(stdout, w);
153 }
154
155 void do_longword P1((w), longword w)
156 {
157 if (interactive) print_longword(w);
158 if (do_expect) {
159 if ((w & 0xfffffffful) != (L_expect & 0xfffffffful)) {
160 if (!interactive) fprint_longword(stderr, w);
161 fprintf(stderr, " != %s (%ld, %ld) -- expected ",
162 opname, (long)L_op1, (long)L_op2 );
163 fprint_longword(stderr, L_expect);
164 putc( '\n', stderr );
165 }
166 } else if (interactive) putchar('\n');
167 }
168
169 void do_word P1((w), word w )
170 {
171 if (interactive) print_word(w);
172 if (do_expect) {
173 if (w != expect) {
174 if (!interactive) fprint_word(stderr, w);
175 fprintf(stderr, " != %s (%ld, %ld) -- expected ",
176 opname, (long)L_op1, (long)L_op2 );
177 fprint_word(stderr, expect);
178 putc('\n', stderr);
179 }
180 } else if (interactive) putchar('\n');
181 }
182
main(ac,av)183 int main(ac, av) char ** av;
184 {
185 char buf[299];
186 char * c;
187 FILE * in;
188
189 if (ac > 2) {
190 fprintf(stderr, "Usage: %s [filename]\n", av[0]);
191 fail:
192 #ifdef EXIT_FAILURE
193 exit(EXIT_FAILURE);
194 #else
195 exit(1);
196 #endif
197 }
198 if (ac < 2) in = stdin;
199 else if (!(in = fopen(av[1], "r"))) {
200 perror(av[1]);
201 fprintf(stderr, "%s: cannot open file \"%s\" for reading\n",
202 av[0], av[1]);
203 goto fail;
204 }
205
206 interactive = isatty(fileno(in));
207
208 for (;;) {
209 if (interactive) fprintf(stderr, "? ");
210
211 if (!fgets(buf, sizeof(buf), in)) exit(0);
212 if ((c = strchr(buf, '\n')) != 0) *c = 0;
213
214 if (*buf == ';' || *buf == '#') continue;
215 if (*buf == '\'') {
216 puts(buf + 1);
217 continue;
218 }
219 if (*buf == '\"') {
220 fprintf(stderr, "%s\n", buf + 1);
221 continue;
222 }
223
224 c = parse(buf);
225
226 if (!c) continue;
227 if (!strcmp(c, "add")) {
228 do_word( gsm_add( op1, op2 ));
229 continue;
230 }
231 if (!strcmp(c, "M_add")) {
232 do_word( M_gsm_add( op1, op2 ));
233 continue;
234 }
235 if (!strcmp(c, "sub")) {
236 do_word( gsm_sub( op1, op2 ));
237 continue;
238 }
239 if (!strcmp(c, "M_sub")) {
240 do_word( M_gsm_sub( op1, op2 ));
241 continue;
242 }
243 if (!strcmp(c, "mult")) {
244 do_word( gsm_mult( op1, op2 ));
245 continue;
246 }
247 if (!strcmp(c, "M_mult")) {
248 do_word( M_gsm_mult( op1, op2 ));
249 continue;
250 }
251 if (!strcmp(c, "mult_r")) {
252 do_word( gsm_mult_r(op1, op2));
253 continue;
254 }
255 if (!strcmp(c, "M_mult_r")) {
256 do_word( M_gsm_mult_r(op1, op2));
257 continue;
258 }
259 if (!strcmp(c, "abs" )) {
260 do_word( gsm_abs(op1) );
261 continue;
262 }
263 if (!strcmp(c, "M_abs" )) {
264 do_word( M_gsm_abs(op1) );
265 continue;
266 }
267 if (!strcmp(c, "div" )) {
268 do_word( gsm_div( op1, op2 ));
269 continue;
270 }
271 if (!strcmp(c, "norm" )) {
272 do_word( gsm_norm(L_op1));
273 continue;
274 }
275 if (!strcmp(c, "<<" )) {
276 do_word( gsm_asl( op1, op2));
277 continue;
278 }
279 if (!strcmp(c, ">>" )) {
280 do_word( gsm_asr( op1, op2 ));
281 continue;
282 }
283 if (!strcmp(c, "L_mult")) {
284 do_longword( gsm_L_mult( op1, op2 ));
285 continue;
286 }
287 if (!strcmp(c, "M_L_mult")) {
288 do_longword( M_gsm_L_mult( op1, op2 ));
289 continue;
290 }
291 if (!strcmp(c, "L_add" )) {
292 do_longword( gsm_L_add( L_op1, L_op2 ));
293 continue;
294 }
295 if (!strcmp(c, "M_L_add" )) {
296 do_longword( M_gsm_L_add( L_op1, L_op2 ));
297 continue;
298 }
299 if (!strcmp(c, "L_sub" )) {
300 do_longword( gsm_L_sub( L_op1, L_op2 ));
301 continue;
302 }
303 if (!strcmp(c, "L_<<" )) {
304 do_longword( gsm_L_asl( L_op1, L_op2 ));
305 continue;
306 }
307 if (!strcmp(c, "L_>>")) {
308 do_longword( gsm_L_asr( L_op1, L_op2 ));
309 continue;
310 }
311 help();
312 }
313 }
314
315 #include "private.h"
316
317 /*
318 * Function stubs for macro implementations of commonly used
319 * math functions
320 */
321 word M_gsm_add P2((op1, op2),word op1, word op2)
322 {
323 longword ltmp;
324 return GSM_ADD(op1, op2);
325 }
326
327 word M_gsm_sub P2((op1, op2), word op1, word op2)
328 {
329 longword ltmp;
330 return GSM_SUB(op1, op2);
331 }
332
333 word M_gsm_mult P2((op1, op2), word op1, word op2)
334 {
335 return GSM_MULT(op1, op2);
336 }
337
338 word M_gsm_mult_r P2((op1, op2), word op1, word op2)
339 {
340 return GSM_MULT_R(op1, op2);
341 }
342
343 word M_gsm_abs P1((op1), word op1)
344 {
345 return GSM_ABS(op1);
346 }
347
348 longword M_gsm_L_mult P2((op1, op2), word op1, word op2)
349 {
350 return GSM_L_MULT(op1, op2);
351 }
352
353 longword M_gsm_L_add P2((op1, op2), longword op1, longword op2)
354 {
355 ulongword utmp;
356 return GSM_L_ADD(op1, op2);
357 }
358