2 * QEMU TCG support -- s390x vector integer instruction support
4 * Copyright (C) 2019 Red Hat Inc
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"
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
,
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
,
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
)
48 g_assert(count
< 128);
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];
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
;
60 d
->doubleword
[0] = a
->doubleword
[1] << (count
- 64);
65 static void s390_vec_sar(S390Vector
*d
, const S390Vector
*a
, uint64_t count
)
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];
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
;
80 d
->doubleword
[1] = (int64_t)a
->doubleword
[0] >> (count
- 64);
85 static void s390_vec_shr(S390Vector
*d
, const S390Vector
*a
, uint64_t count
)
89 g_assert(count
< 128);
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];
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
;
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, \
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); \
121 #define DEF_VAVGL(BITS) \
122 void HELPER(gvec_vavgl##BITS)(void *v1, const void *v2, const void *v3, \
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); \
137 #define DEF_VCLZ(BITS) \
138 void HELPER(gvec_vclz##BITS)(void *v1, const void *v2, uint32_t desc) \
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); \
151 #define DEF_VCTZ(BITS) \
152 void HELPER(gvec_vctz##BITS)(void *v1, const void *v2, uint32_t desc) \
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); \
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, \
170 uint##TBITS##_t res = 0; \
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
)
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);
205 #define DEF_VGFM(BITS, TBITS) \
206 void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \
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); \
226 void HELPER(gvec_vgfm64
)(void *v1
, const void *v2
, const void *v3
,
229 S390Vector tmp1
, tmp2
;
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) \
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); \
263 void HELPER(gvec_vgfma64
)(void *v1
, const void *v2
, const void *v3
,
264 const void *v4
, uint32_t desc
)
266 S390Vector tmp1
, tmp2
;
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
402 #define DEF_VMH(BITS) \
403 void HELPER(gvec_vmh##BITS)(void *v1, const void *v2, const void *v3, \
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); \
418 #define DEF_VMLH(BITS) \
419 void HELPER(gvec_vmlh##BITS)(void *v1, const void *v2, const void *v3, \
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); \
434 #define DEF_VME(BITS, TBITS) \
435 void HELPER(gvec_vme##BITS)(void *v1, const void *v2, const void *v3, \
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); \
451 #define DEF_VMLE(BITS, TBITS) \
452 void HELPER(gvec_vmle##BITS)(void *v1, const void *v2, const void *v3, \
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); \
468 #define DEF_VMO(BITS, TBITS) \
469 void HELPER(gvec_vmo##BITS)(void *v1, const void *v2, const void *v3, \
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); \
485 #define DEF_VMLO(BITS, TBITS) \
486 void HELPER(gvec_vmlo##BITS)(void *v1, const void *v2, const void *v3, \
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); \
502 #define DEF_VPOPCT(BITS) \
503 void HELPER(gvec_vpopct##BITS)(void *v1, const void *v2, uint32_t desc) \
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)); \
516 #define DEF_VERLLV(BITS) \
517 void HELPER(gvec_verllv##BITS)(void *v1, const void *v2, const void *v3, \
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)); \
532 #define DEF_VERLL(BITS) \
533 void HELPER(gvec_verll##BITS)(void *v1, const void *v2, uint64_t count, \
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)); \
547 #define DEF_VERIM(BITS) \
548 void HELPER(gvec_verim##BITS)(void *v1, const void *v2, const void *v3, \
551 const uint8_t count = simd_data(desc); \
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); \
566 void HELPER(gvec_vsl
)(void *v1
, const void *v2
, uint64_t count
,
569 s390_vec_shl(v1
, v2
, count
);
572 void HELPER(gvec_vsra
)(void *v1
, const void *v2
, uint64_t count
,
575 s390_vec_sar(v1
, v2
, count
);
578 void HELPER(gvec_vsrl
)(void *v1
, const void *v2
, uint64_t count
,
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, \
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); \
600 void HELPER(gvec_vtm
)(void *v1
, const void *v2
, CPUS390XState
*env
,
605 s390_vec_and(&tmp
, v1
, v2
);
606 if (s390_vec_is_zero(&tmp
)) {
607 /* Selected bits all zeros; or all mask bits zero */
609 } else if (s390_vec_equal(&tmp
, v2
)) {
610 /* Selected bits all ones */
613 /* Selected bits a mix of zeros and ones */