xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-endian.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002-2020 Free Software Foundation, Inc.
4 
5    Contributed by Andrew Cagney and Red Hat.
6 
7    This file is part of GDB.
8 
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21 
22 
23 #ifndef _SIM_ENDIAN_C_
24 #define _SIM_ENDIAN_C_
25 
26 #include "sim-basics.h"
27 #include "sim-assert.h"
28 
29 
30 #if !defined(_SWAP_1)
31 #define _SWAP_1(SET,RAW) SET (RAW)
32 #endif
33 
34 #if !defined(_SWAP_2) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htons)
35 #define _SWAP_2(SET,RAW) SET htons (RAW)
36 #endif
37 
38 #ifndef	_SWAP_2
39 #define _SWAP_2(SET,RAW) SET (((RAW) >> 8) | ((RAW) << 8))
40 #endif
41 
42 #if !defined(_SWAP_4) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htonl)
43 #define _SWAP_4(SET,RAW) SET htonl (RAW)
44 #endif
45 
46 #ifndef _SWAP_4
47 #define	_SWAP_4(SET,RAW) SET (((RAW) << 24) | (((RAW) & 0xff00) << 8) | (((RAW) & 0xff0000) >> 8) | ((RAW) >> 24))
48 #endif
49 
50 #ifndef _SWAP_8
51 #define _SWAP_8(SET,RAW) \
52   union { unsigned_8 dword; unsigned_4 words[2]; } in, out; \
53   in.dword = RAW; \
54   _SWAP_4 (out.words[0] =, in.words[1]); \
55   _SWAP_4 (out.words[1] =, in.words[0]); \
56   SET out.dword;
57 #endif
58 
59 #ifndef _SWAP_16
60 #define _SWAP_16(SET,RAW) \
61   union { unsigned_16 word; unsigned_4 words[4]; } in, out; \
62   in.word = (RAW); \
63   _SWAP_4 (out.words[0] =, in.words[3]); \
64   _SWAP_4 (out.words[1] =, in.words[2]); \
65   _SWAP_4 (out.words[2] =, in.words[1]); \
66   _SWAP_4 (out.words[3] =, in.words[0]); \
67   SET out.word;
68 #endif
69 
70 
71 #define N 1
72 #include "sim-n-endian.h"
73 #undef N
74 
75 #define N 2
76 #include "sim-n-endian.h"
77 #undef N
78 
79 #define N 4
80 #include "sim-n-endian.h"
81 #undef N
82 
83 #define N 8
84 #include "sim-n-endian.h"
85 #undef N
86 
87 #define N 16
88 #include "sim-n-endian.h"
89 #undef N
90 
91 
92 INLINE_SIM_ENDIAN\
93 (unsigned_8)
94 sim_endian_split_16 (unsigned_16 word, int w)
95 {
96   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
97     {
98       return word.a[1 - w];
99     }
100   else
101     {
102       return word.a[w];
103     }
104 }
105 
106 
107 INLINE_SIM_ENDIAN\
108 (unsigned_16)
109 sim_endian_join_16 (unsigned_8 h, unsigned_8 l)
110 
111 {
112   unsigned_16 word;
113   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
114     {
115       word.a[0] = l;
116       word.a[1] = h;
117     }
118   else
119     {
120       word.a[0] = h;
121       word.a[1] = l;
122     }
123   return word;
124 }
125 
126 
127 
128 #endif /* _SIM_ENDIAN_C_ */
129