Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* This file is part of the program psim. |
2 | ||
3 | Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au> | |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
3fd725ef | 7 | the Free Software Foundation; either version 3 of the License, or |
c906108c SS |
8 | (at your option) any later version. |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
51b318de | 16 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
c906108c SS |
17 | |
18 | */ | |
19 | ||
20 | ||
21 | #ifndef _SIM_ENDIAN_H_ | |
22 | #define _SIM_ENDIAN_H_ | |
23 | ||
24 | ||
25 | /* C byte conversion functions */ | |
26 | ||
d29d5195 MG |
27 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x); |
28 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x); | |
29 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x); | |
30 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x); | |
31 | ||
32 | INLINE_PSIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x); | |
33 | INLINE_PSIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x); | |
34 | INLINE_PSIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x); | |
35 | INLINE_PSIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x); | |
36 | ||
37 | INLINE_PSIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x); | |
38 | INLINE_PSIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x); | |
39 | INLINE_PSIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x); | |
40 | INLINE_PSIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x); | |
41 | ||
42 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x); | |
43 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x); | |
44 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x); | |
45 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x); | |
46 | ||
47 | INLINE_PSIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x); | |
48 | INLINE_PSIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x); | |
49 | INLINE_PSIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x); | |
50 | INLINE_PSIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x); | |
51 | ||
52 | INLINE_PSIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x); | |
53 | INLINE_PSIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x); | |
54 | INLINE_PSIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x); | |
55 | INLINE_PSIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x); | |
56 | ||
57 | INLINE_PSIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x); | |
58 | INLINE_PSIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x); | |
59 | INLINE_PSIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x); | |
60 | INLINE_PSIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x); | |
c906108c SS |
61 | |
62 | ||
63 | /* Host dependant: | |
64 | ||
65 | The CPP below defines information about the compilation host. In | |
66 | particular it defines the macro's: | |
67 | ||
68 | WITH_HOST_BYTE_ORDER The byte order of the host. Could | |
69 | be any of LITTLE_ENDIAN, BIG_ENDIAN | |
70 | or 0 (unknown). Those macro's also | |
71 | need to be defined. | |
72 | ||
73 | */ | |
74 | ||
75 | ||
76 | /* NetBSD: | |
77 | ||
78 | NetBSD is easy, everything you could ever want is in a header file | |
79 | (well almost :-) */ | |
80 | ||
81 | #if defined(__NetBSD__) | |
82 | # include <machine/endian.h> | |
83 | # if (WITH_HOST_BYTE_ORDER == 0) | |
84 | # undef WITH_HOST_BYTE_ORDER | |
85 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
86 | # endif | |
87 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
88 | # error "host endian incorrectly configured, check config.h" | |
89 | # endif | |
90 | #endif | |
91 | ||
92 | /* Linux is similarly easy. */ | |
93 | ||
94 | #if defined(__linux__) | |
95 | # include <endian.h> | |
c906108c SS |
96 | # if defined(__LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN) |
97 | # define LITTLE_ENDIAN __LITTLE_ENDIAN | |
98 | # endif | |
99 | # if defined(__BIG_ENDIAN) && !defined(BIG_ENDIAN) | |
100 | # define BIG_ENDIAN __BIG_ENDIAN | |
101 | # endif | |
102 | # if defined(__BYTE_ORDER) && !defined(BYTE_ORDER) | |
103 | # define BYTE_ORDER __BYTE_ORDER | |
104 | # endif | |
105 | # if (WITH_HOST_BYTE_ORDER == 0) | |
106 | # undef WITH_HOST_BYTE_ORDER | |
107 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
108 | # endif | |
109 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
110 | # error "host endian incorrectly configured, check config.h" | |
111 | # endif | |
112 | #endif | |
113 | ||
114 | /* INSERT HERE - hosts that have available LITTLE_ENDIAN and | |
115 | BIG_ENDIAN macro's */ | |
116 | ||
117 | ||
118 | /* Some hosts don't define LITTLE_ENDIAN or BIG_ENDIAN, help them out */ | |
119 | ||
120 | #ifndef LITTLE_ENDIAN | |
121 | #define LITTLE_ENDIAN 1234 | |
122 | #endif | |
123 | #ifndef BIG_ENDIAN | |
124 | #define BIG_ENDIAN 4321 | |
125 | #endif | |
126 | ||
127 | ||
128 | /* SunOS on SPARC: | |
129 | ||
130 | Big endian last time I looked */ | |
131 | ||
132 | #if defined(sparc) || defined(__sparc__) | |
133 | # if (WITH_HOST_BYTE_ORDER == 0) | |
134 | # undef WITH_HOST_BYTE_ORDER | |
135 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
136 | # endif | |
137 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
138 | # error "sun was big endian last time I looked ..." | |
139 | # endif | |
140 | #endif | |
141 | ||
142 | ||
143 | /* Random x86 | |
144 | ||
145 | Little endian last time I looked */ | |
146 | ||
147 | #if defined(i386) || defined(i486) || defined(i586) || defined (i686) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined (__i686__) | |
148 | # if (WITH_HOST_BYTE_ORDER == 0) | |
149 | # undef WITH_HOST_BYTE_ORDER | |
150 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
151 | # endif | |
152 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
153 | # error "x86 was little endian last time I looked ..." | |
154 | # endif | |
155 | #endif | |
156 | ||
157 | #if (defined (__i486__) || defined (__i586__) || defined (__i686__)) && defined(__GNUC__) && WITH_BSWAP | |
158 | #undef htonl | |
159 | #undef ntohl | |
160 | #define htonl(IN) __extension__ ({ int _out; __asm__ ("bswap %0" : "=r" (_out) : "0" (IN)); _out; }) | |
161 | #define ntohl(IN) __extension__ ({ int _out; __asm__ ("bswap %0" : "=r" (_out) : "0" (IN)); _out; }) | |
162 | #endif | |
163 | ||
164 | /* Power or PowerPC running AIX */ | |
165 | #if defined(_POWER) && defined(_AIX) | |
166 | # if (WITH_HOST_BYTE_ORDER == 0) | |
167 | # undef WITH_HOST_BYTE_ORDER | |
168 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
169 | # endif | |
170 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
171 | # error "Power/PowerPC AIX was big endian last time I looked ..." | |
172 | # endif | |
173 | #endif | |
174 | ||
175 | /* Solaris running PowerPC */ | |
176 | #if defined(__PPC) && defined(__sun__) | |
177 | # if (WITH_HOST_BYTE_ORDER == 0) | |
178 | # undef WITH_HOST_BYTE_ORDER | |
179 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
180 | # endif | |
181 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
182 | # error "Solaris on PowerPCs was little endian last time I looked ..." | |
183 | # endif | |
184 | #endif | |
185 | ||
186 | /* HP/PA */ | |
187 | #if defined(__hppa__) | |
188 | # if (WITH_HOST_BYTE_ORDER == 0) | |
189 | # undef WITH_HOST_BYTE_ORDER | |
190 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
191 | # endif | |
192 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
193 | # error "HP/PA was big endian last time I looked ..." | |
194 | # endif | |
195 | #endif | |
196 | ||
197 | /* Big endian MIPS */ | |
198 | #if defined(__MIPSEB__) | |
199 | # if (WITH_HOST_BYTE_ORDER == 0) | |
200 | # undef WITH_HOST_BYTE_ORDER | |
201 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
202 | # endif | |
203 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
204 | # error "MIPSEB was big endian last time I looked ..." | |
205 | # endif | |
206 | #endif | |
207 | ||
208 | /* Little endian MIPS */ | |
209 | #if defined(__MIPSEL__) | |
210 | # if (WITH_HOST_BYTE_ORDER == 0) | |
211 | # undef WITH_HOST_BYTE_ORDER | |
212 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
213 | # endif | |
214 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
215 | # error "MIPSEL was little endian last time I looked ..." | |
216 | # endif | |
217 | #endif | |
218 | ||
219 | /* Windows NT */ | |
220 | #if defined(__WIN32__) | |
221 | # if (WITH_HOST_BYTE_ORDER == 0) | |
222 | # undef WITH_HOST_BYTE_ORDER | |
223 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
224 | # endif | |
225 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
226 | # error "Windows NT was little endian last time I looked ..." | |
227 | # endif | |
228 | #endif | |
229 | ||
230 | /* Alpha running DEC unix */ | |
231 | #if defined(__osf__) && defined(__alpha__) | |
232 | # if (WITH_HOST_BYTE_ORDER == 0) | |
233 | # undef WITH_HOST_BYTE_ORDER | |
234 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
235 | # endif | |
236 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
237 | # error "AXP running DEC unix was little endian last time I looked ..." | |
238 | # endif | |
239 | #endif | |
240 | ||
241 | ||
242 | /* INSERT HERE - additional hosts that do not have LITTLE_ENDIAN and | |
243 | BIG_ENDIAN definitions available. */ | |
244 | ||
245 | /* SWAP */ | |
246 | ||
4c15ccf7 GK |
247 | #define SWAP_1 swap_1 |
248 | #define SWAP_2 swap_2 | |
249 | #define SWAP_4 swap_4 | |
250 | #define SWAP_8 swap_8 | |
c906108c SS |
251 | |
252 | ||
253 | /* HOST to BE */ | |
254 | ||
4c15ccf7 GK |
255 | #define H2BE_1 endian_h2be_1 |
256 | #define H2BE_2 endian_h2be_2 | |
257 | #define H2BE_4 endian_h2be_4 | |
258 | #define H2BE_8 endian_h2be_8 | |
259 | #define BE2H_1 endian_be2h_1 | |
260 | #define BE2H_2 endian_be2h_2 | |
261 | #define BE2H_4 endian_be2h_4 | |
262 | #define BE2H_8 endian_be2h_8 | |
c906108c SS |
263 | |
264 | ||
265 | /* HOST to LE */ | |
266 | ||
4c15ccf7 GK |
267 | #define H2LE_1 endian_h2le_1 |
268 | #define H2LE_2 endian_h2le_2 | |
269 | #define H2LE_4 endian_h2le_4 | |
270 | #define H2LE_8 endian_h2le_8 | |
271 | #define LE2H_1 endian_le2h_1 | |
272 | #define LE2H_2 endian_le2h_2 | |
273 | #define LE2H_4 endian_le2h_4 | |
274 | #define LE2H_8 endian_le2h_8 | |
c906108c SS |
275 | |
276 | ||
277 | /* HOST to TARGET */ | |
278 | ||
4c15ccf7 GK |
279 | #define H2T_1 endian_h2t_1 |
280 | #define H2T_2 endian_h2t_2 | |
281 | #define H2T_4 endian_h2t_4 | |
282 | #define H2T_8 endian_h2t_8 | |
283 | #define T2H_1 endian_t2h_1 | |
284 | #define T2H_2 endian_t2h_2 | |
285 | #define T2H_4 endian_t2h_4 | |
286 | #define T2H_8 endian_t2h_8 | |
c906108c SS |
287 | |
288 | ||
289 | /* CONVERT IN PLACE | |
290 | ||
291 | These macros, given an argument of unknown size, swap its value in | |
292 | place if a host/target conversion is required. */ | |
293 | ||
294 | #define H2T(VARIABLE) \ | |
295 | do { \ | |
296 | switch (sizeof(VARIABLE)) { \ | |
297 | case 1: VARIABLE = H2T_1(VARIABLE); break; \ | |
298 | case 2: VARIABLE = H2T_2(VARIABLE); break; \ | |
299 | case 4: VARIABLE = H2T_4(VARIABLE); break; \ | |
300 | case 8: VARIABLE = H2T_8(VARIABLE); break; \ | |
301 | } \ | |
302 | } while (0) | |
303 | ||
304 | #define T2H(VARIABLE) \ | |
305 | do { \ | |
306 | switch (sizeof(VARIABLE)) { \ | |
307 | case 1: VARIABLE = T2H_1(VARIABLE); break; \ | |
308 | case 2: VARIABLE = T2H_2(VARIABLE); break; \ | |
309 | case 4: VARIABLE = T2H_4(VARIABLE); break; \ | |
310 | case 8: VARIABLE = T2H_8(VARIABLE); break; \ | |
311 | } \ | |
312 | } while (0) | |
313 | ||
314 | #define SWAP(VARIABLE) \ | |
315 | do { \ | |
316 | switch (sizeof(VARIABLE)) { \ | |
317 | case 1: VARIABLE = SWAP_1(VARIABLE); break; \ | |
318 | case 2: VARIABLE = SWAP_2(VARIABLE); break; \ | |
319 | case 4: VARIABLE = SWAP_4(VARIABLE); break; \ | |
320 | case 8: VARIABLE = SWAP_8(VARIABLE); break; \ | |
321 | } \ | |
322 | } while (0) | |
323 | ||
324 | #define H2BE(VARIABLE) \ | |
325 | do { \ | |
326 | switch (sizeof(VARIABLE)) { \ | |
327 | case 1: VARIABLE = H2BE_1(VARIABLE); break; \ | |
328 | case 2: VARIABLE = H2BE_2(VARIABLE); break; \ | |
329 | case 4: VARIABLE = H2BE_4(VARIABLE); break; \ | |
330 | case 8: VARIABLE = H2BE_8(VARIABLE); break; \ | |
331 | } \ | |
332 | } while (0) | |
333 | ||
334 | #define BE2H(VARIABLE) \ | |
335 | do { \ | |
336 | switch (sizeof(VARIABLE)) { \ | |
337 | case 1: VARIABLE = BE2H_1(VARIABLE); break; \ | |
338 | case 2: VARIABLE = BE2H_2(VARIABLE); break; \ | |
339 | case 4: VARIABLE = BE2H_4(VARIABLE); break; \ | |
340 | case 8: VARIABLE = BE2H_8(VARIABLE); break; \ | |
341 | } \ | |
342 | } while (0) | |
343 | ||
344 | #define H2LE(VARIABLE) \ | |
345 | do { \ | |
346 | switch (sizeof(VARIABLE)) { \ | |
347 | case 1: VARIABLE = H2LE_1(VARIABLE); break; \ | |
348 | case 2: VARIABLE = H2LE_2(VARIABLE); break; \ | |
349 | case 4: VARIABLE = H2LE_4(VARIABLE); break; \ | |
350 | case 8: VARIABLE = H2LE_8(VARIABLE); break; \ | |
351 | } \ | |
352 | } while (0) | |
353 | ||
354 | #define LE2H(VARIABLE) \ | |
355 | do { \ | |
356 | switch (sizeof(VARIABLE)) { \ | |
357 | case 1: VARIABLE = LE2H_1(VARIABLE); break; \ | |
358 | case 2: VARIABLE = LE2H_2(VARIABLE); break; \ | |
359 | case 4: VARIABLE = LE2H_4(VARIABLE); break; \ | |
360 | case 8: VARIABLE = LE2H_8(VARIABLE); break; \ | |
361 | } \ | |
362 | } while (0) | |
363 | ||
364 | ||
365 | ||
366 | /* TARGET WORD: | |
367 | ||
368 | Byte swap a quantity the size of the targets word */ | |
369 | ||
370 | #if (WITH_TARGET_WORD_BITSIZE == 64) | |
4c15ccf7 GK |
371 | #define H2T_word H2T_8 |
372 | #define T2H_word T2H_8 | |
373 | #define H2BE_word H2BE_8 | |
374 | #define BE2H_word BE2H_8 | |
375 | #define H2LE_word H2LE_8 | |
376 | #define LE2H_word LE2H_8 | |
377 | #define SWAP_word SWAP_8 | |
c906108c SS |
378 | #endif |
379 | #if (WITH_TARGET_WORD_BITSIZE == 32) | |
4c15ccf7 GK |
380 | #define H2T_word H2T_4 |
381 | #define T2H_word T2H_4 | |
382 | #define H2BE_word H2BE_4 | |
383 | #define BE2H_word BE2H_4 | |
384 | #define H2LE_word H2LE_4 | |
385 | #define LE2H_word LE2H_4 | |
386 | #define SWAP_word SWAP_4 | |
c906108c SS |
387 | #endif |
388 | ||
389 | ||
390 | /* TARGET CELL: | |
391 | ||
392 | Byte swap a quantity the size of the targets IEEE 1275 memory cell */ | |
393 | ||
4c15ccf7 GK |
394 | #define H2T_cell H2T_4 |
395 | #define T2H_cell T2H_4 | |
396 | #define H2BE_cell H2BE_4 | |
397 | #define BE2H_cell BE2H_4 | |
398 | #define H2LE_cell H2LE_4 | |
399 | #define LE2H_cell LE2H_4 | |
400 | #define SWAP_cell SWAP_4 | |
c906108c SS |
401 | |
402 | ||
403 | #if (SIM_ENDIAN_INLINE & INCLUDE_MODULE) | |
404 | # include "sim-endian.c" | |
405 | #endif | |
406 | ||
407 | #endif /* _SIM_ENDIAN_H_ */ |