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