migration: fix migration_bitmap leak
[qemu/ar7.git] / target-s390x / int_helper.c
blobb683709860440bfe3735d66cc003347f9b8101a8
1 /*
2 * S/390 integer 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/>.
21 #include "cpu.h"
22 #include "qemu/host-utils.h"
23 #include "helper.h"
25 /* #define DEBUG_HELPER */
26 #ifdef DEBUG_HELPER
27 #define HELPER_LOG(x...) qemu_log(x)
28 #else
29 #define HELPER_LOG(x...)
30 #endif
32 /* 64/64 -> 128 unsigned multiplication */
33 void HELPER(mlg)(CPUS390XState *env, uint32_t r1, uint64_t v2)
35 #if HOST_LONG_BITS == 64 && defined(__GNUC__)
36 /* assuming 64-bit hosts have __uint128_t */
37 __uint128_t res = (__uint128_t)env->regs[r1 + 1];
39 res *= (__uint128_t)v2;
40 env->regs[r1] = (uint64_t)(res >> 64);
41 env->regs[r1 + 1] = (uint64_t)res;
42 #else
43 mulu64(&env->regs[r1 + 1], &env->regs[r1], env->regs[r1 + 1], v2);
44 #endif
47 /* 128 -> 64/64 unsigned division */
48 void HELPER(dlg)(CPUS390XState *env, uint32_t r1, uint64_t v2)
50 uint64_t divisor = v2;
52 if (!env->regs[r1]) {
53 /* 64 -> 64/64 case */
54 env->regs[r1] = env->regs[r1 + 1] % divisor;
55 env->regs[r1 + 1] = env->regs[r1 + 1] / divisor;
56 return;
57 } else {
58 #if HOST_LONG_BITS == 64 && defined(__GNUC__)
59 /* assuming 64-bit hosts have __uint128_t */
60 __uint128_t dividend = (((__uint128_t)env->regs[r1]) << 64) |
61 (env->regs[r1 + 1]);
62 __uint128_t quotient = dividend / divisor;
63 __uint128_t remainder = dividend % divisor;
65 env->regs[r1 + 1] = quotient;
66 env->regs[r1] = remainder;
67 #else
68 /* 32-bit hosts would need special wrapper functionality - just abort if
69 we encounter such a case; it's very unlikely anyways. */
70 cpu_abort(env, "128 -> 64/64 division not implemented\n");
71 #endif
75 /* absolute value 32-bit */
76 uint32_t HELPER(abs_i32)(int32_t val)
78 if (val < 0) {
79 return -val;
80 } else {
81 return val;
85 /* negative absolute value 32-bit */
86 int32_t HELPER(nabs_i32)(int32_t val)
88 if (val < 0) {
89 return val;
90 } else {
91 return -val;
95 /* absolute value 64-bit */
96 uint64_t HELPER(abs_i64)(int64_t val)
98 HELPER_LOG("%s: val 0x%" PRIx64 "\n", __func__, val);
100 if (val < 0) {
101 return -val;
102 } else {
103 return val;
107 /* negative absolute value 64-bit */
108 int64_t HELPER(nabs_i64)(int64_t val)
110 if (val < 0) {
111 return val;
112 } else {
113 return -val;
117 /* add with carry 32-bit unsigned */
118 uint32_t HELPER(addc_u32)(uint32_t cc, uint32_t v1, uint32_t v2)
120 uint32_t res;
122 res = v1 + v2;
123 if (cc & 2) {
124 res++;
127 return res;
130 /* subtract unsigned v2 from v1 with borrow */
131 uint32_t HELPER(slb)(CPUS390XState *env, uint32_t cc, uint32_t r1, uint32_t v2)
133 uint32_t v1 = env->regs[r1];
134 uint32_t res = v1 + (~v2) + (cc >> 1);
136 env->regs[r1] = (env->regs[r1] & 0xffffffff00000000ULL) | res;
137 if (cc & 2) {
138 /* borrow */
139 return v1 ? 1 : 0;
140 } else {
141 return v1 ? 3 : 2;
145 /* subtract unsigned v2 from v1 with borrow */
146 uint32_t HELPER(slbg)(CPUS390XState *env, uint32_t cc, uint32_t r1,
147 uint64_t v1, uint64_t v2)
149 uint64_t res = v1 + (~v2) + (cc >> 1);
151 env->regs[r1] = res;
152 if (cc & 2) {
153 /* borrow */
154 return v1 ? 1 : 0;
155 } else {
156 return v1 ? 3 : 2;
160 /* find leftmost one */
161 uint32_t HELPER(flogr)(CPUS390XState *env, uint32_t r1, uint64_t v2)
163 uint64_t res = 0;
164 uint64_t ov2 = v2;
166 while (!(v2 & 0x8000000000000000ULL) && v2) {
167 v2 <<= 1;
168 res++;
171 if (!v2) {
172 env->regs[r1] = 64;
173 env->regs[r1 + 1] = 0;
174 return 0;
175 } else {
176 env->regs[r1] = res;
177 env->regs[r1 + 1] = ov2 & ~(0x8000000000000000ULL >> res);
178 return 2;
182 uint64_t HELPER(cvd)(int32_t bin)
184 /* positive 0 */
185 uint64_t dec = 0x0c;
186 int shift = 4;
188 if (bin < 0) {
189 bin = -bin;
190 dec = 0x0d;
193 for (shift = 4; (shift < 64) && bin; shift += 4) {
194 int current_number = bin % 10;
196 dec |= (current_number) << shift;
197 bin /= 10;
200 return dec;