Fix whitespace snafu in tc-riscv.c
[binutils-gdb.git] / sim / common / sim-bits.c
blobae0b45e748ebc72436b6117727dbaf7d45326c3c
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2023 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
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.
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.
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/>. */
23 #ifndef _SIM_BITS_C_
24 #define _SIM_BITS_C_
26 /* This must come before any other includes. */
27 #include "defs.h"
29 #include "sim-basics.h"
30 #include "sim-assert.h"
31 #include "sim-io.h"
34 INLINE_SIM_BITS\
35 (unsigned_word)
36 LSMASKED (unsigned_word val,
37 int start,
38 int stop)
40 /* NOTE - start, stop can wrap */
41 val &= LSMASK (start, stop);
42 return val;
46 INLINE_SIM_BITS\
47 (unsigned_word)
48 MSMASKED (unsigned_word val,
49 int start,
50 int stop)
52 /* NOTE - start, stop can wrap */
53 val &= MSMASK (start, stop);
54 return val;
58 INLINE_SIM_BITS\
59 (unsigned_word)
60 LSEXTRACTED (unsigned_word val,
61 int start,
62 int stop)
64 ASSERT (start >= stop);
65 #if (WITH_TARGET_WORD_BITSIZE == 64)
66 return LSEXTRACTED64 (val, start, stop);
67 #endif
68 #if (WITH_TARGET_WORD_BITSIZE == 32)
69 if (stop >= 32)
70 return 0;
71 else
73 if (start < 32)
74 val &= LSMASK (start, 0);
75 val >>= stop;
76 return val;
78 #endif
79 #if (WITH_TARGET_WORD_BITSIZE == 16)
80 if (stop >= 16)
81 return 0;
82 else
84 if (start < 16)
85 val &= LSMASK (start, 0);
86 val >>= stop;
87 return val;
89 #endif
93 INLINE_SIM_BITS\
94 (unsigned_word)
95 MSEXTRACTED (unsigned_word val,
96 int start,
97 int stop)
99 ASSERT (start <= stop);
100 #if (WITH_TARGET_WORD_BITSIZE == 64)
101 return MSEXTRACTED64 (val, start, stop);
102 #endif
103 #if (WITH_TARGET_WORD_BITSIZE == 32)
104 if (stop < 32)
105 return 0;
106 else
108 if (start >= 32)
109 val &= MSMASK (start, 64 - 1);
110 val >>= (64 - stop - 1);
111 return val;
113 #endif
114 #if (WITH_TARGET_WORD_BITSIZE == 16)
115 if (stop < 16)
116 return 0;
117 else
119 if (start >= 16)
120 val &= MSMASK (start, 64 - 1);
121 val >>= (64 - stop - 1);
122 return val;
124 #endif
128 INLINE_SIM_BITS\
129 (unsigned_word)
130 LSINSERTED (unsigned_word val,
131 int start,
132 int stop)
134 ASSERT (start >= stop);
135 #if (WITH_TARGET_WORD_BITSIZE == 64)
136 return LSINSERTED64 (val, start, stop);
137 #endif
138 #if (WITH_TARGET_WORD_BITSIZE == 32)
139 /* Bit numbers are 63..0, even for 32 bit targets.
140 On 32 bit targets we ignore 63..32 */
141 if (stop >= 32)
142 return 0;
143 else
145 val <<= stop;
146 val &= LSMASK (start, stop);
147 return val;
149 #endif
150 #if (WITH_TARGET_WORD_BITSIZE == 16)
151 /* Bit numbers are 63..0, even for 16 bit targets.
152 On 16 bit targets we ignore 63..16 */
153 if (stop >= 16)
154 return 0;
155 else
157 val <<= stop;
158 val &= LSMASK (start, stop);
159 return val;
161 #endif
164 INLINE_SIM_BITS\
165 (unsigned_word)
166 MSINSERTED (unsigned_word val,
167 int start,
168 int stop)
170 ASSERT (start <= stop);
171 #if (WITH_TARGET_WORD_BITSIZE == 64)
172 return MSINSERTED64 (val, start, stop);
173 #endif
174 #if (WITH_TARGET_WORD_BITSIZE == 32)
175 /* Bit numbers are 0..63, even for 32 bit targets.
176 On 32 bit targets we ignore 0..31. */
177 if (stop < 32)
178 return 0;
179 else
181 val <<= ((64 - 1) - stop);
182 val &= MSMASK (start, stop);
183 return val;
185 #endif
186 #if (WITH_TARGET_WORD_BITSIZE == 16)
187 /* Bit numbers are 0..63, even for 16 bit targets.
188 On 16 bit targets we ignore 0..47. */
189 if (stop < 32 + 16)
190 return 0;
191 else
193 val <<= ((64 - 1) - stop);
194 val &= MSMASK (start, stop);
195 return val;
197 #endif
202 INLINE_SIM_BITS\
203 (unsigned_word)
204 LSSEXT (signed_word val,
205 int sign_bit)
207 ASSERT (sign_bit < 64);
208 #if (WITH_TARGET_WORD_BITSIZE == 64)
209 return LSSEXT64 (val, sign_bit);
210 #endif
211 #if (WITH_TARGET_WORD_BITSIZE == 32)
212 if (sign_bit >= 32)
213 return val;
214 else {
215 val = LSSEXT32 (val, sign_bit);
216 return val;
218 #endif
219 #if (WITH_TARGET_WORD_BITSIZE == 16)
220 if (sign_bit >= 16)
221 return val;
222 else {
223 val = LSSEXT16 (val, sign_bit);
224 return val;
226 #endif
229 INLINE_SIM_BITS\
230 (unsigned_word)
231 MSSEXT (signed_word val,
232 int sign_bit)
234 ASSERT (sign_bit < 64);
235 #if (WITH_TARGET_WORD_BITSIZE == 64)
236 return MSSEXT64 (val, sign_bit);
237 #endif
238 #if (WITH_TARGET_WORD_BITSIZE == 32)
239 if (sign_bit < 32)
240 return val;
241 else {
242 val = MSSEXT32 (val, sign_bit - 32);
243 return val;
245 #endif
246 #if (WITH_TARGET_WORD_BITSIZE == 16)
247 if (sign_bit < 32 + 16)
248 return val;
249 else {
250 val = MSSEXT16 (val, sign_bit - 32 - 16);
251 return val;
253 #endif
258 #define N 8
259 #include "sim-n-bits.h"
260 #undef N
262 #define N 16
263 #include "sim-n-bits.h"
264 #undef N
266 #define N 32
267 #include "sim-n-bits.h"
268 #undef N
270 #define N 64
271 #include "sim-n-bits.h"
272 #undef N
274 #endif /* _SIM_BITS_C_ */