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 SHIFT_MASK (DATA_BITS - 1)
22 #define SIGN_MASK (((target_ulong)1) << (DATA_BITS - 1))
24 #define SHIFT1_MASK 0x1f
26 #define SHIFT1_MASK 0x3f
31 #define DATA_TYPE uint8_t
32 #define DATA_STYPE int8_t
33 #define DATA_MASK 0xff
36 #define DATA_TYPE uint16_t
37 #define DATA_STYPE int16_t
38 #define DATA_MASK 0xffff
41 #define DATA_TYPE uint32_t
42 #define DATA_STYPE int32_t
43 #define DATA_MASK 0xffffffff
46 #define DATA_TYPE uint64_t
47 #define DATA_STYPE int64_t
48 #define DATA_MASK 0xffffffffffffffffULL
50 #error unhandled operand size
53 /* dynamic flags computation */
55 static int glue(compute_all_add
, SUFFIX
)(void)
57 int cf
, pf
, af
, zf
, sf
, of
;
58 target_long src1
, src2
;
61 src2
= CC_DST
- CC_SRC
;
62 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
63 pf
= parity_table
[(uint8_t)CC_DST
];
64 af
= (CC_DST
^ src1
^ src2
) & 0x10;
65 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
66 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
67 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
68 return cf
| pf
| af
| zf
| sf
| of
;
71 static int glue(compute_c_add
, SUFFIX
)(void)
77 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
81 static int glue(compute_all_adc
, SUFFIX
)(void)
83 int cf
, pf
, af
, zf
, sf
, of
;
84 target_long src1
, src2
;
87 src2
= CC_DST
- CC_SRC
- 1;
88 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
89 pf
= parity_table
[(uint8_t)CC_DST
];
90 af
= (CC_DST
^ src1
^ src2
) & 0x10;
91 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
92 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
93 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
94 return cf
| pf
| af
| zf
| sf
| of
;
97 static int glue(compute_c_adc
, SUFFIX
)(void)
103 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
107 static int glue(compute_all_sub
, SUFFIX
)(void)
109 int cf
, pf
, af
, zf
, sf
, of
;
110 target_long src1
, src2
;
112 src1
= CC_DST
+ CC_SRC
;
114 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
115 pf
= parity_table
[(uint8_t)CC_DST
];
116 af
= (CC_DST
^ src1
^ src2
) & 0x10;
117 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
118 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
119 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
120 return cf
| pf
| af
| zf
| sf
| of
;
123 static int glue(compute_c_sub
, SUFFIX
)(void)
126 target_long src1
, src2
;
128 src1
= CC_DST
+ CC_SRC
;
130 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
134 static int glue(compute_all_sbb
, SUFFIX
)(void)
136 int cf
, pf
, af
, zf
, sf
, of
;
137 target_long src1
, src2
;
139 src1
= CC_DST
+ CC_SRC
+ 1;
141 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
142 pf
= parity_table
[(uint8_t)CC_DST
];
143 af
= (CC_DST
^ src1
^ src2
) & 0x10;
144 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
145 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
146 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
147 return cf
| pf
| af
| zf
| sf
| of
;
150 static int glue(compute_c_sbb
, SUFFIX
)(void)
153 target_long src1
, src2
;
155 src1
= CC_DST
+ CC_SRC
+ 1;
157 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
161 static int glue(compute_all_logic
, SUFFIX
)(void)
163 int cf
, pf
, af
, zf
, sf
, of
;
166 pf
= parity_table
[(uint8_t)CC_DST
];
168 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
169 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
171 return cf
| pf
| af
| zf
| sf
| of
;
174 static int glue(compute_c_logic
, SUFFIX
)(void)
179 static int glue(compute_all_inc
, SUFFIX
)(void)
181 int cf
, pf
, af
, zf
, sf
, of
;
182 target_long src1
, src2
;
187 pf
= parity_table
[(uint8_t)CC_DST
];
188 af
= (CC_DST
^ src1
^ src2
) & 0x10;
189 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
190 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
191 of
= ((CC_DST
& DATA_MASK
) == SIGN_MASK
) << 11;
192 return cf
| pf
| af
| zf
| sf
| of
;
196 static int glue(compute_c_inc
, SUFFIX
)(void)
202 static int glue(compute_all_dec
, SUFFIX
)(void)
204 int cf
, pf
, af
, zf
, sf
, of
;
205 target_long src1
, src2
;
210 pf
= parity_table
[(uint8_t)CC_DST
];
211 af
= (CC_DST
^ src1
^ src2
) & 0x10;
212 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
213 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
214 of
= ((CC_DST
& DATA_MASK
) == ((target_ulong
)SIGN_MASK
- 1)) << 11;
215 return cf
| pf
| af
| zf
| sf
| of
;
218 static int glue(compute_all_shl
, SUFFIX
)(void)
220 int cf
, pf
, af
, zf
, sf
, of
;
222 cf
= (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
223 pf
= parity_table
[(uint8_t)CC_DST
];
224 af
= 0; /* undefined */
225 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
226 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
227 /* of is defined if shift count == 1 */
228 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
229 return cf
| pf
| af
| zf
| sf
| of
;
232 static int glue(compute_c_shl
, SUFFIX
)(void)
234 return (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
238 static int glue(compute_c_sar
, SUFFIX
)(void)
244 static int glue(compute_all_sar
, SUFFIX
)(void)
246 int cf
, pf
, af
, zf
, sf
, of
;
249 pf
= parity_table
[(uint8_t)CC_DST
];
250 af
= 0; /* undefined */
251 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
252 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
253 /* of is defined if shift count == 1 */
254 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
255 return cf
| pf
| af
| zf
| sf
| of
;
259 static int glue(compute_c_mul
, SUFFIX
)(void)
268 /* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
269 CF are modified and it is slower to do that. */
270 static int glue(compute_all_mul
, SUFFIX
)(void)
272 int cf
, pf
, af
, zf
, sf
, of
;
275 pf
= parity_table
[(uint8_t)CC_DST
];
276 af
= 0; /* undefined */
277 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
278 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
280 return cf
| pf
| af
| zf
| sf
| of
;
285 target_ulong
glue(helper_rcl
, SUFFIX
)(target_ulong t0
, target_ulong t1
)
291 count
= t1
& SHIFT1_MASK
;
293 count
= rclw_table
[count
];
295 count
= rclb_table
[count
];
298 eflags
= helper_cc_compute_all(CC_OP
);
301 res
= (t0
<< count
) | ((target_ulong
)(eflags
& CC_C
) << (count
- 1));
303 res
|= t0
>> (DATA_BITS
+ 1 - count
);
306 env
->cc_tmp
= (eflags
& ~(CC_C
| CC_O
)) |
307 (lshift(src
^ t0
, 11 - (DATA_BITS
- 1)) & CC_O
) |
308 ((src
>> (DATA_BITS
- count
)) & CC_C
);
315 target_ulong
glue(helper_rcr
, SUFFIX
)(target_ulong t0
, target_ulong t1
)
321 count
= t1
& SHIFT1_MASK
;
323 count
= rclw_table
[count
];
325 count
= rclb_table
[count
];
328 eflags
= helper_cc_compute_all(CC_OP
);
331 res
= (t0
>> count
) |
332 ((target_ulong
)(eflags
& CC_C
) << (DATA_BITS
- count
));
334 res
|= t0
<< (DATA_BITS
+ 1 - count
);
337 env
->cc_tmp
= (eflags
& ~(CC_C
| CC_O
)) |
338 (lshift(src
^ t0
, 11 - (DATA_BITS
- 1)) & CC_O
) |
339 ((src
>> (count
- 1)) & CC_C
);