nsprpub/pr/include/gencfg.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

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

mercurial