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 uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
36 } else if (src < dst) {
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)
52 } else if (src < dst) {
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)
68 } else if (src < dst) {
75 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
79 } else if (src < dst) {
86 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
88 uint32_t r = val & mask;
92 } else if (r == mask) {
99 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
101 uint64_t r = val & mask;
105 } else if (r == mask) {
108 int top = clz64(mask);
109 if ((int64_t)(val << top) < 0) {
117 static uint32_t cc_calc_nz(uint64_t 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 */
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 */
167 static uint32_t cc_calc_subu_64(uint64_t a1, uint64_t a2, uint64_t ar)
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);
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) {
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) {
209 static uint32_t cc_calc_nabs_64(int64_t dst)
214 static uint32_t cc_calc_comp_64(int64_t dst)
216 if ((uint64_t)dst == 0x8000000000000000ULL) {
218 } else if (dst < 0) {
220 } else if (dst > 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 */
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 */
273 static uint32_t cc_calc_subu_32(uint32_t a1, uint32_t a2, uint32_t ar)
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);
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) {
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) {
315 static uint32_t cc_calc_nabs_32(int32_t dst)
320 static uint32_t cc_calc_comp_32(int32_t dst)
322 if ((uint32_t)dst == 0x80000000UL) {
324 } else if (dst < 0) {
326 } else if (dst > 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) {
339 int top = clz64(mask);
340 if ((int64_t)(val << top) < 0) {
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;
355 /* Check if the sign bit stays the same. */
361 if ((src & mask) != match) {
366 r = ((src << shift) & ~sign) | (src & sign);
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;
382 /* Check if the sign bit stays the same. */
388 if ((src & mask) != match) {
393 r = ((src << shift) & ~sign) | (src & sign);
402 static uint32_t cc_calc_flogr(uint64_t dst)
407 static uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
408 uint64_t src, uint64_t dst, uint64_t vr)
417 /* cc_op value _is_ cc */
421 r = cc_calc_ltgt0_32(dst);
424 r = cc_calc_ltgt0_64(dst);
427 r = cc_calc_ltgt_32(src, dst);
430 r = cc_calc_ltgt_64(src, dst);
432 case CC_OP_LTUGTU_32:
433 r = cc_calc_ltugtu_32(src, dst);
435 case CC_OP_LTUGTU_64:
436 r = cc_calc_ltugtu_64(src, dst);
439 r = cc_calc_tm_32(src, dst);
442 r = cc_calc_tm_64(src, dst);
448 r = cc_calc_add_64(src, dst, vr);
451 r = cc_calc_addu_64(src, dst, vr);
454 r = cc_calc_addc_64(src, dst, vr);
457 r = cc_calc_sub_64(src, dst, vr);
460 r = cc_calc_subu_64(src, dst, vr);
463 r = cc_calc_subb_64(src, dst, vr);
466 r = cc_calc_abs_64(dst);
469 r = cc_calc_nabs_64(dst);
472 r = cc_calc_comp_64(dst);
476 r = cc_calc_add_32(src, dst, vr);
479 r = cc_calc_addu_32(src, dst, vr);
482 r = cc_calc_addc_32(src, dst, vr);
485 r = cc_calc_sub_32(src, dst, vr);
488 r = cc_calc_subu_32(src, dst, vr);
491 r = cc_calc_subb_32(src, dst, vr);
494 r = cc_calc_abs_32(dst);
497 r = cc_calc_nabs_32(dst);
500 r = cc_calc_comp_32(dst);
504 r = cc_calc_icm(src, dst);
507 r = cc_calc_sla_32(src, dst);
510 r = cc_calc_sla_64(src, dst);
513 r = cc_calc_flogr(dst);
517 r = set_cc_nz_f32(dst);
520 r = set_cc_nz_f64(dst);
523 r = set_cc_nz_f128(make_float128(src, dst));
527 cpu_abort(env, "Unknown CC operation: %s\n", cc_name(cc_op));
530 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
531 cc_name(cc_op), src, dst, vr, r);
535 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
538 return do_calc_cc(env, cc_op, src, dst, vr);
541 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
542 uint64_t dst, uint64_t vr)
544 return do_calc_cc(env, cc_op, src, dst, vr);
547 #ifndef CONFIG_USER_ONLY
548 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
550 load_psw(env, mask, addr);
554 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
556 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
558 switch (a1 & 0xf00) {
560 env->psw.mask &= ~PSW_MASK_ASC;
561 env->psw.mask |= PSW_ASC_PRIMARY;
564 env->psw.mask &= ~PSW_MASK_ASC;
565 env->psw.mask |= PSW_ASC_SECONDARY;
568 env->psw.mask &= ~PSW_MASK_ASC;
569 env->psw.mask |= PSW_ASC_HOME;
572 qemu_log("unknown sacf mode: %" PRIx64 "\n", a1);
573 program_interrupt(env, PGM_SPECIFICATION, 2);