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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
|
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <setjmp.h>
#if defined TEST_NATIVE
#define NATIVE_SIGNED int
#define NATIVE_UNSIGNED unsigned
#define NATIVE_UNSIGNED_BITS 32
#define NATIVE_SIGNED_MIN LONG_MIN
#define NATIVE_SIGNED_MAX LONG_MAX
#elif defined TEST_SIMUL
#define SIMUL_ARITH_SUBTYPE unsigned short
#define SIMUL_SUBTYPE_BITS 16
#define SIMUL_NUMBITS 31
#else
#error ====== Either TEST_NATIVE or TEST_SIMUL must be defined.
#endif
#define ARITH_TYPENAME zoinx
#define ARITH_FUNCTION_HEADER static inline
#define ARITH_WARNING(type) z_warn(type)
#define ARITH_ERROR(type) z_error(type)
void z_warn(int type);
void z_error(int type);
#include "arith.c"
#if defined TEST_NATIVE
static inline u_zoinx unsigned_to_uz(unsigned x)
{
return (u_zoinx)x;
}
static inline s_zoinx int_to_sz(int x)
{
return (s_zoinx)x;
}
static inline void print_uz(u_zoinx x)
{
printf("%u", x);
}
static inline void print_sz(s_zoinx x)
{
printf("%d", x);
}
#else
static inline u_zoinx unsigned_to_uz(unsigned x)
{
u_zoinx v;
v.msw = (x >> 16) & 0x7FFFU;
v.lsw = x & 0xFFFFU;
return v;
}
static inline s_zoinx int_to_sz(int x)
{
return unsigned_to_uz((unsigned)x);
}
static inline void print_uz(u_zoinx x)
{
printf("%u", ((unsigned)(x.msw) << 16) + (unsigned)(x.lsw));
}
static inline void print_sz(s_zoinx x)
{
if (x.msw & 0x4000U) {
putchar('-');
x = zoinx_u_neg(x);
}
print_uz(x);
}
#endif
static inline void print_int(int x)
{
printf("%d", x);
}
static jmp_buf jbuf;
void z_warn(int type)
{
switch (type) {
case ARITH_EXCEP_CONV_O:
fputs("[overflow on conversion] ", stdout); break;
case ARITH_EXCEP_NEG_O:
fputs("[overflow on unary minus] ", stdout); break;
case ARITH_EXCEP_NOT_T:
fputs("[trap representation on bitwise inversion] ", stdout);
break;
case ARITH_EXCEP_PLUS_O:
fputs("[overflow on addition] ", stdout); break;
case ARITH_EXCEP_PLUS_U:
fputs("[underflow on addition] ", stdout); break;
case ARITH_EXCEP_MINUS_O:
fputs("[overflow on subtraction] ", stdout); break;
case ARITH_EXCEP_MINUS_U:
fputs("[underflow on subtraction] ", stdout); break;
case ARITH_EXCEP_AND_T:
fputs("[trap representation on bitwise and] ", stdout); break;
case ARITH_EXCEP_XOR_T:
fputs("[trap representation on bitwise xor] ", stdout); break;
case ARITH_EXCEP_OR_T:
fputs("[trap representation on bitwise or] ", stdout); break;
case ARITH_EXCEP_LSH_W:
fputs("[left shift by type width or more] ", stdout); break;
case ARITH_EXCEP_LSH_C:
fputs("[left shift by negative count] ", stdout); break;
case ARITH_EXCEP_LSH_O:
fputs("[overflow on left shift] ", stdout); break;
case ARITH_EXCEP_LSH_U:
fputs("[underflow on left shift] ", stdout); break;
case ARITH_EXCEP_RSH_W:
fputs("[right shift by type width or more] ", stdout); break;
case ARITH_EXCEP_RSH_C:
fputs("[right shift by negative count] ", stdout); break;
case ARITH_EXCEP_RSH_N:
fputs("[right shift of negative value] ", stdout); break;
case ARITH_EXCEP_STAR_O:
fputs("[overflow on multiplication] ", stdout); break;
case ARITH_EXCEP_STAR_U:
fputs("[underflow on multiplication] ", stdout); break;
default:
fprintf(stdout, "UNKNOWN WARNING TYPE: %d\n", type);
exit(EXIT_FAILURE);
}
}
void z_error(int type)
{
switch (type) {
case ARITH_EXCEP_SLASH_D:
fputs("division by 0\n", stdout);
break;
case ARITH_EXCEP_SLASH_O:
fputs("overflow on division\n", stdout);
break;
case ARITH_EXCEP_PCT_D:
fputs("division by 0 on modulus operator\n", stdout);
break;
default:
fprintf(stdout, "UNKNOWN ERROR TYPE: %d\n", type);
exit(EXIT_FAILURE);
}
longjmp(jbuf, 1);
}
int main(void)
{
#define OPTRY_GEN(op, x, y, convx, convy, printz) do { \
printf("%s %s %s -> ", #x, #op, #y); \
if (!setjmp(jbuf)) { \
printz(zoinx_ ## op (convx(x), convy(y))); \
putchar('\n'); \
} \
} while (0)
#define IDENT(x) x
#define OPTRY_UU_U(op, x, y) \
OPTRY_GEN(op, x, y, unsigned_to_uz, unsigned_to_uz, print_uz)
#define OPTRY_UI_U(op, x, y) \
OPTRY_GEN(op, x, y, unsigned_to_uz, IDENT, print_uz)
#define OPTRY_UU_I(op, x, y) \
OPTRY_GEN(op, x, y, unsigned_to_uz, unsigned_to_uz, print_int)
#define OPTRY_SS_S(op, x, y) \
OPTRY_GEN(op, x, y, int_to_sz, int_to_sz, print_sz)
#define OPTRY_SI_S(op, x, y) \
OPTRY_GEN(op, x, y, int_to_sz, IDENT, print_sz)
#define OPTRY_SS_I(op, x, y) \
OPTRY_GEN(op, x, y, int_to_sz, int_to_sz, print_int)
OPTRY_UU_U(u_plus, 3, 4);
OPTRY_UU_U(u_plus, 1549587182, 1790478233);
OPTRY_UU_U(u_minus, 1549587182, 1790478233);
OPTRY_UU_U(u_minus, 1790478233, 1549587182);
OPTRY_UU_U(u_star, 432429875, 347785487);
OPTRY_UU_U(u_slash, 432429875, 34487);
OPTRY_UU_U(u_pct, 432429875, 34487);
OPTRY_UI_U(u_lsh, 1783, 19);
OPTRY_UI_U(u_lsh, 1783, 20);
OPTRY_UI_U(u_lsh, 1783, 21);
OPTRY_UI_U(u_rsh, 475902857, 7);
OPTRY_UI_U(u_rsh, 475902857, 17);
OPTRY_UI_U(u_rsh, 475902857, 38);
OPTRY_SS_S(s_plus, 3, 4);
OPTRY_SS_S(s_plus, 1549587182, 1790478233);
OPTRY_SS_S(s_plus, -1549587182, -1790478233);
OPTRY_SS_S(s_minus, 1549587182, 1790478233);
OPTRY_SS_S(s_minus, 1790478233, 1549587182);
OPTRY_SS_S(s_minus, -1790478233, -1549587182);
OPTRY_SS_S(s_minus, -1790478233, 1549587182);
OPTRY_SS_S(s_star, 432429875, 347785487);
OPTRY_SS_S(s_star, 432429875, -347785487);
OPTRY_SS_S(s_slash, 432429875, 34487);
OPTRY_SS_S(s_slash, -432429875, 34487);
OPTRY_SS_S(s_slash, 432429875, -34487);
OPTRY_SS_S(s_slash, -432429875, -34487);
OPTRY_SS_S(s_slash, 432429875, 0);
OPTRY_SS_S(s_slash, -2147483647 - 1, -1);
OPTRY_SS_S(s_pct, 432429875, 34487);
OPTRY_SS_S(s_pct, 432429875, 0);
OPTRY_SI_S(s_lsh, -1, 10);
OPTRY_SI_S(s_lsh, 1783, 19);
OPTRY_SI_S(s_lsh, 1783, 20);
OPTRY_SI_S(s_lsh, 1783, 21);
OPTRY_SI_S(s_rsh, -1024, 8);
OPTRY_SI_S(s_rsh, 475902857, 7);
OPTRY_SI_S(s_rsh, 475902857, 17);
return 0;
}
|