summaryrefslogtreecommitdiff
path: root/sim/testsuite/common/bits-tst.c
blob: 5a4210a76abe2836c6aa9fb832e25913cac6cb06 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# 2 "bits-tst.c"

/* Drive the bit test routines */


long long
calc (const char *call,
      long long val,
      int row,
      int col)
{
  if (strcmp (call, "MASK") == 0)
    return MASKED (val, row, col);
  if (strcmp (call, "MASK8") == 0)
    return MASKED8 (val, row, col);
  if (strcmp (call, "MASK16") == 0)
    return MASKED16 (val, row, col);
  if (strcmp (call, "MASK32") == 0)
    return MASKED32 (val, row, col);
  if (strcmp (call, "MASK64") == 0)
    return MASKED64 (val, row, col);

  if (strcmp (call, "EXTRACT") == 0)
    return EXTRACTED (val, row, col);
  if (strcmp (call, "EXTRACT8") == 0)
    return EXTRACTED8 (val, row, col);
  if (strcmp (call, "EXTRACT16") == 0)
    return EXTRACTED16 (val, row, col);
  if (strcmp (call, "EXTRACT32") == 0)
    return EXTRACTED32 (val, row, col);
  if (strcmp (call, "EXTRACT64") == 0)
    return EXTRACTED64 (val, row, col);

  if (strcmp (call, "LSEXTRACT") == 0)
    return LSEXTRACTED (val, row, col);
  if (strcmp (call, "LSEXTRACT8") == 0)
    return LSEXTRACTED8 (val, row, col);
  if (strcmp (call, "LSEXTRACT16") == 0)
    return LSEXTRACTED16 (val, row, col);
  if (strcmp (call, "LSEXTRACT32") == 0)
    return LSEXTRACTED32 (val, row, col);
  if (strcmp (call, "LSEXTRACT64") == 0)
    return LSEXTRACTED64 (val, row, col);

  if (strcmp (call, "MSEXTRACT") == 0)
    return MSEXTRACTED (val, row, col);
  if (strcmp (call, "MSEXTRACT8") == 0)
    return MSEXTRACTED8 (val, row, col);
  if (strcmp (call, "MSEXTRACT16") == 0)
    return MSEXTRACTED16 (val, row, col);
  if (strcmp (call, "MSEXTRACT32") == 0)
    return MSEXTRACTED32 (val, row, col);
  if (strcmp (call, "MSEXTRACT64") == 0)
    return MSEXTRACTED64 (val, row, col);

  if (strcmp (call, "INSERT") == 0)
    return INSERTED (val, row, col);
  if (strcmp (call, "INSERT8") == 0)
    return INSERTED8 (val, row, col);
  if (strcmp (call, "INSERT16") == 0)
    return INSERTED16 (val, row, col);
  if (strcmp (call, "INSERT32") == 0)
    return INSERTED32 (val, row, col);
  if (strcmp (call, "INSERT64") == 0)
    return INSERTED64 (val, row, col);

  if (strcmp (call, "LSINSERT") == 0)
    return LSINSERTED (val, row, col);
  if (strcmp (call, "LSINSERT8") == 0)
    return LSINSERTED8 (val, row, col);
  if (strcmp (call, "LSINSERT16") == 0)
    return LSINSERTED16 (val, row, col);
  if (strcmp (call, "LSINSERT32") == 0)
    return LSINSERTED32 (val, row, col);
  if (strcmp (call, "LSINSERT64") == 0)
    return LSINSERTED64 (val, row, col);

  if (strcmp (call, "MSINSERT") == 0)
    return MSINSERTED (val, row, col);
  if (strcmp (call, "MSINSERT8") == 0)
    return MSINSERTED8 (val, row, col);
  if (strcmp (call, "MSINSERT16") == 0)
    return MSINSERTED16 (val, row, col);
  if (strcmp (call, "MSINSERT32") == 0)
    return MSINSERTED32 (val, row, col);
  if (strcmp (call, "MSINSERT64") == 0)
    return MSINSERTED64 (val, row, col);

  if (strcmp (call, "MSMASK") == 0)
    return MSMASKED (val, row, col);
  if (strcmp (call, "MSMASK8") == 0)
    return MSMASKED8 (val, row, col);
  if (strcmp (call, "MSMASK16") == 0)
    return MSMASKED16 (val, row, col);
  if (strcmp (call, "MSMASK32") == 0)
    return MSMASKED32 (val, row, col);
  if (strcmp (call, "MSMASK64") == 0)
    return MSMASKED64 (val, row, col);

  if (strcmp (call, "LSMASK") == 0)
    return LSMASKED (val, row, col);
  if (strcmp (call, "LSMASK8") == 0)
    return LSMASKED8 (val, row, col);
  if (strcmp (call, "LSMASK16") == 0)
    return LSMASKED16 (val, row, col);
  if (strcmp (call, "LSMASK32") == 0)
    return LSMASKED32 (val, row, col);
  if (strcmp (call, "LSMASK64") == 0)
    return LSMASKED64 (val, row, col);

  if (strcmp (call, "ROT64") == 0)
    return ROT64 (val, col);
  if (strcmp (call, "ROT8") == 0)
    return ROT8 (val, col);
  if (strcmp (call, "ROT16") == 0)
    return ROT16 (val, col);
  if (strcmp (call, "ROT32") == 0)
    return ROT32 (val, col);

  if (strcmp (call, "SEXT") == 0)
    return SEXT (val, col);
  if (strcmp (call, "SEXT8") == 0)
    return SEXT8 (val, col);
  if (strcmp (call, "SEXT16") == 0)
    return SEXT16 (val, col);
  if (strcmp (call, "SEXT32") == 0)
    return SEXT32 (val, col);
  if (strcmp (call, "SEXT64") == 0)
    return SEXT64 (val, col);

  if (strcmp (call, "LSSEXT") == 0)
    return LSSEXT (val, col);
  if (strcmp (call, "LSSEXT8") == 0)
    return LSSEXT8 (val, col);
  if (strcmp (call, "LSSEXT16") == 0)
    return LSSEXT16 (val, col);
  if (strcmp (call, "LSSEXT32") == 0)
    return LSSEXT32 (val, col);
  if (strcmp (call, "LSSEXT64") == 0)
    return LSSEXT64 (val, col);

  if (strcmp (call, "MSSEXT8") == 0)
    return MSSEXT8 (val, col);
  if (strcmp (call, "MSSEXT16") == 0)
    return MSSEXT16 (val, col);
  if (strcmp (call, "MSSEXT32") == 0)
    return MSSEXT32 (val, col);
  if (strcmp (call, "MSSEXT64") == 0)
    return MSSEXT64 (val, col);
  if (strcmp (call, "MSSEXT") == 0)
    return MSSEXT (val, col);

  else
    {
      fprintf (stderr,
	       "Unknown call passed to calc (%s, 0x%08lx%08lx, %d, %d)\n",
	       call, (long)(val >> 32), (long)val, row, col);
      abort ();
      return val;
    }
}


int
check_sext (int nr_bits,
	    int msb_nr,
	    const char *sexted,
	    const char *masked,
	    const char *msmasked)
{
  int errors = 0;
  int col;
  for (col = 0; col < nr_bits; col ++)
    {
      long long mask = calc (masked, -1, col, col);
      long long msmask = calc (msmasked, -1,
			       0, (msb_nr ? nr_bits - col - 1 : col));
      long long sext = calc (sexted, mask, -1, col);
      long long mask_1 = mask >> 1;
      long long sext_1 = calc (sexted, mask_1, -1, col);
      long long mask_0 = (mask << 1) | mask_1;
      long long sext_0 = calc (sexted, mask_0, -1, col);
      if (sext_0 != mask_1)
	{
	  fprintf (stderr,
		   "%s:%d: ", __FILE__, __LINE__);
	  fprintf (stderr,
		   " %s(0x%08lx%08lx,%d) == 0x%08lx%08lx wrong, != 0x%08lx%08lx\n",
		   sexted, (long)(mask_0 >> 32), (long)mask_0, col,
		   (long)(sext_0 >> 32), (long)sext_0,
		   (long)(mask_1 >> 32), (long)mask_1);
	  errors ++;
	}
      if (sext_1 != mask_1)
	{
	  fprintf (stderr,
		   "%s:%d: ", __FILE__, __LINE__);
	  fprintf (stderr,
		   " %s(0x%08lx%08lx,%d) == 0x%08lx%08lx wrong, != 0x%08lx%08lx\n",
		   sexted, (long)(mask_1 >> 32), (long)mask_1, col,
		   (long)(sext_1 >> 32), (long)sext_1,
		   (long)(mask_1 >> 32), (long)mask_1);
	  errors ++;
	}
      if (sext != msmask)
	{
	  fprintf (stderr,
		   "%s:%d: ", __FILE__, __LINE__);
	  fprintf (stderr,
		   " %s(0x%08lx%08lx,%d) == 0x%08lx%08lx wrong, != 0x%08lx%08lx (%s(%d,%d))\n",
		   sexted, (long)(mask >> 32), (long)mask, col,
		   (long)(sext >> 32), (long)sext,
		   (long)(msmask >> 32), (long)msmask,
		   msmasked, 0, (msb_nr ? nr_bits - col - 1 : col));
	  errors ++;
	}

    }
  return errors;
}


int
check_rot (int nr_bits,
	   const char *roted,
	   const char *masked)
{
  int errors = 0;
  int row;
  int col;
  for (row = 0; row < nr_bits; row++)
    for (col = 0; col < nr_bits; col++)
      if ((WITH_TARGET_WORD_MSB == 0 && row <= col)
	  || (WITH_TARGET_WORD_MSB != 0 && row >= col))
	{
	  long long mask = calc (masked, -1, row, col);
	  int shift;
	  for (shift = -nr_bits + 1; shift < nr_bits; shift ++)
	    {
	      long long rot = calc (roted, mask, -1, shift);
	      long long urot = calc (roted, rot, -1, -shift);
	      if (mask != urot
		  || (shift == 0 && rot != mask)
		  || (shift != 0 && rot == mask && abs(row - col) != (nr_bits - 1)))
		{
		  fprintf (stderr, "%s:%d: ", __FILE__, __LINE__);
		  fprintf (stderr, " %s(%s(0x%08lx%08lx,%d) == 0x%08lx%08lx, %d) failed\n",
			   roted, roted,
			   (long)(mask >> 32), (long)mask, shift,
			   (long)(urot >> 32), (long)urot, -shift);
		  errors ++;
		}
	    }
	}
  return errors;
}


int
check_extract (int nr_bits,
	       const char *extracted,
	       const char *inserted,
	       const char *masked)
{
  int errors = 0;
  int row;
  int col;
  for (row = 0; row < nr_bits; row++)
    for (col = 0; col < nr_bits; col ++)
      if ((WITH_TARGET_WORD_MSB == 0 && row <= col)
	  || (WITH_TARGET_WORD_MSB != 0 && row >= col))
	{
	  long long mask = calc (masked, -1, row, col);
	  long long extr = calc (extracted, mask, row, col);
	  long long inst = calc (inserted, extr, row, col);
	  if (mask != inst)
	    {
	      fprintf (stderr, "%s:%d: ", __FILE__, __LINE__);
	      fprintf (stderr, " %s(%d,%d)=0x%08lx%08lx -> %s=0x%08lx%08lx -> %s=0x%08lx%08lx failed\n",
		       masked, row, col, (long)(mask >> 32), (long)mask,
		       extracted, (long)(extr >> 32), (long)extr,
		       inserted, (long)(inst >> 32), (long)inst);
	      errors ++;
	    }
	}
  return errors;
}


int
check_bits (int call,
	    test_spec **tests)
{
  int r;
  int c;
  int errors = 0;
  while (*tests != NULL)
    {
      int nr_rows = (*tests)->nr_rows;
      int nr_cols = (*tests)->nr_cols;
      test_tuples *tuples = (*tests)->tuples;
      for (r = 0; r < nr_rows; r++)
	for (c = 0; c < nr_cols; c++)
	  {
	    int i = r * nr_rows + c;
	    test_tuples *tuple = &tuples[i];
	    if (tuple->col >= 0)
	      {
		long long val = (!call ? tuple->val : calc ((*tests)->macro, -1,
							    tuple->row, tuple->col));
		long long check = tuple->check;
		if (val != check)
		  {
		    fprintf (stderr, "%s:%d:", (*tests)->file, tuple->line);
		    fprintf (stderr, " %s", (*tests)->macro);
		    if (tuple->row >= 0)
		      fprintf (stderr, " (%d, %d)", tuple->row, tuple->col);
		    else
		      fprintf (stderr, " (%d)", tuple->col);
		    fprintf (stderr, " == 0x%08lx%08lx wrong, != 0x%08lx%08lx\n",
			     (long) (val >> 32), (long) val,
			     (long) (check >> 32), (long) check);
		    errors ++;
		  }
	      }
	  }
      tests ++;
    }
  return errors;
}     


int
main (argc, argv)
     int argc;
     char **argv;
{
  int errors = 0;


#if defined (DO_BIT_TESTS)
  printf ("Checking BIT*\n");
  errors += check_bits (0, bit_tests);
#endif


#if defined (DO_MASK_TESTS)
  printf ("Checking MASK*\n");
  errors += check_bits (0, mask_tests);

  printf ("Checking MASKED*\n");
  errors += check_bits (1, mask_tests);
#endif


#if defined (DO_LSMASK_TESTS)
  printf ("Checking LSMASK*\n");
  errors += check_bits (0, lsmask_tests);

  printf ("Checking LSMASKED*\n");
  errors += check_bits (1, lsmask_tests);
#endif


#if defined (DO_MSMASK_TESTS)
  printf ("Checking MSMASK*\n");
  errors += check_bits (0, msmask_tests);

  printf ("Checking MSMASKED*\n");
  errors += check_bits (1, msmask_tests);
#endif


  printf ("Checking EXTRACTED*\n");
  errors += check_extract ( 8, "EXTRACT8",  "INSERT8",  "MASK8");
  errors += check_extract (16, "EXTRACT16", "INSERT16", "MASK16");
  errors += check_extract (32, "EXTRACT32", "INSERT32", "MASK32");
  errors += check_extract (64, "EXTRACT64", "INSERT64", "MASK64");
  errors += check_extract (64, "EXTRACT",   "INSERT",   "MASK");

  printf ("Checking SEXT*\n");
  errors += check_sext ( 8, WITH_TARGET_WORD_MSB, "SEXT8",  "MASK8",  "MSMASK8");
  errors += check_sext (16, WITH_TARGET_WORD_MSB, "SEXT16", "MASK16", "MSMASK16");
  errors += check_sext (32, WITH_TARGET_WORD_MSB, "SEXT32", "MASK32", "MSMASK32");
  errors += check_sext (64, WITH_TARGET_WORD_MSB, "SEXT64", "MASK64", "MSMASK64");
  errors += check_sext (64, WITH_TARGET_WORD_MSB, "SEXT",   "MASK",   "MSMASK");
  
  printf ("Checking LSSEXT*\n");
  errors += check_sext ( 8,  8 - 1, "LSSEXT8",  "LSMASK8",  "MSMASK8");
  errors += check_sext (16, 16 - 1, "LSSEXT16", "LSMASK16", "MSMASK16");
  errors += check_sext (32, 32 - 1, "LSSEXT32", "LSMASK32", "MSMASK32");
  errors += check_sext (64, 64 - 1, "LSSEXT64", "LSMASK64", "MSMASK64");
  errors += check_sext (64, WITH_TARGET_WORD_BITSIZE - 1, "LSSEXT",   "LSMASK",   "MSMASK");
  
  printf ("Checking MSSEXT*\n");
  errors += check_sext (8,   0, "MSSEXT8",  "MSMASK8",  "MSMASK8");
  errors += check_sext (16,  0, "MSSEXT16", "MSMASK16", "MSMASK16");
  errors += check_sext (32,  0, "MSSEXT32", "MSMASK32", "MSMASK32");
  errors += check_sext (64,  0, "MSSEXT64", "MSMASK64", "MSMASK64");
  errors += check_sext (64,  0, "MSSEXT",   "MSMASK",   "MSMASK");
  
  printf ("Checking ROT*\n");
  errors += check_rot (16, "ROT16", "MASK16");
  errors += check_rot (32, "ROT32", "MASK32");
  errors += check_rot (64, "ROT64", "MASK64");

  return errors != 0;
}