1 /* This file is part of the program psim.
4 Copyright (C) 1997, Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /* bit manipulation routines:
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.
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
42 NB: Use EXTRACTED, MSEXTRACTED and LSEXTRACTED as a guideline for
43 naming. LSMASK and LSMASKED are wrong.
45 BIT*(POS): Constant with just 1 bit set.
47 LSBIT*(OFFSET): Constant with just 1 bit set - LS bit is zero.
49 MSBIT*(OFFSET): Constant with just 1 bit set - MS bit is zero.
51 MASK*(FIRST, LAST): Constant with bits [FIRST .. LAST] set. The
52 <MACRO> (no size) version permits FIRST >= LAST and generates a
53 wrapped bit mask vis ([0..LAST] | [FIRST..LSB]).
55 LSMASK*(NR_BITS): Like MASK only NR least significant bits are set.
57 MSMASK*(NR_BITS): Like MASK only NR most significant bits are set.
59 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
62 LSMASKED*(VALUE, NR_BITS): Mask out all but the least significant
65 MSMASKED*(VALUE, NR_BITS): Mask out all but the most significant
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).
72 LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is
75 MSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - MS bit is
78 SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
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.
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.
89 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
90 natural size. If in 32bit mode, discard the high 32bits.
92 EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets
93 natural size. If in 64bit mode, sign extend the value.
95 ALIGN_*(VALUE): Round upwards the value so that it is aligned.
97 FLOOR_*(VALUE): Truncate the value so that it is aligned.
99 ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS left.
101 ROTR*(VALUE, NR_BITS): Return the value rotated by NR_BITS right.
103 SEXT*(VAL, SIGN_BIT): Treat SIGN_BIT as the sign, extend it.
105 Note: Only the BIT* and MASK* macros return a constant that can be
106 used in variable declarations.
111 /* compute the number of bits between START and STOP */
113 #if (WITH_TARGET_WORD_MSB == 0)
114 #define _MAKE_WIDTH(START, STOP) (STOP - START + 1)
116 #define _MAKE_WIDTH(START, STOP) (START - STOP + 1)
121 /* compute the number shifts required to move a bit between LSB (MSB)
124 #if (WITH_TARGET_WORD_MSB == 0)
125 #define _LSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
127 #define _LSB_SHIFT(WIDTH, POS) (POS)
130 #if (WITH_TARGET_WORD_MSB == 0)
131 #define _MSB_SHIFT(WIDTH, POS) (POS)
133 #define _MSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
137 /* compute the absolute bit position given the OFFSET from the MSB(LSB)
138 NB: _MAKE_xxx_POS (WIDTH, _MAKE_xxx_SHIFT (WIDTH, POS)) == POS */
140 #if (WITH_TARGET_WORD_MSB == 0)
141 #define _MSB_POS(WIDTH, SHIFT) (SHIFT)
143 #define _MSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
146 #if (WITH_TARGET_WORD_MSB == 0)
147 #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
149 #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
153 /* convert a 64 bit position into a corresponding 32bit position. MSB
154 pos handles the posibility that the bit lies beyond the 32bit
157 #if (WITH_TARGET_WORD_MSB == 0)
158 #define _MSB_32(START, STOP) (START <= STOP \
159 ? (START < 32 ? 0 : START - 32) \
160 : (STOP < 32 ? 0 : STOP - 32))
162 #define _MSB_32(START, STOP) (START >= STOP \
163 ? (START >= 32 ? 31 : START) \
164 : (STOP >= 32 ? 31 : STOP))
167 #if (WITH_TARGET_WORD_MSB == 0)
168 #define _LSB_32(START, STOP) (START <= STOP \
169 ? (STOP < 32 ? 0 : STOP - 32) \
170 : (START < 32 ? 0 : START - 32))
172 #define _LSB_32(START, STOP) (START >= STOP \
173 ? (STOP >= 32 ? 31 : STOP) \
174 : (START >= 32 ? 31 : START))
177 #if (WITH_TARGET_WORD_MSB == 0)
178 #define _MSB(START, STOP) (START <= STOP ? START : STOP)
180 #define _MSB(START, STOP) (START >= STOP ? START : STOP)
183 #if (WITH_TARGET_WORD_MSB == 0)
184 #define _LSB(START, STOP) (START <= STOP ? STOP : START)
186 #define _LSB(START, STOP) (START >= STOP ? STOP : START)
192 #define _BITn(WIDTH, POS) ((natural##WIDTH)1 \
193 << _LSB_SHIFT (WIDTH, POS))
195 #define BIT4(POS) (1 << _LSB_SHIFT (4, (POS)))
196 #define BIT5(POS) (1 << _LSB_SHIFT (5, (POS)))
197 #define BIT8(POS) (1 << _LSB_SHIFT (8, (POS)))
198 #define BIT10(POS) (1 << _LSB_SHIFT (10, (POS)))
199 #define BIT16(POS) _BITn (16, (POS))
200 #define BIT32(POS) _BITn (32, (POS))
201 #define BIT64(POS) _BITn (64, (POS))
203 #if (WITH_TARGET_WORD_BITSIZE == 64)
204 #define BIT(POS) BIT64(POS)
206 #if (WITH_TARGET_WORD_BITSIZE == 32)
207 #if (WITH_TARGET_WORD_MSB == 0)
208 #define BIT(POS) ((POS) < 32 \
210 : (1 << ((POS) < 32 ? 0 : _LSB_SHIFT(64, (POS)))))
212 #define BIT(POS) ((POS) >= 32 \
214 : (1 << ((POS) >= 32 ? 0 : (POS))))
218 #error "BIT never defined"
222 /* LS/MS Bit operations */
224 #define LSBIT8(POS) ((unsigned8)1 << (POS))
225 #define LSBIT16(POS) ((unsigned16)1 << (POS))
226 #define LSBIT32(POS) ((unsigned32)1 << (POS))
227 #define LSBIT64(POS) ((unsigned64)1 << (POS))
228 #define LSBIT(POS) ((unsigned_word)1 << (POS))
230 #define MSBIT8(POS) ((unsigned8)1 << (8 - 1 - (POS)))
231 #define MSBIT16(POS) ((unsigned16)1 << (16 - 1 - (POS)))
232 #define MSBIT32(POS) ((unsigned32)1 << (32 - 1 - (POS)))
233 #define MSBIT64(POS) ((unsigned64)1 << (64 - 1 - (POS)))
234 #define MSBIT(POS) ((unsigned_word)1 << (WITH_TARGET_WORD_BITSIZE - 1 - (POS)))
240 /* 111111 -> mmll11 -> mm11ll */
241 #define _MASKn(WIDTH, START, STOP) (((unsigned##WIDTH)(-1) \
242 >> (_MSB_SHIFT (WIDTH, START) \
243 + _LSB_SHIFT (WIDTH, STOP))) \
244 << _LSB_SHIFT (WIDTH, STOP))
246 #define MASK16(START, STOP) _MASKn(16, (START), (STOP))
247 #define MASK32(START, STOP) _MASKn(32, (START), (STOP))
248 #define MASK64(START, STOP) _MASKn(64, (START), (STOP))
250 #if (WITH_TARGET_WORD_MSB == 0)
251 #define _POS_LE(START, STOP) (START <= STOP)
253 #define _POS_LE(START, STOP) (STOP <= START)
256 #if (WITH_TARGET_WORD_BITSIZE == 64)
257 #define MASK(START, STOP) \
258 (_POS_LE ((START), (STOP)) \
260 _MSB ((START), (STOP)), \
261 _LSB ((START), (STOP)) ) \
262 : (_MASKn(64, _MSB_POS (64, 0), (STOP)) \
263 | _MASKn(64, (START), _LSB_POS (64, 0))))
265 #if (WITH_TARGET_WORD_BITSIZE == 32)
266 #define MASK(START, STOP) \
267 (_POS_LE ((START), (STOP)) \
268 ? (_POS_LE ((STOP), _MSB_POS (64, 31)) \
271 _MSB_32 ((START), (STOP)), \
272 _LSB_32 ((START), (STOP)))) \
274 _LSB_32 ((START), (STOP)), \
276 | (_POS_LE ((STOP), _MSB_POS (64, 31)) \
280 _MSB_32 ((START), (STOP))))))
283 #error "MASK never undefined"
288 /* Multi-bit mask on least significant bits */
290 #if (WITH_TARGET_WORD_MSB == 0)
291 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (WIDTH - NR_BITS), (WIDTH - 1))
293 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (NR_BITS - 1), 0)
296 #define LSMASK16(NR_BITS) _LSMASKn (16, (NR_BITS))
297 #define LSMASK32(NR_BITS) _LSMASKn (32, (NR_BITS))
298 #define LSMASK64(NR_BITS) _LSMASKn (64, (NR_BITS))
300 #if (WITH_TARGET_WORD_BITSIZE == 64)
301 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
309 #if (WITH_TARGET_WORD_BITSIZE == 32)
310 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
314 ((NR_BITS) > 32 ? 31 \
315 : (NR_BITS) < 1 ? 0 \
316 : ((NR_BITS) - 1))), \
319 #if !defined (LSMASK)
320 #error "LSMASK never defined"
324 /* Multi-bit mask on most significant bits */
326 #if (WITH_TARGET_WORD_MSB == 0)
327 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, 0, (NR_BITS - 1))
329 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, (WIDTH - 1), (WIDTH - NR_BITS))
332 #define MSMASK16(NR_BITS) _MSMASKn (16, (NR_BITS))
333 #define MSMASK32(NR_BITS) _MSMASKn (32, (NR_BITS))
334 #define MSMASK64(NR_BITS) _MSMASKn (64, (NR_BITS))
336 #if (WITH_TARGET_WORD_BITSIZE == 64)
337 #define MSMASK(NR_BITS) (NR_BITS < 1 \
345 #if (WITH_TARGET_WORD_BITSIZE == 32)
346 #define MSMASK(NR_BITS) (NR_BITS <= 32 \
351 ((NR_BITS) <= 32 ? 0 \
354 #if !defined (MSMASK)
355 #error "MSMASK never defined"
359 /* mask the required bits, leaving them in place */
361 INLINE_SIM_BITS(unsigned16) MASKED16 (unsigned16 word, unsigned start, unsigned stop);
362 INLINE_SIM_BITS(unsigned32) MASKED32 (unsigned32 word, unsigned start, unsigned stop);
363 INLINE_SIM_BITS(unsigned64) MASKED64 (unsigned64 word, unsigned start, unsigned stop);
365 INLINE_SIM_BITS(unsigned_word) MASKED (unsigned_word word, unsigned start, unsigned stop);
368 /* Ditto but nr of ls-bits specified */
370 INLINE_SIM_BITS(unsigned16) LSMASKED16 (unsigned16 word, unsigned nr_bits);
371 INLINE_SIM_BITS(unsigned32) LSMASKED32 (unsigned32 word, unsigned nr_bits);
372 INLINE_SIM_BITS(unsigned64) LSMASKED64 (unsigned64 word, unsigned nr_bits);
374 INLINE_SIM_BITS(unsigned_word) LSMASKED (unsigned_word word, unsigned nr_bits);
377 /* Ditto but nr of ms-bits specified */
379 INLINE_SIM_BITS(unsigned16) MSMASKED16 (unsigned16 word, unsigned nr_bits);
380 INLINE_SIM_BITS(unsigned32) MSMASKED32 (unsigned32 word, unsigned nr_bits);
381 INLINE_SIM_BITS(unsigned64) MSMASKED64 (unsigned64 word, unsigned nr_bits);
383 INLINE_SIM_BITS(unsigned_word) MSMASKED (unsigned_word word, unsigned nr_bits);
387 /* extract the required bits aligning them with the lsb */
389 INLINE_SIM_BITS(unsigned16) LSEXTRACTED16 (unsigned16 val, unsigned start, unsigned stop);
390 INLINE_SIM_BITS(unsigned32) LSEXTRACTED32 (unsigned32 val, unsigned start, unsigned stop);
391 INLINE_SIM_BITS(unsigned64) LSEXTRACTED64 (unsigned64 val, unsigned start, unsigned stop);
393 INLINE_SIM_BITS(unsigned16) MSEXTRACTED16 (unsigned16 val, unsigned start, unsigned stop);
394 INLINE_SIM_BITS(unsigned32) MSEXTRACTED32 (unsigned32 val, unsigned start, unsigned stop);
395 INLINE_SIM_BITS(unsigned64) MSEXTRACTED64 (unsigned64 val, unsigned start, unsigned stop);
397 #if (WITH_TARGET_WORD_MSB == 0)
398 #define EXTRACTED16 MSEXTRACTED32
399 #define EXTRACTED32 MSEXTRACTED32
400 #define EXTRACTED64 MSEXTRACTED32
402 #define EXTRACTED16 LSEXTRACTED32
403 #define EXTRACTED32 LSEXTRACTED32
404 #define EXTRACTED64 LSEXTRACTED32
410 INLINE_SIM_BITS(unsigned_word) EXTRACTED (unsigned_word val, unsigned start, unsigned stop);
414 /* move a single bit around */
415 /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
416 #define _SHUFFLEDn(N, WORD, OLD, NEW) \
418 ? (((unsigned##N)(WORD) \
419 >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
420 & MASK32((NEW), (NEW))) \
421 : (((unsigned##N)(WORD) \
422 << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
423 & MASK32((NEW), (NEW))))
425 #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn (32, WORD, OLD, NEW)
426 #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn (64, WORD, OLD, NEW)
428 #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn (_word, WORD, OLD, NEW)
431 /* move a group of bits around */
433 INLINE_SIM_BITS(unsigned16) INSERTED16 (unsigned16 val, unsigned start, unsigned stop);
434 INLINE_SIM_BITS(unsigned32) INSERTED32 (unsigned32 val, unsigned start, unsigned stop);
435 INLINE_SIM_BITS(unsigned64) INSERTED64 (unsigned64 val, unsigned start, unsigned stop);
437 INLINE_SIM_BITS(unsigned_word) INSERTED (unsigned_word val, unsigned start, unsigned stop);
441 /* depending on MODE return a 64bit or 32bit (sign extended) value */
442 #if (WITH_TARGET_WORD_BITSIZE == 64)
443 #define EXTENDED(X) ((signed64)(signed32)(X))
445 #if (WITH_TARGET_WORD_BITSIZE == 32)
446 #define EXTENDED(X) (X)
450 /* memory alignment macro's */
451 #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1))
452 #define _FLOORa(A,X) ((X) & ~((A) - 1))
454 #define ALIGN_8(X) _ALIGNa (8, X)
455 #define ALIGN_16(X) _ALIGNa (16, X)
457 #define ALIGN_PAGE(X) _ALIGNa (0x1000, X)
458 #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
461 /* bit bliting macro's */
462 #define BLIT32(V, POS, BIT) \
469 #define MBLIT32(V, LO, HI, VAL) \
471 (V) = (((V) & ~MASK32 ((LO), (HI))) \
472 | INSERTED32 (VAL, LO, HI)); \
477 /* some rotate functions. The generic macro's ROT, ROTL, ROTR are
478 intentionally omited. */
481 INLINE_SIM_BITS(unsigned16) ROT16 (unsigned16 val, int shift);
482 INLINE_SIM_BITS(unsigned32) ROT32 (unsigned32 val, int shift);
483 INLINE_SIM_BITS(unsigned64) ROT64 (unsigned64 val, int shift);
486 INLINE_SIM_BITS(unsigned16) ROTL16 (unsigned16 val, unsigned shift);
487 INLINE_SIM_BITS(unsigned32) ROTL32 (unsigned32 val, unsigned shift);
488 INLINE_SIM_BITS(unsigned64) ROTL64 (unsigned64 val, unsigned shift);
491 INLINE_SIM_BITS(unsigned16) ROTR16 (unsigned16 val, unsigned shift);
492 INLINE_SIM_BITS(unsigned32) ROTR32 (unsigned32 val, unsigned shift);
493 INLINE_SIM_BITS(unsigned64) ROTR64 (unsigned64 val, unsigned shift);
497 /* Sign extension operations */
499 INLINE_SIM_BITS(unsigned16) SEXT16 (signed16 val, unsigned sign_bit);
500 INLINE_SIM_BITS(unsigned32) SEXT32 (signed32 val, unsigned sign_bit);
501 INLINE_SIM_BITS(unsigned64) SEXT64 (signed64 val, unsigned sign_bit);
503 INLINE_SIM_BITS(unsigned_word) SEXT (signed_word val, unsigned sign_bit);
507 #if ((SIM_BITS_INLINE & INCLUDE_MODULE) && (SIM_BITS_INLINE & INCLUDED_BY_MODULE))
508 #include "sim-bits.c"
511 #endif /* _SIM_BITS_H_ */