Debian Codegraph @

nsprpub/pr/include/gencfg.c

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