xref: /netbsd-src/external/gpl3/gdb/dist/sim/common/sim-endian.c (revision 88241920d21b339bf319c0e979ffda80c49a2936)
1 /* The common simulator framework for GDB, the GNU Debugger.
2 
3    Copyright 2002-2024 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 /* This must come before any other includes.  */
27 #include "defs.h"
28 
29 #include "bfd.h"
30 
31 #include "sim-basics.h"
32 #include "sim-assert.h"
33 
34 
35 #if !defined(_SWAP_1)
36 #define _SWAP_1(SET,RAW) SET (RAW)
37 #endif
38 
39 #if !defined(_SWAP_2) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htons)
40 #define _SWAP_2(SET,RAW) SET htons (RAW)
41 #endif
42 
43 #ifndef	_SWAP_2
44 #define _SWAP_2(SET,RAW) SET (((RAW) >> 8) | ((RAW) << 8))
45 #endif
46 
47 #if !defined(_SWAP_4) && (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE) && defined(htonl)
48 #define _SWAP_4(SET,RAW) SET htonl (RAW)
49 #endif
50 
51 #ifndef _SWAP_4
52 #define	_SWAP_4(SET,RAW) SET (((RAW) << 24) | (((RAW) & 0xff00) << 8) | (((RAW) & 0xff0000) >> 8) | ((RAW) >> 24))
53 #endif
54 
55 #ifndef _SWAP_8
56 #define _SWAP_8(SET,RAW) \
57   union { unsigned_8 dword; unsigned_4 words[2]; } in, out; \
58   in.dword = RAW; \
59   _SWAP_4 (out.words[0] =, in.words[1]); \
60   _SWAP_4 (out.words[1] =, in.words[0]); \
61   SET out.dword;
62 #endif
63 
64 #ifndef _SWAP_16
65 #define _SWAP_16(SET,RAW) \
66   union { unsigned_16 word; unsigned_4 words[4]; } in, out; \
67   in.word = (RAW); \
68   _SWAP_4 (out.words[0] =, in.words[3]); \
69   _SWAP_4 (out.words[1] =, in.words[2]); \
70   _SWAP_4 (out.words[2] =, in.words[1]); \
71   _SWAP_4 (out.words[3] =, in.words[0]); \
72   SET out.word;
73 #endif
74 
75 
76 #define N 1
77 #include "sim-n-endian.h"
78 #undef N
79 
80 #define N 2
81 #include "sim-n-endian.h"
82 #undef N
83 
84 #define N 4
85 #include "sim-n-endian.h"
86 #undef N
87 
88 #define N 8
89 #include "sim-n-endian.h"
90 #undef N
91 
92 #define N 16
93 #include "sim-n-endian.h"
94 #undef N
95 
96 
97 INLINE_SIM_ENDIAN\
98 (unsigned_8)
99 sim_endian_split_16 (unsigned_16 word, int w)
100 {
101   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
102     {
103       return word.a[1 - w];
104     }
105   else
106     {
107       return word.a[w];
108     }
109 }
110 
111 
112 INLINE_SIM_ENDIAN\
113 (unsigned_16)
114 sim_endian_join_16 (unsigned_8 h, unsigned_8 l)
115 
116 {
117   unsigned_16 word;
118   if (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
119     {
120       word.a[0] = l;
121       word.a[1] = h;
122     }
123   else
124     {
125       word.a[0] = h;
126       word.a[1] = l;
127     }
128   return word;
129 }
130 
131 
132 
133 #endif /* _SIM_ENDIAN_C_ */
134