gmp-utils: New API to simply use of GMP's integer/rational/float objects
[binutils-gdb.git] / sim / common / sim-bits.c
blob82e82814bd64bc2dc180be9c2a07c5508d6e99b6
1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2020 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 #include "sim-basics.h"
27 #include "sim-assert.h"
28 #include "sim-io.h"
31 INLINE_SIM_BITS\
32 (unsigned_word)
33 LSMASKED (unsigned_word val,
34 int start,
35 int stop)
37 /* NOTE - start, stop can wrap */
38 val &= LSMASK (start, stop);
39 return val;
43 INLINE_SIM_BITS\
44 (unsigned_word)
45 MSMASKED (unsigned_word val,
46 int start,
47 int stop)
49 /* NOTE - start, stop can wrap */
50 val &= MSMASK (start, stop);
51 return val;
55 INLINE_SIM_BITS\
56 (unsigned_word)
57 LSEXTRACTED (unsigned_word val,
58 int start,
59 int stop)
61 ASSERT (start >= stop);
62 #if (WITH_TARGET_WORD_BITSIZE == 64)
63 return LSEXTRACTED64 (val, start, stop);
64 #endif
65 #if (WITH_TARGET_WORD_BITSIZE == 32)
66 if (stop >= 32)
67 return 0;
68 else
70 if (start < 32)
71 val &= LSMASK (start, 0);
72 val >>= stop;
73 return val;
75 #endif
76 #if (WITH_TARGET_WORD_BITSIZE == 16)
77 if (stop >= 16)
78 return 0;
79 else
81 if (start < 16)
82 val &= LSMASK (start, 0);
83 val >>= stop;
84 return val;
86 #endif
90 INLINE_SIM_BITS\
91 (unsigned_word)
92 MSEXTRACTED (unsigned_word val,
93 int start,
94 int stop)
96 ASSERT (start <= stop);
97 #if (WITH_TARGET_WORD_BITSIZE == 64)
98 return MSEXTRACTED64 (val, start, stop);
99 #endif
100 #if (WITH_TARGET_WORD_BITSIZE == 32)
101 if (stop < 32)
102 return 0;
103 else
105 if (start >= 32)
106 val &= MSMASK (start, 64 - 1);
107 val >>= (64 - stop - 1);
108 return val;
110 #endif
111 #if (WITH_TARGET_WORD_BITSIZE == 16)
112 if (stop < 16)
113 return 0;
114 else
116 if (start >= 16)
117 val &= MSMASK (start, 64 - 1);
118 val >>= (64 - stop - 1);
119 return val;
121 #endif
125 INLINE_SIM_BITS\
126 (unsigned_word)
127 LSINSERTED (unsigned_word val,
128 int start,
129 int stop)
131 ASSERT (start >= stop);
132 #if (WITH_TARGET_WORD_BITSIZE == 64)
133 return LSINSERTED64 (val, start, stop);
134 #endif
135 #if (WITH_TARGET_WORD_BITSIZE == 32)
136 /* Bit numbers are 63..0, even for 32 bit targets.
137 On 32 bit targets we ignore 63..32 */
138 if (stop >= 32)
139 return 0;
140 else
142 val <<= stop;
143 val &= LSMASK (start, stop);
144 return val;
146 #endif
147 #if (WITH_TARGET_WORD_BITSIZE == 16)
148 /* Bit numbers are 63..0, even for 16 bit targets.
149 On 16 bit targets we ignore 63..16 */
150 if (stop >= 16)
151 return 0;
152 else
154 val <<= stop;
155 val &= LSMASK (start, stop);
156 return val;
158 #endif
161 INLINE_SIM_BITS\
162 (unsigned_word)
163 MSINSERTED (unsigned_word val,
164 int start,
165 int stop)
167 ASSERT (start <= stop);
168 #if (WITH_TARGET_WORD_BITSIZE == 64)
169 return MSINSERTED64 (val, start, stop);
170 #endif
171 #if (WITH_TARGET_WORD_BITSIZE == 32)
172 /* Bit numbers are 0..63, even for 32 bit targets.
173 On 32 bit targets we ignore 0..31. */
174 if (stop < 32)
175 return 0;
176 else
178 val <<= ((64 - 1) - stop);
179 val &= MSMASK (start, stop);
180 return val;
182 #endif
183 #if (WITH_TARGET_WORD_BITSIZE == 16)
184 /* Bit numbers are 0..63, even for 16 bit targets.
185 On 16 bit targets we ignore 0..47. */
186 if (stop < 32 + 16)
187 return 0;
188 else
190 val <<= ((64 - 1) - stop);
191 val &= MSMASK (start, stop);
192 return val;
194 #endif
199 INLINE_SIM_BITS\
200 (unsigned_word)
201 LSSEXT (signed_word val,
202 int sign_bit)
204 ASSERT (sign_bit < 64);
205 #if (WITH_TARGET_WORD_BITSIZE == 64)
206 return LSSEXT64 (val, sign_bit);
207 #endif
208 #if (WITH_TARGET_WORD_BITSIZE == 32)
209 if (sign_bit >= 32)
210 return val;
211 else {
212 val = LSSEXT32 (val, sign_bit);
213 return val;
215 #endif
216 #if (WITH_TARGET_WORD_BITSIZE == 16)
217 if (sign_bit >= 16)
218 return val;
219 else {
220 val = LSSEXT16 (val, sign_bit);
221 return val;
223 #endif
226 INLINE_SIM_BITS\
227 (unsigned_word)
228 MSSEXT (signed_word val,
229 int sign_bit)
231 ASSERT (sign_bit < 64);
232 #if (WITH_TARGET_WORD_BITSIZE == 64)
233 return MSSEXT64 (val, sign_bit);
234 #endif
235 #if (WITH_TARGET_WORD_BITSIZE == 32)
236 if (sign_bit < 32)
237 return val;
238 else {
239 val = MSSEXT32 (val, sign_bit - 32);
240 return val;
242 #endif
243 #if (WITH_TARGET_WORD_BITSIZE == 16)
244 if (sign_bit < 32 + 16)
245 return val;
246 else {
247 val = MSSEXT16 (val, sign_bit - 32 - 16);
248 return val;
250 #endif
255 #define N 8
256 #include "sim-n-bits.h"
257 #undef N
259 #define N 16
260 #include "sim-n-bits.h"
261 #undef N
263 #define N 32
264 #include "sim-n-bits.h"
265 #undef N
267 #define N 64
268 #include "sim-n-bits.h"
269 #undef N
271 #endif /* _SIM_BITS_C_ */