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