Commit | Line | Data |
---|---|---|
cb7a6892 MM |
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 | |
7 | the Free Software Foundation; either version 2 of the License, or | |
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 | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | ||
19 | */ | |
20 | ||
21 | ||
22 | #ifndef _ENDIAN_H_ | |
23 | #define _ENDIAN_H_ | |
24 | ||
25 | /* C byte conversion functions */ | |
26 | ||
27 | extern unsigned_1 endian_h2t_1(unsigned_1 x); | |
28 | extern unsigned_2 endian_h2t_2(unsigned_2 x); | |
29 | extern unsigned_4 endian_h2t_4(unsigned_4 x); | |
30 | extern unsigned_8 endian_h2t_8(unsigned_8 x); | |
31 | ||
32 | extern unsigned_1 endian_t2h_1(unsigned_1 x); | |
33 | extern unsigned_2 endian_t2h_2(unsigned_2 x); | |
34 | extern unsigned_4 endian_t2h_4(unsigned_4 x); | |
35 | extern unsigned_8 endian_t2h_8(unsigned_8 x); | |
36 | ||
37 | /* Host dependant: | |
38 | ||
39 | The CPP below defines information about the compilation host. In | |
40 | particular it defines the macro's: | |
41 | ||
42 | WITH_HOST_BYTE_ORDER The byte order of the host. Could | |
43 | be any of LITTLE_ENDIAN, BIG_ENDIAN | |
44 | or 0 (unknown). Those macro's also | |
45 | need to be defined. | |
46 | ||
47 | WITH_NTOH Network byte order macros defined. | |
48 | Possible value is 32 or (maybe one | |
49 | day 64 because some 64bit network | |
50 | byte order macro is defined. | |
51 | */ | |
52 | ||
53 | ||
54 | /* NetBSD: | |
55 | ||
56 | NetBSD is easy, everything you could ever want is in a header file | |
57 | (well almost :-) */ | |
58 | ||
59 | #if defined(__NetBSD__) | |
60 | # include <machine/endian.h> | |
61 | # define WITH_NTOH 32 /* what about alpha? */ | |
62 | # if (WITH_HOST_BYTE_ORDER == 0) | |
63 | # undef WITH_HOST_BYTE_ORDER | |
64 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
65 | # endif | |
66 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
67 | # error "host endian incorrectly configured, check config.h" | |
68 | # endif | |
69 | #endif | |
70 | ||
71 | /* Linux is similarly easy. */ | |
72 | ||
73 | #if defined(__linux__) | |
74 | # include <endian.h> | |
75 | # include <asm/byteorder.h> | |
76 | # if defined(__LITTLE_ENDIAN) && !defined(LITTLE_ENDIAN) | |
77 | # define LITTLE_ENDIAN __LITTLE_ENDIAN | |
78 | # endif | |
79 | # if defined(__BIG_ENDIAN) && !defined(BIG_ENDIAN) | |
80 | # define BIG_ENDIAN __BIG_ENDIAN | |
81 | # endif | |
82 | # if defined(__BYTE_ORDER) && !defined(BYTE_ORDER) | |
83 | # define BYTE_ORDER __BYTE_ORDER | |
84 | # endif | |
85 | # if !defined(__alpha__) | |
86 | # define WITH_NTOH 32 /* what about alpha? */ | |
87 | # endif | |
88 | # if (WITH_HOST_BYTE_ORDER == 0) | |
89 | # undef WITH_HOST_BYTE_ORDER | |
90 | # define WITH_HOST_BYTE_ORDER BYTE_ORDER | |
91 | # endif | |
92 | # if (BYTE_ORDER != WITH_HOST_BYTE_ORDER) | |
93 | # error "host endian incorrectly configured, check config.h" | |
94 | # endif | |
95 | #endif | |
96 | ||
97 | /* INSERT HERE - hosts that have available LITTLE_ENDIAN and | |
98 | BIG_ENDIAN macro's */ | |
99 | ||
100 | ||
101 | /* Some hosts don't define LITTLE_ENDIAN or BIG_ENDIAN, help them out */ | |
102 | ||
103 | #ifndef LITTLE_ENDIAN | |
104 | #define LITTLE_ENDIAN 1234 | |
105 | #endif | |
106 | #ifndef BIG_ENDIAN | |
107 | #define BIG_ENDIAN 4321 | |
108 | #endif | |
109 | ||
110 | ||
111 | /* SunOS on SPARC: | |
112 | ||
113 | Big endian last time I looked */ | |
114 | ||
115 | #if defined(sparc) || defined(__sparc__) | |
116 | # if (WITH_HOST_BYTE_ORDER == 0) | |
117 | # undef WITH_HOST_BYTE_ORDER | |
118 | # define WITH_HOST_BYTE_ORDER BIG_ENDIAN | |
119 | # endif | |
120 | # if (WITH_HOST_BYTE_ORDER != BIG_ENDIAN) | |
121 | # error "sun was big endian last time I looked ..." | |
122 | # endif | |
123 | #endif | |
124 | ||
125 | ||
126 | /* Random x86 | |
127 | ||
128 | Little endian last time I looked */ | |
129 | ||
130 | #if defined(i386) || defined(i486) || defined(i586) || defined(__i386__) || defined(__i486__) || defined(__i586__) | |
131 | # if (WITH_HOST_BYTE_ORDER == 0) | |
132 | # undef WITH_HOST_BYTE_ORDER | |
133 | # define WITH_HOST_BYTE_ORDER LITTLE_ENDIAN | |
134 | # endif | |
135 | # if (WITH_HOST_BYTE_ORDER != LITTLE_ENDIAN) | |
136 | # error "x86 was little endian last time I looked ..." | |
137 | # endif | |
138 | #endif | |
139 | ||
140 | ||
141 | /* INSERT HERE - additional hosts that do not have LITTLE_ENDIAN and | |
142 | BIG_ENDIAN definitions available. */ | |
143 | ||
144 | ||
145 | /* SWAPPING: | |
146 | ||
147 | According to the following table: | |
148 | ||
149 | TARG BE TARG LE TARG ?? | |
150 | HOST BE ok s/w s/w | |
151 | HOST LE htohl ok ok|ntohl | |
152 | HOST ?? ntohl s/w s/w | |
153 | ||
154 | define host <-> target byte order conversion macro's */ | |
155 | ||
156 | ||
157 | /* IN PLACE: | |
158 | ||
159 | These macro's given a variable argument swap its value in place if | |
160 | so required */ | |
161 | ||
162 | #define H2T(VARIABLE) \ | |
163 | do { \ | |
164 | switch (sizeof(VARIABLE)) { \ | |
165 | case 1: VARIABLE = H2T_1(VARIABLE); break; \ | |
166 | case 2: VARIABLE = H2T_2(VARIABLE); break; \ | |
167 | case 4: VARIABLE = H2T_4(VARIABLE); break; \ | |
168 | case 8: VARIABLE = H2T_8(VARIABLE); break; \ | |
169 | } \ | |
170 | } while (0) | |
171 | ||
172 | #define T2H(VARIABLE) \ | |
173 | do { \ | |
174 | switch (sizeof(VARIABLE)) { \ | |
175 | case 1: VARIABLE = T2H_1(VARIABLE); break; \ | |
176 | case 2: VARIABLE = T2H_2(VARIABLE); break; \ | |
177 | case 4: VARIABLE = T2H_4(VARIABLE); break; \ | |
178 | case 8: VARIABLE = T2H_8(VARIABLE); break; \ | |
179 | } \ | |
180 | } while (0) | |
181 | ||
182 | ||
183 | /* TARGET WORD: | |
184 | ||
185 | Byte swap a quantity the size of the targets word */ | |
186 | ||
187 | #if WITH_64BIT_TARGET | |
188 | #define H2T_word(X) H2T_8(X) | |
189 | #define T2H_word(X) T2H_8(X) | |
190 | #else | |
191 | #define H2T_word(X) H2T_4(X) | |
192 | #define T2H_word(X) T2H_4(X) | |
193 | #endif | |
194 | ||
195 | ||
196 | /* FUNCTIONS: | |
197 | ||
198 | Returns the value swapped according to the host/target byte order */ | |
199 | ||
200 | /* no need to swap */ | |
201 | #if 0 | |
202 | #if (WITH_HOST_BYTE_ORDER \ | |
203 | && WITH_TARGET_BYTE_ORDER \ | |
204 | && WITH_HOST_BYTE_ORDER == WITH_TARGET_BYTE_ORDER ) | |
205 | #define H2T_1(X) (X) | |
206 | #define H2T_2(X) (X) | |
207 | #define H2T_4(X) (X) | |
208 | #define H2T_8(X) (X) | |
209 | #define T2H_1(X) (X) | |
210 | #define T2H_2(X) (X) | |
211 | #define T2H_4(X) (X) | |
212 | #define T2H_8(X) (X) | |
213 | #endif | |
214 | ||
215 | /* have ntoh and big endian target */ | |
216 | #if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN \ | |
217 | && WITH_HOST_BYTE_ORDER != BIG_ENDIAN \ | |
218 | && WITH_NTOH) | |
219 | #define H2T_8(X) endian_h2t_8(X) | |
220 | #define H2T_4(X) htonl(X) | |
221 | #define H2T_2(X) htons(X) | |
222 | #define H2T_1(X) (X) | |
223 | #define T2H_8(X) endian_t2h_8(X) | |
224 | #define T2H_4(X) htonl(X) | |
225 | #define T2H_2(X) htons(X) | |
226 | #define T2H_1(X) (X) | |
227 | #endif | |
228 | ||
229 | /* have ntoh, little host and unknown target */ | |
230 | #if (WITH_HOST_BYTE_ORDER == LITTLE_ENDIAN \ | |
231 | && WITH_TARGET_BYTE_ORDER == 0 \ | |
232 | && WITH_NTOH) | |
233 | #define H2T_8(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : endian_h2t_8(X)) | |
234 | #define H2T_4(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : htonl(X)) | |
235 | #define H2T_2(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : htons(X)) | |
236 | #define H2T_1(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : (X)) | |
237 | #define T2H_8(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : endian_t2h_8(X)) | |
238 | #define T2H_4(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : htonl(X)) | |
239 | #define T2H_2(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : htons(X)) | |
240 | #define T2H_1(X) (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER ? (X) : (X)) | |
241 | #endif | |
242 | #endif | |
243 | ||
244 | /* if all else fails use software */ | |
245 | #ifndef H2T_1 | |
246 | #define H2T_1(X) (X) | |
247 | #define H2T_2(X) endian_h2t_2(X) | |
248 | #define H2T_4(X) endian_h2t_4(X) | |
249 | #define H2T_8(X) endian_h2t_8(X) | |
250 | #define T2H_1(X) (X) | |
251 | #define T2H_2(X) endian_t2h_2(X) | |
252 | #define T2H_4(X) endian_t2h_4(X) | |
253 | #define T2H_8(X) endian_t2h_8(X) | |
254 | #endif | |
255 | ||
256 | #endif |