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/>.
19 #define DATA_BITS (1 << (3 + SHIFT))
20 #define SHIFT_MASK (DATA_BITS - 1)
21 #define SIGN_MASK (((target_ulong)1) << (DATA_BITS - 1))
23 #define SHIFT1_MASK 0x1f
25 #define SHIFT1_MASK 0x3f
30 #define DATA_TYPE uint8_t
31 #define DATA_STYPE int8_t
32 #define DATA_MASK 0xff
35 #define DATA_TYPE uint16_t
36 #define DATA_STYPE int16_t
37 #define DATA_MASK 0xffff
40 #define DATA_TYPE uint32_t
41 #define DATA_STYPE int32_t
42 #define DATA_MASK 0xffffffff
45 #define DATA_TYPE uint64_t
46 #define DATA_STYPE int64_t
47 #define DATA_MASK 0xffffffffffffffffULL
49 #error unhandled operand size
52 /* dynamic flags computation */
54 static int glue(compute_all_add, SUFFIX)(void)
56 int cf, pf, af, zf, sf, of;
57 target_long src1, src2;
59 src2 = CC_DST - CC_SRC;
60 cf = (DATA_TYPE)CC_DST < (DATA_TYPE)src1;
61 pf = parity_table[(uint8_t)CC_DST];
62 af = (CC_DST ^ src1 ^ src2) & 0x10;
63 zf = ((DATA_TYPE)CC_DST == 0) << 6;
64 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
65 of = lshift((src1 ^ src2 ^ -1) & (src1 ^ CC_DST), 12 - DATA_BITS) & CC_O;
66 return cf | pf | af | zf | sf | of;
69 static int glue(compute_c_add, SUFFIX)(void)
74 cf = (DATA_TYPE)CC_DST < (DATA_TYPE)src1;
78 static int glue(compute_all_adc, SUFFIX)(void)
80 int cf, pf, af, zf, sf, of;
81 target_long src1, src2;
83 src2 = CC_DST - CC_SRC - 1;
84 cf = (DATA_TYPE)CC_DST <= (DATA_TYPE)src1;
85 pf = parity_table[(uint8_t)CC_DST];
86 af = (CC_DST ^ src1 ^ src2) & 0x10;
87 zf = ((DATA_TYPE)CC_DST == 0) << 6;
88 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
89 of = lshift((src1 ^ src2 ^ -1) & (src1 ^ CC_DST), 12 - DATA_BITS) & CC_O;
90 return cf | pf | af | zf | sf | of;
93 static int glue(compute_c_adc, SUFFIX)(void)
98 cf = (DATA_TYPE)CC_DST <= (DATA_TYPE)src1;
102 static int glue(compute_all_sub, SUFFIX)(void)
104 int cf, pf, af, zf, sf, of;
105 target_long src1, src2;
106 src1 = CC_DST + CC_SRC;
108 cf = (DATA_TYPE)src1 < (DATA_TYPE)src2;
109 pf = parity_table[(uint8_t)CC_DST];
110 af = (CC_DST ^ src1 ^ src2) & 0x10;
111 zf = ((DATA_TYPE)CC_DST == 0) << 6;
112 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
113 of = lshift((src1 ^ src2) & (src1 ^ CC_DST), 12 - DATA_BITS) & CC_O;
114 return cf | pf | af | zf | sf | of;
117 static int glue(compute_c_sub, SUFFIX)(void)
120 target_long src1, src2;
121 src1 = CC_DST + CC_SRC;
123 cf = (DATA_TYPE)src1 < (DATA_TYPE)src2;
127 static int glue(compute_all_sbb, SUFFIX)(void)
129 int cf, pf, af, zf, sf, of;
130 target_long src1, src2;
131 src1 = CC_DST + CC_SRC + 1;
133 cf = (DATA_TYPE)src1 <= (DATA_TYPE)src2;
134 pf = parity_table[(uint8_t)CC_DST];
135 af = (CC_DST ^ src1 ^ src2) & 0x10;
136 zf = ((DATA_TYPE)CC_DST == 0) << 6;
137 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
138 of = lshift((src1 ^ src2) & (src1 ^ CC_DST), 12 - DATA_BITS) & CC_O;
139 return cf | pf | af | zf | sf | of;
142 static int glue(compute_c_sbb, SUFFIX)(void)
145 target_long src1, src2;
146 src1 = CC_DST + CC_SRC + 1;
148 cf = (DATA_TYPE)src1 <= (DATA_TYPE)src2;
152 static int glue(compute_all_logic, SUFFIX)(void)
154 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)(void)
171 int cf, pf, af, zf, sf, of;
172 target_long src1, src2;
176 pf = parity_table[(uint8_t)CC_DST];
177 af = (CC_DST ^ src1 ^ src2) & 0x10;
178 zf = ((DATA_TYPE)CC_DST == 0) << 6;
179 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
180 of = ((CC_DST & DATA_MASK) == SIGN_MASK) << 11;
181 return cf | pf | af | zf | sf | of;
185 static int glue(compute_c_inc, SUFFIX)(void)
191 static int glue(compute_all_dec, SUFFIX)(void)
193 int cf, pf, af, zf, sf, of;
194 target_long src1, src2;
198 pf = parity_table[(uint8_t)CC_DST];
199 af = (CC_DST ^ src1 ^ src2) & 0x10;
200 zf = ((DATA_TYPE)CC_DST == 0) << 6;
201 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
202 of = ((CC_DST & DATA_MASK) == ((target_ulong)SIGN_MASK - 1)) << 11;
203 return cf | pf | af | zf | sf | of;
206 static int glue(compute_all_shl, SUFFIX)(void)
208 int cf, pf, af, zf, sf, of;
209 cf = (CC_SRC >> (DATA_BITS - 1)) & CC_C;
210 pf = parity_table[(uint8_t)CC_DST];
211 af = 0; /* undefined */
212 zf = ((DATA_TYPE)CC_DST == 0) << 6;
213 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
214 /* of is defined if shift count == 1 */
215 of = lshift(CC_SRC ^ CC_DST, 12 - DATA_BITS) & CC_O;
216 return cf | pf | af | zf | sf | of;
219 static int glue(compute_c_shl, SUFFIX)(void)
221 return (CC_SRC >> (DATA_BITS - 1)) & CC_C;
225 static int glue(compute_c_sar, SUFFIX)(void)
231 static int glue(compute_all_sar, SUFFIX)(void)
233 int cf, pf, af, zf, sf, of;
235 pf = parity_table[(uint8_t)CC_DST];
236 af = 0; /* undefined */
237 zf = ((DATA_TYPE)CC_DST == 0) << 6;
238 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
239 /* of is defined if shift count == 1 */
240 of = lshift(CC_SRC ^ CC_DST, 12 - DATA_BITS) & CC_O;
241 return cf | pf | af | zf | sf | of;
245 static int glue(compute_c_mul, SUFFIX)(void)
253 /* NOTE: we compute the flags like the P4. On olders CPUs, only OF and
254 CF are modified and it is slower to do that. */
255 static int glue(compute_all_mul, SUFFIX)(void)
257 int cf, pf, af, zf, sf, of;
259 pf = parity_table[(uint8_t)CC_DST];
260 af = 0; /* undefined */
261 zf = ((DATA_TYPE)CC_DST == 0) << 6;
262 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
264 return cf | pf | af | zf | sf | of;
269 target_ulong glue(helper_rcl, SUFFIX)(target_ulong t0, target_ulong t1)
275 count = t1 & SHIFT1_MASK;
277 count = rclw_table[count];
279 count = rclb_table[count];
282 eflags = helper_cc_compute_all(CC_OP);
285 res = (t0 << count) | ((target_ulong)(eflags & CC_C) << (count - 1));
287 res |= t0 >> (DATA_BITS + 1 - count);
289 env->cc_tmp = (eflags & ~(CC_C | CC_O)) |
290 (lshift(src ^ t0, 11 - (DATA_BITS - 1)) & CC_O) |
291 ((src >> (DATA_BITS - count)) & CC_C);
298 target_ulong glue(helper_rcr, SUFFIX)(target_ulong t0, target_ulong t1)
304 count = t1 & SHIFT1_MASK;
306 count = rclw_table[count];
308 count = rclb_table[count];
311 eflags = helper_cc_compute_all(CC_OP);
314 res = (t0 >> count) | ((target_ulong)(eflags & CC_C) << (DATA_BITS - count));
316 res |= t0 << (DATA_BITS + 1 - count);
318 env->cc_tmp = (eflags & ~(CC_C | CC_O)) |
319 (lshift(src ^ t0, 11 - (DATA_BITS - 1)) & CC_O) |
320 ((src >> (count - 1)) & CC_C);