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 #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
)
424 int32_t idiv64(int32_t *q_ptr
, int64_t num
, int32_t den
)
431 void OPPROTO
op_divl_EAX_T0(void)
433 unsigned int den
, q
, r
;
436 num
= EAX
| ((uint64_t)EDX
<< 32);
439 raise_exception(EXCP00_DIVZ
);
440 #ifdef BUGGY_GCC_DIV64
441 r
= div64(&q
, num
, den
);
450 void OPPROTO
op_idivl_EAX_T0(void)
455 num
= EAX
| ((uint64_t)EDX
<< 32);
458 raise_exception(EXCP00_DIVZ
);
459 #ifdef BUGGY_GCC_DIV64
460 r
= idiv64(&q
, num
, den
);
469 /* constant load & misc op */
471 void OPPROTO
op_movl_T0_im(void)
476 void OPPROTO
op_addl_T0_im(void)
481 void OPPROTO
op_andl_T0_ffff(void)
486 void OPPROTO
op_movl_T0_T1(void)
491 void OPPROTO
op_movl_T1_im(void)
496 void OPPROTO
op_addl_T1_im(void)
501 void OPPROTO
op_movl_T1_A0(void)
506 void OPPROTO
op_movl_A0_im(void)
511 void OPPROTO
op_addl_A0_im(void)
516 void OPPROTO
op_addl_A0_AL(void)
521 void OPPROTO
op_andl_A0_ffff(void)
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;
607 void OPPROTO
op_jmp_T0(void)
612 void OPPROTO
op_jmp_im(void)
617 void OPPROTO
op_int_im(void)
622 raise_exception_err(EXCP0D_GPF
, intno
* 8 + 2);
625 void OPPROTO
op_raise_exception(void)
628 exception_index
= PARAM1
;
629 raise_exception(exception_index
);
632 void OPPROTO
op_into(void)
635 eflags
= cc_table
[CC_OP
].compute_all();
638 raise_exception(EXCP04_INTO
);
643 void OPPROTO
op_cli(void)
645 env
->eflags
&= ~IF_MASK
;
648 void OPPROTO
op_sti(void)
650 env
->eflags
|= IF_MASK
;
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
) {
665 raise_exception(EXCP0D_GPF
);
671 void OPPROTO
op_boundw(void)
674 low
= ldsw((uint8_t *)A0
);
675 high
= ldsw((uint8_t *)A0
+ 2);
677 if (v
< low
|| v
> high
)
678 raise_exception(EXCP05_BOUND
);
682 void OPPROTO
op_boundl(void)
685 low
= ldl((uint8_t *)A0
);
686 high
= ldl((uint8_t *)A0
+ 4);
688 if (v
< low
|| v
> high
)
689 raise_exception(EXCP05_BOUND
);
693 void OPPROTO
op_cmpxchg8b(void)
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
);
717 #include "ops_template.h"
721 #include "ops_template.h"
725 #include "ops_template.h"
730 void OPPROTO
op_movsbl_T0_T0(void)
735 void OPPROTO
op_movzbl_T0_T0(void)
740 void OPPROTO
op_movswl_T0_T0(void)
745 void OPPROTO
op_movzwl_T0_T0(void)
750 void OPPROTO
op_movswl_EAX_AX(void)
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);
772 void op_pushl_T0(void)
776 stl((void *)offset
, T0
);
777 /* modify ESP after to handle exceptions correctly */
781 void op_pushw_T0(void)
785 stw((void *)offset
, T0
);
786 /* modify ESP after to handle exceptions correctly */
790 void op_pushl_ss32_T0(void)
794 stl(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
795 /* modify ESP after to handle exceptions correctly */
799 void op_pushw_ss32_T0(void)
803 stw(env
->seg_cache
[R_SS
].base
+ offset
, T0
);
804 /* modify ESP after to handle exceptions correctly */
808 void op_pushl_ss16_T0(void)
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)
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)
862 void op_addl_ESP_2(void)
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)
882 void op_addw_ESP_im(void)
884 ESP
= (ESP
& ~0xffff) | ((ESP
+ PARAM1
) & 0xffff);
892 void OPPROTO
op_rdtsc(void)
896 asm("rdtsc" : "=A" (val
));
898 /* better than nothing: the time increases */
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)
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)
930 EAX
= 1; /* max EAX index supported */
939 EDX
= CPUID_FP87
| CPUID_DE
| CPUID_PSE
|
940 CPUID_TSC
| CPUID_MSR
| CPUID_MCE
|
945 void OPPROTO
op_cpuid(void)
953 void OPPROTO
op_aam(void)
960 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
964 void OPPROTO
op_aad(void)
969 ah
= (EAX
>> 8) & 0xff;
970 al
= ((ah
* base
) + al
) & 0xff;
971 EAX
= (EAX
& ~0xffff) | al
;
975 void OPPROTO
op_aaa(void)
981 eflags
= cc_table
[CC_OP
].compute_all();
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
;
992 eflags
&= ~(CC_C
| CC_A
);
995 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
999 void OPPROTO
op_aas(void)
1005 eflags
= cc_table
[CC_OP
].compute_all();
1008 ah
= (EAX
>> 8) & 0xff;
1011 if (((al
& 0x0f) > 9 ) || af
) {
1012 al
= (al
- 6) & 0x0f;
1013 ah
= (ah
- 1 - icarry
) & 0xff;
1014 eflags
|= CC_C
| CC_A
;
1016 eflags
&= ~(CC_C
| CC_A
);
1019 EAX
= (EAX
& ~0xffff) | al
| (ah
<< 8);
1023 void OPPROTO
op_daa(void)
1028 eflags
= cc_table
[CC_OP
].compute_all();
1034 if (((al
& 0x0f) > 9 ) || af
) {
1035 al
= (al
+ 6) & 0xff;
1038 if ((al
> 0x9f) || cf
) {
1039 al
= (al
+ 0x60) & 0xff;
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 */
1050 void OPPROTO
op_das(void)
1052 int al
, al1
, af
, cf
;
1055 eflags
= cc_table
[CC_OP
].compute_all();
1062 if (((al
& 0x0f) > 9 ) || af
) {
1066 al
= (al
- 6) & 0xff;
1068 if ((al1
> 0x99) || cf
) {
1069 al
= (al
- 0x60) & 0xff;
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 */
1080 /* segment handling */
1082 /* XXX: use static VM86 information */
1083 void load_seg(int seg_reg
, int selector
)
1086 SegmentDescriptorTable
*dt
;
1091 sc
= &env
->seg_cache
[seg_reg
];
1092 if (env
->eflags
& VM_MASK
) {
1093 sc
->base
= (void *)(selector
<< 4);
1101 index
= selector
& ~7;
1102 if ((index
+ 7) > dt
->limit
)
1103 raise_exception_err(EXCP0D_GPF
, selector
);
1104 ptr
= dt
->base
+ index
;
1107 sc
->base
= (void *)((e1
>> 16) | ((e2
& 0xff) << 16) | (e2
& 0xff000000));
1108 sc
->limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
1110 sc
->limit
= (sc
->limit
<< 12) | 0xfff;
1111 sc
->seg_32bit
= (e2
>> 22) & 1;
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
);
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
;
1148 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1149 selector
= T0
& 0xffff;
1154 index
= selector
& ~7;
1155 if ((index
+ 7) > dt
->limit
)
1157 ptr
= dt
->base
+ index
;
1160 limit
= (e1
& 0xffff) | (e2
& 0x000f0000);
1162 limit
= (limit
<< 12) | 0xfff;
1167 void OPPROTO
op_lsl(void)
1172 void helper_lar(void)
1174 unsigned int selector
;
1175 SegmentDescriptorTable
*dt
;
1180 CC_SRC
= cc_table
[CC_OP
].compute_all() & ~CC_Z
;
1181 selector
= T0
& 0xffff;
1186 index
= selector
& ~7;
1187 if ((index
+ 7) > dt
->limit
)
1189 ptr
= dt
->base
+ index
;
1191 T1
= e2
& 0x00f0ff00;
1195 void OPPROTO
op_lar(void)
1200 /* flags handling */
1202 /* slow jumps cases (compute x86 flags) */
1203 void OPPROTO
op_jo_cc(void)
1206 eflags
= cc_table
[CC_OP
].compute_all();
1214 void OPPROTO
op_jb_cc(void)
1216 if (cc_table
[CC_OP
].compute_c())
1223 void OPPROTO
op_jz_cc(void)
1226 eflags
= cc_table
[CC_OP
].compute_all();
1234 void OPPROTO
op_jbe_cc(void)
1237 eflags
= cc_table
[CC_OP
].compute_all();
1238 if (eflags
& (CC_Z
| CC_C
))
1245 void OPPROTO
op_js_cc(void)
1248 eflags
= cc_table
[CC_OP
].compute_all();
1256 void OPPROTO
op_jp_cc(void)
1259 eflags
= cc_table
[CC_OP
].compute_all();
1267 void OPPROTO
op_jl_cc(void)
1270 eflags
= cc_table
[CC_OP
].compute_all();
1271 if ((eflags
^ (eflags
>> 4)) & 0x80)
1278 void OPPROTO
op_jle_cc(void)
1281 eflags
= cc_table
[CC_OP
].compute_all();
1282 if (((eflags
^ (eflags
>> 4)) & 0x80) || (eflags
& CC_Z
))
1289 /* slow set cases (compute x86 flags) */
1290 void OPPROTO
op_seto_T0_cc(void)
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)
1305 eflags
= cc_table
[CC_OP
].compute_all();
1306 T0
= (eflags
>> 6) & 1;
1309 void OPPROTO
op_setbe_T0_cc(void)
1312 eflags
= cc_table
[CC_OP
].compute_all();
1313 T0
= (eflags
& (CC_Z
| CC_C
)) != 0;
1316 void OPPROTO
op_sets_T0_cc(void)
1319 eflags
= cc_table
[CC_OP
].compute_all();
1320 T0
= (eflags
>> 7) & 1;
1323 void OPPROTO
op_setp_T0_cc(void)
1326 eflags
= cc_table
[CC_OP
].compute_all();
1327 T0
= (eflags
>> 2) & 1;
1330 void OPPROTO
op_setl_T0_cc(void)
1333 eflags
= cc_table
[CC_OP
].compute_all();
1334 T0
= ((eflags
^ (eflags
>> 4)) >> 7) & 1;
1337 void OPPROTO
op_setle_T0_cc(void)
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)
1349 void OPPROTO
op_set_cc_op(void)
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)
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)
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
);
1378 /* vm86plus version */
1379 void OPPROTO
op_movw_eflags_T0_vm(void)
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
) {
1392 raise_exception(EXCP0D_GPF
);
1398 void OPPROTO
op_movl_eflags_T0_vm(void)
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
) {
1411 raise_exception(EXCP0D_GPF
);
1418 /* XXX: compute only O flag */
1419 void OPPROTO
op_movb_eflags_T0(void)
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)
1429 eflags
= cc_table
[CC_OP
].compute_all();
1430 eflags
|= (DF
& DF_MASK
);
1431 eflags
|= env
->eflags
& ~(VM_MASK
| RF_MASK
);
1435 /* vm86plus version */
1437 void OPPROTO
op_movl_T0_eflags_vm(void)
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
)
1449 void OPPROTO
op_cld(void)
1454 void OPPROTO
op_std(void)
1459 void OPPROTO
op_clc(void)
1462 eflags
= cc_table
[CC_OP
].compute_all();
1467 void OPPROTO
op_stc(void)
1470 eflags
= cc_table
[CC_OP
].compute_all();
1475 void OPPROTO
op_cmc(void)
1478 eflags
= cc_table
[CC_OP
].compute_all();
1483 void OPPROTO
op_salc(void)
1486 cf
= cc_table
[CC_OP
].compute_c();
1487 EAX
= (EAX
& ~0xff) | ((-cf
) & 0xff);
1490 static int compute_all_eflags(void)
1495 static int compute_c_eflags(void)
1497 return CC_SRC
& CC_C
;
1500 static int compute_c_mul(void)
1507 static int compute_all_mul(void)
1509 int cf
, pf
, af
, zf
, sf
, of
;
1511 pf
= 0; /* undefined */
1512 af
= 0; /* undefined */
1513 zf
= 0; /* undefined */
1514 sf
= 0; /* undefined */
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
1578 #define atan2 atan2l
1579 #define floor floorl
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;
1615 #define rint qemu_rint
1618 #define RC_MASK 0xc00
1619 #define RC_NEAR 0x000
1620 #define RC_DOWN 0x400
1622 #define RC_CHOP 0xc00
1624 #define MAXTAN 9223372036854775808.0
1626 #ifdef USE_X86LDOUBLE
1632 unsigned long long lower
;
1633 unsigned short upper
;
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
1649 #ifndef WORDS_BIGENDIAN
1651 unsigned long lower
;
1657 unsigned long lower
;
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)
1674 void OPPROTO
op_flds_FT0_A0(void)
1676 #ifdef USE_FP_CONVERT
1677 FP_CONVERT
.i32
= ldl((void *)A0
);
1680 FT0
= ldfl((void *)A0
);
1684 void OPPROTO
op_fldl_FT0_A0(void)
1686 #ifdef USE_FP_CONVERT
1687 FP_CONVERT
.i64
= ldq((void *)A0
);
1690 FT0
= ldfq((void *)A0
);
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();
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
;
1735 FT0
= (CPU86_LDouble
)ldsw((void *)A0
);
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
;
1745 FT0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
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
;
1755 FT0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1762 void OPPROTO
op_flds_ST0_A0(void)
1764 #ifdef USE_FP_CONVERT
1765 FP_CONVERT
.i32
= ldl((void *)A0
);
1768 ST0
= ldfl((void *)A0
);
1772 void OPPROTO
op_fldl_ST0_A0(void)
1774 #ifdef USE_FP_CONVERT
1775 FP_CONVERT
.i64
= ldq((void *)A0
);
1778 ST0
= ldfq((void *)A0
);
1782 #ifdef USE_X86LDOUBLE
1783 void OPPROTO
op_fldt_ST0_A0(void)
1785 ST0
= *(long double *)A0
;
1788 void helper_fldt_ST0_A0(void)
1790 CPU86_LDoubleU temp
;
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);
1801 void OPPROTO
op_fldt_ST0_A0(void)
1803 helper_fldt_ST0_A0();
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();
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
;
1848 ST0
= (CPU86_LDouble
)ldsw((void *)A0
);
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
;
1858 ST0
= (CPU86_LDouble
)((int32_t)ldl((void *)A0
));
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
;
1868 ST0
= (CPU86_LDouble
)((int64_t)ldq((void *)A0
));
1876 void OPPROTO
op_fsts_ST0_A0(void)
1878 #ifdef USE_FP_CONVERT
1880 stfl((void *)A0
, FP_CONVERT
.f
);
1882 stfl((void *)A0
, (float)ST0
);
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
;
1897 void helper_fstt_ST0_A0(void)
1899 CPU86_LDoubleU temp
;
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();
1916 void OPPROTO
op_fist_ST0_A0(void)
1918 #if defined(__sparc__) && !defined(__sparc_v9__)
1919 register CPU86_LDouble d
asm("o0");
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");
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");
1955 stq((void *)A0
, val
);
1960 #define MUL10(iv) ( iv + iv + (iv << 3) )
1962 void helper_fbld_ST0_A0(void)
1965 CPU86_LDouble fpsrcop
;
1969 /* in this code, seg/m32i will be used as temporary ptr/int */
1970 seg
= (uint8_t *)A0
+ 8;
1972 /* XXX: raise exception */
1976 /* XXX: raise exception */
1977 if ((v
& 0xf0) != 0)
1979 m32i
= v
; /* <-- d14 */
1981 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d13 */
1982 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d12 */
1984 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d11 */
1985 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d10 */
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;
1992 m32i
= (v
>> 4); /* <-- d7 */
1993 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d6 */
1995 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d5 */
1996 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d4 */
1998 m32i
= MUL10(m32i
) + (v
>> 4); /* <-- val * 10 + d3 */
1999 m32i
= MUL10(m32i
) + (v
& 0xf); /* <-- val * 10 + d2 */
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 )
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
;
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);
2028 stw(mem_end
, 0x0000);
2030 while (mem_ref
< mem_end
) {
2033 fptemp
= floor(fpsrcop
/10.0);
2034 v
= ((int)(fpsrcop
- fptemp
*10.0));
2035 if (fptemp
== 0.0) {
2040 fptemp
= floor(fpsrcop
/10.0);
2041 v
|= (((int)(fpsrcop
- fptemp
*10.0)) << 4);
2045 while (mem_ref
< mem_end
) {
2050 void OPPROTO
op_fbst_ST0_A0(void)
2052 helper_fbst_ST0_A0();
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)
2074 void OPPROTO
op_fpop(void)
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)
2096 void OPPROTO
op_fmov_FT0_STN(void)
2101 void OPPROTO
op_fmov_ST0_STN(void)
2106 void OPPROTO
op_fmov_STN_ST0(void)
2111 void OPPROTO
op_fxchg_ST0_STN(void)
2119 /* FPU operations */
2121 /* XXX: handle nans */
2122 void OPPROTO
op_fcom_ST0_FT0(void)
2124 env
->fpus
&= (~0x4500); /* (C3,C2,C0) <-- 000 */
2126 env
->fpus
|= 0x100; /* (C3,C2,C0) <-- 001 */
2127 else if (ST0
== FT0
)
2128 env
->fpus
|= 0x4000; /* (C3,C2,C0) <-- 100 */
2132 /* XXX: handle nans */
2133 void OPPROTO
op_fucom_ST0_FT0(void)
2135 env
->fpus
&= (~0x4500); /* (C3,C2,C0) <-- 000 */
2137 env
->fpus
|= 0x100; /* (C3,C2,C0) <-- 001 */
2138 else if (ST0
== FT0
)
2139 env
->fpus
|= 0x4000; /* (C3,C2,C0) <-- 100 */
2143 void OPPROTO
op_fadd_ST0_FT0(void)
2148 void OPPROTO
op_fmul_ST0_FT0(void)
2153 void OPPROTO
op_fsub_ST0_FT0(void)
2158 void OPPROTO
op_fsubr_ST0_FT0(void)
2163 void OPPROTO
op_fdiv_ST0_FT0(void)
2168 void OPPROTO
op_fdivr_ST0_FT0(void)
2173 /* fp operations between STN and ST0 */
2175 void OPPROTO
op_fadd_STN_ST0(void)
2180 void OPPROTO
op_fmul_STN_ST0(void)
2185 void OPPROTO
op_fsub_STN_ST0(void)
2190 void OPPROTO
op_fsubr_STN_ST0(void)
2197 void OPPROTO
op_fdiv_STN_ST0(void)
2202 void OPPROTO
op_fdivr_STN_ST0(void)
2209 /* misc FPU operations */
2210 void OPPROTO
op_fchs_ST0(void)
2215 void OPPROTO
op_fabs_ST0(void)
2220 void helper_fxam_ST0(void)
2222 CPU86_LDoubleU temp
;
2227 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2229 env
->fpus
|= 0x200; /* C1 <-- 1 */
2231 expdif
= EXPD(temp
);
2232 if (expdif
== MAXEXPD
) {
2233 if (MANTD(temp
) == 0)
2234 env
->fpus
|= 0x500 /*Infinity*/;
2236 env
->fpus
|= 0x100 /*NaN*/;
2237 } else if (expdif
== 0) {
2238 if (MANTD(temp
) == 0)
2239 env
->fpus
|= 0x4000 /*Zero*/;
2241 env
->fpus
|= 0x4400 /*Denormal*/;
2247 void OPPROTO
op_fxam_ST0(void)
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
;
2303 fptemp
= log(fptemp
)/log(2.0); /* log2(ST) */
2307 env
->fpus
&= (~0x4700);
2312 void helper_fptan(void)
2314 CPU86_LDouble fptemp
;
2317 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
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
;
2334 ST1
= atan2(fpsrcop
,fptemp
);
2338 void helper_fxtract(void)
2340 CPU86_LDoubleU temp
;
2341 unsigned int expdif
;
2344 expdif
= EXPD(temp
) - EXPBIAS
;
2345 /*DP exponent bias*/
2352 void helper_fprem1(void)
2354 CPU86_LDouble dblq
, fpsrcop
, fptemp
;
2355 CPU86_LDoubleU fpsrcop1
, fptemp1
;
2361 fpsrcop1
.d
= fpsrcop
;
2363 expdif
= EXPD(fpsrcop1
) - EXPD(fptemp1
);
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 */
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
;
2394 fpsrcop1
.d
= fpsrcop
;
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 */
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
;
2423 if ((fptemp
+1.0)>0.0) {
2424 fptemp
= log(fptemp
+1.0) / log(2.0); /* log2(ST+1.0) */
2428 env
->fpus
&= (~0x4700);
2433 void helper_fsqrt(void)
2435 CPU86_LDouble fptemp
;
2439 env
->fpus
&= (~0x4700); /* (C3,C2,C1,C0) <-- 0000 */
2445 void helper_fsincos(void)
2447 CPU86_LDouble fptemp
;
2450 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
2456 env
->fpus
&= (~0x400); /* C2 <-- 0 */
2457 /* the above code is for |arg| < 2**63 only */
2461 void helper_frndint(void)
2466 void helper_fscale(void)
2468 CPU86_LDouble fpsrcop
, fptemp
;
2471 fptemp
= pow(fpsrcop
,ST1
);
2475 void helper_fsin(void)
2477 CPU86_LDouble fptemp
;
2480 if ((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
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
;
2494 if((fptemp
> MAXTAN
)||(fptemp
< -MAXTAN
)) {
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)
2511 void OPPROTO
op_fyl2x(void)
2516 void OPPROTO
op_fptan(void)
2521 void OPPROTO
op_fpatan(void)
2526 void OPPROTO
op_fxtract(void)
2531 void OPPROTO
op_fprem1(void)
2537 void OPPROTO
op_fprem(void)
2542 void OPPROTO
op_fyl2xp1(void)
2547 void OPPROTO
op_fsqrt(void)
2552 void OPPROTO
op_fsincos(void)
2557 void OPPROTO
op_frndint(void)
2562 void OPPROTO
op_fscale(void)
2567 void OPPROTO
op_fsin(void)
2572 void OPPROTO
op_fcos(void)
2577 void OPPROTO
op_fnstsw_A0(void)
2580 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
2581 stw((void *)A0
, fpus
);
2584 void OPPROTO
op_fnstsw_EAX(void)
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)
2599 env
->fpuc
= lduw((void *)A0
);
2600 /* set rounding mode */
2601 switch(env
->fpuc
& RC_MASK
) {
2604 rnd_type
= FE_TONEAREST
;
2607 rnd_type
= FE_DOWNWARD
;
2610 rnd_type
= FE_UPWARD
;
2613 rnd_type
= FE_TOWARDZERO
;
2616 fesetround(rnd_type
);
2619 void OPPROTO
op_fclex(void)
2621 env
->fpus
&= 0x7f00;
2624 void OPPROTO
op_fninit(void)
2639 /* threading support */
2640 void OPPROTO
op_lock(void)
2645 void OPPROTO
op_unlock(void)