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