Remove old autovect-branch by moving to "dead" directory.
[official-gcc.git] / old-autovect-branch / gcc / testsuite / objc.dg / gnu-encoding / struct-layout-encoding-1_generate.c
blob4ed33d554c297e5d18e221808d40301cbc2ec174
1 /* Structure layout test generator.
2 Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 /* Compile with gcc -o struct-layout-1_generate{,.c} generate_random{,_r}.c */
24 /* N.B. -- This program cannot use libiberty as that will not work
25 when testing an installed compiler. */
26 #include <limits.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stddef.h>
31 /* We use our own pseudo-random number generator, so that it gives the same
32 values on all hosts. */
33 #include "generate-random.h"
35 #if LLONG_MAX != 9223372036854775807LL && __LONG_LONG_MAX__ != 9223372036854775807LL
36 # error Need 64-bit long long
37 #endif
39 typedef unsigned int hashval_t;
41 enum TYPE
43 TYPE_INT,
44 TYPE_UINT,
45 TYPE_CINT,
46 TYPE_CUINT,
47 TYPE_FLOAT,
48 TYPE_CFLOAT,
49 TYPE_SENUM,
50 TYPE_UENUM,
51 TYPE_PTR,
52 TYPE_FNPTR,
53 TYPE_OTHER
56 struct types
58 const char *name;
59 enum TYPE type;
60 unsigned long long int maxval;
61 char bitfld;
64 struct types base_types[] = {
65 /* As we don't know whether char will be signed or not, just limit ourselves
66 to unsigned values less than maximum signed char value. */
67 { "char", TYPE_UINT, 127, 'C' },
68 { "signed char", TYPE_INT, 127, 'C' },
69 { "unsigned char", TYPE_UINT, 255, 'C' },
70 { "short int", TYPE_INT, 32767, 'S' },
71 { "unsigned short int", TYPE_UINT, 65535, 'S' },
72 { "int", TYPE_INT, 2147483647, 'I' },
73 { "unsigned int", TYPE_UINT, 4294967295U, 'I' },
74 { "long int", TYPE_INT, 9223372036854775807LL, 'L' },
75 { "unsigned long int", TYPE_UINT, 18446744073709551615ULL, 'L' },
76 { "long long int", TYPE_INT, 9223372036854775807LL, 'Q' },
77 { "unsigned long long int", TYPE_UINT, 18446744073709551615ULL, 'Q' },
78 { "bool", TYPE_UINT, 1, 'B' },
79 { "void *", TYPE_PTR, 0, 0 },
80 { "char *", TYPE_PTR, 0, 0 },
81 { "int *", TYPE_PTR, 0, 0 },
82 { "float", TYPE_FLOAT, 0, 0 },
83 { "double", TYPE_FLOAT, 0, 0 },
84 /*{ "long double", TYPE_FLOAT, 0, 0 },*/
85 /* Disabled as double and long double
86 are encoded thee same, currently */
87 #define NTYPES1 16
88 #if 0
89 /* enums are disabled for now as it seems like their encoding is broken, we should
90 just encode them using their underlaying type but we don't. */
91 { "enum E0", TYPE_UENUM, 0, ' ' },
92 { "enum E1", TYPE_UENUM, 1, ' ' },
93 { "enum E2", TYPE_SENUM, 3, ' ' },
94 { "enum E3", TYPE_SENUM, 127, ' ' },
95 { "enum E4", TYPE_UENUM, 255, ' ' },
96 { "enum E5", TYPE_SENUM, 32767, ' ' },
97 { "enum E6", TYPE_UENUM, 65535, ' ' },
98 { "enum E7", TYPE_SENUM, 2147483647, ' ' },
99 { "enum E8", TYPE_UENUM, 4294967295U, ' ' },
100 { "enum E9", TYPE_SENUM, 1099511627775LL, ' ' },
101 #endif
102 #define NTYPES2 (sizeof (base_types) / sizeof (base_types[0]))
104 struct types complex_types[] = {
105 { "_Complex char", TYPE_CUINT, 127, 0 },
106 { "_Complex signed char", TYPE_CINT, 127, 0 },
107 { "_Complex unsigned char", TYPE_CUINT, 255, 0 },
108 { "_Complex short int", TYPE_CINT, 32767, 0 },
109 { "_Complex unsigned short int", TYPE_CUINT, 65535, 0 },
110 { "_Complex int", TYPE_CINT, 2147483647, 0 },
111 { "_Complex unsigned int", TYPE_CUINT, 4294967295U, 0 },
112 { "_Complex long int", TYPE_CINT, 9223372036854775807LL, 0 },
113 { "_Complex unsigned long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
114 { "_Complex long long int", TYPE_CINT, 9223372036854775807LL, 0 },
115 { "_Complex unsigned long long int", TYPE_CUINT, 18446744073709551615ULL, 0 },
116 { "_Complex float", TYPE_CFLOAT, 0, 0 },
117 { "_Complex double", TYPE_CFLOAT, 0, 0 },
118 /*{ "_Complex long double", TYPE_CFLOAT, 0, 0 }, */
119 /* Disable until long doubles are encoded correctly. */
120 #define NCTYPES2 (sizeof (complex_types) / sizeof (complex_types[0]))
122 struct types vector_types[] = {
123 /* vector-defs.h typedefs */
124 { "v8qi", TYPE_OTHER, 0, 0 },
125 { "v16qi", TYPE_OTHER, 0, 0 },
126 { "v2hi", TYPE_OTHER, 0, 0 },
127 { "v4hi", TYPE_OTHER, 0, 0 },
128 { "v8hi", TYPE_OTHER, 0, 0 },
129 { "v2si", TYPE_OTHER, 0, 0 },
130 { "v4si", TYPE_OTHER, 0, 0 },
131 { "v1di", TYPE_OTHER, 0, 0 },
132 { "v2di", TYPE_OTHER, 0, 0 },
133 { "v2sf", TYPE_OTHER, 0, 0 },
134 { "v4sf", TYPE_OTHER, 0, 0 },
135 { "v16sf", TYPE_OTHER, 0, 0 },
136 { "v2df", TYPE_OTHER, 0, 0 },
137 { "u8qi", TYPE_OTHER, 0, 0 },
138 { "u16qi", TYPE_OTHER, 0, 0 },
139 { "u2hi", TYPE_OTHER, 0, 0 },
140 { "u4hi", TYPE_OTHER, 0, 0 },
141 { "u8hi", TYPE_OTHER, 0, 0 },
142 { "u2si", TYPE_OTHER, 0, 0 },
143 { "u4si", TYPE_OTHER, 0, 0 },
144 { "u1di", TYPE_OTHER, 0, 0 },
145 { "u2di", TYPE_OTHER, 0, 0 },
146 { "u2sf", TYPE_OTHER, 0, 0 },
147 { "u4sf", TYPE_OTHER, 0, 0 },
148 { "u16sf", TYPE_OTHER, 0, 0 },
149 { "u2df", TYPE_OTHER, 0, 0 },
150 { "__m64", TYPE_OTHER, 0, 0 },
151 { "__m128", TYPE_OTHER, 0, 0 }
152 #define NVTYPES2 (sizeof (vector_types) / sizeof (vector_types[0]))
155 struct types bitfld_types[NTYPES2];
156 int n_bitfld_types;
158 enum ETYPE
160 ETYPE_TYPE,
161 ETYPE_ARRAY,
162 ETYPE_BITFLD,
163 ETYPE_STRUCT,
164 ETYPE_UNION,
165 ETYPE_STRUCT_ARRAY,
166 ETYPE_UNION_ARRAY
169 struct entry
171 #ifdef __GNUC__
172 enum ETYPE etype : 8;
173 #else
174 unsigned char etype;
175 #endif
176 unsigned short len;
177 unsigned char arr_len;
178 struct types *type;
179 const char *attrib;
180 /* Used to chain together entries in the hash table. */
181 struct entry *next;
184 /* A prime number giving the number of slots in the hash table. */
185 #define HASH_SIZE 32749
186 static struct entry *hash_table[HASH_SIZE];
188 static int idx, limidx, output_one;
189 static const char *destdir;
190 static const char *srcdir;
191 FILE *outfile;
193 void
194 switchfiles (int fields)
196 static int filecnt;
197 static char *destbuf, *destptr;
198 ++filecnt;
199 if (outfile)
200 fclose (outfile);
201 if (output_one)
203 outfile = stdout;
204 return;
206 if (destbuf == NULL)
208 size_t len = strlen (destdir);
209 destbuf = malloc (len + 20);
210 if (!destbuf)
211 abort ();
212 memcpy (destbuf, destdir, len);
213 if (!len || destbuf[len - 1] != '/')
214 destbuf[len++] = '/';
215 destptr = destbuf + len;
217 sprintf (destptr, "t%03d_main.m", filecnt);
218 outfile = fopen (destbuf, "w");
219 if (outfile == NULL)
221 fail:
222 fputs ("failed to create test files\n", stderr);
223 exit (1);
225 fprintf (outfile, "\
226 /* { dg-do run } */\n\
227 /* { dg-options \"-w -I%s -fgnu-runtime\" } */\n\
228 #include <objc/encoding.h> \n\
229 #include \"struct-layout-1.h\"\n\
231 int fails; \n\
232 #define TX(n, type, attrs, fields, ops) \\\n\
233 type S##n { fields } attrs; \\\n\
234 void test##n (void) \\\n\
235 { \\\n\
236 char *encoding = @encode (type S##n); \\\n\
237 if (objc_sizeof_type (encoding) != sizeof(type S##n)) \\\n\
238 { \\\n\
239 fails ++; \\\n\
240 printf(#type \" { \" #fields \"} size is %%u, but is calulated as %%u\\n\", \\\n\
241 sizeof(type S##n), objc_sizeof_type (encoding)); \\\n\
242 } \\\n\
243 if (objc_alignof_type (encoding) != __alignof__ (type S##n)) \\\n\
244 { \\\n\
245 fails ++; \\\n\
246 printf(#type \" { \" #fields \"} align is %%u, but is calulated as %%u\\n\", \\\n\
247 __alignof__ (type S##n), objc_alignof_type (encoding)); \\\n\
248 } \\\n\
249 }\n\
250 #include \"t%03d_test.h\"\n\
251 #undef TX\n\
253 int main (void)\n\
254 {\n\
255 #define TX(n, type, attrs, fields, ops) test##n ();\n\
256 #include \"t%03d_test.h\"\n\
257 #undef TX\n\
258 if (fails)\n\
259 {\n\
260 fflush (stdout);\n\
261 abort ();\n\
262 }\n\
263 exit (0);\n\
264 }\n", srcdir, filecnt, filecnt);
265 fclose (outfile);
266 sprintf (destptr, "t%03d_test.h", filecnt);
267 outfile = fopen (destbuf, "w");
268 if (outfile == NULL)
269 goto fail;
270 if (fields <= 2)
271 limidx = idx + 300;
272 else if (fields <= 4)
273 limidx = idx + 200;
274 else if (fields <= 6)
275 limidx = idx + 100;
276 else
277 limidx = idx + 50;
280 unsigned long long int
281 getrandll (void)
283 unsigned long long int ret;
284 ret = generate_random () & 0xffffff;
285 ret |= (generate_random () & 0xffffffLL) << 24;
286 ret |= ((unsigned long long int) generate_random ()) << 48;
287 return ret;
291 subfield (struct entry *e, char *letter)
293 int i, type;
294 char buf[20];
295 const char *p;
296 switch (e[0].etype)
298 case ETYPE_STRUCT:
299 case ETYPE_UNION:
300 case ETYPE_STRUCT_ARRAY:
301 case ETYPE_UNION_ARRAY:
302 type = e[0].attrib ? 1 + (generate_random () & 3) : 0;
303 if (e[0].etype == ETYPE_STRUCT || e[0].etype == ETYPE_STRUCT_ARRAY)
304 p = "struct";
305 else
306 p = "union";
307 if (e[0].etype == ETYPE_STRUCT_ARRAY || e[0].etype == ETYPE_UNION_ARRAY)
309 if (e[0].arr_len == 255)
310 snprintf (buf, 20, "%c[]", *letter);
311 else
312 snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
314 else
316 buf[0] = *letter;
317 buf[1] = '\0';
319 ++*letter;
320 switch (type)
322 case 0:
323 case 3:
324 case 4:
325 fprintf (outfile, "%s{", p);
326 break;
327 case 1:
328 fprintf (outfile, "%s %s{", e[0].attrib, p);
329 break;
330 case 2:
331 fprintf (outfile, "%s %s{", p, e[0].attrib);
332 break;
335 for (i = 1; i <= e[0].len; )
336 i += subfield (e + i, letter);
338 switch (type)
340 case 0:
341 case 1:
342 case 2:
343 fprintf (outfile, "}%s;", buf);
344 break;
345 case 3:
346 fprintf (outfile, "}%s %s;", e[0].attrib, buf);
347 break;
348 case 4:
349 fprintf (outfile, "}%s %s;", buf, e[0].attrib);
350 break;
352 return 1 + e[0].len;
353 case ETYPE_TYPE:
354 case ETYPE_ARRAY:
355 if (e[0].etype == ETYPE_ARRAY)
357 if (e[0].arr_len == 255)
358 snprintf (buf, 20, "%c[]", *letter);
359 else
360 snprintf (buf, 20, "%c[%d]", *letter, e[0].arr_len);
362 else
364 buf[0] = *letter;
365 buf[1] = '\0';
367 ++*letter;
368 if (e[0].attrib)
369 switch (generate_random () % 3)
371 case 0:
372 fprintf (outfile, "%s %s %s;", e[0].attrib, e[0].type->name, buf);
373 break;
374 case 1:
375 fprintf (outfile, "%s %s %s;", e[0].type->name, e[0].attrib, buf);
376 break;
377 case 2:
378 fprintf (outfile, "%s %s %s;", e[0].type->name, buf, e[0].attrib);
379 break;
381 else
382 fprintf (outfile, "%s %s;", e[0].type->name, buf);
383 return 1;
384 case ETYPE_BITFLD:
385 if (e[0].len == 0)
387 if (e[0].attrib)
388 switch (generate_random () % 3)
390 case 0:
391 fprintf (outfile, "%s %s:0;", e[0].attrib, e[0].type->name);
392 break;
393 case 1:
394 fprintf (outfile, "%s %s:0;", e[0].type->name, e[0].attrib);
395 break;
396 case 2:
397 fprintf (outfile, "%s:0 %s;", e[0].type->name, e[0].attrib);
398 break;
400 else
401 fprintf (outfile, "%s:0;", e[0].type->name);
402 ++*letter;
403 return 1;
405 switch (e[0].type->bitfld)
407 case 'C':
408 case 'S':
409 case 'I':
410 case 'L':
411 case 'Q':
412 snprintf (buf, 20, "B%cN(%d)", e[0].type->bitfld, e[0].len);
413 break;
414 case 'B':
415 case ' ':
416 snprintf (buf, 20, "%d", e[0].len);
417 break;
418 default:
419 abort ();
421 if (e[0].attrib)
422 switch (generate_random () % 3)
424 case 0:
425 fprintf (outfile, "%s %s %c:%s;", e[0].attrib, e[0].type->name,
426 *letter, buf);
427 break;
428 case 1:
429 fprintf (outfile, "%s %s %c:%s;", e[0].type->name, e[0].attrib,
430 *letter, buf);
431 break;
432 case 2:
433 fprintf (outfile, "%s %c:%s %s;", e[0].type->name, *letter,
434 buf, e[0].attrib);
435 break;
437 else
438 fprintf (outfile, "%s %c:%s;", e[0].type->name, *letter, buf);
439 ++*letter;
440 return 1;
441 default:
442 abort ();
446 char namebuf[1024];
448 void
449 output_FNB (char mode, struct entry *e)
451 unsigned long long int l1, l2, m;
452 int signs = 0;
453 const char *p, *q;
455 if (e->type->type == TYPE_OTHER)
457 if (mode == 'B')
458 abort ();
459 fprintf (outfile, "N(%d,%s)", idx, namebuf);
460 return;
462 fprintf (outfile, "%c(%d,%s,", mode, idx, namebuf);
463 l1 = getrandll ();
464 l2 = getrandll ();
465 switch (e->type->type)
467 case TYPE_INT:
468 signs = generate_random () & 3;
469 m = e->type->maxval;
470 if (mode == 'B')
471 m &= e->len > 1 ? (1ULL << (e->len - 1)) - 1 : 1;
472 l1 &= m;
473 l2 &= m;
474 fprintf (outfile, "%s%llu%s,%s%llu%s",
475 (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
476 (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
477 break;
478 case TYPE_UINT:
479 m = e->type->maxval;
480 if (mode == 'B')
481 m &= (1ULL << e->len) - 1;
482 l1 &= m;
483 l2 &= m;
484 fprintf (outfile, "%lluU%s,%lluU%s", l1, l1 > 4294967295U ? "LL" : "",
485 l2, l2 > 4294967295U ? "LL" : "");
486 break;
487 case TYPE_FLOAT:
488 l1 &= 0xffffff;
489 l2 &= 0xffffff;
490 signs = generate_random () & 3;
491 fprintf (outfile, "%s%f,%s%f", (signs & 1) ? "-" : "",
492 ((double) l1) / 64, (signs & 2) ? "-" : "", ((double) l2) / 64);
493 break;
494 case TYPE_CINT:
495 signs = generate_random () & 3;
496 l1 &= e->type->maxval;
497 l2 &= e->type->maxval;
498 fprintf (outfile, "CINT(%s%llu%s,%s%llu%s),",
499 (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
500 (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
501 signs = generate_random () & 3;
502 l1 = getrandll ();
503 l2 = getrandll ();
504 l1 &= e->type->maxval;
505 l2 &= e->type->maxval;
506 fprintf (outfile, "CINT(%s%llu%s,%s%llu%s)",
507 (signs & 1) ? "-" : "", l1, l1 > 2147483647 ? "LL" : "",
508 (signs & 2) ? "-" : "", l2, l2 > 2147483647 ? "LL" : "");
509 break;
510 case TYPE_CUINT:
511 l1 &= e->type->maxval;
512 l2 &= e->type->maxval;
513 fprintf (outfile, "CINT(%lluU%s,%lluU%s),",
514 l1, l1 > 4294967295U ? "LL" : "",
515 l2, l2 > 4294967295U ? "LL" : "");
516 l1 = getrandll ();
517 l2 = getrandll ();
518 l1 &= e->type->maxval;
519 l2 &= e->type->maxval;
520 fprintf (outfile, "CINT(%lluU%s,%lluU%s)",
521 l1, l1 > 4294967295U ? "LL" : "",
522 l2, l2 > 4294967295U ? "LL" : "");
523 break;
524 case TYPE_CFLOAT:
525 l1 &= 0xffffff;
526 l2 &= 0xffffff;
527 signs = generate_random () & 3;
528 fprintf (outfile, "CDBL(%s%f,%s%f),",
529 (signs & 1) ? "-" : "", ((double) l1) / 64,
530 (signs & 2) ? "-" : "", ((double) l2) / 64);
531 l1 = getrandll ();
532 l2 = getrandll ();
533 l1 &= 0xffffff;
534 l2 &= 0xffffff;
535 signs = generate_random () & 3;
536 fprintf (outfile, "CDBL(%s%f,%s%f)",
537 (signs & 1) ? "-" : "", ((double) l1) / 64,
538 (signs & 2) ? "-" : "", ((double) l2) / 64);
539 break;
540 case TYPE_UENUM:
541 if (e->type->maxval == 0)
542 fputs ("e0_0,e0_0", outfile);
543 else if (e->type->maxval == 1)
544 fprintf (outfile, "e1_%lld,e1_%lld", l1 & 1, l2 & 1);
545 else
547 p = strchr (e->type->name, '\0');
548 while (--p >= e->type->name && *p >= '0' && *p <= '9');
549 p++;
550 l1 %= 7;
551 l2 %= 7;
552 if (l1 > 3)
553 l1 += e->type->maxval - 6;
554 if (l2 > 3)
555 l2 += e->type->maxval - 6;
556 fprintf (outfile, "e%s_%lld,e%s_%lld", p, l1, p, l2);
558 break;
559 case TYPE_SENUM:
560 p = strchr (e->type->name, '\0');
561 while (--p >= e->type->name && *p >= '0' && *p <= '9');
562 p++;
563 l1 %= 7;
564 l2 %= 7;
565 fprintf (outfile, "e%s_%s%lld,e%s_%s%lld",
566 p, l1 < 3 ? "m" : "",
567 l1 == 3 ? 0LL : e->type->maxval - (l1 & 3),
568 p, l2 < 3 ? "m" : "",
569 l2 == 3 ? 0LL : e->type->maxval - (l2 & 3));
570 break;
571 case TYPE_PTR:
572 l1 %= 256;
573 l2 %= 256;
574 fprintf (outfile, "(%s)&intarray[%lld],(%s)&intarray[%lld]",
575 e->type->name, l1, e->type->name, l2);
576 break;
577 case TYPE_FNPTR:
578 l1 %= 10;
579 l2 %= 10;
580 fprintf (outfile, "fn%lld,fn%lld", l1, l2);
581 break;
582 default:
583 abort ();
585 fputs (")", outfile);
589 subvalues (struct entry *e, char *p, char *letter)
591 int i, j;
592 char *q;
593 if (p >= namebuf + sizeof (namebuf) - 32)
594 abort ();
595 p[0] = *letter;
596 p[1] = '\0';
597 q = p + 1;
598 switch (e[0].etype)
600 case ETYPE_STRUCT_ARRAY:
601 case ETYPE_UNION_ARRAY:
602 if (e[0].arr_len == 0 || e[0].arr_len == 255)
604 *letter += 1 + e[0].len;
605 return 1 + e[0].len;
607 i = generate_random () % e[0].arr_len;
608 snprintf (p, sizeof (namebuf) - (p - namebuf) - 1,
609 "%c[%d]", *letter, i);
610 q = strchr (p, '\0');
611 /* FALLTHROUGH */
612 case ETYPE_STRUCT:
613 case ETYPE_UNION:
614 *q++ = '.';
615 ++*letter;
616 for (i = 1; i <= e[0].len; )
618 i += subvalues (e + i, q, letter);
619 if (e[0].etype == ETYPE_UNION || e[0].etype == ETYPE_UNION_ARRAY)
621 *letter += e[0].len - i + 1;
622 break;
625 return 1 + e[0].len;
626 case ETYPE_TYPE:
627 ++*letter;
628 output_FNB ('F', e);
629 return 1;
630 case ETYPE_ARRAY:
631 if (e[0].arr_len == 0 || e[0].arr_len == 255)
633 ++*letter;
634 return 1;
636 i = generate_random () % e[0].arr_len;
637 snprintf (p, sizeof (namebuf) - (p - namebuf),
638 "%c[%d]", *letter, i);
639 output_FNB ('F', e);
640 if ((generate_random () & 7) == 0)
642 j = generate_random () % e[0].arr_len;
643 if (i != j)
645 snprintf (p, sizeof (namebuf) - (p - namebuf),
646 "%c[%d]", *letter, j);
647 output_FNB ('F', e);
650 ++*letter;
651 return 1;
652 case ETYPE_BITFLD:
653 ++*letter;
654 if (e[0].len != 0)
655 output_FNB ('B', e);
656 return 1;
660 /* DERIVED FROM:
661 --------------------------------------------------------------------
662 lookup2.c, by Bob Jenkins, December 1996, Public Domain.
663 hash(), hash2(), hash3, and mix() are externally useful functions.
664 Routines to test the hash are included if SELF_TEST is defined.
665 You can use this free for any purpose. It has no warranty.
666 --------------------------------------------------------------------
670 --------------------------------------------------------------------
671 mix -- mix 3 32-bit values reversibly.
672 For every delta with one or two bit set, and the deltas of all three
673 high bits or all three low bits, whether the original value of a,b,c
674 is almost all zero or is uniformly distributed,
675 * If mix() is run forward or backward, at least 32 bits in a,b,c
676 have at least 1/4 probability of changing.
677 * If mix() is run forward, every bit of c will change between 1/3 and
678 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit deltas.)
679 mix() was built out of 36 single-cycle latency instructions in a
680 structure that could supported 2x parallelism, like so:
681 a -= b;
682 a -= c; x = (c>>13);
683 b -= c; a ^= x;
684 b -= a; x = (a<<8);
685 c -= a; b ^= x;
686 c -= b; x = (b>>13);
688 Unfortunately, superscalar Pentiums and Sparcs can't take advantage
689 of that parallelism. They've also turned some of those single-cycle
690 latency instructions into multi-cycle latency instructions. Still,
691 this is the fastest good hash I could find. There were about 2^^68
692 to choose from. I only looked at a billion or so.
693 --------------------------------------------------------------------
695 /* same, but slower, works on systems that might have 8 byte hashval_t's */
696 #define mix(a,b,c) \
698 a -= b; a -= c; a ^= (c>>13); \
699 b -= c; b -= a; b ^= (a<< 8); \
700 c -= a; c -= b; c ^= ((b&0xffffffff)>>13); \
701 a -= b; a -= c; a ^= ((c&0xffffffff)>>12); \
702 b -= c; b -= a; b = (b ^ (a<<16)) & 0xffffffff; \
703 c -= a; c -= b; c = (c ^ (b>> 5)) & 0xffffffff; \
704 a -= b; a -= c; a = (a ^ (c>> 3)) & 0xffffffff; \
705 b -= c; b -= a; b = (b ^ (a<<10)) & 0xffffffff; \
706 c -= a; c -= b; c = (c ^ (b>>15)) & 0xffffffff; \
710 --------------------------------------------------------------------
711 hash() -- hash a variable-length key into a 32-bit value
712 k : the key (the unaligned variable-length array of bytes)
713 len : the length of the key, counting by bytes
714 level : can be any 4-byte value
715 Returns a 32-bit value. Every bit of the key affects every bit of
716 the return value. Every 1-bit and 2-bit delta achieves avalanche.
717 About 36+6len instructions.
719 The best hash table sizes are powers of 2. There is no need to do
720 mod a prime (mod is sooo slow!). If you need less than 32 bits,
721 use a bitmask. For example, if you need only 10 bits, do
722 h = (h & hashmask(10));
723 In which case, the hash table should have hashsize(10) elements.
725 If you are hashing n strings (ub1 **)k, do it like this:
726 for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);
728 By Bob Jenkins, 1996. bob_jenkins@burtleburtle.net. You may use this
729 code any way you wish, private, educational, or commercial. It's free.
731 See http://burtleburtle.net/bob/hash/evahash.html
732 Use for hash table lookup, or anything where one collision in 2^32 is
733 acceptable. Do NOT use for cryptographic purposes.
734 --------------------------------------------------------------------
737 static hashval_t
738 iterative_hash (const void *k_in /* the key */,
739 register size_t length /* the length of the key */,
740 register hashval_t initval /* the previous hash, or
741 an arbitrary value */)
743 register const unsigned char *k = (const unsigned char *)k_in;
744 register hashval_t a,b,c,len;
746 /* Set up the internal state */
747 len = length;
748 a = b = 0x9e3779b9; /* the golden ratio; an arbitrary value */
749 c = initval; /* the previous hash value */
751 /*---------------------------------------- handle most of the key */
752 while (len >= 12)
754 a += (k[0] +((hashval_t)k[1]<<8) +((hashval_t)k[2]<<16) +((hashval_t)k[3]<<24));
755 b += (k[4] +((hashval_t)k[5]<<8) +((hashval_t)k[6]<<16) +((hashval_t)k[7]<<24));
756 c += (k[8] +((hashval_t)k[9]<<8) +((hashval_t)k[10]<<16)+((hashval_t)k[11]<<24));
757 mix(a,b,c);
758 k += 12; len -= 12;
761 /*------------------------------------- handle the last 11 bytes */
762 c += length;
763 switch(len) /* all the case statements fall through */
765 case 11: c+=((hashval_t)k[10]<<24);
766 case 10: c+=((hashval_t)k[9]<<16);
767 case 9 : c+=((hashval_t)k[8]<<8);
768 /* the first byte of c is reserved for the length */
769 case 8 : b+=((hashval_t)k[7]<<24);
770 case 7 : b+=((hashval_t)k[6]<<16);
771 case 6 : b+=((hashval_t)k[5]<<8);
772 case 5 : b+=k[4];
773 case 4 : a+=((hashval_t)k[3]<<24);
774 case 3 : a+=((hashval_t)k[2]<<16);
775 case 2 : a+=((hashval_t)k[1]<<8);
776 case 1 : a+=k[0];
777 /* case 0: nothing left to add */
779 mix(a,b,c);
780 /*-------------------------------------------- report the result */
781 return c;
784 hashval_t
785 e_hash (const void *a)
787 const struct entry *e = a;
788 hashval_t ret = 0;
789 int i;
791 if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
792 abort ();
793 for (i = 0; i <= e[0].len; ++i)
795 int attriblen;
796 ret = iterative_hash (&e[i], offsetof (struct entry, attrib), ret);
797 attriblen = e[i].attrib ? strlen (e[i].attrib) : -1;
798 ret = iterative_hash (&attriblen, sizeof (int), ret);
799 if (e[i].attrib)
800 ret = iterative_hash (e[i].attrib, attriblen, ret);
802 return ret;
806 e_eq (const void *a, const void *b)
808 const struct entry *ea = a, *eb = b;
809 int i;
810 if (ea[0].etype != ETYPE_STRUCT && ea[0].etype != ETYPE_UNION)
811 abort ();
812 if (ea[0].len != eb[0].len)
813 return 0;
814 for (i = 0; i <= ea[0].len; ++i)
816 if (ea[i].etype != eb[i].etype
817 || ea[i].len != eb[i].len
818 || ea[i].arr_len != eb[i].arr_len
819 || ea[i].type != eb[i].type)
820 return 0;
821 if ((ea[i].attrib == NULL) ^ (eb[i].attrib == NULL))
822 return 0;
823 if (ea[i].attrib && strcmp (ea[i].attrib, eb[i].attrib) != 0)
824 return 0;
826 return 1;
829 static int
830 e_exists (const struct entry *e)
832 struct entry *h;
833 hashval_t hval;
835 hval = e_hash (e);
836 for (h = hash_table[hval % HASH_SIZE]; h; h = h->next)
837 if (e_eq (e, h))
838 return 1;
839 return 0;
842 static void
843 e_insert (struct entry *e)
845 hashval_t hval;
847 hval = e_hash (e);
848 e->next = hash_table[hval % HASH_SIZE];
849 hash_table[hval % HASH_SIZE] = e;
852 void
853 output (struct entry *e)
855 int i;
856 char c;
857 struct entry *n;
858 const char *skip_cint = "";
860 if (e[0].etype != ETYPE_STRUCT && e[0].etype != ETYPE_UNION)
861 abort ();
863 if (e_exists (e))
864 return;
866 n = (struct entry *) malloc ((e[0].len + 1) * sizeof (struct entry));
867 memcpy (n, e, (e[0].len + 1) * sizeof (struct entry));
868 e_insert (n);
870 if (idx == limidx)
871 switchfiles (e[0].len);
873 for (i = 1; i <= e[0].len; ++i)
874 if ((e[i].etype == ETYPE_TYPE || e[i].etype == ETYPE_ARRAY)
875 && (e[i].type->type == TYPE_CINT || e[i].type->type == TYPE_CUINT))
876 break;
877 if (i <= e[0].len)
878 skip_cint = "CI";
879 if (e[0].attrib)
880 fprintf (outfile, (generate_random () & 1)
881 ? "TX%s(%d,%s %s,," : "TX%s(%d,%s,%s,", skip_cint,
882 idx, e[0].etype == ETYPE_STRUCT ? "struct" : "union",
883 e[0].attrib);
884 else if (e[0].etype == ETYPE_STRUCT)
885 fprintf (outfile, "T%s(%d,", skip_cint, idx);
886 else
887 fprintf (outfile, "U%s(%d,", skip_cint, idx);
888 c = 'a';
889 for (i = 1; i <= e[0].len; )
890 i += subfield (e + i, &c);
891 fputs (",", outfile);
892 c = 'a';
893 for (i = 1; i <= e[0].len; )
895 i += subvalues (e + i, namebuf, &c);
896 if (e[0].etype == ETYPE_UNION)
897 break;
899 fputs (")\n", outfile);
900 if (output_one && idx == limidx)
901 exit (0);
902 ++idx;
905 enum FEATURE
907 FEATURE_VECTOR = 1,
908 FEATURE_COMPLEX = 2,
909 FEATURE_ZEROARRAY = 8,
910 FEATURE_ZEROBITFLD = 16,
911 ALL_FEATURES = FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY
912 | FEATURE_ZEROBITFLD
915 void
916 singles (enum FEATURE features)
918 struct entry e[2];
919 int i;
920 memset (e, 0, sizeof (e));
921 e[0].etype = ETYPE_STRUCT;
922 output (e);
923 e[0].etype = ETYPE_UNION;
924 output (e);
925 e[0].len = 1;
926 e[0].attrib = NULL;
927 for (i = 0; i < NTYPES2; ++i)
929 e[0].etype = ETYPE_STRUCT;
930 e[1].etype = ETYPE_TYPE;
931 e[1].type = &base_types[i];
932 output (e);
933 e[0].etype = ETYPE_UNION;
934 output (e);
936 if (features & FEATURE_COMPLEX)
937 for (i = 0; i < NCTYPES2; ++i)
939 e[0].etype = ETYPE_STRUCT;
940 e[1].etype = ETYPE_TYPE;
941 e[1].type = &complex_types[i];
942 output (e);
943 e[0].etype = ETYPE_UNION;
944 output (e);
946 if (features & FEATURE_VECTOR)
947 for (i = 0; i < NVTYPES2; ++i)
949 e[0].etype = ETYPE_STRUCT;
950 e[1].etype = ETYPE_TYPE;
951 e[1].type = &vector_types[i];
952 output (e);
953 e[0].etype = ETYPE_UNION;
954 output (e);
958 void
959 choose_type (enum FEATURE features, struct entry *e, int r, int in_array)
961 int i;
963 i = NTYPES2 - NTYPES1;
964 if (features & FEATURE_COMPLEX)
965 i += NCTYPES2;
966 if (features & FEATURE_VECTOR)
967 i += NVTYPES2;
968 r >>= 2;
969 r %= i;
970 if (r < NTYPES2 - NTYPES1)
971 e->type = &base_types[r + NTYPES1];
972 r -= NTYPES2 - NTYPES1;
973 if (e->type == NULL && (features & FEATURE_COMPLEX))
975 if (r < NCTYPES2)
976 e->type = &complex_types[r];
977 r -= NCTYPES2;
979 if (e->type == NULL && (features & FEATURE_VECTOR))
981 if (r < NVTYPES2)
982 e->type = &vector_types[r];
983 r -= NVTYPES2;
985 if (e->type == NULL)
986 abort ();
989 /* This is from gcc.c-torture/execute/builtin-bitops-1.c. */
990 static int
991 my_ffsll (unsigned long long x)
993 int i;
994 if (x == 0)
995 return 0;
996 /* We've tested LLONG_MAX for 64 bits so this should be safe. */
997 for (i = 0; i < 64; i++)
998 if (x & (1ULL << i))
999 break;
1000 return i + 1;
1003 void
1004 generate_fields (enum FEATURE features, struct entry *e, struct entry *parent,
1005 int len)
1007 int r, i, j, ret = 1, n, incr, sametype;
1009 for (n = 0; n < len; n += incr)
1011 r = generate_random ();
1012 /* 50% ETYPE_TYPE base_types NTYPES1
1013 12.5% ETYPE_TYPE other
1014 12.5% ETYPE_ARRAY
1015 12.5% ETYPE_BITFLD
1016 12.5% ETYPE_STRUCT|ETYPE_UNION|ETYPE_STRUCT_ARRAY|ETYPE_UNION_ARRAY */
1017 i = (r & 7);
1018 r >>= 3;
1019 incr = 1;
1020 switch (i)
1022 case 6: /* BITfields disabled for now as _Bool bitfields are broken. */
1023 case 0:
1024 case 1:
1025 case 2:
1026 case 3:
1027 e[n].etype = ETYPE_TYPE;
1028 e[n].type = &base_types[r % NTYPES1];
1029 break;
1030 case 4:
1031 e[n].etype = ETYPE_TYPE;
1032 choose_type (features, &e[n], r, 0);
1033 break;
1034 case 5:
1035 e[n].etype = ETYPE_ARRAY;
1036 i = r & 1;
1037 r >>= 1;
1038 if (i)
1039 e[n].type = &base_types[r % NTYPES1];
1040 else
1041 choose_type (features, &e[n], r, 1);
1042 r = generate_random ();
1043 if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1045 e[n].arr_len = 0;
1046 if (n == len - 1 && (r & 4)
1047 && (parent->etype == ETYPE_STRUCT
1048 || parent->etype == ETYPE_STRUCT_ARRAY))
1050 int k;
1051 for (k = 0; k < n; ++k)
1052 if (e[k].etype != ETYPE_BITFLD || e[k].len)
1054 e[n].arr_len = 255;
1055 break;
1059 else if ((r & 3) != 3)
1060 e[n].arr_len = (r >> 2) & 7;
1061 else
1062 e[n].arr_len = (r >> 2) & 31;
1063 break;
1064 #if 0
1065 case 6:
1066 sametype = 1;
1067 switch (r & 7)
1069 case 0:
1070 case 1:
1071 case 2:
1072 break;
1073 case 3:
1074 case 4:
1075 case 5:
1076 incr = 1 + (r >> 3) % (len - n);
1077 break;
1078 case 6:
1079 case 7:
1080 sametype = 0;
1081 incr = 1 + (r >> 3) % (len - n);
1082 break;
1084 for (j = n; j < n + incr; ++j)
1086 int mi, ma;
1088 e[j].etype = ETYPE_BITFLD;
1089 if (j == n || !sametype)
1091 r = generate_random ();
1092 r >>= 2;
1093 e[j].type
1094 = &bitfld_types[r % n_bitfld_types];
1096 else
1097 e[j].type = e[n].type;
1098 r = generate_random ();
1099 mi = 0;
1100 ma = 0;
1101 switch (e[j].type->bitfld)
1103 case 'C': ma = 8; break;
1104 case 'S': ma = 16; break;
1105 case 'I': ma = 32; break;
1106 case 'L':
1107 case 'Q': ma = 64; break;
1108 case 'B': ma = 1; break;
1109 case ' ':
1110 if (e[j].type->type == TYPE_UENUM)
1111 mi = my_ffsll (e[j].type->maxval + 1) - 1;
1112 else if (e[j].type->type == TYPE_SENUM)
1113 mi = my_ffsll (e[j].type->maxval + 1);
1114 else
1115 abort ();
1116 if (!mi)
1117 mi = 1;
1118 if (mi <= 32)
1119 ma = 32;
1120 else
1121 ma = 64;
1122 break;
1123 default:
1124 abort ();
1126 e[j].len = ma + 1;
1127 if (sametype && (r & 3) == 0 && ma > 1)
1129 int sum = 0, k;
1130 for (k = n; k < j; ++k)
1131 sum += e[k].len;
1132 sum %= ma;
1133 e[j].len = sum ? ma - sum : ma;
1135 r >>= 2;
1136 if (! (features & FEATURE_ZEROBITFLD) && mi == 0)
1137 mi = 1;
1138 if (e[j].len < mi || e[j].len > ma)
1139 e[j].len = mi + (r % (ma + 1 - mi));
1140 r >>= 6;
1141 if ((features & FEATURE_ZEROBITFLD) && (r & 3) == 0
1142 && mi == 0)
1143 e[j].len = 0;
1145 break;
1146 #endif
1147 case 7:
1148 switch (r & 7)
1150 case 0:
1151 case 1:
1152 case 2:
1153 e[n].etype = ETYPE_STRUCT;
1154 break;
1155 case 3:
1156 case 4:
1157 e[n].etype = ETYPE_UNION;
1158 break;
1159 case 5:
1160 case 6:
1161 e[n].etype = ETYPE_STRUCT_ARRAY;
1162 break;
1163 case 7:
1164 e[n].etype = ETYPE_UNION_ARRAY;
1165 break;
1167 r >>= 3;
1168 e[n].len = r % (len - n);
1169 incr = 1 + e[n].len;
1170 generate_fields (features, &e[n + 1], &e[n], e[n].len);
1171 if (e[n].etype == ETYPE_STRUCT_ARRAY
1172 || e[n].etype == ETYPE_UNION_ARRAY)
1174 r = generate_random ();
1175 if ((features & FEATURE_ZEROARRAY) && (r & 3) == 0)
1177 e[n].arr_len = 0;
1178 if (n + incr == len && (r & 4)
1179 && (parent->etype == ETYPE_STRUCT
1180 || parent->etype == ETYPE_STRUCT_ARRAY))
1182 int k;
1183 for (k = 0; k < n; ++k)
1184 if (e[k].etype != ETYPE_BITFLD || e[k].len)
1186 e[n].arr_len = 255;
1187 break;
1191 else if ((r & 3) != 3)
1192 e[n].arr_len = (r >> 2) & 7;
1193 else
1194 e[n].arr_len = (r >> 2) & 31;
1196 break;
1201 void
1202 generate_random_tests (enum FEATURE features, int len)
1204 struct entry e[len + 1];
1205 int i, r;
1206 if (len > 'z' - 'a' + 1)
1207 abort ();
1208 memset (e, 0, sizeof (e));
1209 r = generate_random ();
1210 if ((r & 7) == 0)
1211 e[0].etype = ETYPE_UNION;
1212 else
1213 e[0].etype = ETYPE_STRUCT;
1214 r >>= 3;
1215 e[0].len = len;
1216 generate_fields (features, &e[1], &e[0], len);
1217 output (e);
1220 struct { const char *name; enum FEATURE f; }
1221 features[] = {
1222 { "normal", 0 },
1223 { "complex", FEATURE_COMPLEX },
1224 { "vector", FEATURE_VECTOR },
1225 { "[0] :0", FEATURE_ZEROARRAY | FEATURE_ZEROBITFLD },
1226 { "complex vector [0]",
1227 FEATURE_COMPLEX | FEATURE_VECTOR | FEATURE_ZEROARRAY }
1231 main (int argc, char **argv)
1233 int i, j, count, c, n = 3000;
1234 char *optarg;
1236 if (sizeof (int) != 4 || sizeof (long long) != 8)
1237 return 1;
1239 i = 1;
1240 while (i < argc)
1242 c = '\0';
1243 if (argv[i][0] == '-' && argv[i][2] == '\0')
1244 c = argv[i][1];
1245 optarg = argv[i + 1];
1246 if (!optarg)
1247 goto usage;
1248 switch (c)
1250 case 'n':
1251 n = atoi (optarg);
1252 break;
1253 case 'd':
1254 destdir = optarg;
1255 break;
1256 case 's':
1257 srcdir = optarg;
1258 break;
1259 case 'i':
1260 output_one = 1;
1261 limidx = atoi (optarg);
1262 break;
1263 default:
1264 fprintf (stderr, "unrecognized option %s\n", argv[i]);
1265 goto usage;
1267 i += 2;
1270 if (output_one)
1272 outfile = fopen ("/dev/null", "w");
1273 if (outfile == NULL)
1275 fputs ("could not open /dev/null", stderr);
1276 return 1;
1278 n = limidx + 1;
1281 if (destdir == NULL && !output_one)
1283 usage:
1284 fprintf (stderr, "Usage:\n\
1285 %s [-s srcdir -d destdir] [-n count] [-i idx]\n\
1286 Either -s srcdir -d destdir or -i idx must be used\n", argv[0]);
1287 return 1;
1290 if (srcdir == NULL && !output_one)
1291 goto usage;
1293 for (i = 0; i < NTYPES2; ++i)
1294 if (base_types[i].bitfld)
1295 bitfld_types[n_bitfld_types++] = base_types[i];
1296 for (i = 0; i < sizeof (features) / sizeof (features[0]); ++i)
1298 int startidx = idx;
1299 if (! output_one)
1300 limidx = idx;
1301 if (!i)
1302 count = 200;
1303 else
1304 count = 20;
1305 for (j = 1; j <= 9; ++j)
1306 while (idx < startidx + j * count)
1307 generate_random_tests (features[i].f, j);
1308 while (idx < startidx + count * 10)
1309 generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1311 for (i = 0; n > 3000 && i < sizeof (features) / sizeof (features[0]); ++i)
1313 int startidx;
1314 startidx = idx;
1315 if (! output_one)
1316 limidx = idx;
1317 singles (features[i].f);
1318 if (!i)
1320 count = 1000;
1321 while (idx < startidx + 1000)
1322 generate_random_tests (features[i].f, 1);
1324 else
1326 startidx = idx;
1327 count = 100;
1328 while (idx < startidx + 100)
1329 generate_random_tests (features[i].f, 1);
1331 startidx = idx;
1332 for (j = 2; j <= 9; ++j)
1333 while (idx < startidx + (j - 1) * count)
1334 generate_random_tests (features[i].f, j);
1335 while (idx < startidx + count * 9)
1336 generate_random_tests (features[i].f, 10 + (generate_random () % 16));
1338 if (! output_one)
1339 limidx = idx;
1340 while (idx < n)
1341 generate_random_tests (ALL_FEATURES, 1 + (generate_random () % 25));
1342 fclose (outfile);
1343 return 0;