home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gs252src.zip / GS252 / GENARCH.C < prev    next >
C/C++ Source or Header  |  1992-08-02  |  2KB  |  63 lines

  1. /* genarch.c */
  2. /* Generate a header file (arch.h) with parameters */
  3. /* reflecting the machine architecture and compiler characteristics. */
  4.  
  5. #include <stdio.h>
  6.  
  7. /* We should write the result on stdout, but the Turbo C 'make' */
  8. /* can't handle output redirection (sigh). */
  9.  
  10. main(argc, argv)
  11.     int argc;
  12.     char *argv[];
  13. {    char *fname = argv[1];
  14.     long one = 1;
  15.     struct { char c; short s; } ss;
  16.     struct { char c; long l; } sl;
  17.     struct { char c; float f; } sf;
  18.     struct { char c; double d; } sd;
  19.     long lm1 = -1;
  20.     long lr1 = lm1 >> 1, lr2 = lm1 >> 2;
  21.     unsigned long um1 = ~(unsigned long)0;
  22.     int im1 = -1;
  23.     int ir1 = im1 >> 1, ir2 = im1 >> 2;
  24.     int ars;
  25.     int lwidth = sizeof(long) * 8;
  26.     float f0 = 0.0, f1 = 1.0, fm1 = -1.0;
  27.     FILE *f = fopen(fname, "w");
  28.     if ( f == NULL )
  29.        {    fprintf(stderr, "genarch.c: can't open %s for writing\n", fname);
  30.         exit(1);
  31.        }
  32.     fprintf(f, "/* Parameters derived from machine and compiler architecture */\n\n");
  33.     fprintf(f, "#define arch_is_big_endian %d\n", 1 - *(char *)&one);
  34.     fprintf(f, "#define arch_align_short_mod %d\n",
  35.         (char *)&ss.s - &ss.c);
  36.     fprintf(f, "#define arch_align_long_mod %d\n",
  37.         (char *)&sl.l - &sl.c);
  38.     fprintf(f, "#define arch_align_float_mod %d\n",
  39.         (char *)&sf.f - &sf.c);
  40.     fprintf(f, "#define arch_align_double_mod %d\n",
  41.         (char *)&sd.d - &sd.c);
  42.     fprintf(f, "#define arch_sizeof_short %d\n", sizeof(short));
  43.     fprintf(f, "#define arch_sizeof_int %d\n", sizeof(int));
  44.     fprintf(f, "#define arch_sizeof_long %d\n", sizeof(long));
  45.     fprintf(f, "#define arch_floats_are_IEEE %d\n",
  46.         (*(long *)&f0 == 0 && *(long *)&f1 == 0x3f800000L &&
  47.          *(long *)&fm1 == 0xbf800000L ? 1 : 0));
  48.     /* There are three cases for arithmetic right shift: */
  49.     /* always correct, correct except for right-shifting a long by 1 */
  50.     /* (a bug in some versions of the Turbo C compiler), and */
  51.     /* never correct. */
  52.     ars = (lr2 != -1 || ir1 != -1 || ir2 != -1 ? 0 :
  53.         lr1 != -1 ? 1 :        /* Turbo C problem */
  54.         2);
  55.     fprintf(f, "#define arch_arith_rshift %d\n", ars);
  56.     /* Some machines can't handle a variable shift by */
  57.     /* the full width of a long. */
  58.     fprintf(f, "#define arch_can_shift_full_long %d\n",
  59.         um1 >> lwidth == 0);
  60.     fclose(f);
  61.     return 0;
  62. }
  63.