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 "qemu/osdep.h"
24 #include "exec/exec-all.h"
25 #include "qemu/host-utils.h"
26 #include "exec/helper-proto.h"
28 /* #define DEBUG_HELPER */
30 #define HELPER_LOG(x...) qemu_log(x)
32 #define HELPER_LOG(x...)
35 /* 64/32 -> 32 signed division */
36 int64_t HELPER(divs32
)(CPUS390XState
*env
, int64_t a
, int64_t b64
)
42 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
48 /* Catch non-representable quotient. */
50 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
56 /* 64/32 -> 32 unsigned division */
57 uint64_t HELPER(divu32
)(CPUS390XState
*env
, uint64_t a
, uint64_t b64
)
59 uint32_t ret
, b
= b64
;
63 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
69 /* Catch non-representable quotient. */
71 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
77 /* 64/64 -> 64 signed division */
78 int64_t HELPER(divs64
)(CPUS390XState
*env
, int64_t a
, int64_t b
)
80 /* Catch divide by zero, and non-representable quotient (MIN / -1). */
81 if (b
== 0 || (b
== -1 && a
== (1ll << 63))) {
82 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
88 /* 128 -> 64/64 unsigned division */
89 uint64_t HELPER(divu64
)(CPUS390XState
*env
, uint64_t ah
, uint64_t al
,
93 /* Signal divide by zero. */
95 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
98 /* 64 -> 64/64 case */
102 /* ??? Move i386 idivq helper to host-utils. */
104 __uint128_t a
= ((__uint128_t
)ah
<< 64) | al
;
105 __uint128_t q
= a
/ b
;
109 runtime_exception(env
, PGM_FIXPT_DIVIDE
, GETPC());
112 S390CPU
*cpu
= s390_env_get_cpu(env
);
113 /* 32-bit hosts would need special wrapper functionality - just abort if
114 we encounter such a case; it's very unlikely anyways. */
115 cpu_abort(CPU(cpu
), "128 -> 64/64 division not implemented\n");
121 uint64_t HELPER(cvd
)(int32_t reg
)
133 for (shift
= 4; (shift
< 64) && bin
; shift
+= 4) {
134 dec
|= (bin
% 10) << shift
;
141 uint64_t HELPER(popcnt
)(uint64_t val
)
143 /* Note that we don't fold past bytes. */
144 val
= (val
& 0x5555555555555555ULL
) + ((val
>> 1) & 0x5555555555555555ULL
);
145 val
= (val
& 0x3333333333333333ULL
) + ((val
>> 2) & 0x3333333333333333ULL
);
146 val
= (val
+ (val
>> 4)) & 0x0f0f0f0f0f0f0f0fULL
;