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*(FIRST, LAST): Like MASK - LS bit is zero.
57 MSMASK*(FIRST, LAST): Like MASK - LS bit is zero.
59 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
62 LSMASKED*(VALUE, FIRST, LAST): Like MASKED - LS bit is zero.
64 MSMASKED*(VALUE, FIRST, LAST): Like MASKED - MS bit is zero.
66 EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but
67 also right shifts the masked value so that bit LAST becomes the
68 least significant (right most).
70 LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is
73 MSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - MS bit is
76 SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
79 MOVED**(VALUE, OLD_FIRST, OLD_LAST, NEW_FIRST, NEW_LAST): Moves
80 things around so that bits OLD_FIRST..OLD_LAST are masked then
81 moved to NEW_FIRST..NEW_LAST.
83 INSERTED*(VALUE, FIRST, LAST): Takes VALUE and `inserts' the (LAST
84 - FIRST + 1) least significant bits into bit positions [ FIRST
85 .. LAST ]. This is almost the complement to EXTRACTED.
87 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
88 natural size. If in 32bit mode, discard the high 32bits.
90 EXTENDED*(VALUE): Convert the `*' bit value to the targets natural
91 word size. Sign extned the value if needed.
93 ALIGN_*(VALUE): Round upwards the value so that it is aligned.
95 FLOOR_*(VALUE): Truncate the value so that it is aligned.
97 ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS left.
99 ROTR*(VALUE, NR_BITS): Return the value rotated by NR_BITS right.
101 SEXT*(VAL, SIGN_BIT): Treat SIGN_BIT as the sign, extend it.
103 Note: Only the BIT* and MASK* macros return a constant that can be
104 used in variable declarations.
109 /* compute the number of bits between START and STOP */
111 #if (WITH_TARGET_WORD_MSB == 0)
112 #define _MAKE_WIDTH(START, STOP) (STOP - START + 1)
114 #define _MAKE_WIDTH(START, STOP) (START - STOP + 1)
119 /* compute the number shifts required to move a bit between LSB (MSB)
122 #if (WITH_TARGET_WORD_MSB == 0)
123 #define _LSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
125 #define _LSB_SHIFT(WIDTH, POS) (POS)
128 #if (WITH_TARGET_WORD_MSB == 0)
129 #define _MSB_SHIFT(WIDTH, POS) (POS)
131 #define _MSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
135 /* compute the absolute bit position given the OFFSET from the MSB(LSB)
136 NB: _MAKE_xxx_POS (WIDTH, _MAKE_xxx_SHIFT (WIDTH, POS)) == POS */
138 #if (WITH_TARGET_WORD_MSB == 0)
139 #define _MSB_POS(WIDTH, SHIFT) (SHIFT)
141 #define _MSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
144 #if (WITH_TARGET_WORD_MSB == 0)
145 #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
147 #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
151 /* convert a 64 bit position into a corresponding 32bit position. MSB
152 pos handles the posibility that the bit lies beyond the 32bit
155 #if (WITH_TARGET_WORD_MSB == 0)
156 #define _MSB_32(START, STOP) (START <= STOP \
157 ? (START < 32 ? 0 : START - 32) \
158 : (STOP < 32 ? 0 : STOP - 32))
160 #define _MSB_32(START, STOP) (START >= STOP \
161 ? (START >= 32 ? 31 : START) \
162 : (STOP >= 32 ? 31 : STOP))
165 #if (WITH_TARGET_WORD_MSB == 0)
166 #define _LSB_32(START, STOP) (START <= STOP \
167 ? (STOP < 32 ? 0 : STOP - 32) \
168 : (START < 32 ? 0 : START - 32))
170 #define _LSB_32(START, STOP) (START >= STOP \
171 ? (STOP >= 32 ? 31 : STOP) \
172 : (START >= 32 ? 31 : START))
175 #if (WITH_TARGET_WORD_MSB == 0)
176 #define _MSB(START, STOP) (START <= STOP ? START : STOP)
178 #define _MSB(START, STOP) (START >= STOP ? START : STOP)
181 #if (WITH_TARGET_WORD_MSB == 0)
182 #define _LSB(START, STOP) (START <= STOP ? STOP : START)
184 #define _LSB(START, STOP) (START >= STOP ? STOP : START)
188 /* LS/MS Bit operations */
190 #define LSBIT8(POS) ((unsigned8)1 << (POS))
191 #define LSBIT16(POS) ((unsigned16)1 << (POS))
192 #define LSBIT32(POS) ((unsigned32)1 << (POS))
193 #define LSBIT64(POS) ((unsigned64)1 << (POS))
194 #define LSBIT(POS) ((unsigned_word)1 << (POS))
196 #define MSBIT8(POS) ((unsigned8)1 << (8 - 1 - (POS)))
197 #define MSBIT16(POS) ((unsigned16)1 << (16 - 1 - (POS)))
198 #define MSBIT32(POS) ((unsigned32)1 << (32 - 1 - (POS)))
199 #define MSBIT64(POS) ((unsigned64)1 << (64 - 1 - (POS)))
200 #define MSBIT(POS) ((unsigned_word)1 << (WITH_TARGET_WORD_BITSIZE - 1 - (POS)))
205 #define _BITn(WIDTH, POS) ((natural##WIDTH)1 \
206 << _LSB_SHIFT (WIDTH, POS))
208 #define BIT4(POS) (1 << _LSB_SHIFT (4, (POS)))
209 #define BIT5(POS) (1 << _LSB_SHIFT (5, (POS)))
210 #define BIT8(POS) (1 << _LSB_SHIFT (8, (POS)))
211 #define BIT10(POS) (1 << _LSB_SHIFT (10, (POS)))
212 #define BIT16(POS) _BITn (16, (POS))
213 #define BIT32(POS) _BITn (32, (POS))
214 #define BIT64(POS) _BITn (64, (POS))
216 #if (WITH_TARGET_WORD_BITSIZE == 64)
217 #define BIT(POS) BIT64(POS)
219 #if (WITH_TARGET_WORD_BITSIZE == 32)
220 #if (WITH_TARGET_WORD_MSB == 0)
221 #define BIT(POS) ((POS) < 32 \
223 : (1 << ((POS) < 32 ? 0 : _LSB_SHIFT(64, (POS)))))
225 #define BIT(POS) ((POS) >= 32 \
227 : (1 << ((POS) >= 32 ? 0 : (POS))))
231 #error "BIT never defined"
237 /* 111111 -> mmll11 -> mm11ll */
238 #define _MASKn(WIDTH, START, STOP) (((unsigned##WIDTH)(-1) \
239 >> (_MSB_SHIFT (WIDTH, START) \
240 + _LSB_SHIFT (WIDTH, STOP))) \
241 << _LSB_SHIFT (WIDTH, STOP))
243 #if (WITH_TARGET_WORD_MSB == 0)
244 #define _POS_LE(START, STOP) (START <= STOP)
246 #define _POS_LE(START, STOP) (STOP <= START)
249 #if (WITH_TARGET_WORD_BITSIZE == 64)
250 #define MASK(START, STOP) \
251 (_POS_LE ((START), (STOP)) \
253 _MSB ((START), (STOP)), \
254 _LSB ((START), (STOP)) ) \
255 : (_MASKn(64, _MSB_POS (64, 0), (STOP)) \
256 | _MASKn(64, (START), _LSB_POS (64, 0))))
258 #if (WITH_TARGET_WORD_BITSIZE == 32)
259 #define MASK(START, STOP) \
260 (_POS_LE ((START), (STOP)) \
261 ? (_POS_LE ((STOP), _MSB_POS (64, 31)) \
264 _MSB_32 ((START), (STOP)), \
265 _LSB_32 ((START), (STOP)))) \
267 _LSB_32 ((START), (STOP)), \
269 | (_POS_LE ((STOP), _MSB_POS (64, 31)) \
273 _MSB_32 ((START), (STOP))))))
276 #error "MASK never undefined"
280 /* Multi-bit mask on least significant bits */
282 #define _LSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \
283 _LSB_POS (WIDTH, FIRST), \
284 _LSB_POS (WIDTH, LAST))
286 #define LSMASK16(FIRST, LAST) _LSMASKn (16, (FIRST), (LAST))
287 #define LSMASK32(FIRST, LAST) _LSMASKn (32, (FIRST), (LAST))
288 #define LSMASK64(FIRST, LAST) _LSMASKn (64, (FIRST), (LAST))
290 #define LSMASK(FIRST, LAST) (MASK (_LSB_POS (64, FIRST), _LSB_POS (64, LAST)))
293 /* Multi-bit mask on most significant bits */
295 #define _MSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \
296 _MSB_POS (WIDTH, FIRST), \
297 _MSB_POS (WIDTH, LAST))
299 #define MSMASK16(FIRST, LAST) _MSMASKn (16, (FIRST), (LAST))
300 #define MSMASK32(FIRST, LAST) _MSMASKn (32, (FIRST), (LAST))
301 #define MSMASK64(FIRST, LAST) _MSMASKn (64, (FIRST), (LAST))
303 #define MSMASK(FIRST, LAST) (MASK (_MSB_POS (64, FIRST), _MSB_POS (64, LAST)))
307 #if (WITH_TARGET_WORD_MSB == 0)
308 #define MASK16 MSMASK16
309 #define MASK32 MSMASK32
310 #define MASK64 MSMASK64
312 #define MASK16 LSMASK16
313 #define MASK32 LSMASK32
314 #define MASK64 LSMASK64
319 /* mask the required bits, leaving them in place */
321 INLINE_SIM_BITS(unsigned16) LSMASKED16 (unsigned16 word, int first, int last);
322 INLINE_SIM_BITS(unsigned32) LSMASKED32 (unsigned32 word, int first, int last);
323 INLINE_SIM_BITS(unsigned64) LSMASKED64 (unsigned64 word, int first, int last);
325 INLINE_SIM_BITS(unsigned_word) LSMASKED (unsigned_word word, int first, int last);
327 INLINE_SIM_BITS(unsigned16) MSMASKED16 (unsigned16 word, int first, int last);
328 INLINE_SIM_BITS(unsigned32) MSMASKED32 (unsigned32 word, int first, int last);
329 INLINE_SIM_BITS(unsigned64) MSMASKED64 (unsigned64 word, int first, int last);
331 INLINE_SIM_BITS(unsigned_word) MSMASKED (unsigned_word word, int first, int last);
333 #if (WITH_TARGET_WORD_MSB == 0)
334 #define MASKED16 MSMASKED16
335 #define MASKED32 MSMASKED32
336 #define MASKED64 MSMASKED64
337 #define MASKED MSMASKED
339 #define MASKED16 LSMASKED16
340 #define MASKED32 LSMASKED32
341 #define MASKED64 LSMASKED64
342 #define MASKED LSMASKED
347 /* extract the required bits aligning them with the lsb */
349 INLINE_SIM_BITS(unsigned16) LSEXTRACTED16 (unsigned16 val, int start, int stop);
350 INLINE_SIM_BITS(unsigned32) LSEXTRACTED32 (unsigned32 val, int start, int stop);
351 INLINE_SIM_BITS(unsigned64) LSEXTRACTED64 (unsigned64 val, int start, int stop);
353 INLINE_SIM_BITS(unsigned_word) LSEXTRACTED (unsigned_word val, int start, int stop);
355 INLINE_SIM_BITS(unsigned16) MSEXTRACTED16 (unsigned16 val, int start, int stop);
356 INLINE_SIM_BITS(unsigned32) MSEXTRACTED32 (unsigned32 val, int start, int stop);
357 INLINE_SIM_BITS(unsigned64) MSEXTRACTED64 (unsigned64 val, int start, int stop);
359 INLINE_SIM_BITS(unsigned_word) MSEXTRACTED (unsigned_word val, int start, int stop);
361 #if (WITH_TARGET_WORD_MSB == 0)
362 #define EXTRACTED16 MSEXTRACTED16
363 #define EXTRACTED32 MSEXTRACTED32
364 #define EXTRACTED64 MSEXTRACTED64
365 #define EXTRACTED MSEXTRACTED
367 #define EXTRACTED16 LSEXTRACTED16
368 #define EXTRACTED32 LSEXTRACTED32
369 #define EXTRACTED64 LSEXTRACTED64
370 #define EXTRACTED LSEXTRACTED
375 /* move a single bit around */
376 /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
377 #define _SHUFFLEDn(N, WORD, OLD, NEW) \
379 ? (((unsigned##N)(WORD) \
380 >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
381 & MASK32((NEW), (NEW))) \
382 : (((unsigned##N)(WORD) \
383 << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
384 & MASK32((NEW), (NEW))))
386 #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn (32, WORD, OLD, NEW)
387 #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn (64, WORD, OLD, NEW)
389 #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn (_word, WORD, OLD, NEW)
392 /* move a group of bits around */
394 INLINE_SIM_BITS(unsigned16) LSINSERTED16 (unsigned16 val, int start, int stop);
395 INLINE_SIM_BITS(unsigned32) LSINSERTED32 (unsigned32 val, int start, int stop);
396 INLINE_SIM_BITS(unsigned64) LSINSERTED64 (unsigned64 val, int start, int stop);
397 INLINE_SIM_BITS(unsigned_word) LSINSERTED (unsigned_word val, int start, int stop);
399 INLINE_SIM_BITS(unsigned16) MSINSERTED16 (unsigned16 val, int start, int stop);
400 INLINE_SIM_BITS(unsigned32) MSINSERTED32 (unsigned32 val, int start, int stop);
401 INLINE_SIM_BITS(unsigned64) MSINSERTED64 (unsigned64 val, int start, int stop);
402 INLINE_SIM_BITS(unsigned_word) MSINSERTED (unsigned_word val, int start, int stop);
404 #if (WITH_TARGET_WORD_MSB == 0)
405 #define INSERTED16 MSINSERTED16
406 #define INSERTED32 MSINSERTED32
407 #define INSERTED64 MSINSERTED64
408 #define INSERTED MSINSERTED
410 #define INSERTED16 LSINSERTED16
411 #define INSERTED32 LSINSERTED32
412 #define INSERTED64 LSINSERTED64
413 #define INSERTED LSINSERTED
418 /* Sign extend the quantity to the targets natural word size */
420 #define EXTEND8(X) ((signed_word)(signed8)(X))
421 #define EXTEND16(X) ((signed_word)(signed16)(X))
422 #define EXTEND32(X) ((signed_word)(signed32)(X))
423 #define EXTEND64(X) ((signed_word)(signed64)(X))
425 /* depending on MODE return a 64bit or 32bit (sign extended) value */
426 #if (WITH_TARGET_WORD_BITSIZE == 64)
427 #define EXTENDED(X) ((signed64)(signed32)(X))
429 #if (WITH_TARGET_WORD_BITSIZE == 32)
430 #define EXTENDED(X) (X)
434 /* memory alignment macro's */
435 #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1))
436 #define _FLOORa(A,X) ((X) & ~((A) - 1))
438 #define ALIGN_8(X) _ALIGNa (8, X)
439 #define ALIGN_16(X) _ALIGNa (16, X)
441 #define ALIGN_PAGE(X) _ALIGNa (0x1000, X)
442 #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
445 /* bit bliting macro's */
446 #define BLIT32(V, POS, BIT) \
453 #define MBLIT32(V, LO, HI, VAL) \
455 (V) = (((V) & ~MASK32 ((LO), (HI))) \
456 | INSERTED32 (VAL, LO, HI)); \
461 /* some rotate functions. The generic macro's ROT, ROTL, ROTR are
462 intentionally omited. */
465 INLINE_SIM_BITS(unsigned16) ROT16 (unsigned16 val, int shift);
466 INLINE_SIM_BITS(unsigned32) ROT32 (unsigned32 val, int shift);
467 INLINE_SIM_BITS(unsigned64) ROT64 (unsigned64 val, int shift);
470 INLINE_SIM_BITS(unsigned16) ROTL16 (unsigned16 val, int shift);
471 INLINE_SIM_BITS(unsigned32) ROTL32 (unsigned32 val, int shift);
472 INLINE_SIM_BITS(unsigned64) ROTL64 (unsigned64 val, int shift);
475 INLINE_SIM_BITS(unsigned16) ROTR16 (unsigned16 val, int shift);
476 INLINE_SIM_BITS(unsigned32) ROTR32 (unsigned32 val, int shift);
477 INLINE_SIM_BITS(unsigned64) ROTR64 (unsigned64 val, int shift);
481 /* Sign extension operations */
483 INLINE_SIM_BITS(unsigned16) LSSEXT16 (signed16 val, int sign_bit);
484 INLINE_SIM_BITS(unsigned32) LSSEXT32 (signed32 val, int sign_bit);
485 INLINE_SIM_BITS(unsigned64) LSSEXT64 (signed64 val, int sign_bit);
486 INLINE_SIM_BITS(unsigned_word) LSSEXT (signed_word val, int sign_bit);
488 INLINE_SIM_BITS(unsigned16) MSSEXT16 (signed16 val, int sign_bit);
489 INLINE_SIM_BITS(unsigned32) MSSEXT32 (signed32 val, int sign_bit);
490 INLINE_SIM_BITS(unsigned64) MSSEXT64 (signed64 val, int sign_bit);
491 INLINE_SIM_BITS(unsigned_word) MSSEXT (signed_word val, int sign_bit);
493 #if (WITH_TARGET_WORD_MSB == 0)
494 #define SEXT16 MSSEXT16
495 #define SEXT32 MSSEXT32
496 #define SEXT64 MSSEXT64
499 #define SEXT16 LSSEXT16
500 #define SEXT32 LSSEXT32
501 #define SEXT64 LSSEXT64
507 #if ((SIM_BITS_INLINE & INCLUDE_MODULE) && (SIM_BITS_INLINE & INCLUDED_BY_MODULE))
508 #include "sim-bits.c"
511 #endif /* _SIM_BITS_H_ */