target-s390: Convert 64-bit MULTIPLY LOGICAL
[sdk/emulator/qemu.git] / target-s390x / int_helper.c
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 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 "cpu.h"
22 #include "qemu/host-utils.h"
23 #include "helper.h"
24
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
31
32 /* 64/64 -> 128 unsigned multiplication */
33 uint64_t HELPER(mul128)(CPUS390XState *env, uint64_t v1, uint64_t v2)
34 {
35     uint64_t reth;
36     mulu64(&env->retxl, &reth, v1, v2);
37     return reth;
38 }
39
40 /* 128 -> 64/64 unsigned division */
41 void HELPER(dlg)(CPUS390XState *env, uint32_t r1, uint64_t v2)
42 {
43     uint64_t divisor = v2;
44
45     if (!env->regs[r1]) {
46         /* 64 -> 64/64 case */
47         env->regs[r1] = env->regs[r1 + 1] % divisor;
48         env->regs[r1 + 1] = env->regs[r1 + 1] / divisor;
49         return;
50     } else {
51 #if HOST_LONG_BITS == 64 && defined(__GNUC__)
52         /* assuming 64-bit hosts have __uint128_t */
53         __uint128_t dividend = (((__uint128_t)env->regs[r1]) << 64) |
54             (env->regs[r1 + 1]);
55         __uint128_t quotient = dividend / divisor;
56         __uint128_t remainder = dividend % divisor;
57
58         env->regs[r1 + 1] = quotient;
59         env->regs[r1] = remainder;
60 #else
61         /* 32-bit hosts would need special wrapper functionality - just abort if
62            we encounter such a case; it's very unlikely anyways. */
63         cpu_abort(env, "128 -> 64/64 division not implemented\n");
64 #endif
65     }
66 }
67
68 /* absolute value 32-bit */
69 uint32_t HELPER(abs_i32)(int32_t val)
70 {
71     if (val < 0) {
72         return -val;
73     } else {
74         return val;
75     }
76 }
77
78 /* negative absolute value 32-bit */
79 int32_t HELPER(nabs_i32)(int32_t val)
80 {
81     if (val < 0) {
82         return val;
83     } else {
84         return -val;
85     }
86 }
87
88 /* absolute value 64-bit */
89 uint64_t HELPER(abs_i64)(int64_t val)
90 {
91     HELPER_LOG("%s: val 0x%" PRIx64 "\n", __func__, val);
92
93     if (val < 0) {
94         return -val;
95     } else {
96         return val;
97     }
98 }
99
100 /* negative absolute value 64-bit */
101 int64_t HELPER(nabs_i64)(int64_t val)
102 {
103     if (val < 0) {
104         return val;
105     } else {
106         return -val;
107     }
108 }
109
110 /* add with carry 32-bit unsigned */
111 uint32_t HELPER(addc_u32)(uint32_t cc, uint32_t v1, uint32_t v2)
112 {
113     uint32_t res;
114
115     res = v1 + v2;
116     if (cc & 2) {
117         res++;
118     }
119
120     return res;
121 }
122
123 /* subtract unsigned v2 from v1 with borrow */
124 uint32_t HELPER(slb)(CPUS390XState *env, uint32_t cc, uint32_t r1, uint32_t v2)
125 {
126     uint32_t v1 = env->regs[r1];
127     uint32_t res = v1 + (~v2) + (cc >> 1);
128
129     env->regs[r1] = (env->regs[r1] & 0xffffffff00000000ULL) | res;
130     if (cc & 2) {
131         /* borrow */
132         return v1 ? 1 : 0;
133     } else {
134         return v1 ? 3 : 2;
135     }
136 }
137
138 /* subtract unsigned v2 from v1 with borrow */
139 uint32_t HELPER(slbg)(CPUS390XState *env, uint32_t cc, uint32_t r1,
140                       uint64_t v1, uint64_t v2)
141 {
142     uint64_t res = v1 + (~v2) + (cc >> 1);
143
144     env->regs[r1] = res;
145     if (cc & 2) {
146         /* borrow */
147         return v1 ? 1 : 0;
148     } else {
149         return v1 ? 3 : 2;
150     }
151 }
152
153 /* find leftmost one */
154 uint32_t HELPER(flogr)(CPUS390XState *env, uint32_t r1, uint64_t v2)
155 {
156     uint64_t res = 0;
157     uint64_t ov2 = v2;
158
159     while (!(v2 & 0x8000000000000000ULL) && v2) {
160         v2 <<= 1;
161         res++;
162     }
163
164     if (!v2) {
165         env->regs[r1] = 64;
166         env->regs[r1 + 1] = 0;
167         return 0;
168     } else {
169         env->regs[r1] = res;
170         env->regs[r1 + 1] = ov2 & ~(0x8000000000000000ULL >> res);
171         return 2;
172     }
173 }
174
175 uint64_t HELPER(cvd)(int32_t bin)
176 {
177     /* positive 0 */
178     uint64_t dec = 0x0c;
179     int shift = 4;
180
181     if (bin < 0) {
182         bin = -bin;
183         dec = 0x0d;
184     }
185
186     for (shift = 4; (shift < 64) && bin; shift += 4) {
187         int current_number = bin % 10;
188
189         dec |= (current_number) << shift;
190         bin /= 10;
191     }
192
193     return dec;
194 }