hw/arm/virt: error_report cleanups
[qemu/ar7.git] / target-alpha / int_helper.c
blobd7f4774127120a0ebe599dcbe714e6d78168d8c0
1 /*
2 * Helpers for integer and multimedia instructions.
4 * Copyright (c) 2007 Jocelyn Mayer
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.
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.
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/>.
20 #include "cpu.h"
21 #include "exec/helper-proto.h"
22 #include "qemu/host-utils.h"
25 uint64_t helper_ctpop(uint64_t arg)
27 return ctpop64(arg);
30 uint64_t helper_ctlz(uint64_t arg)
32 return clz64(arg);
35 uint64_t helper_cttz(uint64_t arg)
37 return ctz64(arg);
40 uint64_t helper_zapnot(uint64_t val, uint64_t mskb)
42 uint64_t mask;
44 mask = -(mskb & 0x01) & 0x00000000000000ffull;
45 mask |= -(mskb & 0x02) & 0x000000000000ff00ull;
46 mask |= -(mskb & 0x04) & 0x0000000000ff0000ull;
47 mask |= -(mskb & 0x08) & 0x00000000ff000000ull;
48 mask |= -(mskb & 0x10) & 0x000000ff00000000ull;
49 mask |= -(mskb & 0x20) & 0x0000ff0000000000ull;
50 mask |= -(mskb & 0x40) & 0x00ff000000000000ull;
51 mask |= -(mskb & 0x80) & 0xff00000000000000ull;
53 return val & mask;
56 uint64_t helper_zap(uint64_t val, uint64_t mask)
58 return helper_zapnot(val, ~mask);
61 uint64_t helper_cmpbe0(uint64_t a)
63 uint64_t m = 0x7f7f7f7f7f7f7f7fULL;
64 uint64_t c = ~(((a & m) + m) | a | m);
65 /* a.......b.......c.......d.......e.......f.......g.......h....... */
66 c |= c << 7;
67 /* ab......bc......cd......de......ef......fg......gh......h....... */
68 c |= c << 14;
69 /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */
70 c |= c << 28;
71 /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */
72 return c >> 56;
75 uint64_t helper_cmpbge(uint64_t a, uint64_t b)
77 uint64_t mask = 0x00ff00ff00ff00ffULL;
78 uint64_t test = 0x0100010001000100ULL;
79 uint64_t al, ah, bl, bh, cl, ch;
81 /* Separate the bytes to avoid false positives. */
82 al = a & mask;
83 bl = b & mask;
84 ah = (a >> 8) & mask;
85 bh = (b >> 8) & mask;
87 /* "Compare". If a byte in B is greater than a byte in A,
88 it will clear the test bit. */
89 cl = ((al | test) - bl) & test;
90 ch = ((ah | test) - bh) & test;
92 /* Fold all of the test bits into a contiguous set. */
93 /* ch=.......a...............c...............e...............g........ */
94 /* cl=.......b...............d...............f...............h........ */
95 cl += ch << 1;
96 /* cl=......ab..............cd..............ef..............gh........ */
97 cl |= cl << 14;
98 /* cl=......abcd............cdef............efgh............gh........ */
99 cl |= cl << 28;
100 /* cl=......abcdefgh........cdefgh..........efgh............gh........ */
101 return cl >> 50;
104 uint64_t helper_minub8(uint64_t op1, uint64_t op2)
106 uint64_t res = 0;
107 uint8_t opa, opb, opr;
108 int i;
110 for (i = 0; i < 8; ++i) {
111 opa = op1 >> (i * 8);
112 opb = op2 >> (i * 8);
113 opr = opa < opb ? opa : opb;
114 res |= (uint64_t)opr << (i * 8);
116 return res;
119 uint64_t helper_minsb8(uint64_t op1, uint64_t op2)
121 uint64_t res = 0;
122 int8_t opa, opb;
123 uint8_t opr;
124 int i;
126 for (i = 0; i < 8; ++i) {
127 opa = op1 >> (i * 8);
128 opb = op2 >> (i * 8);
129 opr = opa < opb ? opa : opb;
130 res |= (uint64_t)opr << (i * 8);
132 return res;
135 uint64_t helper_minuw4(uint64_t op1, uint64_t op2)
137 uint64_t res = 0;
138 uint16_t opa, opb, opr;
139 int i;
141 for (i = 0; i < 4; ++i) {
142 opa = op1 >> (i * 16);
143 opb = op2 >> (i * 16);
144 opr = opa < opb ? opa : opb;
145 res |= (uint64_t)opr << (i * 16);
147 return res;
150 uint64_t helper_minsw4(uint64_t op1, uint64_t op2)
152 uint64_t res = 0;
153 int16_t opa, opb;
154 uint16_t opr;
155 int i;
157 for (i = 0; i < 4; ++i) {
158 opa = op1 >> (i * 16);
159 opb = op2 >> (i * 16);
160 opr = opa < opb ? opa : opb;
161 res |= (uint64_t)opr << (i * 16);
163 return res;
166 uint64_t helper_maxub8(uint64_t op1, uint64_t op2)
168 uint64_t res = 0;
169 uint8_t opa, opb, opr;
170 int i;
172 for (i = 0; i < 8; ++i) {
173 opa = op1 >> (i * 8);
174 opb = op2 >> (i * 8);
175 opr = opa > opb ? opa : opb;
176 res |= (uint64_t)opr << (i * 8);
178 return res;
181 uint64_t helper_maxsb8(uint64_t op1, uint64_t op2)
183 uint64_t res = 0;
184 int8_t opa, opb;
185 uint8_t opr;
186 int i;
188 for (i = 0; i < 8; ++i) {
189 opa = op1 >> (i * 8);
190 opb = op2 >> (i * 8);
191 opr = opa > opb ? opa : opb;
192 res |= (uint64_t)opr << (i * 8);
194 return res;
197 uint64_t helper_maxuw4(uint64_t op1, uint64_t op2)
199 uint64_t res = 0;
200 uint16_t opa, opb, opr;
201 int i;
203 for (i = 0; i < 4; ++i) {
204 opa = op1 >> (i * 16);
205 opb = op2 >> (i * 16);
206 opr = opa > opb ? opa : opb;
207 res |= (uint64_t)opr << (i * 16);
209 return res;
212 uint64_t helper_maxsw4(uint64_t op1, uint64_t op2)
214 uint64_t res = 0;
215 int16_t opa, opb;
216 uint16_t opr;
217 int i;
219 for (i = 0; i < 4; ++i) {
220 opa = op1 >> (i * 16);
221 opb = op2 >> (i * 16);
222 opr = opa > opb ? opa : opb;
223 res |= (uint64_t)opr << (i * 16);
225 return res;
228 uint64_t helper_perr(uint64_t op1, uint64_t op2)
230 uint64_t res = 0;
231 uint8_t opa, opb, opr;
232 int i;
234 for (i = 0; i < 8; ++i) {
235 opa = op1 >> (i * 8);
236 opb = op2 >> (i * 8);
237 if (opa >= opb) {
238 opr = opa - opb;
239 } else {
240 opr = opb - opa;
242 res += opr;
244 return res;
247 uint64_t helper_pklb(uint64_t op1)
249 return (op1 & 0xff) | ((op1 >> 24) & 0xff00);
252 uint64_t helper_pkwb(uint64_t op1)
254 return ((op1 & 0xff)
255 | ((op1 >> 8) & 0xff00)
256 | ((op1 >> 16) & 0xff0000)
257 | ((op1 >> 24) & 0xff000000));
260 uint64_t helper_unpkbl(uint64_t op1)
262 return (op1 & 0xff) | ((op1 & 0xff00) << 24);
265 uint64_t helper_unpkbw(uint64_t op1)
267 return ((op1 & 0xff)
268 | ((op1 & 0xff00) << 8)
269 | ((op1 & 0xff0000) << 16)
270 | ((op1 & 0xff000000) << 24));
273 void helper_check_overflow(CPUAlphaState *env, uint64_t op1, uint64_t op2)
275 if (unlikely(op1 != op2)) {
276 arith_excp(env, GETPC(), EXC_M_IOV, 0);