You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

583 lines
36 KiB

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