2 * i386 micro operations
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
,
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,
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,
76 /* an array of Intel 80-bit FP constants, to be loaded via integer ops */
77 typedef unsigned short f15ld
[5];
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}
89 /* the same, 64-bit version */
90 typedef unsigned short f15ld
[4];
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}
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}
113 /* n must be a constant to be efficient */
114 static inline int lshift(int x
, int n
)
122 /* we define the various pieces of code used by the JIT */
126 #include "opreg_template.h"
132 #include "opreg_template.h"
138 #include "opreg_template.h"
144 #include "opreg_template.h"
150 #include "opreg_template.h"
156 #include "opreg_template.h"
162 #include "opreg_template.h"
168 #include "opreg_template.h"
172 /* operations with flags */
174 void OPPROTO
op_addl_T0_T1_cc(void)
181 void OPPROTO
op_orl_T0_T1_cc(void)
187 void OPPROTO
op_andl_T0_T1_cc(void)
193 void OPPROTO
op_subl_T0_T1_cc(void)
200 void OPPROTO
op_xorl_T0_T1_cc(void)
206 void OPPROTO
op_cmpl_T0_T1_cc(void)
212 void OPPROTO
op_negl_T0_cc(void)
219 void OPPROTO
op_incl_T0_cc(void)
221 CC_SRC
= cc_table
[CC_OP
].compute_c();
226 void OPPROTO
op_decl_T0_cc(void)
228 CC_SRC
= cc_table
[CC_OP
].compute_c();
233 void OPPROTO
op_testl_T0_T1_cc(void)
238 /* operations without flags */
240 void OPPROTO
op_addl_T0_T1(void)
245 void OPPROTO
op_orl_T0_T1(void)
250 void OPPROTO
op_andl_T0_T1(void)
255 void OPPROTO
op_subl_T0_T1(void)
260 void OPPROTO
op_xorl_T0_T1(void)
265 void OPPROTO
op_negl_T0(void)
270 void OPPROTO
op_incl_T0(void)
275 void OPPROTO
op_decl_T0(void)
280 void OPPROTO
op_notl_T0(void)
285 void OPPROTO
op_bswapl_T0(void)
290 /* multiply/divide */
291 void OPPROTO
op_mulb_AL_T0(void)
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)
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)
310 res
= (uint16_t)EAX
* (uint16_t)T0
;
311 EAX
= (EAX
& 0xffff0000) | (res
& 0xffff);
312 EDX
= (EDX
& 0xffff0000) | ((res
>> 16) & 0xffff);
316 void OPPROTO
op_imulw_AX_T0(void)
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)
328 res
= (uint64_t)((uint32_t)EAX
) * (uint64_t)((uint32_t)T0
);
334 void OPPROTO
op_imull_EAX_T0(void)
337 res
= (int64_t)((int32_t)EAX
) * (int64_t)((int32_t)T0
);
340 CC_SRC
= (res
!= (int32_t)res
);
343 void OPPROTO
op_imulw_T0_T1(void)
346 res
= (int16_t)T0
* (int16_t)T1
;
348 CC_SRC
= (res
!= (int16_t)res
);
351 void OPPROTO
op_imull_T0_T1(void)
354 res
= (int64_t)((int32_t)T0
) * (int64_t)((int32_t)T1
);
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);
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)
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);
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)
405 num
= (EAX
& 0xffff) | ((EDX
& 0xffff) << 16);
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 void OPPROTO
op_divl_EAX_T0(void)
417 unsigned int den
, q
, r
;
420 num
= EAX
| ((uint64_t)EDX
<< 32);
423 raise_exception(EXCP00_DIVZ
);
430 void OPPROTO
op_idivl_EAX_T0(void)
435 num
= EAX
| ((uint64_t)EDX
<< 32);
438 raise_exception(EXCP00_DIVZ
);
445 /* constant load & misc op */
447 void OPPROTO
op_movl_T0_im(void)
452 void OPPROTO
op_addl_T0_im(void)
457 void OPPROTO
op_andl_T0_ffff(void)
462 void OPPROTO
op_movl_T0_T1(void)
467 void OPPROTO
op_movl_T1_im(void)
472 void OPPROTO
op_addl_T1_im(void)
477 void OPPROTO
op_movl_T1_A0(void)
482 void OPPROTO
op_movl_A0_im(void)
487 void OPPROTO
op_addl_A0_im(void)
492 void OPPROTO
op_addl_A0_AL(void)
497 void OPPROTO
op_andl_A0_ffff(void)
504 void OPPROTO
op_ldub_T0_A0(void)
506 T0
= ldub((uint8_t *)A0
);
509 void OPPROTO
op_ldsb_T0_A0(void)
511 T0
= ldsb((int8_t *)A0
);
514 void OPPROTO
op_lduw_T0_A0(void)
516 T0
= lduw((uint8_t *)A0
);
519 void OPPROTO
op_ldsw_T0_A0(void)
521 T0
= ldsw((int8_t *)A0
);
524 void OPPROTO
op_ldl_T0_A0(void)
526 T0
= ldl((uint8_t *)A0
);
529 void OPPROTO
op_ldub_T1_A0(void)
531 T1
= ldub((uint8_t *)A0
);
534 void OPPROTO
op_ldsb_T1_A0(void)
536 T1
= ldsb((int8_t *)A0
);
539 void OPPROTO
op_lduw_T1_A0(void)
541 T1
= lduw((uint8_t *)A0
);
544 void OPPROTO
op_ldsw_T1_A0(void)
546 T1
= ldsw((int8_t *)A0
);
549 void OPPROTO
op_ldl_T1_A0(void)
551 T1
= ldl((uint8_t *)A0
);
554 void OPPROTO
op_stb_T0_A0(void)
556 stb((uint8_t *)A0
, T0
);
559 void OPPROTO
op_stw_T0_A0(void)
561 stw((uint8_t *)A0
, T0
);
564 void OPPROTO
op_stl_T0_A0(void)
566 stl((uint8_t *)A0
, T0
);
569 /* used for bit operations */
571 void OPPROTO
op_add_bitw_A0_T1(void)
573 A0
+= ((int32_t)T1
>> 4) << 1;
576 void OPPROTO
op_add_bitl_A0_T1(void)
578 A0
+= ((int32_t)T1
>> 5) << 2;
583 void OPPROTO
op_jmp_T0(void)
588 void OPPROTO
op_jmp_im(void)
593 void OPPROTO
op_int_im(void)
596 raise_exception(EXCP0D_GPF
);
599 void OPPROTO
op_int3(void)
602 raise_exception(EXCP03_INT3
);
605 void OPPROTO
op_into(void)
608 eflags
= cc_table
[CC_OP
].compute_all();
610 raise_exception(EXCP04_INTO
);
614 /* XXX: add IOPL/CPL tests */
615 void OPPROTO
op_cli(void)
617 raise_exception(EXCP0D_GPF
);
620 /* XXX: add IOPL/CPL tests */
621 void OPPROTO
op_sti(void)
623 raise_exception(EXCP0D_GPF
);
626 /* vm86plus instructions */
628 void OPPROTO
op_cli_vm(void)
630 env
->eflags
&= ~VIF_MASK
;
633 void OPPROTO
op_sti_vm(void)
635 env
->eflags
|= VIF_MASK
;
636 if (env
->eflags
& VIP_MASK
) {
638 raise_exception(EXCP0D_GPF
);
643 void OPPROTO
op_boundw(void)
646 low
= ldsw((uint8_t *)A0
);
647 high
= ldsw((uint8_t *)A0
+ 2);
649 if (v
< low
|| v
> high
)
650 raise_exception(EXCP05_BOUND
);
654 void OPPROTO
op_boundl(void)
657 low
= ldl((uint8_t *)A0
);
658 high
= ldl((uint8_t *)A0
+ 4);
660 if (v
< low
|| v
> high
)
661 raise_exception(EXCP05_BOUND
);
665 void OPPROTO
op_cmpxchg8b(void)
670 eflags
= cc_table
[CC_OP
].compute_all();
671 d
= ldq((uint8_t *)A0
);
672 if (d
== (((uint64_t)EDX
<< 32) | EAX
)) {
673 stq((uint8_t *)A0
, ((uint64_t)ECX
<< 32) | EBX
);
689 #include "ops_template.h"
693 #include "ops_template.h"
697 #include "ops_template.h"
702 void OPPROTO
op_movsbl_T0_T0(void)
707 void OPPROTO
op_movzbl_T0_T0(void)
712 void OPPROTO
op_movswl_T0_T0(void)
717 void OPPROTO
op_movzwl_T0_T0(void)
722 void OPPROTO
op_movswl_EAX_AX(void)
727 void OPPROTO
op_movsbw_AX_AL(void)
729 EAX
= (EAX
& 0xffff0000) | ((int8_t)EAX
& 0xffff);
732 void OPPROTO
op_movslq_EDX_EAX(void)
734 EDX
= (int32_t)EAX
>> 31;
737 void OPPROTO
op_movswl_DX_AX(void)
739 EDX
= (EDX
& 0xffff0000) | (((int16_t)EAX
>> 15) & 0xffff);
744 void op_pushl_T0(void)
748 stl((void *)offset
, T0
);
749 /* modify ESP after to handle exceptions correctly */
753 void op_pushw_T0(void)
757 stw((void *)offset
, T0
);
758 /* modify ESP after to handle exceptions correctly */
762 void op_pushl_ss32_T0(void)
766 stl(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
767 /* modify ESP after to handle exceptions correctly */
771 void op_pushw_ss32_T0(void)
775 stw(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
776 /* modify ESP after to handle exceptions correctly */
780 void op_pushl_ss16_T0(void)
783 offset
= (ESP
- 4) & 0xffff;
784 stl(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
785 /* modify ESP after to handle exceptions correctly */
786 ESP
= (ESP
& ~0xffff) | offset
;
789 void op_pushw_ss16_T0(void)
792 offset
= (ESP
- 2) & 0xffff;
793 stw(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
794 /* modify ESP after to handle exceptions correctly */
795 ESP
= (ESP
& ~0xffff) | offset
;
798 /* NOTE: ESP update is done after */
799 void op_popl_T0(void)
801 T0
= ldl((void *)ESP
);
804 void op_popw_T0(void)
806 T0
= lduw((void *)ESP
);
809 void op_popl_ss32_T0(void)
811 T0
= ldl(env
->seg_cache
[R_SS
].base
+ ESP
);
814 void op_popw_ss32_T0(void)
816 T0
= lduw(env
->seg_cache
[R_SS
].base
+ ESP
);
819 void op_popl_ss16_T0(void)
821 T0
= ldl(env
->seg_cache
[R_SS
].base
+ (ESP
& 0xffff));
824 void op_popw_ss16_T0(void)
826 T0
= lduw(env
->seg_cache
[R_SS
].base
+ (ESP
& 0xffff));
829 void op_addl_ESP_4(void)
834 void op_addl_ESP_2(void)
839 void op_addw_ESP_4(void)
841 ESP
= (ESP
& ~0xffff) | ((ESP
+ 4) & 0xffff);
844 void op_addw_ESP_2(void)
846 ESP
= (ESP
& ~0xffff) | ((ESP
+ 2) & 0xffff);
849 void op_addl_ESP_im(void)
854 void op_addw_ESP_im(void)
856 ESP
= (ESP
& ~0xffff) | ((ESP
+ PARAM1
) & 0xffff);
864 void OPPROTO
op_rdtsc(void)
868 asm("rdtsc" : "=A" (val
));
870 /* better than nothing: the time increases */
877 /* We simulate a pre-MMX pentium as in valgrind */
878 #define CPUID_FP87 (1 << 0)
879 #define CPUID_VME (1 << 1)
880 #define CPUID_DE (1 << 2)
881 #define CPUID_PSE (1 << 3)
882 #define CPUID_TSC (1 << 4)
883 #define CPUID_MSR (1 << 5)
884 #define CPUID_PAE (1 << 6)
885 #define CPUID_MCE (1 << 7)
886 #define CPUID_CX8 (1 << 8)
887 #define CPUID_APIC (1 << 9)
888 #define CPUID_SEP (1 << 11) /* sysenter/sysexit */
889 #define CPUID_MTRR (1 << 12)
890 #define CPUID_PGE (1 << 13)
891 #define CPUID_MCA (1 << 14)
892 #define CPUID_CMOV (1 << 15)
894 #define CPUID_MMX (1 << 23)
895 #define CPUID_FXSR (1 << 24)
896 #define CPUID_SSE (1 << 25)
897 #define CPUID_SSE2 (1 << 26)
899 void helper_cpuid(void)
902 EAX
= 1; /* max EAX index supported */
911 EDX
= CPUID_FP87
| CPUID_VME
| CPUID_DE
| CPUID_PSE
|
912 CPUID_TSC
| CPUID_MSR
| CPUID_MCE
|
917 void OPPROTO
op_cpuid(void)
925 void OPPROTO
op_aam(void)
932 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
936 void OPPROTO
op_aad(void)
941 ah
= (EAX
>> 8) & 0xff;
942 al
= ((ah
* base
) + al
) & 0xff;
943 EAX
= (EAX
& ~0xffff) | al
;
947 void OPPROTO
op_aaa(void)
953 eflags
= cc_table
[CC_OP
].compute_all();
956 ah
= (EAX
>> 8) & 0xff;
958 icarry
= (al
> 0xf9);
959 if (((al
& 0x0f) > 9 ) || af
) {
960 al
= (al
+ 6) & 0x0f;
961 ah
= (ah
+ 1 + icarry
) & 0xff;
962 eflags
|= CC_C
| CC_A
;
964 eflags
&= ~(CC_C
| CC_A
);
967 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
971 void OPPROTO
op_aas(void)
977 eflags
= cc_table
[CC_OP
].compute_all();
980 ah
= (EAX
>> 8) & 0xff;
983 if (((al
& 0x0f) > 9 ) || af
) {
984 al
= (al
- 6) & 0x0f;
985 ah
= (ah
- 1 - icarry
) & 0xff;
986 eflags
|= CC_C
| CC_A
;
988 eflags
&= ~(CC_C
| CC_A
);
991 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
995 void OPPROTO
op_daa(void)
1000 eflags
= cc_table
[CC_OP
].compute_all();
1006 if (((al
& 0x0f) > 9 ) || af
) {
1007 al
= (al
+ 6) & 0xff;
1010 if ((al
> 0x9f) || cf
) {
1011 al
= (al
+ 0x60) & 0xff;
1014 EAX
= (EAX
& ~0xff) | al
;
1015 /* well, speed is not an issue here, so we compute the flags by hand */
1016 eflags
|= (al
== 0) << 6; /* zf */
1017 eflags
|= parity_table
[al
]; /* pf */
1018 eflags
|= (al
& 0x80); /* sf */
1022 void OPPROTO
op_das(void)
1024 int al
, al1
, af
, cf
;
1027 eflags
= cc_table
[CC_OP
].compute_all();
1034 if (((al
& 0x0f) > 9 ) || af
) {
1038 al
= (al
- 6) & 0xff;
1040 if ((al1
> 0x99) || cf
) {
1041 al
= (al
- 0x60) & 0xff;
1044 EAX
= (EAX
& ~0xff) | al
;
1045 /* well, speed is not an issue here, so we compute the flags by hand */
1046 eflags
|= (al
== 0) << 6; /* zf */
1047 eflags
|= parity_table
[al
]; /* pf */
1048 eflags
|= (al
& 0x80); /* sf */
1052 /* segment handling */
1054 /* XXX: use static VM86 information */
1055 void load_seg(int seg_reg
, int selector
)
1058 SegmentDescriptorTable
*dt
;
1063 env
->segs
[seg_reg
] = selector
;
1064 sc
= &env
->seg_cache
[seg_reg
];
1065 if (env
->eflags
& VM_MASK
) {
1066 sc
->base
= (void *)(selector
<< 4);
1074 index
= selector
& ~7;
1075 if ((index
+ 7) > dt
->limit
)
1076 raise_exception(EXCP0D_GPF
);
1077 ptr
= dt
->base
+ index
;
1080 sc
->base
= (void *)((e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000));
1081 sc
->limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
1083 sc
->limit
= (sc
->limit
<< 12) | 0xfff;
1084 sc
->seg_32bit
= (e2
>> 22) & 1;
1086 fprintf(logfile
, "load_seg: sel=0x%04x base=0x%08lx limit=0x%08lx seg_32bit=%d\n",
1087 selector
, (unsigned long)sc
->base
, sc
->limit
, sc
->seg_32bit
);
1092 void OPPROTO
op_movl_seg_T0(void)
1094 load_seg(PARAM1
, T0
& 0xffff);
1097 void OPPROTO
op_movl_T0_seg(void)
1099 T0
= env
->segs
[PARAM1
];
1102 void OPPROTO
op_movl_A0_seg(void)
1104 A0
= *(unsigned long *)((char *)env
+ PARAM1
);
1107 void OPPROTO
op_addl_A0_seg(void)
1109 A0
+= *(unsigned long *)((char *)env
+ PARAM1
);
1112 /* flags handling */
1114 /* slow jumps cases (compute x86 flags) */
1115 void OPPROTO
op_jo_cc(void)
1118 eflags
= cc_table
[CC_OP
].compute_all();
1126 void OPPROTO
op_jb_cc(void)
1128 if (cc_table
[CC_OP
].compute_c())
1135 void OPPROTO
op_jz_cc(void)
1138 eflags
= cc_table
[CC_OP
].compute_all();
1146 void OPPROTO
op_jbe_cc(void)
1149 eflags
= cc_table
[CC_OP
].compute_all();
1150 if (eflags
& (CC_Z
| CC_C
))
1157 void OPPROTO
op_js_cc(void)
1160 eflags
= cc_table
[CC_OP
].compute_all();
1168 void OPPROTO
op_jp_cc(void)
1171 eflags
= cc_table
[CC_OP
].compute_all();
1179 void OPPROTO
op_jl_cc(void)
1182 eflags
= cc_table
[CC_OP
].compute_all();
1183 if ((eflags
^ (eflags
>> 4)) & 0x80)
1190 void OPPROTO
op_jle_cc(void)
1193 eflags
= cc_table
[CC_OP
].compute_all();
1194 if (((eflags
^ (eflags
>> 4)) & 0x80) || (eflags
& CC_Z
))
1201 /* slow set cases (compute x86 flags) */
1202 void OPPROTO
op_seto_T0_cc(void)
1205 eflags
= cc_table
[CC_OP
].compute_all();
1206 T0
= (eflags
>> 11) & 1;
1209 void OPPROTO
op_setb_T0_cc(void)
1211 T0
= cc_table
[CC_OP
].compute_c();
1214 void OPPROTO
op_setz_T0_cc(void)
1217 eflags
= cc_table
[CC_OP
].compute_all();
1218 T0
= (eflags
>> 6) & 1;
1221 void OPPROTO
op_setbe_T0_cc(void)
1224 eflags
= cc_table
[CC_OP
].compute_all();
1225 T0
= (eflags
& (CC_Z
| CC_C
)) != 0;
1228 void OPPROTO
op_sets_T0_cc(void)
1231 eflags
= cc_table
[CC_OP
].compute_all();
1232 T0
= (eflags
>> 7) & 1;
1235 void OPPROTO
op_setp_T0_cc(void)
1238 eflags
= cc_table
[CC_OP
].compute_all();
1239 T0
= (eflags
>> 2) & 1;
1242 void OPPROTO
op_setl_T0_cc(void)
1245 eflags
= cc_table
[CC_OP
].compute_all();
1246 T0
= ((eflags
^ (eflags
>> 4)) >> 7) & 1;
1249 void OPPROTO
op_setle_T0_cc(void)
1252 eflags
= cc_table
[CC_OP
].compute_all();
1253 T0
= (((eflags
^ (eflags
>> 4)) & 0x80) || (eflags
& CC_Z
)) != 0;
1256 void OPPROTO
op_xor_T0_1(void)
1261 void OPPROTO
op_set_cc_op(void)
1266 #define FL_UPDATE_MASK32 (TF_MASK | AC_MASK | ID_MASK)
1267 #define FL_UPDATE_MASK16 (TF_MASK)
1269 void OPPROTO
op_movl_eflags_T0(void)
1273 CC_SRC
= eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
1274 DF
= 1 - (2 * ((eflags
>> 10) & 1));
1275 /* we also update some system flags as in user mode */
1276 env
->eflags
= (env
->eflags
& ~FL_UPDATE_MASK32
) | (eflags
& FL_UPDATE_MASK32
);
1279 void OPPROTO
op_movw_eflags_T0(void)
1283 CC_SRC
= eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
1284 DF
= 1 - (2 * ((eflags
>> 10) & 1));
1285 /* we also update some system flags as in user mode */
1286 env
->eflags
= (env
->eflags
& ~FL_UPDATE_MASK16
) | (eflags
& FL_UPDATE_MASK16
);
1290 void OPPROTO
op_movw_eflags_T0_vm(void)
1294 CC_SRC
= eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
1295 DF
= 1 - (2 * ((eflags
>> 10) & 1));
1296 /* we also update some system flags as in user mode */
1297 env
->eflags
= (env
->eflags
& ~(FL_UPDATE_MASK16
| VIF_MASK
)) |
1298 (eflags
& FL_UPDATE_MASK16
);
1299 if (eflags
& IF_MASK
) {
1300 env
->eflags
|= VIF_MASK
;
1301 if (env
->eflags
& VIP_MASK
) {
1303 raise_exception(EXCP0D_GPF
);
1309 void OPPROTO
op_movl_eflags_T0_vm(void)
1313 CC_SRC
= eflags
& (CC_O
| CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
);
1314 DF
= 1 - (2 * ((eflags
>> 10) & 1));
1315 /* we also update some system flags as in user mode */
1316 env
->eflags
= (env
->eflags
& ~(FL_UPDATE_MASK32
| VIF_MASK
)) |
1317 (eflags
& FL_UPDATE_MASK32
);
1318 if (eflags
& IF_MASK
) {
1319 env
->eflags
|= VIF_MASK
;
1320 if (env
->eflags
& VIP_MASK
) {
1322 raise_exception(EXCP0D_GPF
);
1328 /* XXX: compute only O flag */
1329 void OPPROTO
op_movb_eflags_T0(void)
1332 of
= cc_table
[CC_OP
].compute_all() & CC_O
;
1333 CC_SRC
= (T0
& (CC_S
| CC_Z
| CC_A
| CC_P
| CC_C
)) | of
;
1336 void OPPROTO
op_movl_T0_eflags(void)
1339 eflags
= cc_table
[CC_OP
].compute_all();
1340 eflags
|= (DF
& DF_MASK
);
1341 eflags
|= env
->eflags
& ~(VM_MASK
| RF_MASK
);
1346 void OPPROTO
op_movl_T0_eflags_vm(void)
1349 eflags
= cc_table
[CC_OP
].compute_all();
1350 eflags
|= (DF
& DF_MASK
);
1351 eflags
|= env
->eflags
& ~(VM_MASK
| RF_MASK
| IF_MASK
);
1352 if (env
->eflags
& VIF_MASK
)
1357 void OPPROTO
op_cld(void)
1362 void OPPROTO
op_std(void)
1367 void OPPROTO
op_clc(void)
1370 eflags
= cc_table
[CC_OP
].compute_all();
1375 void OPPROTO
op_stc(void)
1378 eflags
= cc_table
[CC_OP
].compute_all();
1383 void OPPROTO
op_cmc(void)
1386 eflags
= cc_table
[CC_OP
].compute_all();
1391 void OPPROTO
op_salc(void)
1394 cf
= cc_table
[CC_OP
].compute_c();
1395 EAX
= (EAX
& ~0xff) | ((-cf
) & 0xff);
1398 static int compute_all_eflags(void)
1403 static int compute_c_eflags(void)
1405 return CC_SRC
& CC_C
;
1408 static int compute_c_mul(void)
1415 static int compute_all_mul(void)
1417 int cf
, pf
, af
, zf
, sf
, of
;
1419 pf
= 0; /* undefined */
1420 af
= 0; /* undefined */
1421 zf
= 0; /* undefined */
1422 sf
= 0; /* undefined */
1424 return cf
| pf
| af
| zf
| sf
| of
;
1427 CCTable cc_table
[CC_OP_NB
] = {
1428 [CC_OP_DYNAMIC
] = { /* should never happen */ },
1430 [CC_OP_EFLAGS
] = { compute_all_eflags
, compute_c_eflags
},
1432 [CC_OP_MUL
] = { compute_all_mul
, compute_c_mul
},
1434 [CC_OP_ADDB
] = { compute_all_addb
, compute_c_addb
},
1435 [CC_OP_ADDW
] = { compute_all_addw
, compute_c_addw
},
1436 [CC_OP_ADDL
] = { compute_all_addl
, compute_c_addl
},
1438 [CC_OP_ADCB
] = { compute_all_adcb
, compute_c_adcb
},
1439 [CC_OP_ADCW
] = { compute_all_adcw
, compute_c_adcw
},
1440 [CC_OP_ADCL
] = { compute_all_adcl
, compute_c_adcl
},
1442 [CC_OP_SUBB
] = { compute_all_subb
, compute_c_subb
},
1443 [CC_OP_SUBW
] = { compute_all_subw
, compute_c_subw
},
1444 [CC_OP_SUBL
] = { compute_all_subl
, compute_c_subl
},
1446 [CC_OP_SBBB
] = { compute_all_sbbb
, compute_c_sbbb
},
1447 [CC_OP_SBBW
] = { compute_all_sbbw
, compute_c_sbbw
},
1448 [CC_OP_SBBL
] = { compute_all_sbbl
, compute_c_sbbl
},
1450 [CC_OP_LOGICB
] = { compute_all_logicb
, compute_c_logicb
},
1451 [CC_OP_LOGICW
] = { compute_all_logicw
, compute_c_logicw
},
1452 [CC_OP_LOGICL
] = { compute_all_logicl
, compute_c_logicl
},
1454 [CC_OP_INCB
] = { compute_all_incb
, compute_c_incl
},
1455 [CC_OP_INCW
] = { compute_all_incw
, compute_c_incl
},
1456 [CC_OP_INCL
] = { compute_all_incl
, compute_c_incl
},
1458 [CC_OP_DECB
] = { compute_all_decb
, compute_c_incl
},
1459 [CC_OP_DECW
] = { compute_all_decw
, compute_c_incl
},
1460 [CC_OP_DECL
] = { compute_all_decl
, compute_c_incl
},
1462 [CC_OP_SHLB
] = { compute_all_shlb
, compute_c_shll
},
1463 [CC_OP_SHLW
] = { compute_all_shlw
, compute_c_shll
},
1464 [CC_OP_SHLL
] = { compute_all_shll
, compute_c_shll
},
1466 [CC_OP_SARB
] = { compute_all_sarb
, compute_c_shll
},
1467 [CC_OP_SARW
] = { compute_all_sarw
, compute_c_shll
},
1468 [CC_OP_SARL
] = { compute_all_sarl
, compute_c_shll
},
1471 /* floating point support. Some of the code for complicated x87
1472 functions comes from the LGPL'ed x86 emulator found in the Willows
1473 TWIN windows emulator. */
1475 #ifdef USE_X86LDOUBLE
1476 /* use long double functions */
1477 #define lrint lrintl
1478 #define llrint llrintl
1486 #define atan2 atan2l
1487 #define floor floorl
1492 extern int lrint(CPU86_LDouble x
);
1493 extern int64_t llrint(CPU86_LDouble x
);
1494 extern CPU86_LDouble
fabs(CPU86_LDouble x
);
1495 extern CPU86_LDouble
sin(CPU86_LDouble x
);
1496 extern CPU86_LDouble
cos(CPU86_LDouble x
);
1497 extern CPU86_LDouble
sqrt(CPU86_LDouble x
);
1498 extern CPU86_LDouble
pow(CPU86_LDouble
, CPU86_LDouble
);
1499 extern CPU86_LDouble
log(CPU86_LDouble x
);
1500 extern CPU86_LDouble
tan(CPU86_LDouble x
);
1501 extern CPU86_LDouble
atan2(CPU86_LDouble
, CPU86_LDouble
);
1502 extern CPU86_LDouble
floor(CPU86_LDouble x
);
1503 extern CPU86_LDouble
ceil(CPU86_LDouble x
);
1504 extern CPU86_LDouble
rint(CPU86_LDouble x
);
1506 #define RC_MASK 0xc00
1507 #define RC_NEAR 0x000
1508 #define RC_DOWN 0x400
1510 #define RC_CHOP 0xc00
1512 #define MAXTAN 9223372036854775808.0
1514 #ifdef USE_X86LDOUBLE
1520 unsigned long long lower
;
1521 unsigned short upper
;
1525 /* the following deal with x86 long double-precision numbers */
1526 #define MAXEXPD 0x7fff
1527 #define EXPBIAS 16383
1528 #define EXPD(fp) (fp.l.upper & 0x7fff)
1529 #define SIGND(fp) ((fp.l.upper) & 0x8000)
1530 #define MANTD(fp) (fp.l.lower)
1531 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7fff)) | EXPBIAS
1537 #ifndef WORDS_BIGENDIAN
1539 unsigned long lower
;
1545 unsigned long lower
;
1551 /* the following deal with IEEE double-precision numbers */
1552 #define MAXEXPD 0x7ff
1553 #define EXPBIAS 1023
1554 #define EXPD(fp) (((fp.l.upper) >> 20) & 0x7FF)
1555 #define SIGND(fp) ((fp.l.upper) & 0x80000000)
1556 #define MANTD(fp) (fp.ll & ((1LL << 52) - 1))
1557 #define BIASEXPONENT(fp) fp.l.upper = (fp.l.upper & ~(0x7ff << 20)) | (EXPBIAS << 20)
1562 void OPPROTO
op_flds_FT0_A0(void)
1564 FT0
= ldfl((void *)A0
);
1567 void OPPROTO
op_fldl_FT0_A0(void)
1569 FT0
= ldfq((void *)A0
);
1572 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1573 #ifdef USE_INT_TO_FLOAT_HELPERS
1575 void helper_fild_FT0_A0(void)
1577 FT0
= (CPU86_LDouble
)ldsw((void *)A0
);
1580 void helper_fildl_FT0_A0(void)
1582 FT0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
1585 void helper_fildll_FT0_A0(void)
1587 FT0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1590 void OPPROTO
op_fild_FT0_A0(void)
1592 helper_fild_FT0_A0();
1595 void OPPROTO
op_fildl_FT0_A0(void)
1597 helper_fildl_FT0_A0();
1600 void OPPROTO
op_fildll_FT0_A0(void)
1602 helper_fildll_FT0_A0();
1607 void OPPROTO
op_fild_FT0_A0(void)
1609 FT0
= (CPU86_LDouble
)ldsw((void *)A0
);
1612 void OPPROTO
op_fildl_FT0_A0(void)
1614 FT0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
1617 void OPPROTO
op_fildll_FT0_A0(void)
1619 FT0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1625 void OPPROTO
op_flds_ST0_A0(void)
1627 ST0
= ldfl((void *)A0
);
1630 void OPPROTO
op_fldl_ST0_A0(void)
1632 ST0
= ldfq((void *)A0
);
1635 #ifdef USE_X86LDOUBLE
1636 void OPPROTO
op_fldt_ST0_A0(void)
1638 ST0
= *(long double *)A0
;
1641 void helper_fldt_ST0_A0(void)
1643 CPU86_LDoubleU temp
;
1646 upper
= lduw((uint8_t *)A0
+ 8);
1647 /* XXX: handle overflow ? */
1648 e
= (upper
& 0x7fff) - 16383 + EXPBIAS
; /* exponent */
1649 e
|= (upper
>> 4) & 0x800; /* sign */
1650 temp
.ll
= ((ldq((void *)A0
) >> 11) & ((1LL << 52) - 1)) | ((uint64_t)e
<< 52);
1654 void OPPROTO
op_fldt_ST0_A0(void)
1656 helper_fldt_ST0_A0();
1660 /* helpers are needed to avoid static constant reference. XXX: find a better way */
1661 #ifdef USE_INT_TO_FLOAT_HELPERS
1663 void helper_fild_ST0_A0(void)
1665 ST0
= (CPU86_LDouble
)ldsw((void *)A0
);
1668 void helper_fildl_ST0_A0(void)
1670 ST0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
1673 void helper_fildll_ST0_A0(void)
1675 ST0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1678 void OPPROTO
op_fild_ST0_A0(void)
1680 helper_fild_ST0_A0();
1683 void OPPROTO
op_fildl_ST0_A0(void)
1685 helper_fildl_ST0_A0();
1688 void OPPROTO
op_fildll_ST0_A0(void)
1690 helper_fildll_ST0_A0();
1695 void OPPROTO
op_fild_ST0_A0(void)
1697 ST0
= (CPU86_LDouble
)ldsw((void *)A0
);
1700 void OPPROTO
op_fildl_ST0_A0(void)
1702 ST0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
1705 void OPPROTO
op_fildll_ST0_A0(void)
1707 ST0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1714 void OPPROTO
op_fsts_ST0_A0(void)
1716 stfl((void *)A0
, (float)ST0
);
1719 void OPPROTO
op_fstl_ST0_A0(void)
1721 stfq((void *)A0
, (double)ST0
);
1724 #ifdef USE_X86LDOUBLE
1725 void OPPROTO
op_fstt_ST0_A0(void)
1727 *(long double *)A0
= ST0
;
1730 void helper_fstt_ST0_A0(void)
1732 CPU86_LDoubleU temp
;
1736 stq((void *)A0
, (MANTD(temp
) << 11) | (1LL << 63));
1737 /* exponent + sign */
1738 e
= EXPD(temp
) - EXPBIAS
+ 16383;
1739 e
|= SIGND(temp
) >> 16;
1740 stw((uint8_t *)A0
+ 8, e
);
1743 void OPPROTO
op_fstt_ST0_A0(void)
1745 helper_fstt_ST0_A0();
1749 void OPPROTO
op_fist_ST0_A0(void)
1753 stw((void *)A0
, val
);
1756 void OPPROTO
op_fistl_ST0_A0(void)
1760 stl((void *)A0
, val
);
1763 void OPPROTO
op_fistll_ST0_A0(void)
1767 stq((void *)A0
, val
);
1772 #define MUL10(iv) ( iv + iv + (iv << 3) )
1774 void helper_fbld_ST0_A0(void)
1777 CPU86_LDouble fpsrcop
;
1781 /* in this code, seg/m32i will be used as temporary ptr/int */
1782 seg
= (uint8_t *)A0
+ 8;
1784 /* XXX: raise exception */
1788 /* XXX: raise exception */
1789 if ((v
& 0xf0) != 0)
1791 m32i
= v
; /* <-- d14 */
1793 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d13 */
1794 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d12 */
1796 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d11 */
1797 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d10 */
1799 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d9 */
1800 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d8 */
1801 fpsrcop
= ((CPU86_LDouble
)m32i
) * 100000000.0;
1804 m32i
= (v
>> 4); /* <-- d7 */
1805 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d6 */
1807 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d5 */
1808 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d4 */
1810 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d3 */
1811 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d2 */
1813 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d1 */
1814 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d0 */
1815 fpsrcop
+= ((CPU86_LDouble
)m32i
);
1816 if ( ldub(seg
+9) & 0x80 )
1821 void OPPROTO
op_fbld_ST0_A0(void)
1823 helper_fbld_ST0_A0();
1826 void helper_fbst_ST0_A0(void)
1828 CPU86_LDouble fptemp
;
1829 CPU86_LDouble fpsrcop
;
1831 uint8_t *mem_ref
, *mem_end
;
1833 fpsrcop
= rint(ST0
);
1834 mem_ref
= (uint8_t *)A0
;
1835 mem_end
= mem_ref
+ 8;
1836 if ( fpsrcop
< 0.0 ) {
1837 stw(mem_end
, 0x8000);
1840 stw(mem_end
, 0x0000);
1842 while (mem_ref
< mem_end
) {
1845 fptemp
= floor(fpsrcop
/10.0);
1846 v
= ((int)(fpsrcop
- fptemp
*10.0));
1847 if (fptemp
== 0.0) {
1852 fptemp
= floor(fpsrcop
/10.0);
1853 v
|= (((int)(fpsrcop
- fptemp
*10.0)) << 4);
1857 while (mem_ref
< mem_end
) {
1862 void OPPROTO
op_fbst_ST0_A0(void)
1864 helper_fbst_ST0_A0();
1869 static inline void fpush(void)
1871 env
->fpstt
= (env
->fpstt
- 1) & 7;
1872 env
->fptags
[env
->fpstt
] = 0; /* validate stack entry */
1875 static inline void fpop(void)
1877 env
->fptags
[env
->fpstt
] = 1; /* invvalidate stack entry */
1878 env
->fpstt
= (env
->fpstt
+ 1) & 7;
1881 void OPPROTO
op_fpush(void)
1886 void OPPROTO
op_fpop(void)
1891 void OPPROTO
op_fdecstp(void)
1893 env
->fpstt
= (env
->fpstt
- 1) & 7;
1894 env
->fpus
&= (~0x4700);
1897 void OPPROTO
op_fincstp(void)
1899 env
->fpstt
= (env
->fpstt
+ 1) & 7;
1900 env
->fpus
&= (~0x4700);
1903 void OPPROTO
op_fmov_ST0_FT0(void)
1908 void OPPROTO
op_fmov_FT0_STN(void)
1913 void OPPROTO
op_fmov_ST0_STN(void)
1918 void OPPROTO
op_fmov_STN_ST0(void)
1923 void OPPROTO
op_fxchg_ST0_STN(void)
1931 /* FPU operations */
1933 /* XXX: handle nans */
1934 void OPPROTO
op_fcom_ST0_FT0(void)
1936 env
->fpus
&= (~0x4500); /* (C3,C2,C0) <-- 000 */
1938 env
->fpus
|= 0x100; /* (C3,C2,C0) <-- 001 */
1939 else if (ST0
== FT0
)
1940 env
->fpus
|= 0x4000; /* (C3,C2,C0) <-- 100 */
1944 /* XXX: handle nans */
1945 void OPPROTO
op_fucom_ST0_FT0(void)
1947 env
->fpus
&= (~0x4500); /* (C3,C2,C0) <-- 000 */
1949 env
->fpus
|= 0x100; /* (C3,C2,C0) <-- 001 */
1950 else if (ST0
== FT0
)
1951 env
->fpus
|= 0x4000; /* (C3,C2,C0) <-- 100 */
1955 void OPPROTO
op_fadd_ST0_FT0(void)
1960 void OPPROTO
op_fmul_ST0_FT0(void)
1965 void OPPROTO
op_fsub_ST0_FT0(void)
1970 void OPPROTO
op_fsubr_ST0_FT0(void)
1975 void OPPROTO
op_fdiv_ST0_FT0(void)
1980 void OPPROTO
op_fdivr_ST0_FT0(void)
1985 /* fp operations between STN and ST0 */
1987 void OPPROTO
op_fadd_STN_ST0(void)
1992 void OPPROTO
op_fmul_STN_ST0(void)
1997 void OPPROTO
op_fsub_STN_ST0(void)
2002 void OPPROTO
op_fsubr_STN_ST0(void)
2009 void OPPROTO
op_fdiv_STN_ST0(void)
2014 void OPPROTO
op_fdivr_STN_ST0(void)
2021 /* misc FPU operations */
2022 void OPPROTO
op_fchs_ST0(void)
2027 void OPPROTO
op_fabs_ST0(void)
2032 void helper_fxam_ST0(void)
2034 CPU86_LDoubleU temp
;
2039 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2041 env
->fpus
|= 0x200; /* C1 <-- 1 */
2043 expdif
= EXPD(temp
);
2044 if (expdif
== MAXEXPD
) {
2045 if (MANTD(temp
) == 0)
2046 env
->fpus
|= 0x500 /*Infinity*/;
2048 env
->fpus
|= 0x100 /*NaN*/;
2049 } else if (expdif
== 0) {
2050 if (MANTD(temp
) == 0)
2051 env
->fpus
|= 0x4000 /*Zero*/;
2053 env
->fpus
|= 0x4400 /*Denormal*/;
2059 void OPPROTO
op_fxam_ST0(void)
2064 void OPPROTO
op_fld1_ST0(void)
2066 ST0
= *(CPU86_LDouble
*)&f15rk
[1];
2069 void OPPROTO
op_fldl2t_ST0(void)
2071 ST0
= *(CPU86_LDouble
*)&f15rk
[6];
2074 void OPPROTO
op_fldl2e_ST0(void)
2076 ST0
= *(CPU86_LDouble
*)&f15rk
[5];
2079 void OPPROTO
op_fldpi_ST0(void)
2081 ST0
= *(CPU86_LDouble
*)&f15rk
[2];
2084 void OPPROTO
op_fldlg2_ST0(void)
2086 ST0
= *(CPU86_LDouble
*)&f15rk
[3];
2089 void OPPROTO
op_fldln2_ST0(void)
2091 ST0
= *(CPU86_LDouble
*)&f15rk
[4];
2094 void OPPROTO
op_fldz_ST0(void)
2096 ST0
= *(CPU86_LDouble
*)&f15rk
[0];
2099 void OPPROTO
op_fldz_FT0(void)
2101 ST0
= *(CPU86_LDouble
*)&f15rk
[0];
2104 void helper_f2xm1(void)
2106 ST0
= pow(2.0,ST0
) - 1.0;
2109 void helper_fyl2x(void)
2111 CPU86_LDouble fptemp
;
2115 fptemp
= log(fptemp
)/log(2.0); /* log2(ST) */
2119 env
->fpus
&= (~0x4700);
2124 void helper_fptan(void)
2126 CPU86_LDouble fptemp
;
2129 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2135 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2136 /* the above code is for |arg| < 2**52 only */
2140 void helper_fpatan(void)
2142 CPU86_LDouble fptemp
, fpsrcop
;
2146 ST1
= atan2(fpsrcop
,fptemp
);
2150 void helper_fxtract(void)
2152 CPU86_LDoubleU temp
;
2153 unsigned int expdif
;
2156 expdif
= EXPD(temp
) - EXPBIAS
;
2157 /*DP exponent bias*/
2164 void helper_fprem1(void)
2166 CPU86_LDouble dblq
, fpsrcop
, fptemp
;
2167 CPU86_LDoubleU fpsrcop1
, fptemp1
;
2173 fpsrcop1
.d
= fpsrcop
;
2175 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
2177 dblq
= fpsrcop
/ fptemp
;
2178 dblq
= (dblq
< 0.0)? ceil(dblq
): floor(dblq
);
2179 ST0
= fpsrcop
- fptemp
*dblq
;
2180 q
= (int)dblq
; /* cutting off top bits is assumed here */
2181 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2182 /* (C0,C1,C3) <-- (q2,q1,q0) */
2183 env
->fpus
|= (q
&0x4) << 6; /* (C0) <-- q2 */
2184 env
->fpus
|= (q
&0x2) << 8; /* (C1) <-- q1 */
2185 env
->fpus
|= (q
&0x1) << 14; /* (C3) <-- q0 */
2187 env
->fpus
|= 0x400; /* C2 <-- 1 */
2188 fptemp
= pow(2.0, expdif
-50);
2189 fpsrcop
= (ST0
/ ST1
) / fptemp
;
2190 /* fpsrcop = integer obtained by rounding to the nearest */
2191 fpsrcop
= (fpsrcop
-floor(fpsrcop
) < ceil(fpsrcop
)-fpsrcop
)?
2192 floor(fpsrcop
): ceil(fpsrcop
);
2193 ST0
-= (ST1
* fpsrcop
* fptemp
);
2197 void helper_fprem(void)
2199 CPU86_LDouble dblq
, fpsrcop
, fptemp
;
2200 CPU86_LDoubleU fpsrcop1
, fptemp1
;
2206 fpsrcop1
.d
= fpsrcop
;
2208 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
2209 if ( expdif
< 53 ) {
2210 dblq
= fpsrcop
/ fptemp
;
2211 dblq
= (dblq
< 0.0)? ceil(dblq
): floor(dblq
);
2212 ST0
= fpsrcop
- fptemp
*dblq
;
2213 q
= (int)dblq
; /* cutting off top bits is assumed here */
2214 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2215 /* (C0,C1,C3) <-- (q2,q1,q0) */
2216 env
->fpus
|= (q
&0x4) << 6; /* (C0) <-- q2 */
2217 env
->fpus
|= (q
&0x2) << 8; /* (C1) <-- q1 */
2218 env
->fpus
|= (q
&0x1) << 14; /* (C3) <-- q0 */
2220 env
->fpus
|= 0x400; /* C2 <-- 1 */
2221 fptemp
= pow(2.0, expdif
-50);
2222 fpsrcop
= (ST0
/ ST1
) / fptemp
;
2223 /* fpsrcop = integer obtained by chopping */
2224 fpsrcop
= (fpsrcop
< 0.0)?
2225 -(floor(fabs(fpsrcop
))): floor(fpsrcop
);
2226 ST0
-= (ST1
* fpsrcop
* fptemp
);
2230 void helper_fyl2xp1(void)
2232 CPU86_LDouble fptemp
;
2235 if ((fptemp
+1.0)>0.0) {
2236 fptemp
= log(fptemp
+1.0) / log(2.0); /* log2(ST+1.0) */
2240 env
->fpus
&= (~0x4700);
2245 void helper_fsqrt(void)
2247 CPU86_LDouble fptemp
;
2251 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2257 void helper_fsincos(void)
2259 CPU86_LDouble fptemp
;
2262 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2268 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2269 /* the above code is for |arg| < 2**63 only */
2273 void helper_frndint(void)
2278 void helper_fscale(void)
2280 CPU86_LDouble fpsrcop
, fptemp
;
2283 fptemp
= pow(fpsrcop
,ST1
);
2287 void helper_fsin(void)
2289 CPU86_LDouble fptemp
;
2292 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2296 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2297 /* the above code is for |arg| < 2**53 only */
2301 void helper_fcos(void)
2303 CPU86_LDouble fptemp
;
2306 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2310 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2311 /* the above code is for |arg5 < 2**63 only */
2315 /* associated heplers to reduce generated code length and to simplify
2316 relocation (FP constants are usually stored in .rodata section) */
2318 void OPPROTO
op_f2xm1(void)
2323 void OPPROTO
op_fyl2x(void)
2328 void OPPROTO
op_fptan(void)
2333 void OPPROTO
op_fpatan(void)
2338 void OPPROTO
op_fxtract(void)
2343 void OPPROTO
op_fprem1(void)
2349 void OPPROTO
op_fprem(void)
2354 void OPPROTO
op_fyl2xp1(void)
2359 void OPPROTO
op_fsqrt(void)
2364 void OPPROTO
op_fsincos(void)
2369 void OPPROTO
op_frndint(void)
2374 void OPPROTO
op_fscale(void)
2379 void OPPROTO
op_fsin(void)
2384 void OPPROTO
op_fcos(void)
2389 void OPPROTO
op_fnstsw_A0(void)
2392 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2393 stw((void *)A0
, fpus
);
2396 void OPPROTO
op_fnstsw_EAX(void)
2399 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2400 EAX
= (EAX
& 0xffff0000) | fpus
;
2403 void OPPROTO
op_fnstcw_A0(void)
2405 stw((void *)A0
, env
->fpuc
);
2408 void OPPROTO
op_fldcw_A0(void)
2411 env
->fpuc
= lduw((void *)A0
);
2412 /* set rounding mode */
2413 switch(env
->fpuc
& RC_MASK
) {
2416 rnd_type
= FE_TONEAREST
;
2419 rnd_type
= FE_DOWNWARD
;
2422 rnd_type
= FE_UPWARD
;
2425 rnd_type
= FE_TOWARDZERO
;
2428 fesetround(rnd_type
);
2431 void OPPROTO
op_fclex(void)
2433 env
->fpus
&= 0x7f00;
2436 void OPPROTO
op_fninit(void)
2451 /* threading support */
2452 void OPPROTO
op_lock(void)
2457 void OPPROTO
op_unlock(void)