x86: fix coding style in helper_template.h
[qemu.git] / target-i386 / helper_template.h
blob8b2d4f08f9273e421658a37a3f3439d98ed3d6e3
1 /*
2 * i386 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 SHIFT_MASK (DATA_BITS - 1)
22 #define SIGN_MASK (((target_ulong)1) << (DATA_BITS - 1))
23 #if DATA_BITS <= 32
24 #define SHIFT1_MASK 0x1f
25 #else
26 #define SHIFT1_MASK 0x3f
27 #endif
29 #if DATA_BITS == 8
30 #define SUFFIX b
31 #define DATA_TYPE uint8_t
32 #define DATA_STYPE int8_t
33 #define DATA_MASK 0xff
34 #elif DATA_BITS == 16
35 #define SUFFIX w
36 #define DATA_TYPE uint16_t
37 #define DATA_STYPE int16_t
38 #define DATA_MASK 0xffff
39 #elif DATA_BITS == 32
40 #define SUFFIX l
41 #define DATA_TYPE uint32_t
42 #define DATA_STYPE int32_t
43 #define DATA_MASK 0xffffffff
44 #elif DATA_BITS == 64
45 #define SUFFIX q
46 #define DATA_TYPE uint64_t
47 #define DATA_STYPE int64_t
48 #define DATA_MASK 0xffffffffffffffffULL
49 #else
50 #error unhandled operand size
51 #endif
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 src1 = CC_SRC;
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)
73 int cf;
74 target_long src1;
76 src1 = CC_SRC;
77 cf = (DATA_TYPE)CC_DST < (DATA_TYPE)src1;
78 return cf;
81 static int glue(compute_all_adc, SUFFIX)(void)
83 int cf, pf, af, zf, sf, of;
84 target_long src1, src2;
86 src1 = CC_SRC;
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)
99 int cf;
100 target_long src1;
102 src1 = CC_SRC;
103 cf = (DATA_TYPE)CC_DST <= (DATA_TYPE)src1;
104 return cf;
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;
113 src2 = 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)
125 int cf;
126 target_long src1, src2;
128 src1 = CC_DST + CC_SRC;
129 src2 = CC_SRC;
130 cf = (DATA_TYPE)src1 < (DATA_TYPE)src2;
131 return cf;
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;
140 src2 = CC_SRC;
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)
152 int cf;
153 target_long src1, src2;
155 src1 = CC_DST + CC_SRC + 1;
156 src2 = CC_SRC;
157 cf = (DATA_TYPE)src1 <= (DATA_TYPE)src2;
158 return cf;
161 static int glue(compute_all_logic, SUFFIX)(void)
163 int cf, pf, af, zf, sf, of;
165 cf = 0;
166 pf = parity_table[(uint8_t)CC_DST];
167 af = 0;
168 zf = ((DATA_TYPE)CC_DST == 0) << 6;
169 sf = lshift(CC_DST, 8 - DATA_BITS) & 0x80;
170 of = 0;
171 return cf | pf | af | zf | sf | of;
174 static int glue(compute_c_logic, SUFFIX)(void)
176 return 0;
179 static int glue(compute_all_inc, SUFFIX)(void)
181 int cf, pf, af, zf, sf, of;
182 target_long src1, src2;
184 src1 = CC_DST - 1;
185 src2 = 1;
186 cf = CC_SRC;
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;
195 #if DATA_BITS == 32
196 static int glue(compute_c_inc, SUFFIX)(void)
198 return CC_SRC;
200 #endif
202 static int glue(compute_all_dec, SUFFIX)(void)
204 int cf, pf, af, zf, sf, of;
205 target_long src1, src2;
207 src1 = CC_DST + 1;
208 src2 = 1;
209 cf = CC_SRC;
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;
237 #if DATA_BITS == 32
238 static int glue(compute_c_sar, SUFFIX)(void)
240 return CC_SRC & 1;
242 #endif
244 static int glue(compute_all_sar, SUFFIX)(void)
246 int cf, pf, af, zf, sf, of;
248 cf = CC_SRC & 1;
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;
258 #if DATA_BITS == 32
259 static int glue(compute_c_mul, SUFFIX)(void)
261 int cf;
263 cf = (CC_SRC != 0);
264 return cf;
266 #endif
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;
274 cf = (CC_SRC != 0);
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;
279 of = cf << 11;
280 return cf | pf | af | zf | sf | of;
283 /* shifts */
285 target_ulong glue(helper_rcl, SUFFIX)(target_ulong t0, target_ulong t1)
287 int count, eflags;
288 target_ulong src;
289 target_long res;
291 count = t1 & SHIFT1_MASK;
292 #if DATA_BITS == 16
293 count = rclw_table[count];
294 #elif DATA_BITS == 8
295 count = rclb_table[count];
296 #endif
297 if (count) {
298 eflags = helper_cc_compute_all(CC_OP);
299 t0 &= DATA_MASK;
300 src = t0;
301 res = (t0 << count) | ((target_ulong)(eflags & CC_C) << (count - 1));
302 if (count > 1) {
303 res |= t0 >> (DATA_BITS + 1 - count);
305 t0 = res;
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);
309 } else {
310 env->cc_tmp = -1;
312 return t0;
315 target_ulong glue(helper_rcr, SUFFIX)(target_ulong t0, target_ulong t1)
317 int count, eflags;
318 target_ulong src;
319 target_long res;
321 count = t1 & SHIFT1_MASK;
322 #if DATA_BITS == 16
323 count = rclw_table[count];
324 #elif DATA_BITS == 8
325 count = rclb_table[count];
326 #endif
327 if (count) {
328 eflags = helper_cc_compute_all(CC_OP);
329 t0 &= DATA_MASK;
330 src = t0;
331 res = (t0 >> count) |
332 ((target_ulong)(eflags & CC_C) << (DATA_BITS - count));
333 if (count > 1) {
334 res |= t0 << (DATA_BITS + 1 - count);
336 t0 = res;
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);
340 } else {
341 env->cc_tmp = -1;
343 return t0;
346 #undef DATA_BITS
347 #undef SHIFT_MASK
348 #undef SHIFT1_MASK
349 #undef SIGN_MASK
350 #undef DATA_TYPE
351 #undef DATA_STYPE
352 #undef DATA_MASK
353 #undef SUFFIX