uhci: dynamic type generation
[qemu/opensuse.git] / target-sparc / cc_helper.c
blob63bab077d6c58a6fb5b33b627d9985e376e85b57
1 /*
2 * Helpers for lazy condition code handling
4 * Copyright (c) 2003-2005 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 #include "cpu.h"
21 #include "helper.h"
23 static uint32_t compute_all_flags(CPUSPARCState *env)
25 return env->psr & PSR_ICC;
28 static uint32_t compute_C_flags(CPUSPARCState *env)
30 return env->psr & PSR_CARRY;
33 static inline uint32_t get_NZ_icc(int32_t dst)
35 uint32_t ret = 0;
37 if (dst == 0) {
38 ret = PSR_ZERO;
39 } else if (dst < 0) {
40 ret = PSR_NEG;
42 return ret;
45 #ifdef TARGET_SPARC64
46 static uint32_t compute_all_flags_xcc(CPUSPARCState *env)
48 return env->xcc & PSR_ICC;
51 static uint32_t compute_C_flags_xcc(CPUSPARCState *env)
53 return env->xcc & PSR_CARRY;
56 static inline uint32_t get_NZ_xcc(target_long dst)
58 uint32_t ret = 0;
60 if (!dst) {
61 ret = PSR_ZERO;
62 } else if (dst < 0) {
63 ret = PSR_NEG;
65 return ret;
67 #endif
69 static inline uint32_t get_V_div_icc(target_ulong src2)
71 uint32_t ret = 0;
73 if (src2 != 0) {
74 ret = PSR_OVF;
76 return ret;
79 static uint32_t compute_all_div(CPUSPARCState *env)
81 uint32_t ret;
83 ret = get_NZ_icc(CC_DST);
84 ret |= get_V_div_icc(CC_SRC2);
85 return ret;
88 static uint32_t compute_C_div(CPUSPARCState *env)
90 return 0;
93 static inline uint32_t get_C_add_icc(uint32_t dst, uint32_t src1)
95 uint32_t ret = 0;
97 if (dst < src1) {
98 ret = PSR_CARRY;
100 return ret;
103 static inline uint32_t get_C_addx_icc(uint32_t dst, uint32_t src1,
104 uint32_t src2)
106 uint32_t ret = 0;
108 if (((src1 & src2) | (~dst & (src1 | src2))) & (1U << 31)) {
109 ret = PSR_CARRY;
111 return ret;
114 static inline uint32_t get_V_add_icc(uint32_t dst, uint32_t src1,
115 uint32_t src2)
117 uint32_t ret = 0;
119 if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1U << 31)) {
120 ret = PSR_OVF;
122 return ret;
125 #ifdef TARGET_SPARC64
126 static inline uint32_t get_C_add_xcc(target_ulong dst, target_ulong src1)
128 uint32_t ret = 0;
130 if (dst < src1) {
131 ret = PSR_CARRY;
133 return ret;
136 static inline uint32_t get_C_addx_xcc(target_ulong dst, target_ulong src1,
137 target_ulong src2)
139 uint32_t ret = 0;
141 if (((src1 & src2) | (~dst & (src1 | src2))) & (1ULL << 63)) {
142 ret = PSR_CARRY;
144 return ret;
147 static inline uint32_t get_V_add_xcc(target_ulong dst, target_ulong src1,
148 target_ulong src2)
150 uint32_t ret = 0;
152 if (((src1 ^ src2 ^ -1) & (src1 ^ dst)) & (1ULL << 63)) {
153 ret = PSR_OVF;
155 return ret;
158 static uint32_t compute_all_add_xcc(CPUSPARCState *env)
160 uint32_t ret;
162 ret = get_NZ_xcc(CC_DST);
163 ret |= get_C_add_xcc(CC_DST, CC_SRC);
164 ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
165 return ret;
168 static uint32_t compute_C_add_xcc(CPUSPARCState *env)
170 return get_C_add_xcc(CC_DST, CC_SRC);
172 #endif
174 static uint32_t compute_all_add(CPUSPARCState *env)
176 uint32_t ret;
178 ret = get_NZ_icc(CC_DST);
179 ret |= get_C_add_icc(CC_DST, CC_SRC);
180 ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
181 return ret;
184 static uint32_t compute_C_add(CPUSPARCState *env)
186 return get_C_add_icc(CC_DST, CC_SRC);
189 #ifdef TARGET_SPARC64
190 static uint32_t compute_all_addx_xcc(CPUSPARCState *env)
192 uint32_t ret;
194 ret = get_NZ_xcc(CC_DST);
195 ret |= get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
196 ret |= get_V_add_xcc(CC_DST, CC_SRC, CC_SRC2);
197 return ret;
200 static uint32_t compute_C_addx_xcc(CPUSPARCState *env)
202 uint32_t ret;
204 ret = get_C_addx_xcc(CC_DST, CC_SRC, CC_SRC2);
205 return ret;
207 #endif
209 static uint32_t compute_all_addx(CPUSPARCState *env)
211 uint32_t ret;
213 ret = get_NZ_icc(CC_DST);
214 ret |= get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
215 ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
216 return ret;
219 static uint32_t compute_C_addx(CPUSPARCState *env)
221 uint32_t ret;
223 ret = get_C_addx_icc(CC_DST, CC_SRC, CC_SRC2);
224 return ret;
227 static inline uint32_t get_V_tag_icc(target_ulong src1, target_ulong src2)
229 uint32_t ret = 0;
231 if ((src1 | src2) & 0x3) {
232 ret = PSR_OVF;
234 return ret;
237 static uint32_t compute_all_tadd(CPUSPARCState *env)
239 uint32_t ret;
241 ret = get_NZ_icc(CC_DST);
242 ret |= get_C_add_icc(CC_DST, CC_SRC);
243 ret |= get_V_add_icc(CC_DST, CC_SRC, CC_SRC2);
244 ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
245 return ret;
248 static uint32_t compute_all_taddtv(CPUSPARCState *env)
250 uint32_t ret;
252 ret = get_NZ_icc(CC_DST);
253 ret |= get_C_add_icc(CC_DST, CC_SRC);
254 return ret;
257 static inline uint32_t get_C_sub_icc(uint32_t src1, uint32_t src2)
259 uint32_t ret = 0;
261 if (src1 < src2) {
262 ret = PSR_CARRY;
264 return ret;
267 static inline uint32_t get_C_subx_icc(uint32_t dst, uint32_t src1,
268 uint32_t src2)
270 uint32_t ret = 0;
272 if (((~src1 & src2) | (dst & (~src1 | src2))) & (1U << 31)) {
273 ret = PSR_CARRY;
275 return ret;
278 static inline uint32_t get_V_sub_icc(uint32_t dst, uint32_t src1,
279 uint32_t src2)
281 uint32_t ret = 0;
283 if (((src1 ^ src2) & (src1 ^ dst)) & (1U << 31)) {
284 ret = PSR_OVF;
286 return ret;
290 #ifdef TARGET_SPARC64
291 static inline uint32_t get_C_sub_xcc(target_ulong src1, target_ulong src2)
293 uint32_t ret = 0;
295 if (src1 < src2) {
296 ret = PSR_CARRY;
298 return ret;
301 static inline uint32_t get_C_subx_xcc(target_ulong dst, target_ulong src1,
302 target_ulong src2)
304 uint32_t ret = 0;
306 if (((~src1 & src2) | (dst & (~src1 | src2))) & (1ULL << 63)) {
307 ret = PSR_CARRY;
309 return ret;
312 static inline uint32_t get_V_sub_xcc(target_ulong dst, target_ulong src1,
313 target_ulong src2)
315 uint32_t ret = 0;
317 if (((src1 ^ src2) & (src1 ^ dst)) & (1ULL << 63)) {
318 ret = PSR_OVF;
320 return ret;
323 static uint32_t compute_all_sub_xcc(CPUSPARCState *env)
325 uint32_t ret;
327 ret = get_NZ_xcc(CC_DST);
328 ret |= get_C_sub_xcc(CC_SRC, CC_SRC2);
329 ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
330 return ret;
333 static uint32_t compute_C_sub_xcc(CPUSPARCState *env)
335 return get_C_sub_xcc(CC_SRC, CC_SRC2);
337 #endif
339 static uint32_t compute_all_sub(CPUSPARCState *env)
341 uint32_t ret;
343 ret = get_NZ_icc(CC_DST);
344 ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
345 ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
346 return ret;
349 static uint32_t compute_C_sub(CPUSPARCState *env)
351 return get_C_sub_icc(CC_SRC, CC_SRC2);
354 #ifdef TARGET_SPARC64
355 static uint32_t compute_all_subx_xcc(CPUSPARCState *env)
357 uint32_t ret;
359 ret = get_NZ_xcc(CC_DST);
360 ret |= get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
361 ret |= get_V_sub_xcc(CC_DST, CC_SRC, CC_SRC2);
362 return ret;
365 static uint32_t compute_C_subx_xcc(CPUSPARCState *env)
367 uint32_t ret;
369 ret = get_C_subx_xcc(CC_DST, CC_SRC, CC_SRC2);
370 return ret;
372 #endif
374 static uint32_t compute_all_subx(CPUSPARCState *env)
376 uint32_t ret;
378 ret = get_NZ_icc(CC_DST);
379 ret |= get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
380 ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
381 return ret;
384 static uint32_t compute_C_subx(CPUSPARCState *env)
386 uint32_t ret;
388 ret = get_C_subx_icc(CC_DST, CC_SRC, CC_SRC2);
389 return ret;
392 static uint32_t compute_all_tsub(CPUSPARCState *env)
394 uint32_t ret;
396 ret = get_NZ_icc(CC_DST);
397 ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
398 ret |= get_V_sub_icc(CC_DST, CC_SRC, CC_SRC2);
399 ret |= get_V_tag_icc(CC_SRC, CC_SRC2);
400 return ret;
403 static uint32_t compute_all_tsubtv(CPUSPARCState *env)
405 uint32_t ret;
407 ret = get_NZ_icc(CC_DST);
408 ret |= get_C_sub_icc(CC_SRC, CC_SRC2);
409 return ret;
412 static uint32_t compute_all_logic(CPUSPARCState *env)
414 return get_NZ_icc(CC_DST);
417 static uint32_t compute_C_logic(CPUSPARCState *env)
419 return 0;
422 #ifdef TARGET_SPARC64
423 static uint32_t compute_all_logic_xcc(CPUSPARCState *env)
425 return get_NZ_xcc(CC_DST);
427 #endif
429 typedef struct CCTable {
430 uint32_t (*compute_all)(CPUSPARCState *env); /* return all the flags */
431 uint32_t (*compute_c)(CPUSPARCState *env); /* return the C flag */
432 } CCTable;
434 static const CCTable icc_table[CC_OP_NB] = {
435 /* CC_OP_DYNAMIC should never happen */
436 [CC_OP_FLAGS] = { compute_all_flags, compute_C_flags },
437 [CC_OP_DIV] = { compute_all_div, compute_C_div },
438 [CC_OP_ADD] = { compute_all_add, compute_C_add },
439 [CC_OP_ADDX] = { compute_all_addx, compute_C_addx },
440 [CC_OP_TADD] = { compute_all_tadd, compute_C_add },
441 [CC_OP_TADDTV] = { compute_all_taddtv, compute_C_add },
442 [CC_OP_SUB] = { compute_all_sub, compute_C_sub },
443 [CC_OP_SUBX] = { compute_all_subx, compute_C_subx },
444 [CC_OP_TSUB] = { compute_all_tsub, compute_C_sub },
445 [CC_OP_TSUBTV] = { compute_all_tsubtv, compute_C_sub },
446 [CC_OP_LOGIC] = { compute_all_logic, compute_C_logic },
449 #ifdef TARGET_SPARC64
450 static const CCTable xcc_table[CC_OP_NB] = {
451 /* CC_OP_DYNAMIC should never happen */
452 [CC_OP_FLAGS] = { compute_all_flags_xcc, compute_C_flags_xcc },
453 [CC_OP_DIV] = { compute_all_logic_xcc, compute_C_logic },
454 [CC_OP_ADD] = { compute_all_add_xcc, compute_C_add_xcc },
455 [CC_OP_ADDX] = { compute_all_addx_xcc, compute_C_addx_xcc },
456 [CC_OP_TADD] = { compute_all_add_xcc, compute_C_add_xcc },
457 [CC_OP_TADDTV] = { compute_all_add_xcc, compute_C_add_xcc },
458 [CC_OP_SUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
459 [CC_OP_SUBX] = { compute_all_subx_xcc, compute_C_subx_xcc },
460 [CC_OP_TSUB] = { compute_all_sub_xcc, compute_C_sub_xcc },
461 [CC_OP_TSUBTV] = { compute_all_sub_xcc, compute_C_sub_xcc },
462 [CC_OP_LOGIC] = { compute_all_logic_xcc, compute_C_logic },
464 #endif
466 void helper_compute_psr(CPUSPARCState *env)
468 uint32_t new_psr;
470 new_psr = icc_table[CC_OP].compute_all(env);
471 env->psr = new_psr;
472 #ifdef TARGET_SPARC64
473 new_psr = xcc_table[CC_OP].compute_all(env);
474 env->xcc = new_psr;
475 #endif
476 CC_OP = CC_OP_FLAGS;
479 uint32_t helper_compute_C_icc(CPUSPARCState *env)
481 uint32_t ret;
483 ret = icc_table[CC_OP].compute_c(env) >> PSR_CARRY_SHIFT;
484 return ret;