1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21#include "qemu/osdep.h"
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"
27
28
29#ifdef DEBUG_HELPER
30#define HELPER_LOG(x...) qemu_log(x)
31#else
32#define HELPER_LOG(x...)
33#endif
34
35
36int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
37{
38 int32_t ret, b = b64;
39 int64_t q;
40
41 if (b == 0) {
42 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
43 }
44
45 ret = q = a / b;
46 env->retxl = a % b;
47
48
49 if (ret != q) {
50 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
51 }
52
53 return ret;
54}
55
56
57uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
58{
59 uint32_t ret, b = b64;
60 uint64_t q;
61
62 if (b == 0) {
63 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
64 }
65
66 ret = q = a / b;
67 env->retxl = a % b;
68
69
70 if (ret != q) {
71 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
72 }
73
74 return ret;
75}
76
77
78int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
79{
80
81 if (b == 0 || (b == -1 && a == (1ll << 63))) {
82 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
83 }
84 env->retxl = a % b;
85 return a / b;
86}
87
88
89uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
90 uint64_t b)
91{
92 uint64_t ret;
93
94 if (b == 0) {
95 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
96 }
97 if (ah == 0) {
98
99 env->retxl = al % b;
100 ret = al / b;
101 } else {
102
103#ifdef CONFIG_INT128
104 __uint128_t a = ((__uint128_t)ah << 64) | al;
105 __uint128_t q = a / b;
106 env->retxl = a % b;
107 ret = q;
108 if (ret != q) {
109 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
110 }
111#else
112 S390CPU *cpu = s390_env_get_cpu(env);
113
114
115 cpu_abort(CPU(cpu), "128 -> 64/64 division not implemented\n");
116#endif
117 }
118 return ret;
119}
120
121uint64_t HELPER(cvd)(int32_t reg)
122{
123
124 uint64_t dec = 0x0c;
125 int64_t bin = reg;
126 int shift;
127
128 if (bin < 0) {
129 bin = -bin;
130 dec = 0x0d;
131 }
132
133 for (shift = 4; (shift < 64) && bin; shift += 4) {
134 dec |= (bin % 10) << shift;
135 bin /= 10;
136 }
137
138 return dec;
139}
140
141uint64_t HELPER(popcnt)(uint64_t val)
142{
143
144 val = (val & 0x5555555555555555ULL) + ((val >> 1) & 0x5555555555555555ULL);
145 val = (val & 0x3333333333333333ULL) + ((val >> 2) & 0x3333333333333333ULL);
146 val = (val + (val >> 4)) & 0x0f0f0f0f0f0f0f0fULL;
147 return val;
148}
149