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/>.
20 #include "qemu/osdep.h"
22 #include "exec/exec-all.h"
23 #include "qemu/host-utils.h"
24 #include "exec/helper-proto.h"
26 //#define DEBUG_MULDIV
29 static const uint8_t rclb_table
[32] = {
30 0, 1, 2, 3, 4, 5, 6, 7,
31 8, 0, 1, 2, 3, 4, 5, 6,
32 7, 8, 0, 1, 2, 3, 4, 5,
33 6, 7, 8, 0, 1, 2, 3, 4,
37 static const uint8_t rclw_table
[32] = {
38 0, 1, 2, 3, 4, 5, 6, 7,
39 8, 9, 10, 11, 12, 13, 14, 15,
40 16, 0, 1, 2, 3, 4, 5, 6,
41 7, 8, 9, 10, 11, 12, 13, 14,
44 /* division, flags are undefined */
46 void helper_divb_AL(CPUX86State
*env
, target_ulong t0
)
48 unsigned int num
, den
, q
, r
;
50 num
= (env
->regs
[R_EAX
] & 0xffff);
53 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
57 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
60 r
= (num
% den
) & 0xff;
61 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
64 void helper_idivb_AL(CPUX86State
*env
, target_ulong t0
)
68 num
= (int16_t)env
->regs
[R_EAX
];
71 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
75 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
78 r
= (num
% den
) & 0xff;
79 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | (r
<< 8) | q
;
82 void helper_divw_AX(CPUX86State
*env
, target_ulong t0
)
84 unsigned int num
, den
, q
, r
;
86 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
89 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
93 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
96 r
= (num
% den
) & 0xffff;
97 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
98 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
101 void helper_idivw_AX(CPUX86State
*env
, target_ulong t0
)
105 num
= (env
->regs
[R_EAX
] & 0xffff) | ((env
->regs
[R_EDX
] & 0xffff) << 16);
108 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
111 if (q
!= (int16_t)q
) {
112 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
115 r
= (num
% den
) & 0xffff;
116 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | q
;
117 env
->regs
[R_EDX
] = (env
->regs
[R_EDX
] & ~0xffff) | r
;
120 void helper_divl_EAX(CPUX86State
*env
, target_ulong t0
)
125 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
128 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
132 if (q
> 0xffffffff) {
133 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
135 env
->regs
[R_EAX
] = (uint32_t)q
;
136 env
->regs
[R_EDX
] = (uint32_t)r
;
139 void helper_idivl_EAX(CPUX86State
*env
, target_ulong t0
)
144 num
= ((uint32_t)env
->regs
[R_EAX
]) | ((uint64_t)((uint32_t)env
->regs
[R_EDX
]) << 32);
147 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
151 if (q
!= (int32_t)q
) {
152 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
154 env
->regs
[R_EAX
] = (uint32_t)q
;
155 env
->regs
[R_EDX
] = (uint32_t)r
;
161 void helper_aam(CPUX86State
*env
, int base
)
165 al
= env
->regs
[R_EAX
] & 0xff;
168 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
172 void helper_aad(CPUX86State
*env
, int base
)
176 al
= env
->regs
[R_EAX
] & 0xff;
177 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
178 al
= ((ah
* base
) + al
) & 0xff;
179 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
;
183 void helper_aaa(CPUX86State
*env
)
189 eflags
= cpu_cc_compute_all(env
, CC_OP
);
191 al
= env
->regs
[R_EAX
] & 0xff;
192 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
194 icarry
= (al
> 0xf9);
195 if (((al
& 0x0f) > 9) || af
) {
196 al
= (al
+ 6) & 0x0f;
197 ah
= (ah
+ 1 + icarry
) & 0xff;
198 eflags
|= CC_C
| CC_A
;
200 eflags
&= ~(CC_C
| CC_A
);
203 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
207 void helper_aas(CPUX86State
*env
)
213 eflags
= cpu_cc_compute_all(env
, CC_OP
);
215 al
= env
->regs
[R_EAX
] & 0xff;
216 ah
= (env
->regs
[R_EAX
] >> 8) & 0xff;
219 if (((al
& 0x0f) > 9) || af
) {
220 al
= (al
- 6) & 0x0f;
221 ah
= (ah
- 1 - icarry
) & 0xff;
222 eflags
|= CC_C
| CC_A
;
224 eflags
&= ~(CC_C
| CC_A
);
227 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xffff) | al
| (ah
<< 8);
231 void helper_daa(CPUX86State
*env
)
233 int old_al
, al
, af
, cf
;
236 eflags
= cpu_cc_compute_all(env
, CC_OP
);
239 old_al
= al
= env
->regs
[R_EAX
] & 0xff;
242 if (((al
& 0x0f) > 9) || af
) {
243 al
= (al
+ 6) & 0xff;
246 if ((old_al
> 0x99) || cf
) {
247 al
= (al
+ 0x60) & 0xff;
250 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
251 /* well, speed is not an issue here, so we compute the flags by hand */
252 eflags
|= (al
== 0) << 6; /* zf */
253 eflags
|= parity_table
[al
]; /* pf */
254 eflags
|= (al
& 0x80); /* sf */
258 void helper_das(CPUX86State
*env
)
263 eflags
= cpu_cc_compute_all(env
, CC_OP
);
266 al
= env
->regs
[R_EAX
] & 0xff;
270 if (((al
& 0x0f) > 9) || af
) {
275 al
= (al
- 6) & 0xff;
277 if ((al1
> 0x99) || cf
) {
278 al
= (al
- 0x60) & 0xff;
281 env
->regs
[R_EAX
] = (env
->regs
[R_EAX
] & ~0xff) | al
;
282 /* well, speed is not an issue here, so we compute the flags by hand */
283 eflags
|= (al
== 0) << 6; /* zf */
284 eflags
|= parity_table
[al
]; /* pf */
285 eflags
|= (al
& 0x80); /* sf */
290 static void add128(uint64_t *plow
, uint64_t *phigh
, uint64_t a
, uint64_t b
)
300 static void neg128(uint64_t *plow
, uint64_t *phigh
)
304 add128(plow
, phigh
, 1, 0);
307 /* return TRUE if overflow */
308 static int div64(uint64_t *plow
, uint64_t *phigh
, uint64_t b
)
310 uint64_t q
, r
, a1
, a0
;
324 /* XXX: use a better algorithm */
325 for (i
= 0; i
< 64; i
++) {
327 a1
= (a1
<< 1) | (a0
>> 63);
336 #if defined(DEBUG_MULDIV)
337 printf("div: 0x%016" PRIx64
"%016" PRIx64
" / 0x%016" PRIx64
338 ": q=0x%016" PRIx64
" r=0x%016" PRIx64
"\n",
339 *phigh
, *plow
, b
, a0
, a1
);
347 /* return TRUE if overflow */
348 static int idiv64(uint64_t *plow
, uint64_t *phigh
, int64_t b
)
352 sa
= ((int64_t)*phigh
< 0);
360 if (div64(plow
, phigh
, b
) != 0) {
364 if (*plow
> (1ULL << 63)) {
369 if (*plow
>= (1ULL << 63)) {
379 void helper_divq_EAX(CPUX86State
*env
, target_ulong t0
)
384 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
386 r0
= env
->regs
[R_EAX
];
387 r1
= env
->regs
[R_EDX
];
388 if (div64(&r0
, &r1
, t0
)) {
389 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
391 env
->regs
[R_EAX
] = r0
;
392 env
->regs
[R_EDX
] = r1
;
395 void helper_idivq_EAX(CPUX86State
*env
, target_ulong t0
)
400 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
402 r0
= env
->regs
[R_EAX
];
403 r1
= env
->regs
[R_EDX
];
404 if (idiv64(&r0
, &r1
, t0
)) {
405 raise_exception_ra(env
, EXCP00_DIVZ
, GETPC());
407 env
->regs
[R_EAX
] = r0
;
408 env
->regs
[R_EDX
] = r1
;
412 #if TARGET_LONG_BITS == 32
420 target_ulong
helper_pdep(target_ulong src
, target_ulong mask
)
422 target_ulong dest
= 0;
425 for (i
= 0; mask
!= 0; i
++) {
428 dest
|= ((src
>> i
) & 1) << o
;
433 target_ulong
helper_pext(target_ulong src
, target_ulong mask
)
435 target_ulong dest
= 0;
438 for (o
= 0; mask
!= 0; o
++) {
441 dest
|= ((src
>> i
) & 1) << o
;
447 #include "shift_helper_template.h"
451 #include "shift_helper_template.h"
455 #include "shift_helper_template.h"
460 #include "shift_helper_template.h"
464 /* Test that BIT is enabled in CR4. If not, raise an illegal opcode
465 exception. This reduces the requirements for rare CR4 bits being
466 mapped into HFLAGS. */
467 void helper_cr4_testbit(CPUX86State
*env
, uint32_t bit
)
469 if (unlikely((env
->cr
[4] & bit
) == 0)) {
470 raise_exception_ra(env
, EXCP06_ILLOP
, GETPC());