| 1 | /* This file is part of the program psim. |
| 2 | |
| 3 | Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au> |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #ifndef _BITS_H_ |
| 23 | #define _BITS_H_ |
| 24 | |
| 25 | |
| 26 | /* bit manipulation routines: |
| 27 | |
| 28 | Bit numbering: The bits are numbered according to the PowerPC |
| 29 | convention - the left most (or most significant) is bit 0 while the |
| 30 | right most (least significant) is bit 1. |
| 31 | |
| 32 | Size convention: Each macro is in three forms - <MACRO>32 which |
| 33 | operates in 32bit quantity (bits are numbered 0..31); <MACRO>64 |
| 34 | which operates using 64bit quantites (and bits are numbered 0..64); |
| 35 | and <MACRO> which operates using the bit size of the target |
| 36 | architecture (bits are still numbered 0..63), with 32bit |
| 37 | architectures ignoring the first 32bits having bit 32 as the most |
| 38 | significant. |
| 39 | |
| 40 | BIT*(POS): Quantity with just 1 bit set. |
| 41 | |
| 42 | MASK*(FIRST, LAST): Create a constant bit mask of the specified |
| 43 | size with bits [FIRST .. LAST] set. |
| 44 | |
| 45 | MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST |
| 46 | .. LAST]. |
| 47 | |
| 48 | LSMASKED*(VALUE, FIRST, LAST): Like MASKED - LS bit is zero. |
| 49 | |
| 50 | EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but |
| 51 | also right shifts the masked value so that bit LAST becomes the |
| 52 | least significant (right most). |
| 53 | |
| 54 | LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is |
| 55 | zero. |
| 56 | |
| 57 | SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD |
| 58 | new NEW. |
| 59 | |
| 60 | MOVED**(VALUE, OLD_FIRST, OLD_LAST, NEW_FIRST, NEW_LAST): Moves |
| 61 | things around so that bits OLD_FIRST..OLD_LAST are masked then |
| 62 | moved to NEW_FIRST..NEW_LAST. |
| 63 | |
| 64 | INSERTED*(VALUE, FIRST, LAST): Takes VALUE and `inserts' the (LAST |
| 65 | - FIRST + 1) least significant bits into bit positions [ FIRST |
| 66 | .. LAST ]. This is almost the complement to EXTRACTED. |
| 67 | |
| 68 | IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets |
| 69 | natural size. If in 32bit mode, discard the high 32bits. |
| 70 | |
| 71 | EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets |
| 72 | natural size. If in 64bit mode, sign extend the value. |
| 73 | |
| 74 | ALIGN_*(VALUE): Round upwards the value so that it is aligned. |
| 75 | |
| 76 | FLOOR_*(VALUE): Truncate the value so that it is aligned. |
| 77 | |
| 78 | ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS |
| 79 | |
| 80 | */ |
| 81 | |
| 82 | #define _MAKE_SHIFT(WIDTH, pos) ((WIDTH) - 1 - (pos)) |
| 83 | |
| 84 | |
| 85 | #if (WITH_TARGET_WORD_MSB == 0) |
| 86 | #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT) |
| 87 | #else |
| 88 | #define _LSB_POS(WIDTH, SHIFT) (SHIFT) |
| 89 | #endif |
| 90 | |
| 91 | |
| 92 | /* MakeBit */ |
| 93 | #define _BITn(WIDTH, pos) (((natural##WIDTH)(1)) \ |
| 94 | << _MAKE_SHIFT(WIDTH, pos)) |
| 95 | |
| 96 | #define BIT4(POS) (1 << _MAKE_SHIFT(4, POS)) |
| 97 | #define BIT5(POS) (1 << _MAKE_SHIFT(5, POS)) |
| 98 | #define BIT8(POS) (1 << _MAKE_SHIFT(8, POS)) |
| 99 | #define BIT10(POS) (1 << _MAKE_SHIFT(10, POS)) |
| 100 | #define BIT32(POS) _BITn(32, POS) |
| 101 | #define BIT64(POS) _BITn(64, POS) |
| 102 | |
| 103 | #if (WITH_TARGET_WORD_BITSIZE == 64) |
| 104 | #define BIT(POS) BIT64(POS) |
| 105 | #else |
| 106 | #define BIT(POS) (((POS) < 32) ? 0 : _BITn(32, (POS)-32)) |
| 107 | #endif |
| 108 | |
| 109 | |
| 110 | /* multi bit mask */ |
| 111 | #define _MASKn(WIDTH, START, STOP) \ |
| 112 | (((((unsigned##WIDTH)0) - 1) \ |
| 113 | >> (WIDTH - ((STOP) - (START) + 1))) \ |
| 114 | << (WIDTH - 1 - (STOP))) |
| 115 | |
| 116 | #define MASK32(START, STOP) _MASKn(32, START, STOP) |
| 117 | #define MASK64(START, STOP) _MASKn(64, START, STOP) |
| 118 | |
| 119 | /* Multi-bit mask on least significant bits */ |
| 120 | |
| 121 | #define _LSMASKn(WIDTH, FIRST, LAST) _MASKn (WIDTH, \ |
| 122 | _LSB_POS (WIDTH, FIRST), \ |
| 123 | _LSB_POS (WIDTH, LAST)) |
| 124 | |
| 125 | #define LSMASK64(FIRST, LAST) _LSMASKn (64, (FIRST), (LAST)) |
| 126 | |
| 127 | #if (WITH_TARGET_WORD_BITSIZE == 64) |
| 128 | #define MASK(START, STOP) \ |
| 129 | (((START) <= (STOP)) \ |
| 130 | ? _MASKn(64, START, STOP) \ |
| 131 | : (_MASKn(64, 0, STOP) \ |
| 132 | | _MASKn(64, START, 63))) |
| 133 | #else |
| 134 | #define MASK(START, STOP) \ |
| 135 | (((START) <= (STOP)) \ |
| 136 | ? (((STOP) < 32) \ |
| 137 | ? 0 \ |
| 138 | : _MASKn(32, \ |
| 139 | (START) < 32 ? 0 : (START) - 32, \ |
| 140 | (STOP)-32)) \ |
| 141 | : (_MASKn(32, \ |
| 142 | (START) < 32 ? 0 : (START) - 32, \ |
| 143 | 31) \ |
| 144 | | (((STOP) < 32) \ |
| 145 | ? 0 \ |
| 146 | : _MASKn(32, \ |
| 147 | 0, \ |
| 148 | (STOP) - 32)))) |
| 149 | #endif |
| 150 | |
| 151 | |
| 152 | /* mask the required bits, leaving them in place */ |
| 153 | |
| 154 | INLINE_BITS\ |
| 155 | (unsigned32) MASKED32 |
| 156 | (unsigned32 word, |
| 157 | unsigned start, |
| 158 | unsigned stop); |
| 159 | |
| 160 | INLINE_BITS\ |
| 161 | (unsigned64) MASKED64 |
| 162 | (unsigned64 word, |
| 163 | unsigned start, |
| 164 | unsigned stop); |
| 165 | |
| 166 | INLINE_BITS\ |
| 167 | (unsigned_word) MASKED |
| 168 | (unsigned_word word, |
| 169 | unsigned start, |
| 170 | unsigned stop); |
| 171 | |
| 172 | INLINE_BITS\ |
| 173 | (unsigned64) LSMASKED64 |
| 174 | (unsigned64 word, |
| 175 | int first, |
| 176 | int last); |
| 177 | |
| 178 | |
| 179 | /* extract the required bits aligning them with the lsb */ |
| 180 | #define _EXTRACTEDn(WIDTH, WORD, START, STOP) \ |
| 181 | ((((natural##WIDTH)(WORD)) >> (WIDTH - (STOP) - 1)) \ |
| 182 | & _MASKn(WIDTH, WIDTH-1+(START)-(STOP), WIDTH-1)) |
| 183 | |
| 184 | /* #define EXTRACTED10(WORD, START, STOP) _EXTRACTEDn(10, WORD, START, STOP) */ |
| 185 | #define EXTRACTED32(WORD, START, STOP) _EXTRACTEDn(32, WORD, START, STOP) |
| 186 | #define EXTRACTED64(WORD, START, STOP) _EXTRACTEDn(64, WORD, START, STOP) |
| 187 | |
| 188 | INLINE_BITS\ |
| 189 | (unsigned_word) EXTRACTED |
| 190 | (unsigned_word val, |
| 191 | unsigned start, |
| 192 | unsigned stop); |
| 193 | |
| 194 | INLINE_BITS\ |
| 195 | (unsigned64) LSEXTRACTED64 |
| 196 | (unsigned64 val, |
| 197 | int start, |
| 198 | int stop); |
| 199 | |
| 200 | /* move a single bit around */ |
| 201 | /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */ |
| 202 | #define _SHUFFLEDn(N, WORD, OLD, NEW) \ |
| 203 | ((OLD) < (NEW) \ |
| 204 | ? (((unsigned##N)(WORD) \ |
| 205 | >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \ |
| 206 | & MASK32((NEW), (NEW))) \ |
| 207 | : (((unsigned##N)(WORD) \ |
| 208 | << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \ |
| 209 | & MASK32((NEW), (NEW)))) |
| 210 | |
| 211 | #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn(32, WORD, OLD, NEW) |
| 212 | #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn(64, WORD, OLD, NEW) |
| 213 | |
| 214 | #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn(_word, WORD, OLD, NEW) |
| 215 | |
| 216 | |
| 217 | /* move a group of bits around */ |
| 218 | #define _INSERTEDn(N, WORD, START, STOP) \ |
| 219 | (((natural##N)(WORD) << _MAKE_SHIFT(N, STOP)) & _MASKn(N, START, STOP)) |
| 220 | |
| 221 | #define INSERTED32(WORD, START, STOP) _INSERTEDn(32, WORD, START, STOP) |
| 222 | #define INSERTED64(WORD, START, STOP) _INSERTEDn(64, WORD, START, STOP) |
| 223 | |
| 224 | INLINE_BITS\ |
| 225 | (unsigned_word) INSERTED |
| 226 | (unsigned_word val, |
| 227 | unsigned start, |
| 228 | unsigned stop); |
| 229 | |
| 230 | |
| 231 | /* depending on MODE return a 64bit or 32bit (sign extended) value */ |
| 232 | #if (WITH_TARGET_WORD_BITSIZE == 64) |
| 233 | #define EXTENDED(X) ((signed64)(signed32)(X)) |
| 234 | #else |
| 235 | #define EXTENDED(X) (X) |
| 236 | #endif |
| 237 | |
| 238 | |
| 239 | /* memory alignment macro's */ |
| 240 | #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1)) |
| 241 | #define _FLOORa(A,X) ((X) & ~((A) - 1)) |
| 242 | |
| 243 | #define ALIGN_8(X) _ALIGNa(8, X) |
| 244 | #define ALIGN_16(X) _ALIGNa(16, X) |
| 245 | |
| 246 | #define ALIGN_PAGE(X) _ALIGNa(0x1000, X) |
| 247 | #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1)) |
| 248 | |
| 249 | |
| 250 | /* bit bliting macro's */ |
| 251 | #define BLIT32(V, POS, BIT) \ |
| 252 | do { \ |
| 253 | if (BIT) \ |
| 254 | V |= BIT32(POS); \ |
| 255 | else \ |
| 256 | V &= ~BIT32(POS); \ |
| 257 | } while (0) |
| 258 | #define MBLIT32(V, LO, HI, VAL) \ |
| 259 | do { \ |
| 260 | (V) = (((V) & ~MASK32((LO), (HI))) \ |
| 261 | | INSERTED32(VAL, LO, HI)); \ |
| 262 | } while (0) |
| 263 | |
| 264 | |
| 265 | /* some rotate functions to make things easier |
| 266 | |
| 267 | NOTE: These are functions not macro's as the latter tickles bugs in |
| 268 | gcc-2.6.3 */ |
| 269 | |
| 270 | #define _ROTLn(N, VAL, SHIFT) \ |
| 271 | (((VAL) << (SHIFT)) | ((VAL) >> ((N)-(SHIFT)))) |
| 272 | |
| 273 | INLINE_BITS\ |
| 274 | (unsigned32) ROTL32 |
| 275 | (unsigned32 val, |
| 276 | long shift); |
| 277 | |
| 278 | INLINE_BITS\ |
| 279 | (unsigned64) ROTL64 |
| 280 | (unsigned64 val, |
| 281 | long shift); |
| 282 | |
| 283 | |
| 284 | #if (BITS_INLINE & INCLUDE_MODULE) |
| 285 | #include "bits.c" |
| 286 | #endif |
| 287 | |
| 288 | #endif /* _BITS_H_ */ |