int_helper.c 3.9 KB
Newer Older
B
Blue Swirl 已提交
1 2 3 4 5 6 7 8 9
/*
 *  S/390 integer helper routines
 *
 *  Copyright (c) 2009 Ulrich Hecht
 *  Copyright (c) 2009 Alexander Graf
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
B
Blue Swirl 已提交
11 12 13 14 15 16 17 18 19 20
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

P
Peter Maydell 已提交
21
#include "qemu/osdep.h"
B
Blue Swirl 已提交
22
#include "cpu.h"
23
#include "internal.h"
24
#include "exec/exec-all.h"
25
#include "qemu/host-utils.h"
26
#include "exec/helper-proto.h"
B
Blue Swirl 已提交
27 28 29 30 31 32 33 34

/* #define DEBUG_HELPER */
#ifdef DEBUG_HELPER
#define HELPER_LOG(x...) qemu_log(x)
#else
#define HELPER_LOG(x...)
#endif

R
Richard Henderson 已提交
35
/* 64/32 -> 32 signed division */
36
int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
R
Richard Henderson 已提交
37
{
38 39 40 41
    int32_t ret, b = b64;
    int64_t q;

    if (b == 0) {
42
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
43 44 45 46 47 48 49
    }

    ret = q = a / b;
    env->retxl = a % b;

    /* Catch non-representable quotient.  */
    if (ret != q) {
50
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
51 52 53
    }

    return ret;
R
Richard Henderson 已提交
54 55 56
}

/* 64/32 -> 32 unsigned division */
57
uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
B
Blue Swirl 已提交
58
{
59 60 61 62
    uint32_t ret, b = b64;
    uint64_t q;

    if (b == 0) {
63
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
64 65 66 67 68 69 70
    }

    ret = q = a / b;
    env->retxl = a % b;

    /* Catch non-representable quotient.  */
    if (ret != q) {
71
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
72 73 74
    }

    return ret;
R
Richard Henderson 已提交
75
}
B
Blue Swirl 已提交
76

R
Richard Henderson 已提交
77 78 79
/* 64/64 -> 64 signed division */
int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
{
80 81
    /* Catch divide by zero, and non-representable quotient (MIN / -1).  */
    if (b == 0 || (b == -1 && a == (1ll << 63))) {
82
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
83
    }
R
Richard Henderson 已提交
84 85 86 87 88 89 90 91 92
    env->retxl = a % b;
    return a / b;
}

/* 128 -> 64/64 unsigned division */
uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
                        uint64_t b)
{
    uint64_t ret;
93 94
    /* Signal divide by zero.  */
    if (b == 0) {
95
        s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
96
    }
R
Richard Henderson 已提交
97
    if (ah == 0) {
B
Blue Swirl 已提交
98
        /* 64 -> 64/64 case */
R
Richard Henderson 已提交
99 100
        env->retxl = al % b;
        ret = al / b;
B
Blue Swirl 已提交
101
    } else {
R
Richard Henderson 已提交
102
        /* ??? Move i386 idivq helper to host-utils.  */
103
#ifdef CONFIG_INT128
R
Richard Henderson 已提交
104 105 106 107
        __uint128_t a = ((__uint128_t)ah << 64) | al;
        __uint128_t q = a / b;
        env->retxl = a % b;
        ret = q;
108
        if (ret != q) {
109
            s390_program_interrupt(env, PGM_FIXPT_DIVIDE, ILEN_AUTO, GETPC());
110
        }
B
Blue Swirl 已提交
111 112 113
#else
        /* 32-bit hosts would need special wrapper functionality - just abort if
           we encounter such a case; it's very unlikely anyways. */
114
        cpu_abort(env_cpu(env), "128 -> 64/64 division not implemented\n");
B
Blue Swirl 已提交
115 116
#endif
    }
R
Richard Henderson 已提交
117
    return ret;
B
Blue Swirl 已提交
118 119
}

120
uint64_t HELPER(cvd)(int32_t reg)
B
Blue Swirl 已提交
121 122 123
{
    /* positive 0 */
    uint64_t dec = 0x0c;
124 125
    int64_t bin = reg;
    int shift;
B
Blue Swirl 已提交
126 127 128 129 130 131 132

    if (bin < 0) {
        bin = -bin;
        dec = 0x0d;
    }

    for (shift = 4; (shift < 64) && bin; shift += 4) {
133
        dec |= (bin % 10) << shift;
B
Blue Swirl 已提交
134 135 136 137 138
        bin /= 10;
    }

    return dec;
}
139

140
uint64_t HELPER(popcnt)(uint64_t val)
141
{
142 143 144 145 146
    /* Note that we don't fold past bytes. */
    val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
    val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
    val = (val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
    return val;
147
}