home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / include / gencfg.c next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.1 KB  |  279 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. /************************************************************************/
  81.  
  82. /* Generate cpucfg.h */
  83.  
  84. #ifdef XP_PC
  85. #ifdef WIN32
  86. #define INT64    _PRInt64
  87. #else
  88. #define INT64    long
  89. #endif
  90. #else
  91. #if defined(HPUX) || defined(NECSVR4) || defined(SCO) || defined(UNIXWARE) || defined (NCR)
  92. #define INT64    long
  93. #else
  94. #define INT64    long long
  95. #endif
  96. #endif
  97.  
  98. struct align_short {
  99.     char c;
  100.     short a;
  101. };
  102. struct align_int {
  103.     char c;
  104.     int a;
  105. };
  106. struct align_long {
  107.     char c;
  108.     long a;
  109. };
  110. struct align_PRInt64 {
  111.     char c;
  112.     INT64 a;
  113. };
  114. struct align_fakelonglong {
  115.     char c;
  116.     struct {
  117.     long hi, lo;
  118.     } a;
  119. };
  120. struct align_float {
  121.     char c;
  122.     float a;
  123. };
  124. struct align_double {
  125.     char c;
  126.     double a;
  127. };
  128. struct align_pointer {
  129.     char c;
  130.     void *a;
  131. };
  132.  
  133. #define ALIGN_OF(type) \
  134.     (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
  135.  
  136. int bpb;
  137.  
  138. /* Used if shell doesn't support redirection. By default, assume it does. */
  139. FILE *stream;
  140.  
  141. static int Log2(int n)
  142. {
  143.     int log2 = 0;
  144.  
  145.     if (n & (n-1))
  146.     log2++;
  147.     if (n >> 16)
  148.     log2 += 16, n >>= 16;
  149.     if (n >> 8)
  150.     log2 += 8, n >>= 8;
  151.     if (n >> 4)
  152.     log2 += 4, n >>= 4;
  153.     if (n >> 2)
  154.     log2 += 2, n >>= 2;
  155.     if (n >> 1)
  156.     log2++;
  157.     return log2;
  158. }
  159.  
  160. /* We assume that int's are 32 bits */
  161. static void do64(void)
  162. {
  163.     union {
  164.     int i;
  165.     char c[4];
  166.     } u;
  167.  
  168.     u.i = 0x01020304;
  169.     if (u.c[0] == 0x01) {
  170.     fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
  171.     fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
  172.     } else {
  173.     fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
  174.     fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
  175.     }
  176. }
  177.  
  178. static void do32(void)
  179. {
  180.     union {
  181.     long i;
  182.     char c[4];
  183.     } u;
  184.  
  185.     u.i = 0x01020304;
  186.     if (u.c[0] == 0x01) {
  187.     fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
  188.     fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
  189.     } else {
  190.     fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
  191.     fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
  192.     }
  193. }
  194.  
  195. /*
  196. ** Concievably this could actually be used; but there is lots of code out
  197. ** there with and's and shift's in it that assumes a byte is 8 bits, so
  198. ** forget about porting THIS code to those non 8 bit byte machines.
  199. */
  200. static void BitsPerByte(void)
  201. {
  202.     bpb = 8;
  203. }
  204.  
  205. int main(int argc, char **argv)
  206. {
  207.     BitsPerByte();
  208.  
  209.     /* If we got a command line argument, try to use it as the stream. */
  210.     ++argv;
  211.     if(*argv) {
  212.         if(!(stream = fopen ( *argv, "wt" ))) {
  213.             fprintf(stderr, "Could not write to output file %s.\n", *argv);
  214.             return 1;
  215.         }
  216.     } else {
  217.         stream = stdout;
  218.     }
  219.  
  220.     fprintf(stream, "#ifndef nspr_cpucfg___\n");
  221.     fprintf(stream, "#define nspr_cpucfg___\n\n");
  222.  
  223.     fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
  224.  
  225.     if (sizeof(long) == 8) {
  226.     do64();
  227.     } else {
  228.     do32();
  229.     }
  230.     fprintf(stream, "#define PR_BYTES_PER_BYTE   %d\n", sizeof(char));
  231.     fprintf(stream, "#define PR_BYTES_PER_SHORT  %d\n", sizeof(short));
  232.     fprintf(stream, "#define PR_BYTES_PER_INT    %d\n", sizeof(int));
  233.     fprintf(stream, "#define PR_BYTES_PER_INT64  %d\n", 8);
  234.     fprintf(stream, "#define PR_BYTES_PER_LONG   %d\n", sizeof(long));
  235.     fprintf(stream, "#define PR_BYTES_PER_FLOAT  %d\n", sizeof(float));
  236.     fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double));
  237.  
  238.     fprintf(stream, "#define PR_BITS_PER_BYTE    %d\n", bpb);
  239.     fprintf(stream, "#define PR_BITS_PER_SHORT   %d\n", bpb * sizeof(short));
  240.     fprintf(stream, "#define PR_BITS_PER_INT     %d\n", bpb * sizeof(int));
  241.     fprintf(stream, "#define PR_BITS_PER_INT64   %d\n", bpb * 8);
  242.     fprintf(stream, "#define PR_BITS_PER_LONG    %d\n", bpb * sizeof(long));
  243.     fprintf(stream, "#define PR_BITS_PER_FLOAT   %d\n", bpb * sizeof(float));
  244.     fprintf(stream, "#define PR_BITS_PER_DOUBLE  %d\n\n", 
  245.             bpb * sizeof(double));
  246.  
  247.     fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2   %d\n", Log2(bpb));
  248.     fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2  %d\n", 
  249.             Log2(bpb * sizeof(short)));
  250.     fprintf(stream, "#define PR_BITS_PER_INT_LOG2    %d\n", 
  251.             Log2(bpb * sizeof(int)));
  252.     fprintf(stream, "#define PR_BITS_PER_INT64_LOG2  %d\n", 6);
  253.     fprintf(stream, "#define PR_BITS_PER_LONG_LOG2   %d\n", 
  254.             Log2(bpb * sizeof(long)));
  255.     fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2  %d\n", 
  256.             Log2(bpb * sizeof(float)));
  257.     fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 
  258.             Log2(bpb * sizeof(double)));
  259.  
  260.     fprintf(stream, "#define PR_ALIGN_OF_SHORT   %d\n", ALIGN_OF(short));
  261.     fprintf(stream, "#define PR_ALIGN_OF_INT     %d\n", ALIGN_OF(int));
  262.     fprintf(stream, "#define PR_ALIGN_OF_LONG    %d\n", ALIGN_OF(long));
  263.     if (sizeof(INT64) < 8) {
  264.     /* this machine doesn't actually support PRInt64's */
  265.     fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", 
  266.                 ALIGN_OF(fakelonglong));
  267.     } else {
  268.     fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", ALIGN_OF(PRInt64));
  269.     }
  270.     fprintf(stream, "#define PR_ALIGN_OF_FLOAT   %d\n", ALIGN_OF(float));
  271.     fprintf(stream, "#define PR_ALIGN_OF_DOUBLE  %d\n", ALIGN_OF(double));
  272.     fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer));
  273.  
  274.     fprintf(stream, "#endif /* nspr_cpucfg___ */\n");
  275.     fclose(stream);
  276.  
  277.     return 0;
  278. }
  279.