summaryrefslogtreecommitdiff
path: root/sim/testsuite/common/alu-n-tst.h
blob: cd218d4339d655417b2fab2de929c7b3e9369da9 (plain)
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#ifndef N
#error "N must be #defined"
#endif

#include "symcat.h"

/* NOTE: see end of file for #undef of these macros */
#define unsignedN    XCONCAT2(unsigned,N)
#define MAX_INT      XCONCAT2(MAX_INT,N)
#define MIN_INT      XCONCAT2(MIN_INT,N)
#define alu_N_tests     XCONCAT3(alu_,N,_tests)
#define do_alu_N_tests  XCONCAT3(do_alu_,N,_tests)
#define OP_BEGIN     XCONCAT3(ALU,N,_BEGIN)
#define OP_ADDC      XCONCAT3(ALU,N,_ADDC)
#define OP_ADDC_C    XCONCAT3(ALU,N,_ADDC_C)
#define OP_SUBC      XCONCAT3(ALU,N,_SUBC)
#define OP_SUBB      XCONCAT3(ALU,N,_SUBB)
#define OP_SUBB_B    XCONCAT3(ALU,N,_SUBB_B)
#define OP_SUBC_X    XCONCAT3(ALU,N,_SUBC_X)
#define OP_NEGC      XCONCAT3(ALU,N,_NEGC)
#define OP_NEGB      XCONCAT3(ALU,N,_NEGB)
#define HAD_CARRY_BORROW    (XCONCAT3(ALU,N,_HAD_CARRY_BORROW) != 0)
#define HAD_OVERFLOW (XCONCAT3(ALU,N,_HAD_OVERFLOW) != 0)
#define RESULT          XCONCAT3(ALU,N,_RESULT)
#define CARRY_BORROW_RESULT    XCONCAT3(ALU,N,_CARRY_BORROW_RESULT)
#define OVERFLOW_RESULT XCONCAT3(ALU,N,_OVERFLOW_RESULT)
#define do_op_N      XCONCAT2(do_op_,N)


void
do_op_N (const alu_test *tst)
{
  const alu_op *op;
  int borrow_p = 0;
  OP_BEGIN (tst->begin);
  print_hex (tst->begin, N);
  for (op = tst->ops; op->op != NULL; op++)
    {
      printf (" %s ", op->op);
      print_hex (op->arg, N);
      if (strcmp (op->op, "ADDC") == 0)
	OP_ADDC (op->arg);
      else if (strcmp (op->op, "ADDC_C0") == 0)
	OP_ADDC_C (op->arg, 0);
      else if (strcmp (op->op, "ADDC_C1") == 0)
	OP_ADDC_C (op->arg, 1);
      else if (strcmp (op->op, "SUBC") == 0)
	OP_SUBC (op->arg);
      else if (strcmp (op->op, "SUBC_X0") == 0)
	OP_SUBC_X (op->arg, 0);
      else if (strcmp (op->op, "SUBC_X1") == 0)
	OP_SUBC_X (op->arg, 1);
      else if (strcmp (op->op, "SUBB") == 0)
	{
	  OP_SUBB (op->arg);
	  borrow_p ++;
	}
      else if (strcmp (op->op, "SUBB_B0") == 0)
	{
	  OP_SUBB_B (op->arg, 0);
	  borrow_p ++;
	}
      else if (strcmp (op->op, "SUBB_B1") == 0)
	{
	  OP_SUBB_B (op->arg, 1);
	  borrow_p ++;
	}
      else if (strcmp (op->op, "NEGC") == 0)
	OP_NEGC ();
      else if (strcmp (op->op, "NEGB") == 0)
	{
	  OP_NEGB ();
	  borrow_p ++;
	}
      else
	{
	  printf (" -- operator unknown\n");
	  abort ();
	}
    }
  printf (" = ");
  print_hex (tst->result, N);
  if (borrow_p)
    printf (" B:%d", tst->carry_borrow);
  else
    printf (" C:%d", tst->carry_borrow);
  printf (" V:%d", tst->overflow);
  if (tst->carry_borrow != HAD_CARRY_BORROW)
    {
      if (borrow_p)
	printf (" -- borrow (%d) wrong", HAD_CARRY_BORROW);
      else
	printf (" -- carry (%d) wrong", HAD_CARRY_BORROW);
      errors ++;
    }
  if (tst->overflow != HAD_OVERFLOW)
    {
      printf (" -- overflow (%d) wrong", HAD_OVERFLOW);
      errors ++;
    }
  if ((unsignedN) CARRY_BORROW_RESULT != (unsignedN) tst->result)
    {
      printf (" -- result for carry/borrow wrong ");
      print_hex (CARRY_BORROW_RESULT, N);
      errors ++;
    }
  if ((unsignedN) OVERFLOW_RESULT != (unsignedN) tst->result)
    {
      printf (" -- result for overflow wrong ");
      print_hex (OVERFLOW_RESULT, N);
      errors ++;
    }
  if ((unsignedN) RESULT != (unsignedN) tst->result)
    {
      printf (" -- result wrong ");
      print_hex (RESULT, N);
      errors ++;
    }
  printf ("\n");
}


const alu_test alu_N_tests[] = {

  /* 0 + 0; 0 + 1; 1 + 0; 1 + 1 */
  { 0, { { "ADDC", 0 }, }, 0, 0, 0, },
  { 0, { { "ADDC", 1 }, }, 1, 0, 0, },
  { 1, { { "ADDC", 0 }, }, 1, 0, 0, },
  { 1, { { "ADDC", 1 }, }, 2, 0, 0, },

  /* 0 + 0 + 0; 0 + 0 + 1; 0 + 1 + 0; 0 + 1 + 1 */
  /* 1 + 0 + 0; 1 + 0 + 1; 1 + 1 + 0; 1 + 1 + 1 */
  { 0, { { "ADDC_C0", 0 }, }, 0, 0, 0, },
  { 0, { { "ADDC_C0", 1 }, }, 1, 0, 0, },
  { 0, { { "ADDC_C1", 0 }, }, 1, 0, 0, },
  { 0, { { "ADDC_C1", 1 }, }, 2, 0, 0, },
  { 1, { { "ADDC_C0", 0 }, }, 1, 0, 0, },
  { 1, { { "ADDC_C0", 1 }, }, 2, 0, 0, },
  { 1, { { "ADDC_C1", 0 }, }, 2, 0, 0, },
  { 1, { { "ADDC_C1", 1 }, }, 3, 0, 0, },

  /* */
  { MAX_INT, { { "ADDC", 1 }, }, MIN_INT, 0, 1, },
  { MIN_INT, { { "ADDC", -1 }, }, MAX_INT, 1, 1, },
  { MAX_INT, { { "ADDC", MIN_INT }, }, -1, 0, 0, },
  { MIN_INT, { { "ADDC", MAX_INT }, }, -1, 0, 0, },
  { MAX_INT, { { "ADDC", MAX_INT }, }, MAX_INT << 1, 0, 1, },
  { MIN_INT, { { "ADDC", MIN_INT }, }, 0, 1, 1, },
  /* */
  { 0, { { "ADDC_C1", -1 }, }, 0, 1, 0, },
  { 0, { { "ADDC_C1", -2 }, }, -1, 0, 0, },
  { -1, { { "ADDC_C1", 0 }, }, 0, 1, 0, },
  { 0, { { "ADDC_C0", 0 }, }, 0, 0, 0, },
  { -1, { { "ADDC_C1", -1 }, }, -1, 1, 0, },
  { -1, { { "ADDC_C1", 1 }, }, 1, 1, 0, },
  { 0, { { "ADDC_C1", MAX_INT }, }, MIN_INT, 0, 1, },
  { MAX_INT, { { "ADDC_C1", 1 }, }, MIN_INT + 1, 0, 1, },
  { MAX_INT, { { "ADDC_C1", MIN_INT }, }, 0, 1, 0, },
  { MAX_INT, { { "ADDC_C1", MAX_INT }, }, (MAX_INT << 1) + 1, 0, 1, },
  { MAX_INT, { { "ADDC_C0", MAX_INT }, }, MAX_INT << 1, 0, 1, },

  /* 0 - 0 */
  { 0, { { "SUBC",    0 }, },  0, 1, 0, },
  { 0, { { "SUBB",    0 }, },  0, 0, 0, },

  /* 0 - 1 */
  { 0, { { "SUBC",    1 }, }, -1, 0, 0, },
  { 0, { { "SUBB",    1 }, }, -1, 1, 0, },

  /* 1 - 0 */
  { 1, { { "SUBC",    0 }, },  1, 1, 0, },
  { 1, { { "SUBB",    0 }, },  1, 0, 0, },

  /* 1 - 1 */
  { 1, { { "SUBC",    1 }, },  0, 1, 0, },
  { 1, { { "SUBB",    1 }, },  0, 0, 0, },

  /* 0 - 0 - 0 */
  { 0, { { "SUBC_X0", 0 }, }, -1, 0, 0, },
  { 0, { { "SUBB_B0", 0 }, },  0, 0, 0, },

  /* 0 - 0 - 1 */
  { 0, { { "SUBC_X0", 1 }, }, -2, 0, 0, },
  { 0, { { "SUBB_B0", 1 }, }, -1, 1, 0, },

  /* 0 - 1 - 0 */
  { 0, { { "SUBC_X1", 0 }, },  0, 1, 0, },
  { 0, { { "SUBB_B1", 0 }, }, -1, 1, 0, },

  /* 0 - 1 - 1 */
  { 0, { { "SUBC_X1", 1 }, }, -1, 0, 0, },
  { 0, { { "SUBB_B1", 1 }, }, -2, 1, 0, },

  /* 1 - 0 - 0 */
  { 1, { { "SUBC_X0", 0 }, },  0, 1, 0, },
  { 1, { { "SUBB_B0", 0 }, },  1, 0, 0, },

  /* 1 - 0 - 1 */
  { 1, { { "SUBC_X0", 1 }, }, -1, 0, 0, },
  { 1, { { "SUBB_B0", 1 }, },  0, 0, 0, },

  /* 1 - 1 - 0 */
  { 1, { { "SUBC_X1", 0 }, },  1, 1, 0, },
  { 1, { { "SUBB_B1", 0 }, },  0, 0, 0, },

  /* 1 - 1 - 1 */
  { 1, { { "SUBC_X1", 1 }, },  0, 1, 0, },
  { 1, { { "SUBB_B1", 1 }, }, -1, 1, 0, },

  /* */
  { 0,       { { "SUBC", MIN_INT }, }, MIN_INT, 0, 1, },
  { MIN_INT, { { "SUBC", 1 }, }, MAX_INT, 1, 1, },
  { MAX_INT, { { "SUBC", MAX_INT }, }, 0, 1, 0, },

  /* */
  { 0,       { { "SUBC_X0", MIN_INT }, }, MAX_INT, 0, 0, },
  { MIN_INT, { { "SUBC_X1",       0 }, }, MIN_INT, 1, 0, },
  { MAX_INT, { { "SUBC_X0", MAX_INT }, },      -1, 0, 0, },

  /* */
  { MAX_INT, { { "NEGC", 0 }, }, MIN_INT + 1, 0, 0, },
  { MAX_INT, { { "NEGC", 0 }, }, MIN_INT + 1, 0, 0, },
  { MIN_INT, { { "NEGC", 0 }, }, MIN_INT, 0, 1, },
  { 0, { { "NEGC", 0 }, }, 0, 1, 0, },
  { -1, { { "NEGC", 0 }, }, 1, 0, 0, },
  { 1, { { "NEGC", 0 }, }, -1, 0, 0, },
};


static void
do_alu_N_tests (void)
{
  int i;
  for (i = 0; i < sizeof (alu_N_tests) / sizeof (*alu_N_tests); i++)
    {
      const alu_test *tst = &alu_N_tests[i];
      do_op_N (tst);
    }
}


#undef OP_BEGIN
#undef OP_ADDC
#undef OP_ADDC_C
#undef OP_SUBB
#undef OP_SUBC
#undef OP_SUBC_X
#undef OP_SUBB_B
#undef HAD_OVERFLOW
#undef HAD_CARRY_BORROW
#undef OVERFLOW_RESULT
#undef CARRY_BORROW_RESULT
#undef RESULT
#undef do_op_N
#undef unsignedN
#undef MAX_INT
#undef MIN_INT
#undef alu_N_tests
#undef do_alu_N_tests