1 /*
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.1 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
21 #include "qemu/osdep.h"
22 #include "cpu.h"
23 #include "s390x-internal.h"
24 #include "tcg_s390x.h"
25 #include "qemu/host-utils.h"
26 #include "exec/helper-proto.h"
27 #include "accel/tcg/cpu-ldst.h"
28
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
35
36 /* 64/32 -> 32 signed division */
HELPER(divs32)37 uint64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
38 {
39 int32_t b = b64;
40 int64_t q, r;
41
42 if (b == 0) {
43 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
44 }
45
46 q = a / b;
47 r = a % b;
48
49 /* Catch non-representable quotient. */
50 if (q != (int32_t)q) {
51 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
52 }
53
54 return deposit64(q, 32, 32, r);
55 }
56
57 /* 64/32 -> 32 unsigned division */
HELPER(divu32)58 uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
59 {
60 uint32_t b = b64;
61 uint64_t q, r;
62
63 if (b == 0) {
64 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
65 }
66
67 q = a / b;
68 r = a % b;
69
70 /* Catch non-representable quotient. */
71 if (q != (uint32_t)q) {
72 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
73 }
74
75 return deposit64(q, 32, 32, r);
76 }
77
78 /* 64/64 -> 64 signed division */
HELPER(divs64)79 Int128 HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
80 {
81 /* Catch divide by zero, and non-representable quotient (MIN / -1). */
82 if (b == 0 || (b == -1 && a == (1ll << 63))) {
83 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
84 }
85 return int128_make128(a / b, a % b);
86 }
87
88 /* 128 -> 64/64 unsigned division */
HELPER(divu64)89 Int128 HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al, uint64_t b)
90 {
91 if (b != 0) {
92 uint64_t r = divu128(&al, &ah, b);
93 if (ah == 0) {
94 return int128_make128(al, r);
95 }
96 }
97 /* divide by zero or overflow */
98 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
99 }
100
HELPER(cvb)101 void HELPER(cvb)(CPUS390XState *env, uint32_t r1, uint64_t dec)
102 {
103 int64_t pow10 = 1, bin = 0;
104 int digit, sign;
105
106 sign = dec & 0xf;
107 if (sign < 0xa) {
108 tcg_s390_data_exception(env, 0, GETPC());
109 }
110 dec >>= 4;
111
112 while (dec) {
113 digit = dec & 0xf;
114 if (digit > 0x9) {
115 tcg_s390_data_exception(env, 0, GETPC());
116 }
117 dec >>= 4;
118 bin += digit * pow10;
119 pow10 *= 10;
120 }
121
122 if (sign == 0xb || sign == 0xd) {
123 bin = -bin;
124 }
125
126 /* R1 is updated even on fixed-point-divide exception. */
127 env->regs[r1] = (env->regs[r1] & 0xffffffff00000000ULL) | (uint32_t)bin;
128 if (bin != (int32_t)bin) {
129 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
130 }
131 }
132
HELPER(cvbg)133 uint64_t HELPER(cvbg)(CPUS390XState *env, Int128 dec)
134 {
135 uint64_t dec64[] = {int128_getlo(dec), int128_gethi(dec)};
136 int64_t bin = 0, pow10, tmp;
137 int digit, i, sign;
138
139 sign = dec64[0] & 0xf;
140 if (sign < 0xa) {
141 tcg_s390_data_exception(env, 0, GETPC());
142 }
143 dec64[0] >>= 4;
144 pow10 = (sign == 0xb || sign == 0xd) ? -1 : 1;
145
146 for (i = 1; i < 20; i++) {
147 digit = dec64[i >> 4] & 0xf;
148 if (digit > 0x9) {
149 tcg_s390_data_exception(env, 0, GETPC());
150 }
151 dec64[i >> 4] >>= 4;
152 /*
153 * Prepend the next digit and check for overflow. The multiplication
154 * cannot overflow, since, conveniently, the int64_t limits are
155 * approximately +-9.2E+18. If bin is zero, the addition cannot
156 * overflow. Otherwise bin is known to have the same sign as the rhs
157 * addend, in which case overflow happens if and only if the result
158 * has a different sign.
159 */
160 tmp = bin + pow10 * digit;
161 if (bin && ((tmp ^ bin) < 0)) {
162 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
163 }
164 bin = tmp;
165 pow10 *= 10;
166 }
167
168 g_assert(!dec64[0]);
169 if (dec64[1]) {
170 tcg_s390_program_interrupt(env, PGM_FIXPT_DIVIDE, GETPC());
171 }
172
173 return bin;
174 }
175
HELPER(cvd)176 uint64_t HELPER(cvd)(int32_t reg)
177 {
178 /* positive 0 */
179 uint64_t dec = 0x0c;
180 int64_t bin = reg;
181 int shift;
182
183 if (bin < 0) {
184 bin = -bin;
185 dec = 0x0d;
186 }
187
188 for (shift = 4; (shift < 64) && bin; shift += 4) {
189 dec |= (bin % 10) << shift;
190 bin /= 10;
191 }
192
193 return dec;
194 }
195
HELPER(cvdg)196 Int128 HELPER(cvdg)(int64_t reg)
197 {
198 /* positive 0 */
199 Int128 dec = int128_make64(0x0c);
200 Int128 bin = int128_makes64(reg);
201 Int128 base = int128_make64(10);
202 int shift;
203
204 if (!int128_nonneg(bin)) {
205 bin = int128_neg(bin);
206 dec = int128_make64(0x0d);
207 }
208
209 for (shift = 4; (shift < 128) && int128_nz(bin); shift += 4) {
210 dec = int128_or(dec, int128_lshift(int128_remu(bin, base), shift));
211 bin = int128_divu(bin, base);
212 }
213
214 return dec;
215 }
216
HELPER(popcnt)217 uint64_t HELPER(popcnt)(uint64_t val)
218 {
219 /* Note that we don't fold past bytes. */
220 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
221 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
222 val = (val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
223 return val;
224 }
225