add_test.c 6.8 KB

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