home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / gencfg.c next >
C/C++ Source or Header  |  1998-11-21  |  7KB  |  291 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. #if defined(sgi)
  22. #ifndef IRIX
  23.     error - IRIX is not defined
  24. #endif
  25. #endif
  26.  
  27. #if defined(__sun)
  28. #if defined(__svr4) || defined(__svr4__) || defined(__SVR4)
  29. #ifndef SOLARIS
  30.     error - SOLARIS is not defined
  31. #endif
  32. #else
  33. #ifndef SUNOS4
  34.     error - SUNOS4 is not defined
  35. #endif
  36. #endif
  37. #endif
  38.  
  39. #if defined(__hpux)
  40. #ifndef HPUX
  41.     error - HPUX is not defined
  42. #endif
  43. #endif
  44.  
  45. #if defined(__alpha) 
  46. #if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux))
  47.     error - None of OSF1, _WIN32, or __linux is defined
  48. #endif
  49. #endif
  50.  
  51. #if defined(_IBMR2)
  52. #ifndef AIX
  53.     error - AIX is not defined
  54. #endif
  55. #endif
  56.  
  57. #if defined(linux)
  58. #ifndef LINUX
  59.     error - LINUX is not defined
  60. #endif
  61. #endif
  62.  
  63. #if defined(bsdi)
  64. #ifndef BSDI
  65.     error - BSDI is not defined
  66. #endif
  67. #endif
  68.  
  69. #if defined(M_UNIX)
  70. #ifndef SCO
  71.       error - SCO is not defined
  72. #endif
  73. #endif
  74. #if !defined(M_UNIX) && defined(_USLC_)
  75. #ifndef UNIXWARE
  76.       error - UNIXWARE is not defined
  77. #endif
  78. #endif
  79.  
  80. #if defined(__APPLE__)
  81. #ifndef RHAPSODY
  82.       error - RHAPSODY is not defined
  83. #endif
  84. #endif
  85.  
  86. #if defined(__NeXT__)
  87. #ifndef NEXTSTEP
  88.       error - NEXTSTEP is not defined
  89. #endif
  90. #endif
  91.  
  92. /************************************************************************/
  93.  
  94. /* Generate cpucfg.h */
  95.  
  96. #ifdef XP_PC
  97. #ifdef WIN32
  98. #define INT64    _PRInt64
  99. #else
  100. #define INT64    long
  101. #endif
  102. #else
  103. #if defined(HPUX) || defined(NECSVR4) || defined(SCO) || defined(UNIXWARE) || defined (NCR)
  104. #define INT64    long
  105. #else
  106. #define INT64    long long
  107. #endif
  108. #endif
  109.  
  110. struct align_short {
  111.     char c;
  112.     short a;
  113. };
  114. struct align_int {
  115.     char c;
  116.     int a;
  117. };
  118. struct align_long {
  119.     char c;
  120.     long a;
  121. };
  122. struct align_PRInt64 {
  123.     char c;
  124.     INT64 a;
  125. };
  126. struct align_fakelonglong {
  127.     char c;
  128.     struct {
  129.     long hi, lo;
  130.     } a;
  131. };
  132. struct align_float {
  133.     char c;
  134.     float a;
  135. };
  136. struct align_double {
  137.     char c;
  138.     double a;
  139. };
  140. struct align_pointer {
  141.     char c;
  142.     void *a;
  143. };
  144.  
  145. #define ALIGN_OF(type) \
  146.     (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
  147.  
  148. int bpb;
  149.  
  150. /* Used if shell doesn't support redirection. By default, assume it does. */
  151. FILE *stream;
  152.  
  153. static int Log2(int n)
  154. {
  155.     int log2 = 0;
  156.  
  157.     if (n & (n-1))
  158.     log2++;
  159.     if (n >> 16)
  160.     log2 += 16, n >>= 16;
  161.     if (n >> 8)
  162.     log2 += 8, n >>= 8;
  163.     if (n >> 4)
  164.     log2 += 4, n >>= 4;
  165.     if (n >> 2)
  166.     log2 += 2, n >>= 2;
  167.     if (n >> 1)
  168.     log2++;
  169.     return log2;
  170. }
  171.  
  172. /* We assume that int's are 32 bits */
  173. static void do64(void)
  174. {
  175.     union {
  176.     int i;
  177.     char c[4];
  178.     } u;
  179.  
  180.     u.i = 0x01020304;
  181.     if (u.c[0] == 0x01) {
  182.     fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
  183.     fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
  184.     } else {
  185.     fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
  186.     fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
  187.     }
  188. }
  189.  
  190. static void do32(void)
  191. {
  192.     union {
  193.     long i;
  194.     char c[4];
  195.     } u;
  196.  
  197.     u.i = 0x01020304;
  198.     if (u.c[0] == 0x01) {
  199.     fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
  200.     fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
  201.     } else {
  202.     fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
  203.     fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
  204.     }
  205. }
  206.  
  207. /*
  208. ** Concievably this could actually be used; but there is lots of code out
  209. ** there with and's and shift's in it that assumes a byte is 8 bits, so
  210. ** forget about porting THIS code to those non 8 bit byte machines.
  211. */
  212. static void BitsPerByte(void)
  213. {
  214.     bpb = 8;
  215. }
  216.  
  217. int main(int argc, char **argv)
  218. {
  219.     BitsPerByte();
  220.  
  221.     /* If we got a command line argument, try to use it as the stream. */
  222.     ++argv;
  223.     if(*argv) {
  224.         if(!(stream = fopen ( *argv, "wt" ))) {
  225.             fprintf(stderr, "Could not write to output file %s.\n", *argv);
  226.             return 1;
  227.         }
  228.     } else {
  229.         stream = stdout;
  230.     }
  231.  
  232.     fprintf(stream, "#ifndef nspr_cpucfg___\n");
  233.     fprintf(stream, "#define nspr_cpucfg___\n\n");
  234.  
  235.     fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
  236.  
  237.     if (sizeof(long) == 8) {
  238.     do64();
  239.     } else {
  240.     do32();
  241.     }
  242.     fprintf(stream, "#define PR_BYTES_PER_BYTE   %d\n", sizeof(char));
  243.     fprintf(stream, "#define PR_BYTES_PER_SHORT  %d\n", sizeof(short));
  244.     fprintf(stream, "#define PR_BYTES_PER_INT    %d\n", sizeof(int));
  245.     fprintf(stream, "#define PR_BYTES_PER_INT64  %d\n", 8);
  246.     fprintf(stream, "#define PR_BYTES_PER_LONG   %d\n", sizeof(long));
  247.     fprintf(stream, "#define PR_BYTES_PER_FLOAT  %d\n", sizeof(float));
  248.     fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double));
  249.  
  250.     fprintf(stream, "#define PR_BITS_PER_BYTE    %d\n", bpb);
  251.     fprintf(stream, "#define PR_BITS_PER_SHORT   %d\n", bpb * sizeof(short));
  252.     fprintf(stream, "#define PR_BITS_PER_INT     %d\n", bpb * sizeof(int));
  253.     fprintf(stream, "#define PR_BITS_PER_INT64   %d\n", bpb * 8);
  254.     fprintf(stream, "#define PR_BITS_PER_LONG    %d\n", bpb * sizeof(long));
  255.     fprintf(stream, "#define PR_BITS_PER_FLOAT   %d\n", bpb * sizeof(float));
  256.     fprintf(stream, "#define PR_BITS_PER_DOUBLE  %d\n\n", 
  257.             bpb * sizeof(double));
  258.  
  259.     fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2   %d\n", Log2(bpb));
  260.     fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2  %d\n", 
  261.             Log2(bpb * sizeof(short)));
  262.     fprintf(stream, "#define PR_BITS_PER_INT_LOG2    %d\n", 
  263.             Log2(bpb * sizeof(int)));
  264.     fprintf(stream, "#define PR_BITS_PER_INT64_LOG2  %d\n", 6);
  265.     fprintf(stream, "#define PR_BITS_PER_LONG_LOG2   %d\n", 
  266.             Log2(bpb * sizeof(long)));
  267.     fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2  %d\n", 
  268.             Log2(bpb * sizeof(float)));
  269.     fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 
  270.             Log2(bpb * sizeof(double)));
  271.  
  272.     fprintf(stream, "#define PR_ALIGN_OF_SHORT   %d\n", ALIGN_OF(short));
  273.     fprintf(stream, "#define PR_ALIGN_OF_INT     %d\n", ALIGN_OF(int));
  274.     fprintf(stream, "#define PR_ALIGN_OF_LONG    %d\n", ALIGN_OF(long));
  275.     if (sizeof(INT64) < 8) {
  276.     /* this machine doesn't actually support PRInt64's */
  277.     fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", 
  278.                 ALIGN_OF(fakelonglong));
  279.     } else {
  280.     fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", ALIGN_OF(PRInt64));
  281.     }
  282.     fprintf(stream, "#define PR_ALIGN_OF_FLOAT   %d\n", ALIGN_OF(float));
  283.     fprintf(stream, "#define PR_ALIGN_OF_DOUBLE  %d\n", ALIGN_OF(double));
  284.     fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer));
  285.  
  286.     fprintf(stream, "#endif /* nspr_cpucfg___ */\n");
  287.     fclose(stream);
  288.  
  289.     return 0;
  290. }
  291.