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_zapnot(uint64_t val
, uint64_t mskb
)
31 mask
= -(mskb
& 0x01) & 0x00000000000000ffull
;
32 mask
|= -(mskb
& 0x02) & 0x000000000000ff00ull
;
33 mask
|= -(mskb
& 0x04) & 0x0000000000ff0000ull
;
34 mask
|= -(mskb
& 0x08) & 0x00000000ff000000ull
;
35 mask
|= -(mskb
& 0x10) & 0x000000ff00000000ull
;
36 mask
|= -(mskb
& 0x20) & 0x0000ff0000000000ull
;
37 mask
|= -(mskb
& 0x40) & 0x00ff000000000000ull
;
38 mask
|= -(mskb
& 0x80) & 0xff00000000000000ull
;
43 uint64_t helper_zap(uint64_t val
, uint64_t mask
)
45 return helper_zapnot(val
, ~mask
);
48 uint64_t helper_cmpbe0(uint64_t a
)
50 uint64_t m
= 0x7f7f7f7f7f7f7f7fULL
;
51 uint64_t c
= ~(((a
& m
) + m
) | a
| m
);
52 /* a.......b.......c.......d.......e.......f.......g.......h....... */
54 /* ab......bc......cd......de......ef......fg......gh......h....... */
56 /* abcd....bcde....cdef....defg....efgh....fgh.....gh......h....... */
58 /* abcdefghbcdefgh.cdefgh..defgh...efgh....fgh.....gh......h....... */
62 uint64_t helper_cmpbge(uint64_t a
, uint64_t b
)
64 uint64_t mask
= 0x00ff00ff00ff00ffULL
;
65 uint64_t test
= 0x0100010001000100ULL
;
66 uint64_t al
, ah
, bl
, bh
, cl
, ch
;
68 /* Separate the bytes to avoid false positives. */
74 /* "Compare". If a byte in B is greater than a byte in A,
75 it will clear the test bit. */
76 cl
= ((al
| test
) - bl
) & test
;
77 ch
= ((ah
| test
) - bh
) & test
;
79 /* Fold all of the test bits into a contiguous set. */
80 /* ch=.......a...............c...............e...............g........ */
81 /* cl=.......b...............d...............f...............h........ */
83 /* cl=......ab..............cd..............ef..............gh........ */
85 /* cl=......abcd............cdef............efgh............gh........ */
87 /* cl=......abcdefgh........cdefgh..........efgh............gh........ */
91 uint64_t helper_minub8(uint64_t op1
, uint64_t op2
)
94 uint8_t opa
, opb
, opr
;
97 for (i
= 0; i
< 8; ++i
) {
100 opr
= opa
< opb
? opa
: opb
;
101 res
|= (uint64_t)opr
<< (i
* 8);
106 uint64_t helper_minsb8(uint64_t op1
, uint64_t op2
)
113 for (i
= 0; i
< 8; ++i
) {
114 opa
= op1
>> (i
* 8);
115 opb
= op2
>> (i
* 8);
116 opr
= opa
< opb
? opa
: opb
;
117 res
|= (uint64_t)opr
<< (i
* 8);
122 uint64_t helper_minuw4(uint64_t op1
, uint64_t op2
)
125 uint16_t opa
, opb
, opr
;
128 for (i
= 0; i
< 4; ++i
) {
129 opa
= op1
>> (i
* 16);
130 opb
= op2
>> (i
* 16);
131 opr
= opa
< opb
? opa
: opb
;
132 res
|= (uint64_t)opr
<< (i
* 16);
137 uint64_t helper_minsw4(uint64_t op1
, uint64_t op2
)
144 for (i
= 0; i
< 4; ++i
) {
145 opa
= op1
>> (i
* 16);
146 opb
= op2
>> (i
* 16);
147 opr
= opa
< opb
? opa
: opb
;
148 res
|= (uint64_t)opr
<< (i
* 16);
153 uint64_t helper_maxub8(uint64_t op1
, uint64_t op2
)
156 uint8_t opa
, opb
, opr
;
159 for (i
= 0; i
< 8; ++i
) {
160 opa
= op1
>> (i
* 8);
161 opb
= op2
>> (i
* 8);
162 opr
= opa
> opb
? opa
: opb
;
163 res
|= (uint64_t)opr
<< (i
* 8);
168 uint64_t helper_maxsb8(uint64_t op1
, uint64_t op2
)
175 for (i
= 0; i
< 8; ++i
) {
176 opa
= op1
>> (i
* 8);
177 opb
= op2
>> (i
* 8);
178 opr
= opa
> opb
? opa
: opb
;
179 res
|= (uint64_t)opr
<< (i
* 8);
184 uint64_t helper_maxuw4(uint64_t op1
, uint64_t op2
)
187 uint16_t opa
, opb
, opr
;
190 for (i
= 0; i
< 4; ++i
) {
191 opa
= op1
>> (i
* 16);
192 opb
= op2
>> (i
* 16);
193 opr
= opa
> opb
? opa
: opb
;
194 res
|= (uint64_t)opr
<< (i
* 16);
199 uint64_t helper_maxsw4(uint64_t op1
, uint64_t op2
)
206 for (i
= 0; i
< 4; ++i
) {
207 opa
= op1
>> (i
* 16);
208 opb
= op2
>> (i
* 16);
209 opr
= opa
> opb
? opa
: opb
;
210 res
|= (uint64_t)opr
<< (i
* 16);
215 uint64_t helper_perr(uint64_t op1
, uint64_t op2
)
218 uint8_t opa
, opb
, opr
;
221 for (i
= 0; i
< 8; ++i
) {
222 opa
= op1
>> (i
* 8);
223 opb
= op2
>> (i
* 8);
234 uint64_t helper_pklb(uint64_t op1
)
236 return (op1
& 0xff) | ((op1
>> 24) & 0xff00);
239 uint64_t helper_pkwb(uint64_t op1
)
242 | ((op1
>> 8) & 0xff00)
243 | ((op1
>> 16) & 0xff0000)
244 | ((op1
>> 24) & 0xff000000));
247 uint64_t helper_unpkbl(uint64_t op1
)
249 return (op1
& 0xff) | ((op1
& 0xff00) << 24);
252 uint64_t helper_unpkbw(uint64_t op1
)
255 | ((op1
& 0xff00) << 8)
256 | ((op1
& 0xff0000) << 16)
257 | ((op1
& 0xff000000) << 24));
260 void helper_check_overflow(CPUAlphaState
*env
, uint64_t op1
, uint64_t op2
)
262 if (unlikely(op1
!= op2
)) {
263 arith_excp(env
, GETPC(), EXC_M_IOV
, 0);