added getrusage
[qemu/ar7.git] / op-i386.c
blob64cbe708a5040d075112e95d137ade4ede9fa8e4
1 /*
2 * i386 micro operations
3 *
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, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include "exec-i386.h"
22 /* NOTE: data are not static to force relocation generation by GCC */
24 uint8_t parity_table[256] = {
25 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
26 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
27 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
28 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
29 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
30 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
31 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
32 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
33 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
34 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
35 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
36 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
37 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
38 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
39 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
40 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
41 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
42 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
43 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
44 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
45 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
46 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
47 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
48 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
49 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
50 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
51 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
52 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
53 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
54 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
55 CC_P, 0, 0, CC_P, 0, CC_P, CC_P, 0,
56 0, CC_P, CC_P, 0, CC_P, 0, 0, CC_P,
59 /* modulo 17 table */
60 const uint8_t rclw_table[32] = {
61 0, 1, 2, 3, 4, 5, 6, 7,
62 8, 9,10,11,12,13,14,15,
63 16, 0, 1, 2, 3, 4, 5, 6,
64 7, 8, 9,10,11,12,13,14,
67 /* modulo 9 table */
68 const uint8_t rclb_table[32] = {
69 0, 1, 2, 3, 4, 5, 6, 7,
70 8, 0, 1, 2, 3, 4, 5, 6,
71 7, 8, 0, 1, 2, 3, 4, 5,
72 6, 7, 8, 0, 1, 2, 3, 4,
75 #ifdef USE_X86LDOUBLE
76 /* an array of Intel 80-bit FP constants, to be loaded via integer ops */
77 typedef unsigned short f15ld[5];
78 const f15ld f15rk[] =
80 /*0*/ {0x0000,0x0000,0x0000,0x0000,0x0000},
81 /*1*/ {0x0000,0x0000,0x0000,0x8000,0x3fff},
82 /*pi*/ {0xc235,0x2168,0xdaa2,0xc90f,0x4000},
83 /*lg2*/ {0xf799,0xfbcf,0x9a84,0x9a20,0x3ffd},
84 /*ln2*/ {0x79ac,0xd1cf,0x17f7,0xb172,0x3ffe},
85 /*l2e*/ {0xf0bc,0x5c17,0x3b29,0xb8aa,0x3fff},
86 /*l2t*/ {0x8afe,0xcd1b,0x784b,0xd49a,0x4000}
88 #else
89 /* the same, 64-bit version */
90 typedef unsigned short f15ld[4];
91 const f15ld f15rk[] =
93 #ifndef WORDS_BIGENDIAN
94 /*0*/ {0x0000,0x0000,0x0000,0x0000},
95 /*1*/ {0x0000,0x0000,0x0000,0x3ff0},
96 /*pi*/ {0x2d18,0x5444,0x21fb,0x4009},
97 /*lg2*/ {0x79ff,0x509f,0x4413,0x3fd3},
98 /*ln2*/ {0x39ef,0xfefa,0x2e42,0x3fe6},
99 /*l2e*/ {0x82fe,0x652b,0x1547,0x3ff7},
100 /*l2t*/ {0xa371,0x0979,0x934f,0x400a}
101 #else
102 /*0*/ {0x0000,0x0000,0x0000,0x0000},
103 /*1*/ {0x3ff0,0x0000,0x0000,0x0000},
104 /*pi*/ {0x4009,0x21fb,0x5444,0x2d18},
105 /*lg2*/ {0x3fd3,0x4413,0x509f,0x79ff},
106 /*ln2*/ {0x3fe6,0x2e42,0xfefa,0x39ef},
107 /*l2e*/ {0x3ff7,0x1547,0x652b,0x82fe},
108 /*l2t*/ {0x400a,0x934f,0x0979,0xa371}
109 #endif
111 #endif
113 /* n must be a constant to be efficient */
114 static inline int lshift(int x, int n)
116 if (n >= 0)
117 return x << n;
118 else
119 return x >> (-n);
122 /* we define the various pieces of code used by the JIT */
124 #define REG EAX
125 #define REGNAME _EAX
126 #include "opreg_template.h"
127 #undef REG
128 #undef REGNAME
130 #define REG ECX
131 #define REGNAME _ECX
132 #include "opreg_template.h"
133 #undef REG
134 #undef REGNAME
136 #define REG EDX
137 #define REGNAME _EDX
138 #include "opreg_template.h"
139 #undef REG
140 #undef REGNAME
142 #define REG EBX
143 #define REGNAME _EBX
144 #include "opreg_template.h"
145 #undef REG
146 #undef REGNAME
148 #define REG ESP
149 #define REGNAME _ESP
150 #include "opreg_template.h"
151 #undef REG
152 #undef REGNAME
154 #define REG EBP
155 #define REGNAME _EBP
156 #include "opreg_template.h"
157 #undef REG
158 #undef REGNAME
160 #define REG ESI
161 #define REGNAME _ESI
162 #include "opreg_template.h"
163 #undef REG
164 #undef REGNAME
166 #define REG EDI
167 #define REGNAME _EDI
168 #include "opreg_template.h"
169 #undef REG
170 #undef REGNAME
172 /* operations with flags */
174 void OPPROTO op_addl_T0_T1_cc(void)
176 CC_SRC = T0;
177 T0 += T1;
178 CC_DST = T0;
181 void OPPROTO op_orl_T0_T1_cc(void)
183 T0 |= T1;
184 CC_DST = T0;
187 void OPPROTO op_andl_T0_T1_cc(void)
189 T0 &= T1;
190 CC_DST = T0;
193 void OPPROTO op_subl_T0_T1_cc(void)
195 CC_SRC = T0;
196 T0 -= T1;
197 CC_DST = T0;
200 void OPPROTO op_xorl_T0_T1_cc(void)
202 T0 ^= T1;
203 CC_DST = T0;
206 void OPPROTO op_cmpl_T0_T1_cc(void)
208 CC_SRC = T0;
209 CC_DST = T0 - T1;
212 void OPPROTO op_negl_T0_cc(void)
214 CC_SRC = 0;
215 T0 = -T0;
216 CC_DST = T0;
219 void OPPROTO op_incl_T0_cc(void)
221 CC_SRC = cc_table[CC_OP].compute_c();
222 T0++;
223 CC_DST = T0;
226 void OPPROTO op_decl_T0_cc(void)
228 CC_SRC = cc_table[CC_OP].compute_c();
229 T0--;
230 CC_DST = T0;
233 void OPPROTO op_testl_T0_T1_cc(void)
235 CC_DST = T0 & T1;
238 /* operations without flags */
240 void OPPROTO op_addl_T0_T1(void)
242 T0 += T1;
245 void OPPROTO op_orl_T0_T1(void)
247 T0 |= T1;
250 void OPPROTO op_andl_T0_T1(void)
252 T0 &= T1;
255 void OPPROTO op_subl_T0_T1(void)
257 T0 -= T1;
260 void OPPROTO op_xorl_T0_T1(void)
262 T0 ^= T1;
265 void OPPROTO op_negl_T0(void)
267 T0 = -T0;
270 void OPPROTO op_incl_T0(void)
272 T0++;
275 void OPPROTO op_decl_T0(void)
277 T0--;
280 void OPPROTO op_notl_T0(void)
282 T0 = ~T0;
285 void OPPROTO op_bswapl_T0(void)
287 T0 = bswap32(T0);
290 /* multiply/divide */
291 void OPPROTO op_mulb_AL_T0(void)
293 unsigned int res;
294 res = (uint8_t)EAX * (uint8_t)T0;
295 EAX = (EAX & 0xffff0000) | res;
296 CC_SRC = (res & 0xff00);
299 void OPPROTO op_imulb_AL_T0(void)
301 int res;
302 res = (int8_t)EAX * (int8_t)T0;
303 EAX = (EAX & 0xffff0000) | (res & 0xffff);
304 CC_SRC = (res != (int8_t)res);
307 void OPPROTO op_mulw_AX_T0(void)
309 unsigned int res;
310 res = (uint16_t)EAX * (uint16_t)T0;
311 EAX = (EAX & 0xffff0000) | (res & 0xffff);
312 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
313 CC_SRC = res >> 16;
316 void OPPROTO op_imulw_AX_T0(void)
318 int res;
319 res = (int16_t)EAX * (int16_t)T0;
320 EAX = (EAX & 0xffff0000) | (res & 0xffff);
321 EDX = (EDX & 0xffff0000) | ((res >> 16) & 0xffff);
322 CC_SRC = (res != (int16_t)res);
325 void OPPROTO op_mull_EAX_T0(void)
327 uint64_t res;
328 res = (uint64_t)((uint32_t)EAX) * (uint64_t)((uint32_t)T0);
329 EAX = res;
330 EDX = res >> 32;
331 CC_SRC = res >> 32;
334 void OPPROTO op_imull_EAX_T0(void)
336 int64_t res;
337 res = (int64_t)((int32_t)EAX) * (int64_t)((int32_t)T0);
338 EAX = res;
339 EDX = res >> 32;
340 CC_SRC = (res != (int32_t)res);
343 void OPPROTO op_imulw_T0_T1(void)
345 int res;
346 res = (int16_t)T0 * (int16_t)T1;
347 T0 = res;
348 CC_SRC = (res != (int16_t)res);
351 void OPPROTO op_imull_T0_T1(void)
353 int64_t res;
354 res = (int64_t)((int32_t)T0) * (int64_t)((int32_t)T1);
355 T0 = res;
356 CC_SRC = (res != (int32_t)res);
359 /* division, flags are undefined */
360 /* XXX: add exceptions for overflow */
361 void OPPROTO op_divb_AL_T0(void)
363 unsigned int num, den, q, r;
365 num = (EAX & 0xffff);
366 den = (T0 & 0xff);
367 if (den == 0)
368 raise_exception(EXCP00_DIVZ);
369 q = (num / den) & 0xff;
370 r = (num % den) & 0xff;
371 EAX = (EAX & 0xffff0000) | (r << 8) | q;
374 void OPPROTO op_idivb_AL_T0(void)
376 int num, den, q, r;
378 num = (int16_t)EAX;
379 den = (int8_t)T0;
380 if (den == 0)
381 raise_exception(EXCP00_DIVZ);
382 q = (num / den) & 0xff;
383 r = (num % den) & 0xff;
384 EAX = (EAX & 0xffff0000) | (r << 8) | q;
387 void OPPROTO op_divw_AX_T0(void)
389 unsigned int num, den, q, r;
391 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
392 den = (T0 & 0xffff);
393 if (den == 0)
394 raise_exception(EXCP00_DIVZ);
395 q = (num / den) & 0xffff;
396 r = (num % den) & 0xffff;
397 EAX = (EAX & 0xffff0000) | q;
398 EDX = (EDX & 0xffff0000) | r;
401 void OPPROTO op_idivw_AX_T0(void)
403 int num, den, q, r;
405 num = (EAX & 0xffff) | ((EDX & 0xffff) << 16);
406 den = (int16_t)T0;
407 if (den == 0)
408 raise_exception(EXCP00_DIVZ);
409 q = (num / den) & 0xffff;
410 r = (num % den) & 0xffff;
411 EAX = (EAX & 0xffff0000) | q;
412 EDX = (EDX & 0xffff0000) | r;
415 #ifdef BUGGY_GCC_DIV64
416 /* gcc 2.95.4 on PowerPC does not seem to like using __udivdi3, so we
417 call it from another function */
418 uint32_t div64(uint32_t *q_ptr, uint64_t num, uint32_t den)
420 *q_ptr = num / den;
421 return num % den;
424 int32_t idiv64(int32_t *q_ptr, int64_t num, int32_t den)
426 *q_ptr = num / den;
427 return num % den;
429 #endif
431 void OPPROTO op_divl_EAX_T0(void)
433 unsigned int den, q, r;
434 uint64_t num;
436 num = EAX | ((uint64_t)EDX << 32);
437 den = T0;
438 if (den == 0)
439 raise_exception(EXCP00_DIVZ);
440 #ifdef BUGGY_GCC_DIV64
441 r = div64(&q, num, den);
442 #else
443 q = (num / den);
444 r = (num % den);
445 #endif
446 EAX = q;
447 EDX = r;
450 void OPPROTO op_idivl_EAX_T0(void)
452 int den, q, r;
453 int64_t num;
455 num = EAX | ((uint64_t)EDX << 32);
456 den = T0;
457 if (den == 0)
458 raise_exception(EXCP00_DIVZ);
459 #ifdef BUGGY_GCC_DIV64
460 r = idiv64(&q, num, den);
461 #else
462 q = (num / den);
463 r = (num % den);
464 #endif
465 EAX = q;
466 EDX = r;
469 /* constant load & misc op */
471 void OPPROTO op_movl_T0_im(void)
473 T0 = PARAM1;
476 void OPPROTO op_addl_T0_im(void)
478 T0 += PARAM1;
481 void OPPROTO op_andl_T0_ffff(void)
483 T0 = T0 & 0xffff;
486 void OPPROTO op_movl_T0_T1(void)
488 T0 = T1;
491 void OPPROTO op_movl_T1_im(void)
493 T1 = PARAM1;
496 void OPPROTO op_addl_T1_im(void)
498 T1 += PARAM1;
501 void OPPROTO op_movl_T1_A0(void)
503 T1 = A0;
506 void OPPROTO op_movl_A0_im(void)
508 A0 = PARAM1;
511 void OPPROTO op_addl_A0_im(void)
513 A0 += PARAM1;
516 void OPPROTO op_addl_A0_AL(void)
518 A0 += (EAX & 0xff);
521 void OPPROTO op_andl_A0_ffff(void)
523 A0 = A0 & 0xffff;
526 /* memory access */
528 void OPPROTO op_ldub_T0_A0(void)
530 T0 = ldub((uint8_t *)A0);
533 void OPPROTO op_ldsb_T0_A0(void)
535 T0 = ldsb((int8_t *)A0);
538 void OPPROTO op_lduw_T0_A0(void)
540 T0 = lduw((uint8_t *)A0);
543 void OPPROTO op_ldsw_T0_A0(void)
545 T0 = ldsw((int8_t *)A0);
548 void OPPROTO op_ldl_T0_A0(void)
550 T0 = ldl((uint8_t *)A0);
553 void OPPROTO op_ldub_T1_A0(void)
555 T1 = ldub((uint8_t *)A0);
558 void OPPROTO op_ldsb_T1_A0(void)
560 T1 = ldsb((int8_t *)A0);
563 void OPPROTO op_lduw_T1_A0(void)
565 T1 = lduw((uint8_t *)A0);
568 void OPPROTO op_ldsw_T1_A0(void)
570 T1 = ldsw((int8_t *)A0);
573 void OPPROTO op_ldl_T1_A0(void)
575 T1 = ldl((uint8_t *)A0);
578 void OPPROTO op_stb_T0_A0(void)
580 stb((uint8_t *)A0, T0);
583 void OPPROTO op_stw_T0_A0(void)
585 stw((uint8_t *)A0, T0);
588 void OPPROTO op_stl_T0_A0(void)
590 stl((uint8_t *)A0, T0);
593 /* used for bit operations */
595 void OPPROTO op_add_bitw_A0_T1(void)
597 A0 += ((int32_t)T1 >> 4) << 1;
600 void OPPROTO op_add_bitl_A0_T1(void)
602 A0 += ((int32_t)T1 >> 5) << 2;
605 /* indirect jump */
607 void OPPROTO op_jmp_T0(void)
609 EIP = T0;
612 void OPPROTO op_jmp_im(void)
614 EIP = PARAM1;
617 void OPPROTO op_int_im(void)
619 int intno;
620 intno = PARAM1;
621 EIP = PARAM2;
622 raise_exception_err(EXCP0D_GPF, intno * 8 + 2);
625 void OPPROTO op_raise_exception(void)
627 int exception_index;
628 exception_index = PARAM1;
629 raise_exception(exception_index);
632 void OPPROTO op_into(void)
634 int eflags;
635 eflags = cc_table[CC_OP].compute_all();
636 if (eflags & CC_O) {
637 EIP = PARAM1;
638 raise_exception(EXCP04_INTO);
640 FORCE_RET();
643 void OPPROTO op_cli(void)
645 env->eflags &= ~IF_MASK;
648 void OPPROTO op_sti(void)
650 env->eflags |= IF_MASK;
653 #if 0
654 /* vm86plus instructions */
655 void OPPROTO op_cli_vm(void)
657 env->eflags &= ~VIF_MASK;
660 void OPPROTO op_sti_vm(void)
662 env->eflags |= VIF_MASK;
663 if (env->eflags & VIP_MASK) {
664 EIP = PARAM1;
665 raise_exception(EXCP0D_GPF);
667 FORCE_RET();
669 #endif
671 void OPPROTO op_boundw(void)
673 int low, high, v;
674 low = ldsw((uint8_t *)A0);
675 high = ldsw((uint8_t *)A0 + 2);
676 v = (int16_t)T0;
677 if (v < low || v > high)
678 raise_exception(EXCP05_BOUND);
679 FORCE_RET();
682 void OPPROTO op_boundl(void)
684 int low, high, v;
685 low = ldl((uint8_t *)A0);
686 high = ldl((uint8_t *)A0 + 4);
687 v = T0;
688 if (v < low || v > high)
689 raise_exception(EXCP05_BOUND);
690 FORCE_RET();
693 void OPPROTO op_cmpxchg8b(void)
695 uint64_t d;
696 int eflags;
698 eflags = cc_table[CC_OP].compute_all();
699 d = ldq((uint8_t *)A0);
700 if (d == (((uint64_t)EDX << 32) | EAX)) {
701 stq((uint8_t *)A0, ((uint64_t)ECX << 32) | EBX);
702 eflags |= CC_Z;
703 } else {
704 EDX = d >> 32;
705 EAX = d;
706 eflags &= ~CC_Z;
708 CC_SRC = eflags;
709 FORCE_RET();
712 /* string ops */
714 #define ldul ldl
716 #define SHIFT 0
717 #include "ops_template.h"
718 #undef SHIFT
720 #define SHIFT 1
721 #include "ops_template.h"
722 #undef SHIFT
724 #define SHIFT 2
725 #include "ops_template.h"
726 #undef SHIFT
728 /* sign extend */
730 void OPPROTO op_movsbl_T0_T0(void)
732 T0 = (int8_t)T0;
735 void OPPROTO op_movzbl_T0_T0(void)
737 T0 = (uint8_t)T0;
740 void OPPROTO op_movswl_T0_T0(void)
742 T0 = (int16_t)T0;
745 void OPPROTO op_movzwl_T0_T0(void)
747 T0 = (uint16_t)T0;
750 void OPPROTO op_movswl_EAX_AX(void)
752 EAX = (int16_t)EAX;
755 void OPPROTO op_movsbw_AX_AL(void)
757 EAX = (EAX & 0xffff0000) | ((int8_t)EAX & 0xffff);
760 void OPPROTO op_movslq_EDX_EAX(void)
762 EDX = (int32_t)EAX >> 31;
765 void OPPROTO op_movswl_DX_AX(void)
767 EDX = (EDX & 0xffff0000) | (((int16_t)EAX >> 15) & 0xffff);
770 /* push/pop */
772 void op_pushl_T0(void)
774 uint32_t offset;
775 offset = ESP - 4;
776 stl((void *)offset, T0);
777 /* modify ESP after to handle exceptions correctly */
778 ESP = offset;
781 void op_pushw_T0(void)
783 uint32_t offset;
784 offset = ESP - 2;
785 stw((void *)offset, T0);
786 /* modify ESP after to handle exceptions correctly */
787 ESP = offset;
790 void op_pushl_ss32_T0(void)
792 uint32_t offset;
793 offset = ESP - 4;
794 stl(env->seg_cache[R_SS].base + offset, T0);
795 /* modify ESP after to handle exceptions correctly */
796 ESP = offset;
799 void op_pushw_ss32_T0(void)
801 uint32_t offset;
802 offset = ESP - 2;
803 stw(env->seg_cache[R_SS].base + offset, T0);
804 /* modify ESP after to handle exceptions correctly */
805 ESP = offset;
808 void op_pushl_ss16_T0(void)
810 uint32_t offset;
811 offset = (ESP - 4) & 0xffff;
812 stl(env->seg_cache[R_SS].base + offset, T0);
813 /* modify ESP after to handle exceptions correctly */
814 ESP = (ESP & ~0xffff) | offset;
817 void op_pushw_ss16_T0(void)
819 uint32_t offset;
820 offset = (ESP - 2) & 0xffff;
821 stw(env->seg_cache[R_SS].base + offset, T0);
822 /* modify ESP after to handle exceptions correctly */
823 ESP = (ESP & ~0xffff) | offset;
826 /* NOTE: ESP update is done after */
827 void op_popl_T0(void)
829 T0 = ldl((void *)ESP);
832 void op_popw_T0(void)
834 T0 = lduw((void *)ESP);
837 void op_popl_ss32_T0(void)
839 T0 = ldl(env->seg_cache[R_SS].base + ESP);
842 void op_popw_ss32_T0(void)
844 T0 = lduw(env->seg_cache[R_SS].base + ESP);
847 void op_popl_ss16_T0(void)
849 T0 = ldl(env->seg_cache[R_SS].base + (ESP & 0xffff));
852 void op_popw_ss16_T0(void)
854 T0 = lduw(env->seg_cache[R_SS].base + (ESP & 0xffff));
857 void op_addl_ESP_4(void)
859 ESP += 4;
862 void op_addl_ESP_2(void)
864 ESP += 2;
867 void op_addw_ESP_4(void)
869 ESP = (ESP & ~0xffff) | ((ESP + 4) & 0xffff);
872 void op_addw_ESP_2(void)
874 ESP = (ESP & ~0xffff) | ((ESP + 2) & 0xffff);
877 void op_addl_ESP_im(void)
879 ESP += PARAM1;
882 void op_addw_ESP_im(void)
884 ESP = (ESP & ~0xffff) | ((ESP + PARAM1) & 0xffff);
887 /* rdtsc */
888 #ifndef __i386__
889 uint64_t emu_time;
890 #endif
892 void OPPROTO op_rdtsc(void)
894 uint64_t val;
895 #ifdef __i386__
896 asm("rdtsc" : "=A" (val));
897 #else
898 /* better than nothing: the time increases */
899 val = emu_time++;
900 #endif
901 EAX = val;
902 EDX = val >> 32;
905 /* We simulate a pre-MMX pentium as in valgrind */
906 #define CPUID_FP87 (1 << 0)
907 #define CPUID_VME (1 << 1)
908 #define CPUID_DE (1 << 2)
909 #define CPUID_PSE (1 << 3)
910 #define CPUID_TSC (1 << 4)
911 #define CPUID_MSR (1 << 5)
912 #define CPUID_PAE (1 << 6)
913 #define CPUID_MCE (1 << 7)
914 #define CPUID_CX8 (1 << 8)
915 #define CPUID_APIC (1 << 9)
916 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
917 #define CPUID_MTRR (1 << 12)
918 #define CPUID_PGE (1 << 13)
919 #define CPUID_MCA (1 << 14)
920 #define CPUID_CMOV (1 << 15)
921 /* ... */
922 #define CPUID_MMX (1 << 23)
923 #define CPUID_FXSR (1 << 24)
924 #define CPUID_SSE (1 << 25)
925 #define CPUID_SSE2 (1 << 26)
927 void helper_cpuid(void)
929 if (EAX == 0) {
930 EAX = 1; /* max EAX index supported */
931 EBX = 0x756e6547;
932 ECX = 0x6c65746e;
933 EDX = 0x49656e69;
934 } else {
935 /* EAX = 1 info */
936 EAX = 0x52b;
937 EBX = 0;
938 ECX = 0;
939 EDX = CPUID_FP87 | CPUID_DE | CPUID_PSE |
940 CPUID_TSC | CPUID_MSR | CPUID_MCE |
941 CPUID_CX8;
945 void OPPROTO op_cpuid(void)
947 helper_cpuid();
950 /* bcd */
952 /* XXX: exception */
953 void OPPROTO op_aam(void)
955 int base = PARAM1;
956 int al, ah;
957 al = EAX & 0xff;
958 ah = al / base;
959 al = al % base;
960 EAX = (EAX & ~0xffff) | al | (ah << 8);
961 CC_DST = al;
964 void OPPROTO op_aad(void)
966 int base = PARAM1;
967 int al, ah;
968 al = EAX & 0xff;
969 ah = (EAX >> 8) & 0xff;
970 al = ((ah * base) + al) & 0xff;
971 EAX = (EAX & ~0xffff) | al;
972 CC_DST = al;
975 void OPPROTO op_aaa(void)
977 int icarry;
978 int al, ah, af;
979 int eflags;
981 eflags = cc_table[CC_OP].compute_all();
982 af = eflags & CC_A;
983 al = EAX & 0xff;
984 ah = (EAX >> 8) & 0xff;
986 icarry = (al > 0xf9);
987 if (((al & 0x0f) > 9 ) || af) {
988 al = (al + 6) & 0x0f;
989 ah = (ah + 1 + icarry) & 0xff;
990 eflags |= CC_C | CC_A;
991 } else {
992 eflags &= ~(CC_C | CC_A);
993 al &= 0x0f;
995 EAX = (EAX & ~0xffff) | al | (ah << 8);
996 CC_SRC = eflags;
999 void OPPROTO op_aas(void)
1001 int icarry;
1002 int al, ah, af;
1003 int eflags;
1005 eflags = cc_table[CC_OP].compute_all();
1006 af = eflags & CC_A;
1007 al = EAX & 0xff;
1008 ah = (EAX >> 8) & 0xff;
1010 icarry = (al < 6);
1011 if (((al & 0x0f) > 9 ) || af) {
1012 al = (al - 6) & 0x0f;
1013 ah = (ah - 1 - icarry) & 0xff;
1014 eflags |= CC_C | CC_A;
1015 } else {
1016 eflags &= ~(CC_C | CC_A);
1017 al &= 0x0f;
1019 EAX = (EAX & ~0xffff) | al | (ah << 8);
1020 CC_SRC = eflags;
1023 void OPPROTO op_daa(void)
1025 int al, af, cf;
1026 int eflags;
1028 eflags = cc_table[CC_OP].compute_all();
1029 cf = eflags & CC_C;
1030 af = eflags & CC_A;
1031 al = EAX & 0xff;
1033 eflags = 0;
1034 if (((al & 0x0f) > 9 ) || af) {
1035 al = (al + 6) & 0xff;
1036 eflags |= CC_A;
1038 if ((al > 0x9f) || cf) {
1039 al = (al + 0x60) & 0xff;
1040 eflags |= CC_C;
1042 EAX = (EAX & ~0xff) | al;
1043 /* well, speed is not an issue here, so we compute the flags by hand */
1044 eflags |= (al == 0) << 6; /* zf */
1045 eflags |= parity_table[al]; /* pf */
1046 eflags |= (al & 0x80); /* sf */
1047 CC_SRC = eflags;
1050 void OPPROTO op_das(void)
1052 int al, al1, af, cf;
1053 int eflags;
1055 eflags = cc_table[CC_OP].compute_all();
1056 cf = eflags & CC_C;
1057 af = eflags & CC_A;
1058 al = EAX & 0xff;
1060 eflags = 0;
1061 al1 = al;
1062 if (((al & 0x0f) > 9 ) || af) {
1063 eflags |= CC_A;
1064 if (al < 6 || cf)
1065 eflags |= CC_C;
1066 al = (al - 6) & 0xff;
1068 if ((al1 > 0x99) || cf) {
1069 al = (al - 0x60) & 0xff;
1070 eflags |= CC_C;
1072 EAX = (EAX & ~0xff) | al;
1073 /* well, speed is not an issue here, so we compute the flags by hand */
1074 eflags |= (al == 0) << 6; /* zf */
1075 eflags |= parity_table[al]; /* pf */
1076 eflags |= (al & 0x80); /* sf */
1077 CC_SRC = eflags;
1080 /* segment handling */
1082 /* XXX: use static VM86 information */
1083 void load_seg(int seg_reg, int selector)
1085 SegmentCache *sc;
1086 SegmentDescriptorTable *dt;
1087 int index;
1088 uint32_t e1, e2;
1089 uint8_t *ptr;
1091 sc = &env->seg_cache[seg_reg];
1092 if (env->eflags & VM_MASK) {
1093 sc->base = (void *)(selector << 4);
1094 sc->limit = 0xffff;
1095 sc->seg_32bit = 0;
1096 } else {
1097 if (selector & 0x4)
1098 dt = &env->ldt;
1099 else
1100 dt = &env->gdt;
1101 index = selector & ~7;
1102 if ((index + 7) > dt->limit)
1103 raise_exception_err(EXCP0D_GPF, selector);
1104 ptr = dt->base + index;
1105 e1 = ldl(ptr);
1106 e2 = ldl(ptr + 4);
1107 sc->base = (void *)((e1 >> 16) | ((e2 & 0xff) << 16) | (e2 & 0xff000000));
1108 sc->limit = (e1 & 0xffff) | (e2 & 0x000f0000);
1109 if (e2 & (1 << 23))
1110 sc->limit = (sc->limit << 12) | 0xfff;
1111 sc->seg_32bit = (e2 >> 22) & 1;
1112 #if 0
1113 fprintf(logfile, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx seg_32bit=%d\n",
1114 selector, (unsigned long)sc->base, sc->limit, sc->seg_32bit);
1115 #endif
1117 env->segs[seg_reg] = selector;
1120 void OPPROTO op_movl_seg_T0(void)
1122 load_seg(PARAM1, T0 & 0xffff);
1125 void OPPROTO op_movl_T0_seg(void)
1127 T0 = env->segs[PARAM1];
1130 void OPPROTO op_movl_A0_seg(void)
1132 A0 = *(unsigned long *)((char *)env + PARAM1);
1135 void OPPROTO op_addl_A0_seg(void)
1137 A0 += *(unsigned long *)((char *)env + PARAM1);
1140 void helper_lsl(void)
1142 unsigned int selector, limit;
1143 SegmentDescriptorTable *dt;
1144 int index;
1145 uint32_t e1, e2;
1146 uint8_t *ptr;
1148 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1149 selector = T0 & 0xffff;
1150 if (selector & 0x4)
1151 dt = &env->ldt;
1152 else
1153 dt = &env->gdt;
1154 index = selector & ~7;
1155 if ((index + 7) > dt->limit)
1156 return;
1157 ptr = dt->base + index;
1158 e1 = ldl(ptr);
1159 e2 = ldl(ptr + 4);
1160 limit = (e1 & 0xffff) | (e2 & 0x000f0000);
1161 if (e2 & (1 << 23))
1162 limit = (limit << 12) | 0xfff;
1163 T1 = limit;
1164 CC_SRC |= CC_Z;
1167 void OPPROTO op_lsl(void)
1169 helper_lsl();
1172 void helper_lar(void)
1174 unsigned int selector;
1175 SegmentDescriptorTable *dt;
1176 int index;
1177 uint32_t e2;
1178 uint8_t *ptr;
1180 CC_SRC = cc_table[CC_OP].compute_all() & ~CC_Z;
1181 selector = T0 & 0xffff;
1182 if (selector & 0x4)
1183 dt = &env->ldt;
1184 else
1185 dt = &env->gdt;
1186 index = selector & ~7;
1187 if ((index + 7) > dt->limit)
1188 return;
1189 ptr = dt->base + index;
1190 e2 = ldl(ptr + 4);
1191 T1 = e2 & 0x00f0ff00;
1192 CC_SRC |= CC_Z;
1195 void OPPROTO op_lar(void)
1197 helper_lar();
1200 /* flags handling */
1202 /* slow jumps cases (compute x86 flags) */
1203 void OPPROTO op_jo_cc(void)
1205 int eflags;
1206 eflags = cc_table[CC_OP].compute_all();
1207 if (eflags & CC_O)
1208 EIP = PARAM1;
1209 else
1210 EIP = PARAM2;
1211 FORCE_RET();
1214 void OPPROTO op_jb_cc(void)
1216 if (cc_table[CC_OP].compute_c())
1217 EIP = PARAM1;
1218 else
1219 EIP = PARAM2;
1220 FORCE_RET();
1223 void OPPROTO op_jz_cc(void)
1225 int eflags;
1226 eflags = cc_table[CC_OP].compute_all();
1227 if (eflags & CC_Z)
1228 EIP = PARAM1;
1229 else
1230 EIP = PARAM2;
1231 FORCE_RET();
1234 void OPPROTO op_jbe_cc(void)
1236 int eflags;
1237 eflags = cc_table[CC_OP].compute_all();
1238 if (eflags & (CC_Z | CC_C))
1239 EIP = PARAM1;
1240 else
1241 EIP = PARAM2;
1242 FORCE_RET();
1245 void OPPROTO op_js_cc(void)
1247 int eflags;
1248 eflags = cc_table[CC_OP].compute_all();
1249 if (eflags & CC_S)
1250 EIP = PARAM1;
1251 else
1252 EIP = PARAM2;
1253 FORCE_RET();
1256 void OPPROTO op_jp_cc(void)
1258 int eflags;
1259 eflags = cc_table[CC_OP].compute_all();
1260 if (eflags & CC_P)
1261 EIP = PARAM1;
1262 else
1263 EIP = PARAM2;
1264 FORCE_RET();
1267 void OPPROTO op_jl_cc(void)
1269 int eflags;
1270 eflags = cc_table[CC_OP].compute_all();
1271 if ((eflags ^ (eflags >> 4)) & 0x80)
1272 EIP = PARAM1;
1273 else
1274 EIP = PARAM2;
1275 FORCE_RET();
1278 void OPPROTO op_jle_cc(void)
1280 int eflags;
1281 eflags = cc_table[CC_OP].compute_all();
1282 if (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z))
1283 EIP = PARAM1;
1284 else
1285 EIP = PARAM2;
1286 FORCE_RET();
1289 /* slow set cases (compute x86 flags) */
1290 void OPPROTO op_seto_T0_cc(void)
1292 int eflags;
1293 eflags = cc_table[CC_OP].compute_all();
1294 T0 = (eflags >> 11) & 1;
1297 void OPPROTO op_setb_T0_cc(void)
1299 T0 = cc_table[CC_OP].compute_c();
1302 void OPPROTO op_setz_T0_cc(void)
1304 int eflags;
1305 eflags = cc_table[CC_OP].compute_all();
1306 T0 = (eflags >> 6) & 1;
1309 void OPPROTO op_setbe_T0_cc(void)
1311 int eflags;
1312 eflags = cc_table[CC_OP].compute_all();
1313 T0 = (eflags & (CC_Z | CC_C)) != 0;
1316 void OPPROTO op_sets_T0_cc(void)
1318 int eflags;
1319 eflags = cc_table[CC_OP].compute_all();
1320 T0 = (eflags >> 7) & 1;
1323 void OPPROTO op_setp_T0_cc(void)
1325 int eflags;
1326 eflags = cc_table[CC_OP].compute_all();
1327 T0 = (eflags >> 2) & 1;
1330 void OPPROTO op_setl_T0_cc(void)
1332 int eflags;
1333 eflags = cc_table[CC_OP].compute_all();
1334 T0 = ((eflags ^ (eflags >> 4)) >> 7) & 1;
1337 void OPPROTO op_setle_T0_cc(void)
1339 int eflags;
1340 eflags = cc_table[CC_OP].compute_all();
1341 T0 = (((eflags ^ (eflags >> 4)) & 0x80) || (eflags & CC_Z)) != 0;
1344 void OPPROTO op_xor_T0_1(void)
1346 T0 ^= 1;
1349 void OPPROTO op_set_cc_op(void)
1351 CC_OP = PARAM1;
1354 #define FL_UPDATE_MASK32 (TF_MASK | AC_MASK | ID_MASK)
1355 #define FL_UPDATE_MASK16 (TF_MASK)
1357 void OPPROTO op_movl_eflags_T0(void)
1359 int eflags;
1360 eflags = T0;
1361 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1362 DF = 1 - (2 * ((eflags >> 10) & 1));
1363 /* we also update some system flags as in user mode */
1364 env->eflags = (env->eflags & ~FL_UPDATE_MASK32) | (eflags & FL_UPDATE_MASK32);
1367 void OPPROTO op_movw_eflags_T0(void)
1369 int eflags;
1370 eflags = T0;
1371 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1372 DF = 1 - (2 * ((eflags >> 10) & 1));
1373 /* we also update some system flags as in user mode */
1374 env->eflags = (env->eflags & ~FL_UPDATE_MASK16) | (eflags & FL_UPDATE_MASK16);
1377 #if 0
1378 /* vm86plus version */
1379 void OPPROTO op_movw_eflags_T0_vm(void)
1381 int eflags;
1382 eflags = T0;
1383 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1384 DF = 1 - (2 * ((eflags >> 10) & 1));
1385 /* we also update some system flags as in user mode */
1386 env->eflags = (env->eflags & ~(FL_UPDATE_MASK16 | VIF_MASK)) |
1387 (eflags & FL_UPDATE_MASK16);
1388 if (eflags & IF_MASK) {
1389 env->eflags |= VIF_MASK;
1390 if (env->eflags & VIP_MASK) {
1391 EIP = PARAM1;
1392 raise_exception(EXCP0D_GPF);
1395 FORCE_RET();
1398 void OPPROTO op_movl_eflags_T0_vm(void)
1400 int eflags;
1401 eflags = T0;
1402 CC_SRC = eflags & (CC_O | CC_S | CC_Z | CC_A | CC_P | CC_C);
1403 DF = 1 - (2 * ((eflags >> 10) & 1));
1404 /* we also update some system flags as in user mode */
1405 env->eflags = (env->eflags & ~(FL_UPDATE_MASK32 | VIF_MASK)) |
1406 (eflags & FL_UPDATE_MASK32);
1407 if (eflags & IF_MASK) {
1408 env->eflags |= VIF_MASK;
1409 if (env->eflags & VIP_MASK) {
1410 EIP = PARAM1;
1411 raise_exception(EXCP0D_GPF);
1414 FORCE_RET();
1416 #endif
1418 /* XXX: compute only O flag */
1419 void OPPROTO op_movb_eflags_T0(void)
1421 int of;
1422 of = cc_table[CC_OP].compute_all() & CC_O;
1423 CC_SRC = (T0 & (CC_S | CC_Z | CC_A | CC_P | CC_C)) | of;
1426 void OPPROTO op_movl_T0_eflags(void)
1428 int eflags;
1429 eflags = cc_table[CC_OP].compute_all();
1430 eflags |= (DF & DF_MASK);
1431 eflags |= env->eflags & ~(VM_MASK | RF_MASK);
1432 T0 = eflags;
1435 /* vm86plus version */
1436 #if 0
1437 void OPPROTO op_movl_T0_eflags_vm(void)
1439 int eflags;
1440 eflags = cc_table[CC_OP].compute_all();
1441 eflags |= (DF & DF_MASK);
1442 eflags |= env->eflags & ~(VM_MASK | RF_MASK | IF_MASK);
1443 if (env->eflags & VIF_MASK)
1444 eflags |= IF_MASK;
1445 T0 = eflags;
1447 #endif
1449 void OPPROTO op_cld(void)
1451 DF = 1;
1454 void OPPROTO op_std(void)
1456 DF = -1;
1459 void OPPROTO op_clc(void)
1461 int eflags;
1462 eflags = cc_table[CC_OP].compute_all();
1463 eflags &= ~CC_C;
1464 CC_SRC = eflags;
1467 void OPPROTO op_stc(void)
1469 int eflags;
1470 eflags = cc_table[CC_OP].compute_all();
1471 eflags |= CC_C;
1472 CC_SRC = eflags;
1475 void OPPROTO op_cmc(void)
1477 int eflags;
1478 eflags = cc_table[CC_OP].compute_all();
1479 eflags ^= CC_C;
1480 CC_SRC = eflags;
1483 void OPPROTO op_salc(void)
1485 int cf;
1486 cf = cc_table[CC_OP].compute_c();
1487 EAX = (EAX & ~0xff) | ((-cf) & 0xff);
1490 static int compute_all_eflags(void)
1492 return CC_SRC;
1495 static int compute_c_eflags(void)
1497 return CC_SRC & CC_C;
1500 static int compute_c_mul(void)
1502 int cf;
1503 cf = (CC_SRC != 0);
1504 return cf;
1507 static int compute_all_mul(void)
1509 int cf, pf, af, zf, sf, of;
1510 cf = (CC_SRC != 0);
1511 pf = 0; /* undefined */
1512 af = 0; /* undefined */
1513 zf = 0; /* undefined */
1514 sf = 0; /* undefined */
1515 of = cf << 11;
1516 return cf | pf | af | zf | sf | of;
1519 CCTable cc_table[CC_OP_NB] = {
1520 [CC_OP_DYNAMIC] = { /* should never happen */ },
1522 [CC_OP_EFLAGS] = { compute_all_eflags, compute_c_eflags },
1524 [CC_OP_MUL] = { compute_all_mul, compute_c_mul },
1526 [CC_OP_ADDB] = { compute_all_addb, compute_c_addb },
1527 [CC_OP_ADDW] = { compute_all_addw, compute_c_addw },
1528 [CC_OP_ADDL] = { compute_all_addl, compute_c_addl },
1530 [CC_OP_ADCB] = { compute_all_adcb, compute_c_adcb },
1531 [CC_OP_ADCW] = { compute_all_adcw, compute_c_adcw },
1532 [CC_OP_ADCL] = { compute_all_adcl, compute_c_adcl },
1534 [CC_OP_SUBB] = { compute_all_subb, compute_c_subb },
1535 [CC_OP_SUBW] = { compute_all_subw, compute_c_subw },
1536 [CC_OP_SUBL] = { compute_all_subl, compute_c_subl },
1538 [CC_OP_SBBB] = { compute_all_sbbb, compute_c_sbbb },
1539 [CC_OP_SBBW] = { compute_all_sbbw, compute_c_sbbw },
1540 [CC_OP_SBBL] = { compute_all_sbbl, compute_c_sbbl },
1542 [CC_OP_LOGICB] = { compute_all_logicb, compute_c_logicb },
1543 [CC_OP_LOGICW] = { compute_all_logicw, compute_c_logicw },
1544 [CC_OP_LOGICL] = { compute_all_logicl, compute_c_logicl },
1546 [CC_OP_INCB] = { compute_all_incb, compute_c_incl },
1547 [CC_OP_INCW] = { compute_all_incw, compute_c_incl },
1548 [CC_OP_INCL] = { compute_all_incl, compute_c_incl },
1550 [CC_OP_DECB] = { compute_all_decb, compute_c_incl },
1551 [CC_OP_DECW] = { compute_all_decw, compute_c_incl },
1552 [CC_OP_DECL] = { compute_all_decl, compute_c_incl },
1554 [CC_OP_SHLB] = { compute_all_shlb, compute_c_shlb },
1555 [CC_OP_SHLW] = { compute_all_shlw, compute_c_shlw },
1556 [CC_OP_SHLL] = { compute_all_shll, compute_c_shll },
1558 [CC_OP_SARB] = { compute_all_sarb, compute_c_sarl },
1559 [CC_OP_SARW] = { compute_all_sarw, compute_c_sarl },
1560 [CC_OP_SARL] = { compute_all_sarl, compute_c_sarl },
1563 /* floating point support. Some of the code for complicated x87
1564 functions comes from the LGPL'ed x86 emulator found in the Willows
1565 TWIN windows emulator. */
1567 #ifdef USE_X86LDOUBLE
1568 /* use long double functions */
1569 #define lrint lrintl
1570 #define llrint llrintl
1571 #define fabs fabsl
1572 #define sin sinl
1573 #define cos cosl
1574 #define sqrt sqrtl
1575 #define pow powl
1576 #define log logl
1577 #define tan tanl
1578 #define atan2 atan2l
1579 #define floor floorl
1580 #define ceil ceill
1581 #define rint rintl
1582 #endif
1584 extern int lrint(CPU86_LDouble x);
1585 extern int64_t llrint(CPU86_LDouble x);
1586 extern CPU86_LDouble fabs(CPU86_LDouble x);
1587 extern CPU86_LDouble sin(CPU86_LDouble x);
1588 extern CPU86_LDouble cos(CPU86_LDouble x);
1589 extern CPU86_LDouble sqrt(CPU86_LDouble x);
1590 extern CPU86_LDouble pow(CPU86_LDouble, CPU86_LDouble);
1591 extern CPU86_LDouble log(CPU86_LDouble x);
1592 extern CPU86_LDouble tan(CPU86_LDouble x);
1593 extern CPU86_LDouble atan2(CPU86_LDouble, CPU86_LDouble);
1594 extern CPU86_LDouble floor(CPU86_LDouble x);
1595 extern CPU86_LDouble ceil(CPU86_LDouble x);
1596 extern CPU86_LDouble rint(CPU86_LDouble x);
1598 #if defined(__powerpc__)
1599 extern CPU86_LDouble copysign(CPU86_LDouble, CPU86_LDouble);
1601 /* correct (but slow) PowerPC rint() (glibc version is incorrect) */
1602 double qemu_rint(double x)
1604 double y = 4503599627370496.0;
1605 if (fabs(x) >= y)
1606 return x;
1607 if (x < 0)
1608 y = -y;
1609 y = (x + y) - y;
1610 if (y == 0.0)
1611 y = copysign(y, x);
1612 return y;
1615 #define rint qemu_rint
1616 #endif
1618 #define RC_MASK 0xc00
1619 #define RC_NEAR 0x000
1620 #define RC_DOWN 0x400
1621 #define RC_UP 0x800
1622 #define RC_CHOP 0xc00
1624 #define MAXTAN 9223372036854775808.0
1626 #ifdef USE_X86LDOUBLE
1628 /* only for x86 */
1629 typedef union {
1630 long double d;
1631 struct {
1632 unsigned long long lower;
1633 unsigned short upper;
1634 } l;
1635 } CPU86_LDoubleU;
1637 /* the following deal with x86 long double-precision numbers */
1638 #define MAXEXPD 0x7fff
1639 #define EXPBIAS 16383
1640 #define EXPD(fp) (fp.l.upper & 0x7fff)
1641 #define SIGND(fp) ((fp.l.upper) & 0x8000)
1642 #define MANTD(fp) (fp.l.lower)
1643 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
1645 #else
1647 typedef union {
1648 double d;
1649 #ifndef WORDS_BIGENDIAN
1650 struct {
1651 unsigned long lower;
1652 long upper;
1653 } l;
1654 #else
1655 struct {
1656 long upper;
1657 unsigned long lower;
1658 } l;
1659 #endif
1660 long long ll;
1661 } CPU86_LDoubleU;
1663 /* the following deal with IEEE double-precision numbers */
1664 #define MAXEXPD 0x7ff
1665 #define EXPBIAS 1023
1666 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
1667 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
1668 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
1669 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
1670 #endif
1672 /* fp load FT0 */
1674 void OPPROTO op_flds_FT0_A0(void)
1676 #ifdef USE_FP_CONVERT
1677 FP_CONVERT.i32 = ldl((void *)A0);
1678 FT0 = FP_CONVERT.f;
1679 #else
1680 FT0 = ldfl((void *)A0);
1681 #endif
1684 void OPPROTO op_fldl_FT0_A0(void)
1686 #ifdef USE_FP_CONVERT
1687 FP_CONVERT.i64 = ldq((void *)A0);
1688 FT0 = FP_CONVERT.d;
1689 #else
1690 FT0 = ldfq((void *)A0);
1691 #endif
1694 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1695 #ifdef USE_INT_TO_FLOAT_HELPERS
1697 void helper_fild_FT0_A0(void)
1699 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1702 void helper_fildl_FT0_A0(void)
1704 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1707 void helper_fildll_FT0_A0(void)
1709 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1712 void OPPROTO op_fild_FT0_A0(void)
1714 helper_fild_FT0_A0();
1717 void OPPROTO op_fildl_FT0_A0(void)
1719 helper_fildl_FT0_A0();
1722 void OPPROTO op_fildll_FT0_A0(void)
1724 helper_fildll_FT0_A0();
1727 #else
1729 void OPPROTO op_fild_FT0_A0(void)
1731 #ifdef USE_FP_CONVERT
1732 FP_CONVERT.i32 = ldsw((void *)A0);
1733 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1734 #else
1735 FT0 = (CPU86_LDouble)ldsw((void *)A0);
1736 #endif
1739 void OPPROTO op_fildl_FT0_A0(void)
1741 #ifdef USE_FP_CONVERT
1742 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1743 FT0 = (CPU86_LDouble)FP_CONVERT.i32;
1744 #else
1745 FT0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1746 #endif
1749 void OPPROTO op_fildll_FT0_A0(void)
1751 #ifdef USE_FP_CONVERT
1752 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1753 FT0 = (CPU86_LDouble)FP_CONVERT.i64;
1754 #else
1755 FT0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1756 #endif
1758 #endif
1760 /* fp load ST0 */
1762 void OPPROTO op_flds_ST0_A0(void)
1764 #ifdef USE_FP_CONVERT
1765 FP_CONVERT.i32 = ldl((void *)A0);
1766 ST0 = FP_CONVERT.f;
1767 #else
1768 ST0 = ldfl((void *)A0);
1769 #endif
1772 void OPPROTO op_fldl_ST0_A0(void)
1774 #ifdef USE_FP_CONVERT
1775 FP_CONVERT.i64 = ldq((void *)A0);
1776 ST0 = FP_CONVERT.d;
1777 #else
1778 ST0 = ldfq((void *)A0);
1779 #endif
1782 #ifdef USE_X86LDOUBLE
1783 void OPPROTO op_fldt_ST0_A0(void)
1785 ST0 = *(long double *)A0;
1787 #else
1788 void helper_fldt_ST0_A0(void)
1790 CPU86_LDoubleU temp;
1791 int upper, e;
1792 /* mantissa */
1793 upper = lduw((uint8_t *)A0 + 8);
1794 /* XXX: handle overflow ? */
1795 e = (upper & 0x7fff) - 16383 + EXPBIAS; /* exponent */
1796 e |= (upper >> 4) & 0x800; /* sign */
1797 temp.ll = ((ldq((void *)A0) >> 11) & ((1LL << 52) - 1)) | ((uint64_t)e << 52);
1798 ST0 = temp.d;
1801 void OPPROTO op_fldt_ST0_A0(void)
1803 helper_fldt_ST0_A0();
1805 #endif
1807 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1808 #ifdef USE_INT_TO_FLOAT_HELPERS
1810 void helper_fild_ST0_A0(void)
1812 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1815 void helper_fildl_ST0_A0(void)
1817 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1820 void helper_fildll_ST0_A0(void)
1822 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1825 void OPPROTO op_fild_ST0_A0(void)
1827 helper_fild_ST0_A0();
1830 void OPPROTO op_fildl_ST0_A0(void)
1832 helper_fildl_ST0_A0();
1835 void OPPROTO op_fildll_ST0_A0(void)
1837 helper_fildll_ST0_A0();
1840 #else
1842 void OPPROTO op_fild_ST0_A0(void)
1844 #ifdef USE_FP_CONVERT
1845 FP_CONVERT.i32 = ldsw((void *)A0);
1846 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1847 #else
1848 ST0 = (CPU86_LDouble)ldsw((void *)A0);
1849 #endif
1852 void OPPROTO op_fildl_ST0_A0(void)
1854 #ifdef USE_FP_CONVERT
1855 FP_CONVERT.i32 = (int32_t) ldl((void *)A0);
1856 ST0 = (CPU86_LDouble)FP_CONVERT.i32;
1857 #else
1858 ST0 = (CPU86_LDouble)((int32_t)ldl((void *)A0));
1859 #endif
1862 void OPPROTO op_fildll_ST0_A0(void)
1864 #ifdef USE_FP_CONVERT
1865 FP_CONVERT.i64 = (int64_t) ldq((void *)A0);
1866 ST0 = (CPU86_LDouble)FP_CONVERT.i64;
1867 #else
1868 ST0 = (CPU86_LDouble)((int64_t)ldq((void *)A0));
1869 #endif
1872 #endif
1874 /* fp store */
1876 void OPPROTO op_fsts_ST0_A0(void)
1878 #ifdef USE_FP_CONVERT
1879 FP_CONVERT.d = ST0;
1880 stfl((void *)A0, FP_CONVERT.f);
1881 #else
1882 stfl((void *)A0, (float)ST0);
1883 #endif
1886 void OPPROTO op_fstl_ST0_A0(void)
1888 stfq((void *)A0, (double)ST0);
1891 #ifdef USE_X86LDOUBLE
1892 void OPPROTO op_fstt_ST0_A0(void)
1894 *(long double *)A0 = ST0;
1896 #else
1897 void helper_fstt_ST0_A0(void)
1899 CPU86_LDoubleU temp;
1900 int e;
1901 temp.d = ST0;
1902 /* mantissa */
1903 stq((void *)A0, (MANTD(temp) << 11) | (1LL << 63));
1904 /* exponent + sign */
1905 e = EXPD(temp) - EXPBIAS + 16383;
1906 e |= SIGND(temp) >> 16;
1907 stw((uint8_t *)A0 + 8, e);
1910 void OPPROTO op_fstt_ST0_A0(void)
1912 helper_fstt_ST0_A0();
1914 #endif
1916 void OPPROTO op_fist_ST0_A0(void)
1918 #if defined(__sparc__) && !defined(__sparc_v9__)
1919 register CPU86_LDouble d asm("o0");
1920 #else
1921 CPU86_LDouble d;
1922 #endif
1923 int val;
1925 d = ST0;
1926 val = lrint(d);
1927 stw((void *)A0, val);
1930 void OPPROTO op_fistl_ST0_A0(void)
1932 #if defined(__sparc__) && !defined(__sparc_v9__)
1933 register CPU86_LDouble d asm("o0");
1934 #else
1935 CPU86_LDouble d;
1936 #endif
1937 int val;
1939 d = ST0;
1940 val = lrint(d);
1941 stl((void *)A0, val);
1944 void OPPROTO op_fistll_ST0_A0(void)
1946 #if defined(__sparc__) && !defined(__sparc_v9__)
1947 register CPU86_LDouble d asm("o0");
1948 #else
1949 CPU86_LDouble d;
1950 #endif
1951 int64_t val;
1953 d = ST0;
1954 val = llrint(d);
1955 stq((void *)A0, val);
1958 /* BCD ops */
1960 #define MUL10(iv) ( iv + iv + (iv << 3) )
1962 void helper_fbld_ST0_A0(void)
1964 uint8_t *seg;
1965 CPU86_LDouble fpsrcop;
1966 int m32i;
1967 unsigned int v;
1969 /* in this code, seg/m32i will be used as temporary ptr/int */
1970 seg = (uint8_t *)A0 + 8;
1971 v = ldub(seg--);
1972 /* XXX: raise exception */
1973 if (v != 0)
1974 return;
1975 v = ldub(seg--);
1976 /* XXX: raise exception */
1977 if ((v & 0xf0) != 0)
1978 return;
1979 m32i = v; /* <-- d14 */
1980 v = ldub(seg--);
1981 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d13 */
1982 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d12 */
1983 v = ldub(seg--);
1984 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d11 */
1985 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d10 */
1986 v = ldub(seg--);
1987 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d9 */
1988 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d8 */
1989 fpsrcop = ((CPU86_LDouble)m32i) * 100000000.0;
1991 v = ldub(seg--);
1992 m32i = (v >> 4); /* <-- d7 */
1993 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d6 */
1994 v = ldub(seg--);
1995 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d5 */
1996 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d4 */
1997 v = ldub(seg--);
1998 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d3 */
1999 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d2 */
2000 v = ldub(seg);
2001 m32i = MUL10(m32i) + (v >> 4); /* <-- val * 10 + d1 */
2002 m32i = MUL10(m32i) + (v & 0xf); /* <-- val * 10 + d0 */
2003 fpsrcop += ((CPU86_LDouble)m32i);
2004 if ( ldub(seg+9) & 0x80 )
2005 fpsrcop = -fpsrcop;
2006 ST0 = fpsrcop;
2009 void OPPROTO op_fbld_ST0_A0(void)
2011 helper_fbld_ST0_A0();
2014 void helper_fbst_ST0_A0(void)
2016 CPU86_LDouble fptemp;
2017 CPU86_LDouble fpsrcop;
2018 int v;
2019 uint8_t *mem_ref, *mem_end;
2021 fpsrcop = rint(ST0);
2022 mem_ref = (uint8_t *)A0;
2023 mem_end = mem_ref + 8;
2024 if ( fpsrcop < 0.0 ) {
2025 stw(mem_end, 0x8000);
2026 fpsrcop = -fpsrcop;
2027 } else {
2028 stw(mem_end, 0x0000);
2030 while (mem_ref < mem_end) {
2031 if (fpsrcop == 0.0)
2032 break;
2033 fptemp = floor(fpsrcop/10.0);
2034 v = ((int)(fpsrcop - fptemp*10.0));
2035 if (fptemp == 0.0) {
2036 stb(mem_ref++, v);
2037 break;
2039 fpsrcop = fptemp;
2040 fptemp = floor(fpsrcop/10.0);
2041 v |= (((int)(fpsrcop - fptemp*10.0)) << 4);
2042 stb(mem_ref++, v);
2043 fpsrcop = fptemp;
2045 while (mem_ref < mem_end) {
2046 stb(mem_ref++, 0);
2050 void OPPROTO op_fbst_ST0_A0(void)
2052 helper_fbst_ST0_A0();
2055 /* FPU move */
2057 static inline void fpush(void)
2059 env->fpstt = (env->fpstt - 1) & 7;
2060 env->fptags[env->fpstt] = 0; /* validate stack entry */
2063 static inline void fpop(void)
2065 env->fptags[env->fpstt] = 1; /* invvalidate stack entry */
2066 env->fpstt = (env->fpstt + 1) & 7;
2069 void OPPROTO op_fpush(void)
2071 fpush();
2074 void OPPROTO op_fpop(void)
2076 fpop();
2079 void OPPROTO op_fdecstp(void)
2081 env->fpstt = (env->fpstt - 1) & 7;
2082 env->fpus &= (~0x4700);
2085 void OPPROTO op_fincstp(void)
2087 env->fpstt = (env->fpstt + 1) & 7;
2088 env->fpus &= (~0x4700);
2091 void OPPROTO op_fmov_ST0_FT0(void)
2093 ST0 = FT0;
2096 void OPPROTO op_fmov_FT0_STN(void)
2098 FT0 = ST(PARAM1);
2101 void OPPROTO op_fmov_ST0_STN(void)
2103 ST0 = ST(PARAM1);
2106 void OPPROTO op_fmov_STN_ST0(void)
2108 ST(PARAM1) = ST0;
2111 void OPPROTO op_fxchg_ST0_STN(void)
2113 CPU86_LDouble tmp;
2114 tmp = ST(PARAM1);
2115 ST(PARAM1) = ST0;
2116 ST0 = tmp;
2119 /* FPU operations */
2121 /* XXX: handle nans */
2122 void OPPROTO op_fcom_ST0_FT0(void)
2124 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
2125 if (ST0 < FT0)
2126 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
2127 else if (ST0 == FT0)
2128 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
2129 FORCE_RET();
2132 /* XXX: handle nans */
2133 void OPPROTO op_fucom_ST0_FT0(void)
2135 env->fpus &= (~0x4500); /* (C3,C2,C0) <-- 000 */
2136 if (ST0 < FT0)
2137 env->fpus |= 0x100; /* (C3,C2,C0) <-- 001 */
2138 else if (ST0 == FT0)
2139 env->fpus |= 0x4000; /* (C3,C2,C0) <-- 100 */
2140 FORCE_RET();
2143 void OPPROTO op_fadd_ST0_FT0(void)
2145 ST0 += FT0;
2148 void OPPROTO op_fmul_ST0_FT0(void)
2150 ST0 *= FT0;
2153 void OPPROTO op_fsub_ST0_FT0(void)
2155 ST0 -= FT0;
2158 void OPPROTO op_fsubr_ST0_FT0(void)
2160 ST0 = FT0 - ST0;
2163 void OPPROTO op_fdiv_ST0_FT0(void)
2165 ST0 /= FT0;
2168 void OPPROTO op_fdivr_ST0_FT0(void)
2170 ST0 = FT0 / ST0;
2173 /* fp operations between STN and ST0 */
2175 void OPPROTO op_fadd_STN_ST0(void)
2177 ST(PARAM1) += ST0;
2180 void OPPROTO op_fmul_STN_ST0(void)
2182 ST(PARAM1) *= ST0;
2185 void OPPROTO op_fsub_STN_ST0(void)
2187 ST(PARAM1) -= ST0;
2190 void OPPROTO op_fsubr_STN_ST0(void)
2192 CPU86_LDouble *p;
2193 p = &ST(PARAM1);
2194 *p = ST0 - *p;
2197 void OPPROTO op_fdiv_STN_ST0(void)
2199 ST(PARAM1) /= ST0;
2202 void OPPROTO op_fdivr_STN_ST0(void)
2204 CPU86_LDouble *p;
2205 p = &ST(PARAM1);
2206 *p = ST0 / *p;
2209 /* misc FPU operations */
2210 void OPPROTO op_fchs_ST0(void)
2212 ST0 = -ST0;
2215 void OPPROTO op_fabs_ST0(void)
2217 ST0 = fabs(ST0);
2220 void helper_fxam_ST0(void)
2222 CPU86_LDoubleU temp;
2223 int expdif;
2225 temp.d = ST0;
2227 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2228 if (SIGND(temp))
2229 env->fpus |= 0x200; /* C1 <-- 1 */
2231 expdif = EXPD(temp);
2232 if (expdif == MAXEXPD) {
2233 if (MANTD(temp) == 0)
2234 env->fpus |= 0x500 /*Infinity*/;
2235 else
2236 env->fpus |= 0x100 /*NaN*/;
2237 } else if (expdif == 0) {
2238 if (MANTD(temp) == 0)
2239 env->fpus |= 0x4000 /*Zero*/;
2240 else
2241 env->fpus |= 0x4400 /*Denormal*/;
2242 } else {
2243 env->fpus |= 0x400;
2247 void OPPROTO op_fxam_ST0(void)
2249 helper_fxam_ST0();
2252 void OPPROTO op_fld1_ST0(void)
2254 ST0 = *(CPU86_LDouble *)&f15rk[1];
2257 void OPPROTO op_fldl2t_ST0(void)
2259 ST0 = *(CPU86_LDouble *)&f15rk[6];
2262 void OPPROTO op_fldl2e_ST0(void)
2264 ST0 = *(CPU86_LDouble *)&f15rk[5];
2267 void OPPROTO op_fldpi_ST0(void)
2269 ST0 = *(CPU86_LDouble *)&f15rk[2];
2272 void OPPROTO op_fldlg2_ST0(void)
2274 ST0 = *(CPU86_LDouble *)&f15rk[3];
2277 void OPPROTO op_fldln2_ST0(void)
2279 ST0 = *(CPU86_LDouble *)&f15rk[4];
2282 void OPPROTO op_fldz_ST0(void)
2284 ST0 = *(CPU86_LDouble *)&f15rk[0];
2287 void OPPROTO op_fldz_FT0(void)
2289 ST0 = *(CPU86_LDouble *)&f15rk[0];
2292 void helper_f2xm1(void)
2294 ST0 = pow(2.0,ST0) - 1.0;
2297 void helper_fyl2x(void)
2299 CPU86_LDouble fptemp;
2301 fptemp = ST0;
2302 if (fptemp>0.0){
2303 fptemp = log(fptemp)/log(2.0); /* log2(ST) */
2304 ST1 *= fptemp;
2305 fpop();
2306 } else {
2307 env->fpus &= (~0x4700);
2308 env->fpus |= 0x400;
2312 void helper_fptan(void)
2314 CPU86_LDouble fptemp;
2316 fptemp = ST0;
2317 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2318 env->fpus |= 0x400;
2319 } else {
2320 ST0 = tan(fptemp);
2321 fpush();
2322 ST0 = 1.0;
2323 env->fpus &= (~0x400); /* C2 <-- 0 */
2324 /* the above code is for |arg| < 2**52 only */
2328 void helper_fpatan(void)
2330 CPU86_LDouble fptemp, fpsrcop;
2332 fpsrcop = ST1;
2333 fptemp = ST0;
2334 ST1 = atan2(fpsrcop,fptemp);
2335 fpop();
2338 void helper_fxtract(void)
2340 CPU86_LDoubleU temp;
2341 unsigned int expdif;
2343 temp.d = ST0;
2344 expdif = EXPD(temp) - EXPBIAS;
2345 /*DP exponent bias*/
2346 ST0 = expdif;
2347 fpush();
2348 BIASEXPONENT(temp);
2349 ST0 = temp.d;
2352 void helper_fprem1(void)
2354 CPU86_LDouble dblq, fpsrcop, fptemp;
2355 CPU86_LDoubleU fpsrcop1, fptemp1;
2356 int expdif;
2357 int q;
2359 fpsrcop = ST0;
2360 fptemp = ST1;
2361 fpsrcop1.d = fpsrcop;
2362 fptemp1.d = fptemp;
2363 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2364 if (expdif < 53) {
2365 dblq = fpsrcop / fptemp;
2366 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2367 ST0 = fpsrcop - fptemp*dblq;
2368 q = (int)dblq; /* cutting off top bits is assumed here */
2369 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2370 /* (C0,C1,C3) <-- (q2,q1,q0) */
2371 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2372 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2373 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2374 } else {
2375 env->fpus |= 0x400; /* C2 <-- 1 */
2376 fptemp = pow(2.0, expdif-50);
2377 fpsrcop = (ST0 / ST1) / fptemp;
2378 /* fpsrcop = integer obtained by rounding to the nearest */
2379 fpsrcop = (fpsrcop-floor(fpsrcop) < ceil(fpsrcop)-fpsrcop)?
2380 floor(fpsrcop): ceil(fpsrcop);
2381 ST0 -= (ST1 * fpsrcop * fptemp);
2385 void helper_fprem(void)
2387 CPU86_LDouble dblq, fpsrcop, fptemp;
2388 CPU86_LDoubleU fpsrcop1, fptemp1;
2389 int expdif;
2390 int q;
2392 fpsrcop = ST0;
2393 fptemp = ST1;
2394 fpsrcop1.d = fpsrcop;
2395 fptemp1.d = fptemp;
2396 expdif = EXPD(fpsrcop1) - EXPD(fptemp1);
2397 if ( expdif < 53 ) {
2398 dblq = fpsrcop / fptemp;
2399 dblq = (dblq < 0.0)? ceil(dblq): floor(dblq);
2400 ST0 = fpsrcop - fptemp*dblq;
2401 q = (int)dblq; /* cutting off top bits is assumed here */
2402 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2403 /* (C0,C1,C3) <-- (q2,q1,q0) */
2404 env->fpus |= (q&0x4) << 6; /* (C0) <-- q2 */
2405 env->fpus |= (q&0x2) << 8; /* (C1) <-- q1 */
2406 env->fpus |= (q&0x1) << 14; /* (C3) <-- q0 */
2407 } else {
2408 env->fpus |= 0x400; /* C2 <-- 1 */
2409 fptemp = pow(2.0, expdif-50);
2410 fpsrcop = (ST0 / ST1) / fptemp;
2411 /* fpsrcop = integer obtained by chopping */
2412 fpsrcop = (fpsrcop < 0.0)?
2413 -(floor(fabs(fpsrcop))): floor(fpsrcop);
2414 ST0 -= (ST1 * fpsrcop * fptemp);
2418 void helper_fyl2xp1(void)
2420 CPU86_LDouble fptemp;
2422 fptemp = ST0;
2423 if ((fptemp+1.0)>0.0) {
2424 fptemp = log(fptemp+1.0) / log(2.0); /* log2(ST+1.0) */
2425 ST1 *= fptemp;
2426 fpop();
2427 } else {
2428 env->fpus &= (~0x4700);
2429 env->fpus |= 0x400;
2433 void helper_fsqrt(void)
2435 CPU86_LDouble fptemp;
2437 fptemp = ST0;
2438 if (fptemp<0.0) {
2439 env->fpus &= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2440 env->fpus |= 0x400;
2442 ST0 = sqrt(fptemp);
2445 void helper_fsincos(void)
2447 CPU86_LDouble fptemp;
2449 fptemp = ST0;
2450 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2451 env->fpus |= 0x400;
2452 } else {
2453 ST0 = sin(fptemp);
2454 fpush();
2455 ST0 = cos(fptemp);
2456 env->fpus &= (~0x400); /* C2 <-- 0 */
2457 /* the above code is for |arg| < 2**63 only */
2461 void helper_frndint(void)
2463 ST0 = rint(ST0);
2466 void helper_fscale(void)
2468 CPU86_LDouble fpsrcop, fptemp;
2470 fpsrcop = 2.0;
2471 fptemp = pow(fpsrcop,ST1);
2472 ST0 *= fptemp;
2475 void helper_fsin(void)
2477 CPU86_LDouble fptemp;
2479 fptemp = ST0;
2480 if ((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2481 env->fpus |= 0x400;
2482 } else {
2483 ST0 = sin(fptemp);
2484 env->fpus &= (~0x400); /* C2 <-- 0 */
2485 /* the above code is for |arg| < 2**53 only */
2489 void helper_fcos(void)
2491 CPU86_LDouble fptemp;
2493 fptemp = ST0;
2494 if((fptemp > MAXTAN)||(fptemp < -MAXTAN)) {
2495 env->fpus |= 0x400;
2496 } else {
2497 ST0 = cos(fptemp);
2498 env->fpus &= (~0x400); /* C2 <-- 0 */
2499 /* the above code is for |arg5 < 2**63 only */
2503 /* associated heplers to reduce generated code length and to simplify
2504 relocation (FP constants are usually stored in .rodata section) */
2506 void OPPROTO op_f2xm1(void)
2508 helper_f2xm1();
2511 void OPPROTO op_fyl2x(void)
2513 helper_fyl2x();
2516 void OPPROTO op_fptan(void)
2518 helper_fptan();
2521 void OPPROTO op_fpatan(void)
2523 helper_fpatan();
2526 void OPPROTO op_fxtract(void)
2528 helper_fxtract();
2531 void OPPROTO op_fprem1(void)
2533 helper_fprem1();
2537 void OPPROTO op_fprem(void)
2539 helper_fprem();
2542 void OPPROTO op_fyl2xp1(void)
2544 helper_fyl2xp1();
2547 void OPPROTO op_fsqrt(void)
2549 helper_fsqrt();
2552 void OPPROTO op_fsincos(void)
2554 helper_fsincos();
2557 void OPPROTO op_frndint(void)
2559 helper_frndint();
2562 void OPPROTO op_fscale(void)
2564 helper_fscale();
2567 void OPPROTO op_fsin(void)
2569 helper_fsin();
2572 void OPPROTO op_fcos(void)
2574 helper_fcos();
2577 void OPPROTO op_fnstsw_A0(void)
2579 int fpus;
2580 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2581 stw((void *)A0, fpus);
2584 void OPPROTO op_fnstsw_EAX(void)
2586 int fpus;
2587 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
2588 EAX = (EAX & 0xffff0000) | fpus;
2591 void OPPROTO op_fnstcw_A0(void)
2593 stw((void *)A0, env->fpuc);
2596 void OPPROTO op_fldcw_A0(void)
2598 int rnd_type;
2599 env->fpuc = lduw((void *)A0);
2600 /* set rounding mode */
2601 switch(env->fpuc & RC_MASK) {
2602 default:
2603 case RC_NEAR:
2604 rnd_type = FE_TONEAREST;
2605 break;
2606 case RC_DOWN:
2607 rnd_type = FE_DOWNWARD;
2608 break;
2609 case RC_UP:
2610 rnd_type = FE_UPWARD;
2611 break;
2612 case RC_CHOP:
2613 rnd_type = FE_TOWARDZERO;
2614 break;
2616 fesetround(rnd_type);
2619 void OPPROTO op_fclex(void)
2621 env->fpus &= 0x7f00;
2624 void OPPROTO op_fninit(void)
2626 env->fpus = 0;
2627 env->fpstt = 0;
2628 env->fpuc = 0x37f;
2629 env->fptags[0] = 1;
2630 env->fptags[1] = 1;
2631 env->fptags[2] = 1;
2632 env->fptags[3] = 1;
2633 env->fptags[4] = 1;
2634 env->fptags[5] = 1;
2635 env->fptags[6] = 1;
2636 env->fptags[7] = 1;
2639 /* threading support */
2640 void OPPROTO op_lock(void)
2642 cpu_lock();
2645 void OPPROTO op_unlock(void)
2647 cpu_unlock();