2 * x86 condition code helpers
4 * Copyright (c) 2008 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #define DATA_BITS (1 << (3 + SHIFT))
21 #define SIGN_MASK (((target_ulong)1) << (DATA_BITS - 1))
25 #define DATA_TYPE uint8_t
26 #define DATA_MASK 0xff
29 #define DATA_TYPE uint16_t
30 #define DATA_MASK 0xffff
33 #define DATA_TYPE uint32_t
34 #define DATA_MASK 0xffffffff
37 #define DATA_TYPE uint64_t
38 #define DATA_MASK 0xffffffffffffffffULL
40 #error unhandled operand size
43 /* dynamic flags computation */
45 static int glue(compute_all_add
, SUFFIX
)(CPUX86State
*env
)
47 int cf
, pf
, af
, zf
, sf
, of
;
48 target_long src1
, src2
;
51 src2
= CC_DST
- CC_SRC
;
52 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
53 pf
= parity_table
[(uint8_t)CC_DST
];
54 af
= (CC_DST
^ src1
^ src2
) & 0x10;
55 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
56 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
57 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
58 return cf
| pf
| af
| zf
| sf
| of
;
61 static int glue(compute_c_add
, SUFFIX
)(CPUX86State
*env
)
67 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
71 static int glue(compute_all_adc
, SUFFIX
)(CPUX86State
*env
)
73 int cf
, pf
, af
, zf
, sf
, of
;
74 target_long src1
, src2
;
77 src2
= CC_DST
- CC_SRC
- 1;
78 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
79 pf
= parity_table
[(uint8_t)CC_DST
];
80 af
= (CC_DST
^ src1
^ src2
) & 0x10;
81 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
82 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
83 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
84 return cf
| pf
| af
| zf
| sf
| of
;
87 static int glue(compute_c_adc
, SUFFIX
)(CPUX86State
*env
)
93 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
97 static int glue(compute_all_sub
, SUFFIX
)(CPUX86State
*env
)
99 int cf
, pf
, af
, zf
, sf
, of
;
100 target_long src1
, src2
;
102 src1
= CC_DST
+ CC_SRC
;
104 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
105 pf
= parity_table
[(uint8_t)CC_DST
];
106 af
= (CC_DST
^ src1
^ src2
) & 0x10;
107 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
108 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
109 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
110 return cf
| pf
| af
| zf
| sf
| of
;
113 static int glue(compute_c_sub
, SUFFIX
)(CPUX86State
*env
)
116 target_long src1
, src2
;
118 src1
= CC_DST
+ CC_SRC
;
120 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
124 static int glue(compute_all_sbb
, SUFFIX
)(CPUX86State
*env
)
126 int cf
, pf
, af
, zf
, sf
, of
;
127 target_long src1
, src2
;
129 src1
= CC_DST
+ CC_SRC
+ 1;
131 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
132 pf
= parity_table
[(uint8_t)CC_DST
];
133 af
= (CC_DST
^ src1
^ src2
) & 0x10;
134 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
135 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
136 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
137 return cf
| pf
| af
| zf
| sf
| of
;
140 static int glue(compute_c_sbb
, SUFFIX
)(CPUX86State
*env
)
143 target_long src1
, src2
;
145 src1
= CC_DST
+ CC_SRC
+ 1;
147 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
151 static int glue(compute_all_logic
, SUFFIX
)(CPUX86State
*env
)
153 int cf
, pf
, af
, zf
, sf
, of
;
156 pf
= parity_table
[(uint8_t)CC_DST
];
158 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
159 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
161 return cf
| pf
| af
| zf
| sf
| of
;
164 static int glue(compute_c_logic
, SUFFIX
)(void)
169 static int glue(compute_all_inc
, SUFFIX
)(CPUX86State
*env
)
171 int cf
, pf
, af
, zf
, sf
, of
;
172 target_long src1
, src2
;
177 pf
= parity_table
[(uint8_t)CC_DST
];
178 af
= (CC_DST
^ src1
^ src2
) & 0x10;
179 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
180 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
181 of
= ((CC_DST
& DATA_MASK
) == SIGN_MASK
) << 11;
182 return cf
| pf
| af
| zf
| sf
| of
;
186 static int glue(compute_c_inc
, SUFFIX
)(CPUX86State
*env
)
192 static int glue(compute_all_dec
, SUFFIX
)(CPUX86State
*env
)
194 int cf
, pf
, af
, zf
, sf
, of
;
195 target_long src1
, src2
;
200 pf
= parity_table
[(uint8_t)CC_DST
];
201 af
= (CC_DST
^ src1
^ src2
) & 0x10;
202 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
203 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
204 of
= ((CC_DST
& DATA_MASK
) == ((target_ulong
)SIGN_MASK
- 1)) << 11;
205 return cf
| pf
| af
| zf
| sf
| of
;
208 static int glue(compute_all_shl
, SUFFIX
)(CPUX86State
*env
)
210 int cf
, pf
, af
, zf
, sf
, of
;
212 cf
= (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
213 pf
= parity_table
[(uint8_t)CC_DST
];
214 af
= 0; /* undefined */
215 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
216 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
217 /* of is defined if shift count == 1 */
218 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
219 return cf
| pf
| af
| zf
| sf
| of
;
222 static int glue(compute_c_shl
, SUFFIX
)(CPUX86State
*env
)
224 return (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
228 static int glue(compute_c_sar
, SUFFIX
)(CPUX86State
*env
)
234 static int glue(compute_all_sar
, SUFFIX
)(CPUX86State
*env
)
236 int cf
, pf
, af
, zf
, sf
, of
;
239 pf
= parity_table
[(uint8_t)CC_DST
];
240 af
= 0; /* undefined */
241 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
242 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
243 /* of is defined if shift count == 1 */
244 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
245 return cf
| pf
| af
| zf
| sf
| of
;
249 static int glue(compute_c_mul
, SUFFIX
)(CPUX86State
*env
)
258 /* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
259 CF are modified and it is slower to do that. */
260 static int glue(compute_all_mul
, SUFFIX
)(CPUX86State
*env
)
262 int cf
, pf
, af
, zf
, sf
, of
;
265 pf
= parity_table
[(uint8_t)CC_DST
];
266 af
= 0; /* undefined */
267 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
268 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
270 return cf
| pf
| af
| zf
| sf
| of
;