1 /* This file is part of the program psim.
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
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 BIT*(POS): Constant with just 1 bit set.
44 LSBIT*(OFFSET): Constant with just 1 bit set - LS bit is zero.
46 MSBIT*(OFFSET): Constant with just 1 bit set - MS bit is zero.
48 MASK*(FIRST, LAST): Constant with bits [FIRST .. LAST] set. The
49 <MACRO> (no size) version permits FIRST >= LAST and generates a
50 wrapped bit mask vis ([0..LAST] | [FIRST..LSB]).
52 LSMASK*(NR_BITS): Like MASK only NR least significant bits are set.
54 MSMASK*(NR_BITS): Like MASK only NR most significant bits are set.
56 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
59 LSMASKED*(VALUE, NR_BITS): Mask out all but the least significant
62 MSMASKED*(VALUE, NR_BITS): Mask out all but the most significant
65 EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but
66 also right shifts the masked value so that bit LAST becomes the
67 least significant (right most).
69 SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
72 MOVED**(VALUE, OLD_FIRST, OLD_LAST, NEW_FIRST, NEW_LAST): Moves
73 things around so that bits OLD_FIRST..OLD_LAST are masked then
74 moved to NEW_FIRST..NEW_LAST.
76 INSERTED*(VALUE, FIRST, LAST): Takes VALUE and `inserts' the (LAST
77 - FIRST + 1) least significant bits into bit positions [ FIRST
78 .. LAST ]. This is almost the complement to EXTRACTED.
80 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
81 natural size. If in 32bit mode, discard the high 32bits.
83 EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets
84 natural size. If in 64bit mode, sign extend the value.
86 ALIGN_*(VALUE): Round upwards the value so that it is aligned.
88 FLOOR_*(VALUE): Truncate the value so that it is aligned.
90 ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS left.
92 ROTR*(VALUE, NR_BITS): Return the value rotated by NR_BITS right.
94 SEXT*(VAL, SIGN_BIT): Treat SIGN_BIT as the sign, extend it.
96 Note: Only the BIT* and MASK* macros return a constant that can be
97 used in variable declarations.
102 /* compute the number of bits between START and STOP */
104 #if (WITH_TARGET_WORD_MSB == 0)
105 #define _MAKE_WIDTH(START, STOP) (STOP - START + 1)
107 #define _MAKE_WIDTH(START, STOP) (START - STOP + 1)
112 /* compute the number shifts required to move a bit between LSB (MSB)
115 #if (WITH_TARGET_WORD_MSB == 0)
116 #define _LSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
118 #define _LSB_SHIFT(WIDTH, POS) (POS)
121 #if (WITH_TARGET_WORD_MSB == 0)
122 #define _MSB_SHIFT(WIDTH, POS) (POS)
124 #define _MSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
128 /* compute the absolute bit position given the OFFSET from the MSB(LSB)
129 NB: _MAKE_xxx_POS (WIDTH, _MAKE_xxx_SHIFT (WIDTH, POS)) == POS */
131 #if (WITH_TARGET_WORD_MSB == 0)
132 #define _MSB_POS(WIDTH, SHIFT) (SHIFT)
134 #define _MSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
137 #if (WITH_TARGET_WORD_MSB == 0)
138 #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
140 #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
144 /* convert a 64 bit position into a corresponding 32bit position. MSB
145 pos handles the posibility that the bit lies beyond the 32bit
148 #if (WITH_TARGET_WORD_MSB == 0)
149 #define _MSB_32(START, STOP) (START <= STOP \
150 ? (START < 32 ? 0 : START - 32) \
151 : (STOP < 32 ? 0 : STOP - 32))
153 #define _MSB_32(START, STOP) (START >= STOP \
154 ? (START >= 32 ? 31 : START) \
155 : (STOP >= 32 ? 31 : STOP))
158 #if (WITH_TARGET_WORD_MSB == 0)
159 #define _LSB_32(START, STOP) (START <= STOP \
160 ? (STOP < 32 ? 0 : STOP - 32) \
161 : (START < 32 ? 0 : START - 32))
163 #define _LSB_32(START, STOP) (START >= STOP \
164 ? (STOP >= 32 ? 31 : STOP) \
165 : (START >= 32 ? 31 : START))
168 #if (WITH_TARGET_WORD_MSB == 0)
169 #define _MSB(START, STOP) (START <= STOP ? START : STOP)
171 #define _MSB(START, STOP) (START >= STOP ? START : STOP)
174 #if (WITH_TARGET_WORD_MSB == 0)
175 #define _LSB(START, STOP) (START <= STOP ? STOP : START)
177 #define _LSB(START, STOP) (START >= STOP ? STOP : START)
183 #define _BITn(WIDTH, POS) ((natural##WIDTH)1 \
184 << _LSB_SHIFT (WIDTH, POS))
186 #define BIT4(POS) (1 << _LSB_SHIFT (4, (POS)))
187 #define BIT5(POS) (1 << _LSB_SHIFT (5, (POS)))
188 #define BIT8(POS) (1 << _LSB_SHIFT (8, (POS)))
189 #define BIT10(POS) (1 << _LSB_SHIFT (10, (POS)))
190 #define BIT16(POS) _BITn (16, (POS))
191 #define BIT32(POS) _BITn (32, (POS))
192 #define BIT64(POS) _BITn (64, (POS))
194 #if (WITH_TARGET_WORD_BITSIZE == 64)
195 #define BIT(POS) BIT64(POS)
197 #if (WITH_TARGET_WORD_BITSIZE == 32)
198 #if (WITH_TARGET_WORD_MSB == 0)
199 #define BIT(POS) ((POS) < 32 \
201 : (1 << ((POS) < 32 ? 0 : _LSB_SHIFT(64, (POS)))))
203 #define BIT(POS) ((POS) >= 32 \
205 : (1 << ((POS) >= 32 ? 0 : (POS))))
209 #error "BIT never defined"
213 /* LS/MS Bit operations */
215 #define LSBIT8(POS) ((unsigned8)1 << (POS))
216 #define LSBIT16(POS) ((unsigned16)1 << (POS))
217 #define LSBIT32(POS) ((unsigned32)1 << (POS))
218 #define LSBIT64(POS) ((unsigned64)1 << (POS))
219 #define LSBIT(POS) ((unsigned_word)1 << (POS))
221 #define MSBIT8(POS) ((unsigned8)1 << (8 - 1 - (POS)))
222 #define MSBIT16(POS) ((unsigned16)1 << (16 - 1 - (POS)))
223 #define MSBIT32(POS) ((unsigned32)1 << (32 - 1 - (POS)))
224 #define MSBIT64(POS) ((unsigned64)1 << (64 - 1 - (POS)))
225 #define MSBIT(POS) ((unsigned_word)1 << (WITH_TARGET_WORD_BITSIZE - 1 - (POS)))
231 /* 111111 -> mmll11 -> mm11ll */
232 #define _MASKn(WIDTH, START, STOP) (((unsigned##WIDTH)(-1) \
233 >> (_MSB_SHIFT (WIDTH, START) \
234 + _LSB_SHIFT (WIDTH, STOP))) \
235 << _LSB_SHIFT (WIDTH, STOP))
237 #define MASK16(START, STOP) _MASKn(16, (START), (STOP))
238 #define MASK32(START, STOP) _MASKn(32, (START), (STOP))
239 #define MASK64(START, STOP) _MASKn(64, (START), (STOP))
241 #if (WITH_TARGET_WORD_MSB == 0)
242 #define _POS_LE(START, STOP) (START <= STOP)
244 #define _POS_LE(START, STOP) (STOP <= START)
247 #if (WITH_TARGET_WORD_BITSIZE == 64)
248 #define MASK(START, STOP) \
249 (_POS_LE ((START), (STOP)) \
251 _MSB ((START), (STOP)), \
252 _LSB ((START), (STOP)) ) \
253 : (_MASKn(64, _MSB_POS (64, 0), (STOP)) \
254 | _MASKn(64, (START), _LSB_POS (64, 0))))
256 #if (WITH_TARGET_WORD_BITSIZE == 32)
257 #define MASK(START, STOP) \
258 (_POS_LE ((START), (STOP)) \
259 ? (_POS_LE ((STOP), _MSB_POS (64, 31)) \
262 _MSB_32 ((START), (STOP)), \
263 _LSB_32 ((START), (STOP)))) \
265 _LSB_32 ((START), (STOP)), \
267 | (_POS_LE ((STOP), _MSB_POS (64, 31)) \
271 _MSB_32 ((START), (STOP))))))
274 #error "MASK never undefined"
279 /* Multi-bit mask on least significant bits */
281 #if (WITH_TARGET_WORD_MSB == 0)
282 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (WIDTH - NR_BITS), (WIDTH - 1))
284 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (NR_BITS - 1), 0)
287 #define LSMASK16(NR_BITS) _LSMASKn (16, (NR_BITS))
288 #define LSMASK32(NR_BITS) _LSMASKn (32, (NR_BITS))
289 #define LSMASK64(NR_BITS) _LSMASKn (64, (NR_BITS))
291 #if (WITH_TARGET_WORD_BITSIZE == 64)
292 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
300 #if (WITH_TARGET_WORD_BITSIZE == 32)
301 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
305 ((NR_BITS) > 32 ? 31 \
306 : (NR_BITS) < 1 ? 0 \
307 : ((NR_BITS) - 1))), \
310 #if !defined (LSMASK)
311 #error "LSMASK never defined"
315 /* Multi-bit mask on most significant bits */
317 #if (WITH_TARGET_WORD_MSB == 0)
318 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, 0, (NR_BITS - 1))
320 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, (WIDTH - 1), (WIDTH - NR_BITS))
323 #define MSMASK16(NR_BITS) _MSMASKn (16, (NR_BITS))
324 #define MSMASK32(NR_BITS) _MSMASKn (32, (NR_BITS))
325 #define MSMASK64(NR_BITS) _MSMASKn (64, (NR_BITS))
327 #if (WITH_TARGET_WORD_BITSIZE == 64)
328 #define MSMASK(NR_BITS) (NR_BITS < 1 \
336 #if (WITH_TARGET_WORD_BITSIZE == 32)
337 #define MSMASK(NR_BITS) (NR_BITS <= 32 \
342 ((NR_BITS) <= 32 ? 0 \
345 #if !defined (MSMASK)
346 #error "MSMASK never defined"
350 /* mask the required bits, leaving them in place */
352 INLINE_SIM_BITS(unsigned16) MASKED16 (unsigned16 word, unsigned start, unsigned stop);
353 INLINE_SIM_BITS(unsigned32) MASKED32 (unsigned32 word, unsigned start, unsigned stop);
354 INLINE_SIM_BITS(unsigned64) MASKED64 (unsigned64 word, unsigned start, unsigned stop);
356 INLINE_SIM_BITS(unsigned_word) MASKED (unsigned_word word, unsigned start, unsigned stop);
359 /* Ditto but nr of ls-bits specified */
361 INLINE_SIM_BITS(unsigned16) LSMASKED16 (unsigned16 word, unsigned nr_bits);
362 INLINE_SIM_BITS(unsigned32) LSMASKED32 (unsigned32 word, unsigned nr_bits);
363 INLINE_SIM_BITS(unsigned64) LSMASKED64 (unsigned64 word, unsigned nr_bits);
365 INLINE_SIM_BITS(unsigned_word) LSMASKED (unsigned_word word, unsigned nr_bits);
368 /* Ditto but nr of ms-bits specified */
370 INLINE_SIM_BITS(unsigned16) MSMASKED16 (unsigned16 word, unsigned nr_bits);
371 INLINE_SIM_BITS(unsigned32) MSMASKED32 (unsigned32 word, unsigned nr_bits);
372 INLINE_SIM_BITS(unsigned64) MSMASKED64 (unsigned64 word, unsigned nr_bits);
374 INLINE_SIM_BITS(unsigned_word) MSMASKED (unsigned_word word, unsigned nr_bits);
378 /* extract the required bits aligning them with the lsb */
380 INLINE_SIM_BITS(unsigned16) EXTRACTED16 (unsigned16 val, unsigned start, unsigned stop);
381 INLINE_SIM_BITS(unsigned32) EXTRACTED32 (unsigned32 val, unsigned start, unsigned stop);
382 INLINE_SIM_BITS(unsigned64) EXTRACTED64 (unsigned64 val, unsigned start, unsigned stop);
384 INLINE_SIM_BITS(unsigned_word) EXTRACTED (unsigned_word val, unsigned start, unsigned stop);
388 /* move a single bit around */
389 /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
390 #define _SHUFFLEDn(N, WORD, OLD, NEW) \
392 ? (((unsigned##N)(WORD) \
393 >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
394 & MASK32((NEW), (NEW))) \
395 : (((unsigned##N)(WORD) \
396 << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
397 & MASK32((NEW), (NEW))))
399 #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn (32, WORD, OLD, NEW)
400 #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn (64, WORD, OLD, NEW)
402 #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn (_word, WORD, OLD, NEW)
405 /* move a group of bits around */
407 INLINE_SIM_BITS(unsigned16) INSERTED16 (unsigned16 val, unsigned start, unsigned stop);
408 INLINE_SIM_BITS(unsigned32) INSERTED32 (unsigned32 val, unsigned start, unsigned stop);
409 INLINE_SIM_BITS(unsigned64) INSERTED64 (unsigned64 val, unsigned start, unsigned stop);
411 INLINE_SIM_BITS(unsigned_word) INSERTED (unsigned_word val, unsigned start, unsigned stop);
415 /* depending on MODE return a 64bit or 32bit (sign extended) value */
416 #if (WITH_TARGET_WORD_BITSIZE == 64)
417 #define EXTENDED(X) ((signed64)(signed32)(X))
419 #if (WITH_TARGET_WORD_BITSIZE == 32)
420 #define EXTENDED(X) (X)
424 /* memory alignment macro's */
425 #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1))
426 #define _FLOORa(A,X) ((X) & ~((A) - 1))
428 #define ALIGN_8(X) _ALIGNa (8, X)
429 #define ALIGN_16(X) _ALIGNa (16, X)
431 #define ALIGN_PAGE(X) _ALIGNa (0x1000, X)
432 #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
435 /* bit bliting macro's */
436 #define BLIT32(V, POS, BIT) \
443 #define MBLIT32(V, LO, HI, VAL) \
445 (V) = (((V) & ~MASK32 ((LO), (HI))) \
446 | INSERTED32 (VAL, LO, HI)); \
451 /* some rotate functions. The generic macro's ROT, ROTL, ROTR are
452 intentionally omited. */
455 INLINE_SIM_BITS(unsigned16) ROT16 (unsigned16 val, int shift);
456 INLINE_SIM_BITS(unsigned32) ROT32 (unsigned32 val, int shift);
457 INLINE_SIM_BITS(unsigned64) ROT64 (unsigned64 val, int shift);
460 INLINE_SIM_BITS(unsigned16) ROTL16 (unsigned16 val, unsigned shift);
461 INLINE_SIM_BITS(unsigned32) ROTL32 (unsigned32 val, unsigned shift);
462 INLINE_SIM_BITS(unsigned64) ROTL64 (unsigned64 val, unsigned shift);
465 INLINE_SIM_BITS(unsigned16) ROTR16 (unsigned16 val, unsigned shift);
466 INLINE_SIM_BITS(unsigned32) ROTR32 (unsigned32 val, unsigned shift);
467 INLINE_SIM_BITS(unsigned64) ROTR64 (unsigned64 val, unsigned shift);
471 /* Sign extension operations */
473 INLINE_SIM_BITS(unsigned16) SEXT16 (signed16 val, unsigned sign_bit);
474 INLINE_SIM_BITS(unsigned32) SEXT32 (signed32 val, unsigned sign_bit);
475 INLINE_SIM_BITS(unsigned64) SEXT64 (signed64 val, unsigned sign_bit);
477 INLINE_SIM_BITS(unsigned_word) SEXT (signed_word val, unsigned sign_bit);
481 #if ((SIM_BITS_INLINE & INCLUDE_MODULE) && (SIM_BITS_INLINE & INCLUDED_BY_MODULE))
482 #include "sim-bits.c"
485 #endif /* _SIM_BITS_H_ */