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