Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20200305' into...
[qemu/ar7.git] / target / s390x / vec_int_helper.c
blob0d6bc13dd6032b1e2339dff2d4e43f1e8fc80629
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 tmp = (int64_t)a->doubleword[0] >> 63;
74 d->doubleword[1] = a->doubleword[0];
75 d->doubleword[0] = tmp;
76 } else if (count < 64) {
77 tmp = a->doubleword[1] >> count;
78 d->doubleword[1] = deposit64(tmp, 64 - count, count, a->doubleword[0]);
79 d->doubleword[0] = (int64_t)a->doubleword[0] >> count;
80 } else {
81 tmp = (int64_t)a->doubleword[0] >> 63;
82 d->doubleword[1] = (int64_t)a->doubleword[0] >> (count - 64);
83 d->doubleword[0] = tmp;
87 static void s390_vec_shr(S390Vector *d, const S390Vector *a, uint64_t count)
89 uint64_t tmp;
91 g_assert(count < 128);
92 if (count == 0) {
93 d->doubleword[0] = a->doubleword[0];
94 d->doubleword[1] = a->doubleword[1];
95 } else if (count == 64) {
96 d->doubleword[1] = a->doubleword[0];
97 d->doubleword[0] = 0;
98 } else if (count < 64) {
99 tmp = a->doubleword[1] >> count;
100 d->doubleword[1] = deposit64(tmp, 64 - count, count, a->doubleword[0]);
101 d->doubleword[0] = a->doubleword[0] >> count;
102 } else {
103 d->doubleword[1] = a->doubleword[0] >> (count - 64);
104 d->doubleword[0] = 0;
107 #define DEF_VAVG(BITS) \
108 void HELPER(gvec_vavg##BITS)(void *v1, const void *v2, const void *v3, \
109 uint32_t desc) \
111 int i; \
113 for (i = 0; i < (128 / BITS); i++) { \
114 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
115 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
117 s390_vec_write_element##BITS(v1, i, (a + b + 1) >> 1); \
120 DEF_VAVG(8)
121 DEF_VAVG(16)
123 #define DEF_VAVGL(BITS) \
124 void HELPER(gvec_vavgl##BITS)(void *v1, const void *v2, const void *v3, \
125 uint32_t desc) \
127 int i; \
129 for (i = 0; i < (128 / BITS); i++) { \
130 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
131 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
133 s390_vec_write_element##BITS(v1, i, (a + b + 1) >> 1); \
136 DEF_VAVGL(8)
137 DEF_VAVGL(16)
139 #define DEF_VCLZ(BITS) \
140 void HELPER(gvec_vclz##BITS)(void *v1, const void *v2, uint32_t desc) \
142 int i; \
144 for (i = 0; i < (128 / BITS); i++) { \
145 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
147 s390_vec_write_element##BITS(v1, i, clz32(a) - 32 + BITS); \
150 DEF_VCLZ(8)
151 DEF_VCLZ(16)
153 #define DEF_VCTZ(BITS) \
154 void HELPER(gvec_vctz##BITS)(void *v1, const void *v2, uint32_t desc) \
156 int i; \
158 for (i = 0; i < (128 / BITS); i++) { \
159 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
161 s390_vec_write_element##BITS(v1, i, a ? ctz32(a) : BITS); \
164 DEF_VCTZ(8)
165 DEF_VCTZ(16)
167 /* like binary multiplication, but XOR instead of addition */
168 #define DEF_GALOIS_MULTIPLY(BITS, TBITS) \
169 static uint##TBITS##_t galois_multiply##BITS(uint##TBITS##_t a, \
170 uint##TBITS##_t b) \
172 uint##TBITS##_t res = 0; \
174 while (b) { \
175 if (b & 0x1) { \
176 res = res ^ a; \
178 a = a << 1; \
179 b = b >> 1; \
181 return res; \
183 DEF_GALOIS_MULTIPLY(8, 16)
184 DEF_GALOIS_MULTIPLY(16, 32)
185 DEF_GALOIS_MULTIPLY(32, 64)
187 static S390Vector galois_multiply64(uint64_t a, uint64_t b)
189 S390Vector res = {};
190 S390Vector va = {
191 .doubleword[1] = a,
193 S390Vector vb = {
194 .doubleword[1] = b,
197 while (!s390_vec_is_zero(&vb)) {
198 if (vb.doubleword[1] & 0x1) {
199 s390_vec_xor(&res, &res, &va);
201 s390_vec_shl(&va, &va, 1);
202 s390_vec_shr(&vb, &vb, 1);
204 return res;
207 #define DEF_VGFM(BITS, TBITS) \
208 void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \
209 uint32_t desc) \
211 int i; \
213 for (i = 0; i < (128 / TBITS); i++) { \
214 uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \
215 uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \
216 uint##TBITS##_t d = galois_multiply##BITS(a, b); \
218 a = s390_vec_read_element##BITS(v2, i * 2 + 1); \
219 b = s390_vec_read_element##BITS(v3, i * 2 + 1); \
220 d = d ^ galois_multiply32(a, b); \
221 s390_vec_write_element##TBITS(v1, i, d); \
224 DEF_VGFM(8, 16)
225 DEF_VGFM(16, 32)
226 DEF_VGFM(32, 64)
228 void HELPER(gvec_vgfm64)(void *v1, const void *v2, const void *v3,
229 uint32_t desc)
231 S390Vector tmp1, tmp2;
232 uint64_t a, b;
234 a = s390_vec_read_element64(v2, 0);
235 b = s390_vec_read_element64(v3, 0);
236 tmp1 = galois_multiply64(a, b);
237 a = s390_vec_read_element64(v2, 1);
238 b = s390_vec_read_element64(v3, 1);
239 tmp2 = galois_multiply64(a, b);
240 s390_vec_xor(v1, &tmp1, &tmp2);
243 #define DEF_VGFMA(BITS, TBITS) \
244 void HELPER(gvec_vgfma##BITS)(void *v1, const void *v2, const void *v3, \
245 const void *v4, uint32_t desc) \
247 int i; \
249 for (i = 0; i < (128 / TBITS); i++) { \
250 uint##BITS##_t a = s390_vec_read_element##BITS(v2, i * 2); \
251 uint##BITS##_t b = s390_vec_read_element##BITS(v3, i * 2); \
252 uint##TBITS##_t d = galois_multiply##BITS(a, b); \
254 a = s390_vec_read_element##BITS(v2, i * 2 + 1); \
255 b = s390_vec_read_element##BITS(v3, i * 2 + 1); \
256 d = d ^ galois_multiply32(a, b); \
257 d = d ^ s390_vec_read_element##TBITS(v4, i); \
258 s390_vec_write_element##TBITS(v1, i, d); \
261 DEF_VGFMA(8, 16)
262 DEF_VGFMA(16, 32)
263 DEF_VGFMA(32, 64)
265 void HELPER(gvec_vgfma64)(void *v1, const void *v2, const void *v3,
266 const void *v4, uint32_t desc)
268 S390Vector tmp1, tmp2;
269 uint64_t a, b;
271 a = s390_vec_read_element64(v2, 0);
272 b = s390_vec_read_element64(v3, 0);
273 tmp1 = galois_multiply64(a, b);
274 a = s390_vec_read_element64(v2, 1);
275 b = s390_vec_read_element64(v3, 1);
276 tmp2 = galois_multiply64(a, b);
277 s390_vec_xor(&tmp1, &tmp1, &tmp2);
278 s390_vec_xor(v1, &tmp1, v4);
281 #define DEF_VMAL(BITS) \
282 void HELPER(gvec_vmal##BITS)(void *v1, const void *v2, const void *v3, \
283 const void *v4, uint32_t desc) \
285 int i; \
287 for (i = 0; i < (128 / BITS); i++) { \
288 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
289 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
290 const uint##BITS##_t c = s390_vec_read_element##BITS(v4, i); \
292 s390_vec_write_element##BITS(v1, i, a * b + c); \
295 DEF_VMAL(8)
296 DEF_VMAL(16)
298 #define DEF_VMAH(BITS) \
299 void HELPER(gvec_vmah##BITS)(void *v1, const void *v2, const void *v3, \
300 const void *v4, uint32_t desc) \
302 int i; \
304 for (i = 0; i < (128 / BITS); i++) { \
305 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
306 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
307 const int32_t c = (int##BITS##_t)s390_vec_read_element##BITS(v4, i); \
309 s390_vec_write_element##BITS(v1, i, (a * b + c) >> BITS); \
312 DEF_VMAH(8)
313 DEF_VMAH(16)
315 #define DEF_VMALH(BITS) \
316 void HELPER(gvec_vmalh##BITS)(void *v1, const void *v2, const void *v3, \
317 const void *v4, uint32_t desc) \
319 int i; \
321 for (i = 0; i < (128 / BITS); i++) { \
322 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
323 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
324 const uint##BITS##_t c = s390_vec_read_element##BITS(v4, i); \
326 s390_vec_write_element##BITS(v1, i, (a * b + c) >> BITS); \
329 DEF_VMALH(8)
330 DEF_VMALH(16)
332 #define DEF_VMAE(BITS, TBITS) \
333 void HELPER(gvec_vmae##BITS)(void *v1, const void *v2, const void *v3, \
334 const void *v4, uint32_t desc) \
336 int i, j; \
338 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
339 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
340 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
341 int##TBITS##_t c = s390_vec_read_element##TBITS(v4, i); \
343 s390_vec_write_element##TBITS(v1, i, a * b + c); \
346 DEF_VMAE(8, 16)
347 DEF_VMAE(16, 32)
348 DEF_VMAE(32, 64)
350 #define DEF_VMALE(BITS, TBITS) \
351 void HELPER(gvec_vmale##BITS)(void *v1, const void *v2, const void *v3, \
352 const void *v4, uint32_t desc) \
354 int i, j; \
356 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
357 uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
358 uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
359 uint##TBITS##_t c = s390_vec_read_element##TBITS(v4, i); \
361 s390_vec_write_element##TBITS(v1, i, a * b + c); \
364 DEF_VMALE(8, 16)
365 DEF_VMALE(16, 32)
366 DEF_VMALE(32, 64)
368 #define DEF_VMAO(BITS, TBITS) \
369 void HELPER(gvec_vmao##BITS)(void *v1, const void *v2, const void *v3, \
370 const void *v4, uint32_t desc) \
372 int i, j; \
374 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
375 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
376 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
377 int##TBITS##_t c = s390_vec_read_element##TBITS(v4, i); \
379 s390_vec_write_element##TBITS(v1, i, a * b + c); \
382 DEF_VMAO(8, 16)
383 DEF_VMAO(16, 32)
384 DEF_VMAO(32, 64)
386 #define DEF_VMALO(BITS, TBITS) \
387 void HELPER(gvec_vmalo##BITS)(void *v1, const void *v2, const void *v3, \
388 const void *v4, uint32_t desc) \
390 int i, j; \
392 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
393 uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
394 uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
395 uint##TBITS##_t c = s390_vec_read_element##TBITS(v4, i); \
397 s390_vec_write_element##TBITS(v1, i, a * b + c); \
400 DEF_VMALO(8, 16)
401 DEF_VMALO(16, 32)
402 DEF_VMALO(32, 64)
404 #define DEF_VMH(BITS) \
405 void HELPER(gvec_vmh##BITS)(void *v1, const void *v2, const void *v3, \
406 uint32_t desc) \
408 int i; \
410 for (i = 0; i < (128 / BITS); i++) { \
411 const int32_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, i); \
412 const int32_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, i); \
414 s390_vec_write_element##BITS(v1, i, (a * b) >> BITS); \
417 DEF_VMH(8)
418 DEF_VMH(16)
420 #define DEF_VMLH(BITS) \
421 void HELPER(gvec_vmlh##BITS)(void *v1, const void *v2, const void *v3, \
422 uint32_t desc) \
424 int i; \
426 for (i = 0; i < (128 / BITS); i++) { \
427 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
428 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
430 s390_vec_write_element##BITS(v1, i, (a * b) >> BITS); \
433 DEF_VMLH(8)
434 DEF_VMLH(16)
436 #define DEF_VME(BITS, TBITS) \
437 void HELPER(gvec_vme##BITS)(void *v1, const void *v2, const void *v3, \
438 uint32_t desc) \
440 int i, j; \
442 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
443 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
444 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
446 s390_vec_write_element##TBITS(v1, i, a * b); \
449 DEF_VME(8, 16)
450 DEF_VME(16, 32)
451 DEF_VME(32, 64)
453 #define DEF_VMLE(BITS, TBITS) \
454 void HELPER(gvec_vmle##BITS)(void *v1, const void *v2, const void *v3, \
455 uint32_t desc) \
457 int i, j; \
459 for (i = 0, j = 0; i < (128 / TBITS); i++, j += 2) { \
460 const uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
461 const uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
463 s390_vec_write_element##TBITS(v1, i, a * b); \
466 DEF_VMLE(8, 16)
467 DEF_VMLE(16, 32)
468 DEF_VMLE(32, 64)
470 #define DEF_VMO(BITS, TBITS) \
471 void HELPER(gvec_vmo##BITS)(void *v1, const void *v2, const void *v3, \
472 uint32_t desc) \
474 int i, j; \
476 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
477 int##TBITS##_t a = (int##BITS##_t)s390_vec_read_element##BITS(v2, j); \
478 int##TBITS##_t b = (int##BITS##_t)s390_vec_read_element##BITS(v3, j); \
480 s390_vec_write_element##TBITS(v1, i, a * b); \
483 DEF_VMO(8, 16)
484 DEF_VMO(16, 32)
485 DEF_VMO(32, 64)
487 #define DEF_VMLO(BITS, TBITS) \
488 void HELPER(gvec_vmlo##BITS)(void *v1, const void *v2, const void *v3, \
489 uint32_t desc) \
491 int i, j; \
493 for (i = 0, j = 1; i < (128 / TBITS); i++, j += 2) { \
494 const uint##TBITS##_t a = s390_vec_read_element##BITS(v2, j); \
495 const uint##TBITS##_t b = s390_vec_read_element##BITS(v3, j); \
497 s390_vec_write_element##TBITS(v1, i, a * b); \
500 DEF_VMLO(8, 16)
501 DEF_VMLO(16, 32)
502 DEF_VMLO(32, 64)
504 #define DEF_VPOPCT(BITS) \
505 void HELPER(gvec_vpopct##BITS)(void *v1, const void *v2, uint32_t desc) \
507 int i; \
509 for (i = 0; i < (128 / BITS); i++) { \
510 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
512 s390_vec_write_element##BITS(v1, i, ctpop32(a)); \
515 DEF_VPOPCT(8)
516 DEF_VPOPCT(16)
518 #define DEF_VERLLV(BITS) \
519 void HELPER(gvec_verllv##BITS)(void *v1, const void *v2, const void *v3, \
520 uint32_t desc) \
522 int i; \
524 for (i = 0; i < (128 / BITS); i++) { \
525 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
526 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
528 s390_vec_write_element##BITS(v1, i, rol##BITS(a, b)); \
531 DEF_VERLLV(8)
532 DEF_VERLLV(16)
534 #define DEF_VERLL(BITS) \
535 void HELPER(gvec_verll##BITS)(void *v1, const void *v2, uint64_t count, \
536 uint32_t desc) \
538 int i; \
540 for (i = 0; i < (128 / BITS); i++) { \
541 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
543 s390_vec_write_element##BITS(v1, i, rol##BITS(a, count)); \
546 DEF_VERLL(8)
547 DEF_VERLL(16)
549 #define DEF_VERIM(BITS) \
550 void HELPER(gvec_verim##BITS)(void *v1, const void *v2, const void *v3, \
551 uint32_t desc) \
553 const uint8_t count = simd_data(desc); \
554 int i; \
556 for (i = 0; i < (128 / BITS); i++) { \
557 const uint##BITS##_t a = s390_vec_read_element##BITS(v1, i); \
558 const uint##BITS##_t b = s390_vec_read_element##BITS(v2, i); \
559 const uint##BITS##_t mask = s390_vec_read_element##BITS(v3, i); \
560 const uint##BITS##_t d = (a & ~mask) | (rol##BITS(b, count) & mask); \
562 s390_vec_write_element##BITS(v1, i, d); \
565 DEF_VERIM(8)
566 DEF_VERIM(16)
568 void HELPER(gvec_vsl)(void *v1, const void *v2, uint64_t count,
569 uint32_t desc)
571 s390_vec_shl(v1, v2, count);
574 void HELPER(gvec_vsra)(void *v1, const void *v2, uint64_t count,
575 uint32_t desc)
577 s390_vec_sar(v1, v2, count);
580 void HELPER(gvec_vsrl)(void *v1, const void *v2, uint64_t count,
581 uint32_t desc)
583 s390_vec_shr(v1, v2, count);
586 #define DEF_VSCBI(BITS) \
587 void HELPER(gvec_vscbi##BITS)(void *v1, const void *v2, const void *v3, \
588 uint32_t desc) \
590 int i; \
592 for (i = 0; i < (128 / BITS); i++) { \
593 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
594 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
596 s390_vec_write_element##BITS(v1, i, a >= b); \
599 DEF_VSCBI(8)
600 DEF_VSCBI(16)
602 void HELPER(gvec_vtm)(void *v1, const void *v2, CPUS390XState *env,
603 uint32_t desc)
605 S390Vector tmp;
607 s390_vec_and(&tmp, v1, v2);
608 if (s390_vec_is_zero(&tmp)) {
609 /* Selected bits all zeros; or all mask bits zero */
610 env->cc_op = 0;
611 } else if (s390_vec_equal(&tmp, v2)) {
612 /* Selected bits all ones */
613 env->cc_op = 3;
614 } else {
615 /* Selected bits a mix of zeros and ones */
616 env->cc_op = 1;