Add MSBIT* and LSBIT* macro's to sim-bits.h
[deliverable/binutils-gdb.git] / sim / common / sim-endian.h
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1997, Free Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
20 */
21
22
23 #ifndef _SIM_ENDIAN_H_
24 #define _SIM_ENDIAN_H_
25
26
27 /* C byte conversion functions */
28
29 INLINE_SIM_ENDIAN(unsigned_1) endian_h2t_1(unsigned_1 x);
30 INLINE_SIM_ENDIAN(unsigned_2) endian_h2t_2(unsigned_2 x);
31 INLINE_SIM_ENDIAN(unsigned_4) endian_h2t_4(unsigned_4 x);
32 INLINE_SIM_ENDIAN(unsigned_8) endian_h2t_8(unsigned_8 x);
33
34 INLINE_SIM_ENDIAN(unsigned_1) endian_t2h_1(unsigned_1 x);
35 INLINE_SIM_ENDIAN(unsigned_2) endian_t2h_2(unsigned_2 x);
36 INLINE_SIM_ENDIAN(unsigned_4) endian_t2h_4(unsigned_4 x);
37 INLINE_SIM_ENDIAN(unsigned_8) endian_t2h_8(unsigned_8 x);
38
39 INLINE_SIM_ENDIAN(unsigned_1) swap_1(unsigned_1 x);
40 INLINE_SIM_ENDIAN(unsigned_2) swap_2(unsigned_2 x);
41 INLINE_SIM_ENDIAN(unsigned_4) swap_4(unsigned_4 x);
42 INLINE_SIM_ENDIAN(unsigned_8) swap_8(unsigned_8 x);
43
44 INLINE_SIM_ENDIAN(unsigned_1) endian_h2be_1(unsigned_1 x);
45 INLINE_SIM_ENDIAN(unsigned_2) endian_h2be_2(unsigned_2 x);
46 INLINE_SIM_ENDIAN(unsigned_4) endian_h2be_4(unsigned_4 x);
47 INLINE_SIM_ENDIAN(unsigned_8) endian_h2be_8(unsigned_8 x);
48
49 INLINE_SIM_ENDIAN(unsigned_1) endian_be2h_1(unsigned_1 x);
50 INLINE_SIM_ENDIAN(unsigned_2) endian_be2h_2(unsigned_2 x);
51 INLINE_SIM_ENDIAN(unsigned_4) endian_be2h_4(unsigned_4 x);
52 INLINE_SIM_ENDIAN(unsigned_8) endian_be2h_8(unsigned_8 x);
53
54 INLINE_SIM_ENDIAN(unsigned_1) endian_h2le_1(unsigned_1 x);
55 INLINE_SIM_ENDIAN(unsigned_2) endian_h2le_2(unsigned_2 x);
56 INLINE_SIM_ENDIAN(unsigned_4) endian_h2le_4(unsigned_4 x);
57 INLINE_SIM_ENDIAN(unsigned_8) endian_h2le_8(unsigned_8 x);
58
59 INLINE_SIM_ENDIAN(unsigned_1) endian_le2h_1(unsigned_1 x);
60 INLINE_SIM_ENDIAN(unsigned_2) endian_le2h_2(unsigned_2 x);
61 INLINE_SIM_ENDIAN(unsigned_4) endian_le2h_4(unsigned_4 x);
62 INLINE_SIM_ENDIAN(unsigned_8) endian_le2h_8(unsigned_8 x);
63
64 INLINE_SIM_ENDIAN(void*) offset_1(unsigned_1 *x, int ws, int w);
65 INLINE_SIM_ENDIAN(void*) offset_2(unsigned_2 *x, int ws, int w);
66 INLINE_SIM_ENDIAN(void*) offset_4(unsigned_4 *x, int ws, int w);
67 INLINE_SIM_ENDIAN(void*) offset_8(unsigned_8 *x, int ws, int w);
68
69
70 /* SWAP */
71
72 #define SWAP_1(X) swap_1(X)
73 #define SWAP_2(X) swap_2(X)
74 #define SWAP_4(X) swap_4(X)
75 #define SWAP_8(X) swap_8(X)
76
77
78 /* HOST to BE */
79
80 #define H2BE_1(X) endian_h2be_1(X)
81 #define H2BE_2(X) endian_h2be_2(X)
82 #define H2BE_4(X) endian_h2be_4(X)
83 #define H2BE_8(X) endian_h2be_8(X)
84 #define BE2H_1(X) endian_be2h_1(X)
85 #define BE2H_2(X) endian_be2h_2(X)
86 #define BE2H_4(X) endian_be2h_4(X)
87 #define BE2H_8(X) endian_be2h_8(X)
88
89
90 /* HOST to LE */
91
92 #define H2LE_1(X) endian_h2le_1(X)
93 #define H2LE_2(X) endian_h2le_2(X)
94 #define H2LE_4(X) endian_h2le_4(X)
95 #define H2LE_8(X) endian_h2le_8(X)
96 #define LE2H_1(X) endian_le2h_1(X)
97 #define LE2H_2(X) endian_le2h_2(X)
98 #define LE2H_4(X) endian_le2h_4(X)
99 #define LE2H_8(X) endian_le2h_8(X)
100
101
102 /* HOST to TARGET */
103
104 #define H2T_1(X) endian_h2t_1(X)
105 #define H2T_2(X) endian_h2t_2(X)
106 #define H2T_4(X) endian_h2t_4(X)
107 #define H2T_8(X) endian_h2t_8(X)
108 #define T2H_1(X) endian_t2h_1(X)
109 #define T2H_2(X) endian_t2h_2(X)
110 #define T2H_4(X) endian_t2h_4(X)
111 #define T2H_8(X) endian_t2h_8(X)
112
113
114 /* CONVERT IN PLACE
115
116 These macros, given an argument of unknown size, swap its value in
117 place if a host/target conversion is required. */
118
119 #define H2T(VARIABLE) \
120 do { \
121 switch (sizeof(VARIABLE)) { \
122 case 1: VARIABLE = H2T_1(VARIABLE); break; \
123 case 2: VARIABLE = H2T_2(VARIABLE); break; \
124 case 4: VARIABLE = H2T_4(VARIABLE); break; \
125 case 8: VARIABLE = H2T_8(VARIABLE); break; \
126 } \
127 } while (0)
128
129 #define T2H(VARIABLE) \
130 do { \
131 switch (sizeof(VARIABLE)) { \
132 case 1: VARIABLE = T2H_1(VARIABLE); break; \
133 case 2: VARIABLE = T2H_2(VARIABLE); break; \
134 case 4: VARIABLE = T2H_4(VARIABLE); break; \
135 case 8: VARIABLE = T2H_8(VARIABLE); break; \
136 } \
137 } while (0)
138
139 #define SWAP(VARIABLE) \
140 do { \
141 switch (sizeof(VARIABLE)) { \
142 case 1: VARIABLE = SWAP_1(VARIABLE); break; \
143 case 2: VARIABLE = SWAP_2(VARIABLE); break; \
144 case 4: VARIABLE = SWAP_4(VARIABLE); break; \
145 case 8: VARIABLE = SWAP_8(VARIABLE); break; \
146 } \
147 } while (0)
148
149 #define H2BE(VARIABLE) \
150 do { \
151 switch (sizeof(VARIABLE)) { \
152 case 1: VARIABLE = H2BE_1(VARIABLE); break; \
153 case 2: VARIABLE = H2BE_2(VARIABLE); break; \
154 case 4: VARIABLE = H2BE_4(VARIABLE); break; \
155 case 8: VARIABLE = H2BE_8(VARIABLE); break; \
156 } \
157 } while (0)
158
159 #define BE2H(VARIABLE) \
160 do { \
161 switch (sizeof(VARIABLE)) { \
162 case 1: VARIABLE = BE2H_1(VARIABLE); break; \
163 case 2: VARIABLE = BE2H_2(VARIABLE); break; \
164 case 4: VARIABLE = BE2H_4(VARIABLE); break; \
165 case 8: VARIABLE = BE2H_8(VARIABLE); break; \
166 } \
167 } while (0)
168
169 #define H2LE(VARIABLE) \
170 do { \
171 switch (sizeof(VARIABLE)) { \
172 case 1: VARIABLE = H2LE_1(VARIABLE); break; \
173 case 2: VARIABLE = H2LE_2(VARIABLE); break; \
174 case 4: VARIABLE = H2LE_4(VARIABLE); break; \
175 case 8: VARIABLE = H2LE_8(VARIABLE); break; \
176 } \
177 } while (0)
178
179 #define LE2H(VARIABLE) \
180 do { \
181 switch (sizeof(VARIABLE)) { \
182 case 1: VARIABLE = LE2H_1(VARIABLE); break; \
183 case 2: VARIABLE = LE2H_2(VARIABLE); break; \
184 case 4: VARIABLE = LE2H_4(VARIABLE); break; \
185 case 8: VARIABLE = LE2H_8(VARIABLE); break; \
186 } \
187 } while (0)
188
189
190
191 /* TARGET WORD:
192
193 Byte swap a quantity the size of the targets word */
194
195 #if (WITH_TARGET_WORD_BITSIZE == 64)
196 #define H2T_word(X) H2T_8(X)
197 #define T2H_word(X) T2H_8(X)
198 #define H2BE_word(X) H2BE_8(X)
199 #define BE2H_word(X) BE2H_8(X)
200 #define H2LE_word(X) H2LE_8(X)
201 #define LE2H_word(X) LE2H_8(X)
202 #define SWAP_word(X) SWAP_8(X)
203 #endif
204 #if (WITH_TARGET_WORD_BITSIZE == 32)
205 #define H2T_word(X) H2T_4(X)
206 #define T2H_word(X) T2H_4(X)
207 #define H2BE_word(X) H2BE_4(X)
208 #define BE2H_word(X) BE2H_4(X)
209 #define H2LE_word(X) H2LE_4(X)
210 #define LE2H_word(X) LE2H_4(X)
211 #define SWAP_word(X) SWAP_4(X)
212 #endif
213
214
215
216 /* TARGET CELL:
217
218 Byte swap a quantity the size of the targets IEEE 1275 memory cell */
219
220 #define H2T_cell(X) H2T_4(X)
221 #define T2H_cell(X) T2H_4(X)
222 #define H2BE_cell(X) H2BE_4(X)
223 #define BE2H_cell(X) BE2H_4(X)
224 #define H2LE_cell(X) H2LE_4(X)
225 #define LE2H_cell(X) LE2H_4(X)
226 #define SWAP_cell(X) SWAP_4(X)
227
228
229
230 /* HOST Offsets:
231
232 Address of high/low sub-word within a host word quantity.
233
234 Address of sub-word N within a host word quantity. NOTE: Numbering
235 is BIG endian always. */
236
237 #define AH1_2(X) (unsigned_1*)offset_2((X), 1, 0)
238 #define AL1_2(X) (unsigned_1*)offset_2((X), 1, 1)
239
240 #define AH2_4(X) (unsigned_2*)offset_4((X), 2, 0)
241 #define AL2_4(X) (unsigned_2*)offset_4((X), 2, 1)
242
243 #define AH4_8(X) (unsigned_4*)offset_8((X), 4, 0)
244 #define AL4_8(X) (unsigned_4*)offset_8((X), 4, 1)
245
246 #if (WITH_TARGET_WORD_BITSIZE == 64)
247 #define AH_word(X) AH4_8(X)
248 #define AL_word(X) AL4_8(X)
249 #endif
250 #if (WITH_TARGET_WORD_BITSIZE == 32)
251 #define AH_word(X) AH2_4(X)
252 #define AL_word(X) AL2_4(X)
253 #endif
254
255
256 #define A1_2(X,N) (unsigned_1*)offset_2((X), 1, (N))
257
258 #define A1_4(X,N) (unsigned_1*)offset_4((X), 1, (N))
259 #define A2_4(X,N) (unsigned_2*)offset_4((X), 2, (N))
260
261 #define A1_8(X,N) (unsigned_1*)offset_8((X), 1, (N))
262 #define A2_8(X,N) (unsigned_2*)offset_8((X), 2, (N))
263 #define A4_8(X,N) (unsigned_4*)offset_8((X), 4, (N))
264
265
266
267
268
269 /* HOST Components:
270
271 Value of sub-word within a host word quantity */
272
273 #define VH1_2(X) ((unsigned_1)((unsigned_2)(X) >> 8))
274 #define VL1_2(X) (unsigned_1)(X)
275
276 #define VH2_4(X) ((unsigned_2)((unsigned_4)(X) >> 16))
277 #define VL2_4(X) ((unsigned_2)(X))
278
279 #define VH4_8(X) ((unsigned_4)((unsigned_8)(X) >> 32))
280 #define VL4_8(X) ((unsigned_4)(X))
281
282 #if (WITH_TARGET_WORD_BITSIZE == 64)
283 #define VH_word(X) VH4_8(X)
284 #define VL_word(X) VL4_8(X)
285 #endif
286 #if (WITH_TARGET_WORD_BITSIZE == 32)
287 #define VH_word(X) VH2_4(X)
288 #define VL_word(X) VL2_4(X)
289 #endif
290
291
292 #define V1_2(X,N) ((unsigned_1)((unsigned_2)(X) >> ( 8 * (1 - (N)))))
293 #define V1_4(X,N) ((unsigned_1)((unsigned_4)(X) >> ( 8 * (3 - (N)))))
294 #define V1_8(X,N) ((unsigned_1)((unsigned_8)(X) >> ( 8 * (7 - (N)))))
295
296 #define V2_4(X,N) ((unsigned_2)((unsigned_4)(X) >> (16 * (1 - (N)))))
297 #define V2_8(X,N) ((unsigned_2)((unsigned_8)(X) >> (16 * (3 - (N)))))
298
299 #define V4_8(X,N) ((unsigned_4)((unsigned_8)(X) >> (32 * (1 - (N)))))
300
301
302 /* Reverse - insert sub-word into word quantity */
303
304 #define V2_H1(X) ((unsigned_2)(unsigned_1)(X) << 8)
305 #define V2_L1(X) ((unsigned_2)(unsigned_1)(X))
306
307 #define V4_H2(X) ((unsigned_4)(unsigned_2)(X) << 16)
308 #define V4_L2(X) ((unsigned_4)(unsigned_2)(X))
309
310 #define V8_H4(X) ((unsigned_8)(unsigned_4)(X) << 32)
311 #define V8_L4(X) ((unsigned_8)(unsigned_4)(X))
312
313
314 #define V2_1(X,N) ((unsigned_2)(unsigned_1)(X) << ( 8 * (1 - (N))))
315 #define V4_1(X,N) ((unsigned_4)(unsigned_1)(X) << ( 8 * (3 - (N))))
316 #define V8_1(X,N) ((unsigned_8)(unsigned_1)(X) << ( 8 * (7 - (N))))
317
318 #define V4_2(X,N) ((unsigned_4)(unsigned_2)(X) << (16 * (1 - (N))))
319 #define V8_2(X,N) ((unsigned_8)(unsigned_2)(X) << (16 * (3 - (N))))
320
321 #define V8_4(X,N) ((unsigned_8)(unsigned_4)(X) << (32 * (1 - (N))))
322
323
324 /* Reverse - insert N sub-words into single word quantity */
325
326 #define U2_1(I0,I1) (V2_1(I0,0) | V2_1(I1,1))
327 #define U4_1(I0,I1,I2,I3) (V4_1(I0,0) | V4_1(I1,1) | V4_1(I2,2) | V4_1(I3,3))
328 #define U8_1(I0,I1,I2,I3,I4,I5,I6,I7) \
329 (V8_1(I0,0) | V8_1(I1,1) | V8_1(I2,2) | V8_1(I3,3) \
330 | V8_1(I4,4) | V8_1(I5,5) | V8_1(I6,6) | V8_1(I7,7))
331
332 #define U4_2(I0,I1) (V4_2(I0,0) | V4_2(I1,1))
333 #define U8_2(I0,I1,I2,I3) (V8_2(I0,0) | V8_2(I1,1) | V8_2(I2,2) | V8_2(I3,3))
334
335 #define U8_4(I0,I1) (V8_4(I0,0) | V8_4(I1,1))
336
337
338 #if (WITH_TARGET_WORD_BITSIZE == 64)
339 #define Vword_H(X) V8_H4(X)
340 #define Vword_L(X) V8_L4(X)
341 #endif
342 #if (WITH_TARGET_WORD_BITSIZE == 32)
343 #define Vword_H(X) V4_H2(X)
344 #define Vword_L(X) V4_L2(X)
345 #endif
346
347
348
349
350
351
352 #if (SIM_ENDIAN_INLINE & INCLUDE_MODULE)
353 # include "sim-endian.c"
354 #endif
355
356 #endif /* _SIM_ENDIAN_H_ */
This page took 0.037019 seconds and 5 git commands to generate.