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