]> git.proxmox.com Git - mirror_qemu.git/blame - target/arm/translate-sve.c
target/arm: Use gen_gvec_ool_arg_zzz for do_zip, do_zip_q
[mirror_qemu.git] / target / arm / translate-sve.c
CommitLineData
38388f7e
RH
1/*
2 * AArch64 SVE translation
3 *
4 * Copyright (c) 2018 Linaro, Ltd
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
50f57e09 9 * version 2.1 of the License, or (at your option) any later version.
38388f7e
RH
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "qemu/osdep.h"
21#include "cpu.h"
22#include "exec/exec-all.h"
dcb32f1d
PMD
23#include "tcg/tcg-op.h"
24#include "tcg/tcg-op-gvec.h"
25#include "tcg/tcg-gvec-desc.h"
38388f7e
RH
26#include "qemu/log.h"
27#include "arm_ldst.h"
28#include "translate.h"
29#include "internals.h"
30#include "exec/helper-proto.h"
31#include "exec/helper-gen.h"
32#include "exec/log.h"
38388f7e 33#include "translate-a64.h"
cc48affe 34#include "fpu/softfloat.h"
38388f7e 35
757f9cff 36
9ee3a611
RH
37typedef void GVecGen2sFn(unsigned, uint32_t, uint32_t,
38 TCGv_i64, uint32_t, uint32_t);
39
38cadeba
RH
40typedef void gen_helper_gvec_flags_3(TCGv_i32, TCGv_ptr, TCGv_ptr,
41 TCGv_ptr, TCGv_i32);
757f9cff
RH
42typedef void gen_helper_gvec_flags_4(TCGv_i32, TCGv_ptr, TCGv_ptr,
43 TCGv_ptr, TCGv_ptr, TCGv_i32);
44
c4e7c493 45typedef void gen_helper_gvec_mem(TCGv_env, TCGv_ptr, TCGv_i64, TCGv_i32);
f6dbf62a
RH
46typedef void gen_helper_gvec_mem_scatter(TCGv_env, TCGv_ptr, TCGv_ptr,
47 TCGv_ptr, TCGv_i64, TCGv_i32);
c4e7c493 48
ccd841c3
RH
49/*
50 * Helpers for extracting complex instruction fields.
51 */
52
53/* See e.g. ASR (immediate, predicated).
54 * Returns -1 for unallocated encoding; diagnose later.
55 */
451e4ffd 56static int tszimm_esz(DisasContext *s, int x)
ccd841c3
RH
57{
58 x >>= 3; /* discard imm3 */
59 return 31 - clz32(x);
60}
61
451e4ffd 62static int tszimm_shr(DisasContext *s, int x)
ccd841c3 63{
451e4ffd 64 return (16 << tszimm_esz(s, x)) - x;
ccd841c3
RH
65}
66
67/* See e.g. LSL (immediate, predicated). */
451e4ffd 68static int tszimm_shl(DisasContext *s, int x)
ccd841c3 69{
451e4ffd 70 return x - (8 << tszimm_esz(s, x));
ccd841c3
RH
71}
72
f25a2361 73/* The SH bit is in bit 8. Extract the low 8 and shift. */
451e4ffd 74static inline int expand_imm_sh8s(DisasContext *s, int x)
f25a2361
RH
75{
76 return (int8_t)x << (x & 0x100 ? 8 : 0);
77}
78
451e4ffd 79static inline int expand_imm_sh8u(DisasContext *s, int x)
6e6a157d
RH
80{
81 return (uint8_t)x << (x & 0x100 ? 8 : 0);
82}
83
c4e7c493
RH
84/* Convert a 2-bit memory size (msz) to a 4-bit data type (dtype)
85 * with unsigned data. C.f. SVE Memory Contiguous Load Group.
86 */
451e4ffd 87static inline int msz_dtype(DisasContext *s, int msz)
c4e7c493
RH
88{
89 static const uint8_t dtype[4] = { 0, 5, 10, 15 };
90 return dtype[msz];
91}
92
38388f7e
RH
93/*
94 * Include the generated decoder.
95 */
96
139c1837 97#include "decode-sve.c.inc"
38388f7e
RH
98
99/*
100 * Implement all of the translator functions referenced by the decoder.
101 */
102
d1822297
RH
103/* Return the offset info CPUARMState of the predicate vector register Pn.
104 * Note for this purpose, FFR is P16.
105 */
106static inline int pred_full_reg_offset(DisasContext *s, int regno)
107{
108 return offsetof(CPUARMState, vfp.pregs[regno]);
109}
110
111/* Return the byte size of the whole predicate register, VL / 64. */
112static inline int pred_full_reg_size(DisasContext *s)
113{
114 return s->sve_len >> 3;
115}
116
516e246a
RH
117/* Round up the size of a register to a size allowed by
118 * the tcg vector infrastructure. Any operation which uses this
119 * size may assume that the bits above pred_full_reg_size are zero,
120 * and must leave them the same way.
121 *
122 * Note that this is not needed for the vector registers as they
123 * are always properly sized for tcg vectors.
124 */
125static int size_for_gvec(int size)
126{
127 if (size <= 8) {
128 return 8;
129 } else {
130 return QEMU_ALIGN_UP(size, 16);
131 }
132}
133
134static int pred_gvec_reg_size(DisasContext *s)
135{
136 return size_for_gvec(pred_full_reg_size(s));
137}
138
40e32e5a 139/* Invoke an out-of-line helper on 2 Zregs. */
c5edf07d 140static bool gen_gvec_ool_zz(DisasContext *s, gen_helper_gvec_2 *fn,
40e32e5a
RH
141 int rd, int rn, int data)
142{
c5edf07d
RH
143 if (fn == NULL) {
144 return false;
145 }
146 if (sve_access_check(s)) {
147 unsigned vsz = vec_full_reg_size(s);
148 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, rd),
149 vec_full_reg_offset(s, rn),
150 vsz, vsz, data, fn);
151 }
152 return true;
40e32e5a
RH
153}
154
e645d1a1 155/* Invoke an out-of-line helper on 3 Zregs. */
913a8a00 156static bool gen_gvec_ool_zzz(DisasContext *s, gen_helper_gvec_3 *fn,
e645d1a1
RH
157 int rd, int rn, int rm, int data)
158{
913a8a00
RH
159 if (fn == NULL) {
160 return false;
161 }
162 if (sve_access_check(s)) {
163 unsigned vsz = vec_full_reg_size(s);
164 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
165 vec_full_reg_offset(s, rn),
166 vec_full_reg_offset(s, rm),
167 vsz, vsz, data, fn);
168 }
169 return true;
e645d1a1
RH
170}
171
84a272f5
RH
172static bool gen_gvec_ool_arg_zzz(DisasContext *s, gen_helper_gvec_3 *fn,
173 arg_rrr_esz *a, int data)
174{
175 return gen_gvec_ool_zzz(s, fn, a->rd, a->rn, a->rm, data);
176}
177
38650638 178/* Invoke an out-of-line helper on 4 Zregs. */
7ad416b1 179static bool gen_gvec_ool_zzzz(DisasContext *s, gen_helper_gvec_4 *fn,
38650638
RH
180 int rd, int rn, int rm, int ra, int data)
181{
7ad416b1
RH
182 if (fn == NULL) {
183 return false;
184 }
185 if (sve_access_check(s)) {
186 unsigned vsz = vec_full_reg_size(s);
187 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
188 vec_full_reg_offset(s, rn),
189 vec_full_reg_offset(s, rm),
190 vec_full_reg_offset(s, ra),
191 vsz, vsz, data, fn);
192 }
193 return true;
38650638
RH
194}
195
cab79ac9
RH
196static bool gen_gvec_ool_arg_zzzz(DisasContext *s, gen_helper_gvec_4 *fn,
197 arg_rrrr_esz *a, int data)
198{
199 return gen_gvec_ool_zzzz(s, fn, a->rd, a->rn, a->rm, a->ra, data);
200}
201
e82d3536
RH
202static bool gen_gvec_ool_arg_zzxz(DisasContext *s, gen_helper_gvec_4 *fn,
203 arg_rrxr_esz *a)
204{
205 return gen_gvec_ool_zzzz(s, fn, a->rd, a->rn, a->rm, a->ra, a->index);
206}
207
96a461f7 208/* Invoke an out-of-line helper on 2 Zregs and a predicate. */
8fb27a21 209static bool gen_gvec_ool_zzp(DisasContext *s, gen_helper_gvec_3 *fn,
96a461f7
RH
210 int rd, int rn, int pg, int data)
211{
8fb27a21
RH
212 if (fn == NULL) {
213 return false;
214 }
215 if (sve_access_check(s)) {
216 unsigned vsz = vec_full_reg_size(s);
217 tcg_gen_gvec_3_ool(vec_full_reg_offset(s, rd),
218 vec_full_reg_offset(s, rn),
219 pred_full_reg_offset(s, pg),
220 vsz, vsz, data, fn);
221 }
222 return true;
96a461f7
RH
223}
224
b051809a
RH
225static bool gen_gvec_ool_arg_zpz(DisasContext *s, gen_helper_gvec_3 *fn,
226 arg_rpr_esz *a, int data)
227{
228 return gen_gvec_ool_zzp(s, fn, a->rd, a->rn, a->pg, data);
229}
230
afa2529c
RH
231static bool gen_gvec_ool_arg_zpzi(DisasContext *s, gen_helper_gvec_3 *fn,
232 arg_rpri_esz *a)
233{
234 return gen_gvec_ool_zzp(s, fn, a->rd, a->rn, a->pg, a->imm);
235}
b051809a 236
36cbb7a8 237/* Invoke an out-of-line helper on 3 Zregs and a predicate. */
2a753d1e 238static bool gen_gvec_ool_zzzp(DisasContext *s, gen_helper_gvec_4 *fn,
36cbb7a8
RH
239 int rd, int rn, int rm, int pg, int data)
240{
2a753d1e
RH
241 if (fn == NULL) {
242 return false;
243 }
244 if (sve_access_check(s)) {
245 unsigned vsz = vec_full_reg_size(s);
246 tcg_gen_gvec_4_ool(vec_full_reg_offset(s, rd),
247 vec_full_reg_offset(s, rn),
248 vec_full_reg_offset(s, rm),
249 pred_full_reg_offset(s, pg),
250 vsz, vsz, data, fn);
251 }
252 return true;
36cbb7a8 253}
f7d79c41 254
312016c9
RH
255static bool gen_gvec_ool_arg_zpzz(DisasContext *s, gen_helper_gvec_4 *fn,
256 arg_rprr_esz *a, int data)
257{
258 return gen_gvec_ool_zzzp(s, fn, a->rd, a->rn, a->rm, a->pg, data);
259}
260
faf915e2
RH
261/* Invoke a vector expander on two Zregs and an immediate. */
262static bool gen_gvec_fn_zzi(DisasContext *s, GVecGen2iFn *gvec_fn,
263 int esz, int rd, int rn, uint64_t imm)
264{
265 if (gvec_fn == NULL) {
266 return false;
267 }
268 if (sve_access_check(s)) {
269 unsigned vsz = vec_full_reg_size(s);
270 gvec_fn(esz, vec_full_reg_offset(s, rd),
271 vec_full_reg_offset(s, rn), imm, vsz, vsz);
272 }
273 return true;
274}
275
ada378f0
RH
276static bool gen_gvec_fn_arg_zzi(DisasContext *s, GVecGen2iFn *gvec_fn,
277 arg_rri_esz *a)
278{
279 if (a->esz < 0) {
280 /* Invalid tsz encoding -- see tszimm_esz. */
281 return false;
282 }
283 return gen_gvec_fn_zzi(s, gvec_fn, a->esz, a->rd, a->rn, a->imm);
284}
285
39eea561 286/* Invoke a vector expander on three Zregs. */
50f6db5f 287static bool gen_gvec_fn_zzz(DisasContext *s, GVecGen3Fn *gvec_fn,
28c4da31 288 int esz, int rd, int rn, int rm)
38388f7e 289{
50f6db5f
RH
290 if (gvec_fn == NULL) {
291 return false;
292 }
293 if (sve_access_check(s)) {
294 unsigned vsz = vec_full_reg_size(s);
295 gvec_fn(esz, vec_full_reg_offset(s, rd),
296 vec_full_reg_offset(s, rn),
297 vec_full_reg_offset(s, rm), vsz, vsz);
298 }
299 return true;
38388f7e
RH
300}
301
cd54bbe6
RH
302static bool gen_gvec_fn_arg_zzz(DisasContext *s, GVecGen3Fn *fn,
303 arg_rrr_esz *a)
304{
305 return gen_gvec_fn_zzz(s, fn, a->esz, a->rd, a->rn, a->rm);
306}
307
911cdc6d 308/* Invoke a vector expander on four Zregs. */
189876af
RH
309static bool gen_gvec_fn_arg_zzzz(DisasContext *s, GVecGen4Fn *gvec_fn,
310 arg_rrrr_esz *a)
911cdc6d 311{
189876af
RH
312 if (gvec_fn == NULL) {
313 return false;
314 }
315 if (sve_access_check(s)) {
316 unsigned vsz = vec_full_reg_size(s);
317 gvec_fn(a->esz, vec_full_reg_offset(s, a->rd),
318 vec_full_reg_offset(s, a->rn),
319 vec_full_reg_offset(s, a->rm),
320 vec_full_reg_offset(s, a->ra), vsz, vsz);
321 }
322 return true;
911cdc6d
RH
323}
324
39eea561
RH
325/* Invoke a vector move on two Zregs. */
326static bool do_mov_z(DisasContext *s, int rd, int rn)
38388f7e 327{
f7d79c41 328 if (sve_access_check(s)) {
5f730621
RH
329 unsigned vsz = vec_full_reg_size(s);
330 tcg_gen_gvec_mov(MO_8, vec_full_reg_offset(s, rd),
331 vec_full_reg_offset(s, rn), vsz, vsz);
f7d79c41
RH
332 }
333 return true;
38388f7e
RH
334}
335
d9d78dcc
RH
336/* Initialize a Zreg with replications of a 64-bit immediate. */
337static void do_dupi_z(DisasContext *s, int rd, uint64_t word)
338{
339 unsigned vsz = vec_full_reg_size(s);
8711e71f 340 tcg_gen_gvec_dup_imm(MO_64, vec_full_reg_offset(s, rd), vsz, vsz, word);
d9d78dcc
RH
341}
342
516e246a 343/* Invoke a vector expander on three Pregs. */
dd81a8d7
RH
344static void gen_gvec_fn_ppp(DisasContext *s, GVecGen3Fn *gvec_fn,
345 int rd, int rn, int rm)
516e246a 346{
dd81a8d7
RH
347 unsigned psz = pred_gvec_reg_size(s);
348 gvec_fn(MO_64, pred_full_reg_offset(s, rd),
349 pred_full_reg_offset(s, rn),
350 pred_full_reg_offset(s, rm), psz, psz);
516e246a
RH
351}
352
353/* Invoke a vector move on two Pregs. */
354static bool do_mov_p(DisasContext *s, int rd, int rn)
355{
d0b2df5a
RH
356 if (sve_access_check(s)) {
357 unsigned psz = pred_gvec_reg_size(s);
358 tcg_gen_gvec_mov(MO_8, pred_full_reg_offset(s, rd),
359 pred_full_reg_offset(s, rn), psz, psz);
360 }
361 return true;
516e246a
RH
362}
363
9e18d7a6
RH
364/* Set the cpu flags as per a return from an SVE helper. */
365static void do_pred_flags(TCGv_i32 t)
366{
367 tcg_gen_mov_i32(cpu_NF, t);
368 tcg_gen_andi_i32(cpu_ZF, t, 2);
369 tcg_gen_andi_i32(cpu_CF, t, 1);
370 tcg_gen_movi_i32(cpu_VF, 0);
371}
372
373/* Subroutines computing the ARM PredTest psuedofunction. */
374static void do_predtest1(TCGv_i64 d, TCGv_i64 g)
375{
376 TCGv_i32 t = tcg_temp_new_i32();
377
378 gen_helper_sve_predtest1(t, d, g);
379 do_pred_flags(t);
380 tcg_temp_free_i32(t);
381}
382
383static void do_predtest(DisasContext *s, int dofs, int gofs, int words)
384{
385 TCGv_ptr dptr = tcg_temp_new_ptr();
386 TCGv_ptr gptr = tcg_temp_new_ptr();
392acacc 387 TCGv_i32 t = tcg_temp_new_i32();
9e18d7a6
RH
388
389 tcg_gen_addi_ptr(dptr, cpu_env, dofs);
390 tcg_gen_addi_ptr(gptr, cpu_env, gofs);
9e18d7a6 391
392acacc 392 gen_helper_sve_predtest(t, dptr, gptr, tcg_constant_i32(words));
9e18d7a6
RH
393 tcg_temp_free_ptr(dptr);
394 tcg_temp_free_ptr(gptr);
395
396 do_pred_flags(t);
397 tcg_temp_free_i32(t);
398}
399
028e2a7b
RH
400/* For each element size, the bits within a predicate word that are active. */
401const uint64_t pred_esz_masks[4] = {
402 0xffffffffffffffffull, 0x5555555555555555ull,
403 0x1111111111111111ull, 0x0101010101010101ull
404};
405
39eea561
RH
406/*
407 *** SVE Logical - Unpredicated Group
408 */
409
b262215b
RH
410TRANS_FEAT(AND_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_and, a)
411TRANS_FEAT(ORR_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_or, a)
412TRANS_FEAT(EOR_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_xor, a)
413TRANS_FEAT(BIC_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_andc, a)
d1822297 414
e6eba6e5
RH
415static void gen_xar8_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, int64_t sh)
416{
417 TCGv_i64 t = tcg_temp_new_i64();
418 uint64_t mask = dup_const(MO_8, 0xff >> sh);
419
420 tcg_gen_xor_i64(t, n, m);
421 tcg_gen_shri_i64(d, t, sh);
422 tcg_gen_shli_i64(t, t, 8 - sh);
423 tcg_gen_andi_i64(d, d, mask);
424 tcg_gen_andi_i64(t, t, ~mask);
425 tcg_gen_or_i64(d, d, t);
426 tcg_temp_free_i64(t);
427}
428
429static void gen_xar16_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, int64_t sh)
430{
431 TCGv_i64 t = tcg_temp_new_i64();
432 uint64_t mask = dup_const(MO_16, 0xffff >> sh);
433
434 tcg_gen_xor_i64(t, n, m);
435 tcg_gen_shri_i64(d, t, sh);
436 tcg_gen_shli_i64(t, t, 16 - sh);
437 tcg_gen_andi_i64(d, d, mask);
438 tcg_gen_andi_i64(t, t, ~mask);
439 tcg_gen_or_i64(d, d, t);
440 tcg_temp_free_i64(t);
441}
442
443static void gen_xar_i32(TCGv_i32 d, TCGv_i32 n, TCGv_i32 m, int32_t sh)
444{
445 tcg_gen_xor_i32(d, n, m);
446 tcg_gen_rotri_i32(d, d, sh);
447}
448
449static void gen_xar_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, int64_t sh)
450{
451 tcg_gen_xor_i64(d, n, m);
452 tcg_gen_rotri_i64(d, d, sh);
453}
454
455static void gen_xar_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
456 TCGv_vec m, int64_t sh)
457{
458 tcg_gen_xor_vec(vece, d, n, m);
459 tcg_gen_rotri_vec(vece, d, d, sh);
460}
461
462void gen_gvec_xar(unsigned vece, uint32_t rd_ofs, uint32_t rn_ofs,
463 uint32_t rm_ofs, int64_t shift,
464 uint32_t opr_sz, uint32_t max_sz)
465{
466 static const TCGOpcode vecop[] = { INDEX_op_rotli_vec, 0 };
467 static const GVecGen3i ops[4] = {
468 { .fni8 = gen_xar8_i64,
469 .fniv = gen_xar_vec,
470 .fno = gen_helper_sve2_xar_b,
471 .opt_opc = vecop,
472 .vece = MO_8 },
473 { .fni8 = gen_xar16_i64,
474 .fniv = gen_xar_vec,
475 .fno = gen_helper_sve2_xar_h,
476 .opt_opc = vecop,
477 .vece = MO_16 },
478 { .fni4 = gen_xar_i32,
479 .fniv = gen_xar_vec,
480 .fno = gen_helper_sve2_xar_s,
481 .opt_opc = vecop,
482 .vece = MO_32 },
483 { .fni8 = gen_xar_i64,
484 .fniv = gen_xar_vec,
485 .fno = gen_helper_gvec_xar_d,
486 .opt_opc = vecop,
487 .vece = MO_64 }
488 };
489 int esize = 8 << vece;
490
491 /* The SVE2 range is 1 .. esize; the AdvSIMD range is 0 .. esize-1. */
492 tcg_debug_assert(shift >= 0);
493 tcg_debug_assert(shift <= esize);
494 shift &= esize - 1;
495
496 if (shift == 0) {
497 /* xar with no rotate devolves to xor. */
498 tcg_gen_gvec_xor(vece, rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz);
499 } else {
500 tcg_gen_gvec_3i(rd_ofs, rn_ofs, rm_ofs, opr_sz, max_sz,
501 shift, &ops[vece]);
502 }
503}
504
505static bool trans_XAR(DisasContext *s, arg_rrri_esz *a)
506{
507 if (a->esz < 0 || !dc_isar_feature(aa64_sve2, s)) {
508 return false;
509 }
510 if (sve_access_check(s)) {
511 unsigned vsz = vec_full_reg_size(s);
512 gen_gvec_xar(a->esz, vec_full_reg_offset(s, a->rd),
513 vec_full_reg_offset(s, a->rn),
514 vec_full_reg_offset(s, a->rm), a->imm, vsz, vsz);
515 }
516 return true;
517}
518
911cdc6d
RH
519static void gen_eor3_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_i64 k)
520{
521 tcg_gen_xor_i64(d, n, m);
522 tcg_gen_xor_i64(d, d, k);
523}
524
525static void gen_eor3_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
526 TCGv_vec m, TCGv_vec k)
527{
528 tcg_gen_xor_vec(vece, d, n, m);
529 tcg_gen_xor_vec(vece, d, d, k);
530}
531
532static void gen_eor3(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
533 uint32_t a, uint32_t oprsz, uint32_t maxsz)
534{
535 static const GVecGen4 op = {
536 .fni8 = gen_eor3_i64,
537 .fniv = gen_eor3_vec,
538 .fno = gen_helper_sve2_eor3,
539 .vece = MO_64,
540 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
541 };
542 tcg_gen_gvec_4(d, n, m, a, oprsz, maxsz, &op);
543}
544
b773a5c8 545TRANS_FEAT(EOR3, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_eor3, a)
911cdc6d
RH
546
547static void gen_bcax_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_i64 k)
548{
549 tcg_gen_andc_i64(d, m, k);
550 tcg_gen_xor_i64(d, d, n);
551}
552
553static void gen_bcax_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
554 TCGv_vec m, TCGv_vec k)
555{
556 tcg_gen_andc_vec(vece, d, m, k);
557 tcg_gen_xor_vec(vece, d, d, n);
558}
559
560static void gen_bcax(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
561 uint32_t a, uint32_t oprsz, uint32_t maxsz)
562{
563 static const GVecGen4 op = {
564 .fni8 = gen_bcax_i64,
565 .fniv = gen_bcax_vec,
566 .fno = gen_helper_sve2_bcax,
567 .vece = MO_64,
568 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
569 };
570 tcg_gen_gvec_4(d, n, m, a, oprsz, maxsz, &op);
571}
572
b773a5c8 573TRANS_FEAT(BCAX, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_bcax, a)
911cdc6d
RH
574
575static void gen_bsl(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
576 uint32_t a, uint32_t oprsz, uint32_t maxsz)
577{
578 /* BSL differs from the generic bitsel in argument ordering. */
579 tcg_gen_gvec_bitsel(vece, d, a, n, m, oprsz, maxsz);
580}
581
b773a5c8 582TRANS_FEAT(BSL, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_bsl, a)
911cdc6d
RH
583
584static void gen_bsl1n_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_i64 k)
585{
586 tcg_gen_andc_i64(n, k, n);
587 tcg_gen_andc_i64(m, m, k);
588 tcg_gen_or_i64(d, n, m);
589}
590
591static void gen_bsl1n_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
592 TCGv_vec m, TCGv_vec k)
593{
594 if (TCG_TARGET_HAS_bitsel_vec) {
595 tcg_gen_not_vec(vece, n, n);
596 tcg_gen_bitsel_vec(vece, d, k, n, m);
597 } else {
598 tcg_gen_andc_vec(vece, n, k, n);
599 tcg_gen_andc_vec(vece, m, m, k);
600 tcg_gen_or_vec(vece, d, n, m);
601 }
602}
603
604static void gen_bsl1n(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
605 uint32_t a, uint32_t oprsz, uint32_t maxsz)
606{
607 static const GVecGen4 op = {
608 .fni8 = gen_bsl1n_i64,
609 .fniv = gen_bsl1n_vec,
610 .fno = gen_helper_sve2_bsl1n,
611 .vece = MO_64,
612 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
613 };
614 tcg_gen_gvec_4(d, n, m, a, oprsz, maxsz, &op);
615}
616
b773a5c8 617TRANS_FEAT(BSL1N, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_bsl1n, a)
911cdc6d
RH
618
619static void gen_bsl2n_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_i64 k)
620{
621 /*
622 * Z[dn] = (n & k) | (~m & ~k)
623 * = | ~(m | k)
624 */
625 tcg_gen_and_i64(n, n, k);
626 if (TCG_TARGET_HAS_orc_i64) {
627 tcg_gen_or_i64(m, m, k);
628 tcg_gen_orc_i64(d, n, m);
629 } else {
630 tcg_gen_nor_i64(m, m, k);
631 tcg_gen_or_i64(d, n, m);
632 }
633}
634
635static void gen_bsl2n_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
636 TCGv_vec m, TCGv_vec k)
637{
638 if (TCG_TARGET_HAS_bitsel_vec) {
639 tcg_gen_not_vec(vece, m, m);
640 tcg_gen_bitsel_vec(vece, d, k, n, m);
641 } else {
642 tcg_gen_and_vec(vece, n, n, k);
643 tcg_gen_or_vec(vece, m, m, k);
644 tcg_gen_orc_vec(vece, d, n, m);
645 }
646}
647
648static void gen_bsl2n(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
649 uint32_t a, uint32_t oprsz, uint32_t maxsz)
650{
651 static const GVecGen4 op = {
652 .fni8 = gen_bsl2n_i64,
653 .fniv = gen_bsl2n_vec,
654 .fno = gen_helper_sve2_bsl2n,
655 .vece = MO_64,
656 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
657 };
658 tcg_gen_gvec_4(d, n, m, a, oprsz, maxsz, &op);
659}
660
b773a5c8 661TRANS_FEAT(BSL2N, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_bsl2n, a)
911cdc6d
RH
662
663static void gen_nbsl_i64(TCGv_i64 d, TCGv_i64 n, TCGv_i64 m, TCGv_i64 k)
664{
665 tcg_gen_and_i64(n, n, k);
666 tcg_gen_andc_i64(m, m, k);
667 tcg_gen_nor_i64(d, n, m);
668}
669
670static void gen_nbsl_vec(unsigned vece, TCGv_vec d, TCGv_vec n,
671 TCGv_vec m, TCGv_vec k)
672{
673 tcg_gen_bitsel_vec(vece, d, k, n, m);
674 tcg_gen_not_vec(vece, d, d);
675}
676
677static void gen_nbsl(unsigned vece, uint32_t d, uint32_t n, uint32_t m,
678 uint32_t a, uint32_t oprsz, uint32_t maxsz)
679{
680 static const GVecGen4 op = {
681 .fni8 = gen_nbsl_i64,
682 .fniv = gen_nbsl_vec,
683 .fno = gen_helper_sve2_nbsl,
684 .vece = MO_64,
685 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
686 };
687 tcg_gen_gvec_4(d, n, m, a, oprsz, maxsz, &op);
688}
689
b773a5c8 690TRANS_FEAT(NBSL, aa64_sve2, gen_gvec_fn_arg_zzzz, gen_nbsl, a)
911cdc6d 691
fea98f9c
RH
692/*
693 *** SVE Integer Arithmetic - Unpredicated Group
694 */
695
b262215b
RH
696TRANS_FEAT(ADD_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_add, a)
697TRANS_FEAT(SUB_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_sub, a)
698TRANS_FEAT(SQADD_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_ssadd, a)
699TRANS_FEAT(SQSUB_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_sssub, a)
700TRANS_FEAT(UQADD_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_usadd, a)
701TRANS_FEAT(UQSUB_zzz, aa64_sve, gen_gvec_fn_arg_zzz, tcg_gen_gvec_ussub, a)
fea98f9c 702
f97cfd59
RH
703/*
704 *** SVE Integer Arithmetic - Binary Predicated Group
705 */
706
a2103582
RH
707/* Select active elememnts from Zn and inactive elements from Zm,
708 * storing the result in Zd.
709 */
68cc4ee3 710static bool do_sel_z(DisasContext *s, int rd, int rn, int rm, int pg, int esz)
a2103582
RH
711{
712 static gen_helper_gvec_4 * const fns[4] = {
713 gen_helper_sve_sel_zpzz_b, gen_helper_sve_sel_zpzz_h,
714 gen_helper_sve_sel_zpzz_s, gen_helper_sve_sel_zpzz_d
715 };
68cc4ee3 716 return gen_gvec_ool_zzzp(s, fns[esz], rd, rn, rm, pg, 0);
a2103582
RH
717}
718
8e7acb24
RH
719#define DO_ZPZZ(NAME, FEAT, name) \
720 static gen_helper_gvec_4 * const name##_zpzz_fns[4] = { \
721 gen_helper_##name##_zpzz_b, gen_helper_##name##_zpzz_h, \
722 gen_helper_##name##_zpzz_s, gen_helper_##name##_zpzz_d, \
f97cfd59 723 }; \
8e7acb24
RH
724 TRANS_FEAT(NAME, FEAT, gen_gvec_ool_arg_zpzz, \
725 name##_zpzz_fns[a->esz], a, 0)
726
727DO_ZPZZ(AND_zpzz, aa64_sve, sve_and)
728DO_ZPZZ(EOR_zpzz, aa64_sve, sve_eor)
729DO_ZPZZ(ORR_zpzz, aa64_sve, sve_orr)
730DO_ZPZZ(BIC_zpzz, aa64_sve, sve_bic)
731
732DO_ZPZZ(ADD_zpzz, aa64_sve, sve_add)
733DO_ZPZZ(SUB_zpzz, aa64_sve, sve_sub)
734
735DO_ZPZZ(SMAX_zpzz, aa64_sve, sve_smax)
736DO_ZPZZ(UMAX_zpzz, aa64_sve, sve_umax)
737DO_ZPZZ(SMIN_zpzz, aa64_sve, sve_smin)
738DO_ZPZZ(UMIN_zpzz, aa64_sve, sve_umin)
739DO_ZPZZ(SABD_zpzz, aa64_sve, sve_sabd)
740DO_ZPZZ(UABD_zpzz, aa64_sve, sve_uabd)
741
742DO_ZPZZ(MUL_zpzz, aa64_sve, sve_mul)
743DO_ZPZZ(SMULH_zpzz, aa64_sve, sve_smulh)
744DO_ZPZZ(UMULH_zpzz, aa64_sve, sve_umulh)
745
746DO_ZPZZ(ASR_zpzz, aa64_sve, sve_asr)
747DO_ZPZZ(LSR_zpzz, aa64_sve, sve_lsr)
748DO_ZPZZ(LSL_zpzz, aa64_sve, sve_lsl)
749
750static gen_helper_gvec_4 * const sdiv_fns[4] = {
751 NULL, NULL, gen_helper_sve_sdiv_zpzz_s, gen_helper_sve_sdiv_zpzz_d
752};
753TRANS_FEAT(SDIV_zpzz, aa64_sve, gen_gvec_ool_arg_zpzz, sdiv_fns[a->esz], a, 0)
f97cfd59 754
8e7acb24
RH
755static gen_helper_gvec_4 * const udiv_fns[4] = {
756 NULL, NULL, gen_helper_sve_udiv_zpzz_s, gen_helper_sve_udiv_zpzz_d
757};
758TRANS_FEAT(UDIV_zpzz, aa64_sve, gen_gvec_ool_arg_zpzz, udiv_fns[a->esz], a, 0)
f97cfd59 759
3a7be554 760static bool trans_SEL_zpzz(DisasContext *s, arg_rprr_esz *a)
a2103582 761{
68cc4ee3 762 return do_sel_z(s, a->rd, a->rn, a->rm, a->pg, a->esz);
a2103582 763}
d3fe4a29 764
afac6d04
RH
765/*
766 *** SVE Integer Arithmetic - Unary Predicated Group
767 */
768
817bd5c9
RH
769#define DO_ZPZ(NAME, FEAT, name) \
770 static gen_helper_gvec_3 * const name##_fns[4] = { \
771 gen_helper_##name##_b, gen_helper_##name##_h, \
772 gen_helper_##name##_s, gen_helper_##name##_d, \
afac6d04 773 }; \
817bd5c9
RH
774 TRANS_FEAT(NAME, FEAT, gen_gvec_ool_arg_zpz, name##_fns[a->esz], a, 0)
775
776DO_ZPZ(CLS, aa64_sve, sve_cls)
777DO_ZPZ(CLZ, aa64_sve, sve_clz)
778DO_ZPZ(CNT_zpz, aa64_sve, sve_cnt_zpz)
779DO_ZPZ(CNOT, aa64_sve, sve_cnot)
780DO_ZPZ(NOT_zpz, aa64_sve, sve_not_zpz)
781DO_ZPZ(ABS, aa64_sve, sve_abs)
782DO_ZPZ(NEG, aa64_sve, sve_neg)
783DO_ZPZ(RBIT, aa64_sve, sve_rbit)
784
785static gen_helper_gvec_3 * const fabs_fns[4] = {
786 NULL, gen_helper_sve_fabs_h,
787 gen_helper_sve_fabs_s, gen_helper_sve_fabs_d,
788};
789TRANS_FEAT(FABS, aa64_sve, gen_gvec_ool_arg_zpz, fabs_fns[a->esz], a, 0)
afac6d04 790
817bd5c9
RH
791static gen_helper_gvec_3 * const fneg_fns[4] = {
792 NULL, gen_helper_sve_fneg_h,
793 gen_helper_sve_fneg_s, gen_helper_sve_fneg_d,
794};
795TRANS_FEAT(FNEG, aa64_sve, gen_gvec_ool_arg_zpz, fneg_fns[a->esz], a, 0)
afac6d04 796
817bd5c9
RH
797static gen_helper_gvec_3 * const sxtb_fns[4] = {
798 NULL, gen_helper_sve_sxtb_h,
799 gen_helper_sve_sxtb_s, gen_helper_sve_sxtb_d,
800};
801TRANS_FEAT(SXTB, aa64_sve, gen_gvec_ool_arg_zpz, sxtb_fns[a->esz], a, 0)
afac6d04 802
817bd5c9
RH
803static gen_helper_gvec_3 * const uxtb_fns[4] = {
804 NULL, gen_helper_sve_uxtb_h,
805 gen_helper_sve_uxtb_s, gen_helper_sve_uxtb_d,
806};
807TRANS_FEAT(UXTB, aa64_sve, gen_gvec_ool_arg_zpz, uxtb_fns[a->esz], a, 0)
afac6d04 808
817bd5c9
RH
809static gen_helper_gvec_3 * const sxth_fns[4] = {
810 NULL, NULL, gen_helper_sve_sxth_s, gen_helper_sve_sxth_d
811};
812TRANS_FEAT(SXTH, aa64_sve, gen_gvec_ool_arg_zpz, sxth_fns[a->esz], a, 0)
afac6d04 813
817bd5c9
RH
814static gen_helper_gvec_3 * const uxth_fns[4] = {
815 NULL, NULL, gen_helper_sve_uxth_s, gen_helper_sve_uxth_d
816};
817TRANS_FEAT(UXTH, aa64_sve, gen_gvec_ool_arg_zpz, uxth_fns[a->esz], a, 0)
afac6d04 818
817bd5c9
RH
819TRANS_FEAT(SXTW, aa64_sve, gen_gvec_ool_arg_zpz,
820 a->esz == 3 ? gen_helper_sve_sxtw_d : NULL, a, 0)
821TRANS_FEAT(UXTW, aa64_sve, gen_gvec_ool_arg_zpz,
822 a->esz == 3 ? gen_helper_sve_uxtw_d : NULL, a, 0)
afac6d04 823
047cec97
RH
824/*
825 *** SVE Integer Reduction Group
826 */
827
828typedef void gen_helper_gvec_reduc(TCGv_i64, TCGv_ptr, TCGv_ptr, TCGv_i32);
829static bool do_vpz_ool(DisasContext *s, arg_rpr_esz *a,
830 gen_helper_gvec_reduc *fn)
831{
832 unsigned vsz = vec_full_reg_size(s);
833 TCGv_ptr t_zn, t_pg;
834 TCGv_i32 desc;
835 TCGv_i64 temp;
836
837 if (fn == NULL) {
838 return false;
839 }
840 if (!sve_access_check(s)) {
841 return true;
842 }
843
c6a59b55 844 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
047cec97
RH
845 temp = tcg_temp_new_i64();
846 t_zn = tcg_temp_new_ptr();
847 t_pg = tcg_temp_new_ptr();
848
849 tcg_gen_addi_ptr(t_zn, cpu_env, vec_full_reg_offset(s, a->rn));
850 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, a->pg));
851 fn(temp, t_zn, t_pg, desc);
852 tcg_temp_free_ptr(t_zn);
853 tcg_temp_free_ptr(t_pg);
047cec97
RH
854
855 write_fp_dreg(s, a->rd, temp);
856 tcg_temp_free_i64(temp);
857 return true;
858}
859
860#define DO_VPZ(NAME, name) \
9ac24f1f 861 static gen_helper_gvec_reduc * const name##_fns[4] = { \
047cec97
RH
862 gen_helper_sve_##name##_b, gen_helper_sve_##name##_h, \
863 gen_helper_sve_##name##_s, gen_helper_sve_##name##_d, \
864 }; \
9ac24f1f 865 TRANS_FEAT(NAME, aa64_sve, do_vpz_ool, a, name##_fns[a->esz])
047cec97
RH
866
867DO_VPZ(ORV, orv)
868DO_VPZ(ANDV, andv)
869DO_VPZ(EORV, eorv)
870
871DO_VPZ(UADDV, uaddv)
872DO_VPZ(SMAXV, smaxv)
873DO_VPZ(UMAXV, umaxv)
874DO_VPZ(SMINV, sminv)
875DO_VPZ(UMINV, uminv)
876
9ac24f1f
RH
877static gen_helper_gvec_reduc * const saddv_fns[4] = {
878 gen_helper_sve_saddv_b, gen_helper_sve_saddv_h,
879 gen_helper_sve_saddv_s, NULL
880};
881TRANS_FEAT(SADDV, aa64_sve, do_vpz_ool, a, saddv_fns[a->esz])
047cec97
RH
882
883#undef DO_VPZ
884
ccd841c3
RH
885/*
886 *** SVE Shift by Immediate - Predicated Group
887 */
888
60245996
RH
889/*
890 * Copy Zn into Zd, storing zeros into inactive elements.
891 * If invert, store zeros into the active elements.
ccd841c3 892 */
60245996
RH
893static bool do_movz_zpz(DisasContext *s, int rd, int rn, int pg,
894 int esz, bool invert)
ccd841c3 895{
60245996
RH
896 static gen_helper_gvec_3 * const fns[4] = {
897 gen_helper_sve_movz_b, gen_helper_sve_movz_h,
898 gen_helper_sve_movz_s, gen_helper_sve_movz_d,
ccd841c3 899 };
8fb27a21 900 return gen_gvec_ool_zzp(s, fns[esz], rd, rn, pg, invert);
ccd841c3
RH
901}
902
73c558a8
RH
903static bool do_shift_zpzi(DisasContext *s, arg_rpri_esz *a, bool asr,
904 gen_helper_gvec_3 * const fns[4])
ccd841c3 905{
73c558a8
RH
906 int max;
907
ccd841c3
RH
908 if (a->esz < 0) {
909 /* Invalid tsz encoding -- see tszimm_esz. */
910 return false;
911 }
73c558a8
RH
912
913 /*
914 * Shift by element size is architecturally valid.
915 * For arithmetic right-shift, it's the same as by one less.
916 * For logical shifts and ASRD, it is a zeroing operation.
917 */
918 max = 8 << a->esz;
919 if (a->imm >= max) {
920 if (asr) {
921 a->imm = max - 1;
922 } else {
923 return do_movz_zpz(s, a->rd, a->rd, a->pg, a->esz, true);
924 }
925 }
afa2529c 926 return gen_gvec_ool_arg_zpzi(s, fns[a->esz], a);
ccd841c3
RH
927}
928
5cccd1f1
RH
929static gen_helper_gvec_3 * const asr_zpzi_fns[4] = {
930 gen_helper_sve_asr_zpzi_b, gen_helper_sve_asr_zpzi_h,
931 gen_helper_sve_asr_zpzi_s, gen_helper_sve_asr_zpzi_d,
932};
933TRANS_FEAT(ASR_zpzi, aa64_sve, do_shift_zpzi, a, true, asr_zpzi_fns)
73c558a8 934
5cccd1f1
RH
935static gen_helper_gvec_3 * const lsr_zpzi_fns[4] = {
936 gen_helper_sve_lsr_zpzi_b, gen_helper_sve_lsr_zpzi_h,
937 gen_helper_sve_lsr_zpzi_s, gen_helper_sve_lsr_zpzi_d,
938};
939TRANS_FEAT(LSR_zpzi, aa64_sve, do_shift_zpzi, a, false, lsr_zpzi_fns)
ccd841c3 940
5cccd1f1
RH
941static gen_helper_gvec_3 * const lsl_zpzi_fns[4] = {
942 gen_helper_sve_lsl_zpzi_b, gen_helper_sve_lsl_zpzi_h,
943 gen_helper_sve_lsl_zpzi_s, gen_helper_sve_lsl_zpzi_d,
944};
945TRANS_FEAT(LSL_zpzi, aa64_sve, do_shift_zpzi, a, false, lsl_zpzi_fns)
ccd841c3 946
5cccd1f1
RH
947static gen_helper_gvec_3 * const asrd_fns[4] = {
948 gen_helper_sve_asrd_b, gen_helper_sve_asrd_h,
949 gen_helper_sve_asrd_s, gen_helper_sve_asrd_d,
950};
951TRANS_FEAT(ASRD, aa64_sve, do_shift_zpzi, a, false, asrd_fns)
ccd841c3 952
4df37e41
RH
953static gen_helper_gvec_3 * const sqshl_zpzi_fns[4] = {
954 gen_helper_sve2_sqshl_zpzi_b, gen_helper_sve2_sqshl_zpzi_h,
955 gen_helper_sve2_sqshl_zpzi_s, gen_helper_sve2_sqshl_zpzi_d,
956};
957TRANS_FEAT(SQSHL_zpzi, aa64_sve2, gen_gvec_ool_arg_zpzi,
958 a->esz < 0 ? NULL : sqshl_zpzi_fns[a->esz], a)
a5421b54 959
4df37e41
RH
960static gen_helper_gvec_3 * const uqshl_zpzi_fns[4] = {
961 gen_helper_sve2_uqshl_zpzi_b, gen_helper_sve2_uqshl_zpzi_h,
962 gen_helper_sve2_uqshl_zpzi_s, gen_helper_sve2_uqshl_zpzi_d,
963};
964TRANS_FEAT(UQSHL_zpzi, aa64_sve2, gen_gvec_ool_arg_zpzi,
965 a->esz < 0 ? NULL : uqshl_zpzi_fns[a->esz], a)
a5421b54 966
4df37e41
RH
967static gen_helper_gvec_3 * const srshr_fns[4] = {
968 gen_helper_sve2_srshr_b, gen_helper_sve2_srshr_h,
969 gen_helper_sve2_srshr_s, gen_helper_sve2_srshr_d,
970};
971TRANS_FEAT(SRSHR, aa64_sve2, gen_gvec_ool_arg_zpzi,
972 a->esz < 0 ? NULL : srshr_fns[a->esz], a)
a5421b54 973
4df37e41
RH
974static gen_helper_gvec_3 * const urshr_fns[4] = {
975 gen_helper_sve2_urshr_b, gen_helper_sve2_urshr_h,
976 gen_helper_sve2_urshr_s, gen_helper_sve2_urshr_d,
977};
978TRANS_FEAT(URSHR, aa64_sve2, gen_gvec_ool_arg_zpzi,
979 a->esz < 0 ? NULL : urshr_fns[a->esz], a)
a5421b54 980
4df37e41
RH
981static gen_helper_gvec_3 * const sqshlu_fns[4] = {
982 gen_helper_sve2_sqshlu_b, gen_helper_sve2_sqshlu_h,
983 gen_helper_sve2_sqshlu_s, gen_helper_sve2_sqshlu_d,
984};
985TRANS_FEAT(SQSHLU, aa64_sve2, gen_gvec_ool_arg_zpzi,
986 a->esz < 0 ? NULL : sqshlu_fns[a->esz], a)
a5421b54 987
fe7f8dfb
RH
988/*
989 *** SVE Bitwise Shift - Predicated Group
990 */
991
992#define DO_ZPZW(NAME, name) \
8e7acb24 993 static gen_helper_gvec_4 * const name##_zpzw_fns[4] = { \
fe7f8dfb 994 gen_helper_sve_##name##_zpzw_b, gen_helper_sve_##name##_zpzw_h, \
8e7acb24 995 gen_helper_sve_##name##_zpzw_s, NULL \
fe7f8dfb 996 }; \
8e7acb24
RH
997 TRANS_FEAT(NAME##_zpzw, aa64_sve, gen_gvec_ool_arg_zpzz, \
998 a->esz < 0 ? NULL : name##_zpzw_fns[a->esz], a, 0)
fe7f8dfb
RH
999
1000DO_ZPZW(ASR, asr)
1001DO_ZPZW(LSR, lsr)
1002DO_ZPZW(LSL, lsl)
1003
1004#undef DO_ZPZW
1005
d9d78dcc
RH
1006/*
1007 *** SVE Bitwise Shift - Unpredicated Group
1008 */
1009
1010static bool do_shift_imm(DisasContext *s, arg_rri_esz *a, bool asr,
1011 void (*gvec_fn)(unsigned, uint32_t, uint32_t,
1012 int64_t, uint32_t, uint32_t))
1013{
1014 if (a->esz < 0) {
1015 /* Invalid tsz encoding -- see tszimm_esz. */
1016 return false;
1017 }
1018 if (sve_access_check(s)) {
1019 unsigned vsz = vec_full_reg_size(s);
1020 /* Shift by element size is architecturally valid. For
1021 arithmetic right-shift, it's the same as by one less.
1022 Otherwise it is a zeroing operation. */
1023 if (a->imm >= 8 << a->esz) {
1024 if (asr) {
1025 a->imm = (8 << a->esz) - 1;
1026 } else {
1027 do_dupi_z(s, a->rd, 0);
1028 return true;
1029 }
1030 }
1031 gvec_fn(a->esz, vec_full_reg_offset(s, a->rd),
1032 vec_full_reg_offset(s, a->rn), a->imm, vsz, vsz);
1033 }
1034 return true;
1035}
1036
5e612f80
RH
1037TRANS_FEAT(ASR_zzi, aa64_sve, do_shift_imm, a, true, tcg_gen_gvec_sari)
1038TRANS_FEAT(LSR_zzi, aa64_sve, do_shift_imm, a, false, tcg_gen_gvec_shri)
1039TRANS_FEAT(LSL_zzi, aa64_sve, do_shift_imm, a, false, tcg_gen_gvec_shli)
d9d78dcc 1040
d9d78dcc 1041#define DO_ZZW(NAME, name) \
32e2ad65 1042 static gen_helper_gvec_3 * const name##_zzw_fns[4] = { \
d9d78dcc
RH
1043 gen_helper_sve_##name##_zzw_b, gen_helper_sve_##name##_zzw_h, \
1044 gen_helper_sve_##name##_zzw_s, NULL \
1045 }; \
32e2ad65
RH
1046 TRANS_FEAT(NAME, aa64_sve, gen_gvec_ool_arg_zzz, \
1047 name##_zzw_fns[a->esz], a, 0)
d9d78dcc 1048
32e2ad65
RH
1049DO_ZZW(ASR_zzw, asr)
1050DO_ZZW(LSR_zzw, lsr)
1051DO_ZZW(LSL_zzw, lsl)
d9d78dcc
RH
1052
1053#undef DO_ZZW
1054
96a36e4a
RH
1055/*
1056 *** SVE Integer Multiply-Add Group
1057 */
1058
1059static bool do_zpzzz_ool(DisasContext *s, arg_rprrr_esz *a,
1060 gen_helper_gvec_5 *fn)
1061{
1062 if (sve_access_check(s)) {
1063 unsigned vsz = vec_full_reg_size(s);
1064 tcg_gen_gvec_5_ool(vec_full_reg_offset(s, a->rd),
1065 vec_full_reg_offset(s, a->ra),
1066 vec_full_reg_offset(s, a->rn),
1067 vec_full_reg_offset(s, a->rm),
1068 pred_full_reg_offset(s, a->pg),
1069 vsz, vsz, 0, fn);
1070 }
1071 return true;
1072}
1073
dc67e645
RH
1074static gen_helper_gvec_5 * const mla_fns[4] = {
1075 gen_helper_sve_mla_b, gen_helper_sve_mla_h,
1076 gen_helper_sve_mla_s, gen_helper_sve_mla_d,
1077};
1078TRANS_FEAT(MLA, aa64_sve, do_zpzzz_ool, a, mla_fns[a->esz])
96a36e4a 1079
dc67e645
RH
1080static gen_helper_gvec_5 * const mls_fns[4] = {
1081 gen_helper_sve_mls_b, gen_helper_sve_mls_h,
1082 gen_helper_sve_mls_s, gen_helper_sve_mls_d,
1083};
1084TRANS_FEAT(MLS, aa64_sve, do_zpzzz_ool, a, mls_fns[a->esz])
96a36e4a 1085
9a56c9c3
RH
1086/*
1087 *** SVE Index Generation Group
1088 */
1089
6687d05d 1090static bool do_index(DisasContext *s, int esz, int rd,
9a56c9c3
RH
1091 TCGv_i64 start, TCGv_i64 incr)
1092{
6687d05d
RH
1093 unsigned vsz;
1094 TCGv_i32 desc;
1095 TCGv_ptr t_zd;
1096
1097 if (!sve_access_check(s)) {
1098 return true;
1099 }
1100
1101 vsz = vec_full_reg_size(s);
1102 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
1103 t_zd = tcg_temp_new_ptr();
9a56c9c3
RH
1104
1105 tcg_gen_addi_ptr(t_zd, cpu_env, vec_full_reg_offset(s, rd));
1106 if (esz == 3) {
1107 gen_helper_sve_index_d(t_zd, start, incr, desc);
1108 } else {
1109 typedef void index_fn(TCGv_ptr, TCGv_i32, TCGv_i32, TCGv_i32);
1110 static index_fn * const fns[3] = {
1111 gen_helper_sve_index_b,
1112 gen_helper_sve_index_h,
1113 gen_helper_sve_index_s,
1114 };
1115 TCGv_i32 s32 = tcg_temp_new_i32();
1116 TCGv_i32 i32 = tcg_temp_new_i32();
1117
1118 tcg_gen_extrl_i64_i32(s32, start);
1119 tcg_gen_extrl_i64_i32(i32, incr);
1120 fns[esz](t_zd, s32, i32, desc);
1121
1122 tcg_temp_free_i32(s32);
1123 tcg_temp_free_i32(i32);
1124 }
1125 tcg_temp_free_ptr(t_zd);
6687d05d 1126 return true;
9a56c9c3
RH
1127}
1128
9aa60c83
RH
1129TRANS_FEAT(INDEX_ii, aa64_sve, do_index, a->esz, a->rd,
1130 tcg_constant_i64(a->imm1), tcg_constant_i64(a->imm2))
1131TRANS_FEAT(INDEX_ir, aa64_sve, do_index, a->esz, a->rd,
1132 tcg_constant_i64(a->imm), cpu_reg(s, a->rm))
1133TRANS_FEAT(INDEX_ri, aa64_sve, do_index, a->esz, a->rd,
1134 cpu_reg(s, a->rn), tcg_constant_i64(a->imm))
1135TRANS_FEAT(INDEX_rr, aa64_sve, do_index, a->esz, a->rd,
1136 cpu_reg(s, a->rn), cpu_reg(s, a->rm))
9a56c9c3 1137
96f922cc
RH
1138/*
1139 *** SVE Stack Allocation Group
1140 */
1141
3a7be554 1142static bool trans_ADDVL(DisasContext *s, arg_ADDVL *a)
96f922cc 1143{
5de56742
AC
1144 if (sve_access_check(s)) {
1145 TCGv_i64 rd = cpu_reg_sp(s, a->rd);
1146 TCGv_i64 rn = cpu_reg_sp(s, a->rn);
1147 tcg_gen_addi_i64(rd, rn, a->imm * vec_full_reg_size(s));
1148 }
96f922cc
RH
1149 return true;
1150}
1151
3a7be554 1152static bool trans_ADDPL(DisasContext *s, arg_ADDPL *a)
96f922cc 1153{
5de56742
AC
1154 if (sve_access_check(s)) {
1155 TCGv_i64 rd = cpu_reg_sp(s, a->rd);
1156 TCGv_i64 rn = cpu_reg_sp(s, a->rn);
1157 tcg_gen_addi_i64(rd, rn, a->imm * pred_full_reg_size(s));
1158 }
96f922cc
RH
1159 return true;
1160}
1161
3a7be554 1162static bool trans_RDVL(DisasContext *s, arg_RDVL *a)
96f922cc 1163{
5de56742
AC
1164 if (sve_access_check(s)) {
1165 TCGv_i64 reg = cpu_reg(s, a->rd);
1166 tcg_gen_movi_i64(reg, a->imm * vec_full_reg_size(s));
1167 }
96f922cc
RH
1168 return true;
1169}
1170
4b242d9c
RH
1171/*
1172 *** SVE Compute Vector Address Group
1173 */
1174
1175static bool do_adr(DisasContext *s, arg_rrri *a, gen_helper_gvec_3 *fn)
1176{
913a8a00 1177 return gen_gvec_ool_zzz(s, fn, a->rd, a->rn, a->rm, a->imm);
4b242d9c
RH
1178}
1179
dcba3d67
RH
1180TRANS_FEAT(ADR_p32, aa64_sve, do_adr, a, gen_helper_sve_adr_p32)
1181TRANS_FEAT(ADR_p64, aa64_sve, do_adr, a, gen_helper_sve_adr_p64)
1182TRANS_FEAT(ADR_s32, aa64_sve, do_adr, a, gen_helper_sve_adr_s32)
1183TRANS_FEAT(ADR_u32, aa64_sve, do_adr, a, gen_helper_sve_adr_u32)
4b242d9c 1184
0762cd42
RH
1185/*
1186 *** SVE Integer Misc - Unpredicated Group
1187 */
1188
0ea3cdbf
RH
1189static gen_helper_gvec_2 * const fexpa_fns[4] = {
1190 NULL, gen_helper_sve_fexpa_h,
1191 gen_helper_sve_fexpa_s, gen_helper_sve_fexpa_d,
1192};
1193TRANS_FEAT(FEXPA, aa64_sve, gen_gvec_ool_zz,
1194 fexpa_fns[a->esz], a->rd, a->rn, 0)
0762cd42 1195
32e2ad65
RH
1196static gen_helper_gvec_3 * const ftssel_fns[4] = {
1197 NULL, gen_helper_sve_ftssel_h,
1198 gen_helper_sve_ftssel_s, gen_helper_sve_ftssel_d,
1199};
1200TRANS_FEAT(FTSSEL, aa64_sve, gen_gvec_ool_arg_zzz, ftssel_fns[a->esz], a, 0)
a1f233f2 1201
516e246a
RH
1202/*
1203 *** SVE Predicate Logical Operations Group
1204 */
1205
1206static bool do_pppp_flags(DisasContext *s, arg_rprr_s *a,
1207 const GVecGen4 *gvec_op)
1208{
1209 if (!sve_access_check(s)) {
1210 return true;
1211 }
1212
1213 unsigned psz = pred_gvec_reg_size(s);
1214 int dofs = pred_full_reg_offset(s, a->rd);
1215 int nofs = pred_full_reg_offset(s, a->rn);
1216 int mofs = pred_full_reg_offset(s, a->rm);
1217 int gofs = pred_full_reg_offset(s, a->pg);
1218
dd81a8d7
RH
1219 if (!a->s) {
1220 tcg_gen_gvec_4(dofs, nofs, mofs, gofs, psz, psz, gvec_op);
1221 return true;
1222 }
1223
516e246a
RH
1224 if (psz == 8) {
1225 /* Do the operation and the flags generation in temps. */
1226 TCGv_i64 pd = tcg_temp_new_i64();
1227 TCGv_i64 pn = tcg_temp_new_i64();
1228 TCGv_i64 pm = tcg_temp_new_i64();
1229 TCGv_i64 pg = tcg_temp_new_i64();
1230
1231 tcg_gen_ld_i64(pn, cpu_env, nofs);
1232 tcg_gen_ld_i64(pm, cpu_env, mofs);
1233 tcg_gen_ld_i64(pg, cpu_env, gofs);
1234
1235 gvec_op->fni8(pd, pn, pm, pg);
1236 tcg_gen_st_i64(pd, cpu_env, dofs);
1237
1238 do_predtest1(pd, pg);
1239
1240 tcg_temp_free_i64(pd);
1241 tcg_temp_free_i64(pn);
1242 tcg_temp_free_i64(pm);
1243 tcg_temp_free_i64(pg);
1244 } else {
1245 /* The operation and flags generation is large. The computation
1246 * of the flags depends on the original contents of the guarding
1247 * predicate. If the destination overwrites the guarding predicate,
1248 * then the easiest way to get this right is to save a copy.
1249 */
1250 int tofs = gofs;
1251 if (a->rd == a->pg) {
1252 tofs = offsetof(CPUARMState, vfp.preg_tmp);
1253 tcg_gen_gvec_mov(0, tofs, gofs, psz, psz);
1254 }
1255
1256 tcg_gen_gvec_4(dofs, nofs, mofs, gofs, psz, psz, gvec_op);
1257 do_predtest(s, dofs, tofs, psz / 8);
1258 }
1259 return true;
1260}
1261
1262static void gen_and_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1263{
1264 tcg_gen_and_i64(pd, pn, pm);
1265 tcg_gen_and_i64(pd, pd, pg);
1266}
1267
1268static void gen_and_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1269 TCGv_vec pm, TCGv_vec pg)
1270{
1271 tcg_gen_and_vec(vece, pd, pn, pm);
1272 tcg_gen_and_vec(vece, pd, pd, pg);
1273}
1274
3a7be554 1275static bool trans_AND_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1276{
1277 static const GVecGen4 op = {
1278 .fni8 = gen_and_pg_i64,
1279 .fniv = gen_and_pg_vec,
1280 .fno = gen_helper_sve_and_pppp,
1281 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1282 };
dd81a8d7
RH
1283
1284 if (!a->s) {
1285 if (!sve_access_check(s)) {
1286 return true;
1287 }
1288 if (a->rn == a->rm) {
1289 if (a->pg == a->rn) {
1290 do_mov_p(s, a->rd, a->rn);
1291 } else {
1292 gen_gvec_fn_ppp(s, tcg_gen_gvec_and, a->rd, a->rn, a->pg);
1293 }
1294 return true;
1295 } else if (a->pg == a->rn || a->pg == a->rm) {
1296 gen_gvec_fn_ppp(s, tcg_gen_gvec_and, a->rd, a->rn, a->rm);
1297 return true;
516e246a 1298 }
516e246a 1299 }
dd81a8d7 1300 return do_pppp_flags(s, a, &op);
516e246a
RH
1301}
1302
1303static void gen_bic_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1304{
1305 tcg_gen_andc_i64(pd, pn, pm);
1306 tcg_gen_and_i64(pd, pd, pg);
1307}
1308
1309static void gen_bic_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1310 TCGv_vec pm, TCGv_vec pg)
1311{
1312 tcg_gen_andc_vec(vece, pd, pn, pm);
1313 tcg_gen_and_vec(vece, pd, pd, pg);
1314}
1315
3a7be554 1316static bool trans_BIC_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1317{
1318 static const GVecGen4 op = {
1319 .fni8 = gen_bic_pg_i64,
1320 .fniv = gen_bic_pg_vec,
1321 .fno = gen_helper_sve_bic_pppp,
1322 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1323 };
dd81a8d7
RH
1324
1325 if (!a->s && a->pg == a->rn) {
1326 if (sve_access_check(s)) {
1327 gen_gvec_fn_ppp(s, tcg_gen_gvec_andc, a->rd, a->rn, a->rm);
1328 }
1329 return true;
516e246a 1330 }
dd81a8d7 1331 return do_pppp_flags(s, a, &op);
516e246a
RH
1332}
1333
1334static void gen_eor_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1335{
1336 tcg_gen_xor_i64(pd, pn, pm);
1337 tcg_gen_and_i64(pd, pd, pg);
1338}
1339
1340static void gen_eor_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1341 TCGv_vec pm, TCGv_vec pg)
1342{
1343 tcg_gen_xor_vec(vece, pd, pn, pm);
1344 tcg_gen_and_vec(vece, pd, pd, pg);
1345}
1346
3a7be554 1347static bool trans_EOR_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1348{
1349 static const GVecGen4 op = {
1350 .fni8 = gen_eor_pg_i64,
1351 .fniv = gen_eor_pg_vec,
1352 .fno = gen_helper_sve_eor_pppp,
1353 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1354 };
dd81a8d7 1355 return do_pppp_flags(s, a, &op);
516e246a
RH
1356}
1357
3a7be554 1358static bool trans_SEL_pppp(DisasContext *s, arg_rprr_s *a)
516e246a 1359{
516e246a
RH
1360 if (a->s) {
1361 return false;
516e246a 1362 }
d4bc6232
RH
1363 if (sve_access_check(s)) {
1364 unsigned psz = pred_gvec_reg_size(s);
1365 tcg_gen_gvec_bitsel(MO_8, pred_full_reg_offset(s, a->rd),
1366 pred_full_reg_offset(s, a->pg),
1367 pred_full_reg_offset(s, a->rn),
1368 pred_full_reg_offset(s, a->rm), psz, psz);
1369 }
1370 return true;
516e246a
RH
1371}
1372
1373static void gen_orr_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1374{
1375 tcg_gen_or_i64(pd, pn, pm);
1376 tcg_gen_and_i64(pd, pd, pg);
1377}
1378
1379static void gen_orr_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1380 TCGv_vec pm, TCGv_vec pg)
1381{
1382 tcg_gen_or_vec(vece, pd, pn, pm);
1383 tcg_gen_and_vec(vece, pd, pd, pg);
1384}
1385
3a7be554 1386static bool trans_ORR_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1387{
1388 static const GVecGen4 op = {
1389 .fni8 = gen_orr_pg_i64,
1390 .fniv = gen_orr_pg_vec,
1391 .fno = gen_helper_sve_orr_pppp,
1392 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1393 };
dd81a8d7
RH
1394
1395 if (!a->s && a->pg == a->rn && a->rn == a->rm) {
516e246a 1396 return do_mov_p(s, a->rd, a->rn);
516e246a 1397 }
dd81a8d7 1398 return do_pppp_flags(s, a, &op);
516e246a
RH
1399}
1400
1401static void gen_orn_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1402{
1403 tcg_gen_orc_i64(pd, pn, pm);
1404 tcg_gen_and_i64(pd, pd, pg);
1405}
1406
1407static void gen_orn_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1408 TCGv_vec pm, TCGv_vec pg)
1409{
1410 tcg_gen_orc_vec(vece, pd, pn, pm);
1411 tcg_gen_and_vec(vece, pd, pd, pg);
1412}
1413
3a7be554 1414static bool trans_ORN_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1415{
1416 static const GVecGen4 op = {
1417 .fni8 = gen_orn_pg_i64,
1418 .fniv = gen_orn_pg_vec,
1419 .fno = gen_helper_sve_orn_pppp,
1420 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1421 };
dd81a8d7 1422 return do_pppp_flags(s, a, &op);
516e246a
RH
1423}
1424
1425static void gen_nor_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1426{
1427 tcg_gen_or_i64(pd, pn, pm);
1428 tcg_gen_andc_i64(pd, pg, pd);
1429}
1430
1431static void gen_nor_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1432 TCGv_vec pm, TCGv_vec pg)
1433{
1434 tcg_gen_or_vec(vece, pd, pn, pm);
1435 tcg_gen_andc_vec(vece, pd, pg, pd);
1436}
1437
3a7be554 1438static bool trans_NOR_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1439{
1440 static const GVecGen4 op = {
1441 .fni8 = gen_nor_pg_i64,
1442 .fniv = gen_nor_pg_vec,
1443 .fno = gen_helper_sve_nor_pppp,
1444 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1445 };
dd81a8d7 1446 return do_pppp_flags(s, a, &op);
516e246a
RH
1447}
1448
1449static void gen_nand_pg_i64(TCGv_i64 pd, TCGv_i64 pn, TCGv_i64 pm, TCGv_i64 pg)
1450{
1451 tcg_gen_and_i64(pd, pn, pm);
1452 tcg_gen_andc_i64(pd, pg, pd);
1453}
1454
1455static void gen_nand_pg_vec(unsigned vece, TCGv_vec pd, TCGv_vec pn,
1456 TCGv_vec pm, TCGv_vec pg)
1457{
1458 tcg_gen_and_vec(vece, pd, pn, pm);
1459 tcg_gen_andc_vec(vece, pd, pg, pd);
1460}
1461
3a7be554 1462static bool trans_NAND_pppp(DisasContext *s, arg_rprr_s *a)
516e246a
RH
1463{
1464 static const GVecGen4 op = {
1465 .fni8 = gen_nand_pg_i64,
1466 .fniv = gen_nand_pg_vec,
1467 .fno = gen_helper_sve_nand_pppp,
1468 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
1469 };
dd81a8d7 1470 return do_pppp_flags(s, a, &op);
516e246a
RH
1471}
1472
9e18d7a6
RH
1473/*
1474 *** SVE Predicate Misc Group
1475 */
1476
3a7be554 1477static bool trans_PTEST(DisasContext *s, arg_PTEST *a)
9e18d7a6
RH
1478{
1479 if (sve_access_check(s)) {
1480 int nofs = pred_full_reg_offset(s, a->rn);
1481 int gofs = pred_full_reg_offset(s, a->pg);
1482 int words = DIV_ROUND_UP(pred_full_reg_size(s), 8);
1483
1484 if (words == 1) {
1485 TCGv_i64 pn = tcg_temp_new_i64();
1486 TCGv_i64 pg = tcg_temp_new_i64();
1487
1488 tcg_gen_ld_i64(pn, cpu_env, nofs);
1489 tcg_gen_ld_i64(pg, cpu_env, gofs);
1490 do_predtest1(pn, pg);
1491
1492 tcg_temp_free_i64(pn);
1493 tcg_temp_free_i64(pg);
1494 } else {
1495 do_predtest(s, nofs, gofs, words);
1496 }
1497 }
1498 return true;
1499}
1500
028e2a7b
RH
1501/* See the ARM pseudocode DecodePredCount. */
1502static unsigned decode_pred_count(unsigned fullsz, int pattern, int esz)
1503{
1504 unsigned elements = fullsz >> esz;
1505 unsigned bound;
1506
1507 switch (pattern) {
1508 case 0x0: /* POW2 */
1509 return pow2floor(elements);
1510 case 0x1: /* VL1 */
1511 case 0x2: /* VL2 */
1512 case 0x3: /* VL3 */
1513 case 0x4: /* VL4 */
1514 case 0x5: /* VL5 */
1515 case 0x6: /* VL6 */
1516 case 0x7: /* VL7 */
1517 case 0x8: /* VL8 */
1518 bound = pattern;
1519 break;
1520 case 0x9: /* VL16 */
1521 case 0xa: /* VL32 */
1522 case 0xb: /* VL64 */
1523 case 0xc: /* VL128 */
1524 case 0xd: /* VL256 */
1525 bound = 16 << (pattern - 9);
1526 break;
1527 case 0x1d: /* MUL4 */
1528 return elements - elements % 4;
1529 case 0x1e: /* MUL3 */
1530 return elements - elements % 3;
1531 case 0x1f: /* ALL */
1532 return elements;
1533 default: /* #uimm5 */
1534 return 0;
1535 }
1536 return elements >= bound ? bound : 0;
1537}
1538
1539/* This handles all of the predicate initialization instructions,
1540 * PTRUE, PFALSE, SETFFR. For PFALSE, we will have set PAT == 32
1541 * so that decode_pred_count returns 0. For SETFFR, we will have
1542 * set RD == 16 == FFR.
1543 */
1544static bool do_predset(DisasContext *s, int esz, int rd, int pat, bool setflag)
1545{
1546 if (!sve_access_check(s)) {
1547 return true;
1548 }
1549
1550 unsigned fullsz = vec_full_reg_size(s);
1551 unsigned ofs = pred_full_reg_offset(s, rd);
1552 unsigned numelem, setsz, i;
1553 uint64_t word, lastword;
1554 TCGv_i64 t;
1555
1556 numelem = decode_pred_count(fullsz, pat, esz);
1557
1558 /* Determine what we must store into each bit, and how many. */
1559 if (numelem == 0) {
1560 lastword = word = 0;
1561 setsz = fullsz;
1562 } else {
1563 setsz = numelem << esz;
1564 lastword = word = pred_esz_masks[esz];
1565 if (setsz % 64) {
973558a3 1566 lastword &= MAKE_64BIT_MASK(0, setsz % 64);
028e2a7b
RH
1567 }
1568 }
1569
1570 t = tcg_temp_new_i64();
1571 if (fullsz <= 64) {
1572 tcg_gen_movi_i64(t, lastword);
1573 tcg_gen_st_i64(t, cpu_env, ofs);
1574 goto done;
1575 }
1576
1577 if (word == lastword) {
1578 unsigned maxsz = size_for_gvec(fullsz / 8);
1579 unsigned oprsz = size_for_gvec(setsz / 8);
1580
1581 if (oprsz * 8 == setsz) {
8711e71f 1582 tcg_gen_gvec_dup_imm(MO_64, ofs, oprsz, maxsz, word);
028e2a7b
RH
1583 goto done;
1584 }
028e2a7b
RH
1585 }
1586
1587 setsz /= 8;
1588 fullsz /= 8;
1589
1590 tcg_gen_movi_i64(t, word);
973558a3 1591 for (i = 0; i < QEMU_ALIGN_DOWN(setsz, 8); i += 8) {
028e2a7b
RH
1592 tcg_gen_st_i64(t, cpu_env, ofs + i);
1593 }
1594 if (lastword != word) {
1595 tcg_gen_movi_i64(t, lastword);
1596 tcg_gen_st_i64(t, cpu_env, ofs + i);
1597 i += 8;
1598 }
1599 if (i < fullsz) {
1600 tcg_gen_movi_i64(t, 0);
1601 for (; i < fullsz; i += 8) {
1602 tcg_gen_st_i64(t, cpu_env, ofs + i);
1603 }
1604 }
1605
1606 done:
1607 tcg_temp_free_i64(t);
1608
1609 /* PTRUES */
1610 if (setflag) {
1611 tcg_gen_movi_i32(cpu_NF, -(word != 0));
1612 tcg_gen_movi_i32(cpu_CF, word == 0);
1613 tcg_gen_movi_i32(cpu_VF, 0);
1614 tcg_gen_mov_i32(cpu_ZF, cpu_NF);
1615 }
1616 return true;
1617}
1618
b03a8501 1619TRANS_FEAT(PTRUE, aa64_sve, do_predset, a->esz, a->rd, a->pat, a->s)
028e2a7b 1620
b03a8501
RH
1621/* Note pat == 31 is #all, to set all elements. */
1622TRANS_FEAT(SETFFR, aa64_sve, do_predset, 0, FFR_PRED_NUM, 31, false)
028e2a7b 1623
b03a8501
RH
1624/* Note pat == 32 is #unimp, to set no elements. */
1625TRANS_FEAT(PFALSE, aa64_sve, do_predset, 0, a->rd, 32, false)
028e2a7b 1626
3a7be554 1627static bool trans_RDFFR_p(DisasContext *s, arg_RDFFR_p *a)
028e2a7b
RH
1628{
1629 /* The path through do_pppp_flags is complicated enough to want to avoid
1630 * duplication. Frob the arguments into the form of a predicated AND.
1631 */
1632 arg_rprr_s alt_a = {
1633 .rd = a->rd, .pg = a->pg, .s = a->s,
1634 .rn = FFR_PRED_NUM, .rm = FFR_PRED_NUM,
1635 };
3a7be554 1636 return trans_AND_pppp(s, &alt_a);
028e2a7b
RH
1637}
1638
ff502658
RH
1639TRANS_FEAT(RDFFR, aa64_sve, do_mov_p, a->rd, FFR_PRED_NUM)
1640TRANS_FEAT(WRFFR, aa64_sve, do_mov_p, FFR_PRED_NUM, a->rn)
028e2a7b
RH
1641
1642static bool do_pfirst_pnext(DisasContext *s, arg_rr_esz *a,
1643 void (*gen_fn)(TCGv_i32, TCGv_ptr,
1644 TCGv_ptr, TCGv_i32))
1645{
1646 if (!sve_access_check(s)) {
1647 return true;
1648 }
1649
1650 TCGv_ptr t_pd = tcg_temp_new_ptr();
1651 TCGv_ptr t_pg = tcg_temp_new_ptr();
1652 TCGv_i32 t;
86300b5d 1653 unsigned desc = 0;
028e2a7b 1654
86300b5d
RH
1655 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, pred_full_reg_size(s));
1656 desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
028e2a7b
RH
1657
1658 tcg_gen_addi_ptr(t_pd, cpu_env, pred_full_reg_offset(s, a->rd));
1659 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, a->rn));
392acacc 1660 t = tcg_temp_new_i32();
028e2a7b 1661
392acacc 1662 gen_fn(t, t_pd, t_pg, tcg_constant_i32(desc));
028e2a7b
RH
1663 tcg_temp_free_ptr(t_pd);
1664 tcg_temp_free_ptr(t_pg);
1665
1666 do_pred_flags(t);
1667 tcg_temp_free_i32(t);
1668 return true;
1669}
1670
d95040e3
RH
1671TRANS_FEAT(PFIRST, aa64_sve, do_pfirst_pnext, a, gen_helper_sve_pfirst)
1672TRANS_FEAT(PNEXT, aa64_sve, do_pfirst_pnext, a, gen_helper_sve_pnext)
028e2a7b 1673
24e82e68
RH
1674/*
1675 *** SVE Element Count Group
1676 */
1677
1678/* Perform an inline saturating addition of a 32-bit value within
1679 * a 64-bit register. The second operand is known to be positive,
1680 * which halves the comparisions we must perform to bound the result.
1681 */
1682static void do_sat_addsub_32(TCGv_i64 reg, TCGv_i64 val, bool u, bool d)
1683{
1684 int64_t ibound;
24e82e68
RH
1685
1686 /* Use normal 64-bit arithmetic to detect 32-bit overflow. */
1687 if (u) {
1688 tcg_gen_ext32u_i64(reg, reg);
1689 } else {
1690 tcg_gen_ext32s_i64(reg, reg);
1691 }
1692 if (d) {
1693 tcg_gen_sub_i64(reg, reg, val);
1694 ibound = (u ? 0 : INT32_MIN);
aa5b0b29 1695 tcg_gen_smax_i64(reg, reg, tcg_constant_i64(ibound));
24e82e68
RH
1696 } else {
1697 tcg_gen_add_i64(reg, reg, val);
1698 ibound = (u ? UINT32_MAX : INT32_MAX);
aa5b0b29 1699 tcg_gen_smin_i64(reg, reg, tcg_constant_i64(ibound));
24e82e68 1700 }
24e82e68
RH
1701}
1702
1703/* Similarly with 64-bit values. */
1704static void do_sat_addsub_64(TCGv_i64 reg, TCGv_i64 val, bool u, bool d)
1705{
1706 TCGv_i64 t0 = tcg_temp_new_i64();
24e82e68
RH
1707 TCGv_i64 t2;
1708
1709 if (u) {
1710 if (d) {
1711 tcg_gen_sub_i64(t0, reg, val);
35a1ec8e
PMD
1712 t2 = tcg_constant_i64(0);
1713 tcg_gen_movcond_i64(TCG_COND_LTU, reg, reg, val, t2, t0);
24e82e68
RH
1714 } else {
1715 tcg_gen_add_i64(t0, reg, val);
35a1ec8e
PMD
1716 t2 = tcg_constant_i64(-1);
1717 tcg_gen_movcond_i64(TCG_COND_LTU, reg, t0, reg, t2, t0);
24e82e68
RH
1718 }
1719 } else {
35a1ec8e 1720 TCGv_i64 t1 = tcg_temp_new_i64();
24e82e68
RH
1721 if (d) {
1722 /* Detect signed overflow for subtraction. */
1723 tcg_gen_xor_i64(t0, reg, val);
1724 tcg_gen_sub_i64(t1, reg, val);
7a31e0c6 1725 tcg_gen_xor_i64(reg, reg, t1);
24e82e68
RH
1726 tcg_gen_and_i64(t0, t0, reg);
1727
1728 /* Bound the result. */
1729 tcg_gen_movi_i64(reg, INT64_MIN);
35a1ec8e 1730 t2 = tcg_constant_i64(0);
24e82e68
RH
1731 tcg_gen_movcond_i64(TCG_COND_LT, reg, t0, t2, reg, t1);
1732 } else {
1733 /* Detect signed overflow for addition. */
1734 tcg_gen_xor_i64(t0, reg, val);
1735 tcg_gen_add_i64(reg, reg, val);
1736 tcg_gen_xor_i64(t1, reg, val);
1737 tcg_gen_andc_i64(t0, t1, t0);
1738
1739 /* Bound the result. */
1740 tcg_gen_movi_i64(t1, INT64_MAX);
35a1ec8e 1741 t2 = tcg_constant_i64(0);
24e82e68
RH
1742 tcg_gen_movcond_i64(TCG_COND_LT, reg, t0, t2, t1, reg);
1743 }
35a1ec8e 1744 tcg_temp_free_i64(t1);
24e82e68
RH
1745 }
1746 tcg_temp_free_i64(t0);
24e82e68
RH
1747}
1748
1749/* Similarly with a vector and a scalar operand. */
1750static void do_sat_addsub_vec(DisasContext *s, int esz, int rd, int rn,
1751 TCGv_i64 val, bool u, bool d)
1752{
1753 unsigned vsz = vec_full_reg_size(s);
1754 TCGv_ptr dptr, nptr;
1755 TCGv_i32 t32, desc;
1756 TCGv_i64 t64;
1757
1758 dptr = tcg_temp_new_ptr();
1759 nptr = tcg_temp_new_ptr();
1760 tcg_gen_addi_ptr(dptr, cpu_env, vec_full_reg_offset(s, rd));
1761 tcg_gen_addi_ptr(nptr, cpu_env, vec_full_reg_offset(s, rn));
c6a59b55 1762 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
24e82e68
RH
1763
1764 switch (esz) {
1765 case MO_8:
1766 t32 = tcg_temp_new_i32();
1767 tcg_gen_extrl_i64_i32(t32, val);
1768 if (d) {
1769 tcg_gen_neg_i32(t32, t32);
1770 }
1771 if (u) {
1772 gen_helper_sve_uqaddi_b(dptr, nptr, t32, desc);
1773 } else {
1774 gen_helper_sve_sqaddi_b(dptr, nptr, t32, desc);
1775 }
1776 tcg_temp_free_i32(t32);
1777 break;
1778
1779 case MO_16:
1780 t32 = tcg_temp_new_i32();
1781 tcg_gen_extrl_i64_i32(t32, val);
1782 if (d) {
1783 tcg_gen_neg_i32(t32, t32);
1784 }
1785 if (u) {
1786 gen_helper_sve_uqaddi_h(dptr, nptr, t32, desc);
1787 } else {
1788 gen_helper_sve_sqaddi_h(dptr, nptr, t32, desc);
1789 }
1790 tcg_temp_free_i32(t32);
1791 break;
1792
1793 case MO_32:
1794 t64 = tcg_temp_new_i64();
1795 if (d) {
1796 tcg_gen_neg_i64(t64, val);
1797 } else {
1798 tcg_gen_mov_i64(t64, val);
1799 }
1800 if (u) {
1801 gen_helper_sve_uqaddi_s(dptr, nptr, t64, desc);
1802 } else {
1803 gen_helper_sve_sqaddi_s(dptr, nptr, t64, desc);
1804 }
1805 tcg_temp_free_i64(t64);
1806 break;
1807
1808 case MO_64:
1809 if (u) {
1810 if (d) {
1811 gen_helper_sve_uqsubi_d(dptr, nptr, val, desc);
1812 } else {
1813 gen_helper_sve_uqaddi_d(dptr, nptr, val, desc);
1814 }
1815 } else if (d) {
1816 t64 = tcg_temp_new_i64();
1817 tcg_gen_neg_i64(t64, val);
1818 gen_helper_sve_sqaddi_d(dptr, nptr, t64, desc);
1819 tcg_temp_free_i64(t64);
1820 } else {
1821 gen_helper_sve_sqaddi_d(dptr, nptr, val, desc);
1822 }
1823 break;
1824
1825 default:
1826 g_assert_not_reached();
1827 }
1828
1829 tcg_temp_free_ptr(dptr);
1830 tcg_temp_free_ptr(nptr);
24e82e68
RH
1831}
1832
3a7be554 1833static bool trans_CNT_r(DisasContext *s, arg_CNT_r *a)
24e82e68
RH
1834{
1835 if (sve_access_check(s)) {
1836 unsigned fullsz = vec_full_reg_size(s);
1837 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1838 tcg_gen_movi_i64(cpu_reg(s, a->rd), numelem * a->imm);
1839 }
1840 return true;
1841}
1842
3a7be554 1843static bool trans_INCDEC_r(DisasContext *s, arg_incdec_cnt *a)
24e82e68
RH
1844{
1845 if (sve_access_check(s)) {
1846 unsigned fullsz = vec_full_reg_size(s);
1847 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1848 int inc = numelem * a->imm * (a->d ? -1 : 1);
1849 TCGv_i64 reg = cpu_reg(s, a->rd);
1850
1851 tcg_gen_addi_i64(reg, reg, inc);
1852 }
1853 return true;
1854}
1855
3a7be554 1856static bool trans_SINCDEC_r_32(DisasContext *s, arg_incdec_cnt *a)
24e82e68
RH
1857{
1858 if (!sve_access_check(s)) {
1859 return true;
1860 }
1861
1862 unsigned fullsz = vec_full_reg_size(s);
1863 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1864 int inc = numelem * a->imm;
1865 TCGv_i64 reg = cpu_reg(s, a->rd);
1866
1867 /* Use normal 64-bit arithmetic to detect 32-bit overflow. */
1868 if (inc == 0) {
1869 if (a->u) {
1870 tcg_gen_ext32u_i64(reg, reg);
1871 } else {
1872 tcg_gen_ext32s_i64(reg, reg);
1873 }
1874 } else {
d681f125 1875 do_sat_addsub_32(reg, tcg_constant_i64(inc), a->u, a->d);
24e82e68
RH
1876 }
1877 return true;
1878}
1879
3a7be554 1880static bool trans_SINCDEC_r_64(DisasContext *s, arg_incdec_cnt *a)
24e82e68
RH
1881{
1882 if (!sve_access_check(s)) {
1883 return true;
1884 }
1885
1886 unsigned fullsz = vec_full_reg_size(s);
1887 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1888 int inc = numelem * a->imm;
1889 TCGv_i64 reg = cpu_reg(s, a->rd);
1890
1891 if (inc != 0) {
d681f125 1892 do_sat_addsub_64(reg, tcg_constant_i64(inc), a->u, a->d);
24e82e68
RH
1893 }
1894 return true;
1895}
1896
3a7be554 1897static bool trans_INCDEC_v(DisasContext *s, arg_incdec2_cnt *a)
24e82e68
RH
1898{
1899 if (a->esz == 0) {
1900 return false;
1901 }
1902
1903 unsigned fullsz = vec_full_reg_size(s);
1904 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1905 int inc = numelem * a->imm;
1906
1907 if (inc != 0) {
1908 if (sve_access_check(s)) {
24e82e68
RH
1909 tcg_gen_gvec_adds(a->esz, vec_full_reg_offset(s, a->rd),
1910 vec_full_reg_offset(s, a->rn),
d681f125
RH
1911 tcg_constant_i64(a->d ? -inc : inc),
1912 fullsz, fullsz);
24e82e68
RH
1913 }
1914 } else {
1915 do_mov_z(s, a->rd, a->rn);
1916 }
1917 return true;
1918}
1919
3a7be554 1920static bool trans_SINCDEC_v(DisasContext *s, arg_incdec2_cnt *a)
24e82e68
RH
1921{
1922 if (a->esz == 0) {
1923 return false;
1924 }
1925
1926 unsigned fullsz = vec_full_reg_size(s);
1927 unsigned numelem = decode_pred_count(fullsz, a->pat, a->esz);
1928 int inc = numelem * a->imm;
1929
1930 if (inc != 0) {
1931 if (sve_access_check(s)) {
d681f125
RH
1932 do_sat_addsub_vec(s, a->esz, a->rd, a->rn,
1933 tcg_constant_i64(inc), a->u, a->d);
24e82e68
RH
1934 }
1935 } else {
1936 do_mov_z(s, a->rd, a->rn);
1937 }
1938 return true;
1939}
1940
e1fa1164
RH
1941/*
1942 *** SVE Bitwise Immediate Group
1943 */
1944
1945static bool do_zz_dbm(DisasContext *s, arg_rr_dbm *a, GVecGen2iFn *gvec_fn)
1946{
1947 uint64_t imm;
1948 if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
1949 extract32(a->dbm, 0, 6),
1950 extract32(a->dbm, 6, 6))) {
1951 return false;
1952 }
faf915e2 1953 return gen_gvec_fn_zzi(s, gvec_fn, MO_64, a->rd, a->rn, imm);
e1fa1164
RH
1954}
1955
15a314da
RH
1956TRANS_FEAT(AND_zzi, aa64_sve, do_zz_dbm, a, tcg_gen_gvec_andi)
1957TRANS_FEAT(ORR_zzi, aa64_sve, do_zz_dbm, a, tcg_gen_gvec_ori)
1958TRANS_FEAT(EOR_zzi, aa64_sve, do_zz_dbm, a, tcg_gen_gvec_xori)
e1fa1164 1959
3a7be554 1960static bool trans_DUPM(DisasContext *s, arg_DUPM *a)
e1fa1164
RH
1961{
1962 uint64_t imm;
1963 if (!logic_imm_decode_wmask(&imm, extract32(a->dbm, 12, 1),
1964 extract32(a->dbm, 0, 6),
1965 extract32(a->dbm, 6, 6))) {
1966 return false;
1967 }
1968 if (sve_access_check(s)) {
1969 do_dupi_z(s, a->rd, imm);
1970 }
1971 return true;
1972}
1973
f25a2361
RH
1974/*
1975 *** SVE Integer Wide Immediate - Predicated Group
1976 */
1977
1978/* Implement all merging copies. This is used for CPY (immediate),
1979 * FCPY, CPY (scalar), CPY (SIMD&FP scalar).
1980 */
1981static void do_cpy_m(DisasContext *s, int esz, int rd, int rn, int pg,
1982 TCGv_i64 val)
1983{
1984 typedef void gen_cpy(TCGv_ptr, TCGv_ptr, TCGv_ptr, TCGv_i64, TCGv_i32);
1985 static gen_cpy * const fns[4] = {
1986 gen_helper_sve_cpy_m_b, gen_helper_sve_cpy_m_h,
1987 gen_helper_sve_cpy_m_s, gen_helper_sve_cpy_m_d,
1988 };
1989 unsigned vsz = vec_full_reg_size(s);
c6a59b55 1990 TCGv_i32 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
f25a2361
RH
1991 TCGv_ptr t_zd = tcg_temp_new_ptr();
1992 TCGv_ptr t_zn = tcg_temp_new_ptr();
1993 TCGv_ptr t_pg = tcg_temp_new_ptr();
1994
1995 tcg_gen_addi_ptr(t_zd, cpu_env, vec_full_reg_offset(s, rd));
1996 tcg_gen_addi_ptr(t_zn, cpu_env, vec_full_reg_offset(s, rn));
1997 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, pg));
1998
1999 fns[esz](t_zd, t_zn, t_pg, val, desc);
2000
2001 tcg_temp_free_ptr(t_zd);
2002 tcg_temp_free_ptr(t_zn);
2003 tcg_temp_free_ptr(t_pg);
f25a2361
RH
2004}
2005
3a7be554 2006static bool trans_FCPY(DisasContext *s, arg_FCPY *a)
f25a2361
RH
2007{
2008 if (a->esz == 0) {
2009 return false;
2010 }
2011 if (sve_access_check(s)) {
2012 /* Decode the VFP immediate. */
2013 uint64_t imm = vfp_expand_imm(a->esz, a->imm);
e152b48b 2014 do_cpy_m(s, a->esz, a->rd, a->rn, a->pg, tcg_constant_i64(imm));
f25a2361
RH
2015 }
2016 return true;
2017}
2018
3a7be554 2019static bool trans_CPY_m_i(DisasContext *s, arg_rpri_esz *a)
f25a2361 2020{
3a7be554 2021 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
f25a2361
RH
2022 return false;
2023 }
2024 if (sve_access_check(s)) {
e152b48b 2025 do_cpy_m(s, a->esz, a->rd, a->rn, a->pg, tcg_constant_i64(a->imm));
f25a2361
RH
2026 }
2027 return true;
2028}
2029
3a7be554 2030static bool trans_CPY_z_i(DisasContext *s, arg_CPY_z_i *a)
f25a2361
RH
2031{
2032 static gen_helper_gvec_2i * const fns[4] = {
2033 gen_helper_sve_cpy_z_b, gen_helper_sve_cpy_z_h,
2034 gen_helper_sve_cpy_z_s, gen_helper_sve_cpy_z_d,
2035 };
2036
3a7be554 2037 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
f25a2361
RH
2038 return false;
2039 }
2040 if (sve_access_check(s)) {
2041 unsigned vsz = vec_full_reg_size(s);
f25a2361
RH
2042 tcg_gen_gvec_2i_ool(vec_full_reg_offset(s, a->rd),
2043 pred_full_reg_offset(s, a->pg),
e152b48b
RH
2044 tcg_constant_i64(a->imm),
2045 vsz, vsz, 0, fns[a->esz]);
f25a2361
RH
2046 }
2047 return true;
2048}
2049
b94f8f60
RH
2050/*
2051 *** SVE Permute Extract Group
2052 */
2053
75114792 2054static bool do_EXT(DisasContext *s, int rd, int rn, int rm, int imm)
b94f8f60
RH
2055{
2056 if (!sve_access_check(s)) {
2057 return true;
2058 }
2059
2060 unsigned vsz = vec_full_reg_size(s);
75114792 2061 unsigned n_ofs = imm >= vsz ? 0 : imm;
b94f8f60 2062 unsigned n_siz = vsz - n_ofs;
75114792
SL
2063 unsigned d = vec_full_reg_offset(s, rd);
2064 unsigned n = vec_full_reg_offset(s, rn);
2065 unsigned m = vec_full_reg_offset(s, rm);
b94f8f60
RH
2066
2067 /* Use host vector move insns if we have appropriate sizes
2068 * and no unfortunate overlap.
2069 */
2070 if (m != d
2071 && n_ofs == size_for_gvec(n_ofs)
2072 && n_siz == size_for_gvec(n_siz)
2073 && (d != n || n_siz <= n_ofs)) {
2074 tcg_gen_gvec_mov(0, d, n + n_ofs, n_siz, n_siz);
2075 if (n_ofs != 0) {
2076 tcg_gen_gvec_mov(0, d + n_siz, m, n_ofs, n_ofs);
2077 }
2078 } else {
2079 tcg_gen_gvec_3_ool(d, n, m, vsz, vsz, n_ofs, gen_helper_sve_ext);
2080 }
2081 return true;
2082}
2083
c799c115
RH
2084TRANS_FEAT(EXT, aa64_sve, do_EXT, a->rd, a->rn, a->rm, a->imm)
2085TRANS_FEAT(EXT_sve2, aa64_sve2, do_EXT, a->rd, a->rn, (a->rn + 1) % 32, a->imm)
75114792 2086
30562ab7
RH
2087/*
2088 *** SVE Permute - Unpredicated Group
2089 */
2090
3a7be554 2091static bool trans_DUP_s(DisasContext *s, arg_DUP_s *a)
30562ab7
RH
2092{
2093 if (sve_access_check(s)) {
2094 unsigned vsz = vec_full_reg_size(s);
2095 tcg_gen_gvec_dup_i64(a->esz, vec_full_reg_offset(s, a->rd),
2096 vsz, vsz, cpu_reg_sp(s, a->rn));
2097 }
2098 return true;
2099}
2100
3a7be554 2101static bool trans_DUP_x(DisasContext *s, arg_DUP_x *a)
30562ab7
RH
2102{
2103 if ((a->imm & 0x1f) == 0) {
2104 return false;
2105 }
2106 if (sve_access_check(s)) {
2107 unsigned vsz = vec_full_reg_size(s);
2108 unsigned dofs = vec_full_reg_offset(s, a->rd);
2109 unsigned esz, index;
2110
2111 esz = ctz32(a->imm);
2112 index = a->imm >> (esz + 1);
2113
2114 if ((index << esz) < vsz) {
2115 unsigned nofs = vec_reg_offset(s, a->rn, index, esz);
2116 tcg_gen_gvec_dup_mem(esz, dofs, nofs, vsz, vsz);
2117 } else {
7e17d50e
RH
2118 /*
2119 * While dup_mem handles 128-bit elements, dup_imm does not.
2120 * Thankfully element size doesn't matter for splatting zero.
2121 */
2122 tcg_gen_gvec_dup_imm(MO_64, dofs, vsz, vsz, 0);
30562ab7
RH
2123 }
2124 }
2125 return true;
2126}
2127
2128static void do_insr_i64(DisasContext *s, arg_rrr_esz *a, TCGv_i64 val)
2129{
2130 typedef void gen_insr(TCGv_ptr, TCGv_ptr, TCGv_i64, TCGv_i32);
2131 static gen_insr * const fns[4] = {
2132 gen_helper_sve_insr_b, gen_helper_sve_insr_h,
2133 gen_helper_sve_insr_s, gen_helper_sve_insr_d,
2134 };
2135 unsigned vsz = vec_full_reg_size(s);
c6a59b55 2136 TCGv_i32 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
30562ab7
RH
2137 TCGv_ptr t_zd = tcg_temp_new_ptr();
2138 TCGv_ptr t_zn = tcg_temp_new_ptr();
2139
2140 tcg_gen_addi_ptr(t_zd, cpu_env, vec_full_reg_offset(s, a->rd));
2141 tcg_gen_addi_ptr(t_zn, cpu_env, vec_full_reg_offset(s, a->rn));
2142
2143 fns[a->esz](t_zd, t_zn, val, desc);
2144
2145 tcg_temp_free_ptr(t_zd);
2146 tcg_temp_free_ptr(t_zn);
30562ab7
RH
2147}
2148
3a7be554 2149static bool trans_INSR_f(DisasContext *s, arg_rrr_esz *a)
30562ab7
RH
2150{
2151 if (sve_access_check(s)) {
2152 TCGv_i64 t = tcg_temp_new_i64();
2153 tcg_gen_ld_i64(t, cpu_env, vec_reg_offset(s, a->rm, 0, MO_64));
2154 do_insr_i64(s, a, t);
2155 tcg_temp_free_i64(t);
2156 }
2157 return true;
2158}
2159
3a7be554 2160static bool trans_INSR_r(DisasContext *s, arg_rrr_esz *a)
30562ab7
RH
2161{
2162 if (sve_access_check(s)) {
2163 do_insr_i64(s, a, cpu_reg(s, a->rm));
2164 }
2165 return true;
2166}
2167
0ea3cdbf
RH
2168static gen_helper_gvec_2 * const rev_fns[4] = {
2169 gen_helper_sve_rev_b, gen_helper_sve_rev_h,
2170 gen_helper_sve_rev_s, gen_helper_sve_rev_d
2171};
2172TRANS_FEAT(REV_v, aa64_sve, gen_gvec_ool_zz, rev_fns[a->esz], a->rd, a->rn, 0)
30562ab7 2173
32e2ad65
RH
2174static gen_helper_gvec_3 * const sve_tbl_fns[4] = {
2175 gen_helper_sve_tbl_b, gen_helper_sve_tbl_h,
2176 gen_helper_sve_tbl_s, gen_helper_sve_tbl_d
2177};
2178TRANS_FEAT(TBL, aa64_sve, gen_gvec_ool_arg_zzz, sve_tbl_fns[a->esz], a, 0)
30562ab7 2179
5f425b92
RH
2180static gen_helper_gvec_4 * const sve2_tbl_fns[4] = {
2181 gen_helper_sve2_tbl_b, gen_helper_sve2_tbl_h,
2182 gen_helper_sve2_tbl_s, gen_helper_sve2_tbl_d
2183};
2184TRANS_FEAT(TBL_sve2, aa64_sve2, gen_gvec_ool_zzzz, sve2_tbl_fns[a->esz],
2185 a->rd, a->rn, (a->rn + 1) % 32, a->rm, 0)
80a712a2 2186
32e2ad65
RH
2187static gen_helper_gvec_3 * const tbx_fns[4] = {
2188 gen_helper_sve2_tbx_b, gen_helper_sve2_tbx_h,
2189 gen_helper_sve2_tbx_s, gen_helper_sve2_tbx_d
2190};
2191TRANS_FEAT(TBX, aa64_sve2, gen_gvec_ool_arg_zzz, tbx_fns[a->esz], a, 0)
80a712a2 2192
3a7be554 2193static bool trans_UNPK(DisasContext *s, arg_UNPK *a)
30562ab7
RH
2194{
2195 static gen_helper_gvec_2 * const fns[4][2] = {
2196 { NULL, NULL },
2197 { gen_helper_sve_sunpk_h, gen_helper_sve_uunpk_h },
2198 { gen_helper_sve_sunpk_s, gen_helper_sve_uunpk_s },
2199 { gen_helper_sve_sunpk_d, gen_helper_sve_uunpk_d },
2200 };
2201
2202 if (a->esz == 0) {
2203 return false;
2204 }
2205 if (sve_access_check(s)) {
2206 unsigned vsz = vec_full_reg_size(s);
2207 tcg_gen_gvec_2_ool(vec_full_reg_offset(s, a->rd),
2208 vec_full_reg_offset(s, a->rn)
2209 + (a->h ? vsz / 2 : 0),
2210 vsz, vsz, 0, fns[a->esz][a->u]);
2211 }
2212 return true;
2213}
2214
d731d8cb
RH
2215/*
2216 *** SVE Permute - Predicates Group
2217 */
2218
2219static bool do_perm_pred3(DisasContext *s, arg_rrr_esz *a, bool high_odd,
2220 gen_helper_gvec_3 *fn)
2221{
2222 if (!sve_access_check(s)) {
2223 return true;
2224 }
2225
2226 unsigned vsz = pred_full_reg_size(s);
2227
d731d8cb
RH
2228 TCGv_ptr t_d = tcg_temp_new_ptr();
2229 TCGv_ptr t_n = tcg_temp_new_ptr();
2230 TCGv_ptr t_m = tcg_temp_new_ptr();
f9b0fcce 2231 uint32_t desc = 0;
d731d8cb 2232
f9b0fcce
RH
2233 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz);
2234 desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
2235 desc = FIELD_DP32(desc, PREDDESC, DATA, high_odd);
d731d8cb
RH
2236
2237 tcg_gen_addi_ptr(t_d, cpu_env, pred_full_reg_offset(s, a->rd));
2238 tcg_gen_addi_ptr(t_n, cpu_env, pred_full_reg_offset(s, a->rn));
2239 tcg_gen_addi_ptr(t_m, cpu_env, pred_full_reg_offset(s, a->rm));
d731d8cb 2240
c6a59b55 2241 fn(t_d, t_n, t_m, tcg_constant_i32(desc));
d731d8cb
RH
2242
2243 tcg_temp_free_ptr(t_d);
2244 tcg_temp_free_ptr(t_n);
2245 tcg_temp_free_ptr(t_m);
d731d8cb
RH
2246 return true;
2247}
2248
2249static bool do_perm_pred2(DisasContext *s, arg_rr_esz *a, bool high_odd,
2250 gen_helper_gvec_2 *fn)
2251{
2252 if (!sve_access_check(s)) {
2253 return true;
2254 }
2255
2256 unsigned vsz = pred_full_reg_size(s);
2257 TCGv_ptr t_d = tcg_temp_new_ptr();
2258 TCGv_ptr t_n = tcg_temp_new_ptr();
70acaafe 2259 uint32_t desc = 0;
d731d8cb
RH
2260
2261 tcg_gen_addi_ptr(t_d, cpu_env, pred_full_reg_offset(s, a->rd));
2262 tcg_gen_addi_ptr(t_n, cpu_env, pred_full_reg_offset(s, a->rn));
2263
70acaafe
RH
2264 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz);
2265 desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
2266 desc = FIELD_DP32(desc, PREDDESC, DATA, high_odd);
d731d8cb 2267
c6a59b55 2268 fn(t_d, t_n, tcg_constant_i32(desc));
d731d8cb 2269
d731d8cb
RH
2270 tcg_temp_free_ptr(t_d);
2271 tcg_temp_free_ptr(t_n);
2272 return true;
2273}
2274
bdb349f5
RH
2275TRANS_FEAT(ZIP1_p, aa64_sve, do_perm_pred3, a, 0, gen_helper_sve_zip_p)
2276TRANS_FEAT(ZIP2_p, aa64_sve, do_perm_pred3, a, 1, gen_helper_sve_zip_p)
2277TRANS_FEAT(UZP1_p, aa64_sve, do_perm_pred3, a, 0, gen_helper_sve_uzp_p)
2278TRANS_FEAT(UZP2_p, aa64_sve, do_perm_pred3, a, 1, gen_helper_sve_uzp_p)
2279TRANS_FEAT(TRN1_p, aa64_sve, do_perm_pred3, a, 0, gen_helper_sve_trn_p)
2280TRANS_FEAT(TRN2_p, aa64_sve, do_perm_pred3, a, 1, gen_helper_sve_trn_p)
d731d8cb 2281
1d0fce4b
RH
2282TRANS_FEAT(REV_p, aa64_sve, do_perm_pred2, a, 0, gen_helper_sve_rev_p)
2283TRANS_FEAT(PUNPKLO, aa64_sve, do_perm_pred2, a, 0, gen_helper_sve_punpk_p)
2284TRANS_FEAT(PUNPKHI, aa64_sve, do_perm_pred2, a, 1, gen_helper_sve_punpk_p)
d731d8cb 2285
234b48e9
RH
2286/*
2287 *** SVE Permute - Interleaving Group
2288 */
2289
2290static bool do_zip(DisasContext *s, arg_rrr_esz *a, bool high)
2291{
2292 static gen_helper_gvec_3 * const fns[4] = {
2293 gen_helper_sve_zip_b, gen_helper_sve_zip_h,
2294 gen_helper_sve_zip_s, gen_helper_sve_zip_d,
2295 };
c9e0bd5f
RH
2296 unsigned vsz = vec_full_reg_size(s);
2297 unsigned high_ofs = high ? vsz / 2 : 0;
234b48e9 2298
c9e0bd5f 2299 return gen_gvec_ool_arg_zzz(s, fns[a->esz], a, high_ofs);
234b48e9
RH
2300}
2301
3a7be554 2302static bool trans_ZIP1_z(DisasContext *s, arg_rrr_esz *a)
234b48e9
RH
2303{
2304 return do_zip(s, a, false);
2305}
2306
3a7be554 2307static bool trans_ZIP2_z(DisasContext *s, arg_rrr_esz *a)
234b48e9
RH
2308{
2309 return do_zip(s, a, true);
2310}
2311
74b64b25
RH
2312static bool do_zip_q(DisasContext *s, arg_rrr_esz *a, bool high)
2313{
c9e0bd5f
RH
2314 unsigned vsz = vec_full_reg_size(s);
2315 unsigned high_ofs = high ? QEMU_ALIGN_DOWN(vsz, 32) / 2 : 0;
2316
74b64b25
RH
2317 if (!dc_isar_feature(aa64_sve_f64mm, s)) {
2318 return false;
2319 }
c9e0bd5f 2320 return gen_gvec_ool_arg_zzz(s, gen_helper_sve2_zip_q, a, high_ofs);
74b64b25
RH
2321}
2322
2323static bool trans_ZIP1_q(DisasContext *s, arg_rrr_esz *a)
2324{
2325 return do_zip_q(s, a, false);
2326}
2327
2328static bool trans_ZIP2_q(DisasContext *s, arg_rrr_esz *a)
2329{
2330 return do_zip_q(s, a, true);
2331}
2332
234b48e9
RH
2333static gen_helper_gvec_3 * const uzp_fns[4] = {
2334 gen_helper_sve_uzp_b, gen_helper_sve_uzp_h,
2335 gen_helper_sve_uzp_s, gen_helper_sve_uzp_d,
2336};
2337
32e2ad65
RH
2338TRANS_FEAT(UZP1_z, aa64_sve, gen_gvec_ool_arg_zzz,
2339 uzp_fns[a->esz], a, 0)
2340TRANS_FEAT(UZP2_z, aa64_sve, gen_gvec_ool_arg_zzz,
2341 uzp_fns[a->esz], a, 1 << a->esz)
234b48e9 2342
32e2ad65
RH
2343TRANS_FEAT(UZP1_q, aa64_sve_f64mm, gen_gvec_ool_arg_zzz,
2344 gen_helper_sve2_uzp_q, a, 0)
2345TRANS_FEAT(UZP2_q, aa64_sve_f64mm, gen_gvec_ool_arg_zzz,
2346 gen_helper_sve2_uzp_q, a, 16)
74b64b25 2347
234b48e9
RH
2348static gen_helper_gvec_3 * const trn_fns[4] = {
2349 gen_helper_sve_trn_b, gen_helper_sve_trn_h,
2350 gen_helper_sve_trn_s, gen_helper_sve_trn_d,
2351};
2352
32e2ad65
RH
2353TRANS_FEAT(TRN1_z, aa64_sve, gen_gvec_ool_arg_zzz,
2354 trn_fns[a->esz], a, 0)
2355TRANS_FEAT(TRN2_z, aa64_sve, gen_gvec_ool_arg_zzz,
2356 trn_fns[a->esz], a, 1 << a->esz)
234b48e9 2357
32e2ad65
RH
2358TRANS_FEAT(TRN1_q, aa64_sve_f64mm, gen_gvec_ool_arg_zzz,
2359 gen_helper_sve2_trn_q, a, 0)
2360TRANS_FEAT(TRN2_q, aa64_sve_f64mm, gen_gvec_ool_arg_zzz,
2361 gen_helper_sve2_trn_q, a, 16)
74b64b25 2362
3ca879ae
RH
2363/*
2364 *** SVE Permute Vector - Predicated Group
2365 */
2366
817bd5c9
RH
2367static gen_helper_gvec_3 * const compact_fns[4] = {
2368 NULL, NULL, gen_helper_sve_compact_s, gen_helper_sve_compact_d
2369};
2370TRANS_FEAT(COMPACT, aa64_sve, gen_gvec_ool_arg_zpz, compact_fns[a->esz], a, 0)
3ca879ae 2371
ef23cb72
RH
2372/* Call the helper that computes the ARM LastActiveElement pseudocode
2373 * function, scaled by the element size. This includes the not found
2374 * indication; e.g. not found for esz=3 is -8.
2375 */
2376static void find_last_active(DisasContext *s, TCGv_i32 ret, int esz, int pg)
2377{
2378 /* Predicate sizes may be smaller and cannot use simd_desc. We cannot
2379 * round up, as we do elsewhere, because we need the exact size.
2380 */
2381 TCGv_ptr t_p = tcg_temp_new_ptr();
2acbfbe4 2382 unsigned desc = 0;
ef23cb72 2383
2acbfbe4
RH
2384 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, pred_full_reg_size(s));
2385 desc = FIELD_DP32(desc, PREDDESC, ESZ, esz);
ef23cb72
RH
2386
2387 tcg_gen_addi_ptr(t_p, cpu_env, pred_full_reg_offset(s, pg));
ef23cb72 2388
c6a59b55 2389 gen_helper_sve_last_active_element(ret, t_p, tcg_constant_i32(desc));
ef23cb72 2390
ef23cb72
RH
2391 tcg_temp_free_ptr(t_p);
2392}
2393
2394/* Increment LAST to the offset of the next element in the vector,
2395 * wrapping around to 0.
2396 */
2397static void incr_last_active(DisasContext *s, TCGv_i32 last, int esz)
2398{
2399 unsigned vsz = vec_full_reg_size(s);
2400
2401 tcg_gen_addi_i32(last, last, 1 << esz);
2402 if (is_power_of_2(vsz)) {
2403 tcg_gen_andi_i32(last, last, vsz - 1);
2404 } else {
4b308bd5
RH
2405 TCGv_i32 max = tcg_constant_i32(vsz);
2406 TCGv_i32 zero = tcg_constant_i32(0);
ef23cb72 2407 tcg_gen_movcond_i32(TCG_COND_GEU, last, last, max, zero, last);
ef23cb72
RH
2408 }
2409}
2410
2411/* If LAST < 0, set LAST to the offset of the last element in the vector. */
2412static void wrap_last_active(DisasContext *s, TCGv_i32 last, int esz)
2413{
2414 unsigned vsz = vec_full_reg_size(s);
2415
2416 if (is_power_of_2(vsz)) {
2417 tcg_gen_andi_i32(last, last, vsz - 1);
2418 } else {
4b308bd5
RH
2419 TCGv_i32 max = tcg_constant_i32(vsz - (1 << esz));
2420 TCGv_i32 zero = tcg_constant_i32(0);
ef23cb72 2421 tcg_gen_movcond_i32(TCG_COND_LT, last, last, zero, max, last);
ef23cb72
RH
2422 }
2423}
2424
2425/* Load an unsigned element of ESZ from BASE+OFS. */
2426static TCGv_i64 load_esz(TCGv_ptr base, int ofs, int esz)
2427{
2428 TCGv_i64 r = tcg_temp_new_i64();
2429
2430 switch (esz) {
2431 case 0:
2432 tcg_gen_ld8u_i64(r, base, ofs);
2433 break;
2434 case 1:
2435 tcg_gen_ld16u_i64(r, base, ofs);
2436 break;
2437 case 2:
2438 tcg_gen_ld32u_i64(r, base, ofs);
2439 break;
2440 case 3:
2441 tcg_gen_ld_i64(r, base, ofs);
2442 break;
2443 default:
2444 g_assert_not_reached();
2445 }
2446 return r;
2447}
2448
2449/* Load an unsigned element of ESZ from RM[LAST]. */
2450static TCGv_i64 load_last_active(DisasContext *s, TCGv_i32 last,
2451 int rm, int esz)
2452{
2453 TCGv_ptr p = tcg_temp_new_ptr();
2454 TCGv_i64 r;
2455
2456 /* Convert offset into vector into offset into ENV.
2457 * The final adjustment for the vector register base
2458 * is added via constant offset to the load.
2459 */
e03b5686 2460#if HOST_BIG_ENDIAN
ef23cb72
RH
2461 /* Adjust for element ordering. See vec_reg_offset. */
2462 if (esz < 3) {
2463 tcg_gen_xori_i32(last, last, 8 - (1 << esz));
2464 }
2465#endif
2466 tcg_gen_ext_i32_ptr(p, last);
2467 tcg_gen_add_ptr(p, p, cpu_env);
2468
2469 r = load_esz(p, vec_full_reg_offset(s, rm), esz);
2470 tcg_temp_free_ptr(p);
2471
2472 return r;
2473}
2474
2475/* Compute CLAST for a Zreg. */
2476static bool do_clast_vector(DisasContext *s, arg_rprr_esz *a, bool before)
2477{
2478 TCGv_i32 last;
2479 TCGLabel *over;
2480 TCGv_i64 ele;
2481 unsigned vsz, esz = a->esz;
2482
2483 if (!sve_access_check(s)) {
2484 return true;
2485 }
2486
2487 last = tcg_temp_local_new_i32();
2488 over = gen_new_label();
2489
2490 find_last_active(s, last, esz, a->pg);
2491
2492 /* There is of course no movcond for a 2048-bit vector,
2493 * so we must branch over the actual store.
2494 */
2495 tcg_gen_brcondi_i32(TCG_COND_LT, last, 0, over);
2496
2497 if (!before) {
2498 incr_last_active(s, last, esz);
2499 }
2500
2501 ele = load_last_active(s, last, a->rm, esz);
2502 tcg_temp_free_i32(last);
2503
2504 vsz = vec_full_reg_size(s);
2505 tcg_gen_gvec_dup_i64(esz, vec_full_reg_offset(s, a->rd), vsz, vsz, ele);
2506 tcg_temp_free_i64(ele);
2507
2508 /* If this insn used MOVPRFX, we may need a second move. */
2509 if (a->rd != a->rn) {
2510 TCGLabel *done = gen_new_label();
2511 tcg_gen_br(done);
2512
2513 gen_set_label(over);
2514 do_mov_z(s, a->rd, a->rn);
2515
2516 gen_set_label(done);
2517 } else {
2518 gen_set_label(over);
2519 }
2520 return true;
2521}
2522
3a7be554 2523static bool trans_CLASTA_z(DisasContext *s, arg_rprr_esz *a)
ef23cb72
RH
2524{
2525 return do_clast_vector(s, a, false);
2526}
2527
3a7be554 2528static bool trans_CLASTB_z(DisasContext *s, arg_rprr_esz *a)
ef23cb72
RH
2529{
2530 return do_clast_vector(s, a, true);
2531}
2532
2533/* Compute CLAST for a scalar. */
2534static void do_clast_scalar(DisasContext *s, int esz, int pg, int rm,
2535 bool before, TCGv_i64 reg_val)
2536{
2537 TCGv_i32 last = tcg_temp_new_i32();
053552d3 2538 TCGv_i64 ele, cmp;
ef23cb72
RH
2539
2540 find_last_active(s, last, esz, pg);
2541
2542 /* Extend the original value of last prior to incrementing. */
2543 cmp = tcg_temp_new_i64();
2544 tcg_gen_ext_i32_i64(cmp, last);
2545
2546 if (!before) {
2547 incr_last_active(s, last, esz);
2548 }
2549
2550 /* The conceit here is that while last < 0 indicates not found, after
2551 * adjusting for cpu_env->vfp.zregs[rm], it is still a valid address
2552 * from which we can load garbage. We then discard the garbage with
2553 * a conditional move.
2554 */
2555 ele = load_last_active(s, last, rm, esz);
2556 tcg_temp_free_i32(last);
2557
053552d3
RH
2558 tcg_gen_movcond_i64(TCG_COND_GE, reg_val, cmp, tcg_constant_i64(0),
2559 ele, reg_val);
ef23cb72 2560
ef23cb72
RH
2561 tcg_temp_free_i64(cmp);
2562 tcg_temp_free_i64(ele);
2563}
2564
2565/* Compute CLAST for a Vreg. */
2566static bool do_clast_fp(DisasContext *s, arg_rpr_esz *a, bool before)
2567{
2568 if (sve_access_check(s)) {
2569 int esz = a->esz;
2570 int ofs = vec_reg_offset(s, a->rd, 0, esz);
2571 TCGv_i64 reg = load_esz(cpu_env, ofs, esz);
2572
2573 do_clast_scalar(s, esz, a->pg, a->rn, before, reg);
2574 write_fp_dreg(s, a->rd, reg);
2575 tcg_temp_free_i64(reg);
2576 }
2577 return true;
2578}
2579
3a7be554 2580static bool trans_CLASTA_v(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2581{
2582 return do_clast_fp(s, a, false);
2583}
2584
3a7be554 2585static bool trans_CLASTB_v(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2586{
2587 return do_clast_fp(s, a, true);
2588}
2589
2590/* Compute CLAST for a Xreg. */
2591static bool do_clast_general(DisasContext *s, arg_rpr_esz *a, bool before)
2592{
2593 TCGv_i64 reg;
2594
2595 if (!sve_access_check(s)) {
2596 return true;
2597 }
2598
2599 reg = cpu_reg(s, a->rd);
2600 switch (a->esz) {
2601 case 0:
2602 tcg_gen_ext8u_i64(reg, reg);
2603 break;
2604 case 1:
2605 tcg_gen_ext16u_i64(reg, reg);
2606 break;
2607 case 2:
2608 tcg_gen_ext32u_i64(reg, reg);
2609 break;
2610 case 3:
2611 break;
2612 default:
2613 g_assert_not_reached();
2614 }
2615
2616 do_clast_scalar(s, a->esz, a->pg, a->rn, before, reg);
2617 return true;
2618}
2619
3a7be554 2620static bool trans_CLASTA_r(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2621{
2622 return do_clast_general(s, a, false);
2623}
2624
3a7be554 2625static bool trans_CLASTB_r(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2626{
2627 return do_clast_general(s, a, true);
2628}
2629
2630/* Compute LAST for a scalar. */
2631static TCGv_i64 do_last_scalar(DisasContext *s, int esz,
2632 int pg, int rm, bool before)
2633{
2634 TCGv_i32 last = tcg_temp_new_i32();
2635 TCGv_i64 ret;
2636
2637 find_last_active(s, last, esz, pg);
2638 if (before) {
2639 wrap_last_active(s, last, esz);
2640 } else {
2641 incr_last_active(s, last, esz);
2642 }
2643
2644 ret = load_last_active(s, last, rm, esz);
2645 tcg_temp_free_i32(last);
2646 return ret;
2647}
2648
2649/* Compute LAST for a Vreg. */
2650static bool do_last_fp(DisasContext *s, arg_rpr_esz *a, bool before)
2651{
2652 if (sve_access_check(s)) {
2653 TCGv_i64 val = do_last_scalar(s, a->esz, a->pg, a->rn, before);
2654 write_fp_dreg(s, a->rd, val);
2655 tcg_temp_free_i64(val);
2656 }
2657 return true;
2658}
2659
3a7be554 2660static bool trans_LASTA_v(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2661{
2662 return do_last_fp(s, a, false);
2663}
2664
3a7be554 2665static bool trans_LASTB_v(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2666{
2667 return do_last_fp(s, a, true);
2668}
2669
2670/* Compute LAST for a Xreg. */
2671static bool do_last_general(DisasContext *s, arg_rpr_esz *a, bool before)
2672{
2673 if (sve_access_check(s)) {
2674 TCGv_i64 val = do_last_scalar(s, a->esz, a->pg, a->rn, before);
2675 tcg_gen_mov_i64(cpu_reg(s, a->rd), val);
2676 tcg_temp_free_i64(val);
2677 }
2678 return true;
2679}
2680
3a7be554 2681static bool trans_LASTA_r(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2682{
2683 return do_last_general(s, a, false);
2684}
2685
3a7be554 2686static bool trans_LASTB_r(DisasContext *s, arg_rpr_esz *a)
ef23cb72
RH
2687{
2688 return do_last_general(s, a, true);
2689}
2690
3a7be554 2691static bool trans_CPY_m_r(DisasContext *s, arg_rpr_esz *a)
792a5578
RH
2692{
2693 if (sve_access_check(s)) {
2694 do_cpy_m(s, a->esz, a->rd, a->rd, a->pg, cpu_reg_sp(s, a->rn));
2695 }
2696 return true;
2697}
2698
3a7be554 2699static bool trans_CPY_m_v(DisasContext *s, arg_rpr_esz *a)
792a5578
RH
2700{
2701 if (sve_access_check(s)) {
2702 int ofs = vec_reg_offset(s, a->rn, 0, a->esz);
2703 TCGv_i64 t = load_esz(cpu_env, ofs, a->esz);
2704 do_cpy_m(s, a->esz, a->rd, a->rd, a->pg, t);
2705 tcg_temp_free_i64(t);
2706 }
2707 return true;
2708}
2709
817bd5c9
RH
2710static gen_helper_gvec_3 * const revb_fns[4] = {
2711 NULL, gen_helper_sve_revb_h,
2712 gen_helper_sve_revb_s, gen_helper_sve_revb_d,
2713};
2714TRANS_FEAT(REVB, aa64_sve, gen_gvec_ool_arg_zpz, revb_fns[a->esz], a, 0)
dae8fb90 2715
817bd5c9
RH
2716static gen_helper_gvec_3 * const revh_fns[4] = {
2717 NULL, NULL, gen_helper_sve_revh_s, gen_helper_sve_revh_d,
2718};
2719TRANS_FEAT(REVH, aa64_sve, gen_gvec_ool_arg_zpz, revh_fns[a->esz], a, 0)
dae8fb90 2720
817bd5c9
RH
2721TRANS_FEAT(REVW, aa64_sve, gen_gvec_ool_arg_zpz,
2722 a->esz == 3 ? gen_helper_sve_revw_d : NULL, a, 0)
dae8fb90 2723
3a7be554 2724static bool trans_SPLICE(DisasContext *s, arg_rprr_esz *a)
b48ff240 2725{
2a753d1e
RH
2726 return gen_gvec_ool_zzzp(s, gen_helper_sve_splice,
2727 a->rd, a->rn, a->rm, a->pg, a->esz);
b48ff240
RH
2728}
2729
75114792
SL
2730static bool trans_SPLICE_sve2(DisasContext *s, arg_rpr_esz *a)
2731{
2732 if (!dc_isar_feature(aa64_sve2, s)) {
2733 return false;
2734 }
2a753d1e
RH
2735 return gen_gvec_ool_zzzp(s, gen_helper_sve_splice,
2736 a->rd, a->rn, (a->rn + 1) % 32, a->pg, a->esz);
75114792
SL
2737}
2738
757f9cff
RH
2739/*
2740 *** SVE Integer Compare - Vectors Group
2741 */
2742
2743static bool do_ppzz_flags(DisasContext *s, arg_rprr_esz *a,
2744 gen_helper_gvec_flags_4 *gen_fn)
2745{
2746 TCGv_ptr pd, zn, zm, pg;
2747 unsigned vsz;
2748 TCGv_i32 t;
2749
2750 if (gen_fn == NULL) {
2751 return false;
2752 }
2753 if (!sve_access_check(s)) {
2754 return true;
2755 }
2756
2757 vsz = vec_full_reg_size(s);
392acacc 2758 t = tcg_temp_new_i32();
757f9cff
RH
2759 pd = tcg_temp_new_ptr();
2760 zn = tcg_temp_new_ptr();
2761 zm = tcg_temp_new_ptr();
2762 pg = tcg_temp_new_ptr();
2763
2764 tcg_gen_addi_ptr(pd, cpu_env, pred_full_reg_offset(s, a->rd));
2765 tcg_gen_addi_ptr(zn, cpu_env, vec_full_reg_offset(s, a->rn));
2766 tcg_gen_addi_ptr(zm, cpu_env, vec_full_reg_offset(s, a->rm));
2767 tcg_gen_addi_ptr(pg, cpu_env, pred_full_reg_offset(s, a->pg));
2768
392acacc 2769 gen_fn(t, pd, zn, zm, pg, tcg_constant_i32(simd_desc(vsz, vsz, 0)));
757f9cff
RH
2770
2771 tcg_temp_free_ptr(pd);
2772 tcg_temp_free_ptr(zn);
2773 tcg_temp_free_ptr(zm);
2774 tcg_temp_free_ptr(pg);
2775
2776 do_pred_flags(t);
2777
2778 tcg_temp_free_i32(t);
2779 return true;
2780}
2781
2782#define DO_PPZZ(NAME, name) \
3a7be554 2783static bool trans_##NAME##_ppzz(DisasContext *s, arg_rprr_esz *a) \
757f9cff
RH
2784{ \
2785 static gen_helper_gvec_flags_4 * const fns[4] = { \
2786 gen_helper_sve_##name##_ppzz_b, gen_helper_sve_##name##_ppzz_h, \
2787 gen_helper_sve_##name##_ppzz_s, gen_helper_sve_##name##_ppzz_d, \
2788 }; \
2789 return do_ppzz_flags(s, a, fns[a->esz]); \
2790}
2791
2792DO_PPZZ(CMPEQ, cmpeq)
2793DO_PPZZ(CMPNE, cmpne)
2794DO_PPZZ(CMPGT, cmpgt)
2795DO_PPZZ(CMPGE, cmpge)
2796DO_PPZZ(CMPHI, cmphi)
2797DO_PPZZ(CMPHS, cmphs)
2798
2799#undef DO_PPZZ
2800
2801#define DO_PPZW(NAME, name) \
3a7be554 2802static bool trans_##NAME##_ppzw(DisasContext *s, arg_rprr_esz *a) \
757f9cff
RH
2803{ \
2804 static gen_helper_gvec_flags_4 * const fns[4] = { \
2805 gen_helper_sve_##name##_ppzw_b, gen_helper_sve_##name##_ppzw_h, \
2806 gen_helper_sve_##name##_ppzw_s, NULL \
2807 }; \
2808 return do_ppzz_flags(s, a, fns[a->esz]); \
2809}
2810
2811DO_PPZW(CMPEQ, cmpeq)
2812DO_PPZW(CMPNE, cmpne)
2813DO_PPZW(CMPGT, cmpgt)
2814DO_PPZW(CMPGE, cmpge)
2815DO_PPZW(CMPHI, cmphi)
2816DO_PPZW(CMPHS, cmphs)
2817DO_PPZW(CMPLT, cmplt)
2818DO_PPZW(CMPLE, cmple)
2819DO_PPZW(CMPLO, cmplo)
2820DO_PPZW(CMPLS, cmpls)
2821
2822#undef DO_PPZW
2823
38cadeba
RH
2824/*
2825 *** SVE Integer Compare - Immediate Groups
2826 */
2827
2828static bool do_ppzi_flags(DisasContext *s, arg_rpri_esz *a,
2829 gen_helper_gvec_flags_3 *gen_fn)
2830{
2831 TCGv_ptr pd, zn, pg;
2832 unsigned vsz;
2833 TCGv_i32 t;
2834
2835 if (gen_fn == NULL) {
2836 return false;
2837 }
2838 if (!sve_access_check(s)) {
2839 return true;
2840 }
2841
2842 vsz = vec_full_reg_size(s);
392acacc 2843 t = tcg_temp_new_i32();
38cadeba
RH
2844 pd = tcg_temp_new_ptr();
2845 zn = tcg_temp_new_ptr();
2846 pg = tcg_temp_new_ptr();
2847
2848 tcg_gen_addi_ptr(pd, cpu_env, pred_full_reg_offset(s, a->rd));
2849 tcg_gen_addi_ptr(zn, cpu_env, vec_full_reg_offset(s, a->rn));
2850 tcg_gen_addi_ptr(pg, cpu_env, pred_full_reg_offset(s, a->pg));
2851
392acacc 2852 gen_fn(t, pd, zn, pg, tcg_constant_i32(simd_desc(vsz, vsz, a->imm)));
38cadeba
RH
2853
2854 tcg_temp_free_ptr(pd);
2855 tcg_temp_free_ptr(zn);
2856 tcg_temp_free_ptr(pg);
2857
2858 do_pred_flags(t);
2859
2860 tcg_temp_free_i32(t);
2861 return true;
2862}
2863
2864#define DO_PPZI(NAME, name) \
3a7be554 2865static bool trans_##NAME##_ppzi(DisasContext *s, arg_rpri_esz *a) \
38cadeba
RH
2866{ \
2867 static gen_helper_gvec_flags_3 * const fns[4] = { \
2868 gen_helper_sve_##name##_ppzi_b, gen_helper_sve_##name##_ppzi_h, \
2869 gen_helper_sve_##name##_ppzi_s, gen_helper_sve_##name##_ppzi_d, \
2870 }; \
2871 return do_ppzi_flags(s, a, fns[a->esz]); \
2872}
2873
2874DO_PPZI(CMPEQ, cmpeq)
2875DO_PPZI(CMPNE, cmpne)
2876DO_PPZI(CMPGT, cmpgt)
2877DO_PPZI(CMPGE, cmpge)
2878DO_PPZI(CMPHI, cmphi)
2879DO_PPZI(CMPHS, cmphs)
2880DO_PPZI(CMPLT, cmplt)
2881DO_PPZI(CMPLE, cmple)
2882DO_PPZI(CMPLO, cmplo)
2883DO_PPZI(CMPLS, cmpls)
2884
2885#undef DO_PPZI
2886
35da316f
RH
2887/*
2888 *** SVE Partition Break Group
2889 */
2890
2891static bool do_brk3(DisasContext *s, arg_rprr_s *a,
2892 gen_helper_gvec_4 *fn, gen_helper_gvec_flags_4 *fn_s)
2893{
2894 if (!sve_access_check(s)) {
2895 return true;
2896 }
2897
2898 unsigned vsz = pred_full_reg_size(s);
2899
2900 /* Predicate sizes may be smaller and cannot use simd_desc. */
2901 TCGv_ptr d = tcg_temp_new_ptr();
2902 TCGv_ptr n = tcg_temp_new_ptr();
2903 TCGv_ptr m = tcg_temp_new_ptr();
2904 TCGv_ptr g = tcg_temp_new_ptr();
93418f1c 2905 TCGv_i32 desc = tcg_constant_i32(FIELD_DP32(0, PREDDESC, OPRSZ, vsz));
35da316f
RH
2906
2907 tcg_gen_addi_ptr(d, cpu_env, pred_full_reg_offset(s, a->rd));
2908 tcg_gen_addi_ptr(n, cpu_env, pred_full_reg_offset(s, a->rn));
2909 tcg_gen_addi_ptr(m, cpu_env, pred_full_reg_offset(s, a->rm));
2910 tcg_gen_addi_ptr(g, cpu_env, pred_full_reg_offset(s, a->pg));
2911
2912 if (a->s) {
93418f1c
RH
2913 TCGv_i32 t = tcg_temp_new_i32();
2914 fn_s(t, d, n, m, g, desc);
35da316f 2915 do_pred_flags(t);
93418f1c 2916 tcg_temp_free_i32(t);
35da316f 2917 } else {
93418f1c 2918 fn(d, n, m, g, desc);
35da316f
RH
2919 }
2920 tcg_temp_free_ptr(d);
2921 tcg_temp_free_ptr(n);
2922 tcg_temp_free_ptr(m);
2923 tcg_temp_free_ptr(g);
35da316f
RH
2924 return true;
2925}
2926
2927static bool do_brk2(DisasContext *s, arg_rpr_s *a,
2928 gen_helper_gvec_3 *fn, gen_helper_gvec_flags_3 *fn_s)
2929{
2930 if (!sve_access_check(s)) {
2931 return true;
2932 }
2933
2934 unsigned vsz = pred_full_reg_size(s);
2935
2936 /* Predicate sizes may be smaller and cannot use simd_desc. */
2937 TCGv_ptr d = tcg_temp_new_ptr();
2938 TCGv_ptr n = tcg_temp_new_ptr();
2939 TCGv_ptr g = tcg_temp_new_ptr();
93418f1c 2940 TCGv_i32 desc = tcg_constant_i32(FIELD_DP32(0, PREDDESC, OPRSZ, vsz));
35da316f
RH
2941
2942 tcg_gen_addi_ptr(d, cpu_env, pred_full_reg_offset(s, a->rd));
2943 tcg_gen_addi_ptr(n, cpu_env, pred_full_reg_offset(s, a->rn));
2944 tcg_gen_addi_ptr(g, cpu_env, pred_full_reg_offset(s, a->pg));
2945
2946 if (a->s) {
93418f1c
RH
2947 TCGv_i32 t = tcg_temp_new_i32();
2948 fn_s(t, d, n, g, desc);
35da316f 2949 do_pred_flags(t);
93418f1c 2950 tcg_temp_free_i32(t);
35da316f 2951 } else {
93418f1c 2952 fn(d, n, g, desc);
35da316f
RH
2953 }
2954 tcg_temp_free_ptr(d);
2955 tcg_temp_free_ptr(n);
2956 tcg_temp_free_ptr(g);
35da316f
RH
2957 return true;
2958}
2959
3a7be554 2960static bool trans_BRKPA(DisasContext *s, arg_rprr_s *a)
35da316f
RH
2961{
2962 return do_brk3(s, a, gen_helper_sve_brkpa, gen_helper_sve_brkpas);
2963}
2964
3a7be554 2965static bool trans_BRKPB(DisasContext *s, arg_rprr_s *a)
35da316f
RH
2966{
2967 return do_brk3(s, a, gen_helper_sve_brkpb, gen_helper_sve_brkpbs);
2968}
2969
3a7be554 2970static bool trans_BRKA_m(DisasContext *s, arg_rpr_s *a)
35da316f
RH
2971{
2972 return do_brk2(s, a, gen_helper_sve_brka_m, gen_helper_sve_brkas_m);
2973}
2974
3a7be554 2975static bool trans_BRKB_m(DisasContext *s, arg_rpr_s *a)
35da316f
RH
2976{
2977 return do_brk2(s, a, gen_helper_sve_brkb_m, gen_helper_sve_brkbs_m);
2978}
2979
3a7be554 2980static bool trans_BRKA_z(DisasContext *s, arg_rpr_s *a)
35da316f
RH
2981{
2982 return do_brk2(s, a, gen_helper_sve_brka_z, gen_helper_sve_brkas_z);
2983}
2984
3a7be554 2985static bool trans_BRKB_z(DisasContext *s, arg_rpr_s *a)
35da316f
RH
2986{
2987 return do_brk2(s, a, gen_helper_sve_brkb_z, gen_helper_sve_brkbs_z);
2988}
2989
3a7be554 2990static bool trans_BRKN(DisasContext *s, arg_rpr_s *a)
35da316f
RH
2991{
2992 return do_brk2(s, a, gen_helper_sve_brkn, gen_helper_sve_brkns);
2993}
2994
9ee3a611
RH
2995/*
2996 *** SVE Predicate Count Group
2997 */
2998
2999static void do_cntp(DisasContext *s, TCGv_i64 val, int esz, int pn, int pg)
3000{
3001 unsigned psz = pred_full_reg_size(s);
3002
3003 if (psz <= 8) {
3004 uint64_t psz_mask;
3005
3006 tcg_gen_ld_i64(val, cpu_env, pred_full_reg_offset(s, pn));
3007 if (pn != pg) {
3008 TCGv_i64 g = tcg_temp_new_i64();
3009 tcg_gen_ld_i64(g, cpu_env, pred_full_reg_offset(s, pg));
3010 tcg_gen_and_i64(val, val, g);
3011 tcg_temp_free_i64(g);
3012 }
3013
3014 /* Reduce the pred_esz_masks value simply to reduce the
3015 * size of the code generated here.
3016 */
3017 psz_mask = MAKE_64BIT_MASK(0, psz * 8);
3018 tcg_gen_andi_i64(val, val, pred_esz_masks[esz] & psz_mask);
3019
3020 tcg_gen_ctpop_i64(val, val);
3021 } else {
3022 TCGv_ptr t_pn = tcg_temp_new_ptr();
3023 TCGv_ptr t_pg = tcg_temp_new_ptr();
f556a201 3024 unsigned desc = 0;
9ee3a611 3025
f556a201
RH
3026 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, psz);
3027 desc = FIELD_DP32(desc, PREDDESC, ESZ, esz);
9ee3a611
RH
3028
3029 tcg_gen_addi_ptr(t_pn, cpu_env, pred_full_reg_offset(s, pn));
3030 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, pg));
9ee3a611 3031
c6a59b55 3032 gen_helper_sve_cntp(val, t_pn, t_pg, tcg_constant_i32(desc));
9ee3a611
RH
3033 tcg_temp_free_ptr(t_pn);
3034 tcg_temp_free_ptr(t_pg);
9ee3a611
RH
3035 }
3036}
3037
3a7be554 3038static bool trans_CNTP(DisasContext *s, arg_CNTP *a)
9ee3a611
RH
3039{
3040 if (sve_access_check(s)) {
3041 do_cntp(s, cpu_reg(s, a->rd), a->esz, a->rn, a->pg);
3042 }
3043 return true;
3044}
3045
3a7be554 3046static bool trans_INCDECP_r(DisasContext *s, arg_incdec_pred *a)
9ee3a611
RH
3047{
3048 if (sve_access_check(s)) {
3049 TCGv_i64 reg = cpu_reg(s, a->rd);
3050 TCGv_i64 val = tcg_temp_new_i64();
3051
3052 do_cntp(s, val, a->esz, a->pg, a->pg);
3053 if (a->d) {
3054 tcg_gen_sub_i64(reg, reg, val);
3055 } else {
3056 tcg_gen_add_i64(reg, reg, val);
3057 }
3058 tcg_temp_free_i64(val);
3059 }
3060 return true;
3061}
3062
3a7be554 3063static bool trans_INCDECP_z(DisasContext *s, arg_incdec2_pred *a)
9ee3a611
RH
3064{
3065 if (a->esz == 0) {
3066 return false;
3067 }
3068 if (sve_access_check(s)) {
3069 unsigned vsz = vec_full_reg_size(s);
3070 TCGv_i64 val = tcg_temp_new_i64();
3071 GVecGen2sFn *gvec_fn = a->d ? tcg_gen_gvec_subs : tcg_gen_gvec_adds;
3072
3073 do_cntp(s, val, a->esz, a->pg, a->pg);
3074 gvec_fn(a->esz, vec_full_reg_offset(s, a->rd),
3075 vec_full_reg_offset(s, a->rn), val, vsz, vsz);
3076 }
3077 return true;
3078}
3079
3a7be554 3080static bool trans_SINCDECP_r_32(DisasContext *s, arg_incdec_pred *a)
9ee3a611
RH
3081{
3082 if (sve_access_check(s)) {
3083 TCGv_i64 reg = cpu_reg(s, a->rd);
3084 TCGv_i64 val = tcg_temp_new_i64();
3085
3086 do_cntp(s, val, a->esz, a->pg, a->pg);
3087 do_sat_addsub_32(reg, val, a->u, a->d);
3088 }
3089 return true;
3090}
3091
3a7be554 3092static bool trans_SINCDECP_r_64(DisasContext *s, arg_incdec_pred *a)
9ee3a611
RH
3093{
3094 if (sve_access_check(s)) {
3095 TCGv_i64 reg = cpu_reg(s, a->rd);
3096 TCGv_i64 val = tcg_temp_new_i64();
3097
3098 do_cntp(s, val, a->esz, a->pg, a->pg);
3099 do_sat_addsub_64(reg, val, a->u, a->d);
3100 }
3101 return true;
3102}
3103
3a7be554 3104static bool trans_SINCDECP_z(DisasContext *s, arg_incdec2_pred *a)
9ee3a611
RH
3105{
3106 if (a->esz == 0) {
3107 return false;
3108 }
3109 if (sve_access_check(s)) {
3110 TCGv_i64 val = tcg_temp_new_i64();
3111 do_cntp(s, val, a->esz, a->pg, a->pg);
3112 do_sat_addsub_vec(s, a->esz, a->rd, a->rn, val, a->u, a->d);
3113 }
3114 return true;
3115}
3116
caf1cefc
RH
3117/*
3118 *** SVE Integer Compare Scalars Group
3119 */
3120
3a7be554 3121static bool trans_CTERM(DisasContext *s, arg_CTERM *a)
caf1cefc
RH
3122{
3123 if (!sve_access_check(s)) {
3124 return true;
3125 }
3126
3127 TCGCond cond = (a->ne ? TCG_COND_NE : TCG_COND_EQ);
3128 TCGv_i64 rn = read_cpu_reg(s, a->rn, a->sf);
3129 TCGv_i64 rm = read_cpu_reg(s, a->rm, a->sf);
3130 TCGv_i64 cmp = tcg_temp_new_i64();
3131
3132 tcg_gen_setcond_i64(cond, cmp, rn, rm);
3133 tcg_gen_extrl_i64_i32(cpu_NF, cmp);
3134 tcg_temp_free_i64(cmp);
3135
3136 /* VF = !NF & !CF. */
3137 tcg_gen_xori_i32(cpu_VF, cpu_NF, 1);
3138 tcg_gen_andc_i32(cpu_VF, cpu_VF, cpu_CF);
3139
3140 /* Both NF and VF actually look at bit 31. */
3141 tcg_gen_neg_i32(cpu_NF, cpu_NF);
3142 tcg_gen_neg_i32(cpu_VF, cpu_VF);
3143 return true;
3144}
3145
3a7be554 3146static bool trans_WHILE(DisasContext *s, arg_WHILE *a)
caf1cefc 3147{
bbd0968c 3148 TCGv_i64 op0, op1, t0, t1, tmax;
4481bbf2 3149 TCGv_i32 t2;
caf1cefc 3150 TCGv_ptr ptr;
e610906c
RH
3151 unsigned vsz = vec_full_reg_size(s);
3152 unsigned desc = 0;
caf1cefc 3153 TCGCond cond;
34688dbc
RH
3154 uint64_t maxval;
3155 /* Note that GE/HS has a->eq == 0 and GT/HI has a->eq == 1. */
3156 bool eq = a->eq == a->lt;
caf1cefc 3157
34688dbc
RH
3158 /* The greater-than conditions are all SVE2. */
3159 if (!a->lt && !dc_isar_feature(aa64_sve2, s)) {
3160 return false;
3161 }
bbd0968c
RH
3162 if (!sve_access_check(s)) {
3163 return true;
3164 }
3165
3166 op0 = read_cpu_reg(s, a->rn, 1);
3167 op1 = read_cpu_reg(s, a->rm, 1);
3168
caf1cefc
RH
3169 if (!a->sf) {
3170 if (a->u) {
3171 tcg_gen_ext32u_i64(op0, op0);
3172 tcg_gen_ext32u_i64(op1, op1);
3173 } else {
3174 tcg_gen_ext32s_i64(op0, op0);
3175 tcg_gen_ext32s_i64(op1, op1);
3176 }
3177 }
3178
3179 /* For the helper, compress the different conditions into a computation
3180 * of how many iterations for which the condition is true.
caf1cefc 3181 */
bbd0968c
RH
3182 t0 = tcg_temp_new_i64();
3183 t1 = tcg_temp_new_i64();
34688dbc
RH
3184
3185 if (a->lt) {
3186 tcg_gen_sub_i64(t0, op1, op0);
3187 if (a->u) {
3188 maxval = a->sf ? UINT64_MAX : UINT32_MAX;
3189 cond = eq ? TCG_COND_LEU : TCG_COND_LTU;
3190 } else {
3191 maxval = a->sf ? INT64_MAX : INT32_MAX;
3192 cond = eq ? TCG_COND_LE : TCG_COND_LT;
3193 }
3194 } else {
3195 tcg_gen_sub_i64(t0, op0, op1);
3196 if (a->u) {
3197 maxval = 0;
3198 cond = eq ? TCG_COND_GEU : TCG_COND_GTU;
3199 } else {
3200 maxval = a->sf ? INT64_MIN : INT32_MIN;
3201 cond = eq ? TCG_COND_GE : TCG_COND_GT;
3202 }
3203 }
caf1cefc 3204
4481bbf2 3205 tmax = tcg_constant_i64(vsz >> a->esz);
34688dbc 3206 if (eq) {
caf1cefc
RH
3207 /* Equality means one more iteration. */
3208 tcg_gen_addi_i64(t0, t0, 1);
bbd0968c 3209
34688dbc
RH
3210 /*
3211 * For the less-than while, if op1 is maxval (and the only time
3212 * the addition above could overflow), then we produce an all-true
3213 * predicate by setting the count to the vector length. This is
3214 * because the pseudocode is described as an increment + compare
3215 * loop, and the maximum integer would always compare true.
3216 * Similarly, the greater-than while has the same issue with the
3217 * minimum integer due to the decrement + compare loop.
bbd0968c 3218 */
34688dbc 3219 tcg_gen_movi_i64(t1, maxval);
bbd0968c 3220 tcg_gen_movcond_i64(TCG_COND_EQ, t0, op1, t1, tmax, t0);
caf1cefc
RH
3221 }
3222
bbd0968c
RH
3223 /* Bound to the maximum. */
3224 tcg_gen_umin_i64(t0, t0, tmax);
bbd0968c
RH
3225
3226 /* Set the count to zero if the condition is false. */
caf1cefc
RH
3227 tcg_gen_movi_i64(t1, 0);
3228 tcg_gen_movcond_i64(cond, t0, op0, op1, t0, t1);
bbd0968c 3229 tcg_temp_free_i64(t1);
caf1cefc 3230
bbd0968c 3231 /* Since we're bounded, pass as a 32-bit type. */
caf1cefc
RH
3232 t2 = tcg_temp_new_i32();
3233 tcg_gen_extrl_i64_i32(t2, t0);
3234 tcg_temp_free_i64(t0);
bbd0968c
RH
3235
3236 /* Scale elements to bits. */
3237 tcg_gen_shli_i32(t2, t2, a->esz);
caf1cefc 3238
e610906c
RH
3239 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz / 8);
3240 desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
caf1cefc
RH
3241
3242 ptr = tcg_temp_new_ptr();
3243 tcg_gen_addi_ptr(ptr, cpu_env, pred_full_reg_offset(s, a->rd));
3244
34688dbc 3245 if (a->lt) {
4481bbf2 3246 gen_helper_sve_whilel(t2, ptr, t2, tcg_constant_i32(desc));
34688dbc 3247 } else {
4481bbf2 3248 gen_helper_sve_whileg(t2, ptr, t2, tcg_constant_i32(desc));
34688dbc 3249 }
caf1cefc
RH
3250 do_pred_flags(t2);
3251
3252 tcg_temp_free_ptr(ptr);
3253 tcg_temp_free_i32(t2);
caf1cefc
RH
3254 return true;
3255}
3256
14f6dad1
RH
3257static bool trans_WHILE_ptr(DisasContext *s, arg_WHILE_ptr *a)
3258{
3259 TCGv_i64 op0, op1, diff, t1, tmax;
4481bbf2 3260 TCGv_i32 t2;
14f6dad1
RH
3261 TCGv_ptr ptr;
3262 unsigned vsz = vec_full_reg_size(s);
3263 unsigned desc = 0;
3264
3265 if (!dc_isar_feature(aa64_sve2, s)) {
3266 return false;
3267 }
3268 if (!sve_access_check(s)) {
3269 return true;
3270 }
3271
3272 op0 = read_cpu_reg(s, a->rn, 1);
3273 op1 = read_cpu_reg(s, a->rm, 1);
3274
4481bbf2 3275 tmax = tcg_constant_i64(vsz);
14f6dad1
RH
3276 diff = tcg_temp_new_i64();
3277
3278 if (a->rw) {
3279 /* WHILERW */
3280 /* diff = abs(op1 - op0), noting that op0/1 are unsigned. */
3281 t1 = tcg_temp_new_i64();
3282 tcg_gen_sub_i64(diff, op0, op1);
3283 tcg_gen_sub_i64(t1, op1, op0);
3284 tcg_gen_movcond_i64(TCG_COND_GEU, diff, op0, op1, diff, t1);
3285 tcg_temp_free_i64(t1);
3286 /* Round down to a multiple of ESIZE. */
3287 tcg_gen_andi_i64(diff, diff, -1 << a->esz);
3288 /* If op1 == op0, diff == 0, and the condition is always true. */
3289 tcg_gen_movcond_i64(TCG_COND_EQ, diff, op0, op1, tmax, diff);
3290 } else {
3291 /* WHILEWR */
3292 tcg_gen_sub_i64(diff, op1, op0);
3293 /* Round down to a multiple of ESIZE. */
3294 tcg_gen_andi_i64(diff, diff, -1 << a->esz);
3295 /* If op0 >= op1, diff <= 0, the condition is always true. */
3296 tcg_gen_movcond_i64(TCG_COND_GEU, diff, op0, op1, tmax, diff);
3297 }
3298
3299 /* Bound to the maximum. */
3300 tcg_gen_umin_i64(diff, diff, tmax);
14f6dad1
RH
3301
3302 /* Since we're bounded, pass as a 32-bit type. */
3303 t2 = tcg_temp_new_i32();
3304 tcg_gen_extrl_i64_i32(t2, diff);
3305 tcg_temp_free_i64(diff);
3306
3307 desc = FIELD_DP32(desc, PREDDESC, OPRSZ, vsz / 8);
3308 desc = FIELD_DP32(desc, PREDDESC, ESZ, a->esz);
14f6dad1
RH
3309
3310 ptr = tcg_temp_new_ptr();
3311 tcg_gen_addi_ptr(ptr, cpu_env, pred_full_reg_offset(s, a->rd));
3312
4481bbf2 3313 gen_helper_sve_whilel(t2, ptr, t2, tcg_constant_i32(desc));
14f6dad1
RH
3314 do_pred_flags(t2);
3315
3316 tcg_temp_free_ptr(ptr);
3317 tcg_temp_free_i32(t2);
14f6dad1
RH
3318 return true;
3319}
3320
ed491961
RH
3321/*
3322 *** SVE Integer Wide Immediate - Unpredicated Group
3323 */
3324
3a7be554 3325static bool trans_FDUP(DisasContext *s, arg_FDUP *a)
ed491961
RH
3326{
3327 if (a->esz == 0) {
3328 return false;
3329 }
3330 if (sve_access_check(s)) {
3331 unsigned vsz = vec_full_reg_size(s);
3332 int dofs = vec_full_reg_offset(s, a->rd);
3333 uint64_t imm;
3334
3335 /* Decode the VFP immediate. */
3336 imm = vfp_expand_imm(a->esz, a->imm);
8711e71f 3337 tcg_gen_gvec_dup_imm(a->esz, dofs, vsz, vsz, imm);
ed491961
RH
3338 }
3339 return true;
3340}
3341
3a7be554 3342static bool trans_DUP_i(DisasContext *s, arg_DUP_i *a)
ed491961 3343{
3a7be554 3344 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
ed491961
RH
3345 return false;
3346 }
3347 if (sve_access_check(s)) {
3348 unsigned vsz = vec_full_reg_size(s);
3349 int dofs = vec_full_reg_offset(s, a->rd);
3350
8711e71f 3351 tcg_gen_gvec_dup_imm(a->esz, dofs, vsz, vsz, a->imm);
ed491961
RH
3352 }
3353 return true;
3354}
3355
3a7be554 3356static bool trans_ADD_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3357{
3a7be554 3358 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
6e6a157d
RH
3359 return false;
3360 }
ada378f0 3361 return gen_gvec_fn_arg_zzi(s, tcg_gen_gvec_addi, a);
6e6a157d
RH
3362}
3363
3a7be554 3364static bool trans_SUB_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d
RH
3365{
3366 a->imm = -a->imm;
3a7be554 3367 return trans_ADD_zzi(s, a);
6e6a157d
RH
3368}
3369
3a7be554 3370static bool trans_SUBR_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3371{
53229a77 3372 static const TCGOpcode vecop_list[] = { INDEX_op_sub_vec, 0 };
6e6a157d
RH
3373 static const GVecGen2s op[4] = {
3374 { .fni8 = tcg_gen_vec_sub8_i64,
3375 .fniv = tcg_gen_sub_vec,
3376 .fno = gen_helper_sve_subri_b,
53229a77 3377 .opt_opc = vecop_list,
6e6a157d
RH
3378 .vece = MO_8,
3379 .scalar_first = true },
3380 { .fni8 = tcg_gen_vec_sub16_i64,
3381 .fniv = tcg_gen_sub_vec,
3382 .fno = gen_helper_sve_subri_h,
53229a77 3383 .opt_opc = vecop_list,
6e6a157d
RH
3384 .vece = MO_16,
3385 .scalar_first = true },
3386 { .fni4 = tcg_gen_sub_i32,
3387 .fniv = tcg_gen_sub_vec,
3388 .fno = gen_helper_sve_subri_s,
53229a77 3389 .opt_opc = vecop_list,
6e6a157d
RH
3390 .vece = MO_32,
3391 .scalar_first = true },
3392 { .fni8 = tcg_gen_sub_i64,
3393 .fniv = tcg_gen_sub_vec,
3394 .fno = gen_helper_sve_subri_d,
53229a77 3395 .opt_opc = vecop_list,
6e6a157d
RH
3396 .prefer_i64 = TCG_TARGET_REG_BITS == 64,
3397 .vece = MO_64,
3398 .scalar_first = true }
3399 };
3400
3a7be554 3401 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
6e6a157d
RH
3402 return false;
3403 }
3404 if (sve_access_check(s)) {
3405 unsigned vsz = vec_full_reg_size(s);
6e6a157d
RH
3406 tcg_gen_gvec_2s(vec_full_reg_offset(s, a->rd),
3407 vec_full_reg_offset(s, a->rn),
9fff3fcc 3408 vsz, vsz, tcg_constant_i64(a->imm), &op[a->esz]);
6e6a157d
RH
3409 }
3410 return true;
3411}
3412
3a7be554 3413static bool trans_MUL_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d
RH
3414{
3415 if (sve_access_check(s)) {
3416 unsigned vsz = vec_full_reg_size(s);
3417 tcg_gen_gvec_muli(a->esz, vec_full_reg_offset(s, a->rd),
3418 vec_full_reg_offset(s, a->rn), a->imm, vsz, vsz);
3419 }
3420 return true;
3421}
3422
3a7be554 3423static bool do_zzi_sat(DisasContext *s, arg_rri_esz *a, bool u, bool d)
6e6a157d 3424{
3a7be554 3425 if (a->esz == 0 && extract32(s->insn, 13, 1)) {
6e6a157d
RH
3426 return false;
3427 }
3428 if (sve_access_check(s)) {
138a1f7b
RH
3429 do_sat_addsub_vec(s, a->esz, a->rd, a->rn,
3430 tcg_constant_i64(a->imm), u, d);
6e6a157d
RH
3431 }
3432 return true;
3433}
3434
3a7be554 3435static bool trans_SQADD_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3436{
3a7be554 3437 return do_zzi_sat(s, a, false, false);
6e6a157d
RH
3438}
3439
3a7be554 3440static bool trans_UQADD_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3441{
3a7be554 3442 return do_zzi_sat(s, a, true, false);
6e6a157d
RH
3443}
3444
3a7be554 3445static bool trans_SQSUB_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3446{
3a7be554 3447 return do_zzi_sat(s, a, false, true);
6e6a157d
RH
3448}
3449
3a7be554 3450static bool trans_UQSUB_zzi(DisasContext *s, arg_rri_esz *a)
6e6a157d 3451{
3a7be554 3452 return do_zzi_sat(s, a, true, true);
6e6a157d
RH
3453}
3454
3455static bool do_zzi_ool(DisasContext *s, arg_rri_esz *a, gen_helper_gvec_2i *fn)
3456{
3457 if (sve_access_check(s)) {
3458 unsigned vsz = vec_full_reg_size(s);
6e6a157d
RH
3459 tcg_gen_gvec_2i_ool(vec_full_reg_offset(s, a->rd),
3460 vec_full_reg_offset(s, a->rn),
138a1f7b 3461 tcg_constant_i64(a->imm), vsz, vsz, 0, fn);
6e6a157d
RH
3462 }
3463 return true;
3464}
3465
3466#define DO_ZZI(NAME, name) \
3a7be554 3467static bool trans_##NAME##_zzi(DisasContext *s, arg_rri_esz *a) \
6e6a157d
RH
3468{ \
3469 static gen_helper_gvec_2i * const fns[4] = { \
3470 gen_helper_sve_##name##i_b, gen_helper_sve_##name##i_h, \
3471 gen_helper_sve_##name##i_s, gen_helper_sve_##name##i_d, \
3472 }; \
3473 return do_zzi_ool(s, a, fns[a->esz]); \
3474}
3475
3476DO_ZZI(SMAX, smax)
3477DO_ZZI(UMAX, umax)
3478DO_ZZI(SMIN, smin)
3479DO_ZZI(UMIN, umin)
3480
3481#undef DO_ZZI
3482
5f425b92
RH
3483static gen_helper_gvec_4 * const dot_fns[2][2] = {
3484 { gen_helper_gvec_sdot_b, gen_helper_gvec_sdot_h },
3485 { gen_helper_gvec_udot_b, gen_helper_gvec_udot_h }
3486};
3487TRANS_FEAT(DOT_zzzz, aa64_sve, gen_gvec_ool_zzzz,
3488 dot_fns[a->u][a->sz], a->rd, a->rn, a->rm, a->ra, 0)
d730ecaa 3489
814d4c52
RH
3490/*
3491 * SVE Multiply - Indexed
3492 */
3493
f3500a25
RH
3494TRANS_FEAT(SDOT_zzxw_s, aa64_sve, gen_gvec_ool_arg_zzxz,
3495 gen_helper_gvec_sdot_idx_b, a)
3496TRANS_FEAT(SDOT_zzxw_d, aa64_sve, gen_gvec_ool_arg_zzxz,
3497 gen_helper_gvec_sdot_idx_h, a)
3498TRANS_FEAT(UDOT_zzxw_s, aa64_sve, gen_gvec_ool_arg_zzxz,
3499 gen_helper_gvec_udot_idx_b, a)
3500TRANS_FEAT(UDOT_zzxw_d, aa64_sve, gen_gvec_ool_arg_zzxz,
3501 gen_helper_gvec_udot_idx_h, a)
3502
3503TRANS_FEAT(SUDOT_zzxw_s, aa64_sve_i8mm, gen_gvec_ool_arg_zzxz,
3504 gen_helper_gvec_sudot_idx_b, a)
3505TRANS_FEAT(USDOT_zzxw_s, aa64_sve_i8mm, gen_gvec_ool_arg_zzxz,
3506 gen_helper_gvec_usdot_idx_b, a)
16fcfdc7 3507
814d4c52 3508#define DO_SVE2_RRX(NAME, FUNC) \
af031f64
RH
3509 TRANS_FEAT(NAME, aa64_sve, gen_gvec_ool_zzz, FUNC, \
3510 a->rd, a->rn, a->rm, a->index)
814d4c52 3511
af031f64
RH
3512DO_SVE2_RRX(MUL_zzx_h, gen_helper_gvec_mul_idx_h)
3513DO_SVE2_RRX(MUL_zzx_s, gen_helper_gvec_mul_idx_s)
3514DO_SVE2_RRX(MUL_zzx_d, gen_helper_gvec_mul_idx_d)
814d4c52 3515
af031f64
RH
3516DO_SVE2_RRX(SQDMULH_zzx_h, gen_helper_sve2_sqdmulh_idx_h)
3517DO_SVE2_RRX(SQDMULH_zzx_s, gen_helper_sve2_sqdmulh_idx_s)
3518DO_SVE2_RRX(SQDMULH_zzx_d, gen_helper_sve2_sqdmulh_idx_d)
1aee2d70 3519
af031f64
RH
3520DO_SVE2_RRX(SQRDMULH_zzx_h, gen_helper_sve2_sqrdmulh_idx_h)
3521DO_SVE2_RRX(SQRDMULH_zzx_s, gen_helper_sve2_sqrdmulh_idx_s)
3522DO_SVE2_RRX(SQRDMULH_zzx_d, gen_helper_sve2_sqrdmulh_idx_d)
1aee2d70 3523
814d4c52
RH
3524#undef DO_SVE2_RRX
3525
b95f5eeb 3526#define DO_SVE2_RRX_TB(NAME, FUNC, TOP) \
af031f64
RH
3527 TRANS_FEAT(NAME, aa64_sve, gen_gvec_ool_zzz, FUNC, \
3528 a->rd, a->rn, a->rm, (a->index << 1) | TOP)
3529
3530DO_SVE2_RRX_TB(SQDMULLB_zzx_s, gen_helper_sve2_sqdmull_idx_s, false)
3531DO_SVE2_RRX_TB(SQDMULLB_zzx_d, gen_helper_sve2_sqdmull_idx_d, false)
3532DO_SVE2_RRX_TB(SQDMULLT_zzx_s, gen_helper_sve2_sqdmull_idx_s, true)
3533DO_SVE2_RRX_TB(SQDMULLT_zzx_d, gen_helper_sve2_sqdmull_idx_d, true)
3534
3535DO_SVE2_RRX_TB(SMULLB_zzx_s, gen_helper_sve2_smull_idx_s, false)
3536DO_SVE2_RRX_TB(SMULLB_zzx_d, gen_helper_sve2_smull_idx_d, false)
3537DO_SVE2_RRX_TB(SMULLT_zzx_s, gen_helper_sve2_smull_idx_s, true)
3538DO_SVE2_RRX_TB(SMULLT_zzx_d, gen_helper_sve2_smull_idx_d, true)
3539
3540DO_SVE2_RRX_TB(UMULLB_zzx_s, gen_helper_sve2_umull_idx_s, false)
3541DO_SVE2_RRX_TB(UMULLB_zzx_d, gen_helper_sve2_umull_idx_d, false)
3542DO_SVE2_RRX_TB(UMULLT_zzx_s, gen_helper_sve2_umull_idx_s, true)
3543DO_SVE2_RRX_TB(UMULLT_zzx_d, gen_helper_sve2_umull_idx_d, true)
d3949c4c 3544
b95f5eeb
RH
3545#undef DO_SVE2_RRX_TB
3546
8a02aac7 3547#define DO_SVE2_RRXR(NAME, FUNC) \
8681eb76 3548 TRANS_FEAT(NAME, aa64_sve2, gen_gvec_ool_arg_zzxz, FUNC, a)
8a02aac7 3549
8681eb76
RH
3550DO_SVE2_RRXR(MLA_zzxz_h, gen_helper_gvec_mla_idx_h)
3551DO_SVE2_RRXR(MLA_zzxz_s, gen_helper_gvec_mla_idx_s)
3552DO_SVE2_RRXR(MLA_zzxz_d, gen_helper_gvec_mla_idx_d)
8a02aac7 3553
8681eb76
RH
3554DO_SVE2_RRXR(MLS_zzxz_h, gen_helper_gvec_mls_idx_h)
3555DO_SVE2_RRXR(MLS_zzxz_s, gen_helper_gvec_mls_idx_s)
3556DO_SVE2_RRXR(MLS_zzxz_d, gen_helper_gvec_mls_idx_d)
8a02aac7 3557
8681eb76
RH
3558DO_SVE2_RRXR(SQRDMLAH_zzxz_h, gen_helper_sve2_sqrdmlah_idx_h)
3559DO_SVE2_RRXR(SQRDMLAH_zzxz_s, gen_helper_sve2_sqrdmlah_idx_s)
3560DO_SVE2_RRXR(SQRDMLAH_zzxz_d, gen_helper_sve2_sqrdmlah_idx_d)
75d6d5fc 3561
8681eb76
RH
3562DO_SVE2_RRXR(SQRDMLSH_zzxz_h, gen_helper_sve2_sqrdmlsh_idx_h)
3563DO_SVE2_RRXR(SQRDMLSH_zzxz_s, gen_helper_sve2_sqrdmlsh_idx_s)
3564DO_SVE2_RRXR(SQRDMLSH_zzxz_d, gen_helper_sve2_sqrdmlsh_idx_d)
75d6d5fc 3565
8a02aac7
RH
3566#undef DO_SVE2_RRXR
3567
c5c455d7 3568#define DO_SVE2_RRXR_TB(NAME, FUNC, TOP) \
8681eb76
RH
3569 TRANS_FEAT(NAME, aa64_sve2, gen_gvec_ool_zzzz, FUNC, \
3570 a->rd, a->rn, a->rm, a->ra, (a->index << 1) | TOP)
3571
3572DO_SVE2_RRXR_TB(SQDMLALB_zzxw_s, gen_helper_sve2_sqdmlal_idx_s, false)
3573DO_SVE2_RRXR_TB(SQDMLALB_zzxw_d, gen_helper_sve2_sqdmlal_idx_d, false)
3574DO_SVE2_RRXR_TB(SQDMLALT_zzxw_s, gen_helper_sve2_sqdmlal_idx_s, true)
3575DO_SVE2_RRXR_TB(SQDMLALT_zzxw_d, gen_helper_sve2_sqdmlal_idx_d, true)
3576
3577DO_SVE2_RRXR_TB(SQDMLSLB_zzxw_s, gen_helper_sve2_sqdmlsl_idx_s, false)
3578DO_SVE2_RRXR_TB(SQDMLSLB_zzxw_d, gen_helper_sve2_sqdmlsl_idx_d, false)
3579DO_SVE2_RRXR_TB(SQDMLSLT_zzxw_s, gen_helper_sve2_sqdmlsl_idx_s, true)
3580DO_SVE2_RRXR_TB(SQDMLSLT_zzxw_d, gen_helper_sve2_sqdmlsl_idx_d, true)
3581
3582DO_SVE2_RRXR_TB(SMLALB_zzxw_s, gen_helper_sve2_smlal_idx_s, false)
3583DO_SVE2_RRXR_TB(SMLALB_zzxw_d, gen_helper_sve2_smlal_idx_d, false)
3584DO_SVE2_RRXR_TB(SMLALT_zzxw_s, gen_helper_sve2_smlal_idx_s, true)
3585DO_SVE2_RRXR_TB(SMLALT_zzxw_d, gen_helper_sve2_smlal_idx_d, true)
3586
3587DO_SVE2_RRXR_TB(UMLALB_zzxw_s, gen_helper_sve2_umlal_idx_s, false)
3588DO_SVE2_RRXR_TB(UMLALB_zzxw_d, gen_helper_sve2_umlal_idx_d, false)
3589DO_SVE2_RRXR_TB(UMLALT_zzxw_s, gen_helper_sve2_umlal_idx_s, true)
3590DO_SVE2_RRXR_TB(UMLALT_zzxw_d, gen_helper_sve2_umlal_idx_d, true)
3591
3592DO_SVE2_RRXR_TB(SMLSLB_zzxw_s, gen_helper_sve2_smlsl_idx_s, false)
3593DO_SVE2_RRXR_TB(SMLSLB_zzxw_d, gen_helper_sve2_smlsl_idx_d, false)
3594DO_SVE2_RRXR_TB(SMLSLT_zzxw_s, gen_helper_sve2_smlsl_idx_s, true)
3595DO_SVE2_RRXR_TB(SMLSLT_zzxw_d, gen_helper_sve2_smlsl_idx_d, true)
3596
3597DO_SVE2_RRXR_TB(UMLSLB_zzxw_s, gen_helper_sve2_umlsl_idx_s, false)
3598DO_SVE2_RRXR_TB(UMLSLB_zzxw_d, gen_helper_sve2_umlsl_idx_d, false)
3599DO_SVE2_RRXR_TB(UMLSLT_zzxw_s, gen_helper_sve2_umlsl_idx_s, true)
3600DO_SVE2_RRXR_TB(UMLSLT_zzxw_d, gen_helper_sve2_umlsl_idx_d, true)
c5c455d7
RH
3601
3602#undef DO_SVE2_RRXR_TB
3603
3b787ed8 3604#define DO_SVE2_RRXR_ROT(NAME, FUNC) \
8681eb76
RH
3605 TRANS_FEAT(NAME, aa64_sve2, gen_gvec_ool_zzzz, FUNC, \
3606 a->rd, a->rn, a->rm, a->ra, (a->index << 2) | a->rot)
3b787ed8
RH
3607
3608DO_SVE2_RRXR_ROT(CMLA_zzxz_h, gen_helper_sve2_cmla_idx_h)
3609DO_SVE2_RRXR_ROT(CMLA_zzxz_s, gen_helper_sve2_cmla_idx_s)
3610
3611DO_SVE2_RRXR_ROT(SQRDCMLAH_zzxz_h, gen_helper_sve2_sqrdcmlah_idx_h)
3612DO_SVE2_RRXR_ROT(SQRDCMLAH_zzxz_s, gen_helper_sve2_sqrdcmlah_idx_s)
3613
21068f39
RH
3614DO_SVE2_RRXR_ROT(CDOT_zzxw_s, gen_helper_sve2_cdot_idx_s)
3615DO_SVE2_RRXR_ROT(CDOT_zzxw_d, gen_helper_sve2_cdot_idx_d)
3616
3b787ed8
RH
3617#undef DO_SVE2_RRXR_ROT
3618
ca40a6e6
RH
3619/*
3620 *** SVE Floating Point Multiply-Add Indexed Group
3621 */
3622
0a82d963 3623static bool do_FMLA_zzxz(DisasContext *s, arg_rrxr_esz *a, bool sub)
ca40a6e6
RH
3624{
3625 static gen_helper_gvec_4_ptr * const fns[3] = {
3626 gen_helper_gvec_fmla_idx_h,
3627 gen_helper_gvec_fmla_idx_s,
3628 gen_helper_gvec_fmla_idx_d,
3629 };
3630
3631 if (sve_access_check(s)) {
3632 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3633 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
ca40a6e6
RH
3634 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
3635 vec_full_reg_offset(s, a->rn),
3636 vec_full_reg_offset(s, a->rm),
3637 vec_full_reg_offset(s, a->ra),
0a82d963 3638 status, vsz, vsz, (a->index << 1) | sub,
ca40a6e6
RH
3639 fns[a->esz - 1]);
3640 tcg_temp_free_ptr(status);
3641 }
3642 return true;
3643}
3644
0a82d963
RH
3645static bool trans_FMLA_zzxz(DisasContext *s, arg_FMLA_zzxz *a)
3646{
3647 return do_FMLA_zzxz(s, a, false);
3648}
3649
3650static bool trans_FMLS_zzxz(DisasContext *s, arg_FMLA_zzxz *a)
3651{
3652 return do_FMLA_zzxz(s, a, true);
3653}
3654
ca40a6e6
RH
3655/*
3656 *** SVE Floating Point Multiply Indexed Group
3657 */
3658
3a7be554 3659static bool trans_FMUL_zzx(DisasContext *s, arg_FMUL_zzx *a)
ca40a6e6
RH
3660{
3661 static gen_helper_gvec_3_ptr * const fns[3] = {
3662 gen_helper_gvec_fmul_idx_h,
3663 gen_helper_gvec_fmul_idx_s,
3664 gen_helper_gvec_fmul_idx_d,
3665 };
3666
3667 if (sve_access_check(s)) {
3668 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3669 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
ca40a6e6
RH
3670 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
3671 vec_full_reg_offset(s, a->rn),
3672 vec_full_reg_offset(s, a->rm),
3673 status, vsz, vsz, a->index, fns[a->esz - 1]);
3674 tcg_temp_free_ptr(status);
3675 }
3676 return true;
3677}
3678
23fbe79f
RH
3679/*
3680 *** SVE Floating Point Fast Reduction Group
3681 */
3682
3683typedef void gen_helper_fp_reduce(TCGv_i64, TCGv_ptr, TCGv_ptr,
3684 TCGv_ptr, TCGv_i32);
3685
3686static void do_reduce(DisasContext *s, arg_rpr_esz *a,
3687 gen_helper_fp_reduce *fn)
3688{
3689 unsigned vsz = vec_full_reg_size(s);
3690 unsigned p2vsz = pow2ceil(vsz);
c6a59b55 3691 TCGv_i32 t_desc = tcg_constant_i32(simd_desc(vsz, vsz, p2vsz));
23fbe79f
RH
3692 TCGv_ptr t_zn, t_pg, status;
3693 TCGv_i64 temp;
3694
3695 temp = tcg_temp_new_i64();
3696 t_zn = tcg_temp_new_ptr();
3697 t_pg = tcg_temp_new_ptr();
3698
3699 tcg_gen_addi_ptr(t_zn, cpu_env, vec_full_reg_offset(s, a->rn));
3700 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, a->pg));
cdfb22bb 3701 status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
23fbe79f
RH
3702
3703 fn(temp, t_zn, t_pg, status, t_desc);
3704 tcg_temp_free_ptr(t_zn);
3705 tcg_temp_free_ptr(t_pg);
3706 tcg_temp_free_ptr(status);
23fbe79f
RH
3707
3708 write_fp_dreg(s, a->rd, temp);
3709 tcg_temp_free_i64(temp);
3710}
3711
3712#define DO_VPZ(NAME, name) \
3a7be554 3713static bool trans_##NAME(DisasContext *s, arg_rpr_esz *a) \
23fbe79f
RH
3714{ \
3715 static gen_helper_fp_reduce * const fns[3] = { \
3716 gen_helper_sve_##name##_h, \
3717 gen_helper_sve_##name##_s, \
3718 gen_helper_sve_##name##_d, \
3719 }; \
3720 if (a->esz == 0) { \
3721 return false; \
3722 } \
3723 if (sve_access_check(s)) { \
3724 do_reduce(s, a, fns[a->esz - 1]); \
3725 } \
3726 return true; \
3727}
3728
3729DO_VPZ(FADDV, faddv)
3730DO_VPZ(FMINNMV, fminnmv)
3731DO_VPZ(FMAXNMV, fmaxnmv)
3732DO_VPZ(FMINV, fminv)
3733DO_VPZ(FMAXV, fmaxv)
3734
3887c038
RH
3735/*
3736 *** SVE Floating Point Unary Operations - Unpredicated Group
3737 */
3738
3739static void do_zz_fp(DisasContext *s, arg_rr_esz *a, gen_helper_gvec_2_ptr *fn)
3740{
3741 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3742 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
3887c038
RH
3743
3744 tcg_gen_gvec_2_ptr(vec_full_reg_offset(s, a->rd),
3745 vec_full_reg_offset(s, a->rn),
3746 status, vsz, vsz, 0, fn);
3747 tcg_temp_free_ptr(status);
3748}
3749
3a7be554 3750static bool trans_FRECPE(DisasContext *s, arg_rr_esz *a)
3887c038
RH
3751{
3752 static gen_helper_gvec_2_ptr * const fns[3] = {
3753 gen_helper_gvec_frecpe_h,
3754 gen_helper_gvec_frecpe_s,
3755 gen_helper_gvec_frecpe_d,
3756 };
3757 if (a->esz == 0) {
3758 return false;
3759 }
3760 if (sve_access_check(s)) {
3761 do_zz_fp(s, a, fns[a->esz - 1]);
3762 }
3763 return true;
3764}
3765
3a7be554 3766static bool trans_FRSQRTE(DisasContext *s, arg_rr_esz *a)
3887c038
RH
3767{
3768 static gen_helper_gvec_2_ptr * const fns[3] = {
3769 gen_helper_gvec_frsqrte_h,
3770 gen_helper_gvec_frsqrte_s,
3771 gen_helper_gvec_frsqrte_d,
3772 };
3773 if (a->esz == 0) {
3774 return false;
3775 }
3776 if (sve_access_check(s)) {
3777 do_zz_fp(s, a, fns[a->esz - 1]);
3778 }
3779 return true;
3780}
3781
4d2e2a03
RH
3782/*
3783 *** SVE Floating Point Compare with Zero Group
3784 */
3785
3786static void do_ppz_fp(DisasContext *s, arg_rpr_esz *a,
3787 gen_helper_gvec_3_ptr *fn)
3788{
3789 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3790 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
4d2e2a03
RH
3791
3792 tcg_gen_gvec_3_ptr(pred_full_reg_offset(s, a->rd),
3793 vec_full_reg_offset(s, a->rn),
3794 pred_full_reg_offset(s, a->pg),
3795 status, vsz, vsz, 0, fn);
3796 tcg_temp_free_ptr(status);
3797}
3798
3799#define DO_PPZ(NAME, name) \
3a7be554 3800static bool trans_##NAME(DisasContext *s, arg_rpr_esz *a) \
4d2e2a03
RH
3801{ \
3802 static gen_helper_gvec_3_ptr * const fns[3] = { \
3803 gen_helper_sve_##name##_h, \
3804 gen_helper_sve_##name##_s, \
3805 gen_helper_sve_##name##_d, \
3806 }; \
3807 if (a->esz == 0) { \
3808 return false; \
3809 } \
3810 if (sve_access_check(s)) { \
3811 do_ppz_fp(s, a, fns[a->esz - 1]); \
3812 } \
3813 return true; \
3814}
3815
3816DO_PPZ(FCMGE_ppz0, fcmge0)
3817DO_PPZ(FCMGT_ppz0, fcmgt0)
3818DO_PPZ(FCMLE_ppz0, fcmle0)
3819DO_PPZ(FCMLT_ppz0, fcmlt0)
3820DO_PPZ(FCMEQ_ppz0, fcmeq0)
3821DO_PPZ(FCMNE_ppz0, fcmne0)
3822
3823#undef DO_PPZ
3824
67fcd9ad
RH
3825/*
3826 *** SVE floating-point trig multiply-add coefficient
3827 */
3828
3a7be554 3829static bool trans_FTMAD(DisasContext *s, arg_FTMAD *a)
67fcd9ad
RH
3830{
3831 static gen_helper_gvec_3_ptr * const fns[3] = {
3832 gen_helper_sve_ftmad_h,
3833 gen_helper_sve_ftmad_s,
3834 gen_helper_sve_ftmad_d,
3835 };
3836
3837 if (a->esz == 0) {
3838 return false;
3839 }
3840 if (sve_access_check(s)) {
3841 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3842 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
67fcd9ad
RH
3843 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
3844 vec_full_reg_offset(s, a->rn),
3845 vec_full_reg_offset(s, a->rm),
3846 status, vsz, vsz, a->imm, fns[a->esz - 1]);
3847 tcg_temp_free_ptr(status);
3848 }
3849 return true;
3850}
3851
7f9ddf64
RH
3852/*
3853 *** SVE Floating Point Accumulating Reduction Group
3854 */
3855
3a7be554 3856static bool trans_FADDA(DisasContext *s, arg_rprr_esz *a)
7f9ddf64
RH
3857{
3858 typedef void fadda_fn(TCGv_i64, TCGv_i64, TCGv_ptr,
3859 TCGv_ptr, TCGv_ptr, TCGv_i32);
3860 static fadda_fn * const fns[3] = {
3861 gen_helper_sve_fadda_h,
3862 gen_helper_sve_fadda_s,
3863 gen_helper_sve_fadda_d,
3864 };
3865 unsigned vsz = vec_full_reg_size(s);
3866 TCGv_ptr t_rm, t_pg, t_fpst;
3867 TCGv_i64 t_val;
3868 TCGv_i32 t_desc;
3869
3870 if (a->esz == 0) {
3871 return false;
3872 }
3873 if (!sve_access_check(s)) {
3874 return true;
3875 }
3876
3877 t_val = load_esz(cpu_env, vec_reg_offset(s, a->rn, 0, a->esz), a->esz);
3878 t_rm = tcg_temp_new_ptr();
3879 t_pg = tcg_temp_new_ptr();
3880 tcg_gen_addi_ptr(t_rm, cpu_env, vec_full_reg_offset(s, a->rm));
3881 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, a->pg));
cdfb22bb 3882 t_fpst = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
c6a59b55 3883 t_desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
7f9ddf64
RH
3884
3885 fns[a->esz - 1](t_val, t_val, t_rm, t_pg, t_fpst, t_desc);
3886
7f9ddf64
RH
3887 tcg_temp_free_ptr(t_fpst);
3888 tcg_temp_free_ptr(t_pg);
3889 tcg_temp_free_ptr(t_rm);
3890
3891 write_fp_dreg(s, a->rd, t_val);
3892 tcg_temp_free_i64(t_val);
3893 return true;
3894}
3895
29b80469
RH
3896/*
3897 *** SVE Floating Point Arithmetic - Unpredicated Group
3898 */
3899
3900static bool do_zzz_fp(DisasContext *s, arg_rrr_esz *a,
3901 gen_helper_gvec_3_ptr *fn)
3902{
3903 if (fn == NULL) {
3904 return false;
3905 }
3906 if (sve_access_check(s)) {
3907 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3908 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
29b80469
RH
3909 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
3910 vec_full_reg_offset(s, a->rn),
3911 vec_full_reg_offset(s, a->rm),
3912 status, vsz, vsz, 0, fn);
3913 tcg_temp_free_ptr(status);
3914 }
3915 return true;
3916}
3917
3918
3919#define DO_FP3(NAME, name) \
3a7be554 3920static bool trans_##NAME(DisasContext *s, arg_rrr_esz *a) \
29b80469
RH
3921{ \
3922 static gen_helper_gvec_3_ptr * const fns[4] = { \
3923 NULL, gen_helper_gvec_##name##_h, \
3924 gen_helper_gvec_##name##_s, gen_helper_gvec_##name##_d \
3925 }; \
3926 return do_zzz_fp(s, a, fns[a->esz]); \
3927}
3928
3929DO_FP3(FADD_zzz, fadd)
3930DO_FP3(FSUB_zzz, fsub)
3931DO_FP3(FMUL_zzz, fmul)
3932DO_FP3(FTSMUL, ftsmul)
3933DO_FP3(FRECPS, recps)
3934DO_FP3(FRSQRTS, rsqrts)
3935
3936#undef DO_FP3
3937
ec3b87c2
RH
3938/*
3939 *** SVE Floating Point Arithmetic - Predicated Group
3940 */
3941
3942static bool do_zpzz_fp(DisasContext *s, arg_rprr_esz *a,
3943 gen_helper_gvec_4_ptr *fn)
3944{
3945 if (fn == NULL) {
3946 return false;
3947 }
3948 if (sve_access_check(s)) {
3949 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 3950 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
ec3b87c2
RH
3951 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
3952 vec_full_reg_offset(s, a->rn),
3953 vec_full_reg_offset(s, a->rm),
3954 pred_full_reg_offset(s, a->pg),
3955 status, vsz, vsz, 0, fn);
3956 tcg_temp_free_ptr(status);
3957 }
3958 return true;
3959}
3960
3961#define DO_FP3(NAME, name) \
3a7be554 3962static bool trans_##NAME(DisasContext *s, arg_rprr_esz *a) \
ec3b87c2
RH
3963{ \
3964 static gen_helper_gvec_4_ptr * const fns[4] = { \
3965 NULL, gen_helper_sve_##name##_h, \
3966 gen_helper_sve_##name##_s, gen_helper_sve_##name##_d \
3967 }; \
3968 return do_zpzz_fp(s, a, fns[a->esz]); \
3969}
3970
3971DO_FP3(FADD_zpzz, fadd)
3972DO_FP3(FSUB_zpzz, fsub)
3973DO_FP3(FMUL_zpzz, fmul)
3974DO_FP3(FMIN_zpzz, fmin)
3975DO_FP3(FMAX_zpzz, fmax)
3976DO_FP3(FMINNM_zpzz, fminnum)
3977DO_FP3(FMAXNM_zpzz, fmaxnum)
3978DO_FP3(FABD, fabd)
3979DO_FP3(FSCALE, fscalbn)
3980DO_FP3(FDIV, fdiv)
3981DO_FP3(FMULX, fmulx)
3982
3983#undef DO_FP3
8092c6a3 3984
cc48affe
RH
3985typedef void gen_helper_sve_fp2scalar(TCGv_ptr, TCGv_ptr, TCGv_ptr,
3986 TCGv_i64, TCGv_ptr, TCGv_i32);
3987
3988static void do_fp_scalar(DisasContext *s, int zd, int zn, int pg, bool is_fp16,
3989 TCGv_i64 scalar, gen_helper_sve_fp2scalar *fn)
3990{
3991 unsigned vsz = vec_full_reg_size(s);
3992 TCGv_ptr t_zd, t_zn, t_pg, status;
3993 TCGv_i32 desc;
3994
3995 t_zd = tcg_temp_new_ptr();
3996 t_zn = tcg_temp_new_ptr();
3997 t_pg = tcg_temp_new_ptr();
3998 tcg_gen_addi_ptr(t_zd, cpu_env, vec_full_reg_offset(s, zd));
3999 tcg_gen_addi_ptr(t_zn, cpu_env, vec_full_reg_offset(s, zn));
4000 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, pg));
4001
cdfb22bb 4002 status = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
c6a59b55 4003 desc = tcg_constant_i32(simd_desc(vsz, vsz, 0));
cc48affe
RH
4004 fn(t_zd, t_zn, t_pg, scalar, status, desc);
4005
cc48affe
RH
4006 tcg_temp_free_ptr(status);
4007 tcg_temp_free_ptr(t_pg);
4008 tcg_temp_free_ptr(t_zn);
4009 tcg_temp_free_ptr(t_zd);
4010}
4011
4012static void do_fp_imm(DisasContext *s, arg_rpri_esz *a, uint64_t imm,
4013 gen_helper_sve_fp2scalar *fn)
4014{
138a1f7b
RH
4015 do_fp_scalar(s, a->rd, a->rn, a->pg, a->esz == MO_16,
4016 tcg_constant_i64(imm), fn);
cc48affe
RH
4017}
4018
4019#define DO_FP_IMM(NAME, name, const0, const1) \
3a7be554 4020static bool trans_##NAME##_zpzi(DisasContext *s, arg_rpri_esz *a) \
cc48affe
RH
4021{ \
4022 static gen_helper_sve_fp2scalar * const fns[3] = { \
4023 gen_helper_sve_##name##_h, \
4024 gen_helper_sve_##name##_s, \
4025 gen_helper_sve_##name##_d \
4026 }; \
4027 static uint64_t const val[3][2] = { \
4028 { float16_##const0, float16_##const1 }, \
4029 { float32_##const0, float32_##const1 }, \
4030 { float64_##const0, float64_##const1 }, \
4031 }; \
4032 if (a->esz == 0) { \
4033 return false; \
4034 } \
4035 if (sve_access_check(s)) { \
4036 do_fp_imm(s, a, val[a->esz - 1][a->imm], fns[a->esz - 1]); \
4037 } \
4038 return true; \
4039}
4040
cc48affe
RH
4041DO_FP_IMM(FADD, fadds, half, one)
4042DO_FP_IMM(FSUB, fsubs, half, one)
4043DO_FP_IMM(FMUL, fmuls, half, two)
4044DO_FP_IMM(FSUBR, fsubrs, half, one)
4045DO_FP_IMM(FMAXNM, fmaxnms, zero, one)
4046DO_FP_IMM(FMINNM, fminnms, zero, one)
4047DO_FP_IMM(FMAX, fmaxs, zero, one)
4048DO_FP_IMM(FMIN, fmins, zero, one)
4049
4050#undef DO_FP_IMM
4051
abfdefd5
RH
4052static bool do_fp_cmp(DisasContext *s, arg_rprr_esz *a,
4053 gen_helper_gvec_4_ptr *fn)
4054{
4055 if (fn == NULL) {
4056 return false;
4057 }
4058 if (sve_access_check(s)) {
4059 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4060 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
abfdefd5
RH
4061 tcg_gen_gvec_4_ptr(pred_full_reg_offset(s, a->rd),
4062 vec_full_reg_offset(s, a->rn),
4063 vec_full_reg_offset(s, a->rm),
4064 pred_full_reg_offset(s, a->pg),
4065 status, vsz, vsz, 0, fn);
4066 tcg_temp_free_ptr(status);
4067 }
4068 return true;
4069}
4070
4071#define DO_FPCMP(NAME, name) \
3a7be554 4072static bool trans_##NAME##_ppzz(DisasContext *s, arg_rprr_esz *a) \
abfdefd5
RH
4073{ \
4074 static gen_helper_gvec_4_ptr * const fns[4] = { \
4075 NULL, gen_helper_sve_##name##_h, \
4076 gen_helper_sve_##name##_s, gen_helper_sve_##name##_d \
4077 }; \
4078 return do_fp_cmp(s, a, fns[a->esz]); \
4079}
4080
4081DO_FPCMP(FCMGE, fcmge)
4082DO_FPCMP(FCMGT, fcmgt)
4083DO_FPCMP(FCMEQ, fcmeq)
4084DO_FPCMP(FCMNE, fcmne)
4085DO_FPCMP(FCMUO, fcmuo)
4086DO_FPCMP(FACGE, facge)
4087DO_FPCMP(FACGT, facgt)
4088
4089#undef DO_FPCMP
4090
3a7be554 4091static bool trans_FCADD(DisasContext *s, arg_FCADD *a)
76a9d9cd
RH
4092{
4093 static gen_helper_gvec_4_ptr * const fns[3] = {
4094 gen_helper_sve_fcadd_h,
4095 gen_helper_sve_fcadd_s,
4096 gen_helper_sve_fcadd_d
4097 };
4098
4099 if (a->esz == 0) {
4100 return false;
4101 }
4102 if (sve_access_check(s)) {
4103 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4104 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
76a9d9cd
RH
4105 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
4106 vec_full_reg_offset(s, a->rn),
4107 vec_full_reg_offset(s, a->rm),
4108 pred_full_reg_offset(s, a->pg),
4109 status, vsz, vsz, a->rot, fns[a->esz - 1]);
4110 tcg_temp_free_ptr(status);
4111 }
4112 return true;
4113}
4114
08975da9
RH
4115static bool do_fmla(DisasContext *s, arg_rprrr_esz *a,
4116 gen_helper_gvec_5_ptr *fn)
6ceabaad 4117{
08975da9 4118 if (a->esz == 0) {
6ceabaad
RH
4119 return false;
4120 }
08975da9
RH
4121 if (sve_access_check(s)) {
4122 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4123 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
08975da9
RH
4124 tcg_gen_gvec_5_ptr(vec_full_reg_offset(s, a->rd),
4125 vec_full_reg_offset(s, a->rn),
4126 vec_full_reg_offset(s, a->rm),
4127 vec_full_reg_offset(s, a->ra),
4128 pred_full_reg_offset(s, a->pg),
4129 status, vsz, vsz, 0, fn);
4130 tcg_temp_free_ptr(status);
6ceabaad 4131 }
6ceabaad
RH
4132 return true;
4133}
4134
4135#define DO_FMLA(NAME, name) \
3a7be554 4136static bool trans_##NAME(DisasContext *s, arg_rprrr_esz *a) \
6ceabaad 4137{ \
08975da9 4138 static gen_helper_gvec_5_ptr * const fns[4] = { \
6ceabaad
RH
4139 NULL, gen_helper_sve_##name##_h, \
4140 gen_helper_sve_##name##_s, gen_helper_sve_##name##_d \
4141 }; \
4142 return do_fmla(s, a, fns[a->esz]); \
4143}
4144
4145DO_FMLA(FMLA_zpzzz, fmla_zpzzz)
4146DO_FMLA(FMLS_zpzzz, fmls_zpzzz)
4147DO_FMLA(FNMLA_zpzzz, fnmla_zpzzz)
4148DO_FMLA(FNMLS_zpzzz, fnmls_zpzzz)
4149
4150#undef DO_FMLA
4151
3a7be554 4152static bool trans_FCMLA_zpzzz(DisasContext *s, arg_FCMLA_zpzzz *a)
05f48bab 4153{
08975da9
RH
4154 static gen_helper_gvec_5_ptr * const fns[4] = {
4155 NULL,
05f48bab
RH
4156 gen_helper_sve_fcmla_zpzzz_h,
4157 gen_helper_sve_fcmla_zpzzz_s,
4158 gen_helper_sve_fcmla_zpzzz_d,
4159 };
4160
4161 if (a->esz == 0) {
4162 return false;
4163 }
4164 if (sve_access_check(s)) {
4165 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4166 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
08975da9
RH
4167 tcg_gen_gvec_5_ptr(vec_full_reg_offset(s, a->rd),
4168 vec_full_reg_offset(s, a->rn),
4169 vec_full_reg_offset(s, a->rm),
4170 vec_full_reg_offset(s, a->ra),
4171 pred_full_reg_offset(s, a->pg),
4172 status, vsz, vsz, a->rot, fns[a->esz]);
4173 tcg_temp_free_ptr(status);
05f48bab
RH
4174 }
4175 return true;
4176}
4177
3a7be554 4178static bool trans_FCMLA_zzxz(DisasContext *s, arg_FCMLA_zzxz *a)
18fc2405 4179{
636ddeb1 4180 static gen_helper_gvec_4_ptr * const fns[2] = {
18fc2405
RH
4181 gen_helper_gvec_fcmlah_idx,
4182 gen_helper_gvec_fcmlas_idx,
4183 };
4184
4185 tcg_debug_assert(a->esz == 1 || a->esz == 2);
4186 tcg_debug_assert(a->rd == a->ra);
4187 if (sve_access_check(s)) {
4188 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4189 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
636ddeb1 4190 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
18fc2405
RH
4191 vec_full_reg_offset(s, a->rn),
4192 vec_full_reg_offset(s, a->rm),
636ddeb1 4193 vec_full_reg_offset(s, a->ra),
18fc2405
RH
4194 status, vsz, vsz,
4195 a->index * 4 + a->rot,
4196 fns[a->esz - 1]);
4197 tcg_temp_free_ptr(status);
4198 }
4199 return true;
4200}
4201
8092c6a3
RH
4202/*
4203 *** SVE Floating Point Unary Operations Predicated Group
4204 */
4205
4206static bool do_zpz_ptr(DisasContext *s, int rd, int rn, int pg,
4207 bool is_fp16, gen_helper_gvec_3_ptr *fn)
4208{
4209 if (sve_access_check(s)) {
4210 unsigned vsz = vec_full_reg_size(s);
cdfb22bb 4211 TCGv_ptr status = fpstatus_ptr(is_fp16 ? FPST_FPCR_F16 : FPST_FPCR);
8092c6a3
RH
4212 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, rd),
4213 vec_full_reg_offset(s, rn),
4214 pred_full_reg_offset(s, pg),
4215 status, vsz, vsz, 0, fn);
4216 tcg_temp_free_ptr(status);
4217 }
4218 return true;
4219}
4220
3a7be554 4221static bool trans_FCVT_sh(DisasContext *s, arg_rpr_esz *a)
46d33d1e 4222{
e4ab5124 4223 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_sh);
46d33d1e
RH
4224}
4225
3a7be554 4226static bool trans_FCVT_hs(DisasContext *s, arg_rpr_esz *a)
46d33d1e
RH
4227{
4228 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_hs);
4229}
4230
d29b17ca
RH
4231static bool trans_BFCVT(DisasContext *s, arg_rpr_esz *a)
4232{
4233 if (!dc_isar_feature(aa64_sve_bf16, s)) {
4234 return false;
4235 }
4236 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_bfcvt);
4237}
4238
3a7be554 4239static bool trans_FCVT_dh(DisasContext *s, arg_rpr_esz *a)
46d33d1e 4240{
e4ab5124 4241 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_dh);
46d33d1e
RH
4242}
4243
3a7be554 4244static bool trans_FCVT_hd(DisasContext *s, arg_rpr_esz *a)
46d33d1e
RH
4245{
4246 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_hd);
4247}
4248
3a7be554 4249static bool trans_FCVT_ds(DisasContext *s, arg_rpr_esz *a)
46d33d1e
RH
4250{
4251 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_ds);
4252}
4253
3a7be554 4254static bool trans_FCVT_sd(DisasContext *s, arg_rpr_esz *a)
46d33d1e
RH
4255{
4256 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvt_sd);
4257}
4258
3a7be554 4259static bool trans_FCVTZS_hh(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4260{
4261 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzs_hh);
4262}
4263
3a7be554 4264static bool trans_FCVTZU_hh(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4265{
4266 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzu_hh);
4267}
4268
3a7be554 4269static bool trans_FCVTZS_hs(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4270{
4271 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzs_hs);
4272}
4273
3a7be554 4274static bool trans_FCVTZU_hs(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4275{
4276 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzu_hs);
4277}
4278
3a7be554 4279static bool trans_FCVTZS_hd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4280{
4281 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzs_hd);
4282}
4283
3a7be554 4284static bool trans_FCVTZU_hd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4285{
4286 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_fcvtzu_hd);
4287}
4288
3a7be554 4289static bool trans_FCVTZS_ss(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4290{
4291 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzs_ss);
4292}
4293
3a7be554 4294static bool trans_FCVTZU_ss(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4295{
4296 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzu_ss);
4297}
4298
3a7be554 4299static bool trans_FCVTZS_sd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4300{
4301 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzs_sd);
4302}
4303
3a7be554 4304static bool trans_FCVTZU_sd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4305{
4306 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzu_sd);
4307}
4308
3a7be554 4309static bool trans_FCVTZS_ds(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4310{
4311 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzs_ds);
4312}
4313
3a7be554 4314static bool trans_FCVTZU_ds(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4315{
4316 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzu_ds);
4317}
4318
3a7be554 4319static bool trans_FCVTZS_dd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4320{
4321 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzs_dd);
4322}
4323
3a7be554 4324static bool trans_FCVTZU_dd(DisasContext *s, arg_rpr_esz *a)
df4de1af
RH
4325{
4326 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_fcvtzu_dd);
4327}
4328
cda3c753
RH
4329static gen_helper_gvec_3_ptr * const frint_fns[3] = {
4330 gen_helper_sve_frint_h,
4331 gen_helper_sve_frint_s,
4332 gen_helper_sve_frint_d
4333};
4334
3a7be554 4335static bool trans_FRINTI(DisasContext *s, arg_rpr_esz *a)
cda3c753
RH
4336{
4337 if (a->esz == 0) {
4338 return false;
4339 }
4340 return do_zpz_ptr(s, a->rd, a->rn, a->pg, a->esz == MO_16,
4341 frint_fns[a->esz - 1]);
4342}
4343
3a7be554 4344static bool trans_FRINTX(DisasContext *s, arg_rpr_esz *a)
cda3c753
RH
4345{
4346 static gen_helper_gvec_3_ptr * const fns[3] = {
4347 gen_helper_sve_frintx_h,
4348 gen_helper_sve_frintx_s,
4349 gen_helper_sve_frintx_d
4350 };
4351 if (a->esz == 0) {
4352 return false;
4353 }
4354 return do_zpz_ptr(s, a->rd, a->rn, a->pg, a->esz == MO_16, fns[a->esz - 1]);
4355}
4356
95365277
SL
4357static bool do_frint_mode(DisasContext *s, arg_rpr_esz *a,
4358 int mode, gen_helper_gvec_3_ptr *fn)
cda3c753 4359{
cda3c753
RH
4360 if (sve_access_check(s)) {
4361 unsigned vsz = vec_full_reg_size(s);
4362 TCGv_i32 tmode = tcg_const_i32(mode);
cdfb22bb 4363 TCGv_ptr status = fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
cda3c753
RH
4364
4365 gen_helper_set_rmode(tmode, tmode, status);
4366
4367 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
4368 vec_full_reg_offset(s, a->rn),
4369 pred_full_reg_offset(s, a->pg),
95365277 4370 status, vsz, vsz, 0, fn);
cda3c753
RH
4371
4372 gen_helper_set_rmode(tmode, tmode, status);
4373 tcg_temp_free_i32(tmode);
4374 tcg_temp_free_ptr(status);
4375 }
4376 return true;
4377}
4378
3a7be554 4379static bool trans_FRINTN(DisasContext *s, arg_rpr_esz *a)
cda3c753 4380{
95365277
SL
4381 if (a->esz == 0) {
4382 return false;
4383 }
4384 return do_frint_mode(s, a, float_round_nearest_even, frint_fns[a->esz - 1]);
cda3c753
RH
4385}
4386
3a7be554 4387static bool trans_FRINTP(DisasContext *s, arg_rpr_esz *a)
cda3c753 4388{
95365277
SL
4389 if (a->esz == 0) {
4390 return false;
4391 }
4392 return do_frint_mode(s, a, float_round_up, frint_fns[a->esz - 1]);
cda3c753
RH
4393}
4394
3a7be554 4395static bool trans_FRINTM(DisasContext *s, arg_rpr_esz *a)
cda3c753 4396{
95365277
SL
4397 if (a->esz == 0) {
4398 return false;
4399 }
4400 return do_frint_mode(s, a, float_round_down, frint_fns[a->esz - 1]);
cda3c753
RH
4401}
4402
3a7be554 4403static bool trans_FRINTZ(DisasContext *s, arg_rpr_esz *a)
cda3c753 4404{
95365277
SL
4405 if (a->esz == 0) {
4406 return false;
4407 }
4408 return do_frint_mode(s, a, float_round_to_zero, frint_fns[a->esz - 1]);
cda3c753
RH
4409}
4410
3a7be554 4411static bool trans_FRINTA(DisasContext *s, arg_rpr_esz *a)
cda3c753 4412{
95365277
SL
4413 if (a->esz == 0) {
4414 return false;
4415 }
4416 return do_frint_mode(s, a, float_round_ties_away, frint_fns[a->esz - 1]);
cda3c753
RH
4417}
4418
3a7be554 4419static bool trans_FRECPX(DisasContext *s, arg_rpr_esz *a)
ec5b375b
RH
4420{
4421 static gen_helper_gvec_3_ptr * const fns[3] = {
4422 gen_helper_sve_frecpx_h,
4423 gen_helper_sve_frecpx_s,
4424 gen_helper_sve_frecpx_d
4425 };
4426 if (a->esz == 0) {
4427 return false;
4428 }
4429 return do_zpz_ptr(s, a->rd, a->rn, a->pg, a->esz == MO_16, fns[a->esz - 1]);
4430}
4431
3a7be554 4432static bool trans_FSQRT(DisasContext *s, arg_rpr_esz *a)
ec5b375b
RH
4433{
4434 static gen_helper_gvec_3_ptr * const fns[3] = {
4435 gen_helper_sve_fsqrt_h,
4436 gen_helper_sve_fsqrt_s,
4437 gen_helper_sve_fsqrt_d
4438 };
4439 if (a->esz == 0) {
4440 return false;
4441 }
4442 return do_zpz_ptr(s, a->rd, a->rn, a->pg, a->esz == MO_16, fns[a->esz - 1]);
4443}
4444
3a7be554 4445static bool trans_SCVTF_hh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4446{
4447 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_scvt_hh);
4448}
4449
3a7be554 4450static bool trans_SCVTF_sh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4451{
4452 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_scvt_sh);
4453}
4454
3a7be554 4455static bool trans_SCVTF_dh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4456{
4457 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_scvt_dh);
4458}
4459
3a7be554 4460static bool trans_SCVTF_ss(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4461{
4462 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_scvt_ss);
4463}
4464
3a7be554 4465static bool trans_SCVTF_ds(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4466{
4467 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_scvt_ds);
4468}
4469
3a7be554 4470static bool trans_SCVTF_sd(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4471{
4472 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_scvt_sd);
4473}
4474
3a7be554 4475static bool trans_SCVTF_dd(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4476{
4477 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_scvt_dd);
4478}
4479
3a7be554 4480static bool trans_UCVTF_hh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4481{
4482 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_ucvt_hh);
4483}
4484
3a7be554 4485static bool trans_UCVTF_sh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4486{
4487 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_ucvt_sh);
4488}
4489
3a7be554 4490static bool trans_UCVTF_dh(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4491{
4492 return do_zpz_ptr(s, a->rd, a->rn, a->pg, true, gen_helper_sve_ucvt_dh);
4493}
4494
3a7be554 4495static bool trans_UCVTF_ss(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4496{
4497 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_ucvt_ss);
4498}
4499
3a7be554 4500static bool trans_UCVTF_ds(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4501{
4502 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_ucvt_ds);
4503}
4504
3a7be554 4505static bool trans_UCVTF_sd(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4506{
4507 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_ucvt_sd);
4508}
4509
3a7be554 4510static bool trans_UCVTF_dd(DisasContext *s, arg_rpr_esz *a)
8092c6a3
RH
4511{
4512 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_ucvt_dd);
4513}
4514
d1822297
RH
4515/*
4516 *** SVE Memory - 32-bit Gather and Unsized Contiguous Group
4517 */
4518
4519/* Subroutine loading a vector register at VOFS of LEN bytes.
4520 * The load should begin at the address Rn + IMM.
4521 */
4522
19f2acc9 4523static void do_ldr(DisasContext *s, uint32_t vofs, int len, int rn, int imm)
d1822297 4524{
19f2acc9
RH
4525 int len_align = QEMU_ALIGN_DOWN(len, 8);
4526 int len_remain = len % 8;
4527 int nparts = len / 8 + ctpop8(len_remain);
d1822297 4528 int midx = get_mem_index(s);
b2aa8879 4529 TCGv_i64 dirty_addr, clean_addr, t0, t1;
d1822297 4530
b2aa8879
RH
4531 dirty_addr = tcg_temp_new_i64();
4532 tcg_gen_addi_i64(dirty_addr, cpu_reg_sp(s, rn), imm);
33e74c31 4533 clean_addr = gen_mte_checkN(s, dirty_addr, false, rn != 31, len);
b2aa8879 4534 tcg_temp_free_i64(dirty_addr);
d1822297 4535
b2aa8879
RH
4536 /*
4537 * Note that unpredicated load/store of vector/predicate registers
d1822297 4538 * are defined as a stream of bytes, which equates to little-endian
b2aa8879 4539 * operations on larger quantities.
d1822297
RH
4540 * Attempt to keep code expansion to a minimum by limiting the
4541 * amount of unrolling done.
4542 */
4543 if (nparts <= 4) {
4544 int i;
4545
b2aa8879 4546 t0 = tcg_temp_new_i64();
d1822297 4547 for (i = 0; i < len_align; i += 8) {
fc313c64 4548 tcg_gen_qemu_ld_i64(t0, clean_addr, midx, MO_LEUQ);
d1822297 4549 tcg_gen_st_i64(t0, cpu_env, vofs + i);
d8227b09 4550 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
d1822297 4551 }
b2aa8879 4552 tcg_temp_free_i64(t0);
d1822297
RH
4553 } else {
4554 TCGLabel *loop = gen_new_label();
4555 TCGv_ptr tp, i = tcg_const_local_ptr(0);
4556
b2aa8879
RH
4557 /* Copy the clean address into a local temp, live across the loop. */
4558 t0 = clean_addr;
4b4dc975 4559 clean_addr = new_tmp_a64_local(s);
b2aa8879 4560 tcg_gen_mov_i64(clean_addr, t0);
d1822297 4561
b2aa8879 4562 gen_set_label(loop);
d1822297 4563
b2aa8879 4564 t0 = tcg_temp_new_i64();
fc313c64 4565 tcg_gen_qemu_ld_i64(t0, clean_addr, midx, MO_LEUQ);
b2aa8879 4566 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
d1822297 4567
b2aa8879 4568 tp = tcg_temp_new_ptr();
d1822297
RH
4569 tcg_gen_add_ptr(tp, cpu_env, i);
4570 tcg_gen_addi_ptr(i, i, 8);
4571 tcg_gen_st_i64(t0, tp, vofs);
4572 tcg_temp_free_ptr(tp);
b2aa8879 4573 tcg_temp_free_i64(t0);
d1822297
RH
4574
4575 tcg_gen_brcondi_ptr(TCG_COND_LTU, i, len_align, loop);
4576 tcg_temp_free_ptr(i);
4577 }
4578
b2aa8879
RH
4579 /*
4580 * Predicate register loads can be any multiple of 2.
d1822297
RH
4581 * Note that we still store the entire 64-bit unit into cpu_env.
4582 */
4583 if (len_remain) {
b2aa8879 4584 t0 = tcg_temp_new_i64();
d1822297
RH
4585 switch (len_remain) {
4586 case 2:
4587 case 4:
4588 case 8:
b2aa8879
RH
4589 tcg_gen_qemu_ld_i64(t0, clean_addr, midx,
4590 MO_LE | ctz32(len_remain));
d1822297
RH
4591 break;
4592
4593 case 6:
4594 t1 = tcg_temp_new_i64();
b2aa8879
RH
4595 tcg_gen_qemu_ld_i64(t0, clean_addr, midx, MO_LEUL);
4596 tcg_gen_addi_i64(clean_addr, clean_addr, 4);
4597 tcg_gen_qemu_ld_i64(t1, clean_addr, midx, MO_LEUW);
d1822297
RH
4598 tcg_gen_deposit_i64(t0, t0, t1, 32, 32);
4599 tcg_temp_free_i64(t1);
4600 break;
4601
4602 default:
4603 g_assert_not_reached();
4604 }
4605 tcg_gen_st_i64(t0, cpu_env, vofs + len_align);
b2aa8879 4606 tcg_temp_free_i64(t0);
d1822297 4607 }
d1822297
RH
4608}
4609
5047c204 4610/* Similarly for stores. */
19f2acc9 4611static void do_str(DisasContext *s, uint32_t vofs, int len, int rn, int imm)
5047c204 4612{
19f2acc9
RH
4613 int len_align = QEMU_ALIGN_DOWN(len, 8);
4614 int len_remain = len % 8;
4615 int nparts = len / 8 + ctpop8(len_remain);
5047c204 4616 int midx = get_mem_index(s);
bba87d0a 4617 TCGv_i64 dirty_addr, clean_addr, t0;
5047c204 4618
bba87d0a
RH
4619 dirty_addr = tcg_temp_new_i64();
4620 tcg_gen_addi_i64(dirty_addr, cpu_reg_sp(s, rn), imm);
33e74c31 4621 clean_addr = gen_mte_checkN(s, dirty_addr, false, rn != 31, len);
bba87d0a 4622 tcg_temp_free_i64(dirty_addr);
5047c204
RH
4623
4624 /* Note that unpredicated load/store of vector/predicate registers
4625 * are defined as a stream of bytes, which equates to little-endian
4626 * operations on larger quantities. There is no nice way to force
4627 * a little-endian store for aarch64_be-linux-user out of line.
4628 *
4629 * Attempt to keep code expansion to a minimum by limiting the
4630 * amount of unrolling done.
4631 */
4632 if (nparts <= 4) {
4633 int i;
4634
bba87d0a 4635 t0 = tcg_temp_new_i64();
5047c204
RH
4636 for (i = 0; i < len_align; i += 8) {
4637 tcg_gen_ld_i64(t0, cpu_env, vofs + i);
fc313c64 4638 tcg_gen_qemu_st_i64(t0, clean_addr, midx, MO_LEUQ);
d8227b09 4639 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
5047c204 4640 }
bba87d0a 4641 tcg_temp_free_i64(t0);
5047c204
RH
4642 } else {
4643 TCGLabel *loop = gen_new_label();
bba87d0a 4644 TCGv_ptr tp, i = tcg_const_local_ptr(0);
5047c204 4645
bba87d0a
RH
4646 /* Copy the clean address into a local temp, live across the loop. */
4647 t0 = clean_addr;
4b4dc975 4648 clean_addr = new_tmp_a64_local(s);
bba87d0a 4649 tcg_gen_mov_i64(clean_addr, t0);
5047c204 4650
bba87d0a 4651 gen_set_label(loop);
5047c204 4652
bba87d0a
RH
4653 t0 = tcg_temp_new_i64();
4654 tp = tcg_temp_new_ptr();
4655 tcg_gen_add_ptr(tp, cpu_env, i);
4656 tcg_gen_ld_i64(t0, tp, vofs);
5047c204 4657 tcg_gen_addi_ptr(i, i, 8);
bba87d0a
RH
4658 tcg_temp_free_ptr(tp);
4659
fc313c64 4660 tcg_gen_qemu_st_i64(t0, clean_addr, midx, MO_LEUQ);
bba87d0a
RH
4661 tcg_gen_addi_i64(clean_addr, clean_addr, 8);
4662 tcg_temp_free_i64(t0);
5047c204
RH
4663
4664 tcg_gen_brcondi_ptr(TCG_COND_LTU, i, len_align, loop);
4665 tcg_temp_free_ptr(i);
4666 }
4667
4668 /* Predicate register stores can be any multiple of 2. */
4669 if (len_remain) {
bba87d0a 4670 t0 = tcg_temp_new_i64();
5047c204 4671 tcg_gen_ld_i64(t0, cpu_env, vofs + len_align);
5047c204
RH
4672
4673 switch (len_remain) {
4674 case 2:
4675 case 4:
4676 case 8:
bba87d0a
RH
4677 tcg_gen_qemu_st_i64(t0, clean_addr, midx,
4678 MO_LE | ctz32(len_remain));
5047c204
RH
4679 break;
4680
4681 case 6:
bba87d0a
RH
4682 tcg_gen_qemu_st_i64(t0, clean_addr, midx, MO_LEUL);
4683 tcg_gen_addi_i64(clean_addr, clean_addr, 4);
5047c204 4684 tcg_gen_shri_i64(t0, t0, 32);
bba87d0a 4685 tcg_gen_qemu_st_i64(t0, clean_addr, midx, MO_LEUW);
5047c204
RH
4686 break;
4687
4688 default:
4689 g_assert_not_reached();
4690 }
bba87d0a 4691 tcg_temp_free_i64(t0);
5047c204 4692 }
5047c204
RH
4693}
4694
3a7be554 4695static bool trans_LDR_zri(DisasContext *s, arg_rri *a)
d1822297
RH
4696{
4697 if (sve_access_check(s)) {
4698 int size = vec_full_reg_size(s);
4699 int off = vec_full_reg_offset(s, a->rd);
4700 do_ldr(s, off, size, a->rn, a->imm * size);
4701 }
4702 return true;
4703}
4704
3a7be554 4705static bool trans_LDR_pri(DisasContext *s, arg_rri *a)
d1822297
RH
4706{
4707 if (sve_access_check(s)) {
4708 int size = pred_full_reg_size(s);
4709 int off = pred_full_reg_offset(s, a->rd);
4710 do_ldr(s, off, size, a->rn, a->imm * size);
4711 }
4712 return true;
4713}
c4e7c493 4714
3a7be554 4715static bool trans_STR_zri(DisasContext *s, arg_rri *a)
5047c204
RH
4716{
4717 if (sve_access_check(s)) {
4718 int size = vec_full_reg_size(s);
4719 int off = vec_full_reg_offset(s, a->rd);
4720 do_str(s, off, size, a->rn, a->imm * size);
4721 }
4722 return true;
4723}
4724
3a7be554 4725static bool trans_STR_pri(DisasContext *s, arg_rri *a)
5047c204
RH
4726{
4727 if (sve_access_check(s)) {
4728 int size = pred_full_reg_size(s);
4729 int off = pred_full_reg_offset(s, a->rd);
4730 do_str(s, off, size, a->rn, a->imm * size);
4731 }
4732 return true;
4733}
4734
c4e7c493
RH
4735/*
4736 *** SVE Memory - Contiguous Load Group
4737 */
4738
4739/* The memory mode of the dtype. */
14776ab5 4740static const MemOp dtype_mop[16] = {
c4e7c493
RH
4741 MO_UB, MO_UB, MO_UB, MO_UB,
4742 MO_SL, MO_UW, MO_UW, MO_UW,
4743 MO_SW, MO_SW, MO_UL, MO_UL,
fc313c64 4744 MO_SB, MO_SB, MO_SB, MO_UQ
c4e7c493
RH
4745};
4746
4747#define dtype_msz(x) (dtype_mop[x] & MO_SIZE)
4748
4749/* The vector element size of dtype. */
4750static const uint8_t dtype_esz[16] = {
4751 0, 1, 2, 3,
4752 3, 1, 2, 3,
4753 3, 2, 2, 3,
4754 3, 2, 1, 3
4755};
4756
4757static void do_mem_zpa(DisasContext *s, int zt, int pg, TCGv_i64 addr,
206adacf
RH
4758 int dtype, uint32_t mte_n, bool is_write,
4759 gen_helper_gvec_mem *fn)
c4e7c493
RH
4760{
4761 unsigned vsz = vec_full_reg_size(s);
4762 TCGv_ptr t_pg;
206adacf 4763 int desc = 0;
c4e7c493 4764
206adacf
RH
4765 /*
4766 * For e.g. LD4, there are not enough arguments to pass all 4
c4e7c493
RH
4767 * registers as pointers, so encode the regno into the data field.
4768 * For consistency, do this even for LD1.
4769 */
9473d0ec 4770 if (s->mte_active[0]) {
206adacf
RH
4771 int msz = dtype_msz(dtype);
4772
4773 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
4774 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
4775 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
4776 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
28f32503 4777 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (mte_n << msz) - 1);
206adacf 4778 desc <<= SVE_MTEDESC_SHIFT;
9473d0ec
RH
4779 } else {
4780 addr = clean_data_tbi(s, addr);
206adacf 4781 }
9473d0ec 4782
206adacf 4783 desc = simd_desc(vsz, vsz, zt | desc);
c4e7c493
RH
4784 t_pg = tcg_temp_new_ptr();
4785
4786 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, pg));
c6a59b55 4787 fn(cpu_env, t_pg, addr, tcg_constant_i32(desc));
c4e7c493
RH
4788
4789 tcg_temp_free_ptr(t_pg);
c4e7c493
RH
4790}
4791
c182c6db
RH
4792/* Indexed by [mte][be][dtype][nreg] */
4793static gen_helper_gvec_mem * const ldr_fns[2][2][16][4] = {
4794 { /* mte inactive, little-endian */
4795 { { gen_helper_sve_ld1bb_r, gen_helper_sve_ld2bb_r,
4796 gen_helper_sve_ld3bb_r, gen_helper_sve_ld4bb_r },
4797 { gen_helper_sve_ld1bhu_r, NULL, NULL, NULL },
4798 { gen_helper_sve_ld1bsu_r, NULL, NULL, NULL },
4799 { gen_helper_sve_ld1bdu_r, NULL, NULL, NULL },
4800
4801 { gen_helper_sve_ld1sds_le_r, NULL, NULL, NULL },
4802 { gen_helper_sve_ld1hh_le_r, gen_helper_sve_ld2hh_le_r,
4803 gen_helper_sve_ld3hh_le_r, gen_helper_sve_ld4hh_le_r },
4804 { gen_helper_sve_ld1hsu_le_r, NULL, NULL, NULL },
4805 { gen_helper_sve_ld1hdu_le_r, NULL, NULL, NULL },
4806
4807 { gen_helper_sve_ld1hds_le_r, NULL, NULL, NULL },
4808 { gen_helper_sve_ld1hss_le_r, NULL, NULL, NULL },
4809 { gen_helper_sve_ld1ss_le_r, gen_helper_sve_ld2ss_le_r,
4810 gen_helper_sve_ld3ss_le_r, gen_helper_sve_ld4ss_le_r },
4811 { gen_helper_sve_ld1sdu_le_r, NULL, NULL, NULL },
4812
4813 { gen_helper_sve_ld1bds_r, NULL, NULL, NULL },
4814 { gen_helper_sve_ld1bss_r, NULL, NULL, NULL },
4815 { gen_helper_sve_ld1bhs_r, NULL, NULL, NULL },
4816 { gen_helper_sve_ld1dd_le_r, gen_helper_sve_ld2dd_le_r,
4817 gen_helper_sve_ld3dd_le_r, gen_helper_sve_ld4dd_le_r } },
4818
4819 /* mte inactive, big-endian */
4820 { { gen_helper_sve_ld1bb_r, gen_helper_sve_ld2bb_r,
4821 gen_helper_sve_ld3bb_r, gen_helper_sve_ld4bb_r },
4822 { gen_helper_sve_ld1bhu_r, NULL, NULL, NULL },
4823 { gen_helper_sve_ld1bsu_r, NULL, NULL, NULL },
4824 { gen_helper_sve_ld1bdu_r, NULL, NULL, NULL },
4825
4826 { gen_helper_sve_ld1sds_be_r, NULL, NULL, NULL },
4827 { gen_helper_sve_ld1hh_be_r, gen_helper_sve_ld2hh_be_r,
4828 gen_helper_sve_ld3hh_be_r, gen_helper_sve_ld4hh_be_r },
4829 { gen_helper_sve_ld1hsu_be_r, NULL, NULL, NULL },
4830 { gen_helper_sve_ld1hdu_be_r, NULL, NULL, NULL },
4831
4832 { gen_helper_sve_ld1hds_be_r, NULL, NULL, NULL },
4833 { gen_helper_sve_ld1hss_be_r, NULL, NULL, NULL },
4834 { gen_helper_sve_ld1ss_be_r, gen_helper_sve_ld2ss_be_r,
4835 gen_helper_sve_ld3ss_be_r, gen_helper_sve_ld4ss_be_r },
4836 { gen_helper_sve_ld1sdu_be_r, NULL, NULL, NULL },
4837
4838 { gen_helper_sve_ld1bds_r, NULL, NULL, NULL },
4839 { gen_helper_sve_ld1bss_r, NULL, NULL, NULL },
4840 { gen_helper_sve_ld1bhs_r, NULL, NULL, NULL },
4841 { gen_helper_sve_ld1dd_be_r, gen_helper_sve_ld2dd_be_r,
4842 gen_helper_sve_ld3dd_be_r, gen_helper_sve_ld4dd_be_r } } },
4843
4844 { /* mte active, little-endian */
4845 { { gen_helper_sve_ld1bb_r_mte,
4846 gen_helper_sve_ld2bb_r_mte,
4847 gen_helper_sve_ld3bb_r_mte,
4848 gen_helper_sve_ld4bb_r_mte },
4849 { gen_helper_sve_ld1bhu_r_mte, NULL, NULL, NULL },
4850 { gen_helper_sve_ld1bsu_r_mte, NULL, NULL, NULL },
4851 { gen_helper_sve_ld1bdu_r_mte, NULL, NULL, NULL },
4852
4853 { gen_helper_sve_ld1sds_le_r_mte, NULL, NULL, NULL },
4854 { gen_helper_sve_ld1hh_le_r_mte,
4855 gen_helper_sve_ld2hh_le_r_mte,
4856 gen_helper_sve_ld3hh_le_r_mte,
4857 gen_helper_sve_ld4hh_le_r_mte },
4858 { gen_helper_sve_ld1hsu_le_r_mte, NULL, NULL, NULL },
4859 { gen_helper_sve_ld1hdu_le_r_mte, NULL, NULL, NULL },
4860
4861 { gen_helper_sve_ld1hds_le_r_mte, NULL, NULL, NULL },
4862 { gen_helper_sve_ld1hss_le_r_mte, NULL, NULL, NULL },
4863 { gen_helper_sve_ld1ss_le_r_mte,
4864 gen_helper_sve_ld2ss_le_r_mte,
4865 gen_helper_sve_ld3ss_le_r_mte,
4866 gen_helper_sve_ld4ss_le_r_mte },
4867 { gen_helper_sve_ld1sdu_le_r_mte, NULL, NULL, NULL },
4868
4869 { gen_helper_sve_ld1bds_r_mte, NULL, NULL, NULL },
4870 { gen_helper_sve_ld1bss_r_mte, NULL, NULL, NULL },
4871 { gen_helper_sve_ld1bhs_r_mte, NULL, NULL, NULL },
4872 { gen_helper_sve_ld1dd_le_r_mte,
4873 gen_helper_sve_ld2dd_le_r_mte,
4874 gen_helper_sve_ld3dd_le_r_mte,
4875 gen_helper_sve_ld4dd_le_r_mte } },
4876
4877 /* mte active, big-endian */
4878 { { gen_helper_sve_ld1bb_r_mte,
4879 gen_helper_sve_ld2bb_r_mte,
4880 gen_helper_sve_ld3bb_r_mte,
4881 gen_helper_sve_ld4bb_r_mte },
4882 { gen_helper_sve_ld1bhu_r_mte, NULL, NULL, NULL },
4883 { gen_helper_sve_ld1bsu_r_mte, NULL, NULL, NULL },
4884 { gen_helper_sve_ld1bdu_r_mte, NULL, NULL, NULL },
4885
4886 { gen_helper_sve_ld1sds_be_r_mte, NULL, NULL, NULL },
4887 { gen_helper_sve_ld1hh_be_r_mte,
4888 gen_helper_sve_ld2hh_be_r_mte,
4889 gen_helper_sve_ld3hh_be_r_mte,
4890 gen_helper_sve_ld4hh_be_r_mte },
4891 { gen_helper_sve_ld1hsu_be_r_mte, NULL, NULL, NULL },
4892 { gen_helper_sve_ld1hdu_be_r_mte, NULL, NULL, NULL },
4893
4894 { gen_helper_sve_ld1hds_be_r_mte, NULL, NULL, NULL },
4895 { gen_helper_sve_ld1hss_be_r_mte, NULL, NULL, NULL },
4896 { gen_helper_sve_ld1ss_be_r_mte,
4897 gen_helper_sve_ld2ss_be_r_mte,
4898 gen_helper_sve_ld3ss_be_r_mte,
4899 gen_helper_sve_ld4ss_be_r_mte },
4900 { gen_helper_sve_ld1sdu_be_r_mte, NULL, NULL, NULL },
4901
4902 { gen_helper_sve_ld1bds_r_mte, NULL, NULL, NULL },
4903 { gen_helper_sve_ld1bss_r_mte, NULL, NULL, NULL },
4904 { gen_helper_sve_ld1bhs_r_mte, NULL, NULL, NULL },
4905 { gen_helper_sve_ld1dd_be_r_mte,
4906 gen_helper_sve_ld2dd_be_r_mte,
4907 gen_helper_sve_ld3dd_be_r_mte,
4908 gen_helper_sve_ld4dd_be_r_mte } } },
4909};
4910
c4e7c493
RH
4911static void do_ld_zpa(DisasContext *s, int zt, int pg,
4912 TCGv_i64 addr, int dtype, int nreg)
4913{
206adacf 4914 gen_helper_gvec_mem *fn
c182c6db 4915 = ldr_fns[s->mte_active[0]][s->be_data == MO_BE][dtype][nreg];
c4e7c493 4916
206adacf
RH
4917 /*
4918 * While there are holes in the table, they are not
c4e7c493
RH
4919 * accessible via the instruction encoding.
4920 */
4921 assert(fn != NULL);
206adacf 4922 do_mem_zpa(s, zt, pg, addr, dtype, nreg, false, fn);
c4e7c493
RH
4923}
4924
3a7be554 4925static bool trans_LD_zprr(DisasContext *s, arg_rprr_load *a)
c4e7c493
RH
4926{
4927 if (a->rm == 31) {
4928 return false;
4929 }
4930 if (sve_access_check(s)) {
4931 TCGv_i64 addr = new_tmp_a64(s);
50ef1cbf 4932 tcg_gen_shli_i64(addr, cpu_reg(s, a->rm), dtype_msz(a->dtype));
c4e7c493
RH
4933 tcg_gen_add_i64(addr, addr, cpu_reg_sp(s, a->rn));
4934 do_ld_zpa(s, a->rd, a->pg, addr, a->dtype, a->nreg);
4935 }
4936 return true;
4937}
4938
3a7be554 4939static bool trans_LD_zpri(DisasContext *s, arg_rpri_load *a)
c4e7c493
RH
4940{
4941 if (sve_access_check(s)) {
4942 int vsz = vec_full_reg_size(s);
4943 int elements = vsz >> dtype_esz[a->dtype];
4944 TCGv_i64 addr = new_tmp_a64(s);
4945
4946 tcg_gen_addi_i64(addr, cpu_reg_sp(s, a->rn),
4947 (a->imm * elements * (a->nreg + 1))
4948 << dtype_msz(a->dtype));
4949 do_ld_zpa(s, a->rd, a->pg, addr, a->dtype, a->nreg);
4950 }
4951 return true;
4952}
e2654d75 4953
3a7be554 4954static bool trans_LDFF1_zprr(DisasContext *s, arg_rprr_load *a)
e2654d75 4955{
aa13f7c3
RH
4956 static gen_helper_gvec_mem * const fns[2][2][16] = {
4957 { /* mte inactive, little-endian */
4958 { gen_helper_sve_ldff1bb_r,
4959 gen_helper_sve_ldff1bhu_r,
4960 gen_helper_sve_ldff1bsu_r,
4961 gen_helper_sve_ldff1bdu_r,
4962
4963 gen_helper_sve_ldff1sds_le_r,
4964 gen_helper_sve_ldff1hh_le_r,
4965 gen_helper_sve_ldff1hsu_le_r,
4966 gen_helper_sve_ldff1hdu_le_r,
4967
4968 gen_helper_sve_ldff1hds_le_r,
4969 gen_helper_sve_ldff1hss_le_r,
4970 gen_helper_sve_ldff1ss_le_r,
4971 gen_helper_sve_ldff1sdu_le_r,
4972
4973 gen_helper_sve_ldff1bds_r,
4974 gen_helper_sve_ldff1bss_r,
4975 gen_helper_sve_ldff1bhs_r,
4976 gen_helper_sve_ldff1dd_le_r },
4977
4978 /* mte inactive, big-endian */
4979 { gen_helper_sve_ldff1bb_r,
4980 gen_helper_sve_ldff1bhu_r,
4981 gen_helper_sve_ldff1bsu_r,
4982 gen_helper_sve_ldff1bdu_r,
4983
4984 gen_helper_sve_ldff1sds_be_r,
4985 gen_helper_sve_ldff1hh_be_r,
4986 gen_helper_sve_ldff1hsu_be_r,
4987 gen_helper_sve_ldff1hdu_be_r,
4988
4989 gen_helper_sve_ldff1hds_be_r,
4990 gen_helper_sve_ldff1hss_be_r,
4991 gen_helper_sve_ldff1ss_be_r,
4992 gen_helper_sve_ldff1sdu_be_r,
4993
4994 gen_helper_sve_ldff1bds_r,
4995 gen_helper_sve_ldff1bss_r,
4996 gen_helper_sve_ldff1bhs_r,
4997 gen_helper_sve_ldff1dd_be_r } },
4998
4999 { /* mte active, little-endian */
5000 { gen_helper_sve_ldff1bb_r_mte,
5001 gen_helper_sve_ldff1bhu_r_mte,
5002 gen_helper_sve_ldff1bsu_r_mte,
5003 gen_helper_sve_ldff1bdu_r_mte,
5004
5005 gen_helper_sve_ldff1sds_le_r_mte,
5006 gen_helper_sve_ldff1hh_le_r_mte,
5007 gen_helper_sve_ldff1hsu_le_r_mte,
5008 gen_helper_sve_ldff1hdu_le_r_mte,
5009
5010 gen_helper_sve_ldff1hds_le_r_mte,
5011 gen_helper_sve_ldff1hss_le_r_mte,
5012 gen_helper_sve_ldff1ss_le_r_mte,
5013 gen_helper_sve_ldff1sdu_le_r_mte,
5014
5015 gen_helper_sve_ldff1bds_r_mte,
5016 gen_helper_sve_ldff1bss_r_mte,
5017 gen_helper_sve_ldff1bhs_r_mte,
5018 gen_helper_sve_ldff1dd_le_r_mte },
5019
5020 /* mte active, big-endian */
5021 { gen_helper_sve_ldff1bb_r_mte,
5022 gen_helper_sve_ldff1bhu_r_mte,
5023 gen_helper_sve_ldff1bsu_r_mte,
5024 gen_helper_sve_ldff1bdu_r_mte,
5025
5026 gen_helper_sve_ldff1sds_be_r_mte,
5027 gen_helper_sve_ldff1hh_be_r_mte,
5028 gen_helper_sve_ldff1hsu_be_r_mte,
5029 gen_helper_sve_ldff1hdu_be_r_mte,
5030
5031 gen_helper_sve_ldff1hds_be_r_mte,
5032 gen_helper_sve_ldff1hss_be_r_mte,
5033 gen_helper_sve_ldff1ss_be_r_mte,
5034 gen_helper_sve_ldff1sdu_be_r_mte,
5035
5036 gen_helper_sve_ldff1bds_r_mte,
5037 gen_helper_sve_ldff1bss_r_mte,
5038 gen_helper_sve_ldff1bhs_r_mte,
5039 gen_helper_sve_ldff1dd_be_r_mte } },
e2654d75
RH
5040 };
5041
5042 if (sve_access_check(s)) {
5043 TCGv_i64 addr = new_tmp_a64(s);
5044 tcg_gen_shli_i64(addr, cpu_reg(s, a->rm), dtype_msz(a->dtype));
5045 tcg_gen_add_i64(addr, addr, cpu_reg_sp(s, a->rn));
aa13f7c3
RH
5046 do_mem_zpa(s, a->rd, a->pg, addr, a->dtype, 1, false,
5047 fns[s->mte_active[0]][s->be_data == MO_BE][a->dtype]);
e2654d75
RH
5048 }
5049 return true;
5050}
5051
3a7be554 5052static bool trans_LDNF1_zpri(DisasContext *s, arg_rpri_load *a)
e2654d75 5053{
aa13f7c3
RH
5054 static gen_helper_gvec_mem * const fns[2][2][16] = {
5055 { /* mte inactive, little-endian */
5056 { gen_helper_sve_ldnf1bb_r,
5057 gen_helper_sve_ldnf1bhu_r,
5058 gen_helper_sve_ldnf1bsu_r,
5059 gen_helper_sve_ldnf1bdu_r,
5060
5061 gen_helper_sve_ldnf1sds_le_r,
5062 gen_helper_sve_ldnf1hh_le_r,
5063 gen_helper_sve_ldnf1hsu_le_r,
5064 gen_helper_sve_ldnf1hdu_le_r,
5065
5066 gen_helper_sve_ldnf1hds_le_r,
5067 gen_helper_sve_ldnf1hss_le_r,
5068 gen_helper_sve_ldnf1ss_le_r,
5069 gen_helper_sve_ldnf1sdu_le_r,
5070
5071 gen_helper_sve_ldnf1bds_r,
5072 gen_helper_sve_ldnf1bss_r,
5073 gen_helper_sve_ldnf1bhs_r,
5074 gen_helper_sve_ldnf1dd_le_r },
5075
5076 /* mte inactive, big-endian */
5077 { gen_helper_sve_ldnf1bb_r,
5078 gen_helper_sve_ldnf1bhu_r,
5079 gen_helper_sve_ldnf1bsu_r,
5080 gen_helper_sve_ldnf1bdu_r,
5081
5082 gen_helper_sve_ldnf1sds_be_r,
5083 gen_helper_sve_ldnf1hh_be_r,
5084 gen_helper_sve_ldnf1hsu_be_r,
5085 gen_helper_sve_ldnf1hdu_be_r,
5086
5087 gen_helper_sve_ldnf1hds_be_r,
5088 gen_helper_sve_ldnf1hss_be_r,
5089 gen_helper_sve_ldnf1ss_be_r,
5090 gen_helper_sve_ldnf1sdu_be_r,
5091
5092 gen_helper_sve_ldnf1bds_r,
5093 gen_helper_sve_ldnf1bss_r,
5094 gen_helper_sve_ldnf1bhs_r,
5095 gen_helper_sve_ldnf1dd_be_r } },
5096
5097 { /* mte inactive, little-endian */
5098 { gen_helper_sve_ldnf1bb_r_mte,
5099 gen_helper_sve_ldnf1bhu_r_mte,
5100 gen_helper_sve_ldnf1bsu_r_mte,
5101 gen_helper_sve_ldnf1bdu_r_mte,
5102
5103 gen_helper_sve_ldnf1sds_le_r_mte,
5104 gen_helper_sve_ldnf1hh_le_r_mte,
5105 gen_helper_sve_ldnf1hsu_le_r_mte,
5106 gen_helper_sve_ldnf1hdu_le_r_mte,
5107
5108 gen_helper_sve_ldnf1hds_le_r_mte,
5109 gen_helper_sve_ldnf1hss_le_r_mte,
5110 gen_helper_sve_ldnf1ss_le_r_mte,
5111 gen_helper_sve_ldnf1sdu_le_r_mte,
5112
5113 gen_helper_sve_ldnf1bds_r_mte,
5114 gen_helper_sve_ldnf1bss_r_mte,
5115 gen_helper_sve_ldnf1bhs_r_mte,
5116 gen_helper_sve_ldnf1dd_le_r_mte },
5117
5118 /* mte inactive, big-endian */
5119 { gen_helper_sve_ldnf1bb_r_mte,
5120 gen_helper_sve_ldnf1bhu_r_mte,
5121 gen_helper_sve_ldnf1bsu_r_mte,
5122 gen_helper_sve_ldnf1bdu_r_mte,
5123
5124 gen_helper_sve_ldnf1sds_be_r_mte,
5125 gen_helper_sve_ldnf1hh_be_r_mte,
5126 gen_helper_sve_ldnf1hsu_be_r_mte,
5127 gen_helper_sve_ldnf1hdu_be_r_mte,
5128
5129 gen_helper_sve_ldnf1hds_be_r_mte,
5130 gen_helper_sve_ldnf1hss_be_r_mte,
5131 gen_helper_sve_ldnf1ss_be_r_mte,
5132 gen_helper_sve_ldnf1sdu_be_r_mte,
5133
5134 gen_helper_sve_ldnf1bds_r_mte,
5135 gen_helper_sve_ldnf1bss_r_mte,
5136 gen_helper_sve_ldnf1bhs_r_mte,
5137 gen_helper_sve_ldnf1dd_be_r_mte } },
e2654d75
RH
5138 };
5139
5140 if (sve_access_check(s)) {
5141 int vsz = vec_full_reg_size(s);
5142 int elements = vsz >> dtype_esz[a->dtype];
5143 int off = (a->imm * elements) << dtype_msz(a->dtype);
5144 TCGv_i64 addr = new_tmp_a64(s);
5145
5146 tcg_gen_addi_i64(addr, cpu_reg_sp(s, a->rn), off);
aa13f7c3
RH
5147 do_mem_zpa(s, a->rd, a->pg, addr, a->dtype, 1, false,
5148 fns[s->mte_active[0]][s->be_data == MO_BE][a->dtype]);
e2654d75
RH
5149 }
5150 return true;
5151}
1a039c7e 5152
c182c6db 5153static void do_ldrq(DisasContext *s, int zt, int pg, TCGv_i64 addr, int dtype)
05abe304 5154{
05abe304
RH
5155 unsigned vsz = vec_full_reg_size(s);
5156 TCGv_ptr t_pg;
7924d239 5157 int poff;
05abe304
RH
5158
5159 /* Load the first quadword using the normal predicated load helpers. */
2a99ab2b
RH
5160 poff = pred_full_reg_offset(s, pg);
5161 if (vsz > 16) {
5162 /*
5163 * Zero-extend the first 16 bits of the predicate into a temporary.
5164 * This avoids triggering an assert making sure we don't have bits
5165 * set within a predicate beyond VQ, but we have lowered VQ to 1
5166 * for this load operation.
5167 */
5168 TCGv_i64 tmp = tcg_temp_new_i64();
e03b5686 5169#if HOST_BIG_ENDIAN
2a99ab2b
RH
5170 poff += 6;
5171#endif
5172 tcg_gen_ld16u_i64(tmp, cpu_env, poff);
5173
5174 poff = offsetof(CPUARMState, vfp.preg_tmp);
5175 tcg_gen_st_i64(tmp, cpu_env, poff);
5176 tcg_temp_free_i64(tmp);
5177 }
5178
05abe304 5179 t_pg = tcg_temp_new_ptr();
2a99ab2b 5180 tcg_gen_addi_ptr(t_pg, cpu_env, poff);
05abe304 5181
c182c6db
RH
5182 gen_helper_gvec_mem *fn
5183 = ldr_fns[s->mte_active[0]][s->be_data == MO_BE][dtype][0];
7924d239 5184 fn(cpu_env, t_pg, addr, tcg_constant_i32(simd_desc(16, 16, zt)));
05abe304
RH
5185
5186 tcg_temp_free_ptr(t_pg);
05abe304
RH
5187
5188 /* Replicate that first quadword. */
5189 if (vsz > 16) {
7924d239
RH
5190 int doff = vec_full_reg_offset(s, zt);
5191 tcg_gen_gvec_dup_mem(4, doff + 16, doff, vsz - 16, vsz - 16);
05abe304
RH
5192 }
5193}
5194
3a7be554 5195static bool trans_LD1RQ_zprr(DisasContext *s, arg_rprr_load *a)
05abe304
RH
5196{
5197 if (a->rm == 31) {
5198 return false;
5199 }
5200 if (sve_access_check(s)) {
5201 int msz = dtype_msz(a->dtype);
5202 TCGv_i64 addr = new_tmp_a64(s);
5203 tcg_gen_shli_i64(addr, cpu_reg(s, a->rm), msz);
5204 tcg_gen_add_i64(addr, addr, cpu_reg_sp(s, a->rn));
c182c6db 5205 do_ldrq(s, a->rd, a->pg, addr, a->dtype);
05abe304
RH
5206 }
5207 return true;
5208}
5209
3a7be554 5210static bool trans_LD1RQ_zpri(DisasContext *s, arg_rpri_load *a)
05abe304
RH
5211{
5212 if (sve_access_check(s)) {
5213 TCGv_i64 addr = new_tmp_a64(s);
5214 tcg_gen_addi_i64(addr, cpu_reg_sp(s, a->rn), a->imm * 16);
c182c6db 5215 do_ldrq(s, a->rd, a->pg, addr, a->dtype);
05abe304
RH
5216 }
5217 return true;
5218}
5219
12c563f6
RH
5220static void do_ldro(DisasContext *s, int zt, int pg, TCGv_i64 addr, int dtype)
5221{
5222 unsigned vsz = vec_full_reg_size(s);
5223 unsigned vsz_r32;
5224 TCGv_ptr t_pg;
5225 int poff, doff;
5226
5227 if (vsz < 32) {
5228 /*
5229 * Note that this UNDEFINED check comes after CheckSVEEnabled()
5230 * in the ARM pseudocode, which is the sve_access_check() done
5231 * in our caller. We should not now return false from the caller.
5232 */
5233 unallocated_encoding(s);
5234 return;
5235 }
5236
5237 /* Load the first octaword using the normal predicated load helpers. */
5238
5239 poff = pred_full_reg_offset(s, pg);
5240 if (vsz > 32) {
5241 /*
5242 * Zero-extend the first 32 bits of the predicate into a temporary.
5243 * This avoids triggering an assert making sure we don't have bits
5244 * set within a predicate beyond VQ, but we have lowered VQ to 2
5245 * for this load operation.
5246 */
5247 TCGv_i64 tmp = tcg_temp_new_i64();
e03b5686 5248#if HOST_BIG_ENDIAN
12c563f6
RH
5249 poff += 4;
5250#endif
5251 tcg_gen_ld32u_i64(tmp, cpu_env, poff);
5252
5253 poff = offsetof(CPUARMState, vfp.preg_tmp);
5254 tcg_gen_st_i64(tmp, cpu_env, poff);
5255 tcg_temp_free_i64(tmp);
5256 }
5257
5258 t_pg = tcg_temp_new_ptr();
5259 tcg_gen_addi_ptr(t_pg, cpu_env, poff);
5260
5261 gen_helper_gvec_mem *fn
5262 = ldr_fns[s->mte_active[0]][s->be_data == MO_BE][dtype][0];
5263 fn(cpu_env, t_pg, addr, tcg_constant_i32(simd_desc(32, 32, zt)));
5264
5265 tcg_temp_free_ptr(t_pg);
5266
5267 /*
5268 * Replicate that first octaword.
5269 * The replication happens in units of 32; if the full vector size
5270 * is not a multiple of 32, the final bits are zeroed.
5271 */
5272 doff = vec_full_reg_offset(s, zt);
5273 vsz_r32 = QEMU_ALIGN_DOWN(vsz, 32);
5274 if (vsz >= 64) {
5275 tcg_gen_gvec_dup_mem(5, doff + 32, doff, vsz_r32 - 32, vsz_r32 - 32);
5276 }
5277 vsz -= vsz_r32;
5278 if (vsz) {
5279 tcg_gen_gvec_dup_imm(MO_64, doff + vsz_r32, vsz, vsz, 0);
5280 }
5281}
5282
5283static bool trans_LD1RO_zprr(DisasContext *s, arg_rprr_load *a)
5284{
5285 if (!dc_isar_feature(aa64_sve_f64mm, s)) {
5286 return false;
5287 }
5288 if (a->rm == 31) {
5289 return false;
5290 }
5291 if (sve_access_check(s)) {
5292 TCGv_i64 addr = new_tmp_a64(s);
5293 tcg_gen_shli_i64(addr, cpu_reg(s, a->rm), dtype_msz(a->dtype));
5294 tcg_gen_add_i64(addr, addr, cpu_reg_sp(s, a->rn));
5295 do_ldro(s, a->rd, a->pg, addr, a->dtype);
5296 }
5297 return true;
5298}
5299
5300static bool trans_LD1RO_zpri(DisasContext *s, arg_rpri_load *a)
5301{
5302 if (!dc_isar_feature(aa64_sve_f64mm, s)) {
5303 return false;
5304 }
5305 if (sve_access_check(s)) {
5306 TCGv_i64 addr = new_tmp_a64(s);
5307 tcg_gen_addi_i64(addr, cpu_reg_sp(s, a->rn), a->imm * 32);
5308 do_ldro(s, a->rd, a->pg, addr, a->dtype);
5309 }
5310 return true;
5311}
5312
68459864 5313/* Load and broadcast element. */
3a7be554 5314static bool trans_LD1R_zpri(DisasContext *s, arg_rpri_load *a)
68459864 5315{
68459864
RH
5316 unsigned vsz = vec_full_reg_size(s);
5317 unsigned psz = pred_full_reg_size(s);
5318 unsigned esz = dtype_esz[a->dtype];
d0e372b0 5319 unsigned msz = dtype_msz(a->dtype);
c0ed9166 5320 TCGLabel *over;
4ac430e1 5321 TCGv_i64 temp, clean_addr;
68459864 5322
c0ed9166
RH
5323 if (!sve_access_check(s)) {
5324 return true;
5325 }
5326
5327 over = gen_new_label();
5328
68459864
RH
5329 /* If the guarding predicate has no bits set, no load occurs. */
5330 if (psz <= 8) {
5331 /* Reduce the pred_esz_masks value simply to reduce the
5332 * size of the code generated here.
5333 */
5334 uint64_t psz_mask = MAKE_64BIT_MASK(0, psz * 8);
5335 temp = tcg_temp_new_i64();
5336 tcg_gen_ld_i64(temp, cpu_env, pred_full_reg_offset(s, a->pg));
5337 tcg_gen_andi_i64(temp, temp, pred_esz_masks[esz] & psz_mask);
5338 tcg_gen_brcondi_i64(TCG_COND_EQ, temp, 0, over);
5339 tcg_temp_free_i64(temp);
5340 } else {
5341 TCGv_i32 t32 = tcg_temp_new_i32();
5342 find_last_active(s, t32, esz, a->pg);
5343 tcg_gen_brcondi_i32(TCG_COND_LT, t32, 0, over);
5344 tcg_temp_free_i32(t32);
5345 }
5346
5347 /* Load the data. */
5348 temp = tcg_temp_new_i64();
d0e372b0 5349 tcg_gen_addi_i64(temp, cpu_reg_sp(s, a->rn), a->imm << msz);
4ac430e1
RH
5350 clean_addr = gen_mte_check1(s, temp, false, true, msz);
5351
5352 tcg_gen_qemu_ld_i64(temp, clean_addr, get_mem_index(s),
0ca0f872 5353 finalize_memop(s, dtype_mop[a->dtype]));
68459864
RH
5354
5355 /* Broadcast to *all* elements. */
5356 tcg_gen_gvec_dup_i64(esz, vec_full_reg_offset(s, a->rd),
5357 vsz, vsz, temp);
5358 tcg_temp_free_i64(temp);
5359
5360 /* Zero the inactive elements. */
5361 gen_set_label(over);
60245996 5362 return do_movz_zpz(s, a->rd, a->rd, a->pg, esz, false);
68459864
RH
5363}
5364
1a039c7e
RH
5365static void do_st_zpa(DisasContext *s, int zt, int pg, TCGv_i64 addr,
5366 int msz, int esz, int nreg)
5367{
71b9f394
RH
5368 static gen_helper_gvec_mem * const fn_single[2][2][4][4] = {
5369 { { { gen_helper_sve_st1bb_r,
5370 gen_helper_sve_st1bh_r,
5371 gen_helper_sve_st1bs_r,
5372 gen_helper_sve_st1bd_r },
5373 { NULL,
5374 gen_helper_sve_st1hh_le_r,
5375 gen_helper_sve_st1hs_le_r,
5376 gen_helper_sve_st1hd_le_r },
5377 { NULL, NULL,
5378 gen_helper_sve_st1ss_le_r,
5379 gen_helper_sve_st1sd_le_r },
5380 { NULL, NULL, NULL,
5381 gen_helper_sve_st1dd_le_r } },
5382 { { gen_helper_sve_st1bb_r,
5383 gen_helper_sve_st1bh_r,
5384 gen_helper_sve_st1bs_r,
5385 gen_helper_sve_st1bd_r },
5386 { NULL,
5387 gen_helper_sve_st1hh_be_r,
5388 gen_helper_sve_st1hs_be_r,
5389 gen_helper_sve_st1hd_be_r },
5390 { NULL, NULL,
5391 gen_helper_sve_st1ss_be_r,
5392 gen_helper_sve_st1sd_be_r },
5393 { NULL, NULL, NULL,
5394 gen_helper_sve_st1dd_be_r } } },
5395
5396 { { { gen_helper_sve_st1bb_r_mte,
5397 gen_helper_sve_st1bh_r_mte,
5398 gen_helper_sve_st1bs_r_mte,
5399 gen_helper_sve_st1bd_r_mte },
5400 { NULL,
5401 gen_helper_sve_st1hh_le_r_mte,
5402 gen_helper_sve_st1hs_le_r_mte,
5403 gen_helper_sve_st1hd_le_r_mte },
5404 { NULL, NULL,
5405 gen_helper_sve_st1ss_le_r_mte,
5406 gen_helper_sve_st1sd_le_r_mte },
5407 { NULL, NULL, NULL,
5408 gen_helper_sve_st1dd_le_r_mte } },
5409 { { gen_helper_sve_st1bb_r_mte,
5410 gen_helper_sve_st1bh_r_mte,
5411 gen_helper_sve_st1bs_r_mte,
5412 gen_helper_sve_st1bd_r_mte },
5413 { NULL,
5414 gen_helper_sve_st1hh_be_r_mte,
5415 gen_helper_sve_st1hs_be_r_mte,
5416 gen_helper_sve_st1hd_be_r_mte },
5417 { NULL, NULL,
5418 gen_helper_sve_st1ss_be_r_mte,
5419 gen_helper_sve_st1sd_be_r_mte },
5420 { NULL, NULL, NULL,
5421 gen_helper_sve_st1dd_be_r_mte } } },
1a039c7e 5422 };
71b9f394
RH
5423 static gen_helper_gvec_mem * const fn_multiple[2][2][3][4] = {
5424 { { { gen_helper_sve_st2bb_r,
5425 gen_helper_sve_st2hh_le_r,
5426 gen_helper_sve_st2ss_le_r,
5427 gen_helper_sve_st2dd_le_r },
5428 { gen_helper_sve_st3bb_r,
5429 gen_helper_sve_st3hh_le_r,
5430 gen_helper_sve_st3ss_le_r,
5431 gen_helper_sve_st3dd_le_r },
5432 { gen_helper_sve_st4bb_r,
5433 gen_helper_sve_st4hh_le_r,
5434 gen_helper_sve_st4ss_le_r,
5435 gen_helper_sve_st4dd_le_r } },
5436 { { gen_helper_sve_st2bb_r,
5437 gen_helper_sve_st2hh_be_r,
5438 gen_helper_sve_st2ss_be_r,
5439 gen_helper_sve_st2dd_be_r },
5440 { gen_helper_sve_st3bb_r,
5441 gen_helper_sve_st3hh_be_r,
5442 gen_helper_sve_st3ss_be_r,
5443 gen_helper_sve_st3dd_be_r },
5444 { gen_helper_sve_st4bb_r,
5445 gen_helper_sve_st4hh_be_r,
5446 gen_helper_sve_st4ss_be_r,
5447 gen_helper_sve_st4dd_be_r } } },
5448 { { { gen_helper_sve_st2bb_r_mte,
5449 gen_helper_sve_st2hh_le_r_mte,
5450 gen_helper_sve_st2ss_le_r_mte,
5451 gen_helper_sve_st2dd_le_r_mte },
5452 { gen_helper_sve_st3bb_r_mte,
5453 gen_helper_sve_st3hh_le_r_mte,
5454 gen_helper_sve_st3ss_le_r_mte,
5455 gen_helper_sve_st3dd_le_r_mte },
5456 { gen_helper_sve_st4bb_r_mte,
5457 gen_helper_sve_st4hh_le_r_mte,
5458 gen_helper_sve_st4ss_le_r_mte,
5459 gen_helper_sve_st4dd_le_r_mte } },
5460 { { gen_helper_sve_st2bb_r_mte,
5461 gen_helper_sve_st2hh_be_r_mte,
5462 gen_helper_sve_st2ss_be_r_mte,
5463 gen_helper_sve_st2dd_be_r_mte },
5464 { gen_helper_sve_st3bb_r_mte,
5465 gen_helper_sve_st3hh_be_r_mte,
5466 gen_helper_sve_st3ss_be_r_mte,
5467 gen_helper_sve_st3dd_be_r_mte },
5468 { gen_helper_sve_st4bb_r_mte,
5469 gen_helper_sve_st4hh_be_r_mte,
5470 gen_helper_sve_st4ss_be_r_mte,
5471 gen_helper_sve_st4dd_be_r_mte } } },
1a039c7e
RH
5472 };
5473 gen_helper_gvec_mem *fn;
28d57f2d 5474 int be = s->be_data == MO_BE;
1a039c7e
RH
5475
5476 if (nreg == 0) {
5477 /* ST1 */
71b9f394
RH
5478 fn = fn_single[s->mte_active[0]][be][msz][esz];
5479 nreg = 1;
1a039c7e
RH
5480 } else {
5481 /* ST2, ST3, ST4 -- msz == esz, enforced by encoding */
5482 assert(msz == esz);
71b9f394 5483 fn = fn_multiple[s->mte_active[0]][be][nreg - 1][msz];
1a039c7e
RH
5484 }
5485 assert(fn != NULL);
71b9f394 5486 do_mem_zpa(s, zt, pg, addr, msz_dtype(s, msz), nreg, true, fn);
1a039c7e
RH
5487}
5488
3a7be554 5489static bool trans_ST_zprr(DisasContext *s, arg_rprr_store *a)
1a039c7e
RH
5490{
5491 if (a->rm == 31 || a->msz > a->esz) {
5492 return false;
5493 }
5494 if (sve_access_check(s)) {
5495 TCGv_i64 addr = new_tmp_a64(s);
50ef1cbf 5496 tcg_gen_shli_i64(addr, cpu_reg(s, a->rm), a->msz);
1a039c7e
RH
5497 tcg_gen_add_i64(addr, addr, cpu_reg_sp(s, a->rn));
5498 do_st_zpa(s, a->rd, a->pg, addr, a->msz, a->esz, a->nreg);
5499 }
5500 return true;
5501}
5502
3a7be554 5503static bool trans_ST_zpri(DisasContext *s, arg_rpri_store *a)
1a039c7e
RH
5504{
5505 if (a->msz > a->esz) {
5506 return false;
5507 }
5508 if (sve_access_check(s)) {
5509 int vsz = vec_full_reg_size(s);
5510 int elements = vsz >> a->esz;
5511 TCGv_i64 addr = new_tmp_a64(s);
5512
5513 tcg_gen_addi_i64(addr, cpu_reg_sp(s, a->rn),
5514 (a->imm * elements * (a->nreg + 1)) << a->msz);
5515 do_st_zpa(s, a->rd, a->pg, addr, a->msz, a->esz, a->nreg);
5516 }
5517 return true;
5518}
f6dbf62a
RH
5519
5520/*
5521 *** SVE gather loads / scatter stores
5522 */
5523
500d0484 5524static void do_mem_zpz(DisasContext *s, int zt, int pg, int zm,
d28d12f0 5525 int scale, TCGv_i64 scalar, int msz, bool is_write,
500d0484 5526 gen_helper_gvec_mem_scatter *fn)
f6dbf62a
RH
5527{
5528 unsigned vsz = vec_full_reg_size(s);
f6dbf62a
RH
5529 TCGv_ptr t_zm = tcg_temp_new_ptr();
5530 TCGv_ptr t_pg = tcg_temp_new_ptr();
5531 TCGv_ptr t_zt = tcg_temp_new_ptr();
d28d12f0 5532 int desc = 0;
500d0484 5533
d28d12f0
RH
5534 if (s->mte_active[0]) {
5535 desc = FIELD_DP32(desc, MTEDESC, MIDX, get_mem_index(s));
5536 desc = FIELD_DP32(desc, MTEDESC, TBI, s->tbid);
5537 desc = FIELD_DP32(desc, MTEDESC, TCMA, s->tcma);
5538 desc = FIELD_DP32(desc, MTEDESC, WRITE, is_write);
28f32503 5539 desc = FIELD_DP32(desc, MTEDESC, SIZEM1, (1 << msz) - 1);
d28d12f0
RH
5540 desc <<= SVE_MTEDESC_SHIFT;
5541 }
cdecb3fc 5542 desc = simd_desc(vsz, vsz, desc | scale);
f6dbf62a
RH
5543
5544 tcg_gen_addi_ptr(t_pg, cpu_env, pred_full_reg_offset(s, pg));
5545 tcg_gen_addi_ptr(t_zm, cpu_env, vec_full_reg_offset(s, zm));
5546 tcg_gen_addi_ptr(t_zt, cpu_env, vec_full_reg_offset(s, zt));
c6a59b55 5547 fn(cpu_env, t_zt, t_pg, t_zm, scalar, tcg_constant_i32(desc));
f6dbf62a
RH
5548
5549 tcg_temp_free_ptr(t_zt);
5550 tcg_temp_free_ptr(t_zm);
5551 tcg_temp_free_ptr(t_pg);
f6dbf62a
RH
5552}
5553
d28d12f0
RH
5554/* Indexed by [mte][be][ff][xs][u][msz]. */
5555static gen_helper_gvec_mem_scatter * const
5556gather_load_fn32[2][2][2][2][2][3] = {
5557 { /* MTE Inactive */
5558 { /* Little-endian */
5559 { { { gen_helper_sve_ldbss_zsu,
5560 gen_helper_sve_ldhss_le_zsu,
5561 NULL, },
5562 { gen_helper_sve_ldbsu_zsu,
5563 gen_helper_sve_ldhsu_le_zsu,
5564 gen_helper_sve_ldss_le_zsu, } },
5565 { { gen_helper_sve_ldbss_zss,
5566 gen_helper_sve_ldhss_le_zss,
5567 NULL, },
5568 { gen_helper_sve_ldbsu_zss,
5569 gen_helper_sve_ldhsu_le_zss,
5570 gen_helper_sve_ldss_le_zss, } } },
5571
5572 /* First-fault */
5573 { { { gen_helper_sve_ldffbss_zsu,
5574 gen_helper_sve_ldffhss_le_zsu,
5575 NULL, },
5576 { gen_helper_sve_ldffbsu_zsu,
5577 gen_helper_sve_ldffhsu_le_zsu,
5578 gen_helper_sve_ldffss_le_zsu, } },
5579 { { gen_helper_sve_ldffbss_zss,
5580 gen_helper_sve_ldffhss_le_zss,
5581 NULL, },
5582 { gen_helper_sve_ldffbsu_zss,
5583 gen_helper_sve_ldffhsu_le_zss,
5584 gen_helper_sve_ldffss_le_zss, } } } },
5585
5586 { /* Big-endian */
5587 { { { gen_helper_sve_ldbss_zsu,
5588 gen_helper_sve_ldhss_be_zsu,
5589 NULL, },
5590 { gen_helper_sve_ldbsu_zsu,
5591 gen_helper_sve_ldhsu_be_zsu,
5592 gen_helper_sve_ldss_be_zsu, } },
5593 { { gen_helper_sve_ldbss_zss,
5594 gen_helper_sve_ldhss_be_zss,
5595 NULL, },
5596 { gen_helper_sve_ldbsu_zss,
5597 gen_helper_sve_ldhsu_be_zss,
5598 gen_helper_sve_ldss_be_zss, } } },
5599
5600 /* First-fault */
5601 { { { gen_helper_sve_ldffbss_zsu,
5602 gen_helper_sve_ldffhss_be_zsu,
5603 NULL, },
5604 { gen_helper_sve_ldffbsu_zsu,
5605 gen_helper_sve_ldffhsu_be_zsu,
5606 gen_helper_sve_ldffss_be_zsu, } },
5607 { { gen_helper_sve_ldffbss_zss,
5608 gen_helper_sve_ldffhss_be_zss,
5609 NULL, },
5610 { gen_helper_sve_ldffbsu_zss,
5611 gen_helper_sve_ldffhsu_be_zss,
5612 gen_helper_sve_ldffss_be_zss, } } } } },
5613 { /* MTE Active */
5614 { /* Little-endian */
5615 { { { gen_helper_sve_ldbss_zsu_mte,
5616 gen_helper_sve_ldhss_le_zsu_mte,
5617 NULL, },
5618 { gen_helper_sve_ldbsu_zsu_mte,
5619 gen_helper_sve_ldhsu_le_zsu_mte,
5620 gen_helper_sve_ldss_le_zsu_mte, } },
5621 { { gen_helper_sve_ldbss_zss_mte,
5622 gen_helper_sve_ldhss_le_zss_mte,
5623 NULL, },
5624 { gen_helper_sve_ldbsu_zss_mte,
5625 gen_helper_sve_ldhsu_le_zss_mte,
5626 gen_helper_sve_ldss_le_zss_mte, } } },
5627
5628 /* First-fault */
5629 { { { gen_helper_sve_ldffbss_zsu_mte,
5630 gen_helper_sve_ldffhss_le_zsu_mte,
5631 NULL, },
5632 { gen_helper_sve_ldffbsu_zsu_mte,
5633 gen_helper_sve_ldffhsu_le_zsu_mte,
5634 gen_helper_sve_ldffss_le_zsu_mte, } },
5635 { { gen_helper_sve_ldffbss_zss_mte,
5636 gen_helper_sve_ldffhss_le_zss_mte,
5637 NULL, },
5638 { gen_helper_sve_ldffbsu_zss_mte,
5639 gen_helper_sve_ldffhsu_le_zss_mte,
5640 gen_helper_sve_ldffss_le_zss_mte, } } } },
5641
5642 { /* Big-endian */
5643 { { { gen_helper_sve_ldbss_zsu_mte,
5644 gen_helper_sve_ldhss_be_zsu_mte,
5645 NULL, },
5646 { gen_helper_sve_ldbsu_zsu_mte,
5647 gen_helper_sve_ldhsu_be_zsu_mte,
5648 gen_helper_sve_ldss_be_zsu_mte, } },
5649 { { gen_helper_sve_ldbss_zss_mte,
5650 gen_helper_sve_ldhss_be_zss_mte,
5651 NULL, },
5652 { gen_helper_sve_ldbsu_zss_mte,
5653 gen_helper_sve_ldhsu_be_zss_mte,
5654 gen_helper_sve_ldss_be_zss_mte, } } },
5655
5656 /* First-fault */
5657 { { { gen_helper_sve_ldffbss_zsu_mte,
5658 gen_helper_sve_ldffhss_be_zsu_mte,
5659 NULL, },
5660 { gen_helper_sve_ldffbsu_zsu_mte,
5661 gen_helper_sve_ldffhsu_be_zsu_mte,
5662 gen_helper_sve_ldffss_be_zsu_mte, } },
5663 { { gen_helper_sve_ldffbss_zss_mte,
5664 gen_helper_sve_ldffhss_be_zss_mte,
5665 NULL, },
5666 { gen_helper_sve_ldffbsu_zss_mte,
5667 gen_helper_sve_ldffhsu_be_zss_mte,
5668 gen_helper_sve_ldffss_be_zss_mte, } } } } },
673e9fa6
RH
5669};
5670
5671/* Note that we overload xs=2 to indicate 64-bit offset. */
d28d12f0
RH
5672static gen_helper_gvec_mem_scatter * const
5673gather_load_fn64[2][2][2][3][2][4] = {
5674 { /* MTE Inactive */
5675 { /* Little-endian */
5676 { { { gen_helper_sve_ldbds_zsu,
5677 gen_helper_sve_ldhds_le_zsu,
5678 gen_helper_sve_ldsds_le_zsu,
5679 NULL, },
5680 { gen_helper_sve_ldbdu_zsu,
5681 gen_helper_sve_ldhdu_le_zsu,
5682 gen_helper_sve_ldsdu_le_zsu,
5683 gen_helper_sve_lddd_le_zsu, } },
5684 { { gen_helper_sve_ldbds_zss,
5685 gen_helper_sve_ldhds_le_zss,
5686 gen_helper_sve_ldsds_le_zss,
5687 NULL, },
5688 { gen_helper_sve_ldbdu_zss,
5689 gen_helper_sve_ldhdu_le_zss,
5690 gen_helper_sve_ldsdu_le_zss,
5691 gen_helper_sve_lddd_le_zss, } },
5692 { { gen_helper_sve_ldbds_zd,
5693 gen_helper_sve_ldhds_le_zd,
5694 gen_helper_sve_ldsds_le_zd,
5695 NULL, },
5696 { gen_helper_sve_ldbdu_zd,
5697 gen_helper_sve_ldhdu_le_zd,
5698 gen_helper_sve_ldsdu_le_zd,
5699 gen_helper_sve_lddd_le_zd, } } },
5700
5701 /* First-fault */
5702 { { { gen_helper_sve_ldffbds_zsu,
5703 gen_helper_sve_ldffhds_le_zsu,
5704 gen_helper_sve_ldffsds_le_zsu,
5705 NULL, },
5706 { gen_helper_sve_ldffbdu_zsu,
5707 gen_helper_sve_ldffhdu_le_zsu,
5708 gen_helper_sve_ldffsdu_le_zsu,
5709 gen_helper_sve_ldffdd_le_zsu, } },
5710 { { gen_helper_sve_ldffbds_zss,
5711 gen_helper_sve_ldffhds_le_zss,
5712 gen_helper_sve_ldffsds_le_zss,
5713 NULL, },
5714 { gen_helper_sve_ldffbdu_zss,
5715 gen_helper_sve_ldffhdu_le_zss,
5716 gen_helper_sve_ldffsdu_le_zss,
5717 gen_helper_sve_ldffdd_le_zss, } },
5718 { { gen_helper_sve_ldffbds_zd,
5719 gen_helper_sve_ldffhds_le_zd,
5720 gen_helper_sve_ldffsds_le_zd,
5721 NULL, },
5722 { gen_helper_sve_ldffbdu_zd,
5723 gen_helper_sve_ldffhdu_le_zd,
5724 gen_helper_sve_ldffsdu_le_zd,
5725 gen_helper_sve_ldffdd_le_zd, } } } },
5726 { /* Big-endian */
5727 { { { gen_helper_sve_ldbds_zsu,
5728 gen_helper_sve_ldhds_be_zsu,
5729 gen_helper_sve_ldsds_be_zsu,
5730 NULL, },
5731 { gen_helper_sve_ldbdu_zsu,
5732 gen_helper_sve_ldhdu_be_zsu,
5733 gen_helper_sve_ldsdu_be_zsu,
5734 gen_helper_sve_lddd_be_zsu, } },
5735 { { gen_helper_sve_ldbds_zss,
5736 gen_helper_sve_ldhds_be_zss,
5737 gen_helper_sve_ldsds_be_zss,
5738 NULL, },
5739 { gen_helper_sve_ldbdu_zss,
5740 gen_helper_sve_ldhdu_be_zss,
5741 gen_helper_sve_ldsdu_be_zss,
5742 gen_helper_sve_lddd_be_zss, } },
5743 { { gen_helper_sve_ldbds_zd,
5744 gen_helper_sve_ldhds_be_zd,
5745 gen_helper_sve_ldsds_be_zd,
5746 NULL, },
5747 { gen_helper_sve_ldbdu_zd,
5748 gen_helper_sve_ldhdu_be_zd,
5749 gen_helper_sve_ldsdu_be_zd,
5750 gen_helper_sve_lddd_be_zd, } } },
5751
5752 /* First-fault */
5753 { { { gen_helper_sve_ldffbds_zsu,
5754 gen_helper_sve_ldffhds_be_zsu,
5755 gen_helper_sve_ldffsds_be_zsu,
5756 NULL, },
5757 { gen_helper_sve_ldffbdu_zsu,
5758 gen_helper_sve_ldffhdu_be_zsu,
5759 gen_helper_sve_ldffsdu_be_zsu,
5760 gen_helper_sve_ldffdd_be_zsu, } },
5761 { { gen_helper_sve_ldffbds_zss,
5762 gen_helper_sve_ldffhds_be_zss,
5763 gen_helper_sve_ldffsds_be_zss,
5764 NULL, },
5765 { gen_helper_sve_ldffbdu_zss,
5766 gen_helper_sve_ldffhdu_be_zss,
5767 gen_helper_sve_ldffsdu_be_zss,
5768 gen_helper_sve_ldffdd_be_zss, } },
5769 { { gen_helper_sve_ldffbds_zd,
5770 gen_helper_sve_ldffhds_be_zd,
5771 gen_helper_sve_ldffsds_be_zd,
5772 NULL, },
5773 { gen_helper_sve_ldffbdu_zd,
5774 gen_helper_sve_ldffhdu_be_zd,
5775 gen_helper_sve_ldffsdu_be_zd,
5776 gen_helper_sve_ldffdd_be_zd, } } } } },
5777 { /* MTE Active */
5778 { /* Little-endian */
5779 { { { gen_helper_sve_ldbds_zsu_mte,
5780 gen_helper_sve_ldhds_le_zsu_mte,
5781 gen_helper_sve_ldsds_le_zsu_mte,
5782 NULL, },
5783 { gen_helper_sve_ldbdu_zsu_mte,
5784 gen_helper_sve_ldhdu_le_zsu_mte,
5785 gen_helper_sve_ldsdu_le_zsu_mte,
5786 gen_helper_sve_lddd_le_zsu_mte, } },
5787 { { gen_helper_sve_ldbds_zss_mte,
5788 gen_helper_sve_ldhds_le_zss_mte,
5789 gen_helper_sve_ldsds_le_zss_mte,
5790 NULL, },
5791 { gen_helper_sve_ldbdu_zss_mte,
5792 gen_helper_sve_ldhdu_le_zss_mte,
5793 gen_helper_sve_ldsdu_le_zss_mte,
5794 gen_helper_sve_lddd_le_zss_mte, } },
5795 { { gen_helper_sve_ldbds_zd_mte,
5796 gen_helper_sve_ldhds_le_zd_mte,
5797 gen_helper_sve_ldsds_le_zd_mte,
5798 NULL, },
5799 { gen_helper_sve_ldbdu_zd_mte,
5800 gen_helper_sve_ldhdu_le_zd_mte,
5801 gen_helper_sve_ldsdu_le_zd_mte,
5802 gen_helper_sve_lddd_le_zd_mte, } } },
5803
5804 /* First-fault */
5805 { { { gen_helper_sve_ldffbds_zsu_mte,
5806 gen_helper_sve_ldffhds_le_zsu_mte,
5807 gen_helper_sve_ldffsds_le_zsu_mte,
5808 NULL, },
5809 { gen_helper_sve_ldffbdu_zsu_mte,
5810 gen_helper_sve_ldffhdu_le_zsu_mte,
5811 gen_helper_sve_ldffsdu_le_zsu_mte,
5812 gen_helper_sve_ldffdd_le_zsu_mte, } },
5813 { { gen_helper_sve_ldffbds_zss_mte,
5814 gen_helper_sve_ldffhds_le_zss_mte,
5815 gen_helper_sve_ldffsds_le_zss_mte,
5816 NULL, },
5817 { gen_helper_sve_ldffbdu_zss_mte,
5818 gen_helper_sve_ldffhdu_le_zss_mte,
5819 gen_helper_sve_ldffsdu_le_zss_mte,
5820 gen_helper_sve_ldffdd_le_zss_mte, } },
5821 { { gen_helper_sve_ldffbds_zd_mte,
5822 gen_helper_sve_ldffhds_le_zd_mte,
5823 gen_helper_sve_ldffsds_le_zd_mte,
5824 NULL, },
5825 { gen_helper_sve_ldffbdu_zd_mte,
5826 gen_helper_sve_ldffhdu_le_zd_mte,
5827 gen_helper_sve_ldffsdu_le_zd_mte,
5828 gen_helper_sve_ldffdd_le_zd_mte, } } } },
5829 { /* Big-endian */
5830 { { { gen_helper_sve_ldbds_zsu_mte,
5831 gen_helper_sve_ldhds_be_zsu_mte,
5832 gen_helper_sve_ldsds_be_zsu_mte,
5833 NULL, },
5834 { gen_helper_sve_ldbdu_zsu_mte,
5835 gen_helper_sve_ldhdu_be_zsu_mte,
5836 gen_helper_sve_ldsdu_be_zsu_mte,
5837 gen_helper_sve_lddd_be_zsu_mte, } },
5838 { { gen_helper_sve_ldbds_zss_mte,
5839 gen_helper_sve_ldhds_be_zss_mte,
5840 gen_helper_sve_ldsds_be_zss_mte,
5841 NULL, },
5842 { gen_helper_sve_ldbdu_zss_mte,
5843 gen_helper_sve_ldhdu_be_zss_mte,
5844 gen_helper_sve_ldsdu_be_zss_mte,
5845 gen_helper_sve_lddd_be_zss_mte, } },
5846 { { gen_helper_sve_ldbds_zd_mte,
5847 gen_helper_sve_ldhds_be_zd_mte,
5848 gen_helper_sve_ldsds_be_zd_mte,
5849 NULL, },
5850 { gen_helper_sve_ldbdu_zd_mte,
5851 gen_helper_sve_ldhdu_be_zd_mte,
5852 gen_helper_sve_ldsdu_be_zd_mte,
5853 gen_helper_sve_lddd_be_zd_mte, } } },
5854
5855 /* First-fault */
5856 { { { gen_helper_sve_ldffbds_zsu_mte,
5857 gen_helper_sve_ldffhds_be_zsu_mte,
5858 gen_helper_sve_ldffsds_be_zsu_mte,
5859 NULL, },
5860 { gen_helper_sve_ldffbdu_zsu_mte,
5861 gen_helper_sve_ldffhdu_be_zsu_mte,
5862 gen_helper_sve_ldffsdu_be_zsu_mte,
5863 gen_helper_sve_ldffdd_be_zsu_mte, } },
5864 { { gen_helper_sve_ldffbds_zss_mte,
5865 gen_helper_sve_ldffhds_be_zss_mte,
5866 gen_helper_sve_ldffsds_be_zss_mte,
5867 NULL, },
5868 { gen_helper_sve_ldffbdu_zss_mte,
5869 gen_helper_sve_ldffhdu_be_zss_mte,
5870 gen_helper_sve_ldffsdu_be_zss_mte,
5871 gen_helper_sve_ldffdd_be_zss_mte, } },
5872 { { gen_helper_sve_ldffbds_zd_mte,
5873 gen_helper_sve_ldffhds_be_zd_mte,
5874 gen_helper_sve_ldffsds_be_zd_mte,
5875 NULL, },
5876 { gen_helper_sve_ldffbdu_zd_mte,
5877 gen_helper_sve_ldffhdu_be_zd_mte,
5878 gen_helper_sve_ldffsdu_be_zd_mte,
5879 gen_helper_sve_ldffdd_be_zd_mte, } } } } },
673e9fa6
RH
5880};
5881
3a7be554 5882static bool trans_LD1_zprz(DisasContext *s, arg_LD1_zprz *a)
673e9fa6
RH
5883{
5884 gen_helper_gvec_mem_scatter *fn = NULL;
d28d12f0
RH
5885 bool be = s->be_data == MO_BE;
5886 bool mte = s->mte_active[0];
673e9fa6
RH
5887
5888 if (!sve_access_check(s)) {
5889 return true;
5890 }
5891
5892 switch (a->esz) {
5893 case MO_32:
d28d12f0 5894 fn = gather_load_fn32[mte][be][a->ff][a->xs][a->u][a->msz];
673e9fa6
RH
5895 break;
5896 case MO_64:
d28d12f0 5897 fn = gather_load_fn64[mte][be][a->ff][a->xs][a->u][a->msz];
673e9fa6
RH
5898 break;
5899 }
5900 assert(fn != NULL);
5901
5902 do_mem_zpz(s, a->rd, a->pg, a->rm, a->scale * a->msz,
d28d12f0 5903 cpu_reg_sp(s, a->rn), a->msz, false, fn);
673e9fa6
RH
5904 return true;
5905}
5906
3a7be554 5907static bool trans_LD1_zpiz(DisasContext *s, arg_LD1_zpiz *a)
673e9fa6
RH
5908{
5909 gen_helper_gvec_mem_scatter *fn = NULL;
d28d12f0
RH
5910 bool be = s->be_data == MO_BE;
5911 bool mte = s->mte_active[0];
673e9fa6
RH
5912
5913 if (a->esz < a->msz || (a->esz == a->msz && !a->u)) {
5914 return false;
5915 }
5916 if (!sve_access_check(s)) {
5917 return true;
5918 }
5919
5920 switch (a->esz) {
5921 case MO_32:
d28d12f0 5922 fn = gather_load_fn32[mte][be][a->ff][0][a->u][a->msz];
673e9fa6
RH
5923 break;
5924 case MO_64:
d28d12f0 5925 fn = gather_load_fn64[mte][be][a->ff][2][a->u][a->msz];
673e9fa6
RH
5926 break;
5927 }
5928 assert(fn != NULL);
5929
5930 /* Treat LD1_zpiz (zn[x] + imm) the same way as LD1_zprz (rn + zm[x])
5931 * by loading the immediate into the scalar parameter.
5932 */
2ccdf94f
RH
5933 do_mem_zpz(s, a->rd, a->pg, a->rn, 0,
5934 tcg_constant_i64(a->imm << a->msz), a->msz, false, fn);
673e9fa6
RH
5935 return true;
5936}
5937
cf327449
SL
5938static bool trans_LDNT1_zprz(DisasContext *s, arg_LD1_zprz *a)
5939{
b17ab470
RH
5940 gen_helper_gvec_mem_scatter *fn = NULL;
5941 bool be = s->be_data == MO_BE;
5942 bool mte = s->mte_active[0];
5943
5944 if (a->esz < a->msz + !a->u) {
5945 return false;
5946 }
cf327449
SL
5947 if (!dc_isar_feature(aa64_sve2, s)) {
5948 return false;
5949 }
b17ab470
RH
5950 if (!sve_access_check(s)) {
5951 return true;
5952 }
5953
5954 switch (a->esz) {
5955 case MO_32:
5956 fn = gather_load_fn32[mte][be][0][0][a->u][a->msz];
5957 break;
5958 case MO_64:
5959 fn = gather_load_fn64[mte][be][0][2][a->u][a->msz];
5960 break;
5961 }
5962 assert(fn != NULL);
5963
5964 do_mem_zpz(s, a->rd, a->pg, a->rn, 0,
5965 cpu_reg(s, a->rm), a->msz, false, fn);
5966 return true;
cf327449
SL
5967}
5968
d28d12f0
RH
5969/* Indexed by [mte][be][xs][msz]. */
5970static gen_helper_gvec_mem_scatter * const scatter_store_fn32[2][2][2][3] = {
5971 { /* MTE Inactive */
5972 { /* Little-endian */
5973 { gen_helper_sve_stbs_zsu,
5974 gen_helper_sve_sths_le_zsu,
5975 gen_helper_sve_stss_le_zsu, },
5976 { gen_helper_sve_stbs_zss,
5977 gen_helper_sve_sths_le_zss,
5978 gen_helper_sve_stss_le_zss, } },
5979 { /* Big-endian */
5980 { gen_helper_sve_stbs_zsu,
5981 gen_helper_sve_sths_be_zsu,
5982 gen_helper_sve_stss_be_zsu, },
5983 { gen_helper_sve_stbs_zss,
5984 gen_helper_sve_sths_be_zss,
5985 gen_helper_sve_stss_be_zss, } } },
5986 { /* MTE Active */
5987 { /* Little-endian */
5988 { gen_helper_sve_stbs_zsu_mte,
5989 gen_helper_sve_sths_le_zsu_mte,
5990 gen_helper_sve_stss_le_zsu_mte, },
5991 { gen_helper_sve_stbs_zss_mte,
5992 gen_helper_sve_sths_le_zss_mte,
5993 gen_helper_sve_stss_le_zss_mte, } },
5994 { /* Big-endian */
5995 { gen_helper_sve_stbs_zsu_mte,
5996 gen_helper_sve_sths_be_zsu_mte,
5997 gen_helper_sve_stss_be_zsu_mte, },
5998 { gen_helper_sve_stbs_zss_mte,
5999 gen_helper_sve_sths_be_zss_mte,
6000 gen_helper_sve_stss_be_zss_mte, } } },
408ecde9
RH
6001};
6002
6003/* Note that we overload xs=2 to indicate 64-bit offset. */
d28d12f0
RH
6004static gen_helper_gvec_mem_scatter * const scatter_store_fn64[2][2][3][4] = {
6005 { /* MTE Inactive */
6006 { /* Little-endian */
6007 { gen_helper_sve_stbd_zsu,
6008 gen_helper_sve_sthd_le_zsu,
6009 gen_helper_sve_stsd_le_zsu,
6010 gen_helper_sve_stdd_le_zsu, },
6011 { gen_helper_sve_stbd_zss,
6012 gen_helper_sve_sthd_le_zss,
6013 gen_helper_sve_stsd_le_zss,
6014 gen_helper_sve_stdd_le_zss, },
6015 { gen_helper_sve_stbd_zd,
6016 gen_helper_sve_sthd_le_zd,
6017 gen_helper_sve_stsd_le_zd,
6018 gen_helper_sve_stdd_le_zd, } },
6019 { /* Big-endian */
6020 { gen_helper_sve_stbd_zsu,
6021 gen_helper_sve_sthd_be_zsu,
6022 gen_helper_sve_stsd_be_zsu,
6023 gen_helper_sve_stdd_be_zsu, },
6024 { gen_helper_sve_stbd_zss,
6025 gen_helper_sve_sthd_be_zss,
6026 gen_helper_sve_stsd_be_zss,
6027 gen_helper_sve_stdd_be_zss, },
6028 { gen_helper_sve_stbd_zd,
6029 gen_helper_sve_sthd_be_zd,
6030 gen_helper_sve_stsd_be_zd,
6031 gen_helper_sve_stdd_be_zd, } } },
6032 { /* MTE Inactive */
6033 { /* Little-endian */
6034 { gen_helper_sve_stbd_zsu_mte,
6035 gen_helper_sve_sthd_le_zsu_mte,
6036 gen_helper_sve_stsd_le_zsu_mte,
6037 gen_helper_sve_stdd_le_zsu_mte, },
6038 { gen_helper_sve_stbd_zss_mte,
6039 gen_helper_sve_sthd_le_zss_mte,
6040 gen_helper_sve_stsd_le_zss_mte,
6041 gen_helper_sve_stdd_le_zss_mte, },
6042 { gen_helper_sve_stbd_zd_mte,
6043 gen_helper_sve_sthd_le_zd_mte,
6044 gen_helper_sve_stsd_le_zd_mte,
6045 gen_helper_sve_stdd_le_zd_mte, } },
6046 { /* Big-endian */
6047 { gen_helper_sve_stbd_zsu_mte,
6048 gen_helper_sve_sthd_be_zsu_mte,
6049 gen_helper_sve_stsd_be_zsu_mte,
6050 gen_helper_sve_stdd_be_zsu_mte, },
6051 { gen_helper_sve_stbd_zss_mte,
6052 gen_helper_sve_sthd_be_zss_mte,
6053 gen_helper_sve_stsd_be_zss_mte,
6054 gen_helper_sve_stdd_be_zss_mte, },
6055 { gen_helper_sve_stbd_zd_mte,
6056 gen_helper_sve_sthd_be_zd_mte,
6057 gen_helper_sve_stsd_be_zd_mte,
6058 gen_helper_sve_stdd_be_zd_mte, } } },
408ecde9
RH
6059};
6060
3a7be554 6061static bool trans_ST1_zprz(DisasContext *s, arg_ST1_zprz *a)
f6dbf62a 6062{
f6dbf62a 6063 gen_helper_gvec_mem_scatter *fn;
d28d12f0
RH
6064 bool be = s->be_data == MO_BE;
6065 bool mte = s->mte_active[0];
f6dbf62a
RH
6066
6067 if (a->esz < a->msz || (a->msz == 0 && a->scale)) {
6068 return false;
6069 }
6070 if (!sve_access_check(s)) {
6071 return true;
6072 }
6073 switch (a->esz) {
6074 case MO_32:
d28d12f0 6075 fn = scatter_store_fn32[mte][be][a->xs][a->msz];
f6dbf62a
RH
6076 break;
6077 case MO_64:
d28d12f0 6078 fn = scatter_store_fn64[mte][be][a->xs][a->msz];
f6dbf62a
RH
6079 break;
6080 default:
6081 g_assert_not_reached();
6082 }
6083 do_mem_zpz(s, a->rd, a->pg, a->rm, a->scale * a->msz,
d28d12f0 6084 cpu_reg_sp(s, a->rn), a->msz, true, fn);
f6dbf62a
RH
6085 return true;
6086}
dec6cf6b 6087
3a7be554 6088static bool trans_ST1_zpiz(DisasContext *s, arg_ST1_zpiz *a)
408ecde9
RH
6089{
6090 gen_helper_gvec_mem_scatter *fn = NULL;
d28d12f0
RH
6091 bool be = s->be_data == MO_BE;
6092 bool mte = s->mte_active[0];
408ecde9
RH
6093
6094 if (a->esz < a->msz) {
6095 return false;
6096 }
6097 if (!sve_access_check(s)) {
6098 return true;
6099 }
6100
6101 switch (a->esz) {
6102 case MO_32:
d28d12f0 6103 fn = scatter_store_fn32[mte][be][0][a->msz];
408ecde9
RH
6104 break;
6105 case MO_64:
d28d12f0 6106 fn = scatter_store_fn64[mte][be][2][a->msz];
408ecde9
RH
6107 break;
6108 }
6109 assert(fn != NULL);
6110
6111 /* Treat ST1_zpiz (zn[x] + imm) the same way as ST1_zprz (rn + zm[x])
6112 * by loading the immediate into the scalar parameter.
6113 */
2ccdf94f
RH
6114 do_mem_zpz(s, a->rd, a->pg, a->rn, 0,
6115 tcg_constant_i64(a->imm << a->msz), a->msz, true, fn);
408ecde9
RH
6116 return true;
6117}
6118
6ebca45f
SL
6119static bool trans_STNT1_zprz(DisasContext *s, arg_ST1_zprz *a)
6120{
b17ab470
RH
6121 gen_helper_gvec_mem_scatter *fn;
6122 bool be = s->be_data == MO_BE;
6123 bool mte = s->mte_active[0];
6124
6125 if (a->esz < a->msz) {
6126 return false;
6127 }
6ebca45f
SL
6128 if (!dc_isar_feature(aa64_sve2, s)) {
6129 return false;
6130 }
b17ab470
RH
6131 if (!sve_access_check(s)) {
6132 return true;
6133 }
6134
6135 switch (a->esz) {
6136 case MO_32:
6137 fn = scatter_store_fn32[mte][be][0][a->msz];
6138 break;
6139 case MO_64:
6140 fn = scatter_store_fn64[mte][be][2][a->msz];
6141 break;
6142 default:
6143 g_assert_not_reached();
6144 }
6145
6146 do_mem_zpz(s, a->rd, a->pg, a->rn, 0,
6147 cpu_reg(s, a->rm), a->msz, true, fn);
6148 return true;
6ebca45f
SL
6149}
6150
dec6cf6b
RH
6151/*
6152 * Prefetches
6153 */
6154
3a7be554 6155static bool trans_PRF(DisasContext *s, arg_PRF *a)
dec6cf6b
RH
6156{
6157 /* Prefetch is a nop within QEMU. */
2f95a3b0 6158 (void)sve_access_check(s);
dec6cf6b
RH
6159 return true;
6160}
6161
3a7be554 6162static bool trans_PRF_rr(DisasContext *s, arg_PRF_rr *a)
dec6cf6b
RH
6163{
6164 if (a->rm == 31) {
6165 return false;
6166 }
6167 /* Prefetch is a nop within QEMU. */
2f95a3b0 6168 (void)sve_access_check(s);
dec6cf6b
RH
6169 return true;
6170}
a2103582
RH
6171
6172/*
6173 * Move Prefix
6174 *
6175 * TODO: The implementation so far could handle predicated merging movprfx.
6176 * The helper functions as written take an extra source register to
6177 * use in the operation, but the result is only written when predication
6178 * succeeds. For unpredicated movprfx, we need to rearrange the helpers
6179 * to allow the final write back to the destination to be unconditional.
6180 * For predicated zeroing movprfx, we need to rearrange the helpers to
6181 * allow the final write back to zero inactives.
6182 *
6183 * In the meantime, just emit the moves.
6184 */
6185
3a7be554 6186static bool trans_MOVPRFX(DisasContext *s, arg_MOVPRFX *a)
a2103582
RH
6187{
6188 return do_mov_z(s, a->rd, a->rn);
6189}
6190
3a7be554 6191static bool trans_MOVPRFX_m(DisasContext *s, arg_rpr_esz *a)
a2103582 6192{
68cc4ee3 6193 return do_sel_z(s, a->rd, a->rn, a->rd, a->pg, a->esz);
a2103582
RH
6194}
6195
3a7be554 6196static bool trans_MOVPRFX_z(DisasContext *s, arg_rpr_esz *a)
a2103582 6197{
60245996 6198 return do_movz_zpz(s, a->rd, a->rn, a->pg, a->esz, false);
a2103582 6199}
5dad1ba5
RH
6200
6201/*
6202 * SVE2 Integer Multiply - Unpredicated
6203 */
6204
b262215b 6205TRANS_FEAT(MUL_zzz, aa64_sve2, gen_gvec_fn_arg_zzz, tcg_gen_gvec_mul, a)
5dad1ba5 6206
bd394cf5
RH
6207static gen_helper_gvec_3 * const smulh_zzz_fns[4] = {
6208 gen_helper_gvec_smulh_b, gen_helper_gvec_smulh_h,
6209 gen_helper_gvec_smulh_s, gen_helper_gvec_smulh_d,
6210};
6211TRANS_FEAT(SMULH_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6212 smulh_zzz_fns[a->esz], a, 0)
5dad1ba5 6213
bd394cf5
RH
6214static gen_helper_gvec_3 * const umulh_zzz_fns[4] = {
6215 gen_helper_gvec_umulh_b, gen_helper_gvec_umulh_h,
6216 gen_helper_gvec_umulh_s, gen_helper_gvec_umulh_d,
6217};
6218TRANS_FEAT(UMULH_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6219 umulh_zzz_fns[a->esz], a, 0)
5dad1ba5 6220
bd394cf5
RH
6221TRANS_FEAT(PMUL_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6222 gen_helper_gvec_pmul_b, a, 0)
5dad1ba5 6223
bd394cf5
RH
6224static gen_helper_gvec_3 * const sqdmulh_zzz_fns[4] = {
6225 gen_helper_sve2_sqdmulh_b, gen_helper_sve2_sqdmulh_h,
6226 gen_helper_sve2_sqdmulh_s, gen_helper_sve2_sqdmulh_d,
6227};
6228TRANS_FEAT(SQDMULH_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6229 sqdmulh_zzz_fns[a->esz], a, 0)
169d7c58 6230
bd394cf5
RH
6231static gen_helper_gvec_3 * const sqrdmulh_zzz_fns[4] = {
6232 gen_helper_sve2_sqrdmulh_b, gen_helper_sve2_sqrdmulh_h,
6233 gen_helper_sve2_sqrdmulh_s, gen_helper_sve2_sqrdmulh_d,
6234};
6235TRANS_FEAT(SQRDMULH_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6236 sqrdmulh_zzz_fns[a->esz], a, 0)
169d7c58 6237
d4b1e59d
RH
6238/*
6239 * SVE2 Integer - Predicated
6240 */
6241
5880bdc0
RH
6242static gen_helper_gvec_4 * const sadlp_fns[4] = {
6243 NULL, gen_helper_sve2_sadalp_zpzz_h,
6244 gen_helper_sve2_sadalp_zpzz_s, gen_helper_sve2_sadalp_zpzz_d,
6245};
6246TRANS_FEAT(SADALP_zpzz, aa64_sve2, gen_gvec_ool_arg_zpzz,
6247 sadlp_fns[a->esz], a, 0)
d4b1e59d 6248
5880bdc0
RH
6249static gen_helper_gvec_4 * const uadlp_fns[4] = {
6250 NULL, gen_helper_sve2_uadalp_zpzz_h,
6251 gen_helper_sve2_uadalp_zpzz_s, gen_helper_sve2_uadalp_zpzz_d,
6252};
6253TRANS_FEAT(UADALP_zpzz, aa64_sve2, gen_gvec_ool_arg_zpzz,
6254 uadlp_fns[a->esz], a, 0)
db366da8
RH
6255
6256/*
6257 * SVE2 integer unary operations (predicated)
6258 */
6259
b2c00961
RH
6260TRANS_FEAT(URECPE, aa64_sve2, gen_gvec_ool_arg_zpz,
6261 a->esz == 2 ? gen_helper_sve2_urecpe_s : NULL, a, 0)
db366da8 6262
b2c00961
RH
6263TRANS_FEAT(URSQRTE, aa64_sve2, gen_gvec_ool_arg_zpz,
6264 a->esz == 2 ? gen_helper_sve2_ursqrte_s : NULL, a, 0)
db366da8 6265
b2c00961
RH
6266static gen_helper_gvec_3 * const sqabs_fns[4] = {
6267 gen_helper_sve2_sqabs_b, gen_helper_sve2_sqabs_h,
6268 gen_helper_sve2_sqabs_s, gen_helper_sve2_sqabs_d,
6269};
6270TRANS_FEAT(SQABS, aa64_sve2, gen_gvec_ool_arg_zpz, sqabs_fns[a->esz], a, 0)
db366da8 6271
b2c00961
RH
6272static gen_helper_gvec_3 * const sqneg_fns[4] = {
6273 gen_helper_sve2_sqneg_b, gen_helper_sve2_sqneg_h,
6274 gen_helper_sve2_sqneg_s, gen_helper_sve2_sqneg_d,
6275};
6276TRANS_FEAT(SQNEG, aa64_sve2, gen_gvec_ool_arg_zpz, sqneg_fns[a->esz], a, 0)
45d9503d 6277
5880bdc0
RH
6278DO_ZPZZ(SQSHL, aa64_sve2, sve2_sqshl)
6279DO_ZPZZ(SQRSHL, aa64_sve2, sve2_sqrshl)
6280DO_ZPZZ(SRSHL, aa64_sve2, sve2_srshl)
45d9503d 6281
5880bdc0
RH
6282DO_ZPZZ(UQSHL, aa64_sve2, sve2_uqshl)
6283DO_ZPZZ(UQRSHL, aa64_sve2, sve2_uqrshl)
6284DO_ZPZZ(URSHL, aa64_sve2, sve2_urshl)
a47dc220 6285
5880bdc0
RH
6286DO_ZPZZ(SHADD, aa64_sve2, sve2_shadd)
6287DO_ZPZZ(SRHADD, aa64_sve2, sve2_srhadd)
6288DO_ZPZZ(SHSUB, aa64_sve2, sve2_shsub)
a47dc220 6289
5880bdc0
RH
6290DO_ZPZZ(UHADD, aa64_sve2, sve2_uhadd)
6291DO_ZPZZ(URHADD, aa64_sve2, sve2_urhadd)
6292DO_ZPZZ(UHSUB, aa64_sve2, sve2_uhsub)
8597dc8b 6293
5880bdc0
RH
6294DO_ZPZZ(ADDP, aa64_sve2, sve2_addp)
6295DO_ZPZZ(SMAXP, aa64_sve2, sve2_smaxp)
6296DO_ZPZZ(UMAXP, aa64_sve2, sve2_umaxp)
6297DO_ZPZZ(SMINP, aa64_sve2, sve2_sminp)
6298DO_ZPZZ(UMINP, aa64_sve2, sve2_uminp)
4f07fbeb 6299
5880bdc0
RH
6300DO_ZPZZ(SQADD_zpzz, aa64_sve2, sve2_sqadd)
6301DO_ZPZZ(UQADD_zpzz, aa64_sve2, sve2_uqadd)
6302DO_ZPZZ(SQSUB_zpzz, aa64_sve2, sve2_sqsub)
6303DO_ZPZZ(UQSUB_zpzz, aa64_sve2, sve2_uqsub)
6304DO_ZPZZ(SUQADD, aa64_sve2, sve2_suqadd)
6305DO_ZPZZ(USQADD, aa64_sve2, sve2_usqadd)
0ce1dda8
RH
6306
6307/*
6308 * SVE2 Widening Integer Arithmetic
6309 */
6310
615f19fe
RH
6311static gen_helper_gvec_3 * const saddl_fns[4] = {
6312 NULL, gen_helper_sve2_saddl_h,
6313 gen_helper_sve2_saddl_s, gen_helper_sve2_saddl_d,
6314};
6315TRANS_FEAT(SADDLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6316 saddl_fns[a->esz], a, 0)
6317TRANS_FEAT(SADDLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6318 saddl_fns[a->esz], a, 3)
6319TRANS_FEAT(SADDLBT, aa64_sve2, gen_gvec_ool_arg_zzz,
6320 saddl_fns[a->esz], a, 2)
6321
6322static gen_helper_gvec_3 * const ssubl_fns[4] = {
6323 NULL, gen_helper_sve2_ssubl_h,
6324 gen_helper_sve2_ssubl_s, gen_helper_sve2_ssubl_d,
6325};
6326TRANS_FEAT(SSUBLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6327 ssubl_fns[a->esz], a, 0)
6328TRANS_FEAT(SSUBLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6329 ssubl_fns[a->esz], a, 3)
6330TRANS_FEAT(SSUBLBT, aa64_sve2, gen_gvec_ool_arg_zzz,
6331 ssubl_fns[a->esz], a, 2)
6332TRANS_FEAT(SSUBLTB, aa64_sve2, gen_gvec_ool_arg_zzz,
6333 ssubl_fns[a->esz], a, 1)
6334
6335static gen_helper_gvec_3 * const sabdl_fns[4] = {
6336 NULL, gen_helper_sve2_sabdl_h,
6337 gen_helper_sve2_sabdl_s, gen_helper_sve2_sabdl_d,
6338};
6339TRANS_FEAT(SABDLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6340 sabdl_fns[a->esz], a, 0)
6341TRANS_FEAT(SABDLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6342 sabdl_fns[a->esz], a, 3)
6343
6344static gen_helper_gvec_3 * const uaddl_fns[4] = {
6345 NULL, gen_helper_sve2_uaddl_h,
6346 gen_helper_sve2_uaddl_s, gen_helper_sve2_uaddl_d,
6347};
6348TRANS_FEAT(UADDLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6349 uaddl_fns[a->esz], a, 0)
6350TRANS_FEAT(UADDLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6351 uaddl_fns[a->esz], a, 3)
6352
6353static gen_helper_gvec_3 * const usubl_fns[4] = {
6354 NULL, gen_helper_sve2_usubl_h,
6355 gen_helper_sve2_usubl_s, gen_helper_sve2_usubl_d,
6356};
6357TRANS_FEAT(USUBLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6358 usubl_fns[a->esz], a, 0)
6359TRANS_FEAT(USUBLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6360 usubl_fns[a->esz], a, 3)
6361
6362static gen_helper_gvec_3 * const uabdl_fns[4] = {
6363 NULL, gen_helper_sve2_uabdl_h,
6364 gen_helper_sve2_uabdl_s, gen_helper_sve2_uabdl_d,
6365};
6366TRANS_FEAT(UABDLB, aa64_sve2, gen_gvec_ool_arg_zzz,
6367 uabdl_fns[a->esz], a, 0)
6368TRANS_FEAT(UABDLT, aa64_sve2, gen_gvec_ool_arg_zzz,
6369 uabdl_fns[a->esz], a, 3)
6370
6371static gen_helper_gvec_3 * const sqdmull_fns[4] = {
6372 NULL, gen_helper_sve2_sqdmull_zzz_h,
6373 gen_helper_sve2_sqdmull_zzz_s, gen_helper_sve2_sqdmull_zzz_d,
6374};
6375TRANS_FEAT(SQDMULLB_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6376 sqdmull_fns[a->esz], a, 0)
6377TRANS_FEAT(SQDMULLT_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6378 sqdmull_fns[a->esz], a, 3)
6379
6380static gen_helper_gvec_3 * const smull_fns[4] = {
6381 NULL, gen_helper_sve2_smull_zzz_h,
6382 gen_helper_sve2_smull_zzz_s, gen_helper_sve2_smull_zzz_d,
6383};
6384TRANS_FEAT(SMULLB_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6385 smull_fns[a->esz], a, 0)
6386TRANS_FEAT(SMULLT_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6387 smull_fns[a->esz], a, 3)
6388
6389static gen_helper_gvec_3 * const umull_fns[4] = {
6390 NULL, gen_helper_sve2_umull_zzz_h,
6391 gen_helper_sve2_umull_zzz_s, gen_helper_sve2_umull_zzz_d,
6392};
6393TRANS_FEAT(UMULLB_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6394 umull_fns[a->esz], a, 0)
6395TRANS_FEAT(UMULLT_zzz, aa64_sve2, gen_gvec_ool_arg_zzz,
6396 umull_fns[a->esz], a, 3)
6397
6398static gen_helper_gvec_3 * const eoril_fns[4] = {
6399 gen_helper_sve2_eoril_b, gen_helper_sve2_eoril_h,
6400 gen_helper_sve2_eoril_s, gen_helper_sve2_eoril_d,
6401};
6402TRANS_FEAT(EORBT, aa64_sve2, gen_gvec_ool_arg_zzz, eoril_fns[a->esz], a, 2)
6403TRANS_FEAT(EORTB, aa64_sve2, gen_gvec_ool_arg_zzz, eoril_fns[a->esz], a, 1)
2df3ca55 6404
e3a56131
RH
6405static bool do_trans_pmull(DisasContext *s, arg_rrr_esz *a, bool sel)
6406{
6407 static gen_helper_gvec_3 * const fns[4] = {
6408 gen_helper_gvec_pmull_q, gen_helper_sve2_pmull_h,
6409 NULL, gen_helper_sve2_pmull_d,
6410 };
6411 if (a->esz == 0 && !dc_isar_feature(aa64_sve2_pmull128, s)) {
6412 return false;
6413 }
615f19fe 6414 return gen_gvec_ool_arg_zzz(s, fns[a->esz], a, sel);
e3a56131
RH
6415}
6416
615f19fe
RH
6417TRANS_FEAT(PMULLB, aa64_sve2, do_trans_pmull, a, false)
6418TRANS_FEAT(PMULLT, aa64_sve2, do_trans_pmull, a, true)
e3a56131 6419
615f19fe
RH
6420static gen_helper_gvec_3 * const saddw_fns[4] = {
6421 NULL, gen_helper_sve2_saddw_h,
6422 gen_helper_sve2_saddw_s, gen_helper_sve2_saddw_d,
6423};
6424TRANS_FEAT(SADDWB, aa64_sve2, gen_gvec_ool_arg_zzz, saddw_fns[a->esz], a, 0)
6425TRANS_FEAT(SADDWT, aa64_sve2, gen_gvec_ool_arg_zzz, saddw_fns[a->esz], a, 1)
e3a56131 6426
615f19fe
RH
6427static gen_helper_gvec_3 * const ssubw_fns[4] = {
6428 NULL, gen_helper_sve2_ssubw_h,
6429 gen_helper_sve2_ssubw_s, gen_helper_sve2_ssubw_d,
6430};
6431TRANS_FEAT(SSUBWB, aa64_sve2, gen_gvec_ool_arg_zzz, ssubw_fns[a->esz], a, 0)
6432TRANS_FEAT(SSUBWT, aa64_sve2, gen_gvec_ool_arg_zzz, ssubw_fns[a->esz], a, 1)
81fccf09 6433
615f19fe
RH
6434static gen_helper_gvec_3 * const uaddw_fns[4] = {
6435 NULL, gen_helper_sve2_uaddw_h,
6436 gen_helper_sve2_uaddw_s, gen_helper_sve2_uaddw_d,
6437};
6438TRANS_FEAT(UADDWB, aa64_sve2, gen_gvec_ool_arg_zzz, uaddw_fns[a->esz], a, 0)
6439TRANS_FEAT(UADDWT, aa64_sve2, gen_gvec_ool_arg_zzz, uaddw_fns[a->esz], a, 1)
81fccf09 6440
615f19fe
RH
6441static gen_helper_gvec_3 * const usubw_fns[4] = {
6442 NULL, gen_helper_sve2_usubw_h,
6443 gen_helper_sve2_usubw_s, gen_helper_sve2_usubw_d,
6444};
6445TRANS_FEAT(USUBWB, aa64_sve2, gen_gvec_ool_arg_zzz, usubw_fns[a->esz], a, 0)
6446TRANS_FEAT(USUBWT, aa64_sve2, gen_gvec_ool_arg_zzz, usubw_fns[a->esz], a, 1)
4269fef1
RH
6447
6448static void gen_sshll_vec(unsigned vece, TCGv_vec d, TCGv_vec n, int64_t imm)
6449{
6450 int top = imm & 1;
6451 int shl = imm >> 1;
6452 int halfbits = 4 << vece;
6453
6454 if (top) {
6455 if (shl == halfbits) {
6456 TCGv_vec t = tcg_temp_new_vec_matching(d);
6457 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(halfbits, halfbits));
6458 tcg_gen_and_vec(vece, d, n, t);
6459 tcg_temp_free_vec(t);
6460 } else {
6461 tcg_gen_sari_vec(vece, d, n, halfbits);
6462 tcg_gen_shli_vec(vece, d, d, shl);
6463 }
6464 } else {
6465 tcg_gen_shli_vec(vece, d, n, halfbits);
6466 tcg_gen_sari_vec(vece, d, d, halfbits - shl);
6467 }
6468}
6469
6470static void gen_ushll_i64(unsigned vece, TCGv_i64 d, TCGv_i64 n, int imm)
6471{
6472 int halfbits = 4 << vece;
6473 int top = imm & 1;
6474 int shl = (imm >> 1);
6475 int shift;
6476 uint64_t mask;
6477
6478 mask = MAKE_64BIT_MASK(0, halfbits);
6479 mask <<= shl;
6480 mask = dup_const(vece, mask);
6481
6482 shift = shl - top * halfbits;
6483 if (shift < 0) {
6484 tcg_gen_shri_i64(d, n, -shift);
6485 } else {
6486 tcg_gen_shli_i64(d, n, shift);
6487 }
6488 tcg_gen_andi_i64(d, d, mask);
6489}
6490
6491static void gen_ushll16_i64(TCGv_i64 d, TCGv_i64 n, int64_t imm)
6492{
6493 gen_ushll_i64(MO_16, d, n, imm);
6494}
6495
6496static void gen_ushll32_i64(TCGv_i64 d, TCGv_i64 n, int64_t imm)
6497{
6498 gen_ushll_i64(MO_32, d, n, imm);
6499}
6500
6501static void gen_ushll64_i64(TCGv_i64 d, TCGv_i64 n, int64_t imm)
6502{
6503 gen_ushll_i64(MO_64, d, n, imm);
6504}
6505
6506static void gen_ushll_vec(unsigned vece, TCGv_vec d, TCGv_vec n, int64_t imm)
6507{
6508 int halfbits = 4 << vece;
6509 int top = imm & 1;
6510 int shl = imm >> 1;
6511
6512 if (top) {
6513 if (shl == halfbits) {
6514 TCGv_vec t = tcg_temp_new_vec_matching(d);
6515 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(halfbits, halfbits));
6516 tcg_gen_and_vec(vece, d, n, t);
6517 tcg_temp_free_vec(t);
6518 } else {
6519 tcg_gen_shri_vec(vece, d, n, halfbits);
6520 tcg_gen_shli_vec(vece, d, d, shl);
6521 }
6522 } else {
6523 if (shl == 0) {
6524 TCGv_vec t = tcg_temp_new_vec_matching(d);
6525 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
6526 tcg_gen_and_vec(vece, d, n, t);
6527 tcg_temp_free_vec(t);
6528 } else {
6529 tcg_gen_shli_vec(vece, d, n, halfbits);
6530 tcg_gen_shri_vec(vece, d, d, halfbits - shl);
6531 }
6532 }
6533}
6534
6535static bool do_sve2_shll_tb(DisasContext *s, arg_rri_esz *a,
6536 bool sel, bool uns)
6537{
6538 static const TCGOpcode sshll_list[] = {
6539 INDEX_op_shli_vec, INDEX_op_sari_vec, 0
6540 };
6541 static const TCGOpcode ushll_list[] = {
6542 INDEX_op_shli_vec, INDEX_op_shri_vec, 0
6543 };
6544 static const GVecGen2i ops[2][3] = {
6545 { { .fniv = gen_sshll_vec,
6546 .opt_opc = sshll_list,
6547 .fno = gen_helper_sve2_sshll_h,
6548 .vece = MO_16 },
6549 { .fniv = gen_sshll_vec,
6550 .opt_opc = sshll_list,
6551 .fno = gen_helper_sve2_sshll_s,
6552 .vece = MO_32 },
6553 { .fniv = gen_sshll_vec,
6554 .opt_opc = sshll_list,
6555 .fno = gen_helper_sve2_sshll_d,
6556 .vece = MO_64 } },
6557 { { .fni8 = gen_ushll16_i64,
6558 .fniv = gen_ushll_vec,
6559 .opt_opc = ushll_list,
6560 .fno = gen_helper_sve2_ushll_h,
6561 .vece = MO_16 },
6562 { .fni8 = gen_ushll32_i64,
6563 .fniv = gen_ushll_vec,
6564 .opt_opc = ushll_list,
6565 .fno = gen_helper_sve2_ushll_s,
6566 .vece = MO_32 },
6567 { .fni8 = gen_ushll64_i64,
6568 .fniv = gen_ushll_vec,
6569 .opt_opc = ushll_list,
6570 .fno = gen_helper_sve2_ushll_d,
6571 .vece = MO_64 } },
6572 };
6573
6574 if (a->esz < 0 || a->esz > 2 || !dc_isar_feature(aa64_sve2, s)) {
6575 return false;
6576 }
6577 if (sve_access_check(s)) {
6578 unsigned vsz = vec_full_reg_size(s);
6579 tcg_gen_gvec_2i(vec_full_reg_offset(s, a->rd),
6580 vec_full_reg_offset(s, a->rn),
6581 vsz, vsz, (a->imm << 1) | sel,
6582 &ops[uns][a->esz]);
6583 }
6584 return true;
6585}
6586
6587static bool trans_SSHLLB(DisasContext *s, arg_rri_esz *a)
6588{
6589 return do_sve2_shll_tb(s, a, false, false);
6590}
6591
6592static bool trans_SSHLLT(DisasContext *s, arg_rri_esz *a)
6593{
6594 return do_sve2_shll_tb(s, a, true, false);
6595}
6596
6597static bool trans_USHLLB(DisasContext *s, arg_rri_esz *a)
6598{
6599 return do_sve2_shll_tb(s, a, false, true);
6600}
6601
6602static bool trans_USHLLT(DisasContext *s, arg_rri_esz *a)
6603{
6604 return do_sve2_shll_tb(s, a, true, true);
6605}
cb9c33b8 6606
615f19fe
RH
6607static gen_helper_gvec_3 * const bext_fns[4] = {
6608 gen_helper_sve2_bext_b, gen_helper_sve2_bext_h,
6609 gen_helper_sve2_bext_s, gen_helper_sve2_bext_d,
6610};
6611TRANS_FEAT(BEXT, aa64_sve2_bitperm, gen_gvec_ool_arg_zzz,
6612 bext_fns[a->esz], a, 0)
ed4a6387 6613
615f19fe
RH
6614static gen_helper_gvec_3 * const bdep_fns[4] = {
6615 gen_helper_sve2_bdep_b, gen_helper_sve2_bdep_h,
6616 gen_helper_sve2_bdep_s, gen_helper_sve2_bdep_d,
6617};
6618TRANS_FEAT(BDEP, aa64_sve2_bitperm, gen_gvec_ool_arg_zzz,
6619 bdep_fns[a->esz], a, 0)
ed4a6387 6620
615f19fe
RH
6621static gen_helper_gvec_3 * const bgrp_fns[4] = {
6622 gen_helper_sve2_bgrp_b, gen_helper_sve2_bgrp_h,
6623 gen_helper_sve2_bgrp_s, gen_helper_sve2_bgrp_d,
6624};
6625TRANS_FEAT(BGRP, aa64_sve2_bitperm, gen_gvec_ool_arg_zzz,
6626 bgrp_fns[a->esz], a, 0)
ed4a6387 6627
615f19fe
RH
6628static gen_helper_gvec_3 * const cadd_fns[4] = {
6629 gen_helper_sve2_cadd_b, gen_helper_sve2_cadd_h,
6630 gen_helper_sve2_cadd_s, gen_helper_sve2_cadd_d,
6631};
6632TRANS_FEAT(CADD_rot90, aa64_sve2, gen_gvec_ool_arg_zzz,
6633 cadd_fns[a->esz], a, 0)
6634TRANS_FEAT(CADD_rot270, aa64_sve2, gen_gvec_ool_arg_zzz,
6635 cadd_fns[a->esz], a, 1)
6636
6637static gen_helper_gvec_3 * const sqcadd_fns[4] = {
6638 gen_helper_sve2_sqcadd_b, gen_helper_sve2_sqcadd_h,
6639 gen_helper_sve2_sqcadd_s, gen_helper_sve2_sqcadd_d,
6640};
6641TRANS_FEAT(SQCADD_rot90, aa64_sve2, gen_gvec_ool_arg_zzz,
6642 sqcadd_fns[a->esz], a, 0)
6643TRANS_FEAT(SQCADD_rot270, aa64_sve2, gen_gvec_ool_arg_zzz,
6644 sqcadd_fns[a->esz], a, 1)
38650638 6645
eeb4e84d
RH
6646static gen_helper_gvec_4 * const sabal_fns[4] = {
6647 NULL, gen_helper_sve2_sabal_h,
6648 gen_helper_sve2_sabal_s, gen_helper_sve2_sabal_d,
6649};
6650TRANS_FEAT(SABALB, aa64_sve2, gen_gvec_ool_arg_zzzz, sabal_fns[a->esz], a, 0)
6651TRANS_FEAT(SABALT, aa64_sve2, gen_gvec_ool_arg_zzzz, sabal_fns[a->esz], a, 1)
38650638 6652
eeb4e84d
RH
6653static gen_helper_gvec_4 * const uabal_fns[4] = {
6654 NULL, gen_helper_sve2_uabal_h,
6655 gen_helper_sve2_uabal_s, gen_helper_sve2_uabal_d,
6656};
6657TRANS_FEAT(UABALB, aa64_sve2, gen_gvec_ool_arg_zzzz, uabal_fns[a->esz], a, 0)
6658TRANS_FEAT(UABALT, aa64_sve2, gen_gvec_ool_arg_zzzz, uabal_fns[a->esz], a, 1)
b8295dfb
RH
6659
6660static bool do_adcl(DisasContext *s, arg_rrrr_esz *a, bool sel)
6661{
6662 static gen_helper_gvec_4 * const fns[2] = {
6663 gen_helper_sve2_adcl_s,
6664 gen_helper_sve2_adcl_d,
6665 };
6666 /*
6667 * Note that in this case the ESZ field encodes both size and sign.
6668 * Split out 'subtract' into bit 1 of the data field for the helper.
6669 */
eeb4e84d 6670 return gen_gvec_ool_arg_zzzz(s, fns[a->esz & 1], a, (a->esz & 2) | sel);
b8295dfb
RH
6671}
6672
eeb4e84d
RH
6673TRANS_FEAT(ADCLB, aa64_sve2, do_adcl, a, false)
6674TRANS_FEAT(ADCLT, aa64_sve2, do_adcl, a, true)
a7e3a90e 6675
f2be26a5
RH
6676TRANS_FEAT(SSRA, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_ssra, a)
6677TRANS_FEAT(USRA, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_usra, a)
6678TRANS_FEAT(SRSRA, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_srsra, a)
6679TRANS_FEAT(URSRA, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_ursra, a)
6680TRANS_FEAT(SRI, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_sri, a)
6681TRANS_FEAT(SLI, aa64_sve2, gen_gvec_fn_arg_zzi, gen_gvec_sli, a)
289a1797 6682
79828dcb
RH
6683TRANS_FEAT(SABA, aa64_sve2, gen_gvec_fn_arg_zzz, gen_gvec_saba, a)
6684TRANS_FEAT(UABA, aa64_sve2, gen_gvec_fn_arg_zzz, gen_gvec_uaba, a)
5ff2838d
RH
6685
6686static bool do_sve2_narrow_extract(DisasContext *s, arg_rri_esz *a,
6687 const GVecGen2 ops[3])
6688{
6689 if (a->esz < 0 || a->esz > MO_32 || a->imm != 0 ||
6690 !dc_isar_feature(aa64_sve2, s)) {
6691 return false;
6692 }
6693 if (sve_access_check(s)) {
6694 unsigned vsz = vec_full_reg_size(s);
6695 tcg_gen_gvec_2(vec_full_reg_offset(s, a->rd),
6696 vec_full_reg_offset(s, a->rn),
6697 vsz, vsz, &ops[a->esz]);
6698 }
6699 return true;
6700}
6701
6702static const TCGOpcode sqxtn_list[] = {
6703 INDEX_op_shli_vec, INDEX_op_smin_vec, INDEX_op_smax_vec, 0
6704};
6705
6706static void gen_sqxtnb_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6707{
6708 TCGv_vec t = tcg_temp_new_vec_matching(d);
6709 int halfbits = 4 << vece;
6710 int64_t mask = (1ull << halfbits) - 1;
6711 int64_t min = -1ull << (halfbits - 1);
6712 int64_t max = -min - 1;
6713
6714 tcg_gen_dupi_vec(vece, t, min);
6715 tcg_gen_smax_vec(vece, d, n, t);
6716 tcg_gen_dupi_vec(vece, t, max);
6717 tcg_gen_smin_vec(vece, d, d, t);
6718 tcg_gen_dupi_vec(vece, t, mask);
6719 tcg_gen_and_vec(vece, d, d, t);
6720 tcg_temp_free_vec(t);
6721}
6722
6723static bool trans_SQXTNB(DisasContext *s, arg_rri_esz *a)
6724{
6725 static const GVecGen2 ops[3] = {
6726 { .fniv = gen_sqxtnb_vec,
6727 .opt_opc = sqxtn_list,
6728 .fno = gen_helper_sve2_sqxtnb_h,
6729 .vece = MO_16 },
6730 { .fniv = gen_sqxtnb_vec,
6731 .opt_opc = sqxtn_list,
6732 .fno = gen_helper_sve2_sqxtnb_s,
6733 .vece = MO_32 },
6734 { .fniv = gen_sqxtnb_vec,
6735 .opt_opc = sqxtn_list,
6736 .fno = gen_helper_sve2_sqxtnb_d,
6737 .vece = MO_64 },
6738 };
6739 return do_sve2_narrow_extract(s, a, ops);
6740}
6741
6742static void gen_sqxtnt_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6743{
6744 TCGv_vec t = tcg_temp_new_vec_matching(d);
6745 int halfbits = 4 << vece;
6746 int64_t mask = (1ull << halfbits) - 1;
6747 int64_t min = -1ull << (halfbits - 1);
6748 int64_t max = -min - 1;
6749
6750 tcg_gen_dupi_vec(vece, t, min);
6751 tcg_gen_smax_vec(vece, n, n, t);
6752 tcg_gen_dupi_vec(vece, t, max);
6753 tcg_gen_smin_vec(vece, n, n, t);
6754 tcg_gen_shli_vec(vece, n, n, halfbits);
6755 tcg_gen_dupi_vec(vece, t, mask);
6756 tcg_gen_bitsel_vec(vece, d, t, d, n);
6757 tcg_temp_free_vec(t);
6758}
6759
6760static bool trans_SQXTNT(DisasContext *s, arg_rri_esz *a)
6761{
6762 static const GVecGen2 ops[3] = {
6763 { .fniv = gen_sqxtnt_vec,
6764 .opt_opc = sqxtn_list,
6765 .load_dest = true,
6766 .fno = gen_helper_sve2_sqxtnt_h,
6767 .vece = MO_16 },
6768 { .fniv = gen_sqxtnt_vec,
6769 .opt_opc = sqxtn_list,
6770 .load_dest = true,
6771 .fno = gen_helper_sve2_sqxtnt_s,
6772 .vece = MO_32 },
6773 { .fniv = gen_sqxtnt_vec,
6774 .opt_opc = sqxtn_list,
6775 .load_dest = true,
6776 .fno = gen_helper_sve2_sqxtnt_d,
6777 .vece = MO_64 },
6778 };
6779 return do_sve2_narrow_extract(s, a, ops);
6780}
6781
6782static const TCGOpcode uqxtn_list[] = {
6783 INDEX_op_shli_vec, INDEX_op_umin_vec, 0
6784};
6785
6786static void gen_uqxtnb_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6787{
6788 TCGv_vec t = tcg_temp_new_vec_matching(d);
6789 int halfbits = 4 << vece;
6790 int64_t max = (1ull << halfbits) - 1;
6791
6792 tcg_gen_dupi_vec(vece, t, max);
6793 tcg_gen_umin_vec(vece, d, n, t);
6794 tcg_temp_free_vec(t);
6795}
6796
6797static bool trans_UQXTNB(DisasContext *s, arg_rri_esz *a)
6798{
6799 static const GVecGen2 ops[3] = {
6800 { .fniv = gen_uqxtnb_vec,
6801 .opt_opc = uqxtn_list,
6802 .fno = gen_helper_sve2_uqxtnb_h,
6803 .vece = MO_16 },
6804 { .fniv = gen_uqxtnb_vec,
6805 .opt_opc = uqxtn_list,
6806 .fno = gen_helper_sve2_uqxtnb_s,
6807 .vece = MO_32 },
6808 { .fniv = gen_uqxtnb_vec,
6809 .opt_opc = uqxtn_list,
6810 .fno = gen_helper_sve2_uqxtnb_d,
6811 .vece = MO_64 },
6812 };
6813 return do_sve2_narrow_extract(s, a, ops);
6814}
6815
6816static void gen_uqxtnt_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6817{
6818 TCGv_vec t = tcg_temp_new_vec_matching(d);
6819 int halfbits = 4 << vece;
6820 int64_t max = (1ull << halfbits) - 1;
6821
6822 tcg_gen_dupi_vec(vece, t, max);
6823 tcg_gen_umin_vec(vece, n, n, t);
6824 tcg_gen_shli_vec(vece, n, n, halfbits);
6825 tcg_gen_bitsel_vec(vece, d, t, d, n);
6826 tcg_temp_free_vec(t);
6827}
6828
6829static bool trans_UQXTNT(DisasContext *s, arg_rri_esz *a)
6830{
6831 static const GVecGen2 ops[3] = {
6832 { .fniv = gen_uqxtnt_vec,
6833 .opt_opc = uqxtn_list,
6834 .load_dest = true,
6835 .fno = gen_helper_sve2_uqxtnt_h,
6836 .vece = MO_16 },
6837 { .fniv = gen_uqxtnt_vec,
6838 .opt_opc = uqxtn_list,
6839 .load_dest = true,
6840 .fno = gen_helper_sve2_uqxtnt_s,
6841 .vece = MO_32 },
6842 { .fniv = gen_uqxtnt_vec,
6843 .opt_opc = uqxtn_list,
6844 .load_dest = true,
6845 .fno = gen_helper_sve2_uqxtnt_d,
6846 .vece = MO_64 },
6847 };
6848 return do_sve2_narrow_extract(s, a, ops);
6849}
6850
6851static const TCGOpcode sqxtun_list[] = {
6852 INDEX_op_shli_vec, INDEX_op_umin_vec, INDEX_op_smax_vec, 0
6853};
6854
6855static void gen_sqxtunb_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6856{
6857 TCGv_vec t = tcg_temp_new_vec_matching(d);
6858 int halfbits = 4 << vece;
6859 int64_t max = (1ull << halfbits) - 1;
6860
6861 tcg_gen_dupi_vec(vece, t, 0);
6862 tcg_gen_smax_vec(vece, d, n, t);
6863 tcg_gen_dupi_vec(vece, t, max);
6864 tcg_gen_umin_vec(vece, d, d, t);
6865 tcg_temp_free_vec(t);
6866}
6867
6868static bool trans_SQXTUNB(DisasContext *s, arg_rri_esz *a)
6869{
6870 static const GVecGen2 ops[3] = {
6871 { .fniv = gen_sqxtunb_vec,
6872 .opt_opc = sqxtun_list,
6873 .fno = gen_helper_sve2_sqxtunb_h,
6874 .vece = MO_16 },
6875 { .fniv = gen_sqxtunb_vec,
6876 .opt_opc = sqxtun_list,
6877 .fno = gen_helper_sve2_sqxtunb_s,
6878 .vece = MO_32 },
6879 { .fniv = gen_sqxtunb_vec,
6880 .opt_opc = sqxtun_list,
6881 .fno = gen_helper_sve2_sqxtunb_d,
6882 .vece = MO_64 },
6883 };
6884 return do_sve2_narrow_extract(s, a, ops);
6885}
6886
6887static void gen_sqxtunt_vec(unsigned vece, TCGv_vec d, TCGv_vec n)
6888{
6889 TCGv_vec t = tcg_temp_new_vec_matching(d);
6890 int halfbits = 4 << vece;
6891 int64_t max = (1ull << halfbits) - 1;
6892
6893 tcg_gen_dupi_vec(vece, t, 0);
6894 tcg_gen_smax_vec(vece, n, n, t);
6895 tcg_gen_dupi_vec(vece, t, max);
6896 tcg_gen_umin_vec(vece, n, n, t);
6897 tcg_gen_shli_vec(vece, n, n, halfbits);
6898 tcg_gen_bitsel_vec(vece, d, t, d, n);
6899 tcg_temp_free_vec(t);
6900}
6901
6902static bool trans_SQXTUNT(DisasContext *s, arg_rri_esz *a)
6903{
6904 static const GVecGen2 ops[3] = {
6905 { .fniv = gen_sqxtunt_vec,
6906 .opt_opc = sqxtun_list,
6907 .load_dest = true,
6908 .fno = gen_helper_sve2_sqxtunt_h,
6909 .vece = MO_16 },
6910 { .fniv = gen_sqxtunt_vec,
6911 .opt_opc = sqxtun_list,
6912 .load_dest = true,
6913 .fno = gen_helper_sve2_sqxtunt_s,
6914 .vece = MO_32 },
6915 { .fniv = gen_sqxtunt_vec,
6916 .opt_opc = sqxtun_list,
6917 .load_dest = true,
6918 .fno = gen_helper_sve2_sqxtunt_d,
6919 .vece = MO_64 },
6920 };
6921 return do_sve2_narrow_extract(s, a, ops);
46d111b2
RH
6922}
6923
6924static bool do_sve2_shr_narrow(DisasContext *s, arg_rri_esz *a,
6925 const GVecGen2i ops[3])
6926{
6927 if (a->esz < 0 || a->esz > MO_32 || !dc_isar_feature(aa64_sve2, s)) {
6928 return false;
6929 }
6930 assert(a->imm > 0 && a->imm <= (8 << a->esz));
6931 if (sve_access_check(s)) {
6932 unsigned vsz = vec_full_reg_size(s);
6933 tcg_gen_gvec_2i(vec_full_reg_offset(s, a->rd),
6934 vec_full_reg_offset(s, a->rn),
6935 vsz, vsz, a->imm, &ops[a->esz]);
6936 }
6937 return true;
6938}
6939
6940static void gen_shrnb_i64(unsigned vece, TCGv_i64 d, TCGv_i64 n, int shr)
6941{
6942 int halfbits = 4 << vece;
6943 uint64_t mask = dup_const(vece, MAKE_64BIT_MASK(0, halfbits));
6944
6945 tcg_gen_shri_i64(d, n, shr);
6946 tcg_gen_andi_i64(d, d, mask);
6947}
6948
6949static void gen_shrnb16_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
6950{
6951 gen_shrnb_i64(MO_16, d, n, shr);
6952}
6953
6954static void gen_shrnb32_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
6955{
6956 gen_shrnb_i64(MO_32, d, n, shr);
6957}
6958
6959static void gen_shrnb64_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
6960{
6961 gen_shrnb_i64(MO_64, d, n, shr);
6962}
6963
6964static void gen_shrnb_vec(unsigned vece, TCGv_vec d, TCGv_vec n, int64_t shr)
6965{
6966 TCGv_vec t = tcg_temp_new_vec_matching(d);
6967 int halfbits = 4 << vece;
6968 uint64_t mask = MAKE_64BIT_MASK(0, halfbits);
6969
6970 tcg_gen_shri_vec(vece, n, n, shr);
6971 tcg_gen_dupi_vec(vece, t, mask);
6972 tcg_gen_and_vec(vece, d, n, t);
6973 tcg_temp_free_vec(t);
6974}
6975
6976static bool trans_SHRNB(DisasContext *s, arg_rri_esz *a)
6977{
6978 static const TCGOpcode vec_list[] = { INDEX_op_shri_vec, 0 };
6979 static const GVecGen2i ops[3] = {
6980 { .fni8 = gen_shrnb16_i64,
6981 .fniv = gen_shrnb_vec,
6982 .opt_opc = vec_list,
6983 .fno = gen_helper_sve2_shrnb_h,
6984 .vece = MO_16 },
6985 { .fni8 = gen_shrnb32_i64,
6986 .fniv = gen_shrnb_vec,
6987 .opt_opc = vec_list,
6988 .fno = gen_helper_sve2_shrnb_s,
6989 .vece = MO_32 },
6990 { .fni8 = gen_shrnb64_i64,
6991 .fniv = gen_shrnb_vec,
6992 .opt_opc = vec_list,
6993 .fno = gen_helper_sve2_shrnb_d,
6994 .vece = MO_64 },
6995 };
6996 return do_sve2_shr_narrow(s, a, ops);
6997}
6998
6999static void gen_shrnt_i64(unsigned vece, TCGv_i64 d, TCGv_i64 n, int shr)
7000{
7001 int halfbits = 4 << vece;
7002 uint64_t mask = dup_const(vece, MAKE_64BIT_MASK(0, halfbits));
7003
7004 tcg_gen_shli_i64(n, n, halfbits - shr);
7005 tcg_gen_andi_i64(n, n, ~mask);
7006 tcg_gen_andi_i64(d, d, mask);
7007 tcg_gen_or_i64(d, d, n);
7008}
7009
7010static void gen_shrnt16_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
7011{
7012 gen_shrnt_i64(MO_16, d, n, shr);
7013}
7014
7015static void gen_shrnt32_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
7016{
7017 gen_shrnt_i64(MO_32, d, n, shr);
7018}
7019
7020static void gen_shrnt64_i64(TCGv_i64 d, TCGv_i64 n, int64_t shr)
7021{
7022 tcg_gen_shri_i64(n, n, shr);
7023 tcg_gen_deposit_i64(d, d, n, 32, 32);
7024}
7025
7026static void gen_shrnt_vec(unsigned vece, TCGv_vec d, TCGv_vec n, int64_t shr)
7027{
7028 TCGv_vec t = tcg_temp_new_vec_matching(d);
7029 int halfbits = 4 << vece;
7030 uint64_t mask = MAKE_64BIT_MASK(0, halfbits);
7031
7032 tcg_gen_shli_vec(vece, n, n, halfbits - shr);
7033 tcg_gen_dupi_vec(vece, t, mask);
7034 tcg_gen_bitsel_vec(vece, d, t, d, n);
7035 tcg_temp_free_vec(t);
7036}
7037
7038static bool trans_SHRNT(DisasContext *s, arg_rri_esz *a)
7039{
7040 static const TCGOpcode vec_list[] = { INDEX_op_shli_vec, 0 };
7041 static const GVecGen2i ops[3] = {
7042 { .fni8 = gen_shrnt16_i64,
7043 .fniv = gen_shrnt_vec,
7044 .opt_opc = vec_list,
7045 .load_dest = true,
7046 .fno = gen_helper_sve2_shrnt_h,
7047 .vece = MO_16 },
7048 { .fni8 = gen_shrnt32_i64,
7049 .fniv = gen_shrnt_vec,
7050 .opt_opc = vec_list,
7051 .load_dest = true,
7052 .fno = gen_helper_sve2_shrnt_s,
7053 .vece = MO_32 },
7054 { .fni8 = gen_shrnt64_i64,
7055 .fniv = gen_shrnt_vec,
7056 .opt_opc = vec_list,
7057 .load_dest = true,
7058 .fno = gen_helper_sve2_shrnt_d,
7059 .vece = MO_64 },
7060 };
7061 return do_sve2_shr_narrow(s, a, ops);
7062}
7063
7064static bool trans_RSHRNB(DisasContext *s, arg_rri_esz *a)
7065{
7066 static const GVecGen2i ops[3] = {
7067 { .fno = gen_helper_sve2_rshrnb_h },
7068 { .fno = gen_helper_sve2_rshrnb_s },
7069 { .fno = gen_helper_sve2_rshrnb_d },
7070 };
7071 return do_sve2_shr_narrow(s, a, ops);
7072}
7073
7074static bool trans_RSHRNT(DisasContext *s, arg_rri_esz *a)
7075{
7076 static const GVecGen2i ops[3] = {
7077 { .fno = gen_helper_sve2_rshrnt_h },
7078 { .fno = gen_helper_sve2_rshrnt_s },
7079 { .fno = gen_helper_sve2_rshrnt_d },
7080 };
7081 return do_sve2_shr_narrow(s, a, ops);
81fd3e6e
RH
7082}
7083
7084static void gen_sqshrunb_vec(unsigned vece, TCGv_vec d,
7085 TCGv_vec n, int64_t shr)
7086{
7087 TCGv_vec t = tcg_temp_new_vec_matching(d);
7088 int halfbits = 4 << vece;
7089
7090 tcg_gen_sari_vec(vece, n, n, shr);
7091 tcg_gen_dupi_vec(vece, t, 0);
7092 tcg_gen_smax_vec(vece, n, n, t);
7093 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7094 tcg_gen_umin_vec(vece, d, n, t);
7095 tcg_temp_free_vec(t);
7096}
7097
7098static bool trans_SQSHRUNB(DisasContext *s, arg_rri_esz *a)
7099{
7100 static const TCGOpcode vec_list[] = {
7101 INDEX_op_sari_vec, INDEX_op_smax_vec, INDEX_op_umin_vec, 0
7102 };
7103 static const GVecGen2i ops[3] = {
7104 { .fniv = gen_sqshrunb_vec,
7105 .opt_opc = vec_list,
7106 .fno = gen_helper_sve2_sqshrunb_h,
7107 .vece = MO_16 },
7108 { .fniv = gen_sqshrunb_vec,
7109 .opt_opc = vec_list,
7110 .fno = gen_helper_sve2_sqshrunb_s,
7111 .vece = MO_32 },
7112 { .fniv = gen_sqshrunb_vec,
7113 .opt_opc = vec_list,
7114 .fno = gen_helper_sve2_sqshrunb_d,
7115 .vece = MO_64 },
7116 };
7117 return do_sve2_shr_narrow(s, a, ops);
7118}
7119
7120static void gen_sqshrunt_vec(unsigned vece, TCGv_vec d,
7121 TCGv_vec n, int64_t shr)
7122{
7123 TCGv_vec t = tcg_temp_new_vec_matching(d);
7124 int halfbits = 4 << vece;
7125
7126 tcg_gen_sari_vec(vece, n, n, shr);
7127 tcg_gen_dupi_vec(vece, t, 0);
7128 tcg_gen_smax_vec(vece, n, n, t);
7129 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7130 tcg_gen_umin_vec(vece, n, n, t);
7131 tcg_gen_shli_vec(vece, n, n, halfbits);
7132 tcg_gen_bitsel_vec(vece, d, t, d, n);
7133 tcg_temp_free_vec(t);
7134}
7135
7136static bool trans_SQSHRUNT(DisasContext *s, arg_rri_esz *a)
7137{
7138 static const TCGOpcode vec_list[] = {
7139 INDEX_op_shli_vec, INDEX_op_sari_vec,
7140 INDEX_op_smax_vec, INDEX_op_umin_vec, 0
7141 };
7142 static const GVecGen2i ops[3] = {
7143 { .fniv = gen_sqshrunt_vec,
7144 .opt_opc = vec_list,
7145 .load_dest = true,
7146 .fno = gen_helper_sve2_sqshrunt_h,
7147 .vece = MO_16 },
7148 { .fniv = gen_sqshrunt_vec,
7149 .opt_opc = vec_list,
7150 .load_dest = true,
7151 .fno = gen_helper_sve2_sqshrunt_s,
7152 .vece = MO_32 },
7153 { .fniv = gen_sqshrunt_vec,
7154 .opt_opc = vec_list,
7155 .load_dest = true,
7156 .fno = gen_helper_sve2_sqshrunt_d,
7157 .vece = MO_64 },
7158 };
7159 return do_sve2_shr_narrow(s, a, ops);
7160}
7161
7162static bool trans_SQRSHRUNB(DisasContext *s, arg_rri_esz *a)
7163{
7164 static const GVecGen2i ops[3] = {
7165 { .fno = gen_helper_sve2_sqrshrunb_h },
7166 { .fno = gen_helper_sve2_sqrshrunb_s },
7167 { .fno = gen_helper_sve2_sqrshrunb_d },
7168 };
7169 return do_sve2_shr_narrow(s, a, ops);
7170}
7171
7172static bool trans_SQRSHRUNT(DisasContext *s, arg_rri_esz *a)
7173{
7174 static const GVecGen2i ops[3] = {
7175 { .fno = gen_helper_sve2_sqrshrunt_h },
7176 { .fno = gen_helper_sve2_sqrshrunt_s },
7177 { .fno = gen_helper_sve2_sqrshrunt_d },
7178 };
7179 return do_sve2_shr_narrow(s, a, ops);
c13418da
RH
7180}
7181
743bb147
RH
7182static void gen_sqshrnb_vec(unsigned vece, TCGv_vec d,
7183 TCGv_vec n, int64_t shr)
7184{
7185 TCGv_vec t = tcg_temp_new_vec_matching(d);
7186 int halfbits = 4 << vece;
7187 int64_t max = MAKE_64BIT_MASK(0, halfbits - 1);
7188 int64_t min = -max - 1;
7189
7190 tcg_gen_sari_vec(vece, n, n, shr);
7191 tcg_gen_dupi_vec(vece, t, min);
7192 tcg_gen_smax_vec(vece, n, n, t);
7193 tcg_gen_dupi_vec(vece, t, max);
7194 tcg_gen_smin_vec(vece, n, n, t);
7195 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7196 tcg_gen_and_vec(vece, d, n, t);
7197 tcg_temp_free_vec(t);
7198}
7199
7200static bool trans_SQSHRNB(DisasContext *s, arg_rri_esz *a)
7201{
7202 static const TCGOpcode vec_list[] = {
7203 INDEX_op_sari_vec, INDEX_op_smax_vec, INDEX_op_smin_vec, 0
7204 };
7205 static const GVecGen2i ops[3] = {
7206 { .fniv = gen_sqshrnb_vec,
7207 .opt_opc = vec_list,
7208 .fno = gen_helper_sve2_sqshrnb_h,
7209 .vece = MO_16 },
7210 { .fniv = gen_sqshrnb_vec,
7211 .opt_opc = vec_list,
7212 .fno = gen_helper_sve2_sqshrnb_s,
7213 .vece = MO_32 },
7214 { .fniv = gen_sqshrnb_vec,
7215 .opt_opc = vec_list,
7216 .fno = gen_helper_sve2_sqshrnb_d,
7217 .vece = MO_64 },
7218 };
7219 return do_sve2_shr_narrow(s, a, ops);
7220}
7221
7222static void gen_sqshrnt_vec(unsigned vece, TCGv_vec d,
7223 TCGv_vec n, int64_t shr)
7224{
7225 TCGv_vec t = tcg_temp_new_vec_matching(d);
7226 int halfbits = 4 << vece;
7227 int64_t max = MAKE_64BIT_MASK(0, halfbits - 1);
7228 int64_t min = -max - 1;
7229
7230 tcg_gen_sari_vec(vece, n, n, shr);
7231 tcg_gen_dupi_vec(vece, t, min);
7232 tcg_gen_smax_vec(vece, n, n, t);
7233 tcg_gen_dupi_vec(vece, t, max);
7234 tcg_gen_smin_vec(vece, n, n, t);
7235 tcg_gen_shli_vec(vece, n, n, halfbits);
7236 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7237 tcg_gen_bitsel_vec(vece, d, t, d, n);
7238 tcg_temp_free_vec(t);
7239}
7240
7241static bool trans_SQSHRNT(DisasContext *s, arg_rri_esz *a)
7242{
7243 static const TCGOpcode vec_list[] = {
7244 INDEX_op_shli_vec, INDEX_op_sari_vec,
7245 INDEX_op_smax_vec, INDEX_op_smin_vec, 0
7246 };
7247 static const GVecGen2i ops[3] = {
7248 { .fniv = gen_sqshrnt_vec,
7249 .opt_opc = vec_list,
7250 .load_dest = true,
7251 .fno = gen_helper_sve2_sqshrnt_h,
7252 .vece = MO_16 },
7253 { .fniv = gen_sqshrnt_vec,
7254 .opt_opc = vec_list,
7255 .load_dest = true,
7256 .fno = gen_helper_sve2_sqshrnt_s,
7257 .vece = MO_32 },
7258 { .fniv = gen_sqshrnt_vec,
7259 .opt_opc = vec_list,
7260 .load_dest = true,
7261 .fno = gen_helper_sve2_sqshrnt_d,
7262 .vece = MO_64 },
7263 };
7264 return do_sve2_shr_narrow(s, a, ops);
7265}
7266
7267static bool trans_SQRSHRNB(DisasContext *s, arg_rri_esz *a)
7268{
7269 static const GVecGen2i ops[3] = {
7270 { .fno = gen_helper_sve2_sqrshrnb_h },
7271 { .fno = gen_helper_sve2_sqrshrnb_s },
7272 { .fno = gen_helper_sve2_sqrshrnb_d },
7273 };
7274 return do_sve2_shr_narrow(s, a, ops);
7275}
7276
7277static bool trans_SQRSHRNT(DisasContext *s, arg_rri_esz *a)
7278{
7279 static const GVecGen2i ops[3] = {
7280 { .fno = gen_helper_sve2_sqrshrnt_h },
7281 { .fno = gen_helper_sve2_sqrshrnt_s },
7282 { .fno = gen_helper_sve2_sqrshrnt_d },
7283 };
7284 return do_sve2_shr_narrow(s, a, ops);
7285}
7286
c13418da
RH
7287static void gen_uqshrnb_vec(unsigned vece, TCGv_vec d,
7288 TCGv_vec n, int64_t shr)
7289{
7290 TCGv_vec t = tcg_temp_new_vec_matching(d);
7291 int halfbits = 4 << vece;
7292
7293 tcg_gen_shri_vec(vece, n, n, shr);
7294 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7295 tcg_gen_umin_vec(vece, d, n, t);
7296 tcg_temp_free_vec(t);
7297}
7298
7299static bool trans_UQSHRNB(DisasContext *s, arg_rri_esz *a)
7300{
7301 static const TCGOpcode vec_list[] = {
7302 INDEX_op_shri_vec, INDEX_op_umin_vec, 0
7303 };
7304 static const GVecGen2i ops[3] = {
7305 { .fniv = gen_uqshrnb_vec,
7306 .opt_opc = vec_list,
7307 .fno = gen_helper_sve2_uqshrnb_h,
7308 .vece = MO_16 },
7309 { .fniv = gen_uqshrnb_vec,
7310 .opt_opc = vec_list,
7311 .fno = gen_helper_sve2_uqshrnb_s,
7312 .vece = MO_32 },
7313 { .fniv = gen_uqshrnb_vec,
7314 .opt_opc = vec_list,
7315 .fno = gen_helper_sve2_uqshrnb_d,
7316 .vece = MO_64 },
7317 };
7318 return do_sve2_shr_narrow(s, a, ops);
7319}
7320
7321static void gen_uqshrnt_vec(unsigned vece, TCGv_vec d,
7322 TCGv_vec n, int64_t shr)
7323{
7324 TCGv_vec t = tcg_temp_new_vec_matching(d);
7325 int halfbits = 4 << vece;
7326
7327 tcg_gen_shri_vec(vece, n, n, shr);
7328 tcg_gen_dupi_vec(vece, t, MAKE_64BIT_MASK(0, halfbits));
7329 tcg_gen_umin_vec(vece, n, n, t);
7330 tcg_gen_shli_vec(vece, n, n, halfbits);
7331 tcg_gen_bitsel_vec(vece, d, t, d, n);
7332 tcg_temp_free_vec(t);
7333}
7334
7335static bool trans_UQSHRNT(DisasContext *s, arg_rri_esz *a)
7336{
7337 static const TCGOpcode vec_list[] = {
7338 INDEX_op_shli_vec, INDEX_op_shri_vec, INDEX_op_umin_vec, 0
7339 };
7340 static const GVecGen2i ops[3] = {
7341 { .fniv = gen_uqshrnt_vec,
7342 .opt_opc = vec_list,
7343 .load_dest = true,
7344 .fno = gen_helper_sve2_uqshrnt_h,
7345 .vece = MO_16 },
7346 { .fniv = gen_uqshrnt_vec,
7347 .opt_opc = vec_list,
7348 .load_dest = true,
7349 .fno = gen_helper_sve2_uqshrnt_s,
7350 .vece = MO_32 },
7351 { .fniv = gen_uqshrnt_vec,
7352 .opt_opc = vec_list,
7353 .load_dest = true,
7354 .fno = gen_helper_sve2_uqshrnt_d,
7355 .vece = MO_64 },
7356 };
7357 return do_sve2_shr_narrow(s, a, ops);
7358}
7359
7360static bool trans_UQRSHRNB(DisasContext *s, arg_rri_esz *a)
7361{
7362 static const GVecGen2i ops[3] = {
7363 { .fno = gen_helper_sve2_uqrshrnb_h },
7364 { .fno = gen_helper_sve2_uqrshrnb_s },
7365 { .fno = gen_helper_sve2_uqrshrnb_d },
7366 };
7367 return do_sve2_shr_narrow(s, a, ops);
7368}
7369
7370static bool trans_UQRSHRNT(DisasContext *s, arg_rri_esz *a)
7371{
7372 static const GVecGen2i ops[3] = {
7373 { .fno = gen_helper_sve2_uqrshrnt_h },
7374 { .fno = gen_helper_sve2_uqrshrnt_s },
7375 { .fno = gen_helper_sve2_uqrshrnt_d },
7376 };
7377 return do_sve2_shr_narrow(s, a, ops);
5ff2838d 7378}
b87dbeeb 7379
40d5ea50 7380#define DO_SVE2_ZZZ_NARROW(NAME, name) \
bd394cf5 7381 static gen_helper_gvec_3 * const name##_fns[4] = { \
40d5ea50
SL
7382 NULL, gen_helper_sve2_##name##_h, \
7383 gen_helper_sve2_##name##_s, gen_helper_sve2_##name##_d, \
7384 }; \
bd394cf5
RH
7385 TRANS_FEAT(NAME, aa64_sve2, gen_gvec_ool_arg_zzz, \
7386 name##_fns[a->esz], a, 0)
40d5ea50
SL
7387
7388DO_SVE2_ZZZ_NARROW(ADDHNB, addhnb)
7389DO_SVE2_ZZZ_NARROW(ADDHNT, addhnt)
0ea3ff02
SL
7390DO_SVE2_ZZZ_NARROW(RADDHNB, raddhnb)
7391DO_SVE2_ZZZ_NARROW(RADDHNT, raddhnt)
40d5ea50 7392
c3cd6766
SL
7393DO_SVE2_ZZZ_NARROW(SUBHNB, subhnb)
7394DO_SVE2_ZZZ_NARROW(SUBHNT, subhnt)
e9443d10
SL
7395DO_SVE2_ZZZ_NARROW(RSUBHNB, rsubhnb)
7396DO_SVE2_ZZZ_NARROW(RSUBHNT, rsubhnt)
c3cd6766 7397
e0ae6ec3
SL
7398static bool do_sve2_ppzz_flags(DisasContext *s, arg_rprr_esz *a,
7399 gen_helper_gvec_flags_4 *fn)
7400{
7401 if (!dc_isar_feature(aa64_sve2, s)) {
7402 return false;
7403 }
7404 return do_ppzz_flags(s, a, fn);
7405}
7406
7407#define DO_SVE2_PPZZ_MATCH(NAME, name) \
7408static bool trans_##NAME(DisasContext *s, arg_rprr_esz *a) \
7409{ \
7410 static gen_helper_gvec_flags_4 * const fns[4] = { \
7411 gen_helper_sve2_##name##_ppzz_b, gen_helper_sve2_##name##_ppzz_h, \
7412 NULL, NULL \
7413 }; \
7414 return do_sve2_ppzz_flags(s, a, fns[a->esz]); \
7415}
7416
7417DO_SVE2_PPZZ_MATCH(MATCH, match)
7418DO_SVE2_PPZZ_MATCH(NMATCH, nmatch)
7419
5880bdc0
RH
7420static gen_helper_gvec_4 * const histcnt_fns[4] = {
7421 NULL, NULL, gen_helper_sve2_histcnt_s, gen_helper_sve2_histcnt_d
7422};
7423TRANS_FEAT(HISTCNT, aa64_sve2, gen_gvec_ool_arg_zpzz,
7424 histcnt_fns[a->esz], a, 0)
7d47ac94 7425
bd394cf5
RH
7426TRANS_FEAT(HISTSEG, aa64_sve2, gen_gvec_ool_arg_zzz,
7427 a->esz == 0 ? gen_helper_sve2_histseg : NULL, a, 0)
7d47ac94 7428
b87dbeeb
SL
7429static bool do_sve2_zpzz_fp(DisasContext *s, arg_rprr_esz *a,
7430 gen_helper_gvec_4_ptr *fn)
7431{
7432 if (!dc_isar_feature(aa64_sve2, s)) {
7433 return false;
7434 }
7435 return do_zpzz_fp(s, a, fn);
7436}
7437
7438#define DO_SVE2_ZPZZ_FP(NAME, name) \
7439static bool trans_##NAME(DisasContext *s, arg_rprr_esz *a) \
7440{ \
7441 static gen_helper_gvec_4_ptr * const fns[4] = { \
7442 NULL, gen_helper_sve2_##name##_zpzz_h, \
7443 gen_helper_sve2_##name##_zpzz_s, gen_helper_sve2_##name##_zpzz_d \
7444 }; \
7445 return do_sve2_zpzz_fp(s, a, fns[a->esz]); \
7446}
7447
7448DO_SVE2_ZPZZ_FP(FADDP, faddp)
7449DO_SVE2_ZPZZ_FP(FMAXNMP, fmaxnmp)
7450DO_SVE2_ZPZZ_FP(FMINNMP, fminnmp)
7451DO_SVE2_ZPZZ_FP(FMAXP, fmaxp)
7452DO_SVE2_ZPZZ_FP(FMINP, fminp)
bfc9307e
RH
7453
7454/*
7455 * SVE Integer Multiply-Add (unpredicated)
7456 */
7457
4f26756b
SL
7458static bool trans_FMMLA(DisasContext *s, arg_rrrr_esz *a)
7459{
7460 gen_helper_gvec_4_ptr *fn;
7461
7462 switch (a->esz) {
7463 case MO_32:
7464 if (!dc_isar_feature(aa64_sve_f32mm, s)) {
7465 return false;
7466 }
7467 fn = gen_helper_fmmla_s;
7468 break;
7469 case MO_64:
7470 if (!dc_isar_feature(aa64_sve_f64mm, s)) {
7471 return false;
7472 }
7473 fn = gen_helper_fmmla_d;
7474 break;
7475 default:
7476 return false;
7477 }
7478
7479 if (sve_access_check(s)) {
7480 unsigned vsz = vec_full_reg_size(s);
7481 TCGv_ptr status = fpstatus_ptr(FPST_FPCR);
7482 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
7483 vec_full_reg_offset(s, a->rn),
7484 vec_full_reg_offset(s, a->rm),
7485 vec_full_reg_offset(s, a->ra),
7486 status, vsz, vsz, 0, fn);
7487 tcg_temp_free_ptr(status);
7488 }
7489 return true;
7490}
7491
eeb4e84d
RH
7492static gen_helper_gvec_4 * const sqdmlal_zzzw_fns[] = {
7493 NULL, gen_helper_sve2_sqdmlal_zzzw_h,
7494 gen_helper_sve2_sqdmlal_zzzw_s, gen_helper_sve2_sqdmlal_zzzw_d,
7495};
7496TRANS_FEAT(SQDMLALB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7497 sqdmlal_zzzw_fns[a->esz], a, 0)
7498TRANS_FEAT(SQDMLALT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7499 sqdmlal_zzzw_fns[a->esz], a, 3)
7500TRANS_FEAT(SQDMLALBT, aa64_sve2, gen_gvec_ool_arg_zzzz,
7501 sqdmlal_zzzw_fns[a->esz], a, 2)
7502
7503static gen_helper_gvec_4 * const sqdmlsl_zzzw_fns[] = {
7504 NULL, gen_helper_sve2_sqdmlsl_zzzw_h,
7505 gen_helper_sve2_sqdmlsl_zzzw_s, gen_helper_sve2_sqdmlsl_zzzw_d,
7506};
7507TRANS_FEAT(SQDMLSLB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7508 sqdmlsl_zzzw_fns[a->esz], a, 0)
7509TRANS_FEAT(SQDMLSLT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7510 sqdmlsl_zzzw_fns[a->esz], a, 3)
7511TRANS_FEAT(SQDMLSLBT, aa64_sve2, gen_gvec_ool_arg_zzzz,
7512 sqdmlsl_zzzw_fns[a->esz], a, 2)
7513
7514static gen_helper_gvec_4 * const sqrdmlah_fns[] = {
7515 gen_helper_sve2_sqrdmlah_b, gen_helper_sve2_sqrdmlah_h,
7516 gen_helper_sve2_sqrdmlah_s, gen_helper_sve2_sqrdmlah_d,
7517};
7518TRANS_FEAT(SQRDMLAH_zzzz, aa64_sve2, gen_gvec_ool_arg_zzzz,
7519 sqrdmlah_fns[a->esz], a, 0)
45a32e80 7520
eeb4e84d
RH
7521static gen_helper_gvec_4 * const sqrdmlsh_fns[] = {
7522 gen_helper_sve2_sqrdmlsh_b, gen_helper_sve2_sqrdmlsh_h,
7523 gen_helper_sve2_sqrdmlsh_s, gen_helper_sve2_sqrdmlsh_d,
7524};
7525TRANS_FEAT(SQRDMLSH_zzzz, aa64_sve2, gen_gvec_ool_arg_zzzz,
7526 sqrdmlsh_fns[a->esz], a, 0)
45a32e80 7527
eeb4e84d
RH
7528static gen_helper_gvec_4 * const smlal_zzzw_fns[] = {
7529 NULL, gen_helper_sve2_smlal_zzzw_h,
7530 gen_helper_sve2_smlal_zzzw_s, gen_helper_sve2_smlal_zzzw_d,
7531};
7532TRANS_FEAT(SMLALB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7533 smlal_zzzw_fns[a->esz], a, 0)
7534TRANS_FEAT(SMLALT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7535 smlal_zzzw_fns[a->esz], a, 1)
7536
7537static gen_helper_gvec_4 * const umlal_zzzw_fns[] = {
7538 NULL, gen_helper_sve2_umlal_zzzw_h,
7539 gen_helper_sve2_umlal_zzzw_s, gen_helper_sve2_umlal_zzzw_d,
7540};
7541TRANS_FEAT(UMLALB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7542 umlal_zzzw_fns[a->esz], a, 0)
7543TRANS_FEAT(UMLALT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7544 umlal_zzzw_fns[a->esz], a, 1)
7545
7546static gen_helper_gvec_4 * const smlsl_zzzw_fns[] = {
7547 NULL, gen_helper_sve2_smlsl_zzzw_h,
7548 gen_helper_sve2_smlsl_zzzw_s, gen_helper_sve2_smlsl_zzzw_d,
7549};
7550TRANS_FEAT(SMLSLB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7551 smlsl_zzzw_fns[a->esz], a, 0)
7552TRANS_FEAT(SMLSLT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7553 smlsl_zzzw_fns[a->esz], a, 1)
7554
7555static gen_helper_gvec_4 * const umlsl_zzzw_fns[] = {
7556 NULL, gen_helper_sve2_umlsl_zzzw_h,
7557 gen_helper_sve2_umlsl_zzzw_s, gen_helper_sve2_umlsl_zzzw_d,
7558};
7559TRANS_FEAT(UMLSLB_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7560 umlsl_zzzw_fns[a->esz], a, 0)
7561TRANS_FEAT(UMLSLT_zzzw, aa64_sve2, gen_gvec_ool_arg_zzzz,
7562 umlsl_zzzw_fns[a->esz], a, 1)
d782d3ca 7563
5f425b92
RH
7564static gen_helper_gvec_4 * const cmla_fns[] = {
7565 gen_helper_sve2_cmla_zzzz_b, gen_helper_sve2_cmla_zzzz_h,
7566 gen_helper_sve2_cmla_zzzz_s, gen_helper_sve2_cmla_zzzz_d,
7567};
7568TRANS_FEAT(CMLA_zzzz, aa64_sve2, gen_gvec_ool_zzzz,
7569 cmla_fns[a->esz], a->rd, a->rn, a->rm, a->ra, a->rot)
21068f39 7570
5f425b92
RH
7571static gen_helper_gvec_4 * const cdot_fns[] = {
7572 NULL, NULL, gen_helper_sve2_cdot_zzzz_s, gen_helper_sve2_cdot_zzzz_d
7573};
7574TRANS_FEAT(CDOT_zzzz, aa64_sve2, gen_gvec_ool_zzzz,
7575 cdot_fns[a->esz], a->rd, a->rn, a->rm, a->ra, a->rot)
d782d3ca 7576
5f425b92
RH
7577static gen_helper_gvec_4 * const sqrdcmlah_fns[] = {
7578 gen_helper_sve2_sqrdcmlah_zzzz_b, gen_helper_sve2_sqrdcmlah_zzzz_h,
7579 gen_helper_sve2_sqrdcmlah_zzzz_s, gen_helper_sve2_sqrdcmlah_zzzz_d,
7580};
7581TRANS_FEAT(SQRDCMLAH_zzzz, aa64_sve2, gen_gvec_ool_zzzz,
7582 sqrdcmlah_fns[a->esz], a->rd, a->rn, a->rm, a->ra, a->rot)
6a98cb2a 7583
8740d694
RH
7584TRANS_FEAT(USDOT_zzzz, aa64_sve_i8mm, gen_gvec_ool_arg_zzzz,
7585 a->esz == 2 ? gen_helper_gvec_usdot_b : NULL, a, 0)
b2bcd1be 7586
0ea3cdbf
RH
7587TRANS_FEAT(AESMC, aa64_sve2_aes, gen_gvec_ool_zz,
7588 gen_helper_crypto_aesmc, a->rd, a->rd, a->decrypt)
3cc7a88e 7589
32e2ad65
RH
7590TRANS_FEAT(AESE, aa64_sve2_aes, gen_gvec_ool_arg_zzz,
7591 gen_helper_crypto_aese, a, false)
7592TRANS_FEAT(AESD, aa64_sve2_aes, gen_gvec_ool_arg_zzz,
7593 gen_helper_crypto_aese, a, true)
3cc7a88e 7594
32e2ad65
RH
7595TRANS_FEAT(SM4E, aa64_sve2_sm4, gen_gvec_ool_arg_zzz,
7596 gen_helper_crypto_sm4e, a, 0)
7597TRANS_FEAT(SM4EKEY, aa64_sve2_sm4, gen_gvec_ool_arg_zzz,
7598 gen_helper_crypto_sm4ekey, a, 0)
3358eb3f 7599
2aa469ff 7600TRANS_FEAT(RAX1, aa64_sve2_sha3, gen_gvec_fn_arg_zzz, gen_gvec_rax1, a)
5c1b7226
RH
7601
7602static bool trans_FCVTNT_sh(DisasContext *s, arg_rpr_esz *a)
7603{
7604 if (!dc_isar_feature(aa64_sve2, s)) {
7605 return false;
7606 }
7607 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve2_fcvtnt_sh);
7608}
7609
d29b17ca
RH
7610static bool trans_BFCVTNT(DisasContext *s, arg_rpr_esz *a)
7611{
7612 if (!dc_isar_feature(aa64_sve_bf16, s)) {
7613 return false;
7614 }
7615 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve_bfcvtnt);
7616}
7617
5c1b7226
RH
7618static bool trans_FCVTNT_ds(DisasContext *s, arg_rpr_esz *a)
7619{
7620 if (!dc_isar_feature(aa64_sve2, s)) {
7621 return false;
7622 }
7623 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve2_fcvtnt_ds);
7624}
83c2523f
SL
7625
7626static bool trans_FCVTLT_hs(DisasContext *s, arg_rpr_esz *a)
7627{
7628 if (!dc_isar_feature(aa64_sve2, s)) {
7629 return false;
7630 }
7631 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve2_fcvtlt_hs);
7632}
7633
7634static bool trans_FCVTLT_sd(DisasContext *s, arg_rpr_esz *a)
7635{
7636 if (!dc_isar_feature(aa64_sve2, s)) {
7637 return false;
7638 }
7639 return do_zpz_ptr(s, a->rd, a->rn, a->pg, false, gen_helper_sve2_fcvtlt_sd);
7640}
95365277
SL
7641
7642static bool trans_FCVTX_ds(DisasContext *s, arg_rpr_esz *a)
7643{
7644 if (!dc_isar_feature(aa64_sve2, s)) {
7645 return false;
7646 }
7647 return do_frint_mode(s, a, float_round_to_odd, gen_helper_sve_fcvt_ds);
7648}
7649
7650static bool trans_FCVTXNT_ds(DisasContext *s, arg_rpr_esz *a)
7651{
7652 if (!dc_isar_feature(aa64_sve2, s)) {
7653 return false;
7654 }
7655 return do_frint_mode(s, a, float_round_to_odd, gen_helper_sve2_fcvtnt_ds);
7656}
631be02e
SL
7657
7658static bool trans_FLOGB(DisasContext *s, arg_rpr_esz *a)
7659{
7660 static gen_helper_gvec_3_ptr * const fns[] = {
7661 NULL, gen_helper_flogb_h,
7662 gen_helper_flogb_s, gen_helper_flogb_d
7663 };
7664
7665 if (!dc_isar_feature(aa64_sve2, s) || fns[a->esz] == NULL) {
7666 return false;
7667 }
7668 if (sve_access_check(s)) {
7669 TCGv_ptr status =
7670 fpstatus_ptr(a->esz == MO_16 ? FPST_FPCR_F16 : FPST_FPCR);
7671 unsigned vsz = vec_full_reg_size(s);
7672
7673 tcg_gen_gvec_3_ptr(vec_full_reg_offset(s, a->rd),
7674 vec_full_reg_offset(s, a->rn),
7675 pred_full_reg_offset(s, a->pg),
7676 status, vsz, vsz, 0, fns[a->esz]);
7677 tcg_temp_free_ptr(status);
7678 }
7679 return true;
7680}
50d102bd
SL
7681
7682static bool do_FMLAL_zzzw(DisasContext *s, arg_rrrr_esz *a, bool sub, bool sel)
7683{
7684 if (!dc_isar_feature(aa64_sve2, s)) {
7685 return false;
7686 }
7687 if (sve_access_check(s)) {
7688 unsigned vsz = vec_full_reg_size(s);
7689 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
7690 vec_full_reg_offset(s, a->rn),
7691 vec_full_reg_offset(s, a->rm),
7692 vec_full_reg_offset(s, a->ra),
7693 cpu_env, vsz, vsz, (sel << 1) | sub,
7694 gen_helper_sve2_fmlal_zzzw_s);
7695 }
7696 return true;
7697}
7698
7699static bool trans_FMLALB_zzzw(DisasContext *s, arg_rrrr_esz *a)
7700{
7701 return do_FMLAL_zzzw(s, a, false, false);
7702}
7703
7704static bool trans_FMLALT_zzzw(DisasContext *s, arg_rrrr_esz *a)
7705{
7706 return do_FMLAL_zzzw(s, a, false, true);
7707}
7708
7709static bool trans_FMLSLB_zzzw(DisasContext *s, arg_rrrr_esz *a)
7710{
7711 return do_FMLAL_zzzw(s, a, true, false);
7712}
7713
7714static bool trans_FMLSLT_zzzw(DisasContext *s, arg_rrrr_esz *a)
7715{
7716 return do_FMLAL_zzzw(s, a, true, true);
7717}
7718
7719static bool do_FMLAL_zzxw(DisasContext *s, arg_rrxr_esz *a, bool sub, bool sel)
7720{
7721 if (!dc_isar_feature(aa64_sve2, s)) {
7722 return false;
7723 }
7724 if (sve_access_check(s)) {
7725 unsigned vsz = vec_full_reg_size(s);
7726 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
7727 vec_full_reg_offset(s, a->rn),
7728 vec_full_reg_offset(s, a->rm),
7729 vec_full_reg_offset(s, a->ra),
7730 cpu_env, vsz, vsz,
7731 (a->index << 2) | (sel << 1) | sub,
7732 gen_helper_sve2_fmlal_zzxw_s);
7733 }
7734 return true;
7735}
7736
7737static bool trans_FMLALB_zzxw(DisasContext *s, arg_rrxr_esz *a)
7738{
7739 return do_FMLAL_zzxw(s, a, false, false);
7740}
7741
7742static bool trans_FMLALT_zzxw(DisasContext *s, arg_rrxr_esz *a)
7743{
7744 return do_FMLAL_zzxw(s, a, false, true);
7745}
7746
7747static bool trans_FMLSLB_zzxw(DisasContext *s, arg_rrxr_esz *a)
7748{
7749 return do_FMLAL_zzxw(s, a, true, false);
7750}
7751
7752static bool trans_FMLSLT_zzxw(DisasContext *s, arg_rrxr_esz *a)
7753{
7754 return do_FMLAL_zzxw(s, a, true, true);
7755}
2323c5ff 7756
eec05e4e
RH
7757TRANS_FEAT(SMMLA, aa64_sve_i8mm, gen_gvec_ool_arg_zzzz,
7758 gen_helper_gvec_smmla_b, a, 0)
7759TRANS_FEAT(USMMLA, aa64_sve_i8mm, gen_gvec_ool_arg_zzzz,
7760 gen_helper_gvec_usmmla_b, a, 0)
7761TRANS_FEAT(UMMLA, aa64_sve_i8mm, gen_gvec_ool_arg_zzzz,
7762 gen_helper_gvec_ummla_b, a, 0)
cb8657f7 7763
eec05e4e
RH
7764TRANS_FEAT(BFDOT_zzzz, aa64_sve_bf16, gen_gvec_ool_arg_zzzz,
7765 gen_helper_gvec_bfdot, a, 0)
f3500a25
RH
7766TRANS_FEAT(BFDOT_zzxz, aa64_sve_bf16, gen_gvec_ool_arg_zzxz,
7767 gen_helper_gvec_bfdot_idx, a)
81266a1f 7768
eec05e4e
RH
7769TRANS_FEAT(BFMMLA, aa64_sve_bf16, gen_gvec_ool_arg_zzzz,
7770 gen_helper_gvec_bfmmla, a, 0)
5693887f
RH
7771
7772static bool do_BFMLAL_zzzw(DisasContext *s, arg_rrrr_esz *a, bool sel)
7773{
7774 if (!dc_isar_feature(aa64_sve_bf16, s)) {
7775 return false;
7776 }
7777 if (sve_access_check(s)) {
7778 TCGv_ptr status = fpstatus_ptr(FPST_FPCR);
7779 unsigned vsz = vec_full_reg_size(s);
7780
7781 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
7782 vec_full_reg_offset(s, a->rn),
7783 vec_full_reg_offset(s, a->rm),
7784 vec_full_reg_offset(s, a->ra),
7785 status, vsz, vsz, sel,
7786 gen_helper_gvec_bfmlal);
7787 tcg_temp_free_ptr(status);
7788 }
7789 return true;
7790}
7791
7792static bool trans_BFMLALB_zzzw(DisasContext *s, arg_rrrr_esz *a)
7793{
7794 return do_BFMLAL_zzzw(s, a, false);
7795}
7796
7797static bool trans_BFMLALT_zzzw(DisasContext *s, arg_rrrr_esz *a)
7798{
7799 return do_BFMLAL_zzzw(s, a, true);
7800}
458d0ab6
RH
7801
7802static bool do_BFMLAL_zzxw(DisasContext *s, arg_rrxr_esz *a, bool sel)
7803{
7804 if (!dc_isar_feature(aa64_sve_bf16, s)) {
7805 return false;
7806 }
7807 if (sve_access_check(s)) {
7808 TCGv_ptr status = fpstatus_ptr(FPST_FPCR);
7809 unsigned vsz = vec_full_reg_size(s);
7810
7811 tcg_gen_gvec_4_ptr(vec_full_reg_offset(s, a->rd),
7812 vec_full_reg_offset(s, a->rn),
7813 vec_full_reg_offset(s, a->rm),
7814 vec_full_reg_offset(s, a->ra),
7815 status, vsz, vsz, (a->index << 1) | sel,
7816 gen_helper_gvec_bfmlal_idx);
7817 tcg_temp_free_ptr(status);
7818 }
7819 return true;
7820}
7821
7822static bool trans_BFMLALB_zzxw(DisasContext *s, arg_rrxr_esz *a)
7823{
7824 return do_BFMLAL_zzxw(s, a, false);
7825}
7826
7827static bool trans_BFMLALT_zzxw(DisasContext *s, arg_rrxr_esz *a)
7828{
7829 return do_BFMLAL_zzxw(s, a, true);
7830}