]> www.ginac.de Git - cln.git/blob - autoconf/intparam.c
Make it work with gcc-4.3 snapshots.
[cln.git] / autoconf / intparam.c
1 #line 1 "intparam.d"
2 /* Bestimmung einiger Maschinen-Parameter und -Abh�gigkeiten */
3 /* und Ausgabe in ein Include-File */
4 /* Bruno Haible 10.9.1991, 12.10.1992, 6.12.1992, 24.10.1993 */
5
6 /* Auf einigen Systemen werden in <sys/types.h> die Typen uchar, ushort, uint, */
7 /* ulong definiert. Normalerweise reicht _POSIX_SOURCE aus, dies zu verhindern, */
8 /* bei AIX 3.2.5 (rs6000-ibm-aix3.2.5) jedoch nicht. Wir mssen Gewalt anwenden. */
9 #define _POSIX_SOURCE
10 #define uchar  os_uchar
11 #define ushort os_ushort
12 #define uint   os_uint
13 #define ulong  os_ulong
14 #include <stdio.h>
15 #undef ulong
16 #undef uint
17 #undef ushort
18 #undef uchar
19
20 #if !(defined(__STDC__) || defined(__cplusplus))
21 /* Only for use in function parameter lists and as function return type. */
22 #define void
23 #endif
24
25 #define loop  while(1)
26
27 typedef int boolean;
28 #define TRUE  1
29 #define FALSE 0
30
31 #ifdef __CHAR_UNSIGNED__
32 typedef signed char  schar;
33 #else
34 typedef char  schar;
35 #endif
36 typedef unsigned char  uchar;
37 typedef unsigned short  ushort;
38 typedef unsigned /* int */  uint;
39 typedef unsigned long  ulong;
40 #ifdef HAVE_LONGLONG
41 typedef long long  longlong;
42 typedef unsigned long long  ulonglong;
43 #endif
44 typedef int (function)();
45
46 static int random_table[256] = /* 2048 zuf�lige Bits, hier von pi */
47   { 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,0xC4,0xC6,0x62,0x8B,
48     0x80,0xDC,0x1C,0xD1,0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
49     0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,0x51,0x4A,0x08,0x79,
50     0x8E,0x34,0x04,0xDD,0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
51     0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,0x4F,0xE1,0x35,0x6D,
52     0x6D,0x51,0xC2,0x45,0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
53     0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,0x0B,0xFF,0x5C,0xB6,
54     0xF4,0x06,0xB7,0xED,0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
55     0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,0x49,0x28,0x66,0x51,
56     0xEC,0xE4,0x5B,0x3D,0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
57     0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,0x69,0x16,0x3F,0xA8,
58     0xFD,0x24,0xCF,0x5F,0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
59     0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,0x9E,0xD5,0x29,0x07,
60     0x70,0x96,0x96,0x6D,0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
61     0xF1,0x74,0x6C,0x08,0xCA,0x18,0x21,0x7C,0x32,0x90,0x5E,0x46,
62     0x2E,0x36,0xCE,0x3B,0xE3,0x9E,0x77,0x2C,0x18,0x0E,0x86,0x03,
63     0x9B,0x27,0x83,0xA2,0xEC,0x07,0xA2,0x8F,0xB5,0xC5,0x5D,0xF0,
64     0x6F,0x4C,0x52,0xC9,0xDE,0x2B,0xCB,0xF6,0x95,0x58,0x17,0x18,
65     0x39,0x95,0x49,0x7C,0xEA,0x95,0x6A,0xE5,0x15,0xD2,0x26,0x18,
66     0x98,0xFA,0x05,0x10,0x15,0x72,0x8E,0x5A,0x8A,0xAA,0xC4,0x2D,
67     0xAD,0x33,0x17,0x0D,0x04,0x50,0x7A,0x33,0xA8,0x55,0x21,0xAB,
68     0xDF,0x1C,0xBA,0x65,
69   };
70 #define random_table_length  (8*256)
71 static int random_position = -1;
72 int next_random_bit(void)
73   { random_position++;
74     if (random_position==random_table_length) random_position = 0;
75     return (random_table[random_position/8] >> (random_position % 8)) & 1;
76   }
77
78 #if defined(__STDC__) || defined(__cplusplus)
79 void printf_underscored (const char* string)
80 #else
81 void printf_underscored(string)
82   char* string;
83 #endif
84   { char c;
85     while (!((c = *string++) == '\0')) { printf("%c",(c==' ' ? '_' : c)); }
86   }
87
88 /* string_length(string) is the same as strlen(string). */
89 /* Better avoid to depend on <string.h>. */
90 #if defined(__STDC__) || defined(__cplusplus)
91 int string_length (char* string)
92 #else
93 int string_length(string)
94   char* string;
95 #endif
96   { int count = 0;
97     while (!(*string++ == '\0')) { count++; }
98     return count;
99   }
100
101 static int char_bitsize, short_bitsize, int_bitsize, long_bitsize;
102 static int uchar_bitsize, ushort_bitsize, uint_bitsize, ulong_bitsize;
103 static boolean char_uchar_same, short_ushort_same, int_uint_same, long_ulong_same;
104 static int pointer_bitsize;
105 #ifdef HAVE_LONGLONG
106 static int longlong_bitsize, ulonglong_bitsize;
107 static boolean longlong_ulonglong_same;
108 #endif
109
110 void main1(void) {
111 #define get_unsigned_integer_bitsize(type,where)  \
112   { type x = 1;                               \
113     int bits = 0;                             \
114     loop {                                    \
115       if (x==0) break;                        \
116       x = x+x;                                \
117       bits++;                                 \
118       if (bits==1000) { bits = -1; break; }   \
119     }                                         \
120     where = bits;                             \
121   }
122 #define get_signed_integer_bitsize(type,unsigned_type,where)  \
123   { /* Signed integer overflow is "undefined behaviour" in C99, and gcc-4.3    \
124        (without -fwrapv option) actually does weird things when signed integer \
125        overflow occurs. Therefore perform the addition on the unsigned type.   \
126        Drawback: This will not detect cases where the signed type has more bits\
127        than the unsigned type but the same size according to sizeof. Blech. */ \
128     type x = 1;                               \
129     int bits = 0;                             \
130     loop {                                    \
131       if (x==0) break;                        \
132       x = (unsigned_type)x + (unsigned_type)x;\
133       bits++;                                 \
134       if (bits==1000) { bits = -1; break; }   \
135     }                                         \
136     where = bits;                             \
137   }
138 #define print_integer_bitsize(type,typestr,where)  \
139   { if (where >= 0)                                                  \
140       { printf("/* Integers of t%spe %s have %ld bits. */\n","y",typestr,(long)where); \
141         if (!(typestr[0] == 'u'))                                    \
142           { printf("#define "); printf_underscored(typestr); printf("_bitsize %ld\n",(long)where); } \
143         printf("\n");                                                \
144       }                                                              \
145       else                                                           \
146       { printf("#error \"Integers of t%spe %s have no binary representation!!\"\n","y",typestr); } \
147     if (!(where == char_bitsize * sizeof(type)))                     \
148       { printf("#error \"Formula BITSIZE(T) = SIZEOF(T) * BITSPERBYTE does not hold for t%spe %s!!\"\n","y",typestr); } \
149   }
150   get_signed_integer_bitsize(schar,uchar,char_bitsize);
151   get_signed_integer_bitsize(short,ushort,short_bitsize);
152   get_signed_integer_bitsize(int,uint,int_bitsize);
153   get_signed_integer_bitsize(long,ulong,long_bitsize);
154   print_integer_bitsize(schar,"char",char_bitsize);
155   print_integer_bitsize(short,"short",short_bitsize);
156   print_integer_bitsize(int,"int",int_bitsize);
157   print_integer_bitsize(long,"long",long_bitsize);
158 #ifdef HAVE_LONGLONG
159   get_signed_integer_bitsize(longlong,ulonglong,longlong_bitsize);
160   print_integer_bitsize(longlong,"long long",longlong_bitsize);
161 #endif
162   get_unsigned_integer_bitsize(uchar,uchar_bitsize);
163   get_unsigned_integer_bitsize(ushort,ushort_bitsize);
164   get_unsigned_integer_bitsize(uint,uint_bitsize);
165   get_unsigned_integer_bitsize(ulong,ulong_bitsize);
166   print_integer_bitsize(uchar,"unsigned char",uchar_bitsize);
167   print_integer_bitsize(ushort,"unsigned short",ushort_bitsize);
168   print_integer_bitsize(uint,"unsigned int",uint_bitsize);
169   print_integer_bitsize(ulong,"unsigned long",ulong_bitsize);
170 #ifdef HAVE_LONGLONG
171   get_unsigned_integer_bitsize(ulonglong,ulonglong_bitsize);
172   print_integer_bitsize(ulonglong,"unsigned long long",ulonglong_bitsize);
173 #endif
174 }
175
176 void main2(void) {
177 #define compare_integer_bitsizes(typestr1,typestr2,type1_bitsize,type2_bitsize)  \
178   { if (!(type1_bitsize==type2_bitsize))                                                       \
179       printf("#error \"Integer types %s and %s have different sizes!!\"\n",typestr1,typestr2); \
180   }
181   compare_integer_bitsizes("char","unsigned char",char_bitsize,uchar_bitsize);
182   compare_integer_bitsizes("short","unsigned short",short_bitsize,ushort_bitsize);
183   compare_integer_bitsizes("int","unsigned int",int_bitsize,uint_bitsize);
184   compare_integer_bitsizes("long","unsigned long",long_bitsize,ulong_bitsize);
185 #ifdef HAVE_LONGLONG
186   compare_integer_bitsizes("long long","unsigned long long",longlong_bitsize,ulonglong_bitsize);
187 #endif
188 }
189
190 #define get_a_random(type,bitsize,where)  \
191   { type x = 0;                                          \
192     int i = bitsize;                                     \
193     while (i>0) { x = (x<<1) + next_random_bit(); i--; } \
194     where = x;                                           \
195   }
196 #define get_a_random_twice(type1,type2,bitsize,where1,where2)  \
197   { type1 x1 = 0; type2 x2 = 0;                 \
198     int i = bitsize;                            \
199     while (i>0)                                 \
200       { type1 b = next_random_bit();            \
201         x1 = ((x1<<1) + b); x2 = ((x2<<1) + b); \
202         i--;                                    \
203       }                                         \
204     where1 = x1; where2 = x2;                   \
205   }
206
207 void main3(void) {
208 #define compare_integer_representation(type1,type2,typestr1,typestr2,type1_bitsize,type2_bitsize,where)  \
209   { if ((type1_bitsize>=0) && (type2_bitsize>=0) && (type1_bitsize==type2_bitsize))           \
210       { int i,j;                                                                              \
211         type1 sample1; type2 sample2;                                                         \
212         where = TRUE;                                                                         \
213         for (i = 0; i<100; i++)                                                               \
214           { get_a_random_twice(type1,type2,type1_bitsize,sample1,sample2);                    \
215             if (!(sample1 == (type1)(sample2))) { where = FALSE; }                            \
216             if (!(sample2 == (type2)(sample1))) { where = FALSE; }                            \
217           }                                                                                   \
218         for (i = 0; i<100; i++)                                                               \
219           { get_a_random(type1,type1_bitsize,sample1);                                        \
220             sample2 = (type2)(sample1);                                                       \
221             for (j = 0; j < type1_bitsize; j++)                                               \
222               if (!( ((sample1 & ((type1)1<<j)) == 0)                                         \
223                      == ((sample2 & ((type2)1<<j)) == 0)                                      \
224                  ) )                                                                          \
225                 { where = FALSE; }                                                            \
226           }                                                                                   \
227         if (where)                                                                            \
228           { printf("/* Integer types %s and %s have the same binary representation. */\n",typestr1,typestr2); } \
229           else                                                                                \
230           { printf("#error \"Integer types %s and %s have different binary representations!!\"\n",typestr1,typestr2); } \
231       }                                                                                       \
232       else                                                                                    \
233       { where = FALSE; }                                                                      \
234   }
235   compare_integer_representation(schar,uchar,"char","unsigned char",char_bitsize,uchar_bitsize,char_uchar_same);
236   compare_integer_representation(short,ushort,"short","unsigned short",short_bitsize,ushort_bitsize,short_ushort_same);
237   compare_integer_representation(int,uint,"int","unsigned int",int_bitsize,uint_bitsize,int_uint_same);
238   compare_integer_representation(long,ulong,"long","unsigned long",long_bitsize,ulong_bitsize,long_ulong_same);
239 #ifdef HAVE_LONGLONG
240   compare_integer_representation(longlong,ulonglong,"long long","unsigned long long",longlong_bitsize,ulonglong_bitsize,longlong_ulonglong_same);
241 #endif
242   printf("\n");
243 }
244
245 void main4(void) {
246 #define test_integer_ushift(type,typestr,type_bitsize)  \
247   if (type_bitsize >= 0)                                                                        \
248     { int i,j,shc;                                                                              \
249       type sample1,sample2;                                                                     \
250       boolean left_works = TRUE, right_works = TRUE;                                            \
251       for (i = 0; i<100; i++)                                                                   \
252         { get_a_random(type,type_bitsize,sample1);                                              \
253           for (shc = 0; shc < type_bitsize; shc++)                                              \
254             { sample2 = sample1 << shc;                                                         \
255               for (j=0; j < type_bitsize; j++)                                                  \
256                 { if (!( ((sample2 & ((type)1<<j)) == 0)                                        \
257                          ==                                                                     \
258                          (j < shc ? TRUE : ((sample1 & ((type)1<<(j-shc))) == 0))               \
259                      ) )                                                                        \
260                     { left_works = FALSE; }                                                     \
261         }   }   }                                                                               \
262       for (i = 0; i<100; i++)                                                                   \
263         { get_a_random(type,type_bitsize,sample1);                                              \
264           for (shc = 0; shc < type_bitsize; shc++)                                              \
265             { sample2 = sample1 >> shc;                                                         \
266               for (j=0; j < type_bitsize; j++)                                                  \
267                 { if (!( ((sample2 & ((type)1<<j)) == 0)                                        \
268                          ==                                                                     \
269                          (j >= type_bitsize-shc ? TRUE : ((sample1 & ((type)1<<(j+shc))) == 0)) \
270                      ) )                                                                        \
271                     { right_works = FALSE; }                                                    \
272         }   }   }                                                                               \
273       if (!left_works)                                                                          \
274         { printf("#error \"Left shift of integers of t%spe %s does not work!!\"\n","y",typestr); } \
275       if (!right_works)                                                                         \
276         { printf("#error \"Right shift of integers of t%spe %s does not work!!\"\n","y",typestr); } \
277     }
278 #define test_integer_sshift(type,typestr,type_bitsize)  \
279   if (type_bitsize >= 0)                                                                       \
280     { int i,j,shc;                                                                             \
281       type sample1,sample2;                                                                    \
282       boolean left_works = TRUE, right_works = TRUE;                                           \
283       for (i = 0; i<100; i++)                                                                  \
284         { get_a_random(type,type_bitsize,sample1);                                             \
285           for (shc = 0; shc < type_bitsize; shc++)                                             \
286             { sample2 = sample1 << shc;                                                        \
287               for (j=0; j < type_bitsize; j++)                                                 \
288                 { if (!( ((sample2 & ((type)1<<j)) == 0)                                       \
289                          ==                                                                    \
290                          (j < shc ? TRUE : ((sample1 & ((type)1<<(j-shc))) == 0))              \
291                      ) )                                                                       \
292                     { left_works = FALSE; }                                                    \
293         }   }   }                                                                              \
294       for (i = 0; i<100; i++)                                                                  \
295         { get_a_random(type,type_bitsize,sample1);                                             \
296           for (shc = 0; shc < type_bitsize; shc++)                                             \
297             { sample2 = sample1 >> shc;                                                        \
298               for (j=0; j < type_bitsize; j++)                                                 \
299                 { if (!( ((sample2 & ((type)1<<j)) == 0)                                       \
300                          ==                                                                    \
301                          ((sample1 & ((type)1<< (j+shc>=type_bitsize ? type_bitsize-1 : j+shc))) == 0) \
302                      ) )                                                                       \
303                     { right_works = FALSE; }                                                   \
304         }   }   }                                                                              \
305       if (!left_works)                                                                         \
306         { printf("#error \"Left shift of integers of t%spe %s does not work!!\"\n","y",typestr); } \
307       if (!right_works)                                                                        \
308         { printf("#error \"Right shift of integers of t%spe %s does not work!!\"\n","y",typestr); } \
309     }
310   test_integer_ushift(uchar,"unsigned char",uchar_bitsize);
311   test_integer_ushift(ushort,"unsigned short",ushort_bitsize);
312   test_integer_ushift(uint,"unsigned int",uint_bitsize);
313   test_integer_ushift(ulong,"unsigned long",ulong_bitsize);
314 #ifdef HAVE_LONGLONG
315   test_integer_ushift(ulonglong,"unsigned long long",ulonglong_bitsize);
316 #endif
317   test_integer_sshift(schar,"char",char_bitsize);
318   test_integer_sshift(short,"short",short_bitsize);
319   test_integer_sshift(int,"int",int_bitsize);
320   test_integer_sshift(long,"long",long_bitsize);
321 #ifdef HAVE_LONGLONG
322   test_integer_sshift(longlong,"long long",longlong_bitsize);
323 #endif
324 }
325
326 void main5(void) {
327 #define test_integer_casts(type1,type2,typestr1,typestr2,type1_bitsize,type2_bitsize,want)  \
328   if (type1_bitsize <= type2_bitsize)                                                                      \
329     { int i,j;                                                                                             \
330       boolean modifies = FALSE;                                                                            \
331       boolean zero_extends = TRUE;                                                                         \
332       boolean sign_extends = TRUE;                                                                         \
333       for (i = 0; i<100; i++)                                                                              \
334         { type1 sample1;                                                                                   \
335           type2 sample2;                                                                                   \
336           get_a_random(type1,type1_bitsize,sample1);                                                       \
337           sample2 = (type2)sample1;                                                                        \
338           if (!(sample1 == (type1)sample2)) { modifies = TRUE; }                                           \
339           for (j = 0; j<type1_bitsize; j++)                                                                \
340             if (!( ((sample1 & ((type1)1<<j)) == 0) == ((sample2 & ((type2)1<<j)) == 0) ))                 \
341               { zero_extends = FALSE; sign_extends = FALSE; }                                              \
342           for (j = type1_bitsize; j<type2_bitsize; j++)                                                    \
343             if (!((sample2 & ((type2)1<<j)) == 0))                                                         \
344               { zero_extends = FALSE; }                                                                    \
345           for (j = type1_bitsize; j<type2_bitsize; j++)                                                    \
346             if (!( ((sample1 & ((type1)1<<(type1_bitsize-1))) == 0) == ((sample2 & ((type2)1<<j)) == 0) )) \
347               { sign_extends = FALSE; }                                                                    \
348         }                                                                                                  \
349       if (modifies)                                                                                        \
350         printf("#error \"Casts: (%s)(%s)(x) == x does not hold for every %s x !!\"\n",typestr1,typestr2,typestr1); \
351       if (zero_extends && sign_extends)                                                                    \
352         { if (!(type1_bitsize == type2_bitsize))                                                           \
353             printf("#error \"Casts from %s to %s works by identity!!\"\n",typestr1,typestr2);              \
354         }                                                                                                  \
355       if (zero_extends && !sign_extends)                                                                   \
356         { if ((type1_bitsize == type2_bitsize) || !(typestr1[0] == 'u') || !(want==1))                     \
357             printf("#error \"Casts from %s to %s works by zero-extend!!\"\n",typestr1,typestr2);           \
358         }                                                                                                  \
359       if (sign_extends && !zero_extends)                                                                   \
360         { if ((type1_bitsize == type2_bitsize) || (typestr1[0] == 'u') || !(want==2))                      \
361             printf("#error \"Casts from %s to %s works by sign-extend!!\"\n",typestr1,typestr2);           \
362         }                                                                                                  \
363       if (!sign_extends && !zero_extends)                                                                  \
364         printf("#error \"Casts from %s to %s works in an unknown manner!!\"\n",typestr1,typestr2);         \
365     }
366   /* erst Casts zwischen Integers vermutlich gleicher Gr�e: */
367   test_integer_casts(schar,uchar,"char","unsigned char",char_bitsize,uchar_bitsize,0);
368   test_integer_casts(short,ushort,"short","unsigned short",short_bitsize,ushort_bitsize,0);
369   test_integer_casts(int,uint,"int","unsigned int",int_bitsize,uint_bitsize,0);
370   test_integer_casts(long,ulong,"long","unsigned long",long_bitsize,ulong_bitsize,0);
371   test_integer_casts(uchar,schar,"unsigned char","char",uchar_bitsize,char_bitsize,0);
372   test_integer_casts(ushort,short,"unsigned short","short",ushort_bitsize,short_bitsize,0);
373   test_integer_casts(uint,int,"unsigned int","int",uint_bitsize,int_bitsize,0);
374   test_integer_casts(ulong,long,"unsigned long","long",ulong_bitsize,long_bitsize,0);
375 #ifdef HAVE_LONGLONG
376   test_integer_casts(longlong,ulonglong,"long long","unsigned long long",longlong_bitsize,ulonglong_bitsize,0);
377   test_integer_casts(ulonglong,longlong,"unsigned long long","long long",ulonglong_bitsize,longlong_bitsize,0);
378 #endif
379   /* dann Casts zwischen Integers unterschiedlicher Gr�e, aber gleichen Vorzeichens: */
380   test_integer_casts(uchar,ushort,"unsigned char","unsigned short",uchar_bitsize,ushort_bitsize,1);
381   test_integer_casts(uchar,uint,"unsigned char","unsigned int",uchar_bitsize,uint_bitsize,1);
382   test_integer_casts(uchar,ulong,"unsigned char","unsigned long",uchar_bitsize,ulong_bitsize,1);
383   test_integer_casts(ushort,uint,"unsigned short","unsigned int",ushort_bitsize,uint_bitsize,1);
384   test_integer_casts(ushort,ulong,"unsigned short","unsigned long",ushort_bitsize,ulong_bitsize,1);
385   test_integer_casts(uint,ulong,"unsigned int","unsigned long",uint_bitsize,ulong_bitsize,1);
386 #ifdef HAVE_LONGLONG
387   test_integer_casts(uchar,ulonglong,"unsigned char","unsigned long long",uchar_bitsize,ulonglong_bitsize,1);
388   test_integer_casts(ushort,ulonglong,"unsigned short","unsigned long long",ushort_bitsize,ulonglong_bitsize,1);
389   test_integer_casts(uint,ulonglong,"unsigned int","unsigned long long",uint_bitsize,ulonglong_bitsize,1);
390   test_integer_casts(ulong,ulonglong,"unsigned long","unsigned long long",ulong_bitsize,ulonglong_bitsize,1);
391 #endif
392   test_integer_casts(schar,short,"char","short",char_bitsize,short_bitsize,2);
393   test_integer_casts(schar,int,"char","int",char_bitsize,int_bitsize,2);
394   test_integer_casts(schar,long,"char","long",char_bitsize,long_bitsize,2);
395   test_integer_casts(short,int,"short","int",short_bitsize,int_bitsize,2);
396   test_integer_casts(short,long,"short","long",short_bitsize,long_bitsize,2);
397   test_integer_casts(int,long,"int","long",int_bitsize,long_bitsize,2);
398 #ifdef HAVE_LONGLONG
399   test_integer_casts(schar,longlong,"char","long long",char_bitsize,longlong_bitsize,2);
400   test_integer_casts(short,longlong,"short","long long",short_bitsize,longlong_bitsize,2);
401   test_integer_casts(int,longlong,"int","long long",int_bitsize,longlong_bitsize,2);
402   test_integer_casts(long,longlong,"long","long long",long_bitsize,longlong_bitsize,2);
403 #endif
404   /* dann Casts zwischen Integers unterschiedlicher Gr�e und unterschiedlichen Vorzeichens: */
405   test_integer_casts(uchar,short,"unsigned char","short",uchar_bitsize,short_bitsize,1);
406   test_integer_casts(uchar,int,"unsigned char","int",uchar_bitsize,int_bitsize,1);
407   test_integer_casts(uchar,long,"unsigned char","long",uchar_bitsize,long_bitsize,1);
408   test_integer_casts(ushort,int,"unsigned short","int",ushort_bitsize,int_bitsize,1);
409   test_integer_casts(ushort,long,"unsigned short","long",ushort_bitsize,long_bitsize,1);
410   test_integer_casts(uint,long,"unsigned int","long",uint_bitsize,long_bitsize,1);
411 #ifdef HAVE_LONGLONG
412   test_integer_casts(uchar,longlong,"unsigned char","long long",uchar_bitsize,longlong_bitsize,1);
413   test_integer_casts(ushort,longlong,"unsigned short","long long",ushort_bitsize,longlong_bitsize,1);
414   test_integer_casts(uint,longlong,"unsigned int","long long",uint_bitsize,longlong_bitsize,1);
415   test_integer_casts(ulong,longlong,"unsigned long","long long",ulong_bitsize,longlong_bitsize,1);
416 #endif
417   test_integer_casts(schar,ushort,"char","unsigned short",char_bitsize,ushort_bitsize,2);
418   test_integer_casts(schar,uint,"char","unsigned int",char_bitsize,uint_bitsize,2);
419   test_integer_casts(schar,ulong,"char","unsigned long",char_bitsize,ulong_bitsize,2);
420   test_integer_casts(short,uint,"short","unsigned int",short_bitsize,uint_bitsize,2);
421   test_integer_casts(short,ulong,"short","unsigned long",short_bitsize,ulong_bitsize,2);
422   test_integer_casts(int,ulong,"int","unsigned long",int_bitsize,ulong_bitsize,2);
423 #ifdef HAVE_LONGLONG
424   test_integer_casts(schar,ulonglong,"char","unsigned long long",char_bitsize,ulonglong_bitsize,2);
425   test_integer_casts(short,ulonglong,"short","unsigned long long",short_bitsize,ulonglong_bitsize,2);
426   test_integer_casts(int,ulonglong,"int","unsigned long long",int_bitsize,ulonglong_bitsize,2);
427   test_integer_casts(long,ulonglong,"long","unsigned long long",long_bitsize,ulonglong_bitsize,2);
428 #endif
429 }
430
431 void main6(void) {
432 #define check_sizeof_pointer(type,typestr)  \
433   { if (!(sizeof(type) <= sizeof(long)))                                 \
434       printf("#error \"Type %s does not fit into a long!!\"\n",typestr); \
435   }
436   check_sizeof_pointer(char*,"char *");
437   check_sizeof_pointer(long*,"long *");
438   check_sizeof_pointer(function*,"function *");
439   pointer_bitsize = char_bitsize * sizeof(char*);
440   printf("/* Pointers of t%spe %s have %ld bits. */\n","y","char *",(long)pointer_bitsize);
441   printf("#define pointer_bitsize %ld\n",(long)pointer_bitsize);
442   printf("\n");
443 }
444
445 void main7(void) {
446 #define test_pointer_casts(type1,type2,typestr1,typestr2)  \
447   if (!(sizeof(type1) == sizeof(type2)))                                                               \
448     { printf("#error \"Pointer types %s and %s have different sizes!!\"\n",typestr1,typestr2); }       \
449     else                                                                                               \
450     { int i;                                                                                           \
451       ulong differences1 = 0, differences2 = 0;                                                        \
452       for (i = 0; i<100; i++)                                                                          \
453         { ulong sample;                                                                                \
454           type1 sample1;                                                                               \
455           type2 sample2;                                                                               \
456           get_a_random(ulong,ulong_bitsize,sample);                                                    \
457           sample1 = (type1)sample;                                                                     \
458           sample2 = (type2)sample;                                                                     \
459           differences1 |= ((ulong)sample1 ^ (ulong)(type1)(sample2));                                  \
460           differences2 |= ((ulong)sample2 ^ (ulong)(type2)(sample1));                                  \
461         }                                                                                              \
462       if (differences1==0)                                                                             \
463         printf("/* Casts from %s to %s is OK (does nothing). */\n",typestr2,typestr1);                 \
464       else                                                                                             \
465       if (differences1 == ~(ulong)0)                                                                   \
466         printf("#error \"Casts from %s to %s work in an unknown way!!\"\n",typestr2,typestr1);         \
467       else                                                                                             \
468         printf("#error \"Casts from %s to %s modify part 0x%8lX of pointer!!\"\n",typestr2,typestr1,differences1); \
469       if (differences2==0)                                                                             \
470         printf("/* Casts from %s to %s is OK (does nothing). */\n",typestr1,typestr2);                 \
471       else                                                                                             \
472       if (differences2 == ~(ulong)0)                                                                   \
473         printf("#error \"Casts from %s to %s work in an unknown way!!\"\n",typestr1,typestr2);         \
474       else                                                                                             \
475         printf("#error \"Casts from %s to %s modify part 0x%8lX of pointer!!\"\n",typestr1,typestr2,differences2); \
476     }
477   test_pointer_casts(char*,long*,"char *","long *");
478   test_pointer_casts(char*,function*,"char *","function *");
479   printf("\n");
480 }
481
482 void main8(void) {
483 /* The following macro works only in C, not in C++, because C++ restricts the
484    use of NULL pointers and also because C++ forbids defining types within a
485    cast. */
486 #define alignmentof(type)  \
487   (int)(&((struct { char dummy1; type dummy2; } *)0)->dummy2)
488 #define get_alignment(type,typestr)  \
489   { struct { char dummy1; type dummy2; } dummy;                                                           \
490     long alignment = (char*)&dummy.dummy2 - (char*)&dummy;                                                \
491     printf("/* Type %s has sizeof = %ld and alignment = %ld. */\n",typestr,(long)sizeof(type),alignment); \
492     if (!(typestr[0] == 'u') && !(typestr[string_length(typestr)-1] == '*'))                              \
493       { printf("#define sizeof_"); printf_underscored(typestr); printf(" %ld\n",(long)sizeof(type));      \
494         printf("#define alignment_"); printf_underscored(typestr); printf(" %ld\n",alignment);            \
495       }                                                                                                   \
496     if (!((alignment & (alignment-1)) == 0))                                                              \
497       printf("#error \"The alignment %ld of t%spe %s is not a power of two!!\"\n",alignment,"y",typestr); \
498     printf("\n");                                                                                         \
499   }
500   get_alignment(char,"char"); get_alignment(uchar,"unsigned char");
501   get_alignment(short,"short"); get_alignment(ushort,"unsigned short");
502   get_alignment(int,"int"); get_alignment(uint,"unsigned int");
503   get_alignment(long,"long"); get_alignment(ulong,"unsigned long");
504 #ifdef HAVE_LONGLONG
505   get_alignment(longlong,"long long"); get_alignment(ulonglong,"unsigned long long");
506 #endif
507   get_alignment(float,"float");
508   get_alignment(double,"double");
509   get_alignment(char*,"char *");
510   get_alignment(long*,"long *");
511   get_alignment(function*,"function *");
512 }
513
514 void main9(void) {
515 #define get_endian(type,typestr,type_bitsize)  \
516   { if (type_bitsize == uchar_bitsize * sizeof(type))                                            \
517       { auto union { uchar einzeln[sizeof(type)]; type gesamt; } x;                              \
518         int i,j;                                                                                 \
519         boolean big_endian = TRUE;                                                               \
520         boolean little_endian = TRUE;                                                            \
521         for (i = 0; i<100; i++)                                                                  \
522           { type sample;                                                                         \
523             get_a_random(type,type_bitsize,sample);                                              \
524             x.gesamt = sample;                                                                   \
525             for (j = 0; j<sizeof(type); j++, sample >>= uchar_bitsize)                           \
526               { if (!( (sample & (((type)1<<uchar_bitsize)-1)) == x.einzeln[j] ))                \
527                   { little_endian = FALSE; }                                                     \
528                 if (!( (sample & (((type)1<<uchar_bitsize)-1)) == x.einzeln[sizeof(type)-1-j] )) \
529                   { big_endian = FALSE; }                                                        \
530           }   }                                                                                  \
531         if (big_endian && little_endian)                                                         \
532           { if (!(sizeof(type) == 1))                                                            \
533               printf("#error \"Endianness of t%spe %s in memory doesn't matter.\"\n","y",typestr); } \
534         if (big_endian && !little_endian)                                                        \
535           { printf("/* Type %s is stored BIG-ENDIAN in memory (i.e. like mc68000 or sparc). */\n",typestr); \
536             printf("#define "); printf_underscored(&typestr[9]); printf("_big_endian\n");        \
537           }                                                                                      \
538         if (little_endian && !big_endian)                                                        \
539           { printf("/* Type %s is stored LITTLE-ENDIAN in memory (i.e. like Z80 or VAX). */\n",typestr); \
540             printf("#define "); printf_underscored(&typestr[9]); printf("_little_endian\n");     \
541           }                                                                                      \
542         if (!big_endian && !little_endian)                                                       \
543           { printf("#error \"Type %s is stored in memory in an obscure manner!!\"\n",typestr); } \
544       }                                                                                          \
545       else                                                                                       \
546       { printf("#error \"Endianness makes no sense for t%spe %s !!\"\n","y",typestr); }          \
547   }
548   get_endian(uchar,"unsigned char",uchar_bitsize);
549   get_endian(ushort,"unsigned short",ushort_bitsize);
550   get_endian(uint,"unsigned int",uint_bitsize);
551   get_endian(ulong,"unsigned long",ulong_bitsize);
552 #ifdef HAVE_LONGLONG
553   get_endian(ulonglong,"unsigned long long",ulonglong_bitsize);
554 #endif
555   printf("\n");
556 }
557
558 long get_stack_direction(void)
559   { auto char dummy;
560     static char* dummyaddr = (char*)0;
561     if (!(dummyaddr == (char*)0))
562       { return (&dummy) - dummyaddr; }
563     else
564       { dummyaddr = &dummy;
565         { long result = get_stack_direction();
566           /* The next assignment avoids tail recursion elimination (IRIX 6.4 CC). */
567           dummyaddr = (char*)0;
568           return result;
569       } }
570   }
571
572 void main10(void)
573   { long stack_direction = get_stack_direction();
574     if (stack_direction > 0)
575       { printf("/* Stack grows up, ca. %ld bytes per function call. */\n",(long)stack_direction);
576         printf("#define stack_grows_up\n");
577       }
578     else if (stack_direction < 0)
579       { printf("/* Stack grows down, ca. %ld bytes per function call. */\n",-(long)stack_direction);
580         printf("#define stack_grows_down\n");
581       }
582     else
583       printf("#error \"Unknown stack model -- incorrect C semantics!!\"\n");
584   }
585
586 int main()
587 { main1();
588   main2();
589   main3();
590   main4();
591   main5();
592   main6();
593   main7();
594   main8();
595   main9();
596   main10();
597   if (ferror(stdout)) return 1;
598   return 0;
599 }