michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: michael@0: #if defined(sgi) michael@0: #ifndef IRIX michael@0: error - IRIX is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(__sun) michael@0: #ifndef SOLARIS michael@0: error - SOLARIS is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(__hpux) michael@0: #ifndef HPUX michael@0: error - HPUX is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(__alpha) michael@0: #if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux)) && !(defined(__FreeBSD__)) michael@0: error - None of OSF1, _WIN32, __linux, or __FreeBSD__ is defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(_IBMR2) michael@0: #ifndef AIX michael@0: error - AIX is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(linux) michael@0: #ifndef LINUX michael@0: error - LINUX is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(bsdi) michael@0: #ifndef BSDI michael@0: error - BSDI is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(M_UNIX) michael@0: #ifndef SCO michael@0: error - SCO is not defined michael@0: #endif michael@0: #endif michael@0: #if !defined(M_UNIX) && defined(_USLC_) michael@0: #ifndef UNIXWARE michael@0: error - UNIXWARE is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: #if defined(__APPLE__) michael@0: #ifndef DARWIN michael@0: error - DARWIN is not defined michael@0: #endif michael@0: #endif michael@0: michael@0: /************************************************************************/ michael@0: michael@0: /* Generate cpucfg.h */ michael@0: michael@0: #ifdef XP_PC michael@0: #ifdef WIN32 michael@0: #define INT64 _PRInt64 michael@0: #else michael@0: #define INT64 long michael@0: #endif michael@0: #else michael@0: #if defined(HPUX) || defined(SCO) || defined(UNIXWARE) michael@0: #define INT64 long michael@0: #else michael@0: #define INT64 long long michael@0: #endif michael@0: #endif michael@0: michael@0: struct align_short { michael@0: char c; michael@0: short a; michael@0: }; michael@0: struct align_int { michael@0: char c; michael@0: int a; michael@0: }; michael@0: struct align_long { michael@0: char c; michael@0: long a; michael@0: }; michael@0: struct align_PRInt64 { michael@0: char c; michael@0: INT64 a; michael@0: }; michael@0: struct align_fakelonglong { michael@0: char c; michael@0: struct { michael@0: long hi, lo; michael@0: } a; michael@0: }; michael@0: struct align_float { michael@0: char c; michael@0: float a; michael@0: }; michael@0: struct align_double { michael@0: char c; michael@0: double a; michael@0: }; michael@0: struct align_pointer { michael@0: char c; michael@0: void *a; michael@0: }; michael@0: michael@0: #define ALIGN_OF(type) \ michael@0: (((char*)&(((struct align_##type *)0)->a)) - ((char*)0)) michael@0: michael@0: int bpb; michael@0: michael@0: /* Used if shell doesn't support redirection. By default, assume it does. */ michael@0: FILE *stream; michael@0: michael@0: static int Log2(int n) michael@0: { michael@0: int log2 = 0; michael@0: michael@0: if (n & (n-1)) michael@0: log2++; michael@0: if (n >> 16) michael@0: log2 += 16, n >>= 16; michael@0: if (n >> 8) michael@0: log2 += 8, n >>= 8; michael@0: if (n >> 4) michael@0: log2 += 4, n >>= 4; michael@0: if (n >> 2) michael@0: log2 += 2, n >>= 2; michael@0: if (n >> 1) michael@0: log2++; michael@0: return log2; michael@0: } michael@0: michael@0: /* We assume that int's are 32 bits */ michael@0: static void do64(void) michael@0: { michael@0: union { michael@0: int i; michael@0: char c[4]; michael@0: } u; michael@0: michael@0: u.i = 0x01020304; michael@0: if (u.c[0] == 0x01) { michael@0: fprintf(stream, "#undef IS_LITTLE_ENDIAN\n"); michael@0: fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n"); michael@0: } else { michael@0: fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n"); michael@0: fprintf(stream, "#undef IS_BIG_ENDIAN\n\n"); michael@0: } michael@0: } michael@0: michael@0: static void do32(void) michael@0: { michael@0: union { michael@0: long i; michael@0: char c[4]; michael@0: } u; michael@0: michael@0: u.i = 0x01020304; michael@0: if (u.c[0] == 0x01) { michael@0: fprintf(stream, "#undef IS_LITTLE_ENDIAN\n"); michael@0: fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n"); michael@0: } else { michael@0: fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n"); michael@0: fprintf(stream, "#undef IS_BIG_ENDIAN\n\n"); michael@0: } michael@0: } michael@0: michael@0: /* michael@0: ** Concievably this could actually be used; but there is lots of code out michael@0: ** there with and's and shift's in it that assumes a byte is 8 bits, so michael@0: ** forget about porting THIS code to those non 8 bit byte machines. michael@0: */ michael@0: static void BitsPerByte(void) michael@0: { michael@0: bpb = 8; michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: BitsPerByte(); michael@0: michael@0: /* If we got a command line argument, try to use it as the stream. */ michael@0: ++argv; michael@0: if(*argv) { michael@0: if(!(stream = fopen ( *argv, "wt" ))) { michael@0: fprintf(stderr, "Could not write to output file %s.\n", *argv); michael@0: return 1; michael@0: } michael@0: } else { michael@0: stream = stdout; michael@0: } michael@0: michael@0: fprintf(stream, "#ifndef nspr_cpucfg___\n"); michael@0: fprintf(stream, "#define nspr_cpucfg___\n\n"); michael@0: michael@0: fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n"); michael@0: michael@0: if (sizeof(long) == 8) { michael@0: do64(); michael@0: } else { michael@0: do32(); michael@0: } michael@0: fprintf(stream, "#define PR_BYTES_PER_BYTE %d\n", sizeof(char)); michael@0: fprintf(stream, "#define PR_BYTES_PER_SHORT %d\n", sizeof(short)); michael@0: fprintf(stream, "#define PR_BYTES_PER_INT %d\n", sizeof(int)); michael@0: fprintf(stream, "#define PR_BYTES_PER_INT64 %d\n", 8); michael@0: fprintf(stream, "#define PR_BYTES_PER_LONG %d\n", sizeof(long)); michael@0: fprintf(stream, "#define PR_BYTES_PER_FLOAT %d\n", sizeof(float)); michael@0: fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double)); michael@0: michael@0: fprintf(stream, "#define PR_BITS_PER_BYTE %d\n", bpb); michael@0: fprintf(stream, "#define PR_BITS_PER_SHORT %d\n", bpb * sizeof(short)); michael@0: fprintf(stream, "#define PR_BITS_PER_INT %d\n", bpb * sizeof(int)); michael@0: fprintf(stream, "#define PR_BITS_PER_INT64 %d\n", bpb * 8); michael@0: fprintf(stream, "#define PR_BITS_PER_LONG %d\n", bpb * sizeof(long)); michael@0: fprintf(stream, "#define PR_BITS_PER_FLOAT %d\n", bpb * sizeof(float)); michael@0: fprintf(stream, "#define PR_BITS_PER_DOUBLE %d\n\n", michael@0: bpb * sizeof(double)); michael@0: michael@0: fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2 %d\n", Log2(bpb)); michael@0: fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2 %d\n", michael@0: Log2(bpb * sizeof(short))); michael@0: fprintf(stream, "#define PR_BITS_PER_INT_LOG2 %d\n", michael@0: Log2(bpb * sizeof(int))); michael@0: fprintf(stream, "#define PR_BITS_PER_INT64_LOG2 %d\n", 6); michael@0: fprintf(stream, "#define PR_BITS_PER_LONG_LOG2 %d\n", michael@0: Log2(bpb * sizeof(long))); michael@0: fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2 %d\n", michael@0: Log2(bpb * sizeof(float))); michael@0: fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", michael@0: Log2(bpb * sizeof(double))); michael@0: michael@0: fprintf(stream, "#define PR_ALIGN_OF_SHORT %d\n", ALIGN_OF(short)); michael@0: fprintf(stream, "#define PR_ALIGN_OF_INT %d\n", ALIGN_OF(int)); michael@0: fprintf(stream, "#define PR_ALIGN_OF_LONG %d\n", ALIGN_OF(long)); michael@0: if (sizeof(INT64) < 8) { michael@0: /* this machine doesn't actually support PRInt64's */ michael@0: fprintf(stream, "#define PR_ALIGN_OF_INT64 %d\n", michael@0: ALIGN_OF(fakelonglong)); michael@0: } else { michael@0: fprintf(stream, "#define PR_ALIGN_OF_INT64 %d\n", ALIGN_OF(PRInt64)); michael@0: } michael@0: fprintf(stream, "#define PR_ALIGN_OF_FLOAT %d\n", ALIGN_OF(float)); michael@0: fprintf(stream, "#define PR_ALIGN_OF_DOUBLE %d\n", ALIGN_OF(double)); michael@0: fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer)); michael@0: michael@0: fprintf(stream, "#endif /* nspr_cpucfg___ */\n"); michael@0: fclose(stream); michael@0: michael@0: return 0; michael@0: }