Commit | Line | Data |
---|---|---|
b3e426bc AC |
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 N | |
24 | #error "N must be #defined" | |
25 | #endif | |
26 | ||
7ec396d2 AC |
27 | #include "sim-xcat.h" |
28 | ||
564e2a3f MM |
29 | #ifdef __STDC__ |
30 | /* If signed were defined to be say __signed (ie, some versions of Linux), | |
31 | then the signedN macro would not work correctly. If we have a standard | |
32 | compiler, we have signed. */ | |
33 | #undef signed | |
34 | #endif | |
35 | ||
b3e426bc AC |
36 | /* NOTE: See end of file for #undef */ |
37 | #define unsignedN XCONCAT2(unsigned,N) | |
38 | #define signedN XCONCAT2(signed,N) | |
39 | #define MASKEDn XCONCAT2(MASKED,N) | |
40 | #define MASKn XCONCAT2(MASK,N) | |
41 | #define LSMASKEDn XCONCAT2(LSMASKED,N) | |
7ec396d2 AC |
42 | #define LSMASKn XCONCAT2(LSMASK,N) |
43 | #define MSMASKEDn XCONCAT2(MSMASKED,N) | |
44 | #define MSMASKn XCONCAT2(MSMASK,N) | |
b3e426bc AC |
45 | #define EXTRACTEDn XCONCAT2(EXTRACTED,N) |
46 | #define INSERTEDn XCONCAT2(INSERTED,N) | |
47 | #define ROTn XCONCAT2(ROT,N) | |
48 | #define ROTLn XCONCAT2(ROTL,N) | |
49 | #define ROTRn XCONCAT2(ROTR,N) | |
50 | #define SEXTn XCONCAT2(SEXT,N) | |
51 | ||
52 | ||
53 | INLINE_SIM_BITS\ | |
54 | (unsignedN) | |
7ec396d2 AC |
55 | MASKEDn (unsignedN word, |
56 | unsigned start, | |
57 | unsigned stop) | |
b3e426bc | 58 | { |
7ec396d2 | 59 | return (word & MASKn (start, stop)); |
b3e426bc AC |
60 | } |
61 | ||
62 | ||
63 | INLINE_SIM_BITS\ | |
64 | (unsignedN) | |
7ec396d2 AC |
65 | LSMASKEDn (unsignedN word, |
66 | unsigned nr_bits) | |
b3e426bc | 67 | { |
7ec396d2 | 68 | return (word & LSMASKn (nr_bits)); |
b3e426bc AC |
69 | } |
70 | ||
71 | ||
72 | INLINE_SIM_BITS\ | |
73 | (unsignedN) | |
7ec396d2 AC |
74 | MSMASKEDn (unsignedN word, |
75 | unsigned nr_bits) | |
b3e426bc | 76 | { |
7ec396d2 | 77 | return (word & MSMASKn (nr_bits)); |
b3e426bc AC |
78 | } |
79 | ||
80 | ||
81 | INLINE_SIM_BITS\ | |
82 | (unsignedN) | |
7ec396d2 AC |
83 | EXTRACTEDn (unsignedN val, |
84 | unsigned start, | |
85 | unsigned stop) | |
b3e426bc | 86 | { |
7ec396d2 AC |
87 | val <<= _MSB_SHIFT (N, start); |
88 | val >>= (_MSB_SHIFT (N, start) + _LSB_SHIFT (N, stop)); | |
89 | return val; | |
b3e426bc AC |
90 | } |
91 | ||
92 | ||
93 | INLINE_SIM_BITS\ | |
94 | (unsignedN) | |
7ec396d2 AC |
95 | INSERTEDn (unsignedN val, |
96 | unsigned start, | |
97 | unsigned stop) | |
98 | { | |
99 | val &= LSMASKn (_MAKE_WIDTH (start, stop)); | |
100 | val <<= _LSB_SHIFT (N, stop); | |
101 | return val; | |
102 | } | |
103 | ||
104 | ||
105 | INLINE_SIM_BITS\ | |
106 | (unsignedN) | |
107 | ROTn (unsignedN val, | |
108 | int shift) | |
b3e426bc | 109 | { |
b3e426bc | 110 | if (shift > 0) |
7ec396d2 | 111 | return ROTRn (val, shift); |
b3e426bc | 112 | else if (shift < 0) |
7ec396d2 | 113 | return ROTLn (val, -shift); |
b3e426bc AC |
114 | else |
115 | return val; | |
116 | } | |
117 | ||
118 | ||
119 | INLINE_SIM_BITS\ | |
120 | (unsignedN) | |
7ec396d2 AC |
121 | ROTLn (unsignedN val, |
122 | unsigned shift) | |
b3e426bc AC |
123 | { |
124 | unsignedN result; | |
7ec396d2 | 125 | ASSERT (shift <= N); |
b3e426bc AC |
126 | result = (((val) << (shift)) | ((val) >> ((N)-(shift)))); |
127 | return result; | |
128 | } | |
129 | ||
130 | ||
131 | INLINE_SIM_BITS\ | |
132 | (unsignedN) | |
7ec396d2 AC |
133 | ROTRn (unsignedN val, |
134 | unsigned shift) | |
b3e426bc AC |
135 | { |
136 | unsignedN result; | |
7ec396d2 | 137 | ASSERT (shift <= N); |
b3e426bc AC |
138 | result = (((val) >> (shift)) | ((val) << ((N)-(shift)))); |
139 | return result; | |
140 | } | |
141 | ||
142 | ||
143 | INLINE_SIM_BITS\ | |
144 | (unsignedN) | |
7ec396d2 AC |
145 | SEXTn (signedN val, |
146 | unsigned sign_bit) | |
b3e426bc AC |
147 | { |
148 | /* make the sign-bit most significant and then smear it back into | |
149 | position */ | |
7ec396d2 AC |
150 | ASSERT (sign_bit < N); |
151 | val <<= _MSB_SHIFT (N, sign_bit); | |
152 | val >>= _MSB_SHIFT (N, sign_bit); | |
153 | return val; | |
b3e426bc AC |
154 | } |
155 | ||
156 | ||
157 | /* NOTE: See start of file for #define */ | |
158 | #undef SEXTn | |
159 | #undef ROTLn | |
160 | #undef ROTRn | |
161 | #undef ROTn | |
162 | #undef INSERTEDn | |
163 | #undef EXTRACTEDn | |
164 | #undef LSMASKEDn | |
7ec396d2 AC |
165 | #undef LSMASKn |
166 | #undef MSMASKEDn | |
167 | #undef MSMASKn | |
b3e426bc AC |
168 | #undef MASKn |
169 | #undef MASKEDn | |
170 | #undef signedN | |
171 | #undef unsignedN |