home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / calc / mch_defines.c < prev   
Encoding:
C/C++ Source or Header  |  1988-05-17  |  2.7 KB  |  73 lines

  1. /* This program was snagged off the USENET comp.lang.c mailing list 22 July 1987 */
  2.  
  3. /* The contents of the following program are copyright 1987 by John Cowan.
  4. It is hereby released to the public domain.
  5.  
  6. This program emits C #define statements to the standard output describing
  7. the machine it is executing on.  The following #defines are generated:
  8.         Mch_Csz -       size of a char, in bits
  9.         Mch_Ssz -       size of a short int, in bits
  10.         Mch_Isz -       size of a plain int, in bits
  11.         Mch_Lsz -       size of a long int, in bits
  12.         Mch_BE -        defined if the machine is big-endian; that is, if
  13.                         the most significant byte in a number appears first.
  14.         Mch_LE -        defined if the machine is little-endian; that is, if
  15.                         the least significant byte in a number appears first.
  16.         Mch_PDP -       defined if the machine uses PDP-11 byte ordering;
  17.                         LE for bytes-in-a-word and BE for words-in-a-long.
  18.         Mch_ONE -       defined if the machine uses one's complement arithmetic.
  19.         Mch_sgc -       defined if characters can be signed.
  20. */
  21.  
  22. #include <stdio.h>
  23.  
  24. char bittest[9] = "\001\001\001\001\001\001\001\001"; /*Changed from [6] for CRAY X-MP -WM*/
  25. char endtest[6] = "\001\002\003\004\005";
  26. long be = 1;
  27. long le = 1;
  28. long pdp;
  29. int byteoff;
  30. int bytesize;
  31. long longval;
  32.  
  33. main()
  34.         {
  35.         int i;
  36.  
  37.         byteoff = (*(int *) bittest & 2047) - 1;
  38.         switch (byteoff) {
  39.         case 256: bytesize = 8; break;
  40.         case 512: bytesize = 9; break;
  41.         case 1024: bytesize = 10; break;
  42.         default: fprintf(stderr, "mch: bogus byte size\n"); exit(1);
  43.                 }
  44.         printf("#define Mch_Csz %d\n", bytesize);
  45.         printf("#define Mch_Ssz %d\n", sizeof(short) * bytesize);
  46.         printf("#define Mch_Isz %d\n", sizeof(int) * bytesize);
  47.         printf("#define Mch_Lsz %d\n", sizeof(long) * bytesize);
  48.         longval = *(long *) endtest;
  49.         for (i = 0; i < sizeof(long); i++) {
  50.                 be *= byteoff;
  51.                 be += endtest[i];
  52.                 }
  53.         for (i = sizeof(long) - 1; i >= 0; i--) {
  54.                 le *= byteoff;
  55.                 le += endtest[i];
  56.                 }
  57.         pdp = 0x02010403;
  58.         if (longval == be)
  59.                 printf("#define Mch_BE 1\n");
  60.         else if (longval == le)
  61.                 printf("#define Mch_LE 1\n");
  62.         else if (longval == pdp)
  63.                 printf("#define Mch_PDP 1\n");
  64.         else {
  65.                 fprintf(stderr, "mch: bogus endianism\n");
  66.                 exit(1);
  67.                 }
  68.         if (~0 == 0)
  69.                 printf("#define Mch_ONE 1\n");
  70.         if ('\377' < 0)       /* modified 1987/07/22 R. Dhesi */
  71.                 printf("#define Mch_sgc 1\n");
  72.         }
  73.