qemu-common: Move tcg_enabled() etc. to sysemu/tcg.h
[qemu/ar7.git] / target / s390x / vec_int_helper.c
blob68eaae407b47206be10aeabd0171c78cc6802b4e
1 /*
2 * QEMU TCG support -- s390x vector integer instruction support
4 * Copyright (C) 2019 Red Hat Inc
6 * Authors:
7 * David Hildenbrand <david@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
12 #include "qemu/osdep.h"
13 #include "qemu-common.h"
14 #include "cpu.h"
15 #include "vec.h"
16 #include "exec/helper-proto.h"
17 #include "tcg/tcg-gvec-desc.h"
19 static bool s390_vec_is_zero(const S390Vector *v)
21 return !v->doubleword[0] && !v->doubleword[1];
24 static void s390_vec_xor(S390Vector *res, const S390Vector *a,
25 const S390Vector *b)
27 res->doubleword[0] = a->doubleword[0] ^ b->doubleword[0];
28 res->doubleword[1] = a->doubleword[1] ^ b->doubleword[1];
31 static void s390_vec_and(S390Vector *res, const S390Vector *a,
32 const S390Vector *b)
34 res->doubleword[0] = a->doubleword[0] & b->doubleword[0];
35 res->doubleword[1] = a->doubleword[1] & b->doubleword[1];
38 static bool s390_vec_equal(const S390Vector *a, const S390Vector *b)
40 return a->doubleword[0] == b->doubleword[0] &&
41 a->doubleword[1] == b->doubleword[1];
44 static void s390_vec_shl(S390Vector *d, const S390Vector *a, uint64_t count)
46 uint64_t tmp;
48 g_assert(count < 128);
49 if (count == 0) {
50 d->doubleword[0] = a->doubleword[0];
51 d->doubleword[1] = a->doubleword[1];
52 } else if (count == 64) {
53 d->doubleword[0] = a->doubleword[1];
54 d->doubleword[1] = 0;
55 } else if (count < 64) {
56 tmp = extract64(a->doubleword[1], 64 - count, count);
57 d->doubleword[1] = a->doubleword[1] << count;
58 d->doubleword[0] = (a->doubleword[0] << count) | tmp;
59 } else {
60 d->doubleword[0] = a->doubleword[1] << (count - 64);
61 d->doubleword[1] = 0;
65 static void s390_vec_sar(S390Vector *d, const S390Vector *a, uint64_t count)
67 uint64_t tmp;
69 if (count == 0) {
70 d->doubleword[0] = a->doubleword[0];
71 d->doubleword[1] = a->doubleword[1];
72 } else if (count == 64) {
73 d->doubleword[1] = a->doubleword[0];
74 d->doubleword[0] = 0;
75 } else if (count < 64) {
76 tmp = a->doubleword[1] >> count;
77 d->doubleword[1] = deposit64(tmp, 64 - count, count, a->doubleword[0]);
78 d->doubleword[0] = (int64_t)a->doubleword[0] >> count;
79 } else {
80 d->doubleword[1] = (int64_t)a->doubleword[0] >> (count - 64);
81 d->doubleword[0] = 0;
85 static void s390_vec_shr(S390Vector *d, const S390Vector *a, uint64_t count)
87 uint64_t tmp;
89 g_assert(count < 128);
90 if (count == 0) {
91 d->doubleword[0] = a->doubleword[0];
92 d->doubleword[1] = a->doubleword[1];
93 } else if (count == 64) {
94 d->doubleword[1] = a->doubleword[0];
95 d->doubleword[0] = 0;
96 } else if (count < 64) {
97 tmp = a->doubleword[1] >> count;
98 d->doubleword[1] = deposit64(tmp, 64 - count, count, a->doubleword[0]);
99 d->doubleword[0] = a->doubleword[0] >> count;
100 } else {
101 d->doubleword[1] = a->doubleword[0] >> (count - 64);
102 d->doubleword[0] = 0;
105 #define DEF_VAVG(BITS) \
106 void HELPER(gvec_vavg##BITS)(void *v1, const void *v2, const void *v3, \
107 uint32_t desc) \
109 int i; \
111 for (i = 0; i < (128 / BITS); i++) { \
112 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
113 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
115 s390_vec_write_element##BITS(v1, i, (a + b + 1) >> 1); \
118 DEF_VAVG(8)
119 DEF_VAVG(16)
121 #define DEF_VAVGL(BITS) \
122 void HELPER(gvec_vavgl##BITS)(void *v1, const void *v2, const void *v3, \
123 uint32_t desc) \
125 int i; \
127 for (i = 0; i < (128 / BITS); i++) { \
128 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
129 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
131 s390_vec_write_element##BITS(v1, i, (a + b + 1) >> 1); \
134 DEF_VAVGL(8)
135 DEF_VAVGL(16)
137 #define DEF_VCLZ(BITS) \
138 void HELPER(gvec_vclz##BITS)(void *v1, const void *v2, uint32_t desc) \
140 int i; \
142 for (i = 0; i < (128 / BITS); i++) { \
143 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
145 s390_vec_write_element##BITS(v1, i, clz32(a) - 32 + BITS); \
148 DEF_VCLZ(8)
149 DEF_VCLZ(16)
151 #define DEF_VCTZ(BITS) \
152 void HELPER(gvec_vctz##BITS)(void *v1, const void *v2, uint32_t desc) \
154 int i; \
156 for (i = 0; i < (128 / BITS); i++) { \
157 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
159 s390_vec_write_element##BITS(v1, i, a ? ctz32(a) : BITS); \
162 DEF_VCTZ(8)
163 DEF_VCTZ(16)
165 /* like binary multiplication, but XOR instead of addition */
166 #define DEF_GALOIS_MULTIPLY(BITS, TBITS) \
167 static uint##TBITS##_t galois_multiply##BITS(uint##TBITS##_t a, \
168 uint##TBITS##_t b) \
170 uint##TBITS##_t res = 0; \
172 while (b) { \
173 if (b & 0x1) { \
174 res = res ^ a; \
176 a = a << 1; \
177 b = b >> 1; \
179 return res; \
181 DEF_GALOIS_MULTIPLY(8, 16)
182 DEF_GALOIS_MULTIPLY(16, 32)
183 DEF_GALOIS_MULTIPLY(32, 64)
185 static S390Vector galois_multiply64(uint64_t a, uint64_t b)
187 S390Vector res = {};
188 S390Vector va = {
189 .doubleword[1] = a,
191 S390Vector vb = {
192 .doubleword[1] = b,
195 while (!s390_vec_is_zero(&vb)) {
196 if (vb.doubleword[1] & 0x1) {
197 s390_vec_xor(&res, &res, &va);
199 s390_vec_shl(&va, &va, 1);
200 s390_vec_shr(&vb, &vb, 1);
202 return res;
205 #define DEF_VGFM(BITS, TBITS) \
206 void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \
207 uint32_t desc) \
209 int i; \
211 for (i = 0; i < (128 / TBITS); i++) { \
212 uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \
213 uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \
214 uint##TBITS##_t d = galois_multiply##BITS(a, b); \
216 a = s390_vec_read_element##BITS(v2, i * 2 + 1); \
217 b = s390_vec_read_element##BITS(v3, i * 2 + 1); \
218 d = d ^ galois_multiply32(a, b); \
219 s390_vec_write_element##TBITS(v1, i, d); \
222 DEF_VGFM(8, 16)
223 DEF_VGFM(16, 32)
224 DEF_VGFM(32, 64)
226 void HELPER(gvec_vgfm64)(void *v1, const void *v2, const void *v3,
227 uint32_t desc)
229 S390Vector tmp1, tmp2;
230 uint64_t a, b;
232 a = s390_vec_read_element64(v2, 0);
233 b = s390_vec_read_element64(v3, 0);
234 tmp1 = galois_multiply64(a, b);
235 a = s390_vec_read_element64(v2, 1);
236 b = s390_vec_read_element64(v3, 1);
237 tmp2 = galois_multiply64(a, b);
238 s390_vec_xor(v1, &tmp1, &tmp2);
241 #define DEF_VGFMA(BITS, TBITS) \
242 void HELPER(gvec_vgfma##BITS)(void *v1, const void *v2, const void *v3, \
243 const void *v4, uint32_t desc) \
245 int i; \
247 for (i = 0; i < (128 / TBITS); i++) { \
248 uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \
249 uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \
250 uint##TBITS##_t d = galois_multiply##BITS(a, b); \
252 a = s390_vec_read_element##BITS(v2, i * 2 + 1); \
253 b = s390_vec_read_element##BITS(v3, i * 2 + 1); \
254 d = d ^ galois_multiply32(a, b); \
255 d = d ^ s390_vec_read_element##TBITS(v4, i); \
256 s390_vec_write_element##TBITS(v1, i, d); \
259 DEF_VGFMA(8, 16)
260 DEF_VGFMA(16, 32)
261 DEF_VGFMA(32, 64)
263 void HELPER(gvec_vgfma64)(void *v1, const void *v2, const void *v3,
264 const void *v4, uint32_t desc)
266 S390Vector tmp1, tmp2;
267 uint64_t a, b;
269 a = s390_vec_read_element64(v2, 0);
270 b = s390_vec_read_element64(v3, 0);
271 tmp1 = galois_multiply64(a, b);
272 a = s390_vec_read_element64(v2, 1);
273 b = s390_vec_read_element64(v3, 1);
274 tmp2 = galois_multiply64(a, b);
275 s390_vec_xor(&tmp1, &tmp1, &tmp2);
276 s390_vec_xor(v1, &tmp1, v4);
279 #define DEF_VMAL(BITS) \
280 void HELPER(gvec_vmal##BITS)(void *v1, const void *v2, const void *v3, \
281 const void *v4, uint32_t desc) \
283 int i; \
285 for (i = 0; i < (128 / BITS); i++) { \
286 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
287 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
288 const uint##BITS##_t c = s390_vec_read_element##BITS(v4, i); \
290 s390_vec_write_element##BITS(v1, i, a * b + c); \
293 DEF_VMAL(8)
294 DEF_VMAL(16)
296 #define DEF_VMAH(BITS) \
297 void HELPER(gvec_vmah##BITS)(void *v1, const void *v2, const void *v3, \
298 const void *v4, uint32_t desc) \
300 int i; \
302 for (i = 0; i < (128 / BITS); i++) { \
303 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
304 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
305 const int32_t c = (int##BITS##_t)s390_vec_read_element##BITS(v4, i); \
307 s390_vec_write_element##BITS(v1, i, (a * b + c) >> BITS); \
310 DEF_VMAH(8)
311 DEF_VMAH(16)
313 #define DEF_VMALH(BITS) \
314 void HELPER(gvec_vmalh##BITS)(void *v1, const void *v2, const void *v3, \
315 const void *v4, uint32_t desc) \
317 int i; \
319 for (i = 0; i < (128 / BITS); i++) { \
320 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
321 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
322 const uint##BITS##_t c = s390_vec_read_element##BITS(v4, i); \
324 s390_vec_write_element##BITS(v1, i, (a * b + c) >> BITS); \
327 DEF_VMALH(8)
328 DEF_VMALH(16)
330 #define DEF_VMAE(BITS, TBITS) \
331 void HELPER(gvec_vmae##BITS)(void *v1, const void *v2, const void *v3, \
332 const void *v4, uint32_t desc) \
334 int i, j; \
336 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
337 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
338 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
339 int##TBITS##_t c = (int##BITS##_t)s390_vec_read_element##BITS(v4, j); \
341 s390_vec_write_element##TBITS(v1, i, a * b + c); \
344 DEF_VMAE(8, 16)
345 DEF_VMAE(16, 32)
346 DEF_VMAE(32, 64)
348 #define DEF_VMALE(BITS, TBITS) \
349 void HELPER(gvec_vmale##BITS)(void *v1, const void *v2, const void *v3, \
350 const void *v4, uint32_t desc) \
352 int i, j; \
354 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
355 uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
356 uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
357 uint##TBITS##_t c = s390_vec_read_element##BITS(v4, j); \
359 s390_vec_write_element##TBITS(v1, i, a * b + c); \
362 DEF_VMALE(8, 16)
363 DEF_VMALE(16, 32)
364 DEF_VMALE(32, 64)
366 #define DEF_VMAO(BITS, TBITS) \
367 void HELPER(gvec_vmao##BITS)(void *v1, const void *v2, const void *v3, \
368 const void *v4, uint32_t desc) \
370 int i, j; \
372 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
373 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
374 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
375 int##TBITS##_t c = (int##BITS##_t)s390_vec_read_element##BITS(v4, j); \
377 s390_vec_write_element##TBITS(v1, i, a * b + c); \
380 DEF_VMAO(8, 16)
381 DEF_VMAO(16, 32)
382 DEF_VMAO(32, 64)
384 #define DEF_VMALO(BITS, TBITS) \
385 void HELPER(gvec_vmalo##BITS)(void *v1, const void *v2, const void *v3, \
386 const void *v4, uint32_t desc) \
388 int i, j; \
390 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
391 uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
392 uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
393 uint##TBITS##_t c = s390_vec_read_element##BITS(v4, j); \
395 s390_vec_write_element##TBITS(v1, i, a * b + c); \
398 DEF_VMALO(8, 16)
399 DEF_VMALO(16, 32)
400 DEF_VMALO(32, 64)
402 #define DEF_VMH(BITS) \
403 void HELPER(gvec_vmh##BITS)(void *v1, const void *v2, const void *v3, \
404 uint32_t desc) \
406 int i; \
408 for (i = 0; i < (128 / BITS); i++) { \
409 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
410 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
412 s390_vec_write_element##BITS(v1, i, (a * b) >> BITS); \
415 DEF_VMH(8)
416 DEF_VMH(16)
418 #define DEF_VMLH(BITS) \
419 void HELPER(gvec_vmlh##BITS)(void *v1, const void *v2, const void *v3, \
420 uint32_t desc) \
422 int i; \
424 for (i = 0; i < (128 / BITS); i++) { \
425 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
426 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
428 s390_vec_write_element##BITS(v1, i, (a * b) >> BITS); \
431 DEF_VMLH(8)
432 DEF_VMLH(16)
434 #define DEF_VME(BITS, TBITS) \
435 void HELPER(gvec_vme##BITS)(void *v1, const void *v2, const void *v3, \
436 uint32_t desc) \
438 int i, j; \
440 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
441 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
442 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
444 s390_vec_write_element##TBITS(v1, i, a * b); \
447 DEF_VME(8, 16)
448 DEF_VME(16, 32)
449 DEF_VME(32, 64)
451 #define DEF_VMLE(BITS, TBITS) \
452 void HELPER(gvec_vmle##BITS)(void *v1, const void *v2, const void *v3, \
453 uint32_t desc) \
455 int i, j; \
457 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
458 const uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
459 const uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
461 s390_vec_write_element##TBITS(v1, i, a * b); \
464 DEF_VMLE(8, 16)
465 DEF_VMLE(16, 32)
466 DEF_VMLE(32, 64)
468 #define DEF_VMO(BITS, TBITS) \
469 void HELPER(gvec_vmo##BITS)(void *v1, const void *v2, const void *v3, \
470 uint32_t desc) \
472 int i, j; \
474 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
475 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
476 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
478 s390_vec_write_element##TBITS(v1, i, a * b); \
481 DEF_VMO(8, 16)
482 DEF_VMO(16, 32)
483 DEF_VMO(32, 64)
485 #define DEF_VMLO(BITS, TBITS) \
486 void HELPER(gvec_vmlo##BITS)(void *v1, const void *v2, const void *v3, \
487 uint32_t desc) \
489 int i, j; \
491 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
492 const uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
493 const uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
495 s390_vec_write_element##TBITS(v1, i, a * b); \
498 DEF_VMLO(8, 16)
499 DEF_VMLO(16, 32)
500 DEF_VMLO(32, 64)
502 #define DEF_VPOPCT(BITS) \
503 void HELPER(gvec_vpopct##BITS)(void *v1, const void *v2, uint32_t desc) \
505 int i; \
507 for (i = 0; i < (128 / BITS); i++) { \
508 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
510 s390_vec_write_element##BITS(v1, i, ctpop32(a)); \
513 DEF_VPOPCT(8)
514 DEF_VPOPCT(16)
516 #define DEF_VERLLV(BITS) \
517 void HELPER(gvec_verllv##BITS)(void *v1, const void *v2, const void *v3, \
518 uint32_t desc) \
520 int i; \
522 for (i = 0; i < (128 / BITS); i++) { \
523 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
524 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
526 s390_vec_write_element##BITS(v1, i, rol##BITS(a, b)); \
529 DEF_VERLLV(8)
530 DEF_VERLLV(16)
532 #define DEF_VERLL(BITS) \
533 void HELPER(gvec_verll##BITS)(void *v1, const void *v2, uint64_t count, \
534 uint32_t desc) \
536 int i; \
538 for (i = 0; i < (128 / BITS); i++) { \
539 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
541 s390_vec_write_element##BITS(v1, i, rol##BITS(a, count)); \
544 DEF_VERLL(8)
545 DEF_VERLL(16)
547 #define DEF_VERIM(BITS) \
548 void HELPER(gvec_verim##BITS)(void *v1, const void *v2, const void *v3, \
549 uint32_t desc) \
551 const uint8_t count = simd_data(desc); \
552 int i; \
554 for (i = 0; i < (128 / BITS); i++) { \
555 const uint##BITS##_t a = s390_vec_read_element##BITS(v1, i); \
556 const uint##BITS##_t b = s390_vec_read_element##BITS(v2, i); \
557 const uint##BITS##_t mask = s390_vec_read_element##BITS(v3, i); \
558 const uint##BITS##_t d = (a & ~mask) | (rol##BITS(b, count) & mask); \
560 s390_vec_write_element##BITS(v1, i, d); \
563 DEF_VERIM(8)
564 DEF_VERIM(16)
566 void HELPER(gvec_vsl)(void *v1, const void *v2, uint64_t count,
567 uint32_t desc)
569 s390_vec_shl(v1, v2, count);
572 void HELPER(gvec_vsra)(void *v1, const void *v2, uint64_t count,
573 uint32_t desc)
575 s390_vec_sar(v1, v2, count);
578 void HELPER(gvec_vsrl)(void *v1, const void *v2, uint64_t count,
579 uint32_t desc)
581 s390_vec_shr(v1, v2, count);
584 #define DEF_VSCBI(BITS) \
585 void HELPER(gvec_vscbi##BITS)(void *v1, const void *v2, const void *v3, \
586 uint32_t desc) \
588 int i; \
590 for (i = 0; i < (128 / BITS); i++) { \
591 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
592 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
594 s390_vec_write_element##BITS(v1, i, a < b); \
597 DEF_VSCBI(8)
598 DEF_VSCBI(16)
600 void HELPER(gvec_vtm)(void *v1, const void *v2, CPUS390XState *env,
601 uint32_t desc)
603 S390Vector tmp;
605 s390_vec_and(&tmp, v1, v2);
606 if (s390_vec_is_zero(&tmp)) {
607 /* Selected bits all zeros; or all mask bits zero */
608 env->cc_op = 0;
609 } else if (s390_vec_equal(&tmp, v2)) {
610 /* Selected bits all ones */
611 env->cc_op = 3;
612 } else {
613 /* Selected bits a mix of zeros and ones */
614 env->cc_op = 1;