target-s390: Send signals for divide
[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 /* 64/32 -> 32 signed division */
41 int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
42 {
43     int32_t ret, b = b64;
44     int64_t q;
45
46     if (b == 0) {
47         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
48     }
49
50     ret = q = a / b;
51     env->retxl = a % b;
52
53     /* Catch non-representable quotient.  */
54     if (ret != q) {
55         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
56     }
57
58     return ret;
59 }
60
61 /* 64/32 -> 32 unsigned division */
62 uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
63 {
64     uint32_t ret, b = b64;
65     uint64_t q;
66
67     if (b == 0) {
68         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
69     }
70
71     ret = q = a / b;
72     env->retxl = a % b;
73
74     /* Catch non-representable quotient.  */
75     if (ret != q) {
76         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
77     }
78
79     return ret;
80 }
81
82 /* 64/64 -> 64 signed division */
83 int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
84 {
85     /* Catch divide by zero, and non-representable quotient (MIN / -1).  */
86     if (b == 0 || (b == -1 && a == (1ll << 63))) {
87         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
88     }
89     env->retxl = a % b;
90     return a / b;
91 }
92
93 /* 128 -> 64/64 unsigned division */
94 uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
95                         uint64_t b)
96 {
97     uint64_t ret;
98     /* Signal divide by zero.  */
99     if (b == 0) {
100         runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
101     }
102     if (ah == 0) {
103         /* 64 -> 64/64 case */
104         env->retxl = al % b;
105         ret = al / b;
106     } else {
107         /* ??? Move i386 idivq helper to host-utils.  */
108 #if HOST_LONG_BITS == 64 && defined(__GNUC__)
109         /* assuming 64-bit hosts have __uint128_t */
110         __uint128_t a = ((__uint128_t)ah << 64) | al;
111         __uint128_t q = a / b;
112         env->retxl = a % b;
113         ret = q;
114         if (ret != q) {
115             runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
116         }
117 #else
118         /* 32-bit hosts would need special wrapper functionality - just abort if
119            we encounter such a case; it's very unlikely anyways. */
120         cpu_abort(env, "128 -> 64/64 division not implemented\n");
121 #endif
122     }
123     return ret;
124 }
125
126 /* absolute value 32-bit */
127 uint32_t HELPER(abs_i32)(int32_t val)
128 {
129     if (val < 0) {
130         return -val;
131     } else {
132         return val;
133     }
134 }
135
136 /* negative absolute value 32-bit */
137 int32_t HELPER(nabs_i32)(int32_t val)
138 {
139     if (val < 0) {
140         return val;
141     } else {
142         return -val;
143     }
144 }
145
146 /* absolute value 64-bit */
147 uint64_t HELPER(abs_i64)(int64_t val)
148 {
149     HELPER_LOG("%s: val 0x%" PRIx64 "\n", __func__, val);
150
151     if (val < 0) {
152         return -val;
153     } else {
154         return val;
155     }
156 }
157
158 /* negative absolute value 64-bit */
159 int64_t HELPER(nabs_i64)(int64_t val)
160 {
161     if (val < 0) {
162         return val;
163     } else {
164         return -val;
165     }
166 }
167
168 /* find leftmost one */
169 uint32_t HELPER(flogr)(CPUS390XState *env, uint32_t r1, uint64_t v2)
170 {
171     uint64_t res = 0;
172     uint64_t ov2 = v2;
173
174     while (!(v2 & 0x8000000000000000ULL) && v2) {
175         v2 <<= 1;
176         res++;
177     }
178
179     if (!v2) {
180         env->regs[r1] = 64;
181         env->regs[r1 + 1] = 0;
182         return 0;
183     } else {
184         env->regs[r1] = res;
185         env->regs[r1 + 1] = ov2 & ~(0x8000000000000000ULL >> res);
186         return 2;
187     }
188 }
189
190 uint64_t HELPER(cvd)(int32_t bin)
191 {
192     /* positive 0 */
193     uint64_t dec = 0x0c;
194     int shift = 4;
195
196     if (bin < 0) {
197         bin = -bin;
198         dec = 0x0d;
199     }
200
201     for (shift = 4; (shift < 64) && bin; shift += 4) {
202         int current_number = bin % 10;
203
204         dec |= (current_number) << shift;
205         bin /= 10;
206     }
207
208     return dec;
209 }