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/>.
23 #include "qemu/host-utils.h"
25 /* #define DEBUG_HELPER */
27 #define HELPER_LOG(x...) qemu_log(x)
29 #define HELPER_LOG(x...)
32 static inline uint32_t cc_calc_ltgt_32(CPUS390XState *env, int32_t src,
37 } else if (src < dst) {
44 static inline uint32_t cc_calc_ltgt0_32(CPUS390XState *env, int32_t dst)
46 return cc_calc_ltgt_32(env, dst, 0);
49 static inline uint32_t cc_calc_ltgt_64(CPUS390XState *env, int64_t src,
54 } else if (src < dst) {
61 static inline uint32_t cc_calc_ltgt0_64(CPUS390XState *env, int64_t dst)
63 return cc_calc_ltgt_64(env, dst, 0);
66 static inline uint32_t cc_calc_ltugtu_32(CPUS390XState *env, uint32_t src,
71 } else if (src < dst) {
78 static inline uint32_t cc_calc_ltugtu_64(CPUS390XState *env, uint64_t src,
83 } else if (src < dst) {
90 static uint32_t cc_calc_tm_32(CPUS390XState *env, uint32_t val, uint32_t mask)
92 uint32_t r = val & mask;
96 } else if (r == mask) {
103 static uint32_t cc_calc_tm_64(CPUS390XState *env, uint64_t val, uint64_t mask)
105 uint64_t r = val & mask;
109 } else if (r == mask) {
112 int top = clz64(mask);
113 if ((int64_t)(val << top) < 0) {
121 static inline uint32_t cc_calc_nz(CPUS390XState *env, uint64_t dst)
126 static inline uint32_t cc_calc_add_64(CPUS390XState *env, int64_t a1,
127 int64_t a2, int64_t ar)
129 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
130 return 3; /* overflow */
142 static uint32_t cc_calc_addu_64(CPUS390XState *env, uint64_t a1,
143 uint64_t a2, uint64_t ar)
145 return (ar != 0) + 2 * (ar < a1);
148 static uint32_t cc_calc_addc_64(CPUS390XState *env, uint64_t a1,
149 uint64_t a2, uint64_t ar)
151 /* Recover a2 + carry_in. */
152 uint64_t a2c = ar - a1;
153 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
154 int carry_out = (a2c < a2) || (ar < a1);
156 return (ar != 0) + 2 * carry_out;
159 static inline uint32_t cc_calc_sub_64(CPUS390XState *env, int64_t a1,
160 int64_t a2, int64_t ar)
162 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
163 return 3; /* overflow */
175 static inline uint32_t cc_calc_subu_64(CPUS390XState *env, uint64_t a1,
176 uint64_t a2, uint64_t ar)
189 static uint32_t cc_calc_subb_64(CPUS390XState *env, uint64_t a1,
190 uint64_t a2, uint64_t ar)
192 /* We had borrow-in if normal subtraction isn't equal. */
193 int borrow_in = ar - (a1 - a2);
196 /* If a2 was ULONG_MAX, and borrow_in, then a2 is logically 65 bits,
197 and we must have had borrow out. */
198 if (borrow_in && a2 == (uint64_t)-1) {
202 borrow_out = (a2 > a1);
205 return (ar != 0) + 2 * !borrow_out;
208 static inline uint32_t cc_calc_abs_64(CPUS390XState *env, int64_t dst)
210 if ((uint64_t)dst == 0x8000000000000000ULL) {
219 static inline uint32_t cc_calc_nabs_64(CPUS390XState *env, int64_t dst)
224 static inline uint32_t cc_calc_comp_64(CPUS390XState *env, int64_t dst)
226 if ((uint64_t)dst == 0x8000000000000000ULL) {
228 } else if (dst < 0) {
230 } else if (dst > 0) {
238 static inline uint32_t cc_calc_add_32(CPUS390XState *env, int32_t a1,
239 int32_t a2, int32_t ar)
241 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
242 return 3; /* overflow */
254 static uint32_t cc_calc_addu_32(CPUS390XState *env, uint32_t a1,
255 uint32_t a2, uint32_t ar)
257 return (ar != 0) + 2 * (ar < a1);
260 static uint32_t cc_calc_addc_32(CPUS390XState *env, uint32_t a1,
261 uint32_t a2, uint32_t ar)
263 /* Recover a2 + carry_in. */
264 uint32_t a2c = ar - a1;
265 /* Check for a2+carry_in overflow, then a1+a2c overflow. */
266 int carry_out = (a2c < a2) || (ar < a1);
268 return (ar != 0) + 2 * carry_out;
271 static inline uint32_t cc_calc_sub_32(CPUS390XState *env, int32_t a1,
272 int32_t a2, int32_t ar)
274 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
275 return 3; /* overflow */
287 static inline uint32_t cc_calc_subu_32(CPUS390XState *env, uint32_t a1,
288 uint32_t a2, uint32_t ar)
301 static uint32_t cc_calc_subb_32(CPUS390XState *env, uint32_t a1,
302 uint32_t a2, uint32_t ar)
304 /* We had borrow-in if normal subtraction isn't equal. */
305 int borrow_in = ar - (a1 - a2);
308 /* If a2 was UINT_MAX, and borrow_in, then a2 is logically 65 bits,
309 and we must have had borrow out. */
310 if (borrow_in && a2 == (uint32_t)-1) {
314 borrow_out = (a2 > a1);
317 return (ar != 0) + 2 * !borrow_out;
320 static inline uint32_t cc_calc_abs_32(CPUS390XState *env, int32_t dst)
322 if ((uint32_t)dst == 0x80000000UL) {
331 static inline uint32_t cc_calc_nabs_32(CPUS390XState *env, int32_t dst)
336 static inline uint32_t cc_calc_comp_32(CPUS390XState *env, int32_t dst)
338 if ((uint32_t)dst == 0x80000000UL) {
340 } else if (dst < 0) {
342 } else if (dst > 0) {
349 /* calculate condition code for insert character under mask insn */
350 static inline uint32_t cc_calc_icm_32(CPUS390XState *env, uint32_t mask,
355 HELPER_LOG("%s: mask 0x%x val %d\n", __func__, mask, val);
359 } else if (val & 0x80000000) {
382 static inline uint32_t cc_calc_slag(CPUS390XState *env, uint64_t src,
385 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
388 /* check if the sign bit stays the same */
389 if (src & (1ULL << 63)) {
395 if ((src & mask) != match) {
400 r = ((src << shift) & ((1ULL << 63) - 1)) | (src & (1ULL << 63));
402 if ((int64_t)r == 0) {
404 } else if ((int64_t)r < 0) {
412 static inline uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
413 uint64_t src, uint64_t dst, uint64_t vr)
422 /* cc_op value _is_ cc */
426 r = cc_calc_ltgt0_32(env, dst);
429 r = cc_calc_ltgt0_64(env, dst);
432 r = cc_calc_ltgt_32(env, src, dst);
435 r = cc_calc_ltgt_64(env, src, dst);
437 case CC_OP_LTUGTU_32:
438 r = cc_calc_ltugtu_32(env, src, dst);
440 case CC_OP_LTUGTU_64:
441 r = cc_calc_ltugtu_64(env, src, dst);
444 r = cc_calc_tm_32(env, src, dst);
447 r = cc_calc_tm_64(env, src, dst);
450 r = cc_calc_nz(env, dst);
453 r = cc_calc_add_64(env, src, dst, vr);
456 r = cc_calc_addu_64(env, src, dst, vr);
459 r = cc_calc_addc_64(env, src, dst, vr);
462 r = cc_calc_sub_64(env, src, dst, vr);
465 r = cc_calc_subu_64(env, src, dst, vr);
468 r = cc_calc_subb_64(env, src, dst, vr);
471 r = cc_calc_abs_64(env, dst);
474 r = cc_calc_nabs_64(env, dst);
477 r = cc_calc_comp_64(env, dst);
481 r = cc_calc_add_32(env, src, dst, vr);
484 r = cc_calc_addu_32(env, src, dst, vr);
487 r = cc_calc_addc_32(env, src, dst, vr);
490 r = cc_calc_sub_32(env, src, dst, vr);
493 r = cc_calc_subu_32(env, src, dst, vr);
496 r = cc_calc_subb_32(env, src, dst, vr);
499 r = cc_calc_abs_64(env, dst);
502 r = cc_calc_nabs_64(env, dst);
505 r = cc_calc_comp_32(env, dst);
509 r = cc_calc_icm_32(env, src, dst);
512 r = cc_calc_slag(env, src, dst);
516 r = set_cc_f32(env, src, dst);
519 r = set_cc_f64(env, src, dst);
522 r = set_cc_nz_f32(dst);
525 r = set_cc_nz_f64(dst);
529 cpu_abort(env, "Unknown CC operation: %s\n", cc_name(cc_op));
532 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
533 cc_name(cc_op), src, dst, vr, r);
537 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
540 return do_calc_cc(env, cc_op, src, dst, vr);
543 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
544 uint64_t dst, uint64_t vr)
546 return do_calc_cc(env, cc_op, src, dst, vr);
549 /* insert psw mask and condition code into r1 */
550 void HELPER(ipm)(CPUS390XState *env, uint32_t cc, uint32_t r1)
552 uint64_t r = env->regs[r1];
554 r &= 0xffffffff00ffffffULL;
555 r |= (cc << 28) | ((env->psw.mask >> 40) & 0xf);
557 HELPER_LOG("%s: cc %d psw.mask 0x%lx r1 0x%lx\n", __func__,
558 cc, env->psw.mask, r);
561 #ifndef CONFIG_USER_ONLY
562 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
564 load_psw(env, mask, addr);
568 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
570 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
572 switch (a1 & 0xf00) {
574 env->psw.mask &= ~PSW_MASK_ASC;
575 env->psw.mask |= PSW_ASC_PRIMARY;
578 env->psw.mask &= ~PSW_MASK_ASC;
579 env->psw.mask |= PSW_ASC_SECONDARY;
582 env->psw.mask &= ~PSW_MASK_ASC;
583 env->psw.mask |= PSW_ASC_HOME;
586 qemu_log("unknown sacf mode: %" PRIx64 "\n", a1);
587 program_interrupt(env, PGM_SPECIFICATION, 2);