]>
Commit | Line | Data |
---|---|---|
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 | |
41c6a6dd | 10 | * version 2.1 of the License, or (at your option) any later version. |
fc8d72c2 BS |
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" |
4e58b838 | 23 | #include "internal.h" |
63c91552 | 24 | #include "exec/exec-all.h" |
1de7afc9 | 25 | #include "qemu/host-utils.h" |
2ef6175a | 26 | #include "exec/helper-proto.h" |
fc8d72c2 BS |
27 | |
28 | /* #define DEBUG_HELPER */ | |
29 | #ifdef DEBUG_HELPER | |
30 | #define HELPER_LOG(x...) qemu_log(x) | |
31 | #else | |
32 | #define HELPER_LOG(x...) | |
33 | #endif | |
34 | ||
891452e5 | 35 | /* 64/32 -> 32 signed division */ |
b4e2bd35 | 36 | int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64) |
891452e5 | 37 | { |
b4e2bd35 RH |
38 | int32_t ret, b = b64; |
39 | int64_t q; | |
40 | ||
41 | if (b == 0) { | |
7693f77a | 42 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 RH |
43 | } |
44 | ||
45 | ret = q = a / b; | |
46 | env->retxl = a % b; | |
47 | ||
48 | /* Catch non-representable quotient. */ | |
49 | if (ret != q) { | |
7693f77a | 50 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 RH |
51 | } |
52 | ||
53 | return ret; | |
891452e5 RH |
54 | } |
55 | ||
56 | /* 64/32 -> 32 unsigned division */ | |
b4e2bd35 | 57 | uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64) |
fc8d72c2 | 58 | { |
b4e2bd35 RH |
59 | uint32_t ret, b = b64; |
60 | uint64_t q; | |
61 | ||
62 | if (b == 0) { | |
7693f77a | 63 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 RH |
64 | } |
65 | ||
66 | ret = q = a / b; | |
67 | env->retxl = a % b; | |
68 | ||
69 | /* Catch non-representable quotient. */ | |
70 | if (ret != q) { | |
7693f77a | 71 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 RH |
72 | } |
73 | ||
74 | return ret; | |
891452e5 | 75 | } |
fc8d72c2 | 76 | |
891452e5 RH |
77 | /* 64/64 -> 64 signed division */ |
78 | int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b) | |
79 | { | |
b4e2bd35 RH |
80 | /* Catch divide by zero, and non-representable quotient (MIN / -1). */ |
81 | if (b == 0 || (b == -1 && a == (1ll << 63))) { | |
7693f77a | 82 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 | 83 | } |
891452e5 RH |
84 | env->retxl = a % b; |
85 | return a / b; | |
86 | } | |
87 | ||
88 | /* 128 -> 64/64 unsigned division */ | |
89 | uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al, | |
90 | uint64_t b) | |
91 | { | |
92 | uint64_t ret; | |
b4e2bd35 RH |
93 | /* Signal divide by zero. */ |
94 | if (b == 0) { | |
7693f77a | 95 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 | 96 | } |
891452e5 | 97 | if (ah == 0) { |
fc8d72c2 | 98 | /* 64 -> 64/64 case */ |
891452e5 RH |
99 | env->retxl = al % b; |
100 | ret = al / b; | |
fc8d72c2 | 101 | } else { |
891452e5 | 102 | /* ??? Move i386 idivq helper to host-utils. */ |
d49b8e0b | 103 | #ifdef CONFIG_INT128 |
891452e5 RH |
104 | __uint128_t a = ((__uint128_t)ah << 64) | al; |
105 | __uint128_t q = a / b; | |
106 | env->retxl = a % b; | |
107 | ret = q; | |
b4e2bd35 | 108 | if (ret != q) { |
7693f77a | 109 | s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC()); |
b4e2bd35 | 110 | } |
fc8d72c2 BS |
111 | #else |
112 | /* 32-bit hosts would need special wrapper functionality - just abort if | |
113 | we encounter such a case; it's very unlikely anyways. */ | |
dc79e928 | 114 | cpu_abort(env_cpu(env), "128 -> 64/64 division not implemented\n"); |
fc8d72c2 BS |
115 | #endif |
116 | } | |
891452e5 | 117 | return ret; |
fc8d72c2 BS |
118 | } |
119 | ||
92f2b4e7 | 120 | uint64_t HELPER(cvd)(int32_t reg) |
fc8d72c2 BS |
121 | { |
122 | /* positive 0 */ | |
123 | uint64_t dec = 0x0c; | |
92f2b4e7 AJ |
124 | int64_t bin = reg; |
125 | int shift; | |
fc8d72c2 BS |
126 | |
127 | if (bin < 0) { | |
128 | bin = -bin; | |
129 | dec = 0x0d; | |
130 | } | |
131 | ||
132 | for (shift = 4; (shift < 64) && bin; shift += 4) { | |
92f2b4e7 | 133 | dec |= (bin % 10) << shift; |
fc8d72c2 BS |
134 | bin /= 10; |
135 | } | |
136 | ||
137 | return dec; | |
138 | } | |
99b4f24b | 139 | |
250a87d5 | 140 | uint64_t HELPER(popcnt)(uint64_t val) |
99b4f24b | 141 | { |
250a87d5 RH |
142 | /* Note that we don't fold past bytes. */ |
143 | val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL); | |
144 | val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL); | |
145 | val = (val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fULL; | |
146 | return val; | |
99b4f24b | 147 | } |