90d1d1fc30fb8a7a0542bbcaf31e4967b17f466c
[external/binutils.git] / sim / common / sim-n-endian.h
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3    Copyright 2002, 2007 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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24
25 #ifndef N
26 #error "N must be #defined"
27 #endif
28
29 #include "symcat.h"
30
31 /* NOTE: See end of file for #undef */
32 #define unsigned_N XCONCAT2(unsigned_,N)
33 #define endian_t2h_N XCONCAT2(endian_t2h_,N)
34 #define endian_h2t_N XCONCAT2(endian_h2t_,N)
35 #define _SWAP_N XCONCAT2(_SWAP_,N)
36 #define swap_N XCONCAT2(swap_,N)
37 #define endian_h2be_N XCONCAT2(endian_h2be_,N)
38 #define endian_be2h_N XCONCAT2(endian_be2h_,N)
39 #define endian_h2le_N XCONCAT2(endian_h2le_,N)
40 #define endian_le2h_N XCONCAT2(endian_le2h_,N)
41 #define offset_N XCONCAT2(offset_,N)
42
43 /* TAGS: endian_t2h_1 endian_t2h_2 endian_t2h_4 endian_t2h_8 endian_t2h_16 */
44
45 INLINE_SIM_ENDIAN\
46 (unsigned_N)
47 endian_t2h_N(unsigned_N raw_in)
48 {
49   if (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER) {
50     return raw_in;
51   }
52   else {
53     _SWAP_N(return,raw_in);
54   }
55 }
56
57 /* TAGS: endian_h2t_1 endian_h2t_2 endian_h2t_4 endian_h2t_8 endian_h2t_16 */
58
59 INLINE_SIM_ENDIAN\
60 (unsigned_N)
61 endian_h2t_N(unsigned_N raw_in)
62 {
63   if (CURRENT_TARGET_BYTE_ORDER == CURRENT_HOST_BYTE_ORDER) {
64     return raw_in;
65   }
66   else {
67     _SWAP_N(return,raw_in);
68   }
69 }
70
71 /* TAGS: swap_1 swap_2 swap_4 swap_8 swap_16 */
72
73 INLINE_SIM_ENDIAN\
74 (unsigned_N)
75 swap_N(unsigned_N raw_in)
76 {
77   _SWAP_N(return,raw_in);
78 }
79
80 /* TAGS: endian_h2be_1 endian_h2be_2 endian_h2be_4 endian_h2be_8 endian_h2be_16 */
81
82 INLINE_SIM_ENDIAN\
83 (unsigned_N)
84 endian_h2be_N(unsigned_N raw_in)
85 {
86   if (CURRENT_HOST_BYTE_ORDER == BIG_ENDIAN) {
87     return raw_in;
88   }
89   else {
90     _SWAP_N(return,raw_in);
91   }
92 }
93
94 /* TAGS: endian_be2h_1 endian_be2h_2 endian_be2h_4 endian_be2h_8 endian_be2h_16 */
95
96 INLINE_SIM_ENDIAN\
97 (unsigned_N)
98 endian_be2h_N(unsigned_N raw_in)
99 {
100   if (CURRENT_HOST_BYTE_ORDER == BIG_ENDIAN) {
101     return raw_in;
102   }
103   else {
104     _SWAP_N(return,raw_in);
105   }
106 }
107
108 /* TAGS: endian_h2le_1 endian_h2le_2 endian_h2le_4 endian_h2le_8 endian_h2le_16 */
109
110 INLINE_SIM_ENDIAN\
111 (unsigned_N)
112 endian_h2le_N(unsigned_N raw_in)
113 {
114   if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN) {
115     return raw_in;
116   }
117   else {
118     _SWAP_N(return,raw_in);
119   }
120 }
121
122 /* TAGS: endian_le2h_1 endian_le2h_2 endian_le2h_4 endian_le2h_8 endian_le2h_16 */
123
124 INLINE_SIM_ENDIAN\
125 (unsigned_N)
126 endian_le2h_N(unsigned_N raw_in)
127 {
128   if (CURRENT_HOST_BYTE_ORDER == LITTLE_ENDIAN) {
129     return raw_in;
130   }
131   else {
132     _SWAP_N(return,raw_in);
133   }
134 }
135
136 /* TAGS: offset_1 offset_2 offset_4 offset_8 offset_16 */
137
138 INLINE_SIM_ENDIAN\
139 (void*)
140 offset_N (unsigned_N *x,
141           unsigned sizeof_word,
142           unsigned word)
143 {
144   char *in = (char*)x;
145   char *out;
146   unsigned offset = sizeof_word * word;
147   ASSERT (offset + sizeof_word <= sizeof(unsigned_N));
148   ASSERT (word < (sizeof (unsigned_N) / sizeof_word));
149   ASSERT ((sizeof (unsigned_N) % sizeof_word) == 0);
150   if (WITH_HOST_BYTE_ORDER == LITTLE_ENDIAN)
151     {
152       out = in + sizeof (unsigned_N) - offset - sizeof_word;
153     }
154   else
155     {
156       out = in + offset;
157     }
158   return out;
159 }
160
161
162 /* NOTE: See start of file for #define */
163 #undef unsigned_N
164 #undef endian_t2h_N
165 #undef endian_h2t_N
166 #undef _SWAP_N
167 #undef swap_N
168 #undef endian_h2be_N
169 #undef endian_be2h_N
170 #undef endian_h2le_N
171 #undef endian_le2h_N
172 #undef offset_N