nsprpub/pr/include/gencfg.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

     1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <stdio.h>
     8 #if defined(sgi)
     9 #ifndef IRIX
    10 	error - IRIX is not defined
    11 #endif
    12 #endif
    14 #if defined(__sun)
    15 #ifndef SOLARIS
    16 	error - SOLARIS is not defined
    17 #endif
    18 #endif
    20 #if defined(__hpux)
    21 #ifndef HPUX
    22 	error - HPUX is not defined
    23 #endif
    24 #endif
    26 #if defined(__alpha) 
    27 #if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux)) && !(defined(__FreeBSD__))
    28 	error - None of OSF1, _WIN32, __linux, or __FreeBSD__ is defined
    29 #endif
    30 #endif
    32 #if defined(_IBMR2)
    33 #ifndef AIX
    34 	error - AIX is not defined
    35 #endif
    36 #endif
    38 #if defined(linux)
    39 #ifndef LINUX
    40 	error - LINUX is not defined
    41 #endif
    42 #endif
    44 #if defined(bsdi)
    45 #ifndef BSDI
    46 	error - BSDI is not defined
    47 #endif
    48 #endif
    50 #if defined(M_UNIX)
    51 #ifndef SCO
    52       error - SCO is not defined
    53 #endif
    54 #endif
    55 #if !defined(M_UNIX) && defined(_USLC_)
    56 #ifndef UNIXWARE
    57       error - UNIXWARE is not defined
    58 #endif
    59 #endif
    61 #if defined(__APPLE__)
    62 #ifndef DARWIN
    63       error - DARWIN is not defined
    64 #endif
    65 #endif
    67 /************************************************************************/
    69 /* Generate cpucfg.h */
    71 #ifdef XP_PC
    72 #ifdef WIN32
    73 #define INT64	_PRInt64
    74 #else
    75 #define INT64	long
    76 #endif
    77 #else
    78 #if defined(HPUX) || defined(SCO) || defined(UNIXWARE)
    79 #define INT64	long
    80 #else
    81 #define INT64	long long
    82 #endif
    83 #endif
    85 struct align_short {
    86     char c;
    87     short a;
    88 };
    89 struct align_int {
    90     char c;
    91     int a;
    92 };
    93 struct align_long {
    94     char c;
    95     long a;
    96 };
    97 struct align_PRInt64 {
    98     char c;
    99     INT64 a;
   100 };
   101 struct align_fakelonglong {
   102     char c;
   103     struct {
   104 	long hi, lo;
   105     } a;
   106 };
   107 struct align_float {
   108     char c;
   109     float a;
   110 };
   111 struct align_double {
   112     char c;
   113     double a;
   114 };
   115 struct align_pointer {
   116     char c;
   117     void *a;
   118 };
   120 #define ALIGN_OF(type) \
   121     (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
   123 int bpb;
   125 /* Used if shell doesn't support redirection. By default, assume it does. */
   126 FILE *stream;
   128 static int Log2(int n)
   129 {
   130     int log2 = 0;
   132     if (n & (n-1))
   133 	log2++;
   134     if (n >> 16)
   135 	log2 += 16, n >>= 16;
   136     if (n >> 8)
   137 	log2 += 8, n >>= 8;
   138     if (n >> 4)
   139 	log2 += 4, n >>= 4;
   140     if (n >> 2)
   141 	log2 += 2, n >>= 2;
   142     if (n >> 1)
   143 	log2++;
   144     return log2;
   145 }
   147 /* We assume that int's are 32 bits */
   148 static void do64(void)
   149 {
   150     union {
   151 	int i;
   152 	char c[4];
   153     } u;
   155     u.i = 0x01020304;
   156     if (u.c[0] == 0x01) {
   157 	fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
   158 	fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
   159     } else {
   160 	fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
   161 	fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
   162     }
   163 }
   165 static void do32(void)
   166 {
   167     union {
   168 	long i;
   169 	char c[4];
   170     } u;
   172     u.i = 0x01020304;
   173     if (u.c[0] == 0x01) {
   174 	fprintf(stream, "#undef  IS_LITTLE_ENDIAN\n");
   175 	fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n");
   176     } else {
   177 	fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n");
   178 	fprintf(stream, "#undef  IS_BIG_ENDIAN\n\n");
   179     }
   180 }
   182 /*
   183 ** Concievably this could actually be used; but there is lots of code out
   184 ** there with and's and shift's in it that assumes a byte is 8 bits, so
   185 ** forget about porting THIS code to those non 8 bit byte machines.
   186 */
   187 static void BitsPerByte(void)
   188 {
   189     bpb = 8;
   190 }
   192 int main(int argc, char **argv)
   193 {
   194     BitsPerByte();
   196     /* If we got a command line argument, try to use it as the stream. */
   197     ++argv;
   198     if(*argv) {
   199         if(!(stream = fopen ( *argv, "wt" ))) {
   200             fprintf(stderr, "Could not write to output file %s.\n", *argv);
   201             return 1;
   202         }
   203     } else {
   204 		stream = stdout;
   205 	}
   207     fprintf(stream, "#ifndef nspr_cpucfg___\n");
   208     fprintf(stream, "#define nspr_cpucfg___\n\n");
   210     fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
   212     if (sizeof(long) == 8) {
   213 	do64();
   214     } else {
   215 	do32();
   216     }
   217     fprintf(stream, "#define PR_BYTES_PER_BYTE   %d\n", sizeof(char));
   218     fprintf(stream, "#define PR_BYTES_PER_SHORT  %d\n", sizeof(short));
   219     fprintf(stream, "#define PR_BYTES_PER_INT    %d\n", sizeof(int));
   220     fprintf(stream, "#define PR_BYTES_PER_INT64  %d\n", 8);
   221     fprintf(stream, "#define PR_BYTES_PER_LONG   %d\n", sizeof(long));
   222     fprintf(stream, "#define PR_BYTES_PER_FLOAT  %d\n", sizeof(float));
   223     fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double));
   225     fprintf(stream, "#define PR_BITS_PER_BYTE    %d\n", bpb);
   226     fprintf(stream, "#define PR_BITS_PER_SHORT   %d\n", bpb * sizeof(short));
   227     fprintf(stream, "#define PR_BITS_PER_INT     %d\n", bpb * sizeof(int));
   228     fprintf(stream, "#define PR_BITS_PER_INT64   %d\n", bpb * 8);
   229     fprintf(stream, "#define PR_BITS_PER_LONG    %d\n", bpb * sizeof(long));
   230     fprintf(stream, "#define PR_BITS_PER_FLOAT   %d\n", bpb * sizeof(float));
   231     fprintf(stream, "#define PR_BITS_PER_DOUBLE  %d\n\n", 
   232             bpb * sizeof(double));
   234     fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2   %d\n", Log2(bpb));
   235     fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2  %d\n", 
   236             Log2(bpb * sizeof(short)));
   237     fprintf(stream, "#define PR_BITS_PER_INT_LOG2    %d\n", 
   238             Log2(bpb * sizeof(int)));
   239     fprintf(stream, "#define PR_BITS_PER_INT64_LOG2  %d\n", 6);
   240     fprintf(stream, "#define PR_BITS_PER_LONG_LOG2   %d\n", 
   241             Log2(bpb * sizeof(long)));
   242     fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2  %d\n", 
   243             Log2(bpb * sizeof(float)));
   244     fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 
   245             Log2(bpb * sizeof(double)));
   247     fprintf(stream, "#define PR_ALIGN_OF_SHORT   %d\n", ALIGN_OF(short));
   248     fprintf(stream, "#define PR_ALIGN_OF_INT     %d\n", ALIGN_OF(int));
   249     fprintf(stream, "#define PR_ALIGN_OF_LONG    %d\n", ALIGN_OF(long));
   250     if (sizeof(INT64) < 8) {
   251 	/* this machine doesn't actually support PRInt64's */
   252 	fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", 
   253                 ALIGN_OF(fakelonglong));
   254     } else {
   255 	fprintf(stream, "#define PR_ALIGN_OF_INT64   %d\n", ALIGN_OF(PRInt64));
   256     }
   257     fprintf(stream, "#define PR_ALIGN_OF_FLOAT   %d\n", ALIGN_OF(float));
   258     fprintf(stream, "#define PR_ALIGN_OF_DOUBLE  %d\n", ALIGN_OF(double));
   259     fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer));
   261     fprintf(stream, "#endif /* nspr_cpucfg___ */\n");
   262     fclose(stream);
   264     return 0;
   265 }

mercurial