PowerPC64 .branch_lt address
[binutils-gdb.git] / sim / common / sim-bits.h
1 /* The common simulator framework for GDB, the GNU Debugger.
2
3 Copyright 2002-2022 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_BITS_H
24 #define SIM_BITS_H
25
26
27 /* Bit manipulation routines:
28
29 Bit numbering: The bits are numbered according to the target ISA's
30 convention. That being controlled by WITH_TARGET_WORD_MSB. For
31 the PowerPC (WITH_TARGET_WORD_MSB == 0) the numbering is 0..31
32 while for the MIPS (WITH_TARGET_WORD_MSB == 31) it is 31..0.
33
34 Size convention: Each macro is in three forms - <MACRO>32 which
35 operates in 32bit quantity (bits are numbered 0..31); <MACRO>64
36 which operates using 64bit quantites (and bits are numbered 0..63);
37 and <MACRO> which operates using the bit size of the target
38 architecture (bits are still numbered 0..63), with 32bit
39 architectures ignoring the first 32bits leaving bit 32 as the most
40 significant.
41
42 NB: Use EXTRACTED, MSEXTRACTED and LSEXTRACTED as a guideline for
43 naming. LSMASK and LSMASKED are wrong.
44
45 BIT*(POS): `*' bit constant with just 1 bit set.
46
47 LSBIT*(OFFSET): `*' bit constant with just 1 bit set - LS bit is
48 zero.
49
50 MSBIT*(OFFSET): `*' bit constant with just 1 bit set - MS bit is
51 zero.
52
53 MASK*(FIRST, LAST): `*' bit constant with bits [FIRST .. LAST]
54 set. The <MACRO> (no size) version permits FIRST >= LAST and
55 generates a wrapped bit mask vis ([0..LAST] | [FIRST..LSB]).
56
57 LSMASK*(FIRST, LAST): Like MASK - LS bit is zero.
58
59 MSMASK*(FIRST, LAST): Like MASK - LS bit is zero.
60
61 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
62 .. LAST].
63
64 LSMASKED*(VALUE, FIRST, LAST): Like MASKED - LS bit is zero.
65
66 MSMASKED*(VALUE, FIRST, LAST): Like MASKED - MS bit is zero.
67
68 EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but
69 also right shifts the masked value so that bit LAST becomes the
70 least significant (right most).
71
72 LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is
73 zero.
74
75 MSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - MS bit is
76 zero.
77
78 SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
79 new NEW.
80
81 MOVED**(VALUE, OLD_FIRST, OLD_LAST, NEW_FIRST, NEW_LAST): Moves
82 things around so that bits OLD_FIRST..OLD_LAST are masked then
83 moved to NEW_FIRST..NEW_LAST.
84
85 INSERTED*(VALUE, FIRST, LAST): Takes VALUE and `inserts' the (LAST
86 - FIRST + 1) least significant bits into bit positions [ FIRST
87 .. LAST ]. This is almost the complement to EXTRACTED.
88
89 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
90 natural size. If in 32bit mode, discard the high 32bits.
91
92 EXTEND*(VALUE): Convert the `*' bit value to the targets natural
93 word size. Sign extend the value if needed.
94
95 align_*(VALUE, BYTES): Round the value so that it is aligned to a
96 BYTES boundary.
97
98 ROT*(VALUE, NR_BITS): Return the `*' bit VALUE rotated by NR_BITS
99 right (positive) or left (negative).
100
101 ROTL*(VALUE, NR_BITS): Return the `*' bit value rotated by NR_BITS
102 left. 0 <= NR_BITS <= `*'.
103
104 ROTR*(VALUE, NR_BITS): Return the `*' bit value rotated by NR_BITS
105 right. 0 <= NR_BITS <= N.
106
107 SEXT*(VALUE, SIGN_BIT): Treat SIGN_BIT as VALUEs sign, extend it ti
108 `*' bits.
109
110 Note: Only the BIT* and MASK* macros return a constant that can be
111 used in variable declarations.
112
113 */
114
115
116 /* compute the number of bits between START and STOP */
117
118 #if (WITH_TARGET_WORD_MSB == 0)
119 #define _MAKE_WIDTH(START, STOP) (STOP - START + 1)
120 #else
121 #define _MAKE_WIDTH(START, STOP) (START - STOP + 1)
122 #endif
123
124
125
126 /* compute the number shifts required to move a bit between LSB (MSB)
127 and POS */
128
129 #if (WITH_TARGET_WORD_MSB == 0)
130 #define _LSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
131 #else
132 #define _LSB_SHIFT(WIDTH, POS) (POS)
133 #endif
134
135 #if (WITH_TARGET_WORD_MSB == 0)
136 #define _MSB_SHIFT(WIDTH, POS) (POS)
137 #else
138 #define _MSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
139 #endif
140
141
142 /* compute the absolute bit position given the OFFSET from the MSB(LSB)
143 NB: _MAKE_xxx_POS (WIDTH, _MAKE_xxx_SHIFT (WIDTH, POS)) == POS */
144
145 #if (WITH_TARGET_WORD_MSB == 0)
146 #define _MSB_POS(WIDTH, SHIFT) (SHIFT)
147 #else
148 #define _MSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
149 #endif
150
151 #if (WITH_TARGET_WORD_MSB == 0)
152 #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
153 #else
154 #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
155 #endif
156
157
158 /* convert a 64 bit position into a corresponding 32bit position. MSB
159 pos handles the posibility that the bit lies beyond the 32bit
160 boundary */
161
162 #if (WITH_TARGET_WORD_MSB == 0)
163 #define _MSB_32(START, STOP) (START <= STOP \
164 ? (START < 32 ? 0 : START - 32) \
165 : (STOP < 32 ? 0 : STOP - 32))
166 #define _MSB_16(START, STOP) (START <= STOP \
167 ? (START < 48 ? 0 : START - 48) \
168 : (STOP < 48 ? 0 : STOP - 48))
169 #else
170 #define _MSB_32(START, STOP) (START >= STOP \
171 ? (START >= 32 ? 31 : START) \
172 : (STOP >= 32 ? 31 : STOP))
173 #define _MSB_16(START, STOP) (START >= STOP \
174 ? (START >= 16 ? 15 : START) \
175 : (STOP >= 16 ? 15 : STOP))
176 #endif
177
178 #if (WITH_TARGET_WORD_MSB == 0)
179 #define _LSB_32(START, STOP) (START <= STOP \
180 ? (STOP < 32 ? 0 : STOP - 32) \
181 : (START < 32 ? 0 : START - 32))
182 #define _LSB_16(START, STOP) (START <= STOP \
183 ? (STOP < 48 ? 0 : STOP - 48) \
184 : (START < 48 ? 0 : START - 48))
185 #else
186 #define _LSB_32(START, STOP) (START >= STOP \
187 ? (STOP >= 32 ? 31 : STOP) \
188 : (START >= 32 ? 31 : START))
189 #define _LSB_16(START, STOP) (START >= STOP \
190 ? (STOP >= 16 ? 15 : STOP) \
191 : (START >= 16 ? 15 : START))
192 #endif
193
194 #if (WITH_TARGET_WORD_MSB == 0)
195 #define _MSB(START, STOP) (START <= STOP ? START : STOP)
196 #else
197 #define _MSB(START, STOP) (START >= STOP ? START : STOP)
198 #endif
199
200 #if (WITH_TARGET_WORD_MSB == 0)
201 #define _LSB(START, STOP) (START <= STOP ? STOP : START)
202 #else
203 #define _LSB(START, STOP) (START >= STOP ? STOP : START)
204 #endif
205
206
207 /* LS/MS Bit operations */
208
209 #define LSBIT8(POS) ((uint8_t) 1 << (POS))
210 #define LSBIT16(POS) ((uint16_t)1 << (POS))
211 #define LSBIT32(POS) ((uint32_t)1 << (POS))
212 #define LSBIT64(POS) ((uint64_t)1 << (POS))
213
214 #if (WITH_TARGET_WORD_BITSIZE == 64)
215 #define LSBIT(POS) LSBIT64 (POS)
216 #endif
217 #if (WITH_TARGET_WORD_BITSIZE == 32)
218 #define LSBIT(POS) ((uint32_t)((POS) >= 32 \
219 ? 0 \
220 : (1 << ((POS) >= 32 ? 0 : (POS)))))
221 #endif
222 #if (WITH_TARGET_WORD_BITSIZE == 16)
223 #define LSBIT(POS) ((uint16_t)((POS) >= 16 \
224 ? 0 \
225 : (1 << ((POS) >= 16 ? 0 : (POS)))))
226 #endif
227
228
229 #define MSBIT8(POS) ((uint8_t) 1 << ( 8 - 1 - (POS)))
230 #define MSBIT16(POS) ((uint16_t)1 << (16 - 1 - (POS)))
231 #define MSBIT32(POS) ((uint32_t)1 << (32 - 1 - (POS)))
232 #define MSBIT64(POS) ((uint64_t)1 << (64 - 1 - (POS)))
233
234 #if (WITH_TARGET_WORD_BITSIZE == 64)
235 #define MSBIT(POS) MSBIT64 (POS)
236 #endif
237 #if (WITH_TARGET_WORD_BITSIZE == 32)
238 #define MSBIT(POS) ((uint32_t)((POS) < 32 \
239 ? 0 \
240 : (1 << ((POS) < 32 ? 0 : (64 - 1) - (POS)))))
241 #endif
242 #if (WITH_TARGET_WORD_BITSIZE == 16)
243 #define MSBIT(POS) ((uint16_t)((POS) < 48 \
244 ? 0 \
245 : (1 << ((POS) < 48 ? 0 : (64 - 1) - (POS)))))
246 #endif
247
248
249 /* Bit operations */
250
251 #define BIT4(POS) (1 << _LSB_SHIFT (4, (POS)))
252 #define BIT5(POS) (1 << _LSB_SHIFT (5, (POS)))
253 #define BIT10(POS) (1 << _LSB_SHIFT (10, (POS)))
254
255 #if (WITH_TARGET_WORD_MSB == 0)
256 #define BIT8 MSBIT8
257 #define BIT16 MSBIT16
258 #define BIT32 MSBIT32
259 #define BIT64 MSBIT64
260 #define BIT MSBIT
261 #else
262 #define BIT8 LSBIT8
263 #define BIT16 LSBIT16
264 #define BIT32 LSBIT32
265 #define BIT64 LSBIT64
266 #define BIT LSBIT
267 #endif
268
269
270
271 /* multi bit mask */
272
273 /* 111111 -> mmll11 -> mm11ll */
274 #define _MASKn(WIDTH, START, STOP) (((uint##WIDTH##_t)(-1) \
275 >> (_MSB_SHIFT (WIDTH, START) \
276 + _LSB_SHIFT (WIDTH, STOP))) \
277 << _LSB_SHIFT (WIDTH, STOP))
278
279 #if (WITH_TARGET_WORD_MSB == 0)
280 #define _POS_LE(START, STOP) (START <= STOP)
281 #else
282 #define _POS_LE(START, STOP) (STOP <= START)
283 #endif
284
285 #if (WITH_TARGET_WORD_BITSIZE == 64)
286 #define MASK(START, STOP) \
287 (_POS_LE ((START), (STOP)) \
288 ? _MASKn(64, \
289 _MSB ((START), (STOP)), \
290 _LSB ((START), (STOP)) ) \
291 : (_MASKn(64, _MSB_POS (64, 0), (STOP)) \
292 | _MASKn(64, (START), _LSB_POS (64, 0))))
293 #endif
294 #if (WITH_TARGET_WORD_BITSIZE == 32)
295 #define MASK(START, STOP) \
296 (_POS_LE ((START), (STOP)) \
297 ? (_POS_LE ((STOP), _MSB_POS (64, 31)) \
298 ? 0 \
299 : _MASKn (32, \
300 _MSB_32 ((START), (STOP)), \
301 _LSB_32 ((START), (STOP)))) \
302 : (_MASKn (32, \
303 _LSB_32 ((START), (STOP)), \
304 _LSB_POS (32, 0)) \
305 | (_POS_LE ((STOP), _MSB_POS (64, 31)) \
306 ? 0 \
307 : _MASKn (32, \
308 _MSB_POS (32, 0), \
309 _MSB_32 ((START), (STOP))))))
310 #endif
311 #if (WITH_TARGET_WORD_BITSIZE == 16)
312 #define MASK(START, STOP) \
313 (_POS_LE ((START), (STOP)) \
314 ? (_POS_LE ((STOP), _MSB_POS (64, 15)) \
315 ? 0 \
316 : _MASKn (16, \
317 _MSB_16 ((START), (STOP)), \
318 _LSB_16 ((START), (STOP)))) \
319 : (_MASKn (16, \
320 _LSB_16 ((START), (STOP)), \
321 _LSB_POS (16, 0)) \
322 | (_POS_LE ((STOP), _MSB_POS (64, 15)) \
323 ? 0 \
324 : _MASKn (16, \
325 _MSB_POS (16, 0), \
326 _MSB_16 ((START), (STOP))))))
327 #endif
328 #if !defined (MASK)
329 #error "MASK never undefined"
330 #endif
331
332
333 /* Multi-bit mask on least significant bits */
334
335 #define _LSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \
336 _LSB_POS (WIDTH, FIRST), \
337 _LSB_POS (WIDTH, LAST))
338
339 #define LSMASK8(FIRST, LAST) _LSMASKn ( 8, (FIRST), (LAST))
340 #define LSMASK16(FIRST, LAST) _LSMASKn (16, (FIRST), (LAST))
341 #define LSMASK32(FIRST, LAST) _LSMASKn (32, (FIRST), (LAST))
342 #define LSMASK64(FIRST, LAST) _LSMASKn (64, (FIRST), (LAST))
343
344 #define LSMASK(FIRST, LAST) (MASK (_LSB_POS (64, FIRST), _LSB_POS (64, LAST)))
345
346
347 /* Multi-bit mask on most significant bits */
348
349 #define _MSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \
350 _MSB_POS (WIDTH, FIRST), \
351 _MSB_POS (WIDTH, LAST))
352
353 #define MSMASK8(FIRST, LAST) _MSMASKn ( 8, (FIRST), (LAST))
354 #define MSMASK16(FIRST, LAST) _MSMASKn (16, (FIRST), (LAST))
355 #define MSMASK32(FIRST, LAST) _MSMASKn (32, (FIRST), (LAST))
356 #define MSMASK64(FIRST, LAST) _MSMASKn (64, (FIRST), (LAST))
357
358 #define MSMASK(FIRST, LAST) (MASK (_MSB_POS (64, FIRST), _MSB_POS (64, LAST)))
359
360
361
362 #if (WITH_TARGET_WORD_MSB == 0)
363 #define MASK8 MSMASK8
364 #define MASK16 MSMASK16
365 #define MASK32 MSMASK32
366 #define MASK64 MSMASK64
367 #else
368 #define MASK8 LSMASK8
369 #define MASK16 LSMASK16
370 #define MASK32 LSMASK32
371 #define MASK64 LSMASK64
372 #endif
373
374
375
376 /* mask the required bits, leaving them in place */
377
378 INLINE_SIM_BITS(uint8_t) LSMASKED8 (uint8_t word, int first, int last);
379 INLINE_SIM_BITS(uint16_t) LSMASKED16 (uint16_t word, int first, int last);
380 INLINE_SIM_BITS(uint32_t) LSMASKED32 (uint32_t word, int first, int last);
381 INLINE_SIM_BITS(uint64_t) LSMASKED64 (uint64_t word, int first, int last);
382
383 INLINE_SIM_BITS(unsigned_word) LSMASKED (unsigned_word word, int first, int last);
384
385 INLINE_SIM_BITS(uint8_t) MSMASKED8 (uint8_t word, int first, int last);
386 INLINE_SIM_BITS(uint16_t) MSMASKED16 (uint16_t word, int first, int last);
387 INLINE_SIM_BITS(uint32_t) MSMASKED32 (uint32_t word, int first, int last);
388 INLINE_SIM_BITS(uint64_t) MSMASKED64 (uint64_t word, int first, int last);
389
390 INLINE_SIM_BITS(unsigned_word) MSMASKED (unsigned_word word, int first, int last);
391
392 #if (WITH_TARGET_WORD_MSB == 0)
393 #define MASKED8 MSMASKED8
394 #define MASKED16 MSMASKED16
395 #define MASKED32 MSMASKED32
396 #define MASKED64 MSMASKED64
397 #define MASKED MSMASKED
398 #else
399 #define MASKED8 LSMASKED8
400 #define MASKED16 LSMASKED16
401 #define MASKED32 LSMASKED32
402 #define MASKED64 LSMASKED64
403 #define MASKED LSMASKED
404 #endif
405
406
407
408 /* extract the required bits aligning them with the lsb */
409
410 INLINE_SIM_BITS(uint8_t) LSEXTRACTED8 (uint8_t val, int start, int stop);
411 INLINE_SIM_BITS(uint16_t) LSEXTRACTED16 (uint16_t val, int start, int stop);
412 INLINE_SIM_BITS(uint32_t) LSEXTRACTED32 (uint32_t val, int start, int stop);
413 INLINE_SIM_BITS(uint64_t) LSEXTRACTED64 (uint64_t val, int start, int stop);
414
415 INLINE_SIM_BITS(unsigned_word) LSEXTRACTED (unsigned_word val, int start, int stop);
416
417 INLINE_SIM_BITS(uint8_t) MSEXTRACTED8 (uint8_t val, int start, int stop);
418 INLINE_SIM_BITS(uint16_t) MSEXTRACTED16 (uint16_t val, int start, int stop);
419 INLINE_SIM_BITS(uint32_t) MSEXTRACTED32 (uint32_t val, int start, int stop);
420 INLINE_SIM_BITS(uint64_t) MSEXTRACTED64 (uint64_t val, int start, int stop);
421
422 INLINE_SIM_BITS(unsigned_word) MSEXTRACTED (unsigned_word val, int start, int stop);
423
424 #if (WITH_TARGET_WORD_MSB == 0)
425 #define EXTRACTED8 MSEXTRACTED8
426 #define EXTRACTED16 MSEXTRACTED16
427 #define EXTRACTED32 MSEXTRACTED32
428 #define EXTRACTED64 MSEXTRACTED64
429 #define EXTRACTED MSEXTRACTED
430 #else
431 #define EXTRACTED8 LSEXTRACTED8
432 #define EXTRACTED16 LSEXTRACTED16
433 #define EXTRACTED32 LSEXTRACTED32
434 #define EXTRACTED64 LSEXTRACTED64
435 #define EXTRACTED LSEXTRACTED
436 #endif
437
438
439
440 /* move a single bit around */
441 /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
442 #define _SHUFFLEDn(N, WORD, OLD, NEW) \
443 ((OLD) < (NEW) \
444 ? (((uint##N##_t)(WORD) \
445 >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
446 & MASK32((NEW), (NEW))) \
447 : (((uint##N##_t)(WORD) \
448 << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
449 & MASK32((NEW), (NEW))))
450
451 #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn (32, WORD, OLD, NEW)
452 #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn (64, WORD, OLD, NEW)
453
454 #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn (_word, WORD, OLD, NEW)
455
456
457 /* Insert a group of bits into a bit position */
458
459 INLINE_SIM_BITS(uint8_t) LSINSERTED8 (uint8_t val, int start, int stop);
460 INLINE_SIM_BITS(uint16_t) LSINSERTED16 (uint16_t val, int start, int stop);
461 INLINE_SIM_BITS(uint32_t) LSINSERTED32 (uint32_t val, int start, int stop);
462 INLINE_SIM_BITS(uint64_t) LSINSERTED64 (uint64_t val, int start, int stop);
463 INLINE_SIM_BITS(unsigned_word) LSINSERTED (unsigned_word val, int start, int stop);
464
465 INLINE_SIM_BITS(uint8_t) MSINSERTED8 (uint8_t val, int start, int stop);
466 INLINE_SIM_BITS(uint16_t) MSINSERTED16 (uint16_t val, int start, int stop);
467 INLINE_SIM_BITS(uint32_t) MSINSERTED32 (uint32_t val, int start, int stop);
468 INLINE_SIM_BITS(uint64_t) MSINSERTED64 (uint64_t val, int start, int stop);
469 INLINE_SIM_BITS(unsigned_word) MSINSERTED (unsigned_word val, int start, int stop);
470
471 #if (WITH_TARGET_WORD_MSB == 0)
472 #define INSERTED8 MSINSERTED8
473 #define INSERTED16 MSINSERTED16
474 #define INSERTED32 MSINSERTED32
475 #define INSERTED64 MSINSERTED64
476 #define INSERTED MSINSERTED
477 #else
478 #define INSERTED8 LSINSERTED8
479 #define INSERTED16 LSINSERTED16
480 #define INSERTED32 LSINSERTED32
481 #define INSERTED64 LSINSERTED64
482 #define INSERTED LSINSERTED
483 #endif
484
485
486
487 /* MOVE bits from one loc to another (combination of extract/insert) */
488
489 #define MOVED8(VAL,OH,OL,NH,NL) INSERTED8 (EXTRACTED8 ((VAL), OH, OL), NH, NL)
490 #define MOVED16(VAL,OH,OL,NH,NL) INSERTED16(EXTRACTED16((VAL), OH, OL), NH, NL)
491 #define MOVED32(VAL,OH,OL,NH,NL) INSERTED32(EXTRACTED32((VAL), OH, OL), NH, NL)
492 #define MOVED64(VAL,OH,OL,NH,NL) INSERTED64(EXTRACTED64((VAL), OH, OL), NH, NL)
493 #define MOVED(VAL,OH,OL,NH,NL) INSERTED (EXTRACTED ((VAL), OH, OL), NH, NL)
494
495
496
497 /* Sign extend the quantity to the targets natural word size */
498
499 #define EXTEND4(X) (LSSEXT ((X), 3))
500 #define EXTEND5(X) (LSSEXT ((X), 4))
501 #define EXTEND6(X) (LSSEXT ((X), 5))
502 #define EXTEND8(X) ((signed_word)(int8_t)(X))
503 #define EXTEND9(X) (LSSEXT ((X), 8))
504 #define EXTEND11(X) (LSSEXT ((X), 10))
505 #define EXTEND12(X) (LSSEXT ((X), 11))
506 #define EXTEND15(X) (LSSEXT ((X), 14))
507 #define EXTEND16(X) ((signed_word)(int16_t)(X))
508 #define EXTEND18(X) (LSSEXT ((X), 17))
509 #define EXTEND19(X) (LSSEXT ((X), 18))
510 #define EXTEND21(X) (LSSEXT ((X), 20))
511 #define EXTEND24(X) (LSSEXT ((X), 23))
512 #define EXTEND25(X) (LSSEXT ((X), 24))
513 #define EXTEND26(X) (LSSEXT ((X), 25))
514 #define EXTEND32(X) ((signed_word)(int32_t)(X))
515 #define EXTEND64(X) ((signed_word)(int64_t)(X))
516
517 /* depending on MODE return a 64bit or 32bit (sign extended) value */
518 #if (WITH_TARGET_WORD_BITSIZE == 64)
519 #define EXTENDED(X) ((int64_t)(int32_t)(X))
520 #endif
521 #if (WITH_TARGET_WORD_BITSIZE == 32)
522 #define EXTENDED(X) (X)
523 #endif
524 #if (WITH_TARGET_WORD_BITSIZE == 16)
525 #define EXTENDED(X) (X)
526 #endif
527
528
529 /* memory alignment macro's */
530 #define align_up(v, n) (((v) + (n) - 1) & -(n))
531 #define align_down(v, n) ((v) & -(n))
532
533
534 /* bit bliting macro's */
535 #define BLIT32(V, POS, BIT) \
536 do { \
537 if (BIT) \
538 V |= BIT32 (POS); \
539 else \
540 V &= ~BIT32 (POS); \
541 } while (0)
542 #define MBLIT32(V, LO, HI, VAL) \
543 do { \
544 (V) = (((V) & ~MASK32 ((LO), (HI))) \
545 | INSERTED32 (VAL, LO, HI)); \
546 } while (0)
547
548
549
550 /* some rotate functions. The generic macro's ROT, ROTL, ROTR are
551 intentionally omited. */
552
553
554 INLINE_SIM_BITS(uint8_t) ROT8 (uint8_t val, int shift);
555 INLINE_SIM_BITS(uint16_t) ROT16 (uint16_t val, int shift);
556 INLINE_SIM_BITS(uint32_t) ROT32 (uint32_t val, int shift);
557 INLINE_SIM_BITS(uint64_t) ROT64 (uint64_t val, int shift);
558
559
560 INLINE_SIM_BITS(uint8_t) ROTL8 (uint8_t val, int shift);
561 INLINE_SIM_BITS(uint16_t) ROTL16 (uint16_t val, int shift);
562 INLINE_SIM_BITS(uint32_t) ROTL32 (uint32_t val, int shift);
563 INLINE_SIM_BITS(uint64_t) ROTL64 (uint64_t val, int shift);
564
565
566 INLINE_SIM_BITS(uint8_t) ROTR8 (uint8_t val, int shift);
567 INLINE_SIM_BITS(uint16_t) ROTR16 (uint16_t val, int shift);
568 INLINE_SIM_BITS(uint32_t) ROTR32 (uint32_t val, int shift);
569 INLINE_SIM_BITS(uint64_t) ROTR64 (uint64_t val, int shift);
570
571
572
573 /* Sign extension operations */
574
575 INLINE_SIM_BITS(uint8_t) LSSEXT8 (int8_t val, int sign_bit);
576 INLINE_SIM_BITS(uint16_t) LSSEXT16 (int16_t val, int sign_bit);
577 INLINE_SIM_BITS(uint32_t) LSSEXT32 (int32_t val, int sign_bit);
578 INLINE_SIM_BITS(uint64_t) LSSEXT64 (int64_t val, int sign_bit);
579 INLINE_SIM_BITS(unsigned_word) LSSEXT (signed_word val, int sign_bit);
580
581 INLINE_SIM_BITS(uint8_t) MSSEXT8 (int8_t val, int sign_bit);
582 INLINE_SIM_BITS(uint16_t) MSSEXT16 (int16_t val, int sign_bit);
583 INLINE_SIM_BITS(uint32_t) MSSEXT32 (int32_t val, int sign_bit);
584 INLINE_SIM_BITS(uint64_t) MSSEXT64 (int64_t val, int sign_bit);
585 INLINE_SIM_BITS(unsigned_word) MSSEXT (signed_word val, int sign_bit);
586
587 #if (WITH_TARGET_WORD_MSB == 0)
588 #define SEXT8 MSSEXT8
589 #define SEXT16 MSSEXT16
590 #define SEXT32 MSSEXT32
591 #define SEXT64 MSSEXT64
592 #define SEXT MSSEXT
593 #else
594 #define SEXT8 LSSEXT8
595 #define SEXT16 LSSEXT16
596 #define SEXT32 LSSEXT32
597 #define SEXT64 LSSEXT64
598 #define SEXT LSSEXT
599 #endif
600
601
602
603 #if H_REVEALS_MODULE_P (SIM_BITS_INLINE)
604 #include "sim-bits.c"
605 #endif
606
607 #endif /* SIM_BITS_H */