blob: f26f36a9045e24aa1d934d6220f23e8cdb7ea90d [file] [log] [blame]
Blue Swirlfc8d72c2012-09-02 07:33:33 +00001/*
2 * S/390 integer helper routines
3 *
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
6 *
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.
11 *
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.
16 *
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/>.
19 */
20
Peter Maydell96154952016-01-26 18:17:00 +000021#include "qemu/osdep.h"
Blue Swirlfc8d72c2012-09-02 07:33:33 +000022#include "cpu.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010023#include "exec/exec-all.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010024#include "qemu/host-utils.h"
Richard Henderson2ef61752014-04-07 22:31:41 -070025#include "exec/helper-proto.h"
Blue Swirlfc8d72c2012-09-02 07:33:33 +000026
27/* #define DEBUG_HELPER */
28#ifdef DEBUG_HELPER
29#define HELPER_LOG(x...) qemu_log(x)
30#else
31#define HELPER_LOG(x...)
32#endif
33
Richard Henderson891452e2012-08-20 14:02:02 -070034/* 64/32 -> 32 signed division */
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070035int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
Blue Swirlfc8d72c2012-09-02 07:33:33 +000036{
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070037 int32_t ret, b = b64;
38 int64_t q;
39
40 if (b == 0) {
41 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
42 }
43
44 ret = q = a / b;
45 env->retxl = a % b;
46
47 /* Catch non-representable quotient. */
48 if (ret != q) {
49 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
50 }
51
52 return ret;
Richard Henderson891452e2012-08-20 14:02:02 -070053}
Blue Swirlfc8d72c2012-09-02 07:33:33 +000054
Richard Henderson891452e2012-08-20 14:02:02 -070055/* 64/32 -> 32 unsigned division */
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070056uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
Richard Henderson891452e2012-08-20 14:02:02 -070057{
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070058 uint32_t ret, b = b64;
59 uint64_t q;
60
61 if (b == 0) {
62 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
63 }
64
65 ret = q = a / b;
66 env->retxl = a % b;
67
68 /* Catch non-representable quotient. */
69 if (ret != q) {
70 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
71 }
72
73 return ret;
Richard Henderson891452e2012-08-20 14:02:02 -070074}
75
76/* 64/64 -> 64 signed division */
77int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
78{
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070079 /* Catch divide by zero, and non-representable quotient (MIN / -1). */
80 if (b == 0 || (b == -1 && a == (1ll << 63))) {
81 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
82 }
Richard Henderson891452e2012-08-20 14:02:02 -070083 env->retxl = a % b;
84 return a / b;
85}
86
87/* 128 -> 64/64 unsigned division */
88uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
89 uint64_t b)
90{
91 uint64_t ret;
Richard Hendersonb4e2bd32012-09-05 17:27:40 -070092 /* Signal divide by zero. */
93 if (b == 0) {
94 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
95 }
Richard Henderson891452e2012-08-20 14:02:02 -070096 if (ah == 0) {
Blue Swirlfc8d72c2012-09-02 07:33:33 +000097 /* 64 -> 64/64 case */
Richard Henderson891452e2012-08-20 14:02:02 -070098 env->retxl = al % b;
99 ret = al / b;
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000100 } else {
Richard Henderson891452e2012-08-20 14:02:02 -0700101 /* ??? Move i386 idivq helper to host-utils. */
Gabriel Kerneisd49b8e02013-04-23 18:15:12 +0100102#ifdef CONFIG_INT128
Richard Henderson891452e2012-08-20 14:02:02 -0700103 __uint128_t a = ((__uint128_t)ah << 64) | al;
104 __uint128_t q = a / b;
105 env->retxl = a % b;
106 ret = q;
Richard Hendersonb4e2bd32012-09-05 17:27:40 -0700107 if (ret != q) {
108 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
109 }
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000110#else
Andreas Färbera47dddd2013-09-03 17:38:47 +0200111 S390CPU *cpu = s390_env_get_cpu(env);
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000112 /* 32-bit hosts would need special wrapper functionality - just abort if
113 we encounter such a case; it's very unlikely anyways. */
Andreas Färbera47dddd2013-09-03 17:38:47 +0200114 cpu_abort(CPU(cpu), "128 -> 64/64 division not implemented\n");
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000115#endif
116 }
Richard Henderson891452e2012-08-20 14:02:02 -0700117 return ret;
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000118}
119
Aurelien Jarno92f2b4e2015-06-25 21:16:58 +0200120uint64_t HELPER(cvd)(int32_t reg)
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000121{
122 /* positive 0 */
123 uint64_t dec = 0x0c;
Aurelien Jarno92f2b4e2015-06-25 21:16:58 +0200124 int64_t bin = reg;
125 int shift;
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000126
127 if (bin < 0) {
128 bin = -bin;
129 dec = 0x0d;
130 }
131
132 for (shift = 4; (shift < 64) && bin; shift += 4) {
Aurelien Jarno92f2b4e2015-06-25 21:16:58 +0200133 dec |= (bin % 10) << shift;
Blue Swirlfc8d72c2012-09-02 07:33:33 +0000134 bin /= 10;
135 }
136
137 return dec;
138}
Richard Henderson99b4f242012-09-01 11:14:04 -0700139
Richard Henderson250a87d2016-11-21 12:06:26 +0100140uint64_t HELPER(popcnt)(uint64_t val)
Richard Henderson99b4f242012-09-01 11:14:04 -0700141{
Richard Henderson250a87d2016-11-21 12:06:26 +0100142 /* Note that we don't fold past bytes. */
143 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
144 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
145 val = (val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
146 return val;
Richard Henderson99b4f242012-09-01 11:14:04 -0700147}