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 "exec/exec-all.h"
24#include "qemu/host-utils.h"
25#include "exec/helper-proto.h"
26
27
28#ifdef DEBUG_HELPER
29#define HELPER_LOG(x...) qemu_log(x)
30#else
31#define HELPER_LOG(x...)
32#endif
33
34
35int64_t HELPER(divs32)(CPUS390XState *env, int64_t a, int64_t b64)
36{
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
48 if (ret != q) {
49 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
50 }
51
52 return ret;
53}
54
55
56uint64_t HELPER(divu32)(CPUS390XState *env, uint64_t a, uint64_t b64)
57{
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
69 if (ret != q) {
70 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
71 }
72
73 return ret;
74}
75
76
77int64_t HELPER(divs64)(CPUS390XState *env, int64_t a, int64_t b)
78{
79
80 if (b == 0 || (b == -1 && a == (1ll << 63))) {
81 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
82 }
83 env->retxl = a % b;
84 return a / b;
85}
86
87
88uint64_t HELPER(divu64)(CPUS390XState *env, uint64_t ah, uint64_t al,
89 uint64_t b)
90{
91 uint64_t ret;
92
93 if (b == 0) {
94 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
95 }
96 if (ah == 0) {
97
98 env->retxl = al % b;
99 ret = al / b;
100 } else {
101
102#ifdef CONFIG_INT128
103 __uint128_t a = ((__uint128_t)ah << 64) | al;
104 __uint128_t q = a / b;
105 env->retxl = a % b;
106 ret = q;
107 if (ret != q) {
108 runtime_exception(env, PGM_FIXPT_DIVIDE, GETPC());
109 }
110#else
111 S390CPU *cpu = s390_env_get_cpu(env);
112
113
114 cpu_abort(CPU(cpu), "128 -> 64/64 division not implemented\n");
115#endif
116 }
117 return ret;
118}
119
120
121uint64_t HELPER(clz)(uint64_t v)
122{
123 return clz64(v);
124}
125
126uint64_t HELPER(cvd)(int32_t reg)
127{
128
129 uint64_t dec = 0x0c;
130 int64_t bin = reg;
131 int shift;
132
133 if (bin < 0) {
134 bin = -bin;
135 dec = 0x0d;
136 }
137
138 for (shift = 4; (shift < 64) && bin; shift += 4) {
139 dec |= (bin % 10) << shift;
140 bin /= 10;
141 }
142
143 return dec;
144}
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}
157