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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
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
;
60 src2
= CC_DST
- CC_SRC
;
61 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
62 pf
= parity_table
[(uint8_t)CC_DST
];
63 af
= (CC_DST
^ src1
^ src2
) & 0x10;
64 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
65 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
66 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
67 return cf
| pf
| af
| zf
| sf
| of
;
70 static int glue(compute_c_add
, SUFFIX
)(void)
75 cf
= (DATA_TYPE
)CC_DST
< (DATA_TYPE
)src1
;
79 static int glue(compute_all_adc
, SUFFIX
)(void)
81 int cf
, pf
, af
, zf
, sf
, of
;
82 target_long src1
, src2
;
84 src2
= CC_DST
- CC_SRC
- 1;
85 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
86 pf
= parity_table
[(uint8_t)CC_DST
];
87 af
= (CC_DST
^ src1
^ src2
) & 0x10;
88 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
89 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
90 of
= lshift((src1
^ src2
^ -1) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
91 return cf
| pf
| af
| zf
| sf
| of
;
94 static int glue(compute_c_adc
, SUFFIX
)(void)
99 cf
= (DATA_TYPE
)CC_DST
<= (DATA_TYPE
)src1
;
103 static int glue(compute_all_sub
, SUFFIX
)(void)
105 int cf
, pf
, af
, zf
, sf
, of
;
106 target_long src1
, src2
;
107 src1
= CC_DST
+ CC_SRC
;
109 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
110 pf
= parity_table
[(uint8_t)CC_DST
];
111 af
= (CC_DST
^ src1
^ src2
) & 0x10;
112 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
113 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
114 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
115 return cf
| pf
| af
| zf
| sf
| of
;
118 static int glue(compute_c_sub
, SUFFIX
)(void)
121 target_long src1
, src2
;
122 src1
= CC_DST
+ CC_SRC
;
124 cf
= (DATA_TYPE
)src1
< (DATA_TYPE
)src2
;
128 static int glue(compute_all_sbb
, SUFFIX
)(void)
130 int cf
, pf
, af
, zf
, sf
, of
;
131 target_long src1
, src2
;
132 src1
= CC_DST
+ CC_SRC
+ 1;
134 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
135 pf
= parity_table
[(uint8_t)CC_DST
];
136 af
= (CC_DST
^ src1
^ src2
) & 0x10;
137 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
138 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
139 of
= lshift((src1
^ src2
) & (src1
^ CC_DST
), 12 - DATA_BITS
) & CC_O
;
140 return cf
| pf
| af
| zf
| sf
| of
;
143 static int glue(compute_c_sbb
, SUFFIX
)(void)
146 target_long src1
, src2
;
147 src1
= CC_DST
+ CC_SRC
+ 1;
149 cf
= (DATA_TYPE
)src1
<= (DATA_TYPE
)src2
;
153 static int glue(compute_all_logic
, SUFFIX
)(void)
155 int cf
, pf
, af
, zf
, sf
, of
;
157 pf
= parity_table
[(uint8_t)CC_DST
];
159 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
160 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
162 return cf
| pf
| af
| zf
| sf
| of
;
165 static int glue(compute_c_logic
, SUFFIX
)(void)
170 static int glue(compute_all_inc
, SUFFIX
)(void)
172 int cf
, pf
, af
, zf
, sf
, of
;
173 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
)(void)
192 static int glue(compute_all_dec
, SUFFIX
)(void)
194 int cf
, pf
, af
, zf
, sf
, of
;
195 target_long src1
, src2
;
199 pf
= parity_table
[(uint8_t)CC_DST
];
200 af
= (CC_DST
^ src1
^ src2
) & 0x10;
201 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
202 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
203 of
= ((CC_DST
& DATA_MASK
) == ((target_ulong
)SIGN_MASK
- 1)) << 11;
204 return cf
| pf
| af
| zf
| sf
| of
;
207 static int glue(compute_all_shl
, SUFFIX
)(void)
209 int cf
, pf
, af
, zf
, sf
, of
;
210 cf
= (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
211 pf
= parity_table
[(uint8_t)CC_DST
];
212 af
= 0; /* undefined */
213 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
214 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
215 /* of is defined if shift count == 1 */
216 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
217 return cf
| pf
| af
| zf
| sf
| of
;
220 static int glue(compute_c_shl
, SUFFIX
)(void)
222 return (CC_SRC
>> (DATA_BITS
- 1)) & CC_C
;
226 static int glue(compute_c_sar
, SUFFIX
)(void)
232 static int glue(compute_all_sar
, SUFFIX
)(void)
234 int cf
, pf
, af
, zf
, sf
, of
;
236 pf
= parity_table
[(uint8_t)CC_DST
];
237 af
= 0; /* undefined */
238 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
239 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
240 /* of is defined if shift count == 1 */
241 of
= lshift(CC_SRC
^ CC_DST
, 12 - DATA_BITS
) & CC_O
;
242 return cf
| pf
| af
| zf
| sf
| of
;
246 static int glue(compute_c_mul
, SUFFIX
)(void)
254 /* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
255 CF are modified and it is slower to do that. */
256 static int glue(compute_all_mul
, SUFFIX
)(void)
258 int cf
, pf
, af
, zf
, sf
, of
;
260 pf
= parity_table
[(uint8_t)CC_DST
];
261 af
= 0; /* undefined */
262 zf
= ((DATA_TYPE
)CC_DST
== 0) << 6;
263 sf
= lshift(CC_DST
, 8 - DATA_BITS
) & 0x80;
265 return cf
| pf
| af
| zf
| sf
| of
;
270 target_ulong
glue(helper_rcl
, SUFFIX
)(target_ulong t0
, target_ulong t1
)
276 count
= t1
& SHIFT1_MASK
;
278 count
= rclw_table
[count
];
280 count
= rclb_table
[count
];
283 eflags
= helper_cc_compute_all(CC_OP
);
286 res
= (t0
<< count
) | ((target_ulong
)(eflags
& CC_C
) << (count
- 1));
288 res
|= t0
>> (DATA_BITS
+ 1 - count
);
290 env
->cc_tmp
= (eflags
& ~(CC_C
| CC_O
)) |
291 (lshift(src
^ t0
, 11 - (DATA_BITS
- 1)) & CC_O
) |
292 ((src
>> (DATA_BITS
- count
)) & CC_C
);
299 target_ulong
glue(helper_rcr
, SUFFIX
)(target_ulong t0
, target_ulong t1
)
305 count
= t1
& SHIFT1_MASK
;
307 count
= rclw_table
[count
];
309 count
= rclb_table
[count
];
312 eflags
= helper_cc_compute_all(CC_OP
);
315 res
= (t0
>> count
) | ((target_ulong
)(eflags
& CC_C
) << (DATA_BITS
- count
));
317 res
|= t0
<< (DATA_BITS
+ 1 - count
);
319 env
->cc_tmp
= (eflags
& ~(CC_C
| CC_O
)) |
320 (lshift(src
^ t0
, 11 - (DATA_BITS
- 1)) & CC_O
) |
321 ((src
>> (count
- 1)) & CC_C
);