]> git.proxmox.com Git - qemu.git/blame - target-s390x/int_helper.c
target-s390: Implement POPCNT
[qemu.git] / target-s390x / int_helper.c
CommitLineData
fc8d72c2
BS
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"
1de7afc9 22#include "qemu/host-utils.h"
fc8d72c2
BS
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 */
1ac5889f 33uint64_t HELPER(mul128)(CPUS390XState *env, uint64_t v1, uint64_t v2)
fc8d72c2 34{
1ac5889f
RH
35 uint64_t reth;
36 mulu64(&env->retxl, &reth, v1, v2);
37 return reth;
fc8d72c2
BS
38}
39
891452e5 40/* 64/32 -> 32 signed division */
b4e2bd35 41int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
891452e5 42{
b4e2bd35
RH
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;
891452e5
RH
59}
60
61/* 64/32 -> 32 unsigned division */
b4e2bd35 62uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
fc8d72c2 63{
b4e2bd35
RH
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;
891452e5 80}
fc8d72c2 81
891452e5
RH
82/* 64/64 -> 64 signed division */
83int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
84{
b4e2bd35
RH
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 }
891452e5
RH
89 env->retxl = a % b;
90 return a / b;
91}
92
93/* 128 -> 64/64 unsigned division */
94uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
95 uint64_t b)
96{
97 uint64_t ret;
b4e2bd35
RH
98 /* Signal divide by zero. */
99 if (b == 0) {
100 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
101 }
891452e5 102 if (ah == 0) {
fc8d72c2 103 /* 64 -> 64/64 case */
891452e5
RH
104 env->retxl = al % b;
105 ret = al / b;
fc8d72c2 106 } else {
891452e5 107 /* ??? Move i386 idivq helper to host-utils. */
fc8d72c2
BS
108#if HOST_LONG_BITS == 64 && defined(__GNUC__)
109 /* assuming 64-bit hosts have __uint128_t */
891452e5
RH
110 __uint128_t a = ((__uint128_t)ah << 64) | al;
111 __uint128_t q = a / b;
112 env->retxl = a % b;
113 ret = q;
b4e2bd35
RH
114 if (ret != q) {
115 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
116 }
fc8d72c2
BS
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 }
891452e5 123 return ret;
fc8d72c2
BS
124}
125
126/* absolute value 32-bit */
127uint32_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 */
137int32_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 */
147uint64_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 */
159int64_t HELPER(nabs_i64)(int64_t val)
160{
161 if (val < 0) {
162 return val;
163 } else {
164 return -val;
165 }
166}
167
102bf2c6
RH
168/* count leading zeros, for find leftmost one */
169uint64_t HELPER(clz)(uint64_t v)
fc8d72c2 170{
102bf2c6 171 return clz64(v);
fc8d72c2
BS
172}
173
174uint64_t HELPER(cvd)(int32_t bin)
175{
176 /* positive 0 */
177 uint64_t dec = 0x0c;
178 int shift = 4;
179
180 if (bin < 0) {
181 bin = -bin;
182 dec = 0x0d;
183 }
184
185 for (shift = 4; (shift < 64) && bin; shift += 4) {
186 int current_number = bin % 10;
187
188 dec |= (current_number) << shift;
189 bin /= 10;
190 }
191
192 return dec;
193}
99b4f24b
RH
194
195uint64_t HELPER(popcnt)(uint64_t r2)
196{
197 uint64_t ret = 0;
198 int i;
199
200 for (i = 0; i < 64; i += 8) {
201 uint64_t t = ctpop32((r2 >> i) & 0xff);
202 ret |= t << i;
203 }
204 return ret;
205}