Define SIGNED64 and UNSIGNED64 macros - handle MSC/GCC LL issue.
[deliverable/binutils-gdb.git] / sim / common / sim-bits.h
1 /* This file is part of the program psim.
2
3 Copyright (C) 1994-1996, Andrew Cagney <cagney@highland.com.au>
4 Copyright (C) 1997, Free Software Foundation, Inc.
5
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.
10
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.
15
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.
19
20 */
21
22
23 #ifndef _SIM_BITS_H_
24 #define _SIM_BITS_H_
25
26
27 /* bit manipulation routines:
28
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.
33
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
40 significant.
41
42 NB: Use EXTRACTED, MSEXTRACTED and LSEXTRACTED as a guideline for
43 naming. LSMASK and LSMASKED are wrong.
44
45 BIT*(POS): Constant with just 1 bit set.
46
47 LSBIT*(OFFSET): Constant with just 1 bit set - LS bit is zero.
48
49 MSBIT*(OFFSET): Constant with just 1 bit set - MS bit is zero.
50
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]).
54
55 LSMASK*(NR_BITS): Like MASK only NR least significant bits are set.
56
57 MSMASK*(NR_BITS): Like MASK only NR most significant bits are set.
58
59 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
60 .. LAST].
61
62 LSMASKED*(VALUE, NR_BITS): Mask out all but the least significant
63 NR_BITS of the value.
64
65 MSMASKED*(VALUE, NR_BITS): Mask out all but the most significant
66 NR_BITS of the value.
67
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).
71
72 LSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - LS bit is
73 zero.
74
75 MSEXTRACTED*(VALUE, FIRST, LAST): Same as extracted - MS bit is
76 zero.
77
78 SHUFFLED**(VALUE, OLD, NEW): Mask then move a single bit from OLD
79 new NEW.
80
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.
84
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.
88
89 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
90 natural size. If in 32bit mode, discard the high 32bits.
91
92 EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets
93 natural size. If in 64bit mode, sign extend the value.
94
95 ALIGN_*(VALUE): Round upwards the value so that it is aligned.
96
97 FLOOR_*(VALUE): Truncate the value so that it is aligned.
98
99 ROTL*(VALUE, NR_BITS): Return the value rotated by NR_BITS left.
100
101 ROTR*(VALUE, NR_BITS): Return the value rotated by NR_BITS right.
102
103 SEXT*(VAL, SIGN_BIT): Treat SIGN_BIT as the sign, extend it.
104
105 Note: Only the BIT* and MASK* macros return a constant that can be
106 used in variable declarations.
107
108 */
109
110
111 /* compute the number of bits between START and STOP */
112
113 #if (WITH_TARGET_WORD_MSB == 0)
114 #define _MAKE_WIDTH(START, STOP) (STOP - START + 1)
115 #else
116 #define _MAKE_WIDTH(START, STOP) (START - STOP + 1)
117 #endif
118
119
120
121 /* compute the number shifts required to move a bit between LSB (MSB)
122 and POS */
123
124 #if (WITH_TARGET_WORD_MSB == 0)
125 #define _LSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
126 #else
127 #define _LSB_SHIFT(WIDTH, POS) (POS)
128 #endif
129
130 #if (WITH_TARGET_WORD_MSB == 0)
131 #define _MSB_SHIFT(WIDTH, POS) (POS)
132 #else
133 #define _MSB_SHIFT(WIDTH, POS) (WIDTH - 1 - POS)
134 #endif
135
136
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 */
139
140 #if (WITH_TARGET_WORD_MSB == 0)
141 #define _MSB_POS(WIDTH, SHIFT) (SHIFT)
142 #else
143 #define _MSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
144 #endif
145
146 #if (WITH_TARGET_WORD_MSB == 0)
147 #define _LSB_POS(WIDTH, SHIFT) (WIDTH - 1 - SHIFT)
148 #else
149 #define _LSB_POS(WIDTH, SHIFT) (SHIFT)
150 #endif
151
152
153 /* convert a 64 bit position into a corresponding 32bit position. MSB
154 pos handles the posibility that the bit lies beyond the 32bit
155 boundary */
156
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))
161 #else
162 #define _MSB_32(START, STOP) (START >= STOP \
163 ? (START >= 32 ? 31 : START) \
164 : (STOP >= 32 ? 31 : STOP))
165 #endif
166
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))
171 #else
172 #define _LSB_32(START, STOP) (START >= STOP \
173 ? (STOP >= 32 ? 31 : STOP) \
174 : (START >= 32 ? 31 : START))
175 #endif
176
177 #if (WITH_TARGET_WORD_MSB == 0)
178 #define _MSB(START, STOP) (START <= STOP ? START : STOP)
179 #else
180 #define _MSB(START, STOP) (START >= STOP ? START : STOP)
181 #endif
182
183 #if (WITH_TARGET_WORD_MSB == 0)
184 #define _LSB(START, STOP) (START <= STOP ? STOP : START)
185 #else
186 #define _LSB(START, STOP) (START >= STOP ? STOP : START)
187 #endif
188
189
190 /* Bit operations */
191
192 #define _BITn(WIDTH, POS) ((natural##WIDTH)1 \
193 << _LSB_SHIFT (WIDTH, POS))
194
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))
202
203 #if (WITH_TARGET_WORD_BITSIZE == 64)
204 #define BIT(POS) BIT64(POS)
205 #endif
206 #if (WITH_TARGET_WORD_BITSIZE == 32)
207 #if (WITH_TARGET_WORD_MSB == 0)
208 #define BIT(POS) ((POS) < 32 \
209 ? 0 \
210 : (1 << ((POS) < 32 ? 0 : _LSB_SHIFT(64, (POS)))))
211 #else
212 #define BIT(POS) ((POS) >= 32 \
213 ? 0 \
214 : (1 << ((POS) >= 32 ? 0 : (POS))))
215 #endif
216 #endif
217 #if !defined (BIT)
218 #error "BIT never defined"
219 #endif
220
221
222 /* LS/MS Bit operations */
223
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))
229
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)))
235
236
237
238 /* multi bit mask */
239
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))
245
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))
249
250 #if (WITH_TARGET_WORD_MSB == 0)
251 #define _POS_LE(START, STOP) (START <= STOP)
252 #else
253 #define _POS_LE(START, STOP) (STOP <= START)
254 #endif
255
256 #if (WITH_TARGET_WORD_BITSIZE == 64)
257 #define MASK(START, STOP) \
258 (_POS_LE ((START), (STOP)) \
259 ? _MASKn(64, \
260 _MSB ((START), (STOP)), \
261 _LSB ((START), (STOP)) ) \
262 : (_MASKn(64, _MSB_POS (64, 0), (STOP)) \
263 | _MASKn(64, (START), _LSB_POS (64, 0))))
264 #endif
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)) \
269 ? 0 \
270 : _MASKn (32, \
271 _MSB_32 ((START), (STOP)), \
272 _LSB_32 ((START), (STOP)))) \
273 : (_MASKn (32, \
274 _LSB_32 ((START), (STOP)), \
275 _LSB_POS (32, 0)) \
276 | (_POS_LE ((STOP), _MSB_POS (64, 31)) \
277 ? 0 \
278 : _MASKn (32, \
279 _MSB_POS (32, 0), \
280 _MSB_32 ((START), (STOP))))))
281 #endif
282 #if !defined (MASK)
283 #error "MASK never undefined"
284 #endif
285
286
287
288 /* Multi-bit mask on least significant bits */
289
290 #if (WITH_TARGET_WORD_MSB == 0)
291 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (WIDTH - NR_BITS), (WIDTH - 1))
292 #else
293 #define _LSMASKn(WIDTH, NR_BITS) _MASKn(WIDTH, (NR_BITS - 1), 0)
294 #endif
295
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))
299
300 #if (WITH_TARGET_WORD_BITSIZE == 64)
301 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
302 ? 0 \
303 : _MASKn (64, \
304 _LSB_POS (64, \
305 ((NR_BITS) < 1 ? 0 \
306 : (NR_BITS) - 1)), \
307 _LSB_POS (64, 0)))
308 #endif
309 #if (WITH_TARGET_WORD_BITSIZE == 32)
310 #define LSMASK(NR_BITS) ((NR_BITS) < 1 \
311 ? 0 \
312 : _MASKn (32, \
313 _LSB_POS (32, \
314 ((NR_BITS) > 32 ? 31 \
315 : (NR_BITS) < 1 ? 0 \
316 : ((NR_BITS) - 1))), \
317 _LSB_POS (32, 0)))
318 #endif
319 #if !defined (LSMASK)
320 #error "LSMASK never defined"
321 #endif
322
323
324 /* Multi-bit mask on most significant bits */
325
326 #if (WITH_TARGET_WORD_MSB == 0)
327 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, 0, (NR_BITS - 1))
328 #else
329 #define _MSMASKn(WIDTH, NR_BITS) _MASKn (WIDTH, (WIDTH - 1), (WIDTH - NR_BITS))
330 #endif
331
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))
335
336 #if (WITH_TARGET_WORD_BITSIZE == 64)
337 #define MSMASK(NR_BITS) (NR_BITS < 1 \
338 ? 0 \
339 : _MASKn (64, \
340 _MSB_POS (64, 0), \
341 _MSB_POS (64, \
342 ((NR_BITS) < 1 ? 0 \
343 : (NR_BITS) - 1))))
344 #endif
345 #if (WITH_TARGET_WORD_BITSIZE == 32)
346 #define MSMASK(NR_BITS) (NR_BITS <= 32 \
347 ? 0 \
348 : _MASKn (32, \
349 _MSB_POS (32, 0), \
350 _MSB_POS (32, \
351 ((NR_BITS) <= 32 ? 0 \
352 : (NR_BITS) - 33))))
353 #endif
354 #if !defined (MSMASK)
355 #error "MSMASK never defined"
356 #endif
357
358
359 /* mask the required bits, leaving them in place */
360
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);
364
365 INLINE_SIM_BITS(unsigned_word) MASKED (unsigned_word word, unsigned start, unsigned stop);
366
367
368 /* Ditto but nr of ls-bits specified */
369
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);
373
374 INLINE_SIM_BITS(unsigned_word) LSMASKED (unsigned_word word, unsigned nr_bits);
375
376
377 /* Ditto but nr of ms-bits specified */
378
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);
382
383 INLINE_SIM_BITS(unsigned_word) MSMASKED (unsigned_word word, unsigned nr_bits);
384
385
386
387 /* extract the required bits aligning them with the lsb */
388
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);
392
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);
396
397 #if (WITH_TARGET_WORD_MSB == 0)
398 #define EXTRACTED16 MSEXTRACTED32
399 #define EXTRACTED32 MSEXTRACTED32
400 #define EXTRACTED64 MSEXTRACTED32
401 #else
402 #define EXTRACTED16 LSEXTRACTED32
403 #define EXTRACTED32 LSEXTRACTED32
404 #define EXTRACTED64 LSEXTRACTED32
405 #endif
406
407
408
409
410 INLINE_SIM_BITS(unsigned_word) EXTRACTED (unsigned_word val, unsigned start, unsigned stop);
411
412
413
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) \
417 ((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))))
424
425 #define SHUFFLED32(WORD, OLD, NEW) _SHUFFLEDn (32, WORD, OLD, NEW)
426 #define SHUFFLED64(WORD, OLD, NEW) _SHUFFLEDn (64, WORD, OLD, NEW)
427
428 #define SHUFFLED(WORD, OLD, NEW) _SHUFFLEDn (_word, WORD, OLD, NEW)
429
430
431 /* move a group of bits around */
432
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);
436
437 INLINE_SIM_BITS(unsigned_word) INSERTED (unsigned_word val, unsigned start, unsigned stop);
438
439
440
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))
444 #endif
445 #if (WITH_TARGET_WORD_BITSIZE == 32)
446 #define EXTENDED(X) (X)
447 #endif
448
449
450 /* memory alignment macro's */
451 #define _ALIGNa(A,X) (((X) + ((A) - 1)) & ~((A) - 1))
452 #define _FLOORa(A,X) ((X) & ~((A) - 1))
453
454 #define ALIGN_8(X) _ALIGNa (8, X)
455 #define ALIGN_16(X) _ALIGNa (16, X)
456
457 #define ALIGN_PAGE(X) _ALIGNa (0x1000, X)
458 #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
459
460
461 /* bit bliting macro's */
462 #define BLIT32(V, POS, BIT) \
463 do { \
464 if (BIT) \
465 V |= BIT32 (POS); \
466 else \
467 V &= ~BIT32 (POS); \
468 } while (0)
469 #define MBLIT32(V, LO, HI, VAL) \
470 do { \
471 (V) = (((V) & ~MASK32 ((LO), (HI))) \
472 | INSERTED32 (VAL, LO, HI)); \
473 } while (0)
474
475
476
477 /* some rotate functions. The generic macro's ROT, ROTL, ROTR are
478 intentionally omited. */
479
480
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);
484
485
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);
489
490
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);
494
495
496
497 /* Sign extension operations */
498
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);
502
503 INLINE_SIM_BITS(unsigned_word) SEXT (signed_word val, unsigned sign_bit);
504
505
506
507 #if ((SIM_BITS_INLINE & INCLUDE_MODULE) && (SIM_BITS_INLINE & INCLUDED_BY_MODULE))
508 #include "sim-bits.c"
509 #endif
510
511 #endif /* _SIM_BITS_H_ */
This page took 0.039909 seconds and 4 git commands to generate.