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/>.
21 #include "exec/helper-proto.h"
22 #include "qemu/host-utils.h"
25 uint64_t helper_ctpop(uint64_t arg
)
30 uint64_t helper_ctlz(uint64_t arg
)
35 uint64_t helper_cttz(uint64_t arg
)
40 uint64_t helper_zapnot(uint64_t val
, uint64_t mskb
)
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
;
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....... */
67 /* ab......bc......cd......de......ef......fg......gh......h....... */
69 /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */
71 /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */
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. */
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........ */
96 /* cl=......ab..............cd..............ef..............gh........ */
98 /* cl=......abcd............cdef............efgh............gh........ */
100 /* cl=......abcdefgh........cdefgh..........efgh............gh........ */
104 uint64_t helper_minub8(uint64_t op1
, uint64_t op2
)
107 uint8_t opa
, opb
, opr
;
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);
119 uint64_t helper_minsb8(uint64_t op1
, uint64_t op2
)
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);
135 uint64_t helper_minuw4(uint64_t op1
, uint64_t op2
)
138 uint16_t opa
, opb
, opr
;
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);
150 uint64_t helper_minsw4(uint64_t op1
, uint64_t op2
)
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);
166 uint64_t helper_maxub8(uint64_t op1
, uint64_t op2
)
169 uint8_t opa
, opb
, opr
;
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);
181 uint64_t helper_maxsb8(uint64_t op1
, uint64_t op2
)
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);
197 uint64_t helper_maxuw4(uint64_t op1
, uint64_t op2
)
200 uint16_t opa
, opb
, opr
;
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);
212 uint64_t helper_maxsw4(uint64_t op1
, uint64_t op2
)
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);
228 uint64_t helper_perr(uint64_t op1
, uint64_t op2
)
231 uint8_t opa
, opb
, opr
;
234 for (i
= 0; i
< 8; ++i
) {
235 opa
= op1
>> (i
* 8);
236 opb
= op2
>> (i
* 8);
247 uint64_t helper_pklb(uint64_t op1
)
249 return (op1
& 0xff) | ((op1
>> 24) & 0xff00);
252 uint64_t helper_pkwb(uint64_t op1
)
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
)
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);