]> git.proxmox.com Git - mirror_qemu.git/blame - target-s390x/int_helper.c
smbios: Move table build tools into an include file.
[mirror_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
9615495a 21#include "qemu/osdep.h"
fc8d72c2 22#include "cpu.h"
63c91552 23#include "exec/exec-all.h"
1de7afc9 24#include "qemu/host-utils.h"
2ef6175a 25#include "exec/helper-proto.h"
fc8d72c2
BS
26
27/* #define DEBUG_HELPER */
28#ifdef DEBUG_HELPER
29#define HELPER_LOG(x...) qemu_log(x)
30#else
31#define HELPER_LOG(x...)
32#endif
33
891452e5 34/* 64/32 -> 32 signed division */
b4e2bd35 35int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
891452e5 36{
b4e2bd35
RH
37 int32_t ret, b = b64;
38 int64_t q;
39
40 if (b == 0) {
41 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
42 }
43
44 ret = q = a / b;
45 env->retxl = a % b;
46
47 /* Catch non-representable quotient. */
48 if (ret != q) {
49 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
50 }
51
52 return ret;
891452e5
RH
53}
54
55/* 64/32 -> 32 unsigned division */
b4e2bd35 56uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
fc8d72c2 57{
b4e2bd35
RH
58 uint32_t ret, b = b64;
59 uint64_t q;
60
61 if (b == 0) {
62 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
63 }
64
65 ret = q = a / b;
66 env->retxl = a % b;
67
68 /* Catch non-representable quotient. */
69 if (ret != q) {
70 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
71 }
72
73 return ret;
891452e5 74}
fc8d72c2 75
891452e5
RH
76/* 64/64 -> 64 signed division */
77int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
78{
b4e2bd35
RH
79 /* Catch divide by zero, and non-representable quotient (MIN / -1). */
80 if (b == 0 || (b == -1 && a == (1ll << 63))) {
81 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
82 }
891452e5
RH
83 env->retxl = a % b;
84 return a / b;
85}
86
87/* 128 -> 64/64 unsigned division */
88uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
89 uint64_t b)
90{
91 uint64_t ret;
b4e2bd35
RH
92 /* Signal divide by zero. */
93 if (b == 0) {
94 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
95 }
891452e5 96 if (ah == 0) {
fc8d72c2 97 /* 64 -> 64/64 case */
891452e5
RH
98 env->retxl = al % b;
99 ret = al / b;
fc8d72c2 100 } else {
891452e5 101 /* ??? Move i386 idivq helper to host-utils. */
d49b8e0b 102#ifdef CONFIG_INT128
891452e5
RH
103 __uint128_t a = ((__uint128_t)ah << 64) | al;
104 __uint128_t q = a / b;
105 env->retxl = a % b;
106 ret = q;
b4e2bd35
RH
107 if (ret != q) {
108 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
109 }
fc8d72c2 110#else
a47dddd7 111 S390CPU *cpu = s390_env_get_cpu(env);
fc8d72c2
BS
112 /* 32-bit hosts would need special wrapper functionality - just abort if
113 we encounter such a case; it's very unlikely anyways. */
a47dddd7 114 cpu_abort(CPU(cpu), "128 -> 64/64 division not implemented\n");
fc8d72c2
BS
115#endif
116 }
891452e5 117 return ret;
fc8d72c2
BS
118}
119
102bf2c6
RH
120/* count leading zeros, for find leftmost one */
121uint64_t HELPER(clz)(uint64_t v)
fc8d72c2 122{
102bf2c6 123 return clz64(v);
fc8d72c2
BS
124}
125
92f2b4e7 126uint64_t HELPER(cvd)(int32_t reg)
fc8d72c2
BS
127{
128 /* positive 0 */
129 uint64_t dec = 0x0c;
92f2b4e7
AJ
130 int64_t bin = reg;
131 int shift;
fc8d72c2
BS
132
133 if (bin < 0) {
134 bin = -bin;
135 dec = 0x0d;
136 }
137
138 for (shift = 4; (shift < 64) && bin; shift += 4) {
92f2b4e7 139 dec |= (bin % 10) << shift;
fc8d72c2
BS
140 bin /= 10;
141 }
142
143 return dec;
144}
99b4f24b
RH
145
146uint64_t HELPER(popcnt)(uint64_t r2)
147{
148 uint64_t ret = 0;
149 int i;
150
151 for (i = 0; i < 64; i += 8) {
152 uint64_t t = ctpop32((r2 >> i) & 0xff);
153 ret |= t << i;
154 }
155 return ret;
156}