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/>.
22 #include "qemu/host-utils.h"
25 /* #define DEBUG_HELPER */
27 #define HELPER_LOG(x...) qemu_log(x)
29 #define HELPER_LOG(x...)
32 /* 64/64 -> 128 unsigned multiplication */
33 uint64_t HELPER(mul128)(CPUS390XState *env, uint64_t v1, uint64_t v2)
36 mulu64(&env->retxl, &reth, v1, v2);
40 /* 64/32 -> 32 signed division */
41 int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
47 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
53 /* Catch non-representable quotient. */
55 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
61 /* 64/32 -> 32 unsigned division */
62 uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
64 uint32_t ret, b = b64;
68 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
74 /* Catch non-representable quotient. */
76 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
82 /* 64/64 -> 64 signed division */
83 int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
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());
93 /* 128 -> 64/64 unsigned division */
94 uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
98 /* Signal divide by zero. */
100 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
103 /* 64 -> 64/64 case */
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;
115 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
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");
126 /* absolute value 32-bit */
127 uint32_t HELPER(abs_i32)(int32_t val)
136 /* negative absolute value 32-bit */
137 int32_t HELPER(nabs_i32)(int32_t val)
146 /* absolute value 64-bit */
147 uint64_t HELPER(abs_i64)(int64_t val)
149 HELPER_LOG("%s: val 0x%" PRIx64 "\n", __func__, val);
158 /* negative absolute value 64-bit */
159 int64_t HELPER(nabs_i64)(int64_t val)
168 /* count leading zeros, for find leftmost one */
169 uint64_t HELPER(clz)(uint64_t v)
174 uint64_t HELPER(cvd)(int32_t bin)
185 for (shift = 4; (shift < 64) && bin; shift += 4) {
186 int current_number = bin % 10;
188 dec |= (current_number) << shift;
195 uint64_t HELPER(popcnt)(uint64_t r2)
200 for (i = 0; i < 64; i += 8) {
201 uint64_t t = ctpop32((r2 >> i) & 0xff);