1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/include/gencfg.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,265 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <stdio.h> 1.10 + 1.11 +#if defined(sgi) 1.12 +#ifndef IRIX 1.13 + error - IRIX is not defined 1.14 +#endif 1.15 +#endif 1.16 + 1.17 +#if defined(__sun) 1.18 +#ifndef SOLARIS 1.19 + error - SOLARIS is not defined 1.20 +#endif 1.21 +#endif 1.22 + 1.23 +#if defined(__hpux) 1.24 +#ifndef HPUX 1.25 + error - HPUX is not defined 1.26 +#endif 1.27 +#endif 1.28 + 1.29 +#if defined(__alpha) 1.30 +#if !(defined(_WIN32)) && !(defined(OSF1)) && !(defined(__linux)) && !(defined(__FreeBSD__)) 1.31 + error - None of OSF1, _WIN32, __linux, or __FreeBSD__ is defined 1.32 +#endif 1.33 +#endif 1.34 + 1.35 +#if defined(_IBMR2) 1.36 +#ifndef AIX 1.37 + error - AIX is not defined 1.38 +#endif 1.39 +#endif 1.40 + 1.41 +#if defined(linux) 1.42 +#ifndef LINUX 1.43 + error - LINUX is not defined 1.44 +#endif 1.45 +#endif 1.46 + 1.47 +#if defined(bsdi) 1.48 +#ifndef BSDI 1.49 + error - BSDI is not defined 1.50 +#endif 1.51 +#endif 1.52 + 1.53 +#if defined(M_UNIX) 1.54 +#ifndef SCO 1.55 + error - SCO is not defined 1.56 +#endif 1.57 +#endif 1.58 +#if !defined(M_UNIX) && defined(_USLC_) 1.59 +#ifndef UNIXWARE 1.60 + error - UNIXWARE is not defined 1.61 +#endif 1.62 +#endif 1.63 + 1.64 +#if defined(__APPLE__) 1.65 +#ifndef DARWIN 1.66 + error - DARWIN is not defined 1.67 +#endif 1.68 +#endif 1.69 + 1.70 +/************************************************************************/ 1.71 + 1.72 +/* Generate cpucfg.h */ 1.73 + 1.74 +#ifdef XP_PC 1.75 +#ifdef WIN32 1.76 +#define INT64 _PRInt64 1.77 +#else 1.78 +#define INT64 long 1.79 +#endif 1.80 +#else 1.81 +#if defined(HPUX) || defined(SCO) || defined(UNIXWARE) 1.82 +#define INT64 long 1.83 +#else 1.84 +#define INT64 long long 1.85 +#endif 1.86 +#endif 1.87 + 1.88 +struct align_short { 1.89 + char c; 1.90 + short a; 1.91 +}; 1.92 +struct align_int { 1.93 + char c; 1.94 + int a; 1.95 +}; 1.96 +struct align_long { 1.97 + char c; 1.98 + long a; 1.99 +}; 1.100 +struct align_PRInt64 { 1.101 + char c; 1.102 + INT64 a; 1.103 +}; 1.104 +struct align_fakelonglong { 1.105 + char c; 1.106 + struct { 1.107 + long hi, lo; 1.108 + } a; 1.109 +}; 1.110 +struct align_float { 1.111 + char c; 1.112 + float a; 1.113 +}; 1.114 +struct align_double { 1.115 + char c; 1.116 + double a; 1.117 +}; 1.118 +struct align_pointer { 1.119 + char c; 1.120 + void *a; 1.121 +}; 1.122 + 1.123 +#define ALIGN_OF(type) \ 1.124 + (((char*)&(((struct align_##type *)0)->a)) - ((char*)0)) 1.125 + 1.126 +int bpb; 1.127 + 1.128 +/* Used if shell doesn't support redirection. By default, assume it does. */ 1.129 +FILE *stream; 1.130 + 1.131 +static int Log2(int n) 1.132 +{ 1.133 + int log2 = 0; 1.134 + 1.135 + if (n & (n-1)) 1.136 + log2++; 1.137 + if (n >> 16) 1.138 + log2 += 16, n >>= 16; 1.139 + if (n >> 8) 1.140 + log2 += 8, n >>= 8; 1.141 + if (n >> 4) 1.142 + log2 += 4, n >>= 4; 1.143 + if (n >> 2) 1.144 + log2 += 2, n >>= 2; 1.145 + if (n >> 1) 1.146 + log2++; 1.147 + return log2; 1.148 +} 1.149 + 1.150 +/* We assume that int's are 32 bits */ 1.151 +static void do64(void) 1.152 +{ 1.153 + union { 1.154 + int i; 1.155 + char c[4]; 1.156 + } u; 1.157 + 1.158 + u.i = 0x01020304; 1.159 + if (u.c[0] == 0x01) { 1.160 + fprintf(stream, "#undef IS_LITTLE_ENDIAN\n"); 1.161 + fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n"); 1.162 + } else { 1.163 + fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n"); 1.164 + fprintf(stream, "#undef IS_BIG_ENDIAN\n\n"); 1.165 + } 1.166 +} 1.167 + 1.168 +static void do32(void) 1.169 +{ 1.170 + union { 1.171 + long i; 1.172 + char c[4]; 1.173 + } u; 1.174 + 1.175 + u.i = 0x01020304; 1.176 + if (u.c[0] == 0x01) { 1.177 + fprintf(stream, "#undef IS_LITTLE_ENDIAN\n"); 1.178 + fprintf(stream, "#define IS_BIG_ENDIAN 1\n\n"); 1.179 + } else { 1.180 + fprintf(stream, "#define IS_LITTLE_ENDIAN 1\n"); 1.181 + fprintf(stream, "#undef IS_BIG_ENDIAN\n\n"); 1.182 + } 1.183 +} 1.184 + 1.185 +/* 1.186 +** Concievably this could actually be used; but there is lots of code out 1.187 +** there with and's and shift's in it that assumes a byte is 8 bits, so 1.188 +** forget about porting THIS code to those non 8 bit byte machines. 1.189 +*/ 1.190 +static void BitsPerByte(void) 1.191 +{ 1.192 + bpb = 8; 1.193 +} 1.194 + 1.195 +int main(int argc, char **argv) 1.196 +{ 1.197 + BitsPerByte(); 1.198 + 1.199 + /* If we got a command line argument, try to use it as the stream. */ 1.200 + ++argv; 1.201 + if(*argv) { 1.202 + if(!(stream = fopen ( *argv, "wt" ))) { 1.203 + fprintf(stderr, "Could not write to output file %s.\n", *argv); 1.204 + return 1; 1.205 + } 1.206 + } else { 1.207 + stream = stdout; 1.208 + } 1.209 + 1.210 + fprintf(stream, "#ifndef nspr_cpucfg___\n"); 1.211 + fprintf(stream, "#define nspr_cpucfg___\n\n"); 1.212 + 1.213 + fprintf(stream, "/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n"); 1.214 + 1.215 + if (sizeof(long) == 8) { 1.216 + do64(); 1.217 + } else { 1.218 + do32(); 1.219 + } 1.220 + fprintf(stream, "#define PR_BYTES_PER_BYTE %d\n", sizeof(char)); 1.221 + fprintf(stream, "#define PR_BYTES_PER_SHORT %d\n", sizeof(short)); 1.222 + fprintf(stream, "#define PR_BYTES_PER_INT %d\n", sizeof(int)); 1.223 + fprintf(stream, "#define PR_BYTES_PER_INT64 %d\n", 8); 1.224 + fprintf(stream, "#define PR_BYTES_PER_LONG %d\n", sizeof(long)); 1.225 + fprintf(stream, "#define PR_BYTES_PER_FLOAT %d\n", sizeof(float)); 1.226 + fprintf(stream, "#define PR_BYTES_PER_DOUBLE %d\n\n", sizeof(double)); 1.227 + 1.228 + fprintf(stream, "#define PR_BITS_PER_BYTE %d\n", bpb); 1.229 + fprintf(stream, "#define PR_BITS_PER_SHORT %d\n", bpb * sizeof(short)); 1.230 + fprintf(stream, "#define PR_BITS_PER_INT %d\n", bpb * sizeof(int)); 1.231 + fprintf(stream, "#define PR_BITS_PER_INT64 %d\n", bpb * 8); 1.232 + fprintf(stream, "#define PR_BITS_PER_LONG %d\n", bpb * sizeof(long)); 1.233 + fprintf(stream, "#define PR_BITS_PER_FLOAT %d\n", bpb * sizeof(float)); 1.234 + fprintf(stream, "#define PR_BITS_PER_DOUBLE %d\n\n", 1.235 + bpb * sizeof(double)); 1.236 + 1.237 + fprintf(stream, "#define PR_BITS_PER_BYTE_LOG2 %d\n", Log2(bpb)); 1.238 + fprintf(stream, "#define PR_BITS_PER_SHORT_LOG2 %d\n", 1.239 + Log2(bpb * sizeof(short))); 1.240 + fprintf(stream, "#define PR_BITS_PER_INT_LOG2 %d\n", 1.241 + Log2(bpb * sizeof(int))); 1.242 + fprintf(stream, "#define PR_BITS_PER_INT64_LOG2 %d\n", 6); 1.243 + fprintf(stream, "#define PR_BITS_PER_LONG_LOG2 %d\n", 1.244 + Log2(bpb * sizeof(long))); 1.245 + fprintf(stream, "#define PR_BITS_PER_FLOAT_LOG2 %d\n", 1.246 + Log2(bpb * sizeof(float))); 1.247 + fprintf(stream, "#define PR_BITS_PER_DOUBLE_LOG2 %d\n\n", 1.248 + Log2(bpb * sizeof(double))); 1.249 + 1.250 + fprintf(stream, "#define PR_ALIGN_OF_SHORT %d\n", ALIGN_OF(short)); 1.251 + fprintf(stream, "#define PR_ALIGN_OF_INT %d\n", ALIGN_OF(int)); 1.252 + fprintf(stream, "#define PR_ALIGN_OF_LONG %d\n", ALIGN_OF(long)); 1.253 + if (sizeof(INT64) < 8) { 1.254 + /* this machine doesn't actually support PRInt64's */ 1.255 + fprintf(stream, "#define PR_ALIGN_OF_INT64 %d\n", 1.256 + ALIGN_OF(fakelonglong)); 1.257 + } else { 1.258 + fprintf(stream, "#define PR_ALIGN_OF_INT64 %d\n", ALIGN_OF(PRInt64)); 1.259 + } 1.260 + fprintf(stream, "#define PR_ALIGN_OF_FLOAT %d\n", ALIGN_OF(float)); 1.261 + fprintf(stream, "#define PR_ALIGN_OF_DOUBLE %d\n", ALIGN_OF(double)); 1.262 + fprintf(stream, "#define PR_ALIGN_OF_POINTER %d\n\n", ALIGN_OF(pointer)); 1.263 + 1.264 + fprintf(stream, "#endif /* nspr_cpucfg___ */\n"); 1.265 + fclose(stream); 1.266 + 1.267 + return 0; 1.268 +}