ahci: Reorder error cases in handle_cmd
[qemu.git] / target-alpha / int_helper.c
1 /*
2 * Helpers for integer and multimedia instructions.
3 *
4 * Copyright (c) 2007 Jocelyn Mayer
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library 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 GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "cpu.h"
21 #include "exec/helper-proto.h"
22 #include "qemu/host-utils.h"
23
24
25 uint64_t helper_ctpop(uint64_t arg)
26 {
27 return ctpop64(arg);
28 }
29
30 uint64_t helper_ctlz(uint64_t arg)
31 {
32 return clz64(arg);
33 }
34
35 uint64_t helper_cttz(uint64_t arg)
36 {
37 return ctz64(arg);
38 }
39
40 static inline uint64_t byte_zap(uint64_t op, uint8_t mskb)
41 {
42 uint64_t mask;
43
44 mask = 0;
45 mask |= ((mskb >> 0) & 1) * 0x00000000000000FFULL;
46 mask |= ((mskb >> 1) & 1) * 0x000000000000FF00ULL;
47 mask |= ((mskb >> 2) & 1) * 0x0000000000FF0000ULL;
48 mask |= ((mskb >> 3) & 1) * 0x00000000FF000000ULL;
49 mask |= ((mskb >> 4) & 1) * 0x000000FF00000000ULL;
50 mask |= ((mskb >> 5) & 1) * 0x0000FF0000000000ULL;
51 mask |= ((mskb >> 6) & 1) * 0x00FF000000000000ULL;
52 mask |= ((mskb >> 7) & 1) * 0xFF00000000000000ULL;
53
54 return op & ~mask;
55 }
56
57 uint64_t helper_zap(uint64_t val, uint64_t mask)
58 {
59 return byte_zap(val, mask);
60 }
61
62 uint64_t helper_zapnot(uint64_t val, uint64_t mask)
63 {
64 return byte_zap(val, ~mask);
65 }
66
67 uint64_t helper_cmpbge(uint64_t op1, uint64_t op2)
68 {
69 uint8_t opa, opb, res;
70 int i;
71
72 res = 0;
73 for (i = 0; i < 8; i++) {
74 opa = op1 >> (i * 8);
75 opb = op2 >> (i * 8);
76 if (opa >= opb) {
77 res |= 1 << i;
78 }
79 }
80 return res;
81 }
82
83 uint64_t helper_minub8(uint64_t op1, uint64_t op2)
84 {
85 uint64_t res = 0;
86 uint8_t opa, opb, opr;
87 int i;
88
89 for (i = 0; i < 8; ++i) {
90 opa = op1 >> (i * 8);
91 opb = op2 >> (i * 8);
92 opr = opa < opb ? opa : opb;
93 res |= (uint64_t)opr << (i * 8);
94 }
95 return res;
96 }
97
98 uint64_t helper_minsb8(uint64_t op1, uint64_t op2)
99 {
100 uint64_t res = 0;
101 int8_t opa, opb;
102 uint8_t opr;
103 int i;
104
105 for (i = 0; i < 8; ++i) {
106 opa = op1 >> (i * 8);
107 opb = op2 >> (i * 8);
108 opr = opa < opb ? opa : opb;
109 res |= (uint64_t)opr << (i * 8);
110 }
111 return res;
112 }
113
114 uint64_t helper_minuw4(uint64_t op1, uint64_t op2)
115 {
116 uint64_t res = 0;
117 uint16_t opa, opb, opr;
118 int i;
119
120 for (i = 0; i < 4; ++i) {
121 opa = op1 >> (i * 16);
122 opb = op2 >> (i * 16);
123 opr = opa < opb ? opa : opb;
124 res |= (uint64_t)opr << (i * 16);
125 }
126 return res;
127 }
128
129 uint64_t helper_minsw4(uint64_t op1, uint64_t op2)
130 {
131 uint64_t res = 0;
132 int16_t opa, opb;
133 uint16_t opr;
134 int i;
135
136 for (i = 0; i < 4; ++i) {
137 opa = op1 >> (i * 16);
138 opb = op2 >> (i * 16);
139 opr = opa < opb ? opa : opb;
140 res |= (uint64_t)opr << (i * 16);
141 }
142 return res;
143 }
144
145 uint64_t helper_maxub8(uint64_t op1, uint64_t op2)
146 {
147 uint64_t res = 0;
148 uint8_t opa, opb, opr;
149 int i;
150
151 for (i = 0; i < 8; ++i) {
152 opa = op1 >> (i * 8);
153 opb = op2 >> (i * 8);
154 opr = opa > opb ? opa : opb;
155 res |= (uint64_t)opr << (i * 8);
156 }
157 return res;
158 }
159
160 uint64_t helper_maxsb8(uint64_t op1, uint64_t op2)
161 {
162 uint64_t res = 0;
163 int8_t opa, opb;
164 uint8_t opr;
165 int i;
166
167 for (i = 0; i < 8; ++i) {
168 opa = op1 >> (i * 8);
169 opb = op2 >> (i * 8);
170 opr = opa > opb ? opa : opb;
171 res |= (uint64_t)opr << (i * 8);
172 }
173 return res;
174 }
175
176 uint64_t helper_maxuw4(uint64_t op1, uint64_t op2)
177 {
178 uint64_t res = 0;
179 uint16_t opa, opb, opr;
180 int i;
181
182 for (i = 0; i < 4; ++i) {
183 opa = op1 >> (i * 16);
184 opb = op2 >> (i * 16);
185 opr = opa > opb ? opa : opb;
186 res |= (uint64_t)opr << (i * 16);
187 }
188 return res;
189 }
190
191 uint64_t helper_maxsw4(uint64_t op1, uint64_t op2)
192 {
193 uint64_t res = 0;
194 int16_t opa, opb;
195 uint16_t opr;
196 int i;
197
198 for (i = 0; i < 4; ++i) {
199 opa = op1 >> (i * 16);
200 opb = op2 >> (i * 16);
201 opr = opa > opb ? opa : opb;
202 res |= (uint64_t)opr << (i * 16);
203 }
204 return res;
205 }
206
207 uint64_t helper_perr(uint64_t op1, uint64_t op2)
208 {
209 uint64_t res = 0;
210 uint8_t opa, opb, opr;
211 int i;
212
213 for (i = 0; i < 8; ++i) {
214 opa = op1 >> (i * 8);
215 opb = op2 >> (i * 8);
216 if (opa >= opb) {
217 opr = opa - opb;
218 } else {
219 opr = opb - opa;
220 }
221 res += opr;
222 }
223 return res;
224 }
225
226 uint64_t helper_pklb(uint64_t op1)
227 {
228 return (op1 & 0xff) | ((op1 >> 24) & 0xff00);
229 }
230
231 uint64_t helper_pkwb(uint64_t op1)
232 {
233 return ((op1 & 0xff)
234 | ((op1 >> 8) & 0xff00)
235 | ((op1 >> 16) & 0xff0000)
236 | ((op1 >> 24) & 0xff000000));
237 }
238
239 uint64_t helper_unpkbl(uint64_t op1)
240 {
241 return (op1 & 0xff) | ((op1 & 0xff00) << 24);
242 }
243
244 uint64_t helper_unpkbw(uint64_t op1)
245 {
246 return ((op1 & 0xff)
247 | ((op1 & 0xff00) << 8)
248 | ((op1 & 0xff0000) << 16)
249 | ((op1 & 0xff000000) << 24));
250 }
251
252 uint64_t helper_addqv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
253 {
254 uint64_t tmp = op1;
255 op1 += op2;
256 if (unlikely((tmp ^ op2 ^ (-1ULL)) & (tmp ^ op1) & (1ULL << 63))) {
257 arith_excp(env, GETPC(), EXC_M_IOV, 0);
258 }
259 return op1;
260 }
261
262 uint64_t helper_addlv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
263 {
264 uint64_t tmp = op1;
265 op1 = (uint32_t)(op1 + op2);
266 if (unlikely((tmp ^ op2 ^ (-1UL)) & (tmp ^ op1) & (1UL << 31))) {
267 arith_excp(env, GETPC(), EXC_M_IOV, 0);
268 }
269 return op1;
270 }
271
272 uint64_t helper_subqv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
273 {
274 uint64_t res;
275 res = op1 - op2;
276 if (unlikely((op1 ^ op2) & (res ^ op1) & (1ULL << 63))) {
277 arith_excp(env, GETPC(), EXC_M_IOV, 0);
278 }
279 return res;
280 }
281
282 uint64_t helper_sublv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
283 {
284 uint32_t res;
285 res = op1 - op2;
286 if (unlikely((op1 ^ op2) & (res ^ op1) & (1UL << 31))) {
287 arith_excp(env, GETPC(), EXC_M_IOV, 0);
288 }
289 return res;
290 }
291
292 uint64_t helper_mullv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
293 {
294 int64_t res = (int64_t)op1 * (int64_t)op2;
295
296 if (unlikely((int32_t)res != res)) {
297 arith_excp(env, GETPC(), EXC_M_IOV, 0);
298 }
299 return (int64_t)((int32_t)res);
300 }
301
302 uint64_t helper_mulqv(CPUAlphaState *env, uint64_t op1, uint64_t op2)
303 {
304 uint64_t tl, th;
305
306 muls64(&tl, &th, op1, op2);
307 /* If th != 0 && th != -1, then we had an overflow */
308 if (unlikely((th + 1) > 1)) {
309 arith_excp(env, GETPC(), EXC_M_IOV, 0);
310 }
311 return tl;
312 }