4 * Copyright (c) 2003 Fabrice Bellard
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 "qemu/host-utils.h"
24 //#define DEBUG_MULDIV
27 static const uint8_t rclb_table
[32] = {
28 0, 1, 2, 3, 4, 5, 6, 7,
29 8, 0, 1, 2, 3, 4, 5, 6,
30 7, 8, 0, 1, 2, 3, 4, 5,
31 6, 7, 8, 0, 1, 2, 3, 4,
35 static const uint8_t rclw_table
[32] = {
36 0, 1, 2, 3, 4, 5, 6, 7,
37 8, 9, 10, 11, 12, 13, 14, 15,
38 16, 0, 1, 2, 3, 4, 5, 6,
39 7, 8, 9, 10, 11, 12, 13, 14,
42 /* division, flags are undefined */
44 void helper_divb_AL(CPUX86State
*env
, target_ulong t0
)
46 unsigned int num
, den
, q
, r
;
51 raise_exception(env
, EXCP00_DIVZ
);
55 raise_exception(env
, EXCP00_DIVZ
);
58 r
= (num
% den
) & 0xff;
59 EAX
= (EAX
& ~0xffff) | (r
<< 8) | q
;
62 void helper_idivb_AL(CPUX86State
*env
, target_ulong t0
)
69 raise_exception(env
, EXCP00_DIVZ
);
73 raise_exception(env
, EXCP00_DIVZ
);
76 r
= (num
% den
) & 0xff;
77 EAX
= (EAX
& ~0xffff) | (r
<< 8) | q
;
80 void helper_divw_AX(CPUX86State
*env
, target_ulong t0
)
82 unsigned int num
, den
, q
, r
;
84 num
= (EAX
& 0xffff) | ((EDX
& 0xffff) << 16);
87 raise_exception(env
, EXCP00_DIVZ
);
91 raise_exception(env
, EXCP00_DIVZ
);
94 r
= (num
% den
) & 0xffff;
95 EAX
= (EAX
& ~0xffff) | q
;
96 EDX
= (EDX
& ~0xffff) | r
;
99 void helper_idivw_AX(CPUX86State
*env
, target_ulong t0
)
103 num
= (EAX
& 0xffff) | ((EDX
& 0xffff) << 16);
106 raise_exception(env
, EXCP00_DIVZ
);
109 if (q
!= (int16_t)q
) {
110 raise_exception(env
, EXCP00_DIVZ
);
113 r
= (num
% den
) & 0xffff;
114 EAX
= (EAX
& ~0xffff) | q
;
115 EDX
= (EDX
& ~0xffff) | r
;
118 void helper_divl_EAX(CPUX86State
*env
, target_ulong t0
)
123 num
= ((uint32_t)EAX
) | ((uint64_t)((uint32_t)EDX
) << 32);
126 raise_exception(env
, EXCP00_DIVZ
);
130 if (q
> 0xffffffff) {
131 raise_exception(env
, EXCP00_DIVZ
);
137 void helper_idivl_EAX(CPUX86State
*env
, target_ulong t0
)
142 num
= ((uint32_t)EAX
) | ((uint64_t)((uint32_t)EDX
) << 32);
145 raise_exception(env
, EXCP00_DIVZ
);
149 if (q
!= (int32_t)q
) {
150 raise_exception(env
, EXCP00_DIVZ
);
159 void helper_aam(CPUX86State
*env
, int base
)
166 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
170 void helper_aad(CPUX86State
*env
, int base
)
175 ah
= (EAX
>> 8) & 0xff;
176 al
= ((ah
* base
) + al
) & 0xff;
177 EAX
= (EAX
& ~0xffff) | al
;
181 void helper_aaa(CPUX86State
*env
)
187 eflags
= cpu_cc_compute_all(env
, CC_OP
);
190 ah
= (EAX
>> 8) & 0xff;
192 icarry
= (al
> 0xf9);
193 if (((al
& 0x0f) > 9) || af
) {
194 al
= (al
+ 6) & 0x0f;
195 ah
= (ah
+ 1 + icarry
) & 0xff;
196 eflags
|= CC_C
| CC_A
;
198 eflags
&= ~(CC_C
| CC_A
);
201 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
205 void helper_aas(CPUX86State
*env
)
211 eflags
= cpu_cc_compute_all(env
, CC_OP
);
214 ah
= (EAX
>> 8) & 0xff;
217 if (((al
& 0x0f) > 9) || af
) {
218 al
= (al
- 6) & 0x0f;
219 ah
= (ah
- 1 - icarry
) & 0xff;
220 eflags
|= CC_C
| CC_A
;
222 eflags
&= ~(CC_C
| CC_A
);
225 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
229 void helper_daa(CPUX86State
*env
)
231 int old_al
, al
, af
, cf
;
234 eflags
= cpu_cc_compute_all(env
, CC_OP
);
237 old_al
= al
= EAX
& 0xff;
240 if (((al
& 0x0f) > 9) || af
) {
241 al
= (al
+ 6) & 0xff;
244 if ((old_al
> 0x99) || cf
) {
245 al
= (al
+ 0x60) & 0xff;
248 EAX
= (EAX
& ~0xff) | al
;
249 /* well, speed is not an issue here, so we compute the flags by hand */
250 eflags
|= (al
== 0) << 6; /* zf */
251 eflags
|= parity_table
[al
]; /* pf */
252 eflags
|= (al
& 0x80); /* sf */
256 void helper_das(CPUX86State
*env
)
261 eflags
= cpu_cc_compute_all(env
, CC_OP
);
268 if (((al
& 0x0f) > 9) || af
) {
273 al
= (al
- 6) & 0xff;
275 if ((al1
> 0x99) || cf
) {
276 al
= (al
- 0x60) & 0xff;
279 EAX
= (EAX
& ~0xff) | al
;
280 /* well, speed is not an issue here, so we compute the flags by hand */
281 eflags
|= (al
== 0) << 6; /* zf */
282 eflags
|= parity_table
[al
]; /* pf */
283 eflags
|= (al
& 0x80); /* sf */
288 static void add128(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
298 static void neg128(uint64_t *plow
, uint64_t *phigh
)
302 add128(plow
, phigh
, 1, 0);
305 /* return TRUE if overflow */
306 static int div64(uint64_t *plow
, uint64_t *phigh
, uint64_t b
)
308 uint64_t q
, r
, a1
, a0
;
322 /* XXX: use a better algorithm */
323 for (i
= 0; i
< 64; i
++) {
325 a1
= (a1
<< 1) | (a0
>> 63);
334 #if defined(DEBUG_MULDIV)
335 printf("div: 0x%016" PRIx64
"%016" PRIx64
" / 0x%016" PRIx64
336 ": q=0x%016" PRIx64
" r=0x%016" PRIx64
"\n",
337 *phigh
, *plow
, b
, a0
, a1
);
345 /* return TRUE if overflow */
346 static int idiv64(uint64_t *plow
, uint64_t *phigh
, int64_t b
)
350 sa
= ((int64_t)*phigh
< 0);
358 if (div64(plow
, phigh
, b
) != 0) {
362 if (*plow
> (1ULL << 63)) {
367 if (*plow
>= (1ULL << 63)) {
377 void helper_divq_EAX(CPUX86State
*env
, target_ulong t0
)
382 raise_exception(env
, EXCP00_DIVZ
);
386 if (div64(&r0
, &r1
, t0
)) {
387 raise_exception(env
, EXCP00_DIVZ
);
393 void helper_idivq_EAX(CPUX86State
*env
, target_ulong t0
)
398 raise_exception(env
, EXCP00_DIVZ
);
402 if (idiv64(&r0
, &r1
, t0
)) {
403 raise_exception(env
, EXCP00_DIVZ
);
410 #if TARGET_LONG_BITS == 32
419 target_ulong
helper_ctz(target_ulong t0
)
424 target_ulong
helper_clz(target_ulong t0
)
429 target_ulong
helper_pdep(target_ulong src
, target_ulong mask
)
431 target_ulong dest
= 0;
434 for (i
= 0; mask
!= 0; i
++) {
437 dest
|= ((src
>> i
) & 1) << o
;
442 target_ulong
helper_pext(target_ulong src
, target_ulong mask
)
444 target_ulong dest
= 0;
447 for (o
= 0; mask
!= 0; o
++) {
450 dest
|= ((src
>> i
) & 1) << o
;
456 #include "shift_helper_template.h"
460 #include "shift_helper_template.h"
464 #include "shift_helper_template.h"
469 #include "shift_helper_template.h"