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 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
;
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
)
91 g_assert(count
< 128);
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];
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
;
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, \
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); \
123 #define DEF_VAVGL(BITS) \
124 void HELPER(gvec_vavgl##BITS)(void *v1, const void *v2, const void *v3, \
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); \
139 #define DEF_VCLZ(BITS) \
140 void HELPER(gvec_vclz##BITS)(void *v1, const void *v2, uint32_t desc) \
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); \
153 #define DEF_VCTZ(BITS) \
154 void HELPER(gvec_vctz##BITS)(void *v1, const void *v2, uint32_t desc) \
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); \
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, \
172 uint##TBITS##_t res = 0; \
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
)
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);
207 #define DEF_VGFM(BITS, TBITS) \
208 void HELPER(gvec_vgfm##BITS)(void *v1, const void *v2, const void *v3, \
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); \
228 void HELPER(gvec_vgfm64
)(void *v1
, const void *v2
, const void *v3
,
231 S390Vector tmp1
, tmp2
;
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) \
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); \
265 void HELPER(gvec_vgfma64
)(void *v1
, const void *v2
, const void *v3
,
266 const void *v4
, uint32_t desc
)
268 S390Vector tmp1
, tmp2
;
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
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) \
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); \
404 #define DEF_VMH(BITS) \
405 void HELPER(gvec_vmh##BITS)(void *v1, const void *v2, const void *v3, \
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); \
420 #define DEF_VMLH(BITS) \
421 void HELPER(gvec_vmlh##BITS)(void *v1, const void *v2, const void *v3, \
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); \
436 #define DEF_VME(BITS, TBITS) \
437 void HELPER(gvec_vme##BITS)(void *v1, const void *v2, const void *v3, \
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); \
453 #define DEF_VMLE(BITS, TBITS) \
454 void HELPER(gvec_vmle##BITS)(void *v1, const void *v2, const void *v3, \
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); \
470 #define DEF_VMO(BITS, TBITS) \
471 void HELPER(gvec_vmo##BITS)(void *v1, const void *v2, const void *v3, \
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); \
487 #define DEF_VMLO(BITS, TBITS) \
488 void HELPER(gvec_vmlo##BITS)(void *v1, const void *v2, const void *v3, \
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); \
504 #define DEF_VPOPCT(BITS) \
505 void HELPER(gvec_vpopct##BITS)(void *v1, const void *v2, uint32_t desc) \
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)); \
518 #define DEF_VERIM(BITS) \
519 void HELPER(gvec_verim##BITS)(void *v1, const void *v2, const void *v3, \
522 const uint8_t count = simd_data(desc); \
525 for (i = 0; i < (128 / BITS); i++) { \
526 const uint##BITS##_t a = s390_vec_read_element##BITS(v1, i); \
527 const uint##BITS##_t b = s390_vec_read_element##BITS(v2, i); \
528 const uint##BITS##_t mask = s390_vec_read_element##BITS(v3, i); \
529 const uint##BITS##_t d = (a & ~mask) | (rol##BITS(b, count) & mask); \
531 s390_vec_write_element##BITS(v1, i, d); \
537 void HELPER(gvec_vsl
)(void *v1
, const void *v2
, uint64_t count
,
540 s390_vec_shl(v1
, v2
, count
);
543 void HELPER(gvec_vsra
)(void *v1
, const void *v2
, uint64_t count
,
546 s390_vec_sar(v1
, v2
, count
);
549 void HELPER(gvec_vsrl
)(void *v1
, const void *v2
, uint64_t count
,
552 s390_vec_shr(v1
, v2
, count
);
555 #define DEF_VSCBI(BITS) \
556 void HELPER(gvec_vscbi##BITS)(void *v1, const void *v2, const void *v3, \
561 for (i = 0; i < (128 / BITS); i++) { \
562 const uint##BITS##_t a = s390_vec_read_element##BITS(v2, i); \
563 const uint##BITS##_t b = s390_vec_read_element##BITS(v3, i); \
565 s390_vec_write_element##BITS(v1, i, a >= b); \
571 void HELPER(gvec_vtm
)(void *v1
, const void *v2
, CPUS390XState
*env
,
576 s390_vec_and(&tmp
, v1
, v2
);
577 if (s390_vec_is_zero(&tmp
)) {
578 /* Selected bits all zeros; or all mask bits zero */
580 } else if (s390_vec_equal(&tmp
, v2
)) {
581 /* Selected bits all ones */
584 /* Selected bits a mix of zeros and ones */