target/s390x: Improve cc computation for SUBTRACT LOGICAL
[qemu/ar7.git] / target / s390x / cc_helper.c
blobc7728d1225a3090945de65ddf8b750fa61ebcb3f
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.1 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 "qemu/osdep.h"
22 #include "cpu.h"
23 #include "internal.h"
24 #include "tcg_s390x.h"
25 #include "exec/exec-all.h"
26 #include "exec/helper-proto.h"
27 #include "qemu/host-utils.h"
29 /* #define DEBUG_HELPER */
30 #ifdef DEBUG_HELPER
31 #define HELPER_LOG(x...) qemu_log(x)
32 #else
33 #define HELPER_LOG(x...)
34 #endif
36 static uint32_t cc_calc_ltgt_32(int32_t src, int32_t dst)
38 if (src == dst) {
39 return 0;
40 } else if (src < dst) {
41 return 1;
42 } else {
43 return 2;
47 static uint32_t cc_calc_ltgt0_32(int32_t dst)
49 return cc_calc_ltgt_32(dst, 0);
52 static uint32_t cc_calc_ltgt_64(int64_t src, int64_t dst)
54 if (src == dst) {
55 return 0;
56 } else if (src < dst) {
57 return 1;
58 } else {
59 return 2;
63 static uint32_t cc_calc_ltgt0_64(int64_t dst)
65 return cc_calc_ltgt_64(dst, 0);
68 static uint32_t cc_calc_ltugtu_32(uint32_t src, uint32_t dst)
70 if (src == dst) {
71 return 0;
72 } else if (src < dst) {
73 return 1;
74 } else {
75 return 2;
79 static uint32_t cc_calc_ltugtu_64(uint64_t src, uint64_t dst)
81 if (src == dst) {
82 return 0;
83 } else if (src < dst) {
84 return 1;
85 } else {
86 return 2;
90 static uint32_t cc_calc_tm_32(uint32_t val, uint32_t mask)
92 uint32_t r = val & mask;
94 if (r == 0) {
95 return 0;
96 } else if (r == mask) {
97 return 3;
98 } else {
99 return 1;
103 static uint32_t cc_calc_tm_64(uint64_t val, uint64_t mask)
105 uint64_t r = val & mask;
107 if (r == 0) {
108 return 0;
109 } else if (r == mask) {
110 return 3;
111 } else {
112 int top = clz64(mask);
113 if ((int64_t)(val << top) < 0) {
114 return 2;
115 } else {
116 return 1;
121 static uint32_t cc_calc_nz(uint64_t dst)
123 return !!dst;
126 static uint32_t cc_calc_addu(uint64_t carry_out, uint64_t result)
128 g_assert(carry_out <= 1);
129 return (result != 0) + 2 * carry_out;
132 static uint32_t cc_calc_subu(uint64_t borrow_out, uint64_t result)
134 return cc_calc_addu(borrow_out + 1, result);
137 static uint32_t cc_calc_add_64(int64_t a1, int64_t a2, int64_t ar)
139 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
140 return 3; /* overflow */
141 } else {
142 if (ar < 0) {
143 return 1;
144 } else if (ar > 0) {
145 return 2;
146 } else {
147 return 0;
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_subb_64(uint64_t a1, uint64_t a2, uint64_t ar)
169 int borrow_out;
171 if (ar != a1 - a2) { /* difference means borrow-in */
172 borrow_out = (a2 >= a1);
173 } else {
174 borrow_out = (a2 > a1);
177 return (ar != 0) + 2 * !borrow_out;
180 static uint32_t cc_calc_abs_64(int64_t dst)
182 if ((uint64_t)dst == 0x8000000000000000ULL) {
183 return 3;
184 } else if (dst) {
185 return 2;
186 } else {
187 return 0;
191 static uint32_t cc_calc_nabs_64(int64_t dst)
193 return !!dst;
196 static uint32_t cc_calc_comp_64(int64_t dst)
198 if ((uint64_t)dst == 0x8000000000000000ULL) {
199 return 3;
200 } else if (dst < 0) {
201 return 1;
202 } else if (dst > 0) {
203 return 2;
204 } else {
205 return 0;
210 static uint32_t cc_calc_add_32(int32_t a1, int32_t a2, int32_t ar)
212 if ((a1 > 0 && a2 > 0 && ar < 0) || (a1 < 0 && a2 < 0 && ar > 0)) {
213 return 3; /* overflow */
214 } else {
215 if (ar < 0) {
216 return 1;
217 } else if (ar > 0) {
218 return 2;
219 } else {
220 return 0;
225 static uint32_t cc_calc_sub_32(int32_t a1, int32_t a2, int32_t ar)
227 if ((a1 > 0 && a2 < 0 && ar < 0) || (a1 < 0 && a2 > 0 && ar > 0)) {
228 return 3; /* overflow */
229 } else {
230 if (ar < 0) {
231 return 1;
232 } else if (ar > 0) {
233 return 2;
234 } else {
235 return 0;
240 static uint32_t cc_calc_subb_32(uint32_t a1, uint32_t a2, uint32_t ar)
242 int borrow_out;
244 if (ar != a1 - a2) { /* difference means borrow-in */
245 borrow_out = (a2 >= a1);
246 } else {
247 borrow_out = (a2 > a1);
250 return (ar != 0) + 2 * !borrow_out;
253 static uint32_t cc_calc_abs_32(int32_t dst)
255 if ((uint32_t)dst == 0x80000000UL) {
256 return 3;
257 } else if (dst) {
258 return 2;
259 } else {
260 return 0;
264 static uint32_t cc_calc_nabs_32(int32_t dst)
266 return !!dst;
269 static uint32_t cc_calc_comp_32(int32_t dst)
271 if ((uint32_t)dst == 0x80000000UL) {
272 return 3;
273 } else if (dst < 0) {
274 return 1;
275 } else if (dst > 0) {
276 return 2;
277 } else {
278 return 0;
282 /* calculate condition code for insert character under mask insn */
283 static uint32_t cc_calc_icm(uint64_t mask, uint64_t val)
285 if ((val & mask) == 0) {
286 return 0;
287 } else {
288 int top = clz64(mask);
289 if ((int64_t)(val << top) < 0) {
290 return 1;
291 } else {
292 return 2;
297 static uint32_t cc_calc_sla_32(uint32_t src, int shift)
299 uint32_t mask = ((1U << shift) - 1U) << (32 - shift);
300 uint32_t sign = 1U << 31;
301 uint32_t match;
302 int32_t r;
304 /* Check if the sign bit stays the same. */
305 if (src & sign) {
306 match = mask;
307 } else {
308 match = 0;
310 if ((src & mask) != match) {
311 /* Overflow. */
312 return 3;
315 r = ((src << shift) & ~sign) | (src & sign);
316 if (r == 0) {
317 return 0;
318 } else if (r < 0) {
319 return 1;
321 return 2;
324 static uint32_t cc_calc_sla_64(uint64_t src, int shift)
326 uint64_t mask = ((1ULL << shift) - 1ULL) << (64 - shift);
327 uint64_t sign = 1ULL << 63;
328 uint64_t match;
329 int64_t r;
331 /* Check if the sign bit stays the same. */
332 if (src & sign) {
333 match = mask;
334 } else {
335 match = 0;
337 if ((src & mask) != match) {
338 /* Overflow. */
339 return 3;
342 r = ((src << shift) & ~sign) | (src & sign);
343 if (r == 0) {
344 return 0;
345 } else if (r < 0) {
346 return 1;
348 return 2;
351 static uint32_t cc_calc_flogr(uint64_t dst)
353 return dst ? 2 : 0;
356 static uint32_t cc_calc_lcbb(uint64_t dst)
358 return dst == 16 ? 0 : 3;
361 static uint32_t cc_calc_vc(uint64_t low, uint64_t high)
363 if (high == -1ull && low == -1ull) {
364 /* all elements match */
365 return 0;
366 } else if (high == 0 && low == 0) {
367 /* no elements match */
368 return 3;
369 } else {
370 /* some elements but not all match */
371 return 1;
375 static uint32_t cc_calc_muls_32(int64_t res)
377 const int64_t tmp = res >> 31;
379 if (!res) {
380 return 0;
381 } else if (tmp && tmp != -1) {
382 return 3;
383 } else if (res < 0) {
384 return 1;
386 return 2;
389 static uint64_t cc_calc_muls_64(int64_t res_high, uint64_t res_low)
391 if (!res_high && !res_low) {
392 return 0;
393 } else if (res_high + (res_low >> 63) != 0) {
394 return 3;
395 } else if (res_high < 0) {
396 return 1;
398 return 2;
401 static uint32_t do_calc_cc(CPUS390XState *env, uint32_t cc_op,
402 uint64_t src, uint64_t dst, uint64_t vr)
404 uint32_t r = 0;
406 switch (cc_op) {
407 case CC_OP_CONST0:
408 case CC_OP_CONST1:
409 case CC_OP_CONST2:
410 case CC_OP_CONST3:
411 /* cc_op value _is_ cc */
412 r = cc_op;
413 break;
414 case CC_OP_LTGT0_32:
415 r = cc_calc_ltgt0_32(dst);
416 break;
417 case CC_OP_LTGT0_64:
418 r = cc_calc_ltgt0_64(dst);
419 break;
420 case CC_OP_LTGT_32:
421 r = cc_calc_ltgt_32(src, dst);
422 break;
423 case CC_OP_LTGT_64:
424 r = cc_calc_ltgt_64(src, dst);
425 break;
426 case CC_OP_LTUGTU_32:
427 r = cc_calc_ltugtu_32(src, dst);
428 break;
429 case CC_OP_LTUGTU_64:
430 r = cc_calc_ltugtu_64(src, dst);
431 break;
432 case CC_OP_TM_32:
433 r = cc_calc_tm_32(src, dst);
434 break;
435 case CC_OP_TM_64:
436 r = cc_calc_tm_64(src, dst);
437 break;
438 case CC_OP_NZ:
439 r = cc_calc_nz(dst);
440 break;
441 case CC_OP_ADDU:
442 r = cc_calc_addu(src, dst);
443 break;
444 case CC_OP_SUBU:
445 r = cc_calc_subu(src, dst);
446 break;
447 case CC_OP_ADD_64:
448 r = cc_calc_add_64(src, dst, vr);
449 break;
450 case CC_OP_SUB_64:
451 r = cc_calc_sub_64(src, dst, vr);
452 break;
453 case CC_OP_SUBB_64:
454 r = cc_calc_subb_64(src, dst, vr);
455 break;
456 case CC_OP_ABS_64:
457 r = cc_calc_abs_64(dst);
458 break;
459 case CC_OP_NABS_64:
460 r = cc_calc_nabs_64(dst);
461 break;
462 case CC_OP_COMP_64:
463 r = cc_calc_comp_64(dst);
464 break;
465 case CC_OP_MULS_64:
466 r = cc_calc_muls_64(src, dst);
467 break;
469 case CC_OP_ADD_32:
470 r = cc_calc_add_32(src, dst, vr);
471 break;
472 case CC_OP_SUB_32:
473 r = cc_calc_sub_32(src, dst, vr);
474 break;
475 case CC_OP_SUBB_32:
476 r = cc_calc_subb_32(src, dst, vr);
477 break;
478 case CC_OP_ABS_32:
479 r = cc_calc_abs_32(dst);
480 break;
481 case CC_OP_NABS_32:
482 r = cc_calc_nabs_32(dst);
483 break;
484 case CC_OP_COMP_32:
485 r = cc_calc_comp_32(dst);
486 break;
487 case CC_OP_MULS_32:
488 r = cc_calc_muls_32(dst);
489 break;
491 case CC_OP_ICM:
492 r = cc_calc_icm(src, dst);
493 break;
494 case CC_OP_SLA_32:
495 r = cc_calc_sla_32(src, dst);
496 break;
497 case CC_OP_SLA_64:
498 r = cc_calc_sla_64(src, dst);
499 break;
500 case CC_OP_FLOGR:
501 r = cc_calc_flogr(dst);
502 break;
503 case CC_OP_LCBB:
504 r = cc_calc_lcbb(dst);
505 break;
506 case CC_OP_VC:
507 r = cc_calc_vc(src, dst);
508 break;
510 case CC_OP_NZ_F32:
511 r = set_cc_nz_f32(dst);
512 break;
513 case CC_OP_NZ_F64:
514 r = set_cc_nz_f64(dst);
515 break;
516 case CC_OP_NZ_F128:
517 r = set_cc_nz_f128(make_float128(src, dst));
518 break;
520 default:
521 cpu_abort(env_cpu(env), "Unknown CC operation: %s\n", cc_name(cc_op));
524 HELPER_LOG("%s: %15s 0x%016lx 0x%016lx 0x%016lx = %d\n", __func__,
525 cc_name(cc_op), src, dst, vr, r);
526 return r;
529 uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
530 uint64_t vr)
532 return do_calc_cc(env, cc_op, src, dst, vr);
535 uint32_t HELPER(calc_cc)(CPUS390XState *env, uint32_t cc_op, uint64_t src,
536 uint64_t dst, uint64_t vr)
538 return do_calc_cc(env, cc_op, src, dst, vr);
541 #ifndef CONFIG_USER_ONLY
542 void HELPER(load_psw)(CPUS390XState *env, uint64_t mask, uint64_t addr)
544 load_psw(env, mask, addr);
545 cpu_loop_exit(env_cpu(env));
548 void HELPER(sacf)(CPUS390XState *env, uint64_t a1)
550 HELPER_LOG("%s: %16" PRIx64 "\n", __func__, a1);
552 switch (a1 & 0xf00) {
553 case 0x000:
554 env->psw.mask &= ~PSW_MASK_ASC;
555 env->psw.mask |= PSW_ASC_PRIMARY;
556 break;
557 case 0x100:
558 env->psw.mask &= ~PSW_MASK_ASC;
559 env->psw.mask |= PSW_ASC_SECONDARY;
560 break;
561 case 0x300:
562 env->psw.mask &= ~PSW_MASK_ASC;
563 env->psw.mask |= PSW_ASC_HOME;
564 break;
565 default:
566 HELPER_LOG("unknown sacf mode: %" PRIx64 "\n", a1);
567 tcg_s390_program_interrupt(env, PGM_SPECIFICATION, GETPC());
570 #endif