target-mips: convert single case switch into if statement
[qemu/ar7.git] / target-s390x / cc_helper.c
blob373eb176a113d81c627ac25da1847e858cc02f3b
1 /*
2 * S/390 condition code helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "cpu.h"
22 #include "exec/helper-proto.h"
23 #include "qemu/host-utils.h"
25 /* #define DEBUG_HELPER */
26 #ifdef DEBUG_HELPER
27 #define HELPER_LOG(x...) qemu_log(x)
28 #else
29 #define HELPER_LOG(x...)
30 #endif
32 static uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
34 if (src == dst) {
35 return 0;
36 } else if (src < dst) {
37 return 1;
38 } else {
39 return 2;
43 static uint32_t cc_calc_ltgt0_32(int32_t dst)
45 return cc_calc_ltgt_32(dst, 0);
48 static uint32_t cc_calc_ltgt_64(int64_t src, int64_t dst)
50 if (src == dst) {
51 return 0;
52 } else if (src < dst) {
53 return 1;
54 } else {
55 return 2;
59 static uint32_t cc_calc_ltgt0_64(int64_t dst)
61 return cc_calc_ltgt_64(dst, 0);
64 static uint32_t cc_calc_ltugtu_32(uint32_t src, uint32_t dst)
66 if (src == dst) {
67 return 0;
68 } else if (src < dst) {
69 return 1;
70 } else {
71 return 2;
75 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
77 if (src == dst) {
78 return 0;
79 } else if (src < dst) {
80 return 1;
81 } else {
82 return 2;
86 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
88 uint32_t r = val & mask;
90 if (r == 0) {
91 return 0;
92 } else if (r == mask) {
93 return 3;
94 } else {
95 return 1;
99 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
101 uint64_t r = val & mask;
103 if (r == 0) {
104 return 0;
105 } else if (r == mask) {
106 return 3;
107 } else {
108 int top = clz64(mask);
109 if ((int64_t)(val << top) < 0) {
110 return 2;
111 } else {
112 return 1;
117 static uint32_t cc_calc_nz(uint64_t dst)
119 return !!dst;
122 static uint32_t cc_calc_add_64(int64_t a1, int64_t a2, int64_t ar)
124 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
125 return 3; /* overflow */
126 } else {
127 if (ar < 0) {
128 return 1;
129 } else if (ar > 0) {
130 return 2;
131 } else {
132 return 0;
137 static uint32_t cc_calc_addu_64(uint64_t a1, uint64_t a2, uint64_t ar)
139 return (ar != 0) + 2 * (ar < a1);
142 static uint32_t cc_calc_addc_64(uint64_t a1, uint64_t a2, uint64_t ar)
144 /* Recover a2 + carry_in. */
145 uint64_t a2c = ar - a1;
146 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
147 int carry_out = (a2c < a2) || (ar < a1);
149 return (ar != 0) + 2 * carry_out;
152 static uint32_t cc_calc_sub_64(int64_t a1, int64_t a2, int64_t ar)
154 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
155 return 3; /* overflow */
156 } else {
157 if (ar < 0) {
158 return 1;
159 } else if (ar > 0) {
160 return 2;
161 } else {
162 return 0;
167 static uint32_t cc_calc_subu_64(uint64_t a1, uint64_t a2, uint64_t ar)
169 if (ar == 0) {
170 return 2;
171 } else {
172 if (a2 > a1) {
173 return 1;
174 } else {
175 return 3;
180 static uint32_t cc_calc_subb_64(uint64_t a1, uint64_t a2, uint64_t ar)
182 /* We had borrow-in if normal subtraction isn't equal. */
183 int borrow_in = ar - (a1 - a2);
184 int borrow_out;
186 /* If a2 was ULONG_MAX, and borrow_in, then a2 is logically 65 bits,
187 and we must have had borrow out. */
188 if (borrow_in && a2 == (uint64_t)-1) {
189 borrow_out = 1;
190 } else {
191 a2 += borrow_in;
192 borrow_out = (a2 > a1);
195 return (ar != 0) + 2 * !borrow_out;
198 static uint32_t cc_calc_abs_64(int64_t dst)
200 if ((uint64_t)dst == 0x8000000000000000ULL) {
201 return 3;
202 } else if (dst) {
203 return 1;
204 } else {
205 return 0;
209 static uint32_t cc_calc_nabs_64(int64_t dst)
211 return !!dst;
214 static uint32_t cc_calc_comp_64(int64_t dst)
216 if ((uint64_t)dst == 0x8000000000000000ULL) {
217 return 3;
218 } else if (dst < 0) {
219 return 1;
220 } else if (dst > 0) {
221 return 2;
222 } else {
223 return 0;
228 static uint32_t cc_calc_add_32(int32_t a1, int32_t a2, int32_t ar)
230 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
231 return 3; /* overflow */
232 } else {
233 if (ar < 0) {
234 return 1;
235 } else if (ar > 0) {
236 return 2;
237 } else {
238 return 0;
243 static uint32_t cc_calc_addu_32(uint32_t a1, uint32_t a2, uint32_t ar)
245 return (ar != 0) + 2 * (ar < a1);
248 static uint32_t cc_calc_addc_32(uint32_t a1, uint32_t a2, uint32_t ar)
250 /* Recover a2 + carry_in. */
251 uint32_t a2c = ar - a1;
252 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
253 int carry_out = (a2c < a2) || (ar < a1);
255 return (ar != 0) + 2 * carry_out;
258 static uint32_t cc_calc_sub_32(int32_t a1, int32_t a2, int32_t ar)
260 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
261 return 3; /* overflow */
262 } else {
263 if (ar < 0) {
264 return 1;
265 } else if (ar > 0) {
266 return 2;
267 } else {
268 return 0;
273 static uint32_t cc_calc_subu_32(uint32_t a1, uint32_t a2, uint32_t ar)
275 if (ar == 0) {
276 return 2;
277 } else {
278 if (a2 > a1) {
279 return 1;
280 } else {
281 return 3;
286 static uint32_t cc_calc_subb_32(uint32_t a1, uint32_t a2, uint32_t ar)
288 /* We had borrow-in if normal subtraction isn't equal. */
289 int borrow_in = ar - (a1 - a2);
290 int borrow_out;
292 /* If a2 was UINT_MAX, and borrow_in, then a2 is logically 65 bits,
293 and we must have had borrow out. */
294 if (borrow_in && a2 == (uint32_t)-1) {
295 borrow_out = 1;
296 } else {
297 a2 += borrow_in;
298 borrow_out = (a2 > a1);
301 return (ar != 0) + 2 * !borrow_out;
304 static uint32_t cc_calc_abs_32(int32_t dst)
306 if ((uint32_t)dst == 0x80000000UL) {
307 return 3;
308 } else if (dst) {
309 return 1;
310 } else {
311 return 0;
315 static uint32_t cc_calc_nabs_32(int32_t dst)
317 return !!dst;
320 static uint32_t cc_calc_comp_32(int32_t dst)
322 if ((uint32_t)dst == 0x80000000UL) {
323 return 3;
324 } else if (dst < 0) {
325 return 1;
326 } else if (dst > 0) {
327 return 2;
328 } else {
329 return 0;
333 /* calculate condition code for insert character under mask insn */
334 static uint32_t cc_calc_icm(uint64_t mask, uint64_t val)
336 if ((val & mask) == 0) {
337 return 0;
338 } else {
339 int top = clz64(mask);
340 if ((int64_t)(val << top) < 0) {
341 return 1;
342 } else {
343 return 2;
348 static uint32_t cc_calc_sla_32(uint32_t src, int shift)
350 uint32_t mask = ((1U << shift) - 1U) << (32 - shift);
351 uint32_t sign = 1U << 31;
352 uint32_t match;
353 int32_t r;
355 /* Check if the sign bit stays the same. */
356 if (src & sign) {
357 match = mask;
358 } else {
359 match = 0;
361 if ((src & mask) != match) {
362 /* Overflow. */
363 return 3;
366 r = ((src << shift) & ~sign) | (src & sign);
367 if (r == 0) {
368 return 0;
369 } else if (r < 0) {
370 return 1;
372 return 2;
375 static uint32_t cc_calc_sla_64(uint64_t src, int shift)
377 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
378 uint64_t sign = 1ULL << 63;
379 uint64_t match;
380 int64_t r;
382 /* Check if the sign bit stays the same. */
383 if (src & sign) {
384 match = mask;
385 } else {
386 match = 0;
388 if ((src & mask) != match) {
389 /* Overflow. */
390 return 3;
393 r = ((src << shift) & ~sign) | (src & sign);
394 if (r == 0) {
395 return 0;
396 } else if (r < 0) {
397 return 1;
399 return 2;
402 static uint32_t cc_calc_flogr(uint64_t dst)
404 return dst ? 2 : 0;
407 static uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
408 uint64_t src, uint64_t dst, uint64_t vr)
410 S390CPU *cpu = s390_env_get_cpu(env);
411 uint32_t r = 0;
413 switch (cc_op) {
414 case CC_OP_CONST0:
415 case CC_OP_CONST1:
416 case CC_OP_CONST2:
417 case CC_OP_CONST3:
418 /* cc_op value _is_ cc */
419 r = cc_op;
420 break;
421 case CC_OP_LTGT0_32:
422 r = cc_calc_ltgt0_32(dst);
423 break;
424 case CC_OP_LTGT0_64:
425 r = cc_calc_ltgt0_64(dst);
426 break;
427 case CC_OP_LTGT_32:
428 r = cc_calc_ltgt_32(src, dst);
429 break;
430 case CC_OP_LTGT_64:
431 r = cc_calc_ltgt_64(src, dst);
432 break;
433 case CC_OP_LTUGTU_32:
434 r = cc_calc_ltugtu_32(src, dst);
435 break;
436 case CC_OP_LTUGTU_64:
437 r = cc_calc_ltugtu_64(src, dst);
438 break;
439 case CC_OP_TM_32:
440 r = cc_calc_tm_32(src, dst);
441 break;
442 case CC_OP_TM_64:
443 r = cc_calc_tm_64(src, dst);
444 break;
445 case CC_OP_NZ:
446 r = cc_calc_nz(dst);
447 break;
448 case CC_OP_ADD_64:
449 r = cc_calc_add_64(src, dst, vr);
450 break;
451 case CC_OP_ADDU_64:
452 r = cc_calc_addu_64(src, dst, vr);
453 break;
454 case CC_OP_ADDC_64:
455 r = cc_calc_addc_64(src, dst, vr);
456 break;
457 case CC_OP_SUB_64:
458 r = cc_calc_sub_64(src, dst, vr);
459 break;
460 case CC_OP_SUBU_64:
461 r = cc_calc_subu_64(src, dst, vr);
462 break;
463 case CC_OP_SUBB_64:
464 r = cc_calc_subb_64(src, dst, vr);
465 break;
466 case CC_OP_ABS_64:
467 r = cc_calc_abs_64(dst);
468 break;
469 case CC_OP_NABS_64:
470 r = cc_calc_nabs_64(dst);
471 break;
472 case CC_OP_COMP_64:
473 r = cc_calc_comp_64(dst);
474 break;
476 case CC_OP_ADD_32:
477 r = cc_calc_add_32(src, dst, vr);
478 break;
479 case CC_OP_ADDU_32:
480 r = cc_calc_addu_32(src, dst, vr);
481 break;
482 case CC_OP_ADDC_32:
483 r = cc_calc_addc_32(src, dst, vr);
484 break;
485 case CC_OP_SUB_32:
486 r = cc_calc_sub_32(src, dst, vr);
487 break;
488 case CC_OP_SUBU_32:
489 r = cc_calc_subu_32(src, dst, vr);
490 break;
491 case CC_OP_SUBB_32:
492 r = cc_calc_subb_32(src, dst, vr);
493 break;
494 case CC_OP_ABS_32:
495 r = cc_calc_abs_32(dst);
496 break;
497 case CC_OP_NABS_32:
498 r = cc_calc_nabs_32(dst);
499 break;
500 case CC_OP_COMP_32:
501 r = cc_calc_comp_32(dst);
502 break;
504 case CC_OP_ICM:
505 r = cc_calc_icm(src, dst);
506 break;
507 case CC_OP_SLA_32:
508 r = cc_calc_sla_32(src, dst);
509 break;
510 case CC_OP_SLA_64:
511 r = cc_calc_sla_64(src, dst);
512 break;
513 case CC_OP_FLOGR:
514 r = cc_calc_flogr(dst);
515 break;
517 case CC_OP_NZ_F32:
518 r = set_cc_nz_f32(dst);
519 break;
520 case CC_OP_NZ_F64:
521 r = set_cc_nz_f64(dst);
522 break;
523 case CC_OP_NZ_F128:
524 r = set_cc_nz_f128(make_float128(src, dst));
525 break;
527 default:
528 cpu_abort(CPU(cpu), "Unknown CC operation: %s\n", cc_name(cc_op));
531 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
532 cc_name(cc_op), src, dst, vr, r);
533 return r;
536 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
537 uint64_t vr)
539 return do_calc_cc(env, cc_op, src, dst, vr);
542 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
543 uint64_t dst, uint64_t vr)
545 return do_calc_cc(env, cc_op, src, dst, vr);
548 #ifndef CONFIG_USER_ONLY
549 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
551 load_psw(env, mask, addr);
552 cpu_loop_exit(CPU(s390_env_get_cpu(env)));
555 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
557 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
559 switch (a1 & 0xf00) {
560 case 0x000:
561 env->psw.mask &= ~PSW_MASK_ASC;
562 env->psw.mask |= PSW_ASC_PRIMARY;
563 break;
564 case 0x100:
565 env->psw.mask &= ~PSW_MASK_ASC;
566 env->psw.mask |= PSW_ASC_SECONDARY;
567 break;
568 case 0x300:
569 env->psw.mask &= ~PSW_MASK_ASC;
570 env->psw.mask |= PSW_ASC_HOME;
571 break;
572 default:
573 qemu_log("unknown sacf mode: %" PRIx64 "\n", a1);
574 program_interrupt(env, PGM_SPECIFICATION, 2);
575 break;
578 #endif