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 "qemu/host-utils.h"
23 #include "exec/helper-proto.h"
25 //#define DEBUG_MULDIV
28 static const uint8_t rclb_table[32] = {
29 0, 1, 2, 3, 4, 5, 6, 7,
30 8, 0, 1, 2, 3, 4, 5, 6,
31 7, 8, 0, 1, 2, 3, 4, 5,
32 6, 7, 8, 0, 1, 2, 3, 4,
36 static const uint8_t rclw_table[32] = {
37 0, 1, 2, 3, 4, 5, 6, 7,
38 8, 9, 10, 11, 12, 13, 14, 15,
39 16, 0, 1, 2, 3, 4, 5, 6,
40 7, 8, 9, 10, 11, 12, 13, 14,
43 /* division, flags are undefined */
45 void helper_divb_AL(CPUX86State *env, target_ulong t0)
47 unsigned int num, den, q, r;
49 num = (env->regs[R_EAX] & 0xffff);
52 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
56 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
59 r = (num % den) & 0xff;
60 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
63 void helper_idivb_AL(CPUX86State *env, target_ulong t0)
67 num = (int16_t)env->regs[R_EAX];
70 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
74 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
77 r = (num % den) & 0xff;
78 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | (r << 8) | q;
81 void helper_divw_AX(CPUX86State *env, target_ulong t0)
83 unsigned int num, den, q, r;
85 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
88 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
92 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
95 r = (num % den) & 0xffff;
96 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
97 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
100 void helper_idivw_AX(CPUX86State *env, target_ulong t0)
104 num = (env->regs[R_EAX] & 0xffff) | ((env->regs[R_EDX] & 0xffff) << 16);
107 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
110 if (q != (int16_t)q) {
111 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
114 r = (num % den) & 0xffff;
115 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | q;
116 env->regs[R_EDX] = (env->regs[R_EDX] & ~0xffff) | r;
119 void helper_divl_EAX(CPUX86State *env, target_ulong t0)
124 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
127 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
131 if (q > 0xffffffff) {
132 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
134 env->regs[R_EAX] = (uint32_t)q;
135 env->regs[R_EDX] = (uint32_t)r;
138 void helper_idivl_EAX(CPUX86State *env, target_ulong t0)
143 num = ((uint32_t)env->regs[R_EAX]) | ((uint64_t)((uint32_t)env->regs[R_EDX]) << 32);
146 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
150 if (q != (int32_t)q) {
151 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
153 env->regs[R_EAX] = (uint32_t)q;
154 env->regs[R_EDX] = (uint32_t)r;
160 void helper_aam(CPUX86State *env, int base)
164 al = env->regs[R_EAX] & 0xff;
167 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
171 void helper_aad(CPUX86State *env, int base)
175 al = env->regs[R_EAX] & 0xff;
176 ah = (env->regs[R_EAX] >> 8) & 0xff;
177 al = ((ah * base) + al) & 0xff;
178 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al;
182 void helper_aaa(CPUX86State *env)
188 eflags = cpu_cc_compute_all(env, CC_OP);
190 al = env->regs[R_EAX] & 0xff;
191 ah = (env->regs[R_EAX] >> 8) & 0xff;
193 icarry = (al > 0xf9);
194 if (((al & 0x0f) > 9) || af) {
195 al = (al + 6) & 0x0f;
196 ah = (ah + 1 + icarry) & 0xff;
197 eflags |= CC_C | CC_A;
199 eflags &= ~(CC_C | CC_A);
202 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
206 void helper_aas(CPUX86State *env)
212 eflags = cpu_cc_compute_all(env, CC_OP);
214 al = env->regs[R_EAX] & 0xff;
215 ah = (env->regs[R_EAX] >> 8) & 0xff;
218 if (((al & 0x0f) > 9) || af) {
219 al = (al - 6) & 0x0f;
220 ah = (ah - 1 - icarry) & 0xff;
221 eflags |= CC_C | CC_A;
223 eflags &= ~(CC_C | CC_A);
226 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xffff) | al | (ah << 8);
230 void helper_daa(CPUX86State *env)
232 int old_al, al, af, cf;
235 eflags = cpu_cc_compute_all(env, CC_OP);
238 old_al = al = env->regs[R_EAX] & 0xff;
241 if (((al & 0x0f) > 9) || af) {
242 al = (al + 6) & 0xff;
245 if ((old_al > 0x99) || cf) {
246 al = (al + 0x60) & 0xff;
249 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
250 /* well, speed is not an issue here, so we compute the flags by hand */
251 eflags |= (al == 0) << 6; /* zf */
252 eflags |= parity_table[al]; /* pf */
253 eflags |= (al & 0x80); /* sf */
257 void helper_das(CPUX86State *env)
262 eflags = cpu_cc_compute_all(env, CC_OP);
265 al = env->regs[R_EAX] & 0xff;
269 if (((al & 0x0f) > 9) || af) {
274 al = (al - 6) & 0xff;
276 if ((al1 > 0x99) || cf) {
277 al = (al - 0x60) & 0xff;
280 env->regs[R_EAX] = (env->regs[R_EAX] & ~0xff) | al;
281 /* well, speed is not an issue here, so we compute the flags by hand */
282 eflags |= (al == 0) << 6; /* zf */
283 eflags |= parity_table[al]; /* pf */
284 eflags |= (al & 0x80); /* sf */
289 static void add128(uint64_t *plow, uint64_t *phigh, uint64_t a, uint64_t b)
299 static void neg128(uint64_t *plow, uint64_t *phigh)
303 add128(plow, phigh, 1, 0);
306 /* return TRUE if overflow */
307 static int div64(uint64_t *plow, uint64_t *phigh, uint64_t b)
309 uint64_t q, r, a1, a0;
323 /* XXX: use a better algorithm */
324 for (i = 0; i < 64; i++) {
326 a1 = (a1 << 1) | (a0 >> 63);
335 #if defined(DEBUG_MULDIV)
336 printf("div: 0x%016" PRIx64 "%016" PRIx64 " / 0x%016" PRIx64
337 ": q=0x%016" PRIx64 " r=0x%016" PRIx64 "\n",
338 *phigh, *plow, b, a0, a1);
346 /* return TRUE if overflow */
347 static int idiv64(uint64_t *plow, uint64_t *phigh, int64_t b)
351 sa = ((int64_t)*phigh < 0);
359 if (div64(plow, phigh, b) != 0) {
363 if (*plow > (1ULL << 63)) {
368 if (*plow >= (1ULL << 63)) {
378 void helper_divq_EAX(CPUX86State *env, target_ulong t0)
383 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
385 r0 = env->regs[R_EAX];
386 r1 = env->regs[R_EDX];
387 if (div64(&r0, &r1, t0)) {
388 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
390 env->regs[R_EAX] = r0;
391 env->regs[R_EDX] = r1;
394 void helper_idivq_EAX(CPUX86State *env, target_ulong t0)
399 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
401 r0 = env->regs[R_EAX];
402 r1 = env->regs[R_EDX];
403 if (idiv64(&r0, &r1, t0)) {
404 raise_exception_ra(env, EXCP00_DIVZ, GETPC());
406 env->regs[R_EAX] = r0;
407 env->regs[R_EDX] = r1;
411 #if TARGET_LONG_BITS == 32
420 target_ulong helper_ctz(target_ulong t0)
425 target_ulong helper_clz(target_ulong t0)
430 target_ulong helper_pdep(target_ulong src, target_ulong mask)
432 target_ulong dest = 0;
435 for (i = 0; mask != 0; i++) {
438 dest |= ((src >> i) & 1) << o;
443 target_ulong helper_pext(target_ulong src, target_ulong mask)
445 target_ulong dest = 0;
448 for (o = 0; mask != 0; o++) {
451 dest |= ((src >> i) & 1) << o;
457 #include "shift_helper_template.h"
461 #include "shift_helper_template.h"
465 #include "shift_helper_template.h"
470 #include "shift_helper_template.h"