734ea213e52e3894f7a2e8174c496ee6b66a3342
[external/binutils.git] / sim / ppc / ppc-endian.c
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_C_
23 #define _ENDIAN_C_
24
25 #ifndef STATIC_INLINE_ENDIAN
26 #define STATIC_INLINE_ENDIAN STATIC_INLINE
27 #endif
28
29
30 #include "config.h"
31 #include "ppc-config.h"
32 #include "words.h"
33 #include "ppc-endian.h"
34 #include "sim_callbacks.h"
35
36
37 typedef union {
38   unsigned_1 val_1[8];
39   unsigned_2 val_2[4];
40   unsigned_4 val_4[2];
41   unsigned_8 val_8[1];
42 } endian_map;
43
44 #define ENDIAN_N(NAME,BYTE_SIZE) \
45 unsigned_##BYTE_SIZE \
46 endian_##NAME##_##BYTE_SIZE(unsigned_##BYTE_SIZE raw_in) \
47 { \
48   if (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER) { \
49     return raw_in; \
50   } \
51   else { \
52     endian_map in; \
53     endian_map out; \
54     int byte_nr; \
55     in.val_##BYTE_SIZE[0] = raw_in; \
56     for (byte_nr = 0; byte_nr < BYTE_SIZE; byte_nr++) { \
57       out.val_1[BYTE_SIZE-1-byte_nr] = in.val_1[byte_nr]; \
58     } \
59     return out.val_##BYTE_SIZE[0]; \
60   } \
61 }
62
63
64 ENDIAN_N(h2t, 2)
65 ENDIAN_N(h2t, 4)
66 ENDIAN_N(h2t, 8)
67 ENDIAN_N(t2h, 2)
68 ENDIAN_N(t2h, 4)
69 ENDIAN_N(t2h, 8)
70
71 #endif /* _ENDIAN_C_ */