Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 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 | |
Peter Maydell | 9615495 | 2016-01-26 18:17:00 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 22 | #include "cpu.h" |
Paolo Bonzini | 63c9155 | 2016-03-15 13:18:37 +0100 | [diff] [blame] | 23 | #include "exec/exec-all.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 24 | #include "qemu/host-utils.h" |
Richard Henderson | 2ef6175 | 2014-04-07 22:31:41 -0700 | [diff] [blame] | 25 | #include "exec/helper-proto.h" |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 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 | |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 34 | /* 64/32 -> 32 signed division */ |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 35 | int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64) |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 36 | { |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 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; |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 53 | } |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 54 | |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 55 | /* 64/32 -> 32 unsigned division */ |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 56 | uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64) |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 57 | { |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 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; |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* 64/64 -> 64 signed division */ |
| 77 | int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b) |
| 78 | { |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 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 | } |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 83 | env->retxl = a % b; |
| 84 | return a / b; |
| 85 | } |
| 86 | |
| 87 | /* 128 -> 64/64 unsigned division */ |
| 88 | uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al, |
| 89 | uint64_t b) |
| 90 | { |
| 91 | uint64_t ret; |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 92 | /* Signal divide by zero. */ |
| 93 | if (b == 0) { |
| 94 | runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC()); |
| 95 | } |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 96 | if (ah == 0) { |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 97 | /* 64 -> 64/64 case */ |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 98 | env->retxl = al % b; |
| 99 | ret = al / b; |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 100 | } else { |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 101 | /* ??? Move i386 idivq helper to host-utils. */ |
Gabriel Kerneis | d49b8e0 | 2013-04-23 18:15:12 +0100 | [diff] [blame] | 102 | #ifdef CONFIG_INT128 |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 103 | __uint128_t a = ((__uint128_t)ah << 64) | al; |
| 104 | __uint128_t q = a / b; |
| 105 | env->retxl = a % b; |
| 106 | ret = q; |
Richard Henderson | b4e2bd3 | 2012-09-05 17:27:40 -0700 | [diff] [blame] | 107 | if (ret != q) { |
| 108 | runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC()); |
| 109 | } |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 110 | #else |
Andreas Färber | a47dddd | 2013-09-03 17:38:47 +0200 | [diff] [blame] | 111 | S390CPU *cpu = s390_env_get_cpu(env); |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 112 | /* 32-bit hosts would need special wrapper functionality - just abort if |
| 113 | we encounter such a case; it's very unlikely anyways. */ |
Andreas Färber | a47dddd | 2013-09-03 17:38:47 +0200 | [diff] [blame] | 114 | cpu_abort(CPU(cpu), "128 -> 64/64 division not implemented\n"); |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 115 | #endif |
| 116 | } |
Richard Henderson | 891452e | 2012-08-20 14:02:02 -0700 | [diff] [blame] | 117 | return ret; |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Aurelien Jarno | 92f2b4e | 2015-06-25 21:16:58 +0200 | [diff] [blame] | 120 | uint64_t HELPER(cvd)(int32_t reg) |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 121 | { |
| 122 | /* positive 0 */ |
| 123 | uint64_t dec = 0x0c; |
Aurelien Jarno | 92f2b4e | 2015-06-25 21:16:58 +0200 | [diff] [blame] | 124 | int64_t bin = reg; |
| 125 | int shift; |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 126 | |
| 127 | if (bin < 0) { |
| 128 | bin = -bin; |
| 129 | dec = 0x0d; |
| 130 | } |
| 131 | |
| 132 | for (shift = 4; (shift < 64) && bin; shift += 4) { |
Aurelien Jarno | 92f2b4e | 2015-06-25 21:16:58 +0200 | [diff] [blame] | 133 | dec |= (bin % 10) << shift; |
Blue Swirl | fc8d72c | 2012-09-02 07:33:33 +0000 | [diff] [blame] | 134 | bin /= 10; |
| 135 | } |
| 136 | |
| 137 | return dec; |
| 138 | } |
Richard Henderson | 99b4f24 | 2012-09-01 11:14:04 -0700 | [diff] [blame] | 139 | |
Richard Henderson | 250a87d | 2016-11-21 12:06:26 +0100 | [diff] [blame] | 140 | uint64_t HELPER(popcnt)(uint64_t val) |
Richard Henderson | 99b4f24 | 2012-09-01 11:14:04 -0700 | [diff] [blame] | 141 | { |
Richard Henderson | 250a87d | 2016-11-21 12:06:26 +0100 | [diff] [blame] | 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; |
Richard Henderson | 99b4f24 | 2012-09-01 11:14:04 -0700 | [diff] [blame] | 147 | } |