Add PowerPC simulator from Andrew Cagney <cagney@highland.com.au>
[deliverable/binutils-gdb.git] / sim / ppc / bits.h
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 /* bit manipulation routines:
26
27 Bit numbering: The bits are numbered according to the PowerPC
28 convention - the left most (or most significant) is bit 0 while the
29 right most (least significant) is bit 1.
30
31 Size convention: Each macro is in three forms - <MACRO>32 which
32 operates in 32bit quantity (bits are numbered 0..31); <MACRO>64
33 which operates using 64bit quantites (and bits are numbered 0..64);
34 and <MACRO> which operates using the bit size of the target
35 architecture (bits are still numbered 0..63), with 32bit
36 architectures ignoring the first 32bits having bit 32 as the most
37 significant.
38
39 BIT*(POS): Quantity with just 1 bit set.
40
41 MASK*(FIRST, LAST): Create a constant bit mask of the specified
42 size with bits [FIRST .. LAST] set.
43
44 MASKED*(VALUE, FIRST, LAST): Masks out all but bits [FIRST
45 .. LAST].
46
47 EXTRACTED*(VALUE, FIRST, LAST): Masks out bits [FIRST .. LAST] but
48 also right shifts the masked value so that bit LAST becomes the
49 least significant (right most).
50
51 SHUFFLE*(VALUE, OLD, NEW): Moves things around so that bit pos OLD
52 is extracted and than moved to bit pos NEW.
53
54
55
56 IEA_MASKED(SHOULD_MASK, ADDR): Convert the address to the targets
57 natural size. If in 32bit mode, discard the high 32bits.
58
59 EXTENDED(VALUE): Convert VALUE (32bits of it) to the targets
60 natural size. If in 64bit mode, sign extend the value.
61
62 */
63
64 /* Bit operators */
65 #define BIT4(POS) (1 << _MAKE_SHIFT(4, POS))
66 #define BIT5(POS) (1 << _MAKE_SHIFT(5, POS))
67 #define BIT10(POS) (1 << _MAKE_SHIFT(10, POS))
68 #define BIT32(POS) _BITn(32, POS)
69 #define BIT64(POS) _BITn(64, POS)
70
71 #if (WITH_64BIT_TARGET)
72 #define BIT(POS) BIT64(POS)
73 #else
74 #define BIT(POS) (((POS) < 32) ? 0 : _BITn(32, (POS)-32))
75 #endif
76
77
78 /* multi bit mask */
79
80 #define MASK32(START, STOP) _MASKn(32, START, STOP)
81 #define MASK64(START, STOP) _MASKn(64, START, STOP)
82
83 #if (WITH_64BIT_TARGET)
84 #define MASK(START, STOP) (((START) <= (STOP)) \
85 ? _MASKn(64, START, STOP) \
86 : (_MASKn(64, 0, STOP) \
87 | _MASKn(64, START, 63)))
88 #else
89 #define MASK(START, STOP) (((START) <= (STOP)) \
90 ? (((STOP) < 32) \
91 ? 0 \
92 : _MASKn(32, \
93 (START) < 32 ? 0 : (START) - 32, \
94 (STOP)-32)) \
95 : (_MASKn(32, \
96 (START) < 32 ? 0 : (START) - 32, \
97 31) \
98 | (((STOP) < 32) \
99 ? 0 \
100 : _MASKn(32, \
101 0, \
102 (STOP) - 32))))
103 #endif
104
105
106 #define MASKED32(WORD, START, STOP) _MASKEDn(32, WORD, START, STOP)
107 #define MASKED64(WORD, START, STOP) _MASKEDn(64, WORD, START, STOP)
108 #define MASKED10(WORD, START, STOP) _MASKEDn(10, WORD, START, STOP)
109 #define EXTRACTED32(WORD, START, STOP) _EXTRACTEDn(32, WORD, START, STOP)
110 #define EXTRACTED64(WORD, START, STOP) _EXTRACTEDn(64, WORD, START, STOP)
111 #define EXTRACTED10(WORD, START, STOP) _EXTRACTEDn(10, WORD, START, STOP)
112
113 #define MASKED(WORD, START, STOP) ((natural_word)(WORD) & MASK(START, STOP))
114 #if (WITH_64BITS_TARGET)
115 #define EXTRACTED(WORD, START, STOP) _EXTRACTEDn(64, WORD, START, STOP)
116 #else
117 #define EXTRACTED(WORD, START, STOP) (STOP < 32 \
118 ? 0 \
119 : (((natural_word)WORD \
120 >> (63 - (STOP))) \
121 && MASK(START+(63-STOP), 63)))
122 #endif
123
124
125 #define SHUFFLE32(WORD, OLD, NEW) _SHUFFLEn(32, WORD, OLD, NEW)
126 #define SHUFFLE64(WORD, OLD, NEW) _SHUFFLEn(64, WORD, OLD, NEW)
127 #define SHUFFLE(WORD, OLD, NEW) _SHUFFLEn(_word, WORD, OLD, NEW)
128 /* NB: the wierdness (N>O?N-O:0) is to stop a warning from GCC */
129 #define _SHUFFLEn(N, WORD, OLD, NEW) \
130 ((OLD) < (NEW) \
131 ? (((unsigned##N)(WORD) \
132 >> (((NEW) > (OLD)) ? ((NEW) - (OLD)) : 0)) \
133 & MASK32((NEW), (NEW))) \
134 : (((unsigned##N)(WORD) \
135 << (((OLD) > (NEW)) ? ((OLD) - (NEW)) : 0)) \
136 & MASK32((NEW), (NEW))))
137
138
139
140 /* depending on MODE return a 64bit or 32bit (sign extended) value */
141 #if (WITH_64BIT_TARGET)
142 #define EXTENDED(X) ((signed64)(signed32)(X))
143 #else
144 #define EXTENDED(X) (X)
145 #endif
146
147
148
149
150 /* memory alignment macro's */
151 #define _ALIGNa(A,X) (((X) + ((A)-1)) & ~((A)-1))
152 #define ALIGN_8(X) _ALIGNa(8, X)
153 #define ALIGN_16(X) _ALIGNa(16, X)
154 #define ALIGN_PAGE(X) _ALIGNa(0x1000, X)
155 #define FLOOR_PAGE(X) ((X) & ~(0x1000 - 1))
156
157 /* bit bliting macro's */
158 #define BLIT32(V, POS, BIT) \
159 do { \
160 if (BIT) \
161 V |= BIT32(POS); \
162 else \
163 V &= ~BIT32(POS); \
164 } while (0)
165 #define MLIT32(V, LO, HI, VAL) \
166 do { \
167 (V) = (((V) & ~MASK32((LO), (HI))) \
168 | ((VAL) << _MAKE_SHIFT(32,HI))); \
169 } while (0)
170
171
172
173 /* Things for creating single bit set values */
174 /* MakeBit */
175 #define _MAKE_SHIFT(WIDTH, pos) (WIDTH - 1 - (pos))
176 #define _BITn(WIDTH, pos) (((natural##WIDTH)(1)) \
177 << _MAKE_SHIFT(WIDTH, pos))
178 /* MakeBitMask */
179 #define _MASKn(WIDTH, START, STOP) (((((unsigned##WIDTH)0) - 1) \
180 >> (WIDTH - ((STOP) - (START) + 1))) \
181 << (WIDTH - 1 - (STOP)))
182
183
184
185 /* mask the required bits, leaving them in place */
186 #define _MASKEDn(WIDTH, WORD, START, STOP) \
187 (((natural##WIDTH)(WORD)) & MASK##WIDTH(START, STOP))
188
189 /* extract the required bits aligning them with the lsb */
190 #define _EXTRACTEDn(WIDTH, WORD, START, STOP) \
191 ((((natural##WIDTH)(WORD)) >> (WIDTH - (STOP) - 1)) \
192 & _MASKn(WIDTH, WIDTH-1+(START)-(STOP), WIDTH-1))
193
194 #endif /* _BITS_H_ */
This page took 0.049134 seconds and 5 git commands to generate.