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 "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "exec/helper-proto.h"
24 #include "qemu/host-utils.h"
27 uint64_t helper_ctpop(uint64_t arg
)
32 uint64_t helper_ctlz(uint64_t arg
)
37 uint64_t helper_cttz(uint64_t arg
)
42 uint64_t helper_zapnot(uint64_t val
, uint64_t mskb
)
46 mask
= -(mskb
& 0x01) & 0x00000000000000ffull
;
47 mask
|= -(mskb
& 0x02) & 0x000000000000ff00ull
;
48 mask
|= -(mskb
& 0x04) & 0x0000000000ff0000ull
;
49 mask
|= -(mskb
& 0x08) & 0x00000000ff000000ull
;
50 mask
|= -(mskb
& 0x10) & 0x000000ff00000000ull
;
51 mask
|= -(mskb
& 0x20) & 0x0000ff0000000000ull
;
52 mask
|= -(mskb
& 0x40) & 0x00ff000000000000ull
;
53 mask
|= -(mskb
& 0x80) & 0xff00000000000000ull
;
58 uint64_t helper_zap(uint64_t val
, uint64_t mask
)
60 return helper_zapnot(val
, ~mask
);
63 uint64_t helper_cmpbe0(uint64_t a
)
65 uint64_t m
= 0x7f7f7f7f7f7f7f7fULL
;
66 uint64_t c
= ~(((a
& m
) + m
) | a
| m
);
67 /* a.......b.......c.......d.......e.......f.......g.......h....... */
69 /* ab......bc......cd......de......ef......fg......gh......h....... */
71 /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */
73 /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */
77 uint64_t helper_cmpbge(uint64_t a
, uint64_t b
)
79 uint64_t mask
= 0x00ff00ff00ff00ffULL
;
80 uint64_t test
= 0x0100010001000100ULL
;
81 uint64_t al
, ah
, bl
, bh
, cl
, ch
;
83 /* Separate the bytes to avoid false positives. */
89 /* "Compare". If a byte in B is greater than a byte in A,
90 it will clear the test bit. */
91 cl
= ((al
| test
) - bl
) & test
;
92 ch
= ((ah
| test
) - bh
) & test
;
94 /* Fold all of the test bits into a contiguous set. */
95 /* ch=.......a...............c...............e...............g........ */
96 /* cl=.......b...............d...............f...............h........ */
98 /* cl=......ab..............cd..............ef..............gh........ */
100 /* cl=......abcd............cdef............efgh............gh........ */
102 /* cl=......abcdefgh........cdefgh..........efgh............gh........ */
106 uint64_t helper_minub8(uint64_t op1
, uint64_t op2
)
109 uint8_t opa
, opb
, opr
;
112 for (i
= 0; i
< 8; ++i
) {
113 opa
= op1
>> (i
* 8);
114 opb
= op2
>> (i
* 8);
115 opr
= opa
< opb
? opa
: opb
;
116 res
|= (uint64_t)opr
<< (i
* 8);
121 uint64_t helper_minsb8(uint64_t op1
, uint64_t op2
)
128 for (i
= 0; i
< 8; ++i
) {
129 opa
= op1
>> (i
* 8);
130 opb
= op2
>> (i
* 8);
131 opr
= opa
< opb
? opa
: opb
;
132 res
|= (uint64_t)opr
<< (i
* 8);
137 uint64_t helper_minuw4(uint64_t op1
, uint64_t op2
)
140 uint16_t opa
, opb
, opr
;
143 for (i
= 0; i
< 4; ++i
) {
144 opa
= op1
>> (i
* 16);
145 opb
= op2
>> (i
* 16);
146 opr
= opa
< opb
? opa
: opb
;
147 res
|= (uint64_t)opr
<< (i
* 16);
152 uint64_t helper_minsw4(uint64_t op1
, uint64_t op2
)
159 for (i
= 0; i
< 4; ++i
) {
160 opa
= op1
>> (i
* 16);
161 opb
= op2
>> (i
* 16);
162 opr
= opa
< opb
? opa
: opb
;
163 res
|= (uint64_t)opr
<< (i
* 16);
168 uint64_t helper_maxub8(uint64_t op1
, uint64_t op2
)
171 uint8_t opa
, opb
, opr
;
174 for (i
= 0; i
< 8; ++i
) {
175 opa
= op1
>> (i
* 8);
176 opb
= op2
>> (i
* 8);
177 opr
= opa
> opb
? opa
: opb
;
178 res
|= (uint64_t)opr
<< (i
* 8);
183 uint64_t helper_maxsb8(uint64_t op1
, uint64_t op2
)
190 for (i
= 0; i
< 8; ++i
) {
191 opa
= op1
>> (i
* 8);
192 opb
= op2
>> (i
* 8);
193 opr
= opa
> opb
? opa
: opb
;
194 res
|= (uint64_t)opr
<< (i
* 8);
199 uint64_t helper_maxuw4(uint64_t op1
, uint64_t op2
)
202 uint16_t opa
, opb
, opr
;
205 for (i
= 0; i
< 4; ++i
) {
206 opa
= op1
>> (i
* 16);
207 opb
= op2
>> (i
* 16);
208 opr
= opa
> opb
? opa
: opb
;
209 res
|= (uint64_t)opr
<< (i
* 16);
214 uint64_t helper_maxsw4(uint64_t op1
, uint64_t op2
)
221 for (i
= 0; i
< 4; ++i
) {
222 opa
= op1
>> (i
* 16);
223 opb
= op2
>> (i
* 16);
224 opr
= opa
> opb
? opa
: opb
;
225 res
|= (uint64_t)opr
<< (i
* 16);
230 uint64_t helper_perr(uint64_t op1
, uint64_t op2
)
233 uint8_t opa
, opb
, opr
;
236 for (i
= 0; i
< 8; ++i
) {
237 opa
= op1
>> (i
* 8);
238 opb
= op2
>> (i
* 8);
249 uint64_t helper_pklb(uint64_t op1
)
251 return (op1
& 0xff) | ((op1
>> 24) & 0xff00);
254 uint64_t helper_pkwb(uint64_t op1
)
257 | ((op1
>> 8) & 0xff00)
258 | ((op1
>> 16) & 0xff0000)
259 | ((op1
>> 24) & 0xff000000));
262 uint64_t helper_unpkbl(uint64_t op1
)
264 return (op1
& 0xff) | ((op1
& 0xff00) << 24);
267 uint64_t helper_unpkbw(uint64_t op1
)
270 | ((op1
& 0xff00) << 8)
271 | ((op1
& 0xff0000) << 16)
272 | ((op1
& 0xff000000) << 24));
275 void helper_check_overflow(CPUAlphaState
*env
, uint64_t op1
, uint64_t op2
)
277 if (unlikely(op1
!= op2
)) {
278 arith_excp(env
, GETPC(), EXC_M_IOV
, 0);