blockdev: Add blockdev-open-tray
[qemu.git] / target-s390x / int_helper.c
bloba46c736d67bc66388225c60a3059850955d014a7
1 /*
2 * S/390 integer helper routines
4 * Copyright (c) 2009 Ulrich Hecht
5 * Copyright (c) 2009 Alexander Graf
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.
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.
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/>.
21 #include "cpu.h"
22 #include "qemu/host-utils.h"
23 #include "exec/helper-proto.h"
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
32 /* 64/32 -> 32 signed division */
33 int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
35 int32_t ret, b = b64;
36 int64_t q;
38 if (b == 0) {
39 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
42 ret = q = a / b;
43 env->retxl = a % b;
45 /* Catch non-representable quotient. */
46 if (ret != q) {
47 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
50 return ret;
53 /* 64/32 -> 32 unsigned division */
54 uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
56 uint32_t ret, b = b64;
57 uint64_t q;
59 if (b == 0) {
60 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
63 ret = q = a / b;
64 env->retxl = a % b;
66 /* Catch non-representable quotient. */
67 if (ret != q) {
68 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
71 return ret;
74 /* 64/64 -> 64 signed division */
75 int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
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());
81 env->retxl = a % b;
82 return a / b;
85 /* 128 -> 64/64 unsigned division */
86 uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
87 uint64_t b)
89 uint64_t ret;
90 /* Signal divide by zero. */
91 if (b == 0) {
92 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
94 if (ah == 0) {
95 /* 64 -> 64/64 case */
96 env->retxl = al % b;
97 ret = al / b;
98 } else {
99 /* ??? Move i386 idivq helper to host-utils. */
100 #ifdef CONFIG_INT128
101 __uint128_t a = ((__uint128_t)ah << 64) | al;
102 __uint128_t q = a / b;
103 env->retxl = a % b;
104 ret = q;
105 if (ret != q) {
106 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
108 #else
109 S390CPU *cpu = s390_env_get_cpu(env);
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(CPU(cpu), "128 -> 64/64 division not implemented\n");
113 #endif
115 return ret;
118 /* count leading zeros, for find leftmost one */
119 uint64_t HELPER(clz)(uint64_t v)
121 return clz64(v);
124 uint64_t HELPER(cvd)(int32_t reg)
126 /* positive 0 */
127 uint64_t dec = 0x0c;
128 int64_t bin = reg;
129 int shift;
131 if (bin < 0) {
132 bin = -bin;
133 dec = 0x0d;
136 for (shift = 4; (shift < 64) && bin; shift += 4) {
137 dec |= (bin % 10) << shift;
138 bin /= 10;
141 return dec;
144 uint64_t HELPER(popcnt)(uint64_t r2)
146 uint64_t ret = 0;
147 int i;
149 for (i = 0; i < 64; i += 8) {
150 uint64_t t = ctpop32((r2 >> i) & 0xff);
151 ret |= t << i;
153 return ret;