1 /* $OpenBSD: divremtest.c,v 1.2 2001/01/29 02:05:39 niklas Exp $ */ 2 /* $NetBSD: divremtest.c,v 1.1 1995/04/24 05:53:35 cgd Exp $ */ 3 4 /* 5 * Copyright (c) 1995 Christopher G. Demetriou 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Christopher G. Demetriou 19 * for the NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <stdio.h> 36 #include <unistd.h> 37 #include <signal.h> 38 39 void testfile(); 40 void usage(); 41 42 int generate; 43 44 int 45 main(argc, argv) 46 int argc; 47 char **argv; 48 { 49 int c; 50 51 signal(SIGFPE, SIG_IGN); 52 53 while ((c = getopt(argc, argv, "g")) != -1) 54 switch (c) { 55 case 'g': 56 generate = 1; 57 break; 58 59 default: 60 usage(); 61 break; 62 } 63 64 argc -= optind; 65 argv += optind; 66 67 if (argc == 0) 68 testfile(); 69 else 70 for (; argc != 0; argc--, argv++) { 71 if (freopen(argv[0], "r", stdin) == NULL) { 72 fprintf(stderr, 73 "divremtest: couldn't open %s\n", 74 argv[0]); 75 exit(1); 76 } 77 78 testfile(); 79 } 80 81 exit(0); 82 } 83 84 void 85 testfile() 86 { 87 union operand { 88 unsigned long input; 89 int op_int; 90 unsigned int op_u_int; 91 long op_long; 92 unsigned long op_u_long; 93 } op1, op2, divres, modres, divwant, modwant; 94 char opspec[6]; 95 int encoded, i; 96 97 while (scanf("%6c %lx %lx %lx %lx\n", opspec, &op1.input, 98 &op2.input, &divwant.input, &modwant.input) != EOF) { 99 100 encoded = 0; 101 102 for (i = 0; i < 6; i += 2) { 103 int posval; 104 105 switch (opspec[i]) { 106 case '.': 107 posval = 0; 108 break; 109 case '-': 110 posval = 1; 111 break; 112 default: 113 fprintf(stderr, 114 "unknown signedness spec %c\n", 115 opspec[i]); 116 exit(1); 117 } 118 encoded |= posval << ((5 - i) * 4); 119 } 120 121 for (i = 1; i < 6; i += 2) { 122 int posval; 123 124 switch (opspec[i]) { 125 case 'i': 126 posval = 0; 127 break; 128 case 'l': 129 posval = 1; 130 break; 131 default: 132 fprintf(stderr, "unknown length spec %c\n", 133 opspec[i]); 134 exit(1); 135 } 136 encoded |= posval << ((5 - i) * 4); 137 } 138 139 /* KILL ME!!! */ 140 switch (encoded) { 141 142 #define TRY_IT(a, b, c) \ 143 divres.a = op1.b / op2.c; \ 144 modres.a = op1.b % op2.c; \ 145 if (generate) { \ 146 printf("%6s 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n", \ 147 opspec, op1.input, op2.input, \ 148 divres.a, modres.a); \ 149 } else { \ 150 if ((divres.a != divwant.a) || \ 151 (modres.a != modwant.a)) { \ 152 fprintf(stderr, "%6s 0x%016lx 0x%016lx\n", \ 153 opspec, op1.input, op2.input); \ 154 fprintf(stderr, "FAILED:\n"); \ 155 fprintf(stderr, \ 156 "div:\twanted 0x%16lx, got 0x%16lx\n", \ 157 divwant.a, divres.a); \ 158 fprintf(stderr, \ 159 "mod:\twanted 0x%16lx, got 0x%16lx\n", \ 160 modwant.a, modres.a); \ 161 \ 162 exit(1); \ 163 } \ 164 } 165 166 #include "cases.c" 167 168 #undef TRY_IT 169 170 default: 171 fprintf(stderr, 172 "INTERNAL ERROR: unknown encoding %x\n", encoded); 173 exit(1); 174 } 175 } 176 } 177 178 void 179 usage() 180 { 181 182 fprintf(stderr, "usage: divremtest [-v] [testfile ...]\n"); 183 exit(1); 184 } 185