target/riscv: add support for RISC-V P extension (v0.20 draft)
Subsystems (2)
cover target/riscv: add support for RISC-V P extension (v0.20 draft)
This series adds support for the RISC-V Packed SIMD (P) extension.
The P extension defines packed-SIMD fixed-point operations intended for DSP-style workloads such as multimedia and signal processing. These instructions operate on packed subword elements within general-purpose registers (GPRs).
The implementation follows the current development draft of the specification (v0.20):
https://www.jhauser.us/RISCV/ext-P/
All instructions defined prior to the v0.20 update were verified using a simple instruction coverage test suite:
https://github.com/mollybuild/qemu-riscv-test-uart
Instructions newly introduced in v0.20 were not tested yet because toolchain support for generating them is still under development.
The implementation focuses on functional correctness and ISA coverage. Performance optimizations were not considered at this stage.
Feedback on implementation details would be highly appreciated.
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
Molly Chen (14):
target/riscv: rvp: Add option defines and dependency check for packed
simd extension
target/riscv: rvp: add arithmetic instructions,including saturating
and non-saturating operations
target/riscv: rvp: add averaging operations
target/riscv: rvp: add absolute value and difference,comparison and
mask generation operations
target/riscv: rvp: add shift operations
target/riscv: rvp: add exchange operations
target/riscv: rvp: add horizontal reduction, pack, merge and cout
leading operations
target/riscv: rvp: add pure multiplication operations target/riscv: rvp: add multiply-accumulate operations target/riscv: rvp: add Q-format multiplication operations target/riscv: rvp: add two-way and four-way multiply and accumulate
operations
target/riscv: rvp: add load and replicate instructions. target/riscv: rvp: add rv32-only register-pair instructions target/riscv: rvp: update to v020, add SHL and PNCLIP[U]P.*
instructions
target/riscv/cpu.c | 5 +- target/riscv/cpu.h | 1 + target/riscv/helper.h | 531 ++ target/riscv/insn32.decode | 829 ++ target/riscv/insn_trans/trans_rvp.c.inc | 1691 ++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 9452 +++++++++++++++++++++++ target/riscv/tcg/tcg-cpu.c | 16 + target/riscv/translate.c | 3 + 9 files changed, 12528 insertions(+), 3 deletions(-) create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644 target/riscv/psimd_helper.c
Signature · 1 line
2.34.1
PATCH 1/14 target/riscv: rvp: Add option defines and dependency check for packed simd extension
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
--- target/riscv/cpu.c | 5 +++-- target/riscv/cpu.h | 1 + target/riscv/tcg/tcg-cpu.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)
target/riscv/cpu.c +3 -2
@@ -41,7 +41,7 @@/* RISC-V CPU definitions */ static const char riscv_single_letter_exts[] ="IEMAFDQCBPVH" ;const uint32_t misa_bits[] = {RVI ,RVE ,RVM ,RVA ,RVF ,RVD ,RVV , -RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,0 }; +RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,RVP ,0 };/* * From vector_helper.c
@@ -1172,7 +1172,8 @@ static const MISAExtInfo misa_ext_info_arr[] = {MISA_EXT_INFO (RVH ,"h" ,"Hypervisor" ),MISA_EXT_INFO (RVV ,"v" ,"Vector operations" ),MISA_EXT_INFO (RVG ,"g" ,"General purpose (IMAFD_Zicsr_Zifencei)" ), - MISA_EXT_INFO(RVB, "b", "Bit manipulation (Zba_Zbb_Zbs)") +MISA_EXT_INFO (RVB ,"b" ,"Bit manipulation (Zba_Zbb_Zbs)" ), + MISA_EXT_INFO(RVP, "x-p", "Packed-SIMD instructions") };static void riscv_cpu_validate_misa_mxl (RISCVCPUClass *mcc)
target/riscv/cpu.h +1 -0
@@ -69,6 +69,7 @@ typedef struct CPUArchState CPURISCVState;#define RVH RV('H')#define RVG RV('G')#define RVB RV('B') +#define RVP RV('P')extern const uint32_t misa_bits[];const char *riscv_get_misa_ext_name (uint32_t bit);
target/riscv/tcg/tcg-cpu.c +16 -0
@@ -601,6 +601,11 @@ static void riscv_cpu_validate_b(RISCVCPU *cpu) } } +static void riscv_cpu_validate_p (RISCVCPU *cpu) +{ +/* Enable sub-extensions here. Do nothing for now. */ +} +/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly.
@@ -619,6 +624,10 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)riscv_cpu_validate_b (cpu); } +if (riscv_has_ext (env,RVP )) { +riscv_cpu_validate_p (cpu); + } +if (riscv_has_ext (env,RVI ) &&riscv_has_ext (env,RVE )) {error_setg (errp,"I and E extensions are incompatible" );
@@ -683,6 +692,12 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } +if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +riscv_cpu_validate_v (env, &cpu->cfg, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);
@@ -1413,6 +1428,7 @@ static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {MISA_CFG (RVV , false),MISA_CFG (RVG , false),MISA_CFG (RVB , false), + MISA_CFG(RVP, false), };/*
Signature · 1 line
2.34.1
Re: [PATCH 01/14] target/riscv: rvp: Add option defines and dependency check for packed simd extension
Quoted 51 lines · Molly Chen
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
Signed-off-by Molly Chen <[email protected]>--- target/riscv/cpu.c | 5 +++-- target/riscv/cpu.h | 1 + target/riscv/tcg/tcg-cpu.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)target/riscv/cpu.c +3
-2@@ -41,7 +41,7 @@/* RISC-V CPU definitions */ static const char riscv_single_letter_exts[] ="IEMAFDQCBPVH" ;const uint32_t misa_bits[] = {RVI ,RVE ,RVM ,RVA ,RVF ,RVD ,RVV , -RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,0 }; +RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,RVP ,0 };/* * From vector_helper.c @@ -1172,7 +1172,8 @@ static const MISAExtInfo misa_ext_info_arr[] = {MISA_EXT_INFO (RVH ,"h" ,"Hypervisor" ),MISA_EXT_INFO (RVV ,"v" ,"Vector operations" ),MISA_EXT_INFO (RVG ,"g" ,"General purpose (IMAFD_Zicsr_Zifencei)" ), - MISA_EXT_INFO(RVB, "b", "Bit manipulation (Zba_Zbb_Zbs)") +MISA_EXT_INFO (RVB ,"b" ,"Bit manipulation (Zba_Zbb_Zbs)" ), + MISA_EXT_INFO(RVP, "x-p", "Packed-SIMD instructions") };static void riscv_cpu_validate_misa_mxl (RISCVCPUClass *mcc)target/riscv/cpu.h +1
-0@@ -69,6 +69,7 @@ typedef struct CPUArchState CPURISCVState;#define RVH RV('H')#define RVG RV('G')#define RVB RV('B') +#define RVP RV('P')extern const uint32_t misa_bits[];const char *riscv_get_misa_ext_name (uint32_t bit);target/riscv/tcg/tcg-cpu.c +0
-0
target/riscv/tcg/tcg-cpu.c static void riscv_cpu_validate_b(RISCVCPU *cpu) · line 607
Quoted 7 lines
target/riscv/tcg/tcg-cpu.c +4
-0@@ -601,6 +601,11 @@ static void riscv_cpu_validate_b(RISCVCPU *cpu) } } +static void riscv_cpu_validate_p (RISCVCPU *cpu) +{ +/* Enable sub-extensions here. Do nothing for now. */ +}
To enable sub-extensions/dependencies we have implicit/implied rules (e.g. RVA_IMPLIED in target/riscv/cpu.c). I believe we should also use this logic here when the time comes.
Usually we use 'validate' functions to assert if the preconditions are met, so basically what you ended up doing down there:
target/riscv/tcg/tcg-cpu.c void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) · line 700
Quoted 6 lines
target/riscv/tcg/tcg-cpu.c +6
-0+if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +
I believe this piece of code should be put in riscv_cpu_validate_p().
The rest LGTM. Thanks,
Daniel
Quoted 36 lines
target/riscv/tcg/tcg-cpu.c +12
-0+/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly. @@ -619,6 +624,10 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)riscv_cpu_validate_b (cpu); } +if (riscv_has_ext (env,RVP )) { +riscv_cpu_validate_p (cpu); + } +if (riscv_has_ext (env,RVI ) &&riscv_has_ext (env,RVE )) {error_setg (errp,"I and E extensions are incompatible" );@@ -683,6 +692,12 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } +if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +riscv_cpu_validate_v (env, &cpu->cfg, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);@@ -1413,6 +1428,7 @@ static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {MISA_CFG (RVV , false),MISA_CFG (RVG , false),MISA_CFG (RVB , false), + MISA_CFG(RVP, false), };/*
Re: [PATCH 01/14] target/riscv: rvp: Add option defines and dependency check for packed simd extension
Hi Molly:
target/riscv/tcg/tcg-cpu.c void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) · line 700
Quoted 6 lines
target/riscv/tcg/tcg-cpu.c +6
-0+if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +
zbkb should not in required extension according spec:
Re: [PATCH 01/14] target/riscv: rvp: Add option defines and dependency check for packed simd extension
Hi Kito,
Thanks for review. This is because the P extension includes pseudo-instructions for |pack|,
while the trans function for |pack|checks for a dependency on |zbkb|.
Perhaps the dependency check in the |pack|function should first be adjusted to depend on either |zbkb|or |zbb|.
Quoted 5 lines
static bool
trans_pack(DisasContext *ctx, arg_pack *a){
REQUIRE_ZBKB(ctx);
returngen_arith(ctx, a, EXT_NONE, gen_pack, NULL); }
Best regards,
Molly
Quoted 11 lines · Kito Cheng
Hi Molly:
target/riscv/tcg/tcg-cpu.c void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) · line 700
Quoted 6 lines
target/riscv/tcg/tcg-cpu.c +6
-0+if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +zbkb should not in required extension according spec:
Re: [PATCH 01/14] target/riscv: rvp: Add option defines and dependency check for packed simd extension
In reply to Molly ChenQuoted 3 lines · Molly Chen
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
Should be 'Co-authored-by', other patches have similar issues too, please check.
Thanks,
Chao
Quoted 94 lines
Signed-off-by Molly Chen <[email protected]>--- target/riscv/cpu.c | 5 +++-- target/riscv/cpu.h | 1 + target/riscv/tcg/tcg-cpu.c | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-)target/riscv/cpu.c +3
-2@@ -41,7 +41,7 @@/* RISC-V CPU definitions */ static const char riscv_single_letter_exts[] ="IEMAFDQCBPVH" ;const uint32_t misa_bits[] = {RVI ,RVE ,RVM ,RVA ,RVF ,RVD ,RVV , -RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,0 }; +RVC ,RVS ,RVU ,RVH ,RVG ,RVB ,RVP ,0 };/* * From vector_helper.c @@ -1172,7 +1172,8 @@ static const MISAExtInfo misa_ext_info_arr[] = {MISA_EXT_INFO (RVH ,"h" ,"Hypervisor" ),MISA_EXT_INFO (RVV ,"v" ,"Vector operations" ),MISA_EXT_INFO (RVG ,"g" ,"General purpose (IMAFD_Zicsr_Zifencei)" ), - MISA_EXT_INFO(RVB, "b", "Bit manipulation (Zba_Zbb_Zbs)") +MISA_EXT_INFO (RVB ,"b" ,"Bit manipulation (Zba_Zbb_Zbs)" ), + MISA_EXT_INFO(RVP, "x-p", "Packed-SIMD instructions") };static void riscv_cpu_validate_misa_mxl (RISCVCPUClass *mcc)target/riscv/cpu.h +1
-0@@ -69,6 +69,7 @@ typedef struct CPUArchState CPURISCVState;#define RVH RV('H')#define RVG RV('G')#define RVB RV('B') +#define RVP RV('P')extern const uint32_t misa_bits[];const char *riscv_get_misa_ext_name (uint32_t bit);target/riscv/tcg/tcg-cpu.c +16
-0@@ -601,6 +601,11 @@ static void riscv_cpu_validate_b(RISCVCPU *cpu) } } +static void riscv_cpu_validate_p (RISCVCPU *cpu) +{ +/* Enable sub-extensions here. Do nothing for now. */ +} +/* * Check consistency between chosen extensions while setting * cpu->cfg accordingly. @@ -619,6 +624,10 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)riscv_cpu_validate_b (cpu); } +if (riscv_has_ext (env,RVP )) { +riscv_cpu_validate_p (cpu); + } +if (riscv_has_ext (env,RVI ) &&riscv_has_ext (env,RVE )) {error_setg (errp,"I and E extensions are incompatible" );@@ -683,6 +692,12 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp)return ; } +if (riscv_has_ext (env,RVP ) && + !(cpu->cfg.ext_zba && cpu->cfg.ext_zbb && cpu->cfg.ext_zbkb)) { +error_setg (errp,"P extension requires zba, zbb and zbkb extensions" ); +return ; + } +riscv_cpu_validate_v (env, &cpu->cfg, &local_err);if (local_err !=NULL ) {error_propagate (errp, local_err);@@ -1413,6 +1428,7 @@ static const RISCVCPUMisaExtConfig misa_ext_cfgs[] = {MISA_CFG (RVV , false),MISA_CFG (RVG , false),MISA_CFG (RVB , false), + MISA_CFG(RVP, false), };/* Signature · 1 line
2.34.1
PATCH 2/14 target/riscv: rvp: add arithmetic instructions, including saturating and non-saturating operations
In reply to Molly Chen--- target/riscv/helper.h | 40 + target/riscv/insn32.decode | 65 ++ target/riscv/insn_trans/trans_rvp.c.inc | 564 ++++++++++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 1069 +++++++++++++++++++++++ target/riscv/translate.c | 1 + 6 files changed, 1741 insertions(+), 1 deletion(-) create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644 target/riscv/psimd_helper.c
target/riscv/helper.h +40 -0
@@ -1351,3 +1351,43 @@ DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(ssamoswap_disabled, void, env)#endif + +/* Packed SIMD */ +DEF_HELPER_3(padd_b, tl, env, tl, tl) +DEF_HELPER_3(padd_h, tl, env, tl, tl) +DEF_HELPER_3(padd_w, i64, env, i64, i64) +DEF_HELPER_3(padd_bs, tl, env, tl, tl) +DEF_HELPER_3(padd_hs, tl, env, tl, tl) +DEF_HELPER_3(padd_ws, i64, env, i64, i64) +DEF_HELPER_3(psub_b, tl, env, tl, tl) +DEF_HELPER_3(psub_h, tl, env, tl, tl) +DEF_HELPER_3(psub_w, i64, env, i64, i64) +DEF_HELPER_3(psh1add_h, tl, env, tl, tl) +DEF_HELPER_3(psh1add_w, i64, env, i64, i64) +DEF_HELPER_3(pssh1sadd_h, tl, env, tl, tl) +DEF_HELPER_3(pssh1sadd_w, i64, env, i64, i64) +DEF_HELPER_3(ssh1sadd, i32, env, i32, i32) +DEF_HELPER_3(psadd_b, tl, env, tl, tl) +DEF_HELPER_3(psadd_h, tl, env, tl, tl) +DEF_HELPER_3(psadd_w, i64, env, i64, i64) +DEF_HELPER_3(psaddu_b, tl, env, tl, tl) +DEF_HELPER_3(psaddu_h, tl, env, tl, tl) +DEF_HELPER_3(psaddu_w, i64, env, i64, i64) +DEF_HELPER_3(sadd, i32, env, i32, i32) +DEF_HELPER_3(saddu, i32, env, i32, i32) +DEF_HELPER_3(pssub_b, tl, env, tl, tl) +DEF_HELPER_3(pssub_h, tl, env, tl, tl) +DEF_HELPER_3(pssub_w, i64, env, i64, i64) +DEF_HELPER_3(pssubu_b, tl, env, tl, tl) +DEF_HELPER_3(pssubu_h, tl, env, tl, tl) +DEF_HELPER_3(pssubu_w, i64, env, i64, i64) +DEF_HELPER_3(ssub, i32, env, i32, i32) +DEF_HELPER_3(ssubu, i32, env, i32, i32) +DEF_HELPER_3(psati_h, tl, env, tl, tl) +DEF_HELPER_3(pusati_h, tl, env, tl, tl) +DEF_HELPER_3(psati_w, i64, env, i64, i64) +DEF_HELPER_3(pusati_w, i64, env, i64, i64) +DEF_HELPER_3(sati_32, i32, env, i32, i32) +DEF_HELPER_3(usati_32, i32, env, i32, i32) +DEF_HELPER_3(sati_64, i64, env, i64, i64) +DEF_HELPER_3 (usati_64, i64, env, i64, i64)
target/riscv/insn32.decode +65 -0
80 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc new file +564 -0
564 lines not on this page · raw message
target/riscv/meson.build +2 -1
@@ -28,7 +28,8 @@ riscv_ss.add(files( 'm128_helper.c', 'crypto_helper.c', 'zce_helper.c', - 'vcrypto_helper.c' + 'vcrypto_helper.c', + 'psimd_helper.c' )) riscv_system_ss = ss.source_set()
target/riscv/psimd_helper.c new file +1069 -0
1,069 lines not on this page · raw message
target/riscv/translate.c +1 -0
@@ -1206,6 +1206,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "insn_trans/trans_rvh.c.inc" #include "insn_trans/trans_rvv.c.inc" #include "insn_trans/trans_rvb.c.inc" +#include "insn_trans/trans_rvp.c.inc" #include "insn_trans/trans_rvzicond.c.inc" #include "insn_trans/trans_rvzacas.c.inc" #include "insn_trans/trans_rvzabha.c.inc"
Signature · 1 line
2.34.1
Re: [PATCH 02/14] target/riscv: rvp: add arithmetic instructions, including saturating and non-saturating operations
Quoted 8 lines · Molly Chen
target/riscv/psimd_helper.c new file +8
-0+/** +* PADD.B - Packed 8-bit addition +* For each byte: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ;
target/riscv/psimd_helper.c line 151
Quoted 10 lines
target/riscv/psimd_helper.c new file +10
-0+int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +}
tcg_gen_vec_add8_{i32,i64}.
There are a fair few other vector operations that can be expanded from wide integer operands at the end of include/tcg/tcg-op-gvec-common.h.
r~
Re: [PATCH 02/14] target/riscv: rvp: add arithmetic instructions,including saturating and non-saturating operations
In reply to Molly ChenQuoted 170 lines · Molly Chen
Signed-off-by Molly Chen <[email protected]>--- target/riscv/helper.h | 40 + target/riscv/insn32.decode | 65 ++ target/riscv/insn_trans/trans_rvp.c.inc | 564 ++++++++++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 1069 +++++++++++++++++++++++ target/riscv/translate.c | 1 + 6 files changed, 1741 insertions(+), 1 deletion(-)create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644
target/riscv/psimd_helper.ctarget/riscv/helper.h +40
-0@@ -1351,3 +1351,43 @@ DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(ssamoswap_disabled, void, env)#endif + +/* Packed SIMD */ +DEF_HELPER_3(padd_b, tl, env, tl, tl) +DEF_HELPER_3(padd_h, tl, env, tl, tl) +DEF_HELPER_3(padd_w, i64, env, i64, i64) +DEF_HELPER_3(padd_bs, tl, env, tl, tl) +DEF_HELPER_3(padd_hs, tl, env, tl, tl) +DEF_HELPER_3(padd_ws, i64, env, i64, i64) +DEF_HELPER_3(psub_b, tl, env, tl, tl) +DEF_HELPER_3(psub_h, tl, env, tl, tl) +DEF_HELPER_3(psub_w, i64, env, i64, i64) +DEF_HELPER_3(psh1add_h, tl, env, tl, tl) +DEF_HELPER_3(psh1add_w, i64, env, i64, i64) +DEF_HELPER_3(pssh1sadd_h, tl, env, tl, tl) +DEF_HELPER_3(pssh1sadd_w, i64, env, i64, i64) +DEF_HELPER_3(ssh1sadd, i32, env, i32, i32) +DEF_HELPER_3(psadd_b, tl, env, tl, tl) +DEF_HELPER_3(psadd_h, tl, env, tl, tl) +DEF_HELPER_3(psadd_w, i64, env, i64, i64) +DEF_HELPER_3(psaddu_b, tl, env, tl, tl) +DEF_HELPER_3(psaddu_h, tl, env, tl, tl) +DEF_HELPER_3(psaddu_w, i64, env, i64, i64) +DEF_HELPER_3(sadd, i32, env, i32, i32) +DEF_HELPER_3(saddu, i32, env, i32, i32) +DEF_HELPER_3(pssub_b, tl, env, tl, tl) +DEF_HELPER_3(pssub_h, tl, env, tl, tl) +DEF_HELPER_3(pssub_w, i64, env, i64, i64) +DEF_HELPER_3(pssubu_b, tl, env, tl, tl) +DEF_HELPER_3(pssubu_h, tl, env, tl, tl) +DEF_HELPER_3(pssubu_w, i64, env, i64, i64) +DEF_HELPER_3(ssub, i32, env, i32, i32) +DEF_HELPER_3(ssubu, i32, env, i32, i32) +DEF_HELPER_3(psati_h, tl, env, tl, tl) +DEF_HELPER_3(pusati_h, tl, env, tl, tl) +DEF_HELPER_3(psati_w, i64, env, i64, i64) +DEF_HELPER_3(pusati_w, i64, env, i64, i64) +DEF_HELPER_3(sati_32, i32, env, i32, i32) +DEF_HELPER_3(usati_32, i32, env, i32, i32) +DEF_HELPER_3(sati_64, i64, env, i64, i64) +DEF_HELPER_3 (usati_64, i64, env, i64, i64)target/riscv/insn32.decode +65
-0@@ -40,6 +40,9 @@ %imm_z6 26:1 15:5 %imm_mop5 30:1 26:2 20:2 %imm_mop3 30:1 26:2 +%imm_p_ui16 20:4 +%imm_p_ui32 20:5 +%imm_p_ui64 20:6 # Argument sets: &empty@@ -105,6 +108,10 @@ @mop5 . . .. .. .... .. ..... ... ..... ....... &mop5 imm=%imm_mop5 %rd %rs1 @mop3 . . .. .. . ..... ..... ... ..... ....... &mop3 imm=%imm_mop3 %rd %rs1 %rs2 +@p_ui16 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui16 %rs1 %rd +@p_ui32 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui32 %rs1 %rd +@p_ui64 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui64 %rs1 %rd + # Formats 64: @sh5 ....... ..... ..... ... ..... ....... &shift shamt=%sh5 %rs1 %rd@@ -1084,3 +1091,61 @@ sb_aqrl 00111 . . ..... ..... 000 ..... 0101111 @atom_st sh_aqrl 00111 . . ..... ..... 001 ..... 0101111 @atom_st sw_aqrl 00111 . . ..... ..... 010 ..... 0101111 @atom_st sd_aqrl 00111 . . ..... ..... 011 ..... 0101111 @atom_st + + +# *** P Experimental Extension Version v018 *** +# Arithmetic Operations(Non-Saturating and Saturating) +padd_b 1000010 ..... ..... 000 ..... 0111011 @r +padd_h 1000000 ..... ..... 000 ..... 0111011 @r +padd_w 1000001 ..... ..... 000 ..... 0111011 @r +padd_bs 1001110 ..... ..... 010 ..... 0011011 @r +padd_hs 1001100 ..... ..... 010 ..... 0011011 @r +padd_ws 1001101 ..... ..... 010 ..... 0011011 @r +psub_b 1100010 ..... ..... 000 ..... 0111011 @r +psub_h 1100000 ..... ..... 000 ..... 0111011 @r +psub_w 1100001 ..... ..... 000 ..... 0111011 @r +psh1add_h 1010000 ..... ..... 010 ..... 0111011 @r +psh1add_w 1010001 ..... ..... 010 ..... 0111011 @r +pssh1sadd_h 1011000 ..... ..... 010 ..... 0111011 @r +{ + ssh1sadd 1011001 ..... ..... 010 ..... 0111011 @r + pssh1sadd_w 1011001 ..... ..... 010 ..... 0111011 @r +} +psadd_b 1001010 ..... ..... 000 ..... 0111011 @r +psadd_h 1001000 ..... ..... 000 ..... 0111011 @r +{ + sadd 1001001 ..... ..... 000 ..... 0111011 @r + psadd_w 1001001 ..... ..... 000 ..... 0111011 @r +} +psaddu_b 1011010 ..... ..... 000 ..... 0111011 @r +psaddu_h 1011000 ..... ..... 000 ..... 0111011 @r +{ + saddu 1011001 ..... ..... 000 ..... 0111011 @r + psaddu_w 1011001 ..... ..... 000 ..... 0111011 @r +} +pssub_b 1101010 ..... ..... 000 ..... 0111011 @r +pssub_h 1101000 ..... ..... 000 ..... 0111011 @r +{ + ssub 1101001 ..... ..... 000 ..... 0111011 @r + pssub_w 1101001 ..... ..... 000 ..... 0111011 @r +} +pssubu_b 1111010 ..... ..... 000 ..... 0111011 @r +pssubu_h 1111000 ..... ..... 000 ..... 0111011 @r +{ + ssubu 1111001 ..... ..... 000 ..... 0111011 @r + pssubu_w 1111001 ..... ..... 000 ..... 0111011 @r +} +psati_h 11100 001.... ..... 100 ..... 0011011 @p_ui16 +pusati_h 10100 001.... ..... 100 ..... 0011011 @p_ui16 +{ + sati_32 11100 01..... ..... 100 ..... 0011011 @p_ui32 + psati_w 11100 01..... ..... 100 ..... 0011011 @p_ui32 +} +{ + usati_32 10100 01..... ..... 100 ..... 0011011 @p_ui32 + pusati_w 10100 01..... ..... 100 ..... 0011011 @p_ui32 +} + +sati_64 111001 ...... ..... 100 ..... 0011011 @p_ui64 +usati_64 101001 ...... ..... 100 ..... 0011011 @p_ui64 +target/riscv/insn_trans/trans_rvp.c.inc new file +17
-0@@ -0,0 +1,564 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V translation routines for the P Standard Extensions. */ +/* Copyright (c) 2026 ISRC ISCAS. */ + +#define GEN_SIMD_TRANS(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_32(NAME) \
target/riscv/insn_trans/trans_rvp.c.inc line 27
Quoted 10 lines
target/riscv/insn_trans/trans_rvp.c.inc new file +10
-0+static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +}
I believe creating trans_rvp.c.inc and psimd_helper.c are definitely the right thing to do, so that's nice. We need to reduce code repetition as much as possible though, in particular because we're looking at 11,000 lines to be added.
One thing we can do is re-use as much code as we can. For instance, I noticed that a lot of 32 bit helpers are basically the same 64 bit helper with a "REQUIRE_32BIT(ctx);" that precedes it, like GEN_SIMD_TRANS(NAME) and GEN_SIMD_TRANS_32(NAME) from above.
One thing we can do is (ps: pseudo untested code):
Quoted 12 lines
target/riscv/insn_trans/trans_rvp.c.inc new file +12
-0+#define GEN_SIMD_TRANS(NAME) \ +static bool gen_trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \
target/riscv/insn_trans/trans_rvp.c.inc line 84
Quoted 10 lines
target/riscv/insn_trans/trans_rvp.c.inc new file +10
-0+ return gen_trans_##NAME(ctx, a); \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return gen_trans_##NAME(ctx, a); \ +}
I.e. create a helper that contains the common logic for 32 and 64 bits, then use the 32/64 bit macros to add any specific checks and then call the common helper.
A keen observation here would be "your version has one less line than the original", and that's completely true. But note that the code from gen_trans_##NAME is used in other translations like GEN_SIMD_TRANS_64 down below, so there's more reduction to be made. There's also a variant of that that calls gen_set_gpr() at the end that can be turned into another helper that can re-used in other translations too.
This advice applies to other patches as well. We want to eliminate code repetition as much as we can - not only because of lines of code but also to have fewer points of logic to take care of.
Thanks,
Daniel
Quoted 1,638 lines
target/riscv/insn_trans/trans_rvp.c.inc new file +537
-0+#else +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_ACC(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_R1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + gen_set_gpr(ctx, a->rd, dest); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, src2); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER##_32(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER##_32(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1); \ + gen_set_gpr(ctx, (a->rd) * 2, dest_0); \ + gen_set_gpr(ctx, (a->rd) * 2 + 1, dest_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 imm = tcg_constant_i32(a->imm); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, imm); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + if (a->rd == 0) { \ + tcg_gen_movi_i64(t, 0); \ + } else { \ + get_pair_regs(ctx, t, (a->rd) * 2); \ + } \ + gen_helper_##NAME(t, tcg_env, src1, src2, t); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1_l; \ + TCGv_i32 src1_h; \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + if (a->rs1 == 0) { \ + src1_l = tcg_temp_new_i32(); \ + src1_h = tcg_temp_new_i32(); \ + tcg_gen_movi_i32(src1_l, 0); \ + tcg_gen_movi_i32(src1_h, 0); \ + } else { \ + src1_l = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + src1_h = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + } \ + gen_helper_##NAME(dest, tcg_env, src1_l, src1_h, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv shamt = tcg_constant_tl(a->imm); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, shamt); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv_i32 rs2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, rs2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +GEN_SIMD_TRANS(padd_b) +GEN_SIMD_TRANS(padd_h) +GEN_SIMD_TRANS_64(padd_w) +GEN_SIMD_TRANS(padd_bs) +GEN_SIMD_TRANS(padd_hs) +GEN_SIMD_TRANS_64(padd_ws) +GEN_SIMD_TRANS(psub_b) +GEN_SIMD_TRANS(psub_h) +GEN_SIMD_TRANS_64(psub_w) +GEN_SIMD_TRANS(psh1add_h) +GEN_SIMD_TRANS_64(psh1add_w) +GEN_SIMD_TRANS(pssh1sadd_h) +GEN_SIMD_TRANS_64(pssh1sadd_w) +GEN_SIMD_TRANS_32(ssh1sadd) +GEN_SIMD_TRANS(psadd_b) +GEN_SIMD_TRANS(psadd_h) +GEN_SIMD_TRANS_64(psadd_w) +GEN_SIMD_TRANS(psaddu_b) +GEN_SIMD_TRANS(psaddu_h) +GEN_SIMD_TRANS_64(psaddu_w) +GEN_SIMD_TRANS_32(sadd) +GEN_SIMD_TRANS_32(saddu) +GEN_SIMD_TRANS(pssub_b) +GEN_SIMD_TRANS(pssub_h) +GEN_SIMD_TRANS_64(pssub_w) +GEN_SIMD_TRANS(pssubu_b) +GEN_SIMD_TRANS(pssubu_h) +GEN_SIMD_TRANS_64(pssubu_w) +GEN_SIMD_TRANS_32(ssub) +GEN_SIMD_TRANS_32(ssubu) +GEN_SIMD_TRANS_IMM(psati_h) +GEN_SIMD_TRANS_IMM(pusati_h) +GEN_SIMD_TRANS_IMM_64(psati_w) +GEN_SIMD_TRANS_IMM_64(pusati_w) +GEN_SIMD_TRANS_IMM_32(sati_32) +GEN_SIMD_TRANS_IMM_32(usati_32) +GEN_SIMD_TRANS_IMM_64(sati_64) +GEN_SIMD_TRANS_IMM_64(usati_64)target/riscv/meson.build +2
-1@@ -28,7 +28,8 @@ riscv_ss.add(files( 'm128_helper.c', 'crypto_helper.c', 'zce_helper.c', - 'vcrypto_helper.c' + 'vcrypto_helper.c', + 'psimd_helper.c' )) riscv_system_ss = ss.source_set()target/riscv/psimd_helper.c new file +1069
-0@@ -0,0 +1,1069 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V Packed SIMD Extension Helpers for QEMU. */ +/* Copyright (C) 2026 ISRC ISCAS. */ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "qemu/host-utils.h" +#include "exec/helper-proto.h" +#include "fpu/softfloat.h" +#include "internals.h" + + +/* Helper macros */ + +/* Element count calculations */ +#define ELEMS_B (target) (sizeof(target) * 8 / 8)/* byte elements count */ +#define ELEMS_H (target) (sizeof(target) * 8 / 16) +#define ELEMS_W (target) (sizeof(target) * 8 / 32)/* word elements count */ + +/* Element extraction macros - unsigned to avoid sign extension */ +#define EXTRACT8 (val, idx) (((val) >> ((idx) * 8)) & 0xFF) +#define EXTRACT16 (val, idx) (((val) >> ((idx) * 16)) & 0xFFFF) +#define EXTRACT32 (val, idx) (((val) >> ((idx) * 32)) & 0xFFFFFFFF) + +/* Element insertion macros */ +#define INSERT8 (val, res, idx) \ + ((val) | ((target_ulong)(uint8_t)(res) << ((idx) * 8))) +#define INSERT16 (val, res, idx) \ + ((val) | ((target_ulong)(uint16_t)(res) << ((idx) * 16))) +#define INSERT32 (val, res, idx) \ + ((val) | ((target_ulong)(uint32_t)(res) << ((idx) * 32))) + +/* Saturation constants */ +static const int8_t SAT_MAX_B =127 ; +static const int8_t SAT_MIN_B =-128 ; +static const int16_t SAT_MAX_H =32767 ; +static const int16_t SAT_MIN_H =-32768 ; +static const int32_t SAT_MAX_W =2147483647 ; +static const int32_t SAT_MIN_W =-2147483648LL ; +static const uint8_t USAT_MAX_B =255 ; +static const uint16_t USAT_MAX_H =65535 ; +static const uint32_t USAT_MAX_W =4294967295U ; + + +/* Saturation helper functions */ + +/** +* Signed saturation for 8-bit elements +* Returns saturated value and sets *sat if saturation occurred +*/ +static inline int8_t signed_saturate_b (int32_t val,int *sat) +{ +if (val >SAT_MAX_B ) { + *sat =1 ; +return SAT_MAX_B ; + } +if (val <SAT_MIN_B ) { + *sat =1 ; +return SAT_MIN_B ; + } +return (int8_t )val; +} + +/** +* Signed saturation for 16-bit elements +*/ +static inline int16_t signed_saturate_h (int32_t val,int *sat) +{ +if (val >SAT_MAX_H ) { + *sat =1 ; +return SAT_MAX_H ; + } +if (val <SAT_MIN_H ) { + *sat =1 ; +return SAT_MIN_H ; + } +return (int16_t )val; +} + +/** +* Signed saturation for 32-bit elements +*/ +static inline int32_t signed_saturate_w (int64_t val,int *sat) +{ +if (val >SAT_MAX_W ) { + *sat =1 ; +return SAT_MAX_W ; + } +if (val <SAT_MIN_W ) { + *sat =1 ; +return SAT_MIN_W ; + } +return (int32_t )val; +} + +/** +* Unsigned saturation for 8-bit elements +*/ +static inline uint8_t unsigned_saturate_b (uint32_t val,int *sat) +{ +if (val >USAT_MAX_B ) { + *sat =1 ; +return USAT_MAX_B ; + } +return (uint8_t )val; +} + +/** +* Unsigned saturation for 16-bit elements +*/ +static inline uint16_t unsigned_saturate_h (uint32_t val,int *sat) +{ +if (val >USAT_MAX_H ) { + *sat =1 ; +return USAT_MAX_H ; + } +return (uint16_t )val; +} + +/** +* Unsigned saturation for 32-bit elements +*/ +static inline uint32_t unsigned_saturate_w (uint64_t val,int *sat) +{ +if (val >USAT_MAX_W ) { + *sat =1 ; +return USAT_MAX_W ; + } +return (uint32_t )val; +} + +/* Basic addition operations (non-saturating) */ + +/** +* PADD.B - Packed 8-bit addition +* For each byte: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PADD.H - Packed 16-bit addition +* For each halfword: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.W - Packed 32-bit addition (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[i] (modular) +*/ +uint64_t HELPER (padd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ;/* 2 words in 64-bit */ + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PADD.BS - Packed 8-bit addition with scalar second operand +* For each byte: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_bs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +uint8_t e2 =EXTRACT8 (rs2,0 );/* Scalar, take least significant byte */ + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PADD.HS - Packed 16-bit addition with scalar second operand +* For each halfword: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_hs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint16_t e2 =EXTRACT16 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.WS - Packed 32-bit addition with scalar second operand (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[0] (modular) +*/ +uint64_t HELPER (padd_ws )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +uint32_t e2 =EXTRACT32 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + + +/* Basic subtraction operations (non-saturating) */ + +/** +* PSUB.B - Packed 8-bit subtraction +* For each byte: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 - e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PSUB.H - Packed 16-bit subtraction +* For each halfword: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 - e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSUB.W - Packed 32-bit subtraction (RV64 only) +* For each word: rd[i] = rs1[i] - rs2[i] (modular) +*/ +uint64_t HELPER (psub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 - e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/* Shift-left-by-one and add operations */ + +/** +* PSH1ADD.H - Shift left by 1 and add (16-bit) +* For each halfword: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +target_ulong HELPER (psh1add_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = (e1 <<1 ) + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSH1ADD.W - Shift left by 1 and add (32-bit, RV64 only) +* For each word: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +uint64_t HELPER (psh1add_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 <<1 ) + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PSSH1SADD.H - Saturating shift left by 1 and saturating add (16-bit) +* For each halfword: rd[i] = sat16(sat16(rs1[i] << 1) + rs2[i]) +*/ +target_ulong HELPER (pssh1sadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFF || e1 <-0x4000 ) { + shifted = (e1 <0 ) ?0xFFFF8000LL :0x7FFF ; + sat =1 ; + }else { + shifted = e1 <<1 ; + } + +int32_t sum = shifted + e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSH1SADD.W - Saturating shift left by 1 and add +* with saturation (32-bit, RV64 only) +* For each word: rd[i] = sat32(sat32(rs1[i] << 1) + rs2[i]) +*/ +uint64_t HELPER (pssh1sadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFFFFFF || e1 <-0x40000000 ) { + shifted = (e1 <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )e1 <<1 ; + } + +int64_t sum = shifted + e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSH1SADD - 32-bit scalar saturating shift left by 1 and saturating add +*/ +uint32_t HELPER (ssh1sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t shifted; +int sat =0 ; + +/* Check if shift-left-1 would overflow */ +if (a >0x3FFFFFFF || a <-0x40000000 ) { + shifted = (a <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )a <<1 ; + } + +int64_t sum = shifted + b; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/* Saturating addition operations */ + +/** +* PSADD.B - Packed 8-bit signed saturating addition +* For each byte: rd[i] = sat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int8_t res =signed_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.H - Packed 16-bit signed saturating addition +* For each halfword: rd[i] = sat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.W - Packed 32-bit signed saturating addition (RV64 only) +* For each word: rd[i] = sat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t sum = (int64_t )e1 + (int64_t )e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.B - Packed 8-bit unsigned saturating addition +* For each byte: rd[i] = usat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint8_t res =unsigned_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.H - Packed 16-bit unsigned saturating addition +* For each halfword: rd[i] = usat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint16_t res =unsigned_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.W - Packed 32-bit unsigned saturating addition (RV64 only) +* For each word: rd[i] = usat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psaddu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint64_t sum = (uint64_t )e1 + (uint64_t )e2; +uint32_t res =unsigned_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SADD - 32-bit signed saturating addition +*/ +uint32_t HELPER (sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t sum = (int64_t )a + (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SADDU - 32-bit unsigned saturating addition +*/ +uint32_t HELPER (saddu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t sum = (uint64_t )a + (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturating subtraction operations */ + +/** +* PSSUB.B - Packed 8-bit signed saturating subtraction +* For each byte: rd[i] = sat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int8_t res =signed_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.H - Packed 16-bit signed saturating subtraction +* For each halfword: rd[i] = sat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int16_t res =signed_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.W - Packed 32-bit signed saturating subtraction (RV64 only) +* For each word: rd[i] = sat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t diff = (int64_t )e1 - (int64_t )e2; +int32_t res =signed_saturate_w (diff, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.B - Packed 8-bit unsigned saturating subtraction +* For each byte: rd[i] = usat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t diff = e1 - e2;/* Unsigned subtraction may underflow */ +uint8_t res =unsigned_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.H - Packed 16-bit unsigned saturating subtraction +* For each halfword: rd[i] = usat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t diff = e1 - e2; +uint16_t res =unsigned_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.W - Packed 32-bit unsigned saturating subtraction (RV64 only) +* For each word: rd[i] = usat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssubu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 >= e2) ? (e1 - e2) :0 ; +if (e1 < e2) { + sat =1 ; + } + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSUB - 32-bit signed saturating subtraction +*/ +uint32_t HELPER (ssub )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t diff = (int64_t )a - (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SSUBU - 32-bit unsigned saturating subtraction +*/ +uint32_t HELPER (ssubu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t diff = (uint64_t )a - (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturation instructions (SAT, USAT) */ + +/** +* PSATI.H - Packed 16-bit signed saturate to immediate bit-width +* For each halfword: rd[i] = sat(rs1[i], imm+1 bits) +*/ +target_ulong HELPER (psati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int range = (imm &0x0F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.H - Packed 16-bit unsigned saturate to immediate bit-width +* For each halfword: rd[i] = usat(rs1[i], imm bits) +*/ +target_ulong HELPER (pusati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint16_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSATI.W - Packed 32-bit signed saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = sat(rs1[i], imm+1 bits) +*/ +uint64_t HELPER (psati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +int range = (imm &0x1F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.W - Packed 32-bit unsigned saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = usat(rs1[i], imm bits) +*/ +uint64_t HELPER (pusati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint32_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SATI_32 - 32-bit scalar signed saturation with immediate range +*/ +uint32_t HELPER (sati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +int range = (imm &0x1F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* USATI_32 - 32-bit scalar unsigned saturation with immediate range +*/ +uint32_t HELPER (usati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint32_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* SATI_64 - 64-bit scalar signed saturation with immediate range +*/ +uint64_t HELPER (sati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +int range = (imm &0x3F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +} + +/** +* USATI_64 - 64-bit scalar unsigned saturation with immediate range +*/ +uint64_t HELPER (usati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint64_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +}target/riscv/translate.c +1
-0@@ -1206,6 +1206,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "insn_trans/trans_rvh.c.inc" #include "insn_trans/trans_rvv.c.inc" #include "insn_trans/trans_rvb.c.inc" +#include "insn_trans/trans_rvp.c.inc" #include "insn_trans/trans_rvzicond.c.inc" #include "insn_trans/trans_rvzacas.c.inc" #include "insn_trans/trans_rvzabha.c.inc"
Re: [PATCH 02/14] target/riscv: rvp: add arithmetic instructions,including saturating and non-saturating operations
In reply to Molly ChenQuoted 878 lines · Molly Chen
Signed-off-by Molly Chen <[email protected]>--- target/riscv/helper.h | 40 + target/riscv/insn32.decode | 65 ++ target/riscv/insn_trans/trans_rvp.c.inc | 564 ++++++++++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 1069 +++++++++++++++++++++++ target/riscv/translate.c | 1 + 6 files changed, 1741 insertions(+), 1 deletion(-) create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644 target/riscv/psimd_helper.ctarget/riscv/helper.h +40
-0@@ -1351,3 +1351,43 @@ DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(ssamoswap_disabled, void, env)#endif + +/* Packed SIMD */ +DEF_HELPER_3(padd_b, tl, env, tl, tl) +DEF_HELPER_3(padd_h, tl, env, tl, tl) +DEF_HELPER_3(padd_w, i64, env, i64, i64) +DEF_HELPER_3(padd_bs, tl, env, tl, tl) +DEF_HELPER_3(padd_hs, tl, env, tl, tl) +DEF_HELPER_3(padd_ws, i64, env, i64, i64) +DEF_HELPER_3(psub_b, tl, env, tl, tl) +DEF_HELPER_3(psub_h, tl, env, tl, tl) +DEF_HELPER_3(psub_w, i64, env, i64, i64) +DEF_HELPER_3(psh1add_h, tl, env, tl, tl) +DEF_HELPER_3(psh1add_w, i64, env, i64, i64) +DEF_HELPER_3(pssh1sadd_h, tl, env, tl, tl) +DEF_HELPER_3(pssh1sadd_w, i64, env, i64, i64) +DEF_HELPER_3(ssh1sadd, i32, env, i32, i32) +DEF_HELPER_3(psadd_b, tl, env, tl, tl) +DEF_HELPER_3(psadd_h, tl, env, tl, tl) +DEF_HELPER_3(psadd_w, i64, env, i64, i64) +DEF_HELPER_3(psaddu_b, tl, env, tl, tl) +DEF_HELPER_3(psaddu_h, tl, env, tl, tl) +DEF_HELPER_3(psaddu_w, i64, env, i64, i64) +DEF_HELPER_3(sadd, i32, env, i32, i32) +DEF_HELPER_3(saddu, i32, env, i32, i32) +DEF_HELPER_3(pssub_b, tl, env, tl, tl) +DEF_HELPER_3(pssub_h, tl, env, tl, tl) +DEF_HELPER_3(pssub_w, i64, env, i64, i64) +DEF_HELPER_3(pssubu_b, tl, env, tl, tl) +DEF_HELPER_3(pssubu_h, tl, env, tl, tl) +DEF_HELPER_3(pssubu_w, i64, env, i64, i64) +DEF_HELPER_3(ssub, i32, env, i32, i32) +DEF_HELPER_3(ssubu, i32, env, i32, i32) +DEF_HELPER_3(psati_h, tl, env, tl, tl) +DEF_HELPER_3(pusati_h, tl, env, tl, tl) +DEF_HELPER_3(psati_w, i64, env, i64, i64) +DEF_HELPER_3(pusati_w, i64, env, i64, i64) +DEF_HELPER_3(sati_32, i32, env, i32, i32) +DEF_HELPER_3(usati_32, i32, env, i32, i32) +DEF_HELPER_3(sati_64, i64, env, i64, i64) +DEF_HELPER_3 (usati_64, i64, env, i64, i64)target/riscv/insn32.decode +65
-0@@ -40,6 +40,9 @@ %imm_z6 26:1 15:5 %imm_mop5 30:1 26:2 20:2 %imm_mop3 30:1 26:2 +%imm_p_ui16 20:4 +%imm_p_ui32 20:5 +%imm_p_ui64 20:6 # Argument sets: &empty@@ -105,6 +108,10 @@ @mop5 . . .. .. .... .. ..... ... ..... ....... &mop5 imm=%imm_mop5 %rd %rs1 @mop3 . . .. .. . ..... ..... ... ..... ....... &mop3 imm=%imm_mop3 %rd %rs1 %rs2 +@p_ui16 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui16 %rs1 %rd +@p_ui32 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui32 %rs1 %rd +@p_ui64 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui64 %rs1 %rd + # Formats 64: @sh5 ....... ..... ..... ... ..... ....... &shift shamt=%sh5 %rs1 %rd@@ -1084,3 +1091,61 @@ sb_aqrl 00111 . . ..... ..... 000 ..... 0101111 @atom_st sh_aqrl 00111 . . ..... ..... 001 ..... 0101111 @atom_st sw_aqrl 00111 . . ..... ..... 010 ..... 0101111 @atom_st sd_aqrl 00111 . . ..... ..... 011 ..... 0101111 @atom_st + + +# *** P Experimental Extension Version v018 *** +# Arithmetic Operations(Non-Saturating and Saturating) +padd_b 1000010 ..... ..... 000 ..... 0111011 @r +padd_h 1000000 ..... ..... 000 ..... 0111011 @r +padd_w 1000001 ..... ..... 000 ..... 0111011 @r +padd_bs 1001110 ..... ..... 010 ..... 0011011 @r +padd_hs 1001100 ..... ..... 010 ..... 0011011 @r +padd_ws 1001101 ..... ..... 010 ..... 0011011 @r +psub_b 1100010 ..... ..... 000 ..... 0111011 @r +psub_h 1100000 ..... ..... 000 ..... 0111011 @r +psub_w 1100001 ..... ..... 000 ..... 0111011 @r +psh1add_h 1010000 ..... ..... 010 ..... 0111011 @r +psh1add_w 1010001 ..... ..... 010 ..... 0111011 @r +pssh1sadd_h 1011000 ..... ..... 010 ..... 0111011 @r +{ + ssh1sadd 1011001 ..... ..... 010 ..... 0111011 @r + pssh1sadd_w 1011001 ..... ..... 010 ..... 0111011 @r +} +psadd_b 1001010 ..... ..... 000 ..... 0111011 @r +psadd_h 1001000 ..... ..... 000 ..... 0111011 @r +{ + sadd 1001001 ..... ..... 000 ..... 0111011 @r + psadd_w 1001001 ..... ..... 000 ..... 0111011 @r +} +psaddu_b 1011010 ..... ..... 000 ..... 0111011 @r +psaddu_h 1011000 ..... ..... 000 ..... 0111011 @r +{ + saddu 1011001 ..... ..... 000 ..... 0111011 @r + psaddu_w 1011001 ..... ..... 000 ..... 0111011 @r +} +pssub_b 1101010 ..... ..... 000 ..... 0111011 @r +pssub_h 1101000 ..... ..... 000 ..... 0111011 @r +{ + ssub 1101001 ..... ..... 000 ..... 0111011 @r + pssub_w 1101001 ..... ..... 000 ..... 0111011 @r +} +pssubu_b 1111010 ..... ..... 000 ..... 0111011 @r +pssubu_h 1111000 ..... ..... 000 ..... 0111011 @r +{ + ssubu 1111001 ..... ..... 000 ..... 0111011 @r + pssubu_w 1111001 ..... ..... 000 ..... 0111011 @r +} +psati_h 11100 001.... ..... 100 ..... 0011011 @p_ui16 +pusati_h 10100 001.... ..... 100 ..... 0011011 @p_ui16 +{ + sati_32 11100 01..... ..... 100 ..... 0011011 @p_ui32 + psati_w 11100 01..... ..... 100 ..... 0011011 @p_ui32 +} +{ + usati_32 10100 01..... ..... 100 ..... 0011011 @p_ui32 + pusati_w 10100 01..... ..... 100 ..... 0011011 @p_ui32 +} + +sati_64 111001 ...... ..... 100 ..... 0011011 @p_ui64 +usati_64 101001 ...... ..... 100 ..... 0011011 @p_ui64 +target/riscv/insn_trans/trans_rvp.c.inc new file +564
-0@@ -0,0 +1,564 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V translation routines for the P Standard Extensions. */ +/* Copyright (c) 2026 ISRC ISCAS. */ + +#define GEN_SIMD_TRANS(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_ACC(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_R1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + gen_set_gpr(ctx, a->rd, dest); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, src2); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER##_32(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER##_32(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1); \ + gen_set_gpr(ctx, (a->rd) * 2, dest_0); \ + gen_set_gpr(ctx, (a->rd) * 2 + 1, dest_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 imm = tcg_constant_i32(a->imm); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, imm); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + if (a->rd == 0) { \ + tcg_gen_movi_i64(t, 0); \ + } else { \ + get_pair_regs(ctx, t, (a->rd) * 2); \ + } \ + gen_helper_##NAME(t, tcg_env, src1, src2, t); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1_l; \ + TCGv_i32 src1_h; \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + if (a->rs1 == 0) { \ + src1_l = tcg_temp_new_i32(); \ + src1_h = tcg_temp_new_i32(); \ + tcg_gen_movi_i32(src1_l, 0); \ + tcg_gen_movi_i32(src1_h, 0); \ + } else { \ + src1_l = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + src1_h = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + } \ + gen_helper_##NAME(dest, tcg_env, src1_l, src1_h, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv shamt = tcg_constant_tl(a->imm); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, shamt); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv_i32 rs2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, rs2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +GEN_SIMD_TRANS(padd_b) +GEN_SIMD_TRANS(padd_h) +GEN_SIMD_TRANS_64(padd_w) +GEN_SIMD_TRANS(padd_bs) +GEN_SIMD_TRANS(padd_hs) +GEN_SIMD_TRANS_64(padd_ws) +GEN_SIMD_TRANS(psub_b) +GEN_SIMD_TRANS(psub_h) +GEN_SIMD_TRANS_64(psub_w) +GEN_SIMD_TRANS(psh1add_h) +GEN_SIMD_TRANS_64(psh1add_w) +GEN_SIMD_TRANS(pssh1sadd_h) +GEN_SIMD_TRANS_64(pssh1sadd_w) +GEN_SIMD_TRANS_32(ssh1sadd) +GEN_SIMD_TRANS(psadd_b) +GEN_SIMD_TRANS(psadd_h) +GEN_SIMD_TRANS_64(psadd_w) +GEN_SIMD_TRANS(psaddu_b) +GEN_SIMD_TRANS(psaddu_h) +GEN_SIMD_TRANS_64(psaddu_w) +GEN_SIMD_TRANS_32(sadd) +GEN_SIMD_TRANS_32(saddu) +GEN_SIMD_TRANS(pssub_b) +GEN_SIMD_TRANS(pssub_h) +GEN_SIMD_TRANS_64(pssub_w) +GEN_SIMD_TRANS(pssubu_b) +GEN_SIMD_TRANS(pssubu_h) +GEN_SIMD_TRANS_64(pssubu_w) +GEN_SIMD_TRANS_32(ssub) +GEN_SIMD_TRANS_32(ssubu) +GEN_SIMD_TRANS_IMM(psati_h) +GEN_SIMD_TRANS_IMM(pusati_h) +GEN_SIMD_TRANS_IMM_64(psati_w) +GEN_SIMD_TRANS_IMM_64(pusati_w) +GEN_SIMD_TRANS_IMM_32(sati_32) +GEN_SIMD_TRANS_IMM_32(usati_32) +GEN_SIMD_TRANS_IMM_64(sati_64) +GEN_SIMD_TRANS_IMM_64(usati_64)target/riscv/meson.build +2
-1@@ -28,7 +28,8 @@ riscv_ss.add(files( 'm128_helper.c', 'crypto_helper.c', 'zce_helper.c', - 'vcrypto_helper.c' + 'vcrypto_helper.c', + 'psimd_helper.c' )) riscv_system_ss = ss.source_set()target/riscv/psimd_helper.c new file +141
-0@@ -0,0 +1,1069 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V Packed SIMD Extension Helpers for QEMU. */ +/* Copyright (C) 2026 ISRC ISCAS. */ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "qemu/host-utils.h" +#include "exec/helper-proto.h" +#include "fpu/softfloat.h" +#include "internals.h" + + +/* Helper macros */ + +/* Element count calculations */ +#define ELEMS_B (target) (sizeof(target) * 8 / 8)/* byte elements count */ +#define ELEMS_H (target) (sizeof(target) * 8 / 16) +#define ELEMS_W (target) (sizeof(target) * 8 / 32)/* word elements count */ + +/* Element extraction macros - unsigned to avoid sign extension */ +#define EXTRACT8 (val, idx) (((val) >> ((idx) * 8)) & 0xFF) +#define EXTRACT16 (val, idx) (((val) >> ((idx) * 16)) & 0xFFFF) +#define EXTRACT32 (val, idx) (((val) >> ((idx) * 32)) & 0xFFFFFFFF) + +/* Element insertion macros */ +#define INSERT8 (val, res, idx) \ + ((val) | ((target_ulong)(uint8_t)(res) << ((idx) * 8))) +#define INSERT16 (val, res, idx) \ + ((val) | ((target_ulong)(uint16_t)(res) << ((idx) * 16))) +#define INSERT32 (val, res, idx) \ + ((val) | ((target_ulong)(uint32_t)(res) << ((idx) * 32))) + +/* Saturation constants */ +static const int8_t SAT_MAX_B =127 ; +static const int8_t SAT_MIN_B =-128 ; +static const int16_t SAT_MAX_H =32767 ; +static const int16_t SAT_MIN_H =-32768 ; +static const int32_t SAT_MAX_W =2147483647 ; +static const int32_t SAT_MIN_W =-2147483648LL ; +static const uint8_t USAT_MAX_B =255 ; +static const uint16_t USAT_MAX_H =65535 ; +static const uint32_t USAT_MAX_W =4294967295U ; + + +/* Saturation helper functions */ + +/** +* Signed saturation for 8-bit elements +* Returns saturated value and sets *sat if saturation occurred +*/ +static inline int8_t signed_saturate_b (int32_t val,int *sat) +{ +if (val >SAT_MAX_B ) { + *sat =1 ; +return SAT_MAX_B ; + } +if (val <SAT_MIN_B ) { + *sat =1 ; +return SAT_MIN_B ; + } +return (int8_t )val; +} + +/** +* Signed saturation for 16-bit elements +*/ +static inline int16_t signed_saturate_h (int32_t val,int *sat) +{ +if (val >SAT_MAX_H ) { + *sat =1 ; +return SAT_MAX_H ; + } +if (val <SAT_MIN_H ) { + *sat =1 ; +return SAT_MIN_H ; + } +return (int16_t )val; +} + +/** +* Signed saturation for 32-bit elements +*/ +static inline int32_t signed_saturate_w (int64_t val,int *sat) +{ +if (val >SAT_MAX_W ) { + *sat =1 ; +return SAT_MAX_W ; + } +if (val <SAT_MIN_W ) { + *sat =1 ; +return SAT_MIN_W ; + } +return (int32_t )val; +} + +/** +* Unsigned saturation for 8-bit elements +*/ +static inline uint8_t unsigned_saturate_b (uint32_t val,int *sat) +{ +if (val >USAT_MAX_B ) { + *sat =1 ; +return USAT_MAX_B ; + } +return (uint8_t )val; +} + +/** +* Unsigned saturation for 16-bit elements +*/ +static inline uint16_t unsigned_saturate_h (uint32_t val,int *sat) +{ +if (val >USAT_MAX_H ) { + *sat =1 ; +return USAT_MAX_H ; + } +return (uint16_t )val; +} + +/** +* Unsigned saturation for 32-bit elements +*/ +static inline uint32_t unsigned_saturate_w (uint64_t val,int *sat) +{ +if (val >USAT_MAX_W ) { + *sat =1 ; +return USAT_MAX_W ; + } +return (uint32_t )val; +} + +/* Basic addition operations (non-saturating) */ + +/** +* PADD.B - Packed 8-bit addition +* For each byte: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ;
target/riscv/psimd_helper.c line 151
Quoted 10 lines
target/riscv/psimd_helper.c new file +10
-0+int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +}
If we don't care about performance in the early patchset, but considering maintainability, I'd suggest providing unified helper macros for these operations.
The current SIMD implementations all share a similar loop structure, differing only in element types. We could follow the approach used in vector_helper.c and define a few groups of helper macros to reduce the boilerplate.
e.g.
```
#define DO_ADD(a, b) ((a) + (b))
#define DO_SUB(a, b) ((a) - (b))
#define GEN_PSIMD_VV_B(NAME, OP) \
target_ulong HELPER(NAME)(CPURISCVState *env, \
target_ulong rs1, target_ulong rs2) \
{ \
target_ulong rd = 0; \
for (int i = 0; i < ELEMS_B(rd); i++) { \
uint8_t e1 = EXTRACT8(rs1, i); \
uint8_t e2 = EXTRACT8(rs2, i); \
rd = INSERT8(rd, (uint8_t)OP(e1, e2), i); \
} \
return rd; \
}
GEN_PSIMD_VV_B(padd_b, DO_ADD)
GEN_PSIMD_VV_B(psub_b, DO_SUB)
```The same applies to the helper functions for other instructions as well.
Thanks,
Chao
Quoted 933 lines
target/riscv/psimd_helper.c new file +918
-0+ +/** +* PADD.H - Packed 16-bit addition +* For each halfword: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.W - Packed 32-bit addition (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[i] (modular) +*/ +uint64_t HELPER (padd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ;/* 2 words in 64-bit */ + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PADD.BS - Packed 8-bit addition with scalar second operand +* For each byte: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_bs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +uint8_t e2 =EXTRACT8 (rs2,0 );/* Scalar, take least significant byte */ + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PADD.HS - Packed 16-bit addition with scalar second operand +* For each halfword: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_hs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint16_t e2 =EXTRACT16 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.WS - Packed 32-bit addition with scalar second operand (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[0] (modular) +*/ +uint64_t HELPER (padd_ws )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +uint32_t e2 =EXTRACT32 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + + +/* Basic subtraction operations (non-saturating) */ + +/** +* PSUB.B - Packed 8-bit subtraction +* For each byte: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 - e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PSUB.H - Packed 16-bit subtraction +* For each halfword: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 - e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSUB.W - Packed 32-bit subtraction (RV64 only) +* For each word: rd[i] = rs1[i] - rs2[i] (modular) +*/ +uint64_t HELPER (psub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 - e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/* Shift-left-by-one and add operations */ + +/** +* PSH1ADD.H - Shift left by 1 and add (16-bit) +* For each halfword: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +target_ulong HELPER (psh1add_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = (e1 <<1 ) + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSH1ADD.W - Shift left by 1 and add (32-bit, RV64 only) +* For each word: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +uint64_t HELPER (psh1add_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 <<1 ) + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PSSH1SADD.H - Saturating shift left by 1 and saturating add (16-bit) +* For each halfword: rd[i] = sat16(sat16(rs1[i] << 1) + rs2[i]) +*/ +target_ulong HELPER (pssh1sadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFF || e1 <-0x4000 ) { + shifted = (e1 <0 ) ?0xFFFF8000LL :0x7FFF ; + sat =1 ; + }else { + shifted = e1 <<1 ; + } + +int32_t sum = shifted + e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSH1SADD.W - Saturating shift left by 1 and add +* with saturation (32-bit, RV64 only) +* For each word: rd[i] = sat32(sat32(rs1[i] << 1) + rs2[i]) +*/ +uint64_t HELPER (pssh1sadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFFFFFF || e1 <-0x40000000 ) { + shifted = (e1 <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )e1 <<1 ; + } + +int64_t sum = shifted + e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSH1SADD - 32-bit scalar saturating shift left by 1 and saturating add +*/ +uint32_t HELPER (ssh1sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t shifted; +int sat =0 ; + +/* Check if shift-left-1 would overflow */ +if (a >0x3FFFFFFF || a <-0x40000000 ) { + shifted = (a <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )a <<1 ; + } + +int64_t sum = shifted + b; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/* Saturating addition operations */ + +/** +* PSADD.B - Packed 8-bit signed saturating addition +* For each byte: rd[i] = sat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int8_t res =signed_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.H - Packed 16-bit signed saturating addition +* For each halfword: rd[i] = sat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.W - Packed 32-bit signed saturating addition (RV64 only) +* For each word: rd[i] = sat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t sum = (int64_t )e1 + (int64_t )e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.B - Packed 8-bit unsigned saturating addition +* For each byte: rd[i] = usat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint8_t res =unsigned_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.H - Packed 16-bit unsigned saturating addition +* For each halfword: rd[i] = usat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint16_t res =unsigned_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.W - Packed 32-bit unsigned saturating addition (RV64 only) +* For each word: rd[i] = usat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psaddu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint64_t sum = (uint64_t )e1 + (uint64_t )e2; +uint32_t res =unsigned_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SADD - 32-bit signed saturating addition +*/ +uint32_t HELPER (sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t sum = (int64_t )a + (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SADDU - 32-bit unsigned saturating addition +*/ +uint32_t HELPER (saddu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t sum = (uint64_t )a + (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturating subtraction operations */ + +/** +* PSSUB.B - Packed 8-bit signed saturating subtraction +* For each byte: rd[i] = sat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int8_t res =signed_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.H - Packed 16-bit signed saturating subtraction +* For each halfword: rd[i] = sat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int16_t res =signed_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.W - Packed 32-bit signed saturating subtraction (RV64 only) +* For each word: rd[i] = sat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t diff = (int64_t )e1 - (int64_t )e2; +int32_t res =signed_saturate_w (diff, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.B - Packed 8-bit unsigned saturating subtraction +* For each byte: rd[i] = usat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t diff = e1 - e2;/* Unsigned subtraction may underflow */ +uint8_t res =unsigned_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.H - Packed 16-bit unsigned saturating subtraction +* For each halfword: rd[i] = usat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t diff = e1 - e2; +uint16_t res =unsigned_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.W - Packed 32-bit unsigned saturating subtraction (RV64 only) +* For each word: rd[i] = usat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssubu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 >= e2) ? (e1 - e2) :0 ; +if (e1 < e2) { + sat =1 ; + } + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSUB - 32-bit signed saturating subtraction +*/ +uint32_t HELPER (ssub )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t diff = (int64_t )a - (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SSUBU - 32-bit unsigned saturating subtraction +*/ +uint32_t HELPER (ssubu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t diff = (uint64_t )a - (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturation instructions (SAT, USAT) */ + +/** +* PSATI.H - Packed 16-bit signed saturate to immediate bit-width +* For each halfword: rd[i] = sat(rs1[i], imm+1 bits) +*/ +target_ulong HELPER (psati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int range = (imm &0x0F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.H - Packed 16-bit unsigned saturate to immediate bit-width +* For each halfword: rd[i] = usat(rs1[i], imm bits) +*/ +target_ulong HELPER (pusati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint16_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSATI.W - Packed 32-bit signed saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = sat(rs1[i], imm+1 bits) +*/ +uint64_t HELPER (psati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +int range = (imm &0x1F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.W - Packed 32-bit unsigned saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = usat(rs1[i], imm bits) +*/ +uint64_t HELPER (pusati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint32_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SATI_32 - 32-bit scalar signed saturation with immediate range +*/ +uint32_t HELPER (sati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +int range = (imm &0x1F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* USATI_32 - 32-bit scalar unsigned saturation with immediate range +*/ +uint32_t HELPER (usati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint32_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* SATI_64 - 64-bit scalar signed saturation with immediate range +*/ +uint64_t HELPER (sati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +int range = (imm &0x3F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +} + +/** +* USATI_64 - 64-bit scalar unsigned saturation with immediate range +*/ +uint64_t HELPER (usati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint64_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +}target/riscv/translate.c +1
-0@@ -1206,6 +1206,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "insn_trans/trans_rvh.c.inc" #include "insn_trans/trans_rvv.c.inc" #include "insn_trans/trans_rvb.c.inc" +#include "insn_trans/trans_rvp.c.inc" #include "insn_trans/trans_rvzicond.c.inc" #include "insn_trans/trans_rvzacas.c.inc" #include "insn_trans/trans_rvzabha.c.inc" Signature · 1 line
2.34.1
Re: [PATCH 02/14] target/riscv: rvp: add arithmetic instructions,including saturating and non-saturating operations
Quoted 921 lines · Chao Liu
Quoted 878 lines · Molly Chen
Signed-off-by Molly Chen<[email protected]>--- target/riscv/helper.h | 40 + target/riscv/insn32.decode | 65 ++ target/riscv/insn_trans/trans_rvp.c.inc | 564 ++++++++++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 1069 +++++++++++++++++++++++ target/riscv/translate.c | 1 + 6 files changed, 1741 insertions(+), 1 deletion(-)create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644
target/riscv/psimd_helper.ctarget/riscv/helper.h +40
-0@@ -1351,3 +1351,43 @@ DEF_HELPER_4(vsm4r_vs, void, ptr, ptr, env, i32)#ifndef CONFIG_USER_ONLY DEF_HELPER_1(ssamoswap_disabled, void, env)#endif + +/* Packed SIMD */ +DEF_HELPER_3(padd_b, tl, env, tl, tl) +DEF_HELPER_3(padd_h, tl, env, tl, tl) +DEF_HELPER_3(padd_w, i64, env, i64, i64) +DEF_HELPER_3(padd_bs, tl, env, tl, tl) +DEF_HELPER_3(padd_hs, tl, env, tl, tl) +DEF_HELPER_3(padd_ws, i64, env, i64, i64) +DEF_HELPER_3(psub_b, tl, env, tl, tl) +DEF_HELPER_3(psub_h, tl, env, tl, tl) +DEF_HELPER_3(psub_w, i64, env, i64, i64) +DEF_HELPER_3(psh1add_h, tl, env, tl, tl) +DEF_HELPER_3(psh1add_w, i64, env, i64, i64) +DEF_HELPER_3(pssh1sadd_h, tl, env, tl, tl) +DEF_HELPER_3(pssh1sadd_w, i64, env, i64, i64) +DEF_HELPER_3(ssh1sadd, i32, env, i32, i32) +DEF_HELPER_3(psadd_b, tl, env, tl, tl) +DEF_HELPER_3(psadd_h, tl, env, tl, tl) +DEF_HELPER_3(psadd_w, i64, env, i64, i64) +DEF_HELPER_3(psaddu_b, tl, env, tl, tl) +DEF_HELPER_3(psaddu_h, tl, env, tl, tl) +DEF_HELPER_3(psaddu_w, i64, env, i64, i64) +DEF_HELPER_3(sadd, i32, env, i32, i32) +DEF_HELPER_3(saddu, i32, env, i32, i32) +DEF_HELPER_3(pssub_b, tl, env, tl, tl) +DEF_HELPER_3(pssub_h, tl, env, tl, tl) +DEF_HELPER_3(pssub_w, i64, env, i64, i64) +DEF_HELPER_3(pssubu_b, tl, env, tl, tl) +DEF_HELPER_3(pssubu_h, tl, env, tl, tl) +DEF_HELPER_3(pssubu_w, i64, env, i64, i64) +DEF_HELPER_3(ssub, i32, env, i32, i32) +DEF_HELPER_3(ssubu, i32, env, i32, i32) +DEF_HELPER_3(psati_h, tl, env, tl, tl) +DEF_HELPER_3(pusati_h, tl, env, tl, tl) +DEF_HELPER_3(psati_w, i64, env, i64, i64) +DEF_HELPER_3(pusati_w, i64, env, i64, i64) +DEF_HELPER_3(sati_32, i32, env, i32, i32) +DEF_HELPER_3(usati_32, i32, env, i32, i32) +DEF_HELPER_3(sati_64, i64, env, i64, i64) +DEF_HELPER_3 (usati_64, i64, env, i64, i64)target/riscv/insn32.decode +65
-0@@ -40,6 +40,9 @@ %imm_z6 26:1 15:5 %imm_mop5 30:1 26:2 20:2 %imm_mop3 30:1 26:2 +%imm_p_ui16 20:4 +%imm_p_ui32 20:5 +%imm_p_ui64 20:6 # Argument sets: &empty@@ -105,6 +108,10 @@ @mop5 . . .. .. .... .. ..... ... ..... ....... &mop5 imm=%imm_mop5 %rd %rs1 @mop3 . . .. .. . ..... ..... ... ..... ....... &mop3 imm=%imm_mop3 %rd %rs1 %rs2 +@p_ui16 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui16 %rs1 %rd +@p_ui32 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui32 %rs1 %rd +@p_ui64 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui64 %rs1 %rd + # Formats 64: @sh5 ....... ..... ..... ... ..... ....... &shift shamt=%sh5 %rs1 %rd@@ -1084,3 +1091,61 @@ sb_aqrl 00111 . . ..... ..... 000 ..... 0101111 @atom_st sh_aqrl 00111 . . ..... ..... 001 ..... 0101111 @atom_st sw_aqrl 00111 . . ..... ..... 010 ..... 0101111 @atom_st sd_aqrl 00111 . . ..... ..... 011 ..... 0101111 @atom_st + + +# *** P Experimental Extension Version v018 *** +# Arithmetic Operations(Non-Saturating and Saturating) +padd_b 1000010 ..... ..... 000 ..... 0111011 @r +padd_h 1000000 ..... ..... 000 ..... 0111011 @r +padd_w 1000001 ..... ..... 000 ..... 0111011 @r +padd_bs 1001110 ..... ..... 010 ..... 0011011 @r +padd_hs 1001100 ..... ..... 010 ..... 0011011 @r +padd_ws 1001101 ..... ..... 010 ..... 0011011 @r +psub_b 1100010 ..... ..... 000 ..... 0111011 @r +psub_h 1100000 ..... ..... 000 ..... 0111011 @r +psub_w 1100001 ..... ..... 000 ..... 0111011 @r +psh1add_h 1010000 ..... ..... 010 ..... 0111011 @r +psh1add_w 1010001 ..... ..... 010 ..... 0111011 @r +pssh1sadd_h 1011000 ..... ..... 010 ..... 0111011 @r +{ + ssh1sadd 1011001 ..... ..... 010 ..... 0111011 @r + pssh1sadd_w 1011001 ..... ..... 010 ..... 0111011 @r +} +psadd_b 1001010 ..... ..... 000 ..... 0111011 @r +psadd_h 1001000 ..... ..... 000 ..... 0111011 @r +{ + sadd 1001001 ..... ..... 000 ..... 0111011 @r + psadd_w 1001001 ..... ..... 000 ..... 0111011 @r +} +psaddu_b 1011010 ..... ..... 000 ..... 0111011 @r +psaddu_h 1011000 ..... ..... 000 ..... 0111011 @r +{ + saddu 1011001 ..... ..... 000 ..... 0111011 @r + psaddu_w 1011001 ..... ..... 000 ..... 0111011 @r +} +pssub_b 1101010 ..... ..... 000 ..... 0111011 @r +pssub_h 1101000 ..... ..... 000 ..... 0111011 @r +{ + ssub 1101001 ..... ..... 000 ..... 0111011 @r + pssub_w 1101001 ..... ..... 000 ..... 0111011 @r +} +pssubu_b 1111010 ..... ..... 000 ..... 0111011 @r +pssubu_h 1111000 ..... ..... 000 ..... 0111011 @r +{ + ssubu 1111001 ..... ..... 000 ..... 0111011 @r + pssubu_w 1111001 ..... ..... 000 ..... 0111011 @r +} +psati_h 11100 001.... ..... 100 ..... 0011011 @p_ui16 +pusati_h 10100 001.... ..... 100 ..... 0011011 @p_ui16 +{ + sati_32 11100 01..... ..... 100 ..... 0011011 @p_ui32 + psati_w 11100 01..... ..... 100 ..... 0011011 @p_ui32 +} +{ + usati_32 10100 01..... ..... 100 ..... 0011011 @p_ui32 + pusati_w 10100 01..... ..... 100 ..... 0011011 @p_ui32 +} + +sati_64 111001 ...... ..... 100 ..... 0011011 @p_ui64 +usati_64 101001 ...... ..... 100 ..... 0011011 @p_ui64 +target/riscv/insn_trans/trans_rvp.c.inc new file +564
-0@@ -0,0 +1,564 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V translation routines for the P Standard Extensions. */ +/* Copyright (c) 2026 ISRC ISCAS. */ + +#define GEN_SIMD_TRANS(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, src2); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_ACC(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + TCGv t = tcg_temp_new(); \ + gen_helper_##NAME(t, tcg_env, src1, src2, dest); \ + gen_set_gpr(ctx, a->rd, t); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_R1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + gen_set_gpr(ctx, a->rd, dest); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_R1_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1); \ + return true; \ +} +#endif + +#define GEN_SIMD_TRANS_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_32(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_IMM_64(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_64BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv imm = tcg_constant_tl(a->imm); \ + TCGv dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, src1, imm); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, src2); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_3(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv src2_0 = get_gpr(ctx, (a->rs2) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv src2_1 = get_gpr(ctx, (a->rs2) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, src2_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, src2_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER##_32(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER##_32(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_DW_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1); \ + gen_set_gpr(ctx, (a->rd) * 2, dest_0); \ + gen_set_gpr(ctx, (a->rd) * 2 + 1, dest_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_5(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv_i32 imm = tcg_constant_i32(a->imm); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + gen_helper_##NAME(t, tcg_env, src1, imm); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1_0 = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + TCGv imm_0 = tcg_constant_tl(a->imm); \ + TCGv dest_0 = dest_gpr(ctx, (a->rd) * 2); \ + TCGv src1_1 = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + TCGv imm_1 = tcg_constant_tl(a->imm); \ + TCGv dest_1 = dest_gpr(ctx, (a->rd) * 2 + 1); \ + gen_helper_##HELPER(dest_0, tcg_env, src1_0, imm_0); \ + gen_helper_##HELPER(dest_1, tcg_env, src1_1, imm_1); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_IMM_2(INSN, HELPER) \ +static bool trans_##INSN(DisasContext *ctx, arg_##INSN * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE); \ + TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i64 t = tcg_temp_new_i64(); \ + if (a->rd == 0) { \ + tcg_gen_movi_i64(t, 0); \ + } else { \ + get_pair_regs(ctx, t, (a->rd) * 2); \ + } \ + gen_helper_##NAME(t, tcg_env, src1, src2, t); \ + set_pair_regs(ctx, (a->rd) * 2, t); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_ACC_REG_PAIR_1(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i32 src1_l; \ + TCGv_i32 src1_h; \ + TCGv_i32 src2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + if (a->rs1 == 0) { \ + src1_l = tcg_temp_new_i32(); \ + src1_h = tcg_temp_new_i32(); \ + tcg_gen_movi_i32(src1_l, 0); \ + tcg_gen_movi_i32(src1_h, 0); \ + } else { \ + src1_l = get_gpr(ctx, (a->rs1) * 2, EXT_NONE); \ + src1_h = get_gpr(ctx, (a->rs1) * 2 + 1, EXT_NONE); \ + } \ + gen_helper_##NAME(dest, tcg_env, src1_l, src1_h, src2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_REG_PAIR_PREDSUM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv shamt = tcg_constant_tl(a->imm); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, shamt); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_IMM(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +#if defined(TARGET_RISCV32) +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + REQUIRE_EXT(ctx, RVP); \ + TCGv_i64 s1 = tcg_temp_new_i64(); \ + if (a->rs1 == 0) { \ + tcg_gen_mov_i64(s1, 0); \ + } else { \ + get_pair_regs(ctx, s1, a->rs1 * 2); \ + } \ + TCGv_i32 rs2 = get_gpr(ctx, a->rs2, EXT_NONE); \ + TCGv_i32 dest = dest_gpr(ctx, a->rd); \ + gen_helper_##NAME(dest, tcg_env, s1, rs2); \ + return true; \ +} +#else +#define GEN_SIMD_TRANS_PN_OP_REG(NAME) \ +static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ +{ \ + REQUIRE_32BIT(ctx); \ + return true; \ +} +#endif + +GEN_SIMD_TRANS(padd_b) +GEN_SIMD_TRANS(padd_h) +GEN_SIMD_TRANS_64(padd_w) +GEN_SIMD_TRANS(padd_bs) +GEN_SIMD_TRANS(padd_hs) +GEN_SIMD_TRANS_64(padd_ws) +GEN_SIMD_TRANS(psub_b) +GEN_SIMD_TRANS(psub_h) +GEN_SIMD_TRANS_64(psub_w) +GEN_SIMD_TRANS(psh1add_h) +GEN_SIMD_TRANS_64(psh1add_w) +GEN_SIMD_TRANS(pssh1sadd_h) +GEN_SIMD_TRANS_64(pssh1sadd_w) +GEN_SIMD_TRANS_32(ssh1sadd) +GEN_SIMD_TRANS(psadd_b) +GEN_SIMD_TRANS(psadd_h) +GEN_SIMD_TRANS_64(psadd_w) +GEN_SIMD_TRANS(psaddu_b) +GEN_SIMD_TRANS(psaddu_h) +GEN_SIMD_TRANS_64(psaddu_w) +GEN_SIMD_TRANS_32(sadd) +GEN_SIMD_TRANS_32(saddu) +GEN_SIMD_TRANS(pssub_b) +GEN_SIMD_TRANS(pssub_h) +GEN_SIMD_TRANS_64(pssub_w) +GEN_SIMD_TRANS(pssubu_b) +GEN_SIMD_TRANS(pssubu_h) +GEN_SIMD_TRANS_64(pssubu_w) +GEN_SIMD_TRANS_32(ssub) +GEN_SIMD_TRANS_32(ssubu) +GEN_SIMD_TRANS_IMM(psati_h) +GEN_SIMD_TRANS_IMM(pusati_h) +GEN_SIMD_TRANS_IMM_64(psati_w) +GEN_SIMD_TRANS_IMM_64(pusati_w) +GEN_SIMD_TRANS_IMM_32(sati_32) +GEN_SIMD_TRANS_IMM_32(usati_32) +GEN_SIMD_TRANS_IMM_64(sati_64) +GEN_SIMD_TRANS_IMM_64(usati_64)target/riscv/meson.build +2
-1@@ -28,7 +28,8 @@ riscv_ss.add(files( 'm128_helper.c', 'crypto_helper.c', 'zce_helper.c', - 'vcrypto_helper.c' + 'vcrypto_helper.c', + 'psimd_helper.c' )) riscv_system_ss = ss.source_set()target/riscv/psimd_helper.c new file +141
-0@@ -0,0 +1,1069 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* RISC-V Packed SIMD Extension Helpers for QEMU. */ +/* Copyright (C) 2026 ISRC ISCAS. */ + +#include "qemu/osdep.h" +#include "cpu.h" +#include "qemu/host-utils.h" +#include "exec/helper-proto.h" +#include "fpu/softfloat.h" +#include "internals.h" + + +/* Helper macros */ + +/* Element count calculations */ +#define ELEMS_B (target) (sizeof(target) * 8 / 8)/* byte elements count */ +#define ELEMS_H (target) (sizeof(target) * 8 / 16) +#define ELEMS_W (target) (sizeof(target) * 8 / 32)/* word elements count */ + +/* Element extraction macros - unsigned to avoid sign extension */ +#define EXTRACT8 (val, idx) (((val) >> ((idx) * 8)) & 0xFF) +#define EXTRACT16 (val, idx) (((val) >> ((idx) * 16)) & 0xFFFF) +#define EXTRACT32 (val, idx) (((val) >> ((idx) * 32)) & 0xFFFFFFFF) + +/* Element insertion macros */ +#define INSERT8 (val, res, idx) \ + ((val) | ((target_ulong)(uint8_t)(res) << ((idx) * 8))) +#define INSERT16 (val, res, idx) \ + ((val) | ((target_ulong)(uint16_t)(res) << ((idx) * 16))) +#define INSERT32 (val, res, idx) \ + ((val) | ((target_ulong)(uint32_t)(res) << ((idx) * 32))) + +/* Saturation constants */ +static const int8_t SAT_MAX_B =127 ; +static const int8_t SAT_MIN_B =-128 ; +static const int16_t SAT_MAX_H =32767 ; +static const int16_t SAT_MIN_H =-32768 ; +static const int32_t SAT_MAX_W =2147483647 ; +static const int32_t SAT_MIN_W =-2147483648LL ; +static const uint8_t USAT_MAX_B =255 ; +static const uint16_t USAT_MAX_H =65535 ; +static const uint32_t USAT_MAX_W =4294967295U ; + + +/* Saturation helper functions */ + +/** +* Signed saturation for 8-bit elements +* Returns saturated value and sets *sat if saturation occurred +*/ +static inline int8_t signed_saturate_b (int32_t val,int *sat) +{ +if (val >SAT_MAX_B ) { + *sat =1 ; +return SAT_MAX_B ; + } +if (val <SAT_MIN_B ) { + *sat =1 ; +return SAT_MIN_B ; + } +return (int8_t )val; +} + +/** +* Signed saturation for 16-bit elements +*/ +static inline int16_t signed_saturate_h (int32_t val,int *sat) +{ +if (val >SAT_MAX_H ) { + *sat =1 ; +return SAT_MAX_H ; + } +if (val <SAT_MIN_H ) { + *sat =1 ; +return SAT_MIN_H ; + } +return (int16_t )val; +} + +/** +* Signed saturation for 32-bit elements +*/ +static inline int32_t signed_saturate_w (int64_t val,int *sat) +{ +if (val >SAT_MAX_W ) { + *sat =1 ; +return SAT_MAX_W ; + } +if (val <SAT_MIN_W ) { + *sat =1 ; +return SAT_MIN_W ; + } +return (int32_t )val; +} + +/** +* Unsigned saturation for 8-bit elements +*/ +static inline uint8_t unsigned_saturate_b (uint32_t val,int *sat) +{ +if (val >USAT_MAX_B ) { + *sat =1 ; +return USAT_MAX_B ; + } +return (uint8_t )val; +} + +/** +* Unsigned saturation for 16-bit elements +*/ +static inline uint16_t unsigned_saturate_h (uint32_t val,int *sat) +{ +if (val >USAT_MAX_H ) { + *sat =1 ; +return USAT_MAX_H ; + } +return (uint16_t )val; +} + +/** +* Unsigned saturation for 32-bit elements +*/ +static inline uint32_t unsigned_saturate_w (uint64_t val,int *sat) +{ +if (val >USAT_MAX_W ) { + *sat =1 ; +return USAT_MAX_W ; + } +return (uint32_t )val; +} + +/* Basic addition operations (non-saturating) */ + +/** +* PADD.B - Packed 8-bit addition +* For each byte: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ;target/riscv/psimd_helper.c line 151
Quoted 10 lines
target/riscv/psimd_helper.c new file +10
-0+int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +}If we don't care about performance in the early patchset, but considering maintainability, I'd suggest providing unified helper macros for these operations.
The current SIMD implementations all share a similar loop structure, differing only in element types. We could follow the approach used in
vector_helper.cand define a few groups of helper macros to reduce the boilerplate.e.g.
``` #define DO_ADD(a, b) ((a) + (b)) #define DO_SUB(a, b) ((a) - (b)) #define GEN_PSIMD_VV_B(NAME, OP) \ target_ulong HELPER(NAME)(CPURISCVState *env, \ target_ulong rs1, target_ulong rs2) \ { \ target_ulong rd = 0; \ for (int i = 0; i < ELEMS_B(rd); i++) { \ uint8_t e1 = EXTRACT8(rs1, i); \ uint8_t e2 = EXTRACT8(rs2, i); \ rd = INSERT8(rd, (uint8_t)OP(e1, e2), i); \ } \ return rd; \ } GEN_PSIMD_VV_B(padd_b, DO_ADD) GEN_PSIMD_VV_B(psub_b, DO_SUB) ```
Quoted 5 lines
The same applies to the helper functions for other instructions as well.
Thanks,
Chao
Thank you for the detailed and constructive suggestion. I agree that using unified helper macros is better for maintainability and conciseness. I'll refactor the code following the vector_helper.capproach, using helper macros for SIMD operations. This will be addressed in v2.
Thanks again for the guidance!
Best regards, Molly
Quoted 933 lines
Quoted 933 lines
target/riscv/psimd_helper.c new file +918
-0+ +/** +* PADD.H - Packed 16-bit addition +* For each halfword: rd[i] = rs1[i] + rs2[i] (modular) +*/ +target_ulong HELPER (padd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.W - Packed 32-bit addition (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[i] (modular) +*/ +uint64_t HELPER (padd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ;/* 2 words in 64-bit */ + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PADD.BS - Packed 8-bit addition with scalar second operand +* For each byte: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_bs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +uint8_t e2 =EXTRACT8 (rs2,0 );/* Scalar, take least significant byte */ + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t res = e1 + e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PADD.HS - Packed 16-bit addition with scalar second operand +* For each halfword: rd[i] = rs1[i] + rs2[0] (modular) +*/ +target_ulong HELPER (padd_hs )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint16_t e2 =EXTRACT16 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t res = e1 + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PADD.WS - Packed 32-bit addition with scalar second operand (RV64 only) +* For each word: rd[i] = rs1[i] + rs2[0] (modular) +*/ +uint64_t HELPER (padd_ws )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +uint32_t e2 =EXTRACT32 (rs2,0 ); + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t res = e1 + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + + +/* Basic subtraction operations (non-saturating) */ + +/** +* PSUB.B - Packed 8-bit subtraction +* For each byte: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint8_t res = e1 - e2; + rd =INSERT8 (rd, res, i); + } +return rd; +} + +/** +* PSUB.H - Packed 16-bit subtraction +* For each halfword: rd[i] = rs1[i] - rs2[i] (modular) +*/ +target_ulong HELPER (psub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = e1 - e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSUB.W - Packed 32-bit subtraction (RV64 only) +* For each word: rd[i] = rs1[i] - rs2[i] (modular) +*/ +uint64_t HELPER (psub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = e1 - e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/* Shift-left-by-one and add operations */ + +/** +* PSH1ADD.H - Shift left by 1 and add (16-bit) +* For each halfword: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +target_ulong HELPER (psh1add_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint16_t res = (e1 <<1 ) + e2; + rd =INSERT16 (rd, res, i); + } +return rd; +} + +/** +* PSH1ADD.W - Shift left by 1 and add (32-bit, RV64 only) +* For each word: rd[i] = (rs1[i] << 1) + rs2[i] +*/ +uint64_t HELPER (psh1add_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 <<1 ) + e2; + rd =INSERT32 (rd, res, i); + } +return rd; +} + +/** +* PSSH1SADD.H - Saturating shift left by 1 and saturating add (16-bit) +* For each halfword: rd[i] = sat16(sat16(rs1[i] << 1) + rs2[i]) +*/ +target_ulong HELPER (pssh1sadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFF || e1 <-0x4000 ) { + shifted = (e1 <0 ) ?0xFFFF8000LL :0x7FFF ; + sat =1 ; + }else { + shifted = e1 <<1 ; + } + +int32_t sum = shifted + e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSH1SADD.W - Saturating shift left by 1 and add +* with saturation (32-bit, RV64 only) +* For each word: rd[i] = sat32(sat32(rs1[i] << 1) + rs2[i]) +*/ +uint64_t HELPER (pssh1sadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t shifted; + +/* Check if shift-left-1 would overflow */ +if (e1 >0x3FFFFFFF || e1 <-0x40000000 ) { + shifted = (e1 <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )e1 <<1 ; + } + +int64_t sum = shifted + e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSH1SADD - 32-bit scalar saturating shift left by 1 and saturating add +*/ +uint32_t HELPER (ssh1sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t shifted; +int sat =0 ; + +/* Check if shift-left-1 would overflow */ +if (a >0x3FFFFFFF || a <-0x40000000 ) { + shifted = (a <0 ) ?0xFFFFFFFF80000000LL :0x7FFFFFFF ; + sat =1 ; + }else { + shifted = (int64_t )a <<1 ; + } + +int64_t sum = shifted + b; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/* Saturating addition operations */ + +/** +* PSADD.B - Packed 8-bit signed saturating addition +* For each byte: rd[i] = sat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int8_t res =signed_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.H - Packed 16-bit signed saturating addition +* For each halfword: rd[i] = sat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psadd_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t sum = (int32_t )e1 + (int32_t )e2; +int16_t res =signed_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADD.W - Packed 32-bit signed saturating addition (RV64 only) +* For each word: rd[i] = sat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psadd_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t sum = (int64_t )e1 + (int64_t )e2; +int32_t res =signed_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.B - Packed 8-bit unsigned saturating addition +* For each byte: rd[i] = usat8(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint8_t res =unsigned_saturate_b (sum, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.H - Packed 16-bit unsigned saturating addition +* For each halfword: rd[i] = usat16(rs1[i] + rs2[i]) +*/ +target_ulong HELPER (psaddu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t sum = (uint32_t )e1 + (uint32_t )e2; +uint16_t res =unsigned_saturate_h (sum, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSADDU.W - Packed 32-bit unsigned saturating addition (RV64 only) +* For each word: rd[i] = usat32(rs1[i] + rs2[i]) +*/ +uint64_t HELPER (psaddu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint64_t sum = (uint64_t )e1 + (uint64_t )e2; +uint32_t res =unsigned_saturate_w (sum, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SADD - 32-bit signed saturating addition +*/ +uint32_t HELPER (sadd )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t sum = (int64_t )a + (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SADDU - 32-bit unsigned saturating addition +*/ +uint32_t HELPER (saddu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t sum = (uint64_t )a + (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (sum, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturating subtraction operations */ + +/** +* PSSUB.B - Packed 8-bit signed saturating subtraction +* For each byte: rd[i] = sat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int8_t e1 = (int8_t )EXTRACT8 (rs1, i); +int8_t e2 = (int8_t )EXTRACT8 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int8_t res =signed_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.H - Packed 16-bit signed saturating subtraction +* For each halfword: rd[i] = sat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssub_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t e2 = (int16_t )EXTRACT16 (rs2, i); +int32_t diff = (int32_t )e1 - (int32_t )e2; +int16_t res =signed_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUB.W - Packed 32-bit signed saturating subtraction (RV64 only) +* For each word: rd[i] = sat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssub_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t e2 = (int32_t )EXTRACT32 (rs2, i); +int64_t diff = (int64_t )e1 - (int64_t )e2; +int32_t res =signed_saturate_w (diff, &sat); + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.B - Packed 8-bit unsigned saturating subtraction +* For each byte: rd[i] = usat8(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_b )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_B (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint8_t e1 =EXTRACT8 (rs1, i); +uint8_t e2 =EXTRACT8 (rs2, i); +uint32_t diff = e1 - e2;/* Unsigned subtraction may underflow */ +uint8_t res =unsigned_saturate_b (diff, &sat); + rd =INSERT8 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.H - Packed 16-bit unsigned saturating subtraction +* For each halfword: rd[i] = usat16(rs1[i] - rs2[i]) +*/ +target_ulong HELPER (pssubu_h )(CPURISCVState *env, +target_ulong rs1,target_ulong rs2) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint16_t e1 =EXTRACT16 (rs1, i); +uint16_t e2 =EXTRACT16 (rs2, i); +uint32_t diff = e1 - e2; +uint16_t res =unsigned_saturate_h (diff, &sat); + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSSUBU.W - Packed 32-bit unsigned saturating subtraction (RV64 only) +* For each word: rd[i] = usat32(rs1[i] - rs2[i]) +*/ +uint64_t HELPER (pssubu_w )(CPURISCVState *env, +uint64_t rs1,uint64_t rs2) +{ +uint64_t rd =0 ; +int elems =2 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +uint32_t e1 =EXTRACT32 (rs1, i); +uint32_t e2 =EXTRACT32 (rs2, i); +uint32_t res = (e1 >= e2) ? (e1 - e2) :0 ; +if (e1 < e2) { + sat =1 ; + } + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SSUB - 32-bit signed saturating subtraction +*/ +uint32_t HELPER (ssub )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +int32_t a = (int32_t )rs1; +int32_t b = (int32_t )rs2; +int64_t diff = (int64_t )a - (int64_t )b; +int sat =0 ; +int32_t res =signed_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )res; +} + +/** +* SSUBU - 32-bit unsigned saturating subtraction +*/ +uint32_t HELPER (ssubu )(CPURISCVState *env, +uint32_t rs1,uint32_t rs2) +{ +uint32_t a = rs1; +uint32_t b = rs2; +uint64_t diff = (uint64_t )a - (uint64_t )b; +int sat =0 ; +uint32_t res =unsigned_saturate_w (diff, &sat); + +if (sat) { + env->vxsat =1 ; + } +return res; +} + +/* Saturation instructions (SAT, USAT) */ + +/** +* PSATI.H - Packed 16-bit signed saturate to immediate bit-width +* For each halfword: rd[i] = sat(rs1[i], imm+1 bits) +*/ +target_ulong HELPER (psati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +int range = (imm &0x0F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.H - Packed 16-bit unsigned saturate to immediate bit-width +* For each halfword: rd[i] = usat(rs1[i], imm bits) +*/ +target_ulong HELPER (pusati_h )(CPURISCVState *env, +target_ulong rs1,target_ulong imm) +{ +target_ulong rd =0 ; +int elems =ELEMS_H (rd); +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int16_t e1 = (int16_t )EXTRACT16 (rs1, i); +int16_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint16_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT16 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PSATI.W - Packed 32-bit signed saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = sat(rs1[i], imm+1 bits) +*/ +uint64_t HELPER (psati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +int range = (imm &0x1F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 > max) { + res = max; + sat =1 ; + }else if (e1 < min) { + res = min; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* PUSATI.W - Packed 32-bit unsigned saturate to immediate bit-width (RV64 only) +* For each word: rd[i] = usat(rs1[i], imm bits) +*/ +uint64_t HELPER (pusati_w )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +uint64_t rd =0 ; +int elems =2 ; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +for (int i =0 ; i < elems; i++) { +int32_t e1 = (int32_t )EXTRACT32 (rs1, i); +int32_t res; + +if (e1 <0 ) { + res =0 ; + sat =1 ; + }else if ((uint32_t )e1 > max) { + res = max; + sat =1 ; + }else { + res = e1; + } + + rd =INSERT32 (rd, res, i); + } + +if (sat) { + env->vxsat =1 ; + } +return rd; +} + +/** +* SATI_32 - 32-bit scalar signed saturation with immediate range +*/ +uint32_t HELPER (sati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +int range = (imm &0x1F ) +1 ;/* imm specifies bits-1 */ +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* USATI_32 - 32-bit scalar unsigned saturation with immediate range +*/ +uint32_t HELPER (usati_32 )(CPURISCVState *env, +uint32_t rs1,uint32_t imm) +{ +int32_t a = (int32_t )rs1; +uint32_t max = (1U << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint32_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint32_t )a; +} + +/** +* SATI_64 - 64-bit scalar signed saturation with immediate range +*/ +uint64_t HELPER (sati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +int range = (imm &0x3F ) +1 ; +int64_t max = (1LL << (range -1 )) -1 ; +int64_t min = -(1LL << (range -1 )); +int sat =0 ; + +if (a > max) { + a = max; + sat =1 ; + }else if (a < min) { + a = min; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +} + +/** +* USATI_64 - 64-bit scalar unsigned saturation with immediate range +*/ +uint64_t HELPER (usati_64 )(CPURISCVState *env, +uint64_t rs1,uint64_t imm) +{ +int64_t a = (int64_t )rs1; +uint64_t max = (1ULL << imm) -1 ; +int sat =0 ; + +if (a <0 ) { + a =0 ; + sat =1 ; + }else if ((uint64_t )a > max) { + a = max; + sat =1 ; + } + +if (sat) { + env->vxsat =1 ; + } +return (uint64_t )a; +}target/riscv/translate.c +1
-0@@ -1206,6 +1206,7 @@ static uint32_t opcode_at(DisasContextBase *dcbase, target_ulong pc)#include "insn_trans/trans_rvh.c.inc" #include "insn_trans/trans_rvv.c.inc" #include "insn_trans/trans_rvb.c.inc" +#include "insn_trans/trans_rvp.c.inc" #include "insn_trans/trans_rvzicond.c.inc" #include "insn_trans/trans_rvzacas.c.inc" #include "insn_trans/trans_rvzabha.c.inc" Signature · 1 line
2.34.1
Re: [PATCH 02/14] target/riscv: rvp: add arithmetic instructions,including saturating and non-saturating operations
target/riscv/psimd_helper.c line 42
Quoted 10 lines · Molly Chen
target/riscv/psimd_helper.c new file +10
-0+/* Saturation constants */ +static const int8_t SAT_MAX_B =127 ; +static const int8_t SAT_MIN_B =-128 ; +static const int16_t SAT_MAX_H =32767 ; +static const int16_t SAT_MIN_H =-32768 ; +static const int32_t SAT_MAX_W =2147483647 ; +static const int32_t SAT_MIN_W =-2147483648LL ; +static const uint8_t USAT_MAX_B =255 ; +static const uint16_t USAT_MAX_H =65535 ; +static const uint32_t USAT_MAX_W =4294967295U ;
This is not C++: static const variables still consume space in the data segment. You're better off with #define.
r~
PATCH 3/14 target/riscv: rvp: add averaging operations
In reply to Molly Chen--- target/riscv/helper.h | 20 ++ target/riscv/insn32.decode | 28 ++- target/riscv/insn_trans/trans_rvp.c.inc | 20 ++ target/riscv/psimd_helper.c | 266 ++++++++++++++++++++++++ 4 files changed, 333 insertions(+), 1 deletion(-)
target/riscv/helper.h +20 -0
@@ -1353,6 +1353,7 @@ DEF_HELPER_1(ssamoswap_disabled, void, env)#endif /* Packed SIMD */ +/* Packed SIMD - Arithmetic Operations(Non-Saturating and Saturating) */ DEF_HELPER_3(padd_b, tl, env, tl, tl) DEF_HELPER_3(padd_h, tl, env, tl, tl)DEF_HELPER_3 (padd_w, i64, env, i64, i64)
@@ -1391,3 +1392,22 @@ DEF_HELPER_3(sati_32, i32, env, i32, i32) DEF_HELPER_3(usati_32, i32, env, i32, i32) DEF_HELPER_3(sati_64, i64, env, i64, i64)DEF_HELPER_3 (usati_64, i64, env, i64, i64) + +/* Packed SIMD - Averaging and Rounding Operations */ +DEF_HELPER_3(paadd_b, tl, env, tl, tl) +DEF_HELPER_3(paadd_h, tl, env, tl, tl) +DEF_HELPER_3(paadd_w, i64, env, i64, i64) +DEF_HELPER_3(paaddu_b, tl, env, tl, tl) +DEF_HELPER_3(paaddu_h, tl, env, tl, tl) +DEF_HELPER_3(paaddu_w, i64, env, i64, i64) +DEF_HELPER_3(aadd, i32, env, i32, i32) +DEF_HELPER_3(aaddu, i32, env, i32, i32) +DEF_HELPER_3(pasub_b, tl, env, tl, tl) +DEF_HELPER_3(pasub_h, tl, env, tl, tl) +DEF_HELPER_3(pasub_w, i64, env, i64, i64) +DEF_HELPER_3(pasubu_b, tl, env, tl, tl) +DEF_HELPER_3(pasubu_h, tl, env, tl, tl) +DEF_HELPER_3(pasubu_w, i64, env, i64, i64) +DEF_HELPER_3(asub, i32, env, i32, i32) +DEF_HELPER_3 (asubu, i32, env, i32, i32) +
target/riscv/insn32.decode +27 -1
@@ -1094,7 +1094,7 @@ sd_aqrl 00111 . . ..... ..... 011 ..... 0101111 @atom_st # *** P Experimental Extension Version v018 *** -# Arithmetic Operations(Non-Saturating and Saturating) +# Packed SIMD - Arithmetic Operations(Non-Saturating and Saturating) padd_b 1000010 ..... ..... 000 ..... 0111011 @r padd_h 1000000 ..... ..... 000 ..... 0111011 @r padd_w 1000001 ..... ..... 000 ..... 0111011 @r
@@ -1149,3 +1149,29 @@ pusati_h 10100 001.... ..... 100 ..... 0011011 @p_ui16 sati_64 111001 ...... ..... 100 ..... 0011011 @p_ui64 usati_64 101001 ...... ..... 100 ..... 0011011 @p_ui64 +# Packed SIMD - Averaging and Rounding Operations +paadd_b 1001110 ..... ..... 000 ..... 0111011 @r +paadd_h 1001100 ..... ..... 000 ..... 0111011 @r +{ + aadd 1001101 ..... ..... 000 ..... 0111011 @r + paadd_w 1001101 ..... ..... 000 ..... 0111011 @r +} +paaddu_b 1011110 ..... ..... 000 ..... 0111011 @r +paaddu_h 1011100 ..... ..... 000 ..... 0111011 @r +{ + aaddu 1011101 ..... ..... 000 ..... 0111011 @r + paaddu_w 1011101 ..... ..... 000 ..... 0111011 @r +} +pasub_b 1101110 ..... ..... 000 ..... 0111011 @r +pasub_h 1101100 ..... ..... 000 ..... 0111011 @r +{ + asub 1101101 ..... ..... 000 ..... 0111011 @r + pasub_w 1101101 ..... ..... 000 ..... 0111011 @r +} +pasubu_b 1111110 ..... ..... 000 ..... 0111011 @r +pasubu_h 1111100 ..... ..... 000 ..... 0111011 @r +{ + asubu 1111101 ..... ..... 000 ..... 0111011 @r + pasubu_w 1111101 ..... ..... 000 ..... 0111011 @r +} +
target/riscv/insn_trans/trans_rvp.c.inc +20 -0
@@ -524,6 +524,7 @@ static bool trans_##NAME(DisasContext *ctx, arg_##NAME * a) \ } #endif +/* Packed SIMD - Arithmetic Operations(Non-Saturating and Saturating) */ GEN_SIMD_TRANS(padd_b) GEN_SIMD_TRANS(padd_h) GEN_SIMD_TRANS_64(padd_w)
@@ -562,3 +563,22 @@ GEN_SIMD_TRANS_IMM_32(sati_32) GEN_SIMD_TRANS_IMM_32(usati_32) GEN_SIMD_TRANS_IMM_64(sati_64) GEN_SIMD_TRANS_IMM_64(usati_64) + +/* Packed SIMD - Averaging and Rounding Operations */ +GEN_SIMD_TRANS(paadd_b) +GEN_SIMD_TRANS(paadd_h) +GEN_SIMD_TRANS_64(paadd_w) +GEN_SIMD_TRANS(paaddu_b) +GEN_SIMD_TRANS(paaddu_h) +GEN_SIMD_TRANS_64(paaddu_w) +GEN_SIMD_TRANS_32(aadd) +GEN_SIMD_TRANS_32(aaddu) +GEN_SIMD_TRANS(pasub_b) +GEN_SIMD_TRANS(pasub_h) +GEN_SIMD_TRANS_64(pasub_w) +GEN_SIMD_TRANS(pasubu_b) +GEN_SIMD_TRANS(pasubu_h) +GEN_SIMD_TRANS_64(pasubu_w) +GEN_SIMD_TRANS_32(asub) +GEN_SIMD_TRANS_32(asubu) +
target/riscv/psimd_helper.c +266 -0
269 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 4/14 target/riscv: rvp: add absolute value and difference, comparison and mask generation operations
In reply to Molly Chen--- target/riscv/helper.h | 38 ++ target/riscv/insn32.decode | 44 ++ target/riscv/insn_trans/trans_rvp.c.inc | 38 ++ target/riscv/psimd_helper.c | 634 ++++++++++++++++++++++++ 4 files changed, 754 insertions(+)
target/riscv/helper.h +38 -0
@@ -1411,3 +1411,41 @@ DEF_HELPER_3(pasubu_w, i64, env, i64, i64) DEF_HELPER_3(asub, i32, env, i32, i32)DEF_HELPER_3 (asubu, i32, env, i32, i32) +/* Packed SIMD - Absolute Value and Difference Operations */ +DEF_HELPER_2(psabs_b, tl, env, tl) +DEF_HELPER_2(psabs_h, tl, env, tl) +DEF_HELPER_2(abs, tl, env, tl) +DEF_HELPER_2(absw, i64, env, i64) +DEF_HELPER_3(pabd_b, tl, env, tl, tl) +DEF_HELPER_3(pabdu_b, tl, env, tl, tl) +DEF_HELPER_3(pabd_h, tl, env, tl, tl) +DEF_HELPER_3(pabdu_h, tl, env, tl, tl) +DEF_HELPER_3(pabdsumu_b, tl, env, tl, tl) +DEF_HELPER_4 (pabdsumau_b, tl, env, tl, tl, tl) + +/* Packed SIMD - Comparison and Mask Generation Operations */ +DEF_HELPER_3(pmseq_b, tl, env, tl, tl) +DEF_HELPER_3(pmslt_b, tl, env, tl, tl) +DEF_HELPER_3(pmsltu_b, tl, env, tl, tl) +DEF_HELPER_3(pmin_b, tl, env, tl, tl) +DEF_HELPER_3(pminu_b, tl, env, tl, tl) +DEF_HELPER_3(pmax_b, tl, env, tl, tl) +DEF_HELPER_3(pmaxu_b, tl, env, tl, tl) +DEF_HELPER_3(pmseq_h, tl, env, tl, tl) +DEF_HELPER_3(pmslt_h, tl, env, tl, tl) +DEF_HELPER_3(pmsltu_h, tl, env, tl, tl) +DEF_HELPER_3(pmin_h, tl, env, tl, tl) +DEF_HELPER_3(pminu_h, tl, env, tl, tl) +DEF_HELPER_3(pmax_h, tl, env, tl, tl) +DEF_HELPER_3(pmaxu_h, tl, env, tl, tl) +DEF_HELPER_3(pmseq_w, i64, env, i64, i64) +DEF_HELPER_3(pmslt_w, i64, env, i64, i64) +DEF_HELPER_3(pmsltu_w, i64, env, i64, i64) +DEF_HELPER_3(pmin_w, i64, env, i64, i64) +DEF_HELPER_3(pminu_w, i64, env, i64, i64) +DEF_HELPER_3(pmax_w, i64, env, i64, i64) +DEF_HELPER_3(pmaxu_w, i64, env, i64, i64) +DEF_HELPER_3(mseq, i32, env, i32, i32) +DEF_HELPER_3(mslt, i32, env, i32, i32) +DEF_HELPER_3 (msltu, i32, env, i32, i32) +
target/riscv/insn32.decode +44 -0
47 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +38 -0
@@ -582,3 +582,41 @@ GEN_SIMD_TRANS_64(pasubu_w) GEN_SIMD_TRANS_32(asub) GEN_SIMD_TRANS_32(asubu) +/* Packed SIMD - Absolute Value and Difference Operations */ +GEN_SIMD_TRANS_R1(psabs_b) +GEN_SIMD_TRANS_R1(psabs_h) +GEN_SIMD_TRANS_R1(abs) +GEN_SIMD_TRANS_R1_64(absw) +GEN_SIMD_TRANS(pabd_b) +GEN_SIMD_TRANS(pabdu_b) +GEN_SIMD_TRANS(pabd_h) +GEN_SIMD_TRANS(pabdu_h) +GEN_SIMD_TRANS(pabdsumu_b) +GEN_SIMD_TRANS_ACC(pabdsumau_b) + +/* Packed SIMD - Comparison and Mask Generation Operations */ +GEN_SIMD_TRANS(pmseq_b) +GEN_SIMD_TRANS(pmslt_b) +GEN_SIMD_TRANS(pmsltu_b) +GEN_SIMD_TRANS(pmin_b) +GEN_SIMD_TRANS(pminu_b) +GEN_SIMD_TRANS(pmax_b) +GEN_SIMD_TRANS(pmaxu_b) +GEN_SIMD_TRANS(pmseq_h) +GEN_SIMD_TRANS(pmslt_h) +GEN_SIMD_TRANS(pmsltu_h) +GEN_SIMD_TRANS(pmin_h) +GEN_SIMD_TRANS(pminu_h) +GEN_SIMD_TRANS(pmax_h) +GEN_SIMD_TRANS(pmaxu_h) +GEN_SIMD_TRANS_64(pmseq_w) +GEN_SIMD_TRANS_64(pmslt_w) +GEN_SIMD_TRANS_64(pmsltu_w) +GEN_SIMD_TRANS_64(pmin_w) +GEN_SIMD_TRANS_64(pminu_w) +GEN_SIMD_TRANS_64(pmax_w) +GEN_SIMD_TRANS_64(pmaxu_w) +GEN_SIMD_TRANS_32(mseq) +GEN_SIMD_TRANS_32(mslt) +GEN_SIMD_TRANS_32(msltu) +
target/riscv/psimd_helper.c +634 -0
637 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 5/14 target/riscv: rvp: add shift operations
In reply to Molly Chen--- target/riscv/helper.h | 34 ++ target/riscv/insn32.decode | 44 ++ target/riscv/insn_trans/trans_rvp.c.inc | 34 ++ target/riscv/psimd_helper.c | 736 ++++++++++++++++++++++++ 4 files changed, 848 insertions(+)
target/riscv/helper.h +34 -0
@@ -1449,3 +1449,37 @@ DEF_HELPER_3(mseq, i32, env, i32, i32) DEF_HELPER_3(mslt, i32, env, i32, i32)DEF_HELPER_3 (msltu, i32, env, i32, i32) +/* Packed SIMD - Shift Operations */ +DEF_HELPER_3(pslli_b, tl, env, tl, tl) +DEF_HELPER_3(psll_bs, tl, env, tl, tl) +DEF_HELPER_3(pslli_h, tl, env, tl, tl) +DEF_HELPER_3(psll_hs, tl, env, tl, tl) +DEF_HELPER_3(pslli_w, i64, env, i64, i64) +DEF_HELPER_3(psll_ws, i64, env, i64, i64) +DEF_HELPER_3(psrli_b, tl, env, tl, tl) +DEF_HELPER_3(psrl_bs, tl, env, tl, tl) +DEF_HELPER_3(psrli_h, tl, env, tl, tl) +DEF_HELPER_3(psrl_hs, tl, env, tl, tl) +DEF_HELPER_3(psrli_w, i64, env, i64, i64) +DEF_HELPER_3(psrl_ws, i64, env, i64, i64) +DEF_HELPER_3(psrai_b, tl, env, tl, tl) +DEF_HELPER_3(psra_bs, tl, env, tl, tl) +DEF_HELPER_3(psrai_h, tl, env, tl, tl) +DEF_HELPER_3(psra_hs, tl, env, tl, tl) +DEF_HELPER_3(psrai_w, i64, env, i64, i64) +DEF_HELPER_3(psra_ws, i64, env, i64, i64) +DEF_HELPER_3(psslai_h, tl, env, tl, tl) +DEF_HELPER_3(psslai_w, i64, env, i64, i64) +DEF_HELPER_3(sslai, i32, env, i32, i32) +DEF_HELPER_3(psrari_h, tl, env, tl, tl) +DEF_HELPER_3(psrari_w, i64, env, i64, i64) +DEF_HELPER_3(srari_32, i32, env, i32, i32) +DEF_HELPER_3(srari_64, i64, env, i64, i64) +DEF_HELPER_3(pssha_hs, tl, env, tl, tl) +DEF_HELPER_3(pssha_ws, i64, env, i64, i64) +DEF_HELPER_3(psshar_hs, tl, env, tl, tl) +DEF_HELPER_3(psshar_ws, i64, env, i64, i64) +DEF_HELPER_3(ssha, i32, env, i32, i32) +DEF_HELPER_3(sshar, i32, env, i32, i32) +DEF_HELPER_3(sha, i64, env, i64, i64) +DEF_HELPER_3 (shar, i64, env, i64, i64)
target/riscv/insn32.decode +44 -0
59 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +34 -0
@@ -620,3 +620,37 @@ GEN_SIMD_TRANS_32(mseq) GEN_SIMD_TRANS_32(mslt) GEN_SIMD_TRANS_32(msltu) +/* Packed SIMD - Shift Operations */ +GEN_SIMD_TRANS_IMM(pslli_b) +GEN_SIMD_TRANS(psll_bs) +GEN_SIMD_TRANS_IMM(pslli_h) +GEN_SIMD_TRANS(psll_hs) +GEN_SIMD_TRANS_IMM_64(pslli_w) +GEN_SIMD_TRANS_64(psll_ws) +GEN_SIMD_TRANS_IMM(psrli_b) +GEN_SIMD_TRANS(psrl_bs) +GEN_SIMD_TRANS_IMM(psrli_h) +GEN_SIMD_TRANS(psrl_hs) +GEN_SIMD_TRANS_IMM_64(psrli_w) +GEN_SIMD_TRANS_64(psrl_ws) +GEN_SIMD_TRANS_IMM(psrai_b) +GEN_SIMD_TRANS(psra_bs) +GEN_SIMD_TRANS_IMM(psrai_h) +GEN_SIMD_TRANS(psra_hs) +GEN_SIMD_TRANS_IMM_64(psrai_w) +GEN_SIMD_TRANS_64(psra_ws) +GEN_SIMD_TRANS_IMM(psslai_h) +GEN_SIMD_TRANS_IMM_64(psslai_w) +GEN_SIMD_TRANS_IMM_32(sslai) +GEN_SIMD_TRANS_IMM(psrari_h) +GEN_SIMD_TRANS_IMM_64(psrari_w) +GEN_SIMD_TRANS_IMM_32(srari_32) +GEN_SIMD_TRANS_IMM_64(srari_64) +GEN_SIMD_TRANS(pssha_hs) +GEN_SIMD_TRANS_64(pssha_ws) +GEN_SIMD_TRANS(psshar_hs) +GEN_SIMD_TRANS_64(psshar_ws) +GEN_SIMD_TRANS_32(ssha) +GEN_SIMD_TRANS_32(sshar) +GEN_SIMD_TRANS_64(sha) +GEN_SIMD_TRANS_64(shar)
target/riscv/psimd_helper.c +736 -0
739 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 6/14 target/riscv: rvp: add exchange operations
In reply to Molly Chen--- target/riscv/helper.h | 14 ++ target/riscv/insn32.decode | 14 ++ target/riscv/insn_trans/trans_rvp.c.inc | 14 ++ target/riscv/psimd_helper.c | 294 ++++++++++++++++++++++++ 4 files changed, 336 insertions(+)
target/riscv/helper.h +14 -0
@@ -1483,3 +1483,17 @@ DEF_HELPER_3(ssha, i32, env, i32, i32) DEF_HELPER_3(sshar, i32, env, i32, i32) DEF_HELPER_3(sha, i64, env, i64, i64)DEF_HELPER_3 (shar, i64, env, i64, i64) + +/* Packed SIMD - Exchange Operations */ +DEF_HELPER_3(pas_hx, tl, env, tl, tl) +DEF_HELPER_3(psa_hx, tl, env, tl, tl) +DEF_HELPER_3(psas_hx, tl, env, tl, tl) +DEF_HELPER_3(pssa_hx, tl, env, tl, tl) +DEF_HELPER_3(paas_hx, tl, env, tl, tl) +DEF_HELPER_3(pasa_hx, tl, env, tl, tl) +DEF_HELPER_3(pas_wx, i64, env, i64, i64) +DEF_HELPER_3(psa_wx, i64, env, i64, i64) +DEF_HELPER_3(psas_wx, i64, env, i64, i64) +DEF_HELPER_3(pssa_wx, i64, env, i64, i64) +DEF_HELPER_3(paas_wx, i64, env, i64, i64) +DEF_HELPER_3 (pasa_wx, i64, env, i64, i64)
target/riscv/insn32.decode +14 -0
@@ -1263,3 +1263,17 @@ psshar_hs 1111100 ..... ..... 010 ..... 0011011 @r } sha 1110111 ..... ..... 010 ..... 0011011 @r shar 1111111 ..... ..... 010 ..... 0011011 @r + +# Packed SIMD - Exchange Operations +pas_hx 1000000 ..... ..... 110 ..... 0111011 @r +psa_hx 1000010 ..... ..... 110 ..... 0111011 @r +psas_hx 1001000 ..... ..... 110 ..... 0111011 @r +pssa_hx 1001010 ..... ..... 110 ..... 0111011 @r +paas_hx 1001100 ..... ..... 110 ..... 0111011 @r +pasa_hx 1001110 ..... ..... 110 ..... 0111011 @r +pas_wx 1000001 ..... ..... 110 ..... 0111011 @r +psa_wx 1000011 ..... ..... 110 ..... 0111011 @r +psas_wx 1001001 ..... ..... 110 ..... 0111011 @r +pssa_wx 1001011 ..... ..... 110 ..... 0111011 @r +paas_wx 1001101 ..... ..... 110 ..... 0111011 @r +pasa_wx 1001111 ..... ..... 110 ..... 0111011 @r
target/riscv/insn_trans/trans_rvp.c.inc +14 -0
@@ -654,3 +654,17 @@ GEN_SIMD_TRANS_32(ssha) GEN_SIMD_TRANS_32(sshar) GEN_SIMD_TRANS_64(sha) GEN_SIMD_TRANS_64(shar) + +/* Packed SIMD - Exchange Operations */ +GEN_SIMD_TRANS(pas_hx) +GEN_SIMD_TRANS(psa_hx) +GEN_SIMD_TRANS(psas_hx) +GEN_SIMD_TRANS(pssa_hx) +GEN_SIMD_TRANS(paas_hx) +GEN_SIMD_TRANS(pasa_hx) +GEN_SIMD_TRANS_64(pas_wx) +GEN_SIMD_TRANS_64(psa_wx) +GEN_SIMD_TRANS_64(psas_wx) +GEN_SIMD_TRANS_64(pssa_wx) +GEN_SIMD_TRANS_64(paas_wx) +GEN_SIMD_TRANS_64(pasa_wx)
target/riscv/psimd_helper.c +294 -0
297 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 7/14 target/riscv: rvp: add horizontal reduction, pack, merge and cout leading operations
In reply to Molly Chen--- target/riscv/helper.h | 46 ++ target/riscv/insn32.decode | 44 ++ target/riscv/insn_trans/trans_rvp.c.inc | 44 ++ target/riscv/psimd_helper.c | 619 ++++++++++++++++++++++++ 4 files changed, 753 insertions(+)
target/riscv/helper.h +46 -0
@@ -1497,3 +1497,49 @@ DEF_HELPER_3(psas_wx, i64, env, i64, i64) DEF_HELPER_3(pssa_wx, i64, env, i64, i64) DEF_HELPER_3(paas_wx, i64, env, i64, i64)DEF_HELPER_3 (pasa_wx, i64, env, i64, i64) + +/* Packed SIMD - Horizontal Reduction Operations */ +DEF_HELPER_3(predsum_bs, tl, env, tl, tl) +DEF_HELPER_3(predsumu_bs, tl, env, tl, tl) +DEF_HELPER_3(predsum_hs, tl, env, tl, tl) +DEF_HELPER_3(predsumu_hs, tl, env, tl, tl) +DEF_HELPER_3(predsum_ws, i64, env, i64, i64) +DEF_HELPER_3 (predsumu_ws, i64, env, i64, i64) + +/* Packed SIMD - Pack, Unpack, and Merge Operations */ +DEF_HELPER_3(ppaire_b, tl, env, tl, tl) +DEF_HELPER_3(ppaireo_b, tl, env, tl, tl) +DEF_HELPER_3(ppairoe_b, tl, env, tl, tl) +DEF_HELPER_3(ppairo_b, tl, env, tl, tl) + +DEF_HELPER_3(ppaire_h, i64, env, i64, i64) +DEF_HELPER_3(ppaireo_h, tl, env, tl, tl) +DEF_HELPER_3(ppairoe_h, tl, env, tl, tl) +DEF_HELPER_3(ppairo_h, tl, env, tl, tl) + +DEF_HELPER_3(ppaireo_w, i64, env, i64, i64) +DEF_HELPER_3(ppairoe_w, i64, env, i64, i64) +DEF_HELPER_3(ppairo_w, i64, env, i64, i64) +DEF_HELPER_2(psext_h_b, tl, env, tl) +DEF_HELPER_2(psext_w_b, i64, env, i64) +DEF_HELPER_2(psext_w_h, i64, env, i64) +DEF_HELPER_2(rev, tl, env, tl) +DEF_HELPER_2(rev16, i64, env, i64) +DEF_HELPER_3(zip8p, i64, env, i64, i64) +DEF_HELPER_3(zip8hp, i64, env, i64, i64) +DEF_HELPER_3(unzip8p, i64, env, i64, i64) +DEF_HELPER_3(unzip8hp, i64, env, i64, i64) +DEF_HELPER_3(zip16p, i64, env, i64, i64) +DEF_HELPER_3(zip16hp, i64, env, i64, i64) +DEF_HELPER_3(unzip16p, i64, env, i64, i64) +DEF_HELPER_3(unzip16hp, i64, env, i64, i64) +DEF_HELPER_4(slx, tl, env, tl, tl, tl) +DEF_HELPER_4(srx, tl, env, tl, tl, tl) +DEF_HELPER_4(mvm, tl, env, tl, tl, tl) +DEF_HELPER_4(mvmn, tl, env, tl, tl, tl) +DEF_HELPER_4 (merge, tl, env, tl, tl, tl) + +/* Packed SIMD - Count Leading Operations */ +DEF_HELPER_2(cls, tl, env, tl) +DEF_HELPER_2 (clsw, i64, env, i64) +
target/riscv/insn32.decode +44 -0
47 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +44 -0
47 lines not on this page · raw message
target/riscv/psimd_helper.c +619 -0
622 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 8/14 target/riscv: rvp: add pure multiplication operations
In reply to Molly Chen--- target/riscv/helper.h | 62 ++ target/riscv/insn32.decode | 92 ++ target/riscv/insn_trans/trans_rvp.c.inc | 62 ++ target/riscv/psimd_helper.c | 1066 +++++++++++++++++++++++ 4 files changed, 1282 insertions(+)
target/riscv/helper.h +62 -0
@@ -1543,3 +1543,65 @@ DEF_HELPER_4(merge, tl, env, tl, tl, tl) DEF_HELPER_2(cls, tl, env, tl)DEF_HELPER_2 (clsw, i64, env, i64) +/* Packed SIMD - Pure Multiplication Operations */ +DEF_HELPER_3(pmulh_h, tl, env, tl, tl) +DEF_HELPER_3(pmulhsu_h, tl, env, tl, tl) +DEF_HELPER_3(pmulhu_h, tl, env, tl, tl) +DEF_HELPER_3(pmulhr_h, tl, env, tl, tl) +DEF_HELPER_3(pmulhrsu_h, tl, env, tl, tl) +DEF_HELPER_3(pmulhru_h, tl, env, tl, tl) +DEF_HELPER_3(pmulh_w, i64, env, i64, i64) +DEF_HELPER_3(pmulhr_w, i64, env, i64, i64) +DEF_HELPER_3(pmulhsu_w, i64, env, i64, i64) +DEF_HELPER_3(pmulhrsu_w, i64, env, i64, i64) +DEF_HELPER_3(pmulhu_w, i64, env, i64, i64) +DEF_HELPER_3(pmulhru_w, i64, env, i64, i64) +DEF_HELPER_3(mulhr, i32, env, i32, i32) +DEF_HELPER_3(mulhrsu, i32, env, i32, i32) +DEF_HELPER_3(mulhru, i32, env, i32, i32) +DEF_HELPER_3(pmulh_h_b0, tl, env, tl, tl) +DEF_HELPER_3(pmulh_h_b1, tl, env, tl, tl) +DEF_HELPER_3(pmulhsu_h_b0, tl, env, tl, tl) +DEF_HELPER_3(pmulhsu_h_b1, tl, env, tl, tl) +DEF_HELPER_3(mulh_h0, i32, env, i32, i32) +DEF_HELPER_3(mulh_h1, i32, env, i32, i32) +DEF_HELPER_3(mulhsu_h0, i32, env, i32, i32) +DEF_HELPER_3(mulhsu_h1, i32, env, i32, i32) +DEF_HELPER_3(pmulh_w_h0, i64, env, i64, i64) +DEF_HELPER_3(pmulh_w_h1, i64, env, i64, i64) +DEF_HELPER_3(pmulhsu_w_h0, i64, env, i64, i64) +DEF_HELPER_3(pmulhsu_w_h1, i64, env, i64, i64) +DEF_HELPER_3(pmul_h_b00, tl, env, tl, tl) +DEF_HELPER_3(pmul_h_b01, tl, env, tl, tl) +DEF_HELPER_3(pmul_h_b11, tl, env, tl, tl) +DEF_HELPER_3(pmulsu_h_b00, tl, env, tl, tl) +DEF_HELPER_3(pmulsu_h_b11, tl, env, tl, tl) +DEF_HELPER_3(pmulu_h_b00, tl, env, tl, tl) +DEF_HELPER_3(pmulu_h_b01, tl, env, tl, tl) +DEF_HELPER_3(pmulu_h_b11, tl, env, tl, tl) +DEF_HELPER_3(pmul_w_h00, i64, env, i64, i64) +DEF_HELPER_3(pmul_w_h01, i64, env, i64, i64) +DEF_HELPER_3(pmul_w_h11, i64, env, i64, i64) +DEF_HELPER_3(pmulsu_w_h00, i64, env, i64, i64) +DEF_HELPER_3(pmulsu_w_h11, i64, env, i64, i64) +DEF_HELPER_3(pmulu_w_h00, i64, env, i64, i64) +DEF_HELPER_3(pmulu_w_h01, i64, env, i64, i64) +DEF_HELPER_3(pmulu_w_h11, i64, env, i64, i64) +DEF_HELPER_3(pm2sadd_h, tl, env, tl, tl) +DEF_HELPER_3(pm2sadd_hx, tl, env, tl, tl) +DEF_HELPER_3(mul_h00, i32, env, i32, i32) +DEF_HELPER_3(mul_h01, i32, env, i32, i32) +DEF_HELPER_3(mul_h11, i32, env, i32, i32) +DEF_HELPER_3(mulsu_h00, i32, env, i32, i32) +DEF_HELPER_3(mulsu_h11, i32, env, i32, i32) +DEF_HELPER_3(mulu_h00, i32, env, i32, i32) +DEF_HELPER_3(mulu_h01, i32, env, i32, i32) +DEF_HELPER_3(mulu_h11, i32, env, i32, i32) +DEF_HELPER_3(mul_w00, i64, env, i64, i64) +DEF_HELPER_3(mul_w01, i64, env, i64, i64) +DEF_HELPER_3(mul_w11, i64, env, i64, i64) +DEF_HELPER_3(mulsu_w00, i64, env, i64, i64) +DEF_HELPER_3(mulsu_w11, i64, env, i64, i64) +DEF_HELPER_3(mulu_w00, i64, env, i64, i64) +DEF_HELPER_3(mulu_w01, i64, env, i64, i64) +DEF_HELPER_3 (mulu_w11, i64, env, i64, i64)
target/riscv/insn32.decode +92 -0
95 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +62 -0
65 lines not on this page · raw message
target/riscv/psimd_helper.c +1066 -0
1,069 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 9/14 target/riscv: rvp: add multiply-accumulate operations
In reply to Molly Chen--- target/riscv/helper.h | 56 ++ target/riscv/insn32.decode | 92 +++ target/riscv/insn_trans/trans_rvp.c.inc | 56 ++ target/riscv/psimd_helper.c | 946 ++++++++++++++++++++++++ 4 files changed, 1150 insertions(+)
target/riscv/helper.h +56 -0
@@ -1605,3 +1605,59 @@ DEF_HELPER_3(mulsu_w11, i64, env, i64, i64) DEF_HELPER_3(mulu_w00, i64, env, i64, i64) DEF_HELPER_3(mulu_w01, i64, env, i64, i64)DEF_HELPER_3 (mulu_w11, i64, env, i64, i64) + +/* Packed SIMD - Multiply-Accumulate Operations */ +DEF_HELPER_4(pmhacc_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhaccsu_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhaccu_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhracc_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhraccsu_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhraccu_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhacc_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhracc_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhaccsu_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhraccsu_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhaccu_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhraccu_w, i64, env, i64, i64, i64) +DEF_HELPER_4(mhacc, i32, env, i32, i32, i32) +DEF_HELPER_4(mhracc, i32, env, i32, i32, i32) +DEF_HELPER_4(mhaccsu, i32, env, i32, i32, i32) +DEF_HELPER_4(mhraccsu, i32, env, i32, i32, i32) +DEF_HELPER_4(mhaccu, i32, env, i32, i32, i32) +DEF_HELPER_4(mhraccu, i32, env, i32, i32, i32) +DEF_HELPER_4(pmhacc_h_b0, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhacc_h_b1, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhaccsu_h_b0, tl, env, tl, tl, tl) +DEF_HELPER_4(pmhaccsu_h_b1, tl, env, tl, tl, tl) +DEF_HELPER_4(mhacc_h0, i32, env, i32, i32, i32) +DEF_HELPER_4(mhacc_h1, i32, env, i32, i32, i32) +DEF_HELPER_4(mhaccsu_h0, i32, env, i32, i32, i32) +DEF_HELPER_4(mhaccsu_h1, i32, env, i32, i32, i32) +DEF_HELPER_4(pmhacc_w_h0, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhacc_w_h1, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhaccsu_w_h0, i64, env, i64, i64, i64) +DEF_HELPER_4(pmhaccsu_w_h1, i64, env, i64, i64, i64) +DEF_HELPER_4(pmacc_w_h00, i64, env, i64, i64, i64) +DEF_HELPER_4(pmacc_w_h01, i64, env, i64, i64, i64) +DEF_HELPER_4(pmacc_w_h11, i64, env, i64, i64, i64) +DEF_HELPER_4(pmaccsu_w_h00, i64, env, i64, i64, i64) +DEF_HELPER_4(pmaccsu_w_h11, i64, env, i64, i64, i64) +DEF_HELPER_4(pmaccu_w_h00, i64, env, i64, i64, i64) +DEF_HELPER_4(pmaccu_w_h01, i64, env, i64, i64, i64) +DEF_HELPER_4(pmaccu_w_h11, i64, env, i64, i64, i64) +DEF_HELPER_4(macc_h00, i32, env, i32, i32, i32) +DEF_HELPER_4(macc_h01, i32, env, i32, i32, i32) +DEF_HELPER_4(macc_h11, i32, env, i32, i32, i32) +DEF_HELPER_4(maccsu_h00, i32, env, i32, i32, i32) +DEF_HELPER_4(maccsu_h11, i32, env, i32, i32, i32) +DEF_HELPER_4(maccu_h00, i32, env, i32, i32, i32) +DEF_HELPER_4(maccu_h01, i32, env, i32, i32, i32) +DEF_HELPER_4(maccu_h11, i32, env, i32, i32, i32) +DEF_HELPER_4(macc_w00, i64, env, i64, i64, i64) +DEF_HELPER_4(macc_w01, i64, env, i64, i64, i64) +DEF_HELPER_4(macc_w11, i64, env, i64, i64, i64) +DEF_HELPER_4(maccsu_w00, i64, env, i64, i64, i64) +DEF_HELPER_4(maccsu_w11, i64, env, i64, i64, i64) +DEF_HELPER_4(maccu_w00, i64, env, i64, i64, i64) +DEF_HELPER_4(maccu_w01, i64, env, i64, i64, i64) +DEF_HELPER_4 (maccu_w11, i64, env, i64, i64, i64)
target/riscv/insn32.decode +92 -0
95 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +56 -0
59 lines not on this page · raw message
target/riscv/psimd_helper.c +946 -0
949 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 10/14 target/riscv: rvp: add Q-format multiplication operations
In reply to Molly Chen--- target/riscv/helper.h | 28 ++ target/riscv/insn32.decode | 43 +++ target/riscv/insn_trans/trans_rvp.c.inc | 28 ++ target/riscv/psimd_helper.c | 446 ++++++++++++++++++++++++ 4 files changed, 545 insertions(+)
target/riscv/helper.h +28 -0
@@ -1661,3 +1661,31 @@ DEF_HELPER_4(maccsu_w11, i64, env, i64, i64, i64) DEF_HELPER_4(maccu_w00, i64, env, i64, i64, i64) DEF_HELPER_4(maccu_w01, i64, env, i64, i64, i64)DEF_HELPER_4 (maccu_w11, i64, env, i64, i64, i64) + +/* Packed SIMD - Q-Format Multiplication Operations */ +DEF_HELPER_3(pmulq_h, tl, env, tl, tl) +DEF_HELPER_3(pmulqr_h, tl, env, tl, tl) +DEF_HELPER_3(pmulq_w, i64, env, i64, i64) +DEF_HELPER_3(pmulqr_w, i64, env, i64, i64) +DEF_HELPER_3(mulq, i32, env, i32, i32) +DEF_HELPER_3 (mulqr, i32, env, i32, i32) + +/* Packed SIMD - Q-Format Multiply-Accumulate Operations */ +DEF_HELPER_4(mqacc_h00, i32, env, i32, i32, i32) +DEF_HELPER_4(mqacc_h01, i32, env, i32, i32, i32) +DEF_HELPER_4(mqacc_h11, i32, env, i32, i32, i32) +DEF_HELPER_4(mqracc_h00, i32, env, i32, i32, i32) +DEF_HELPER_4(mqracc_h01, i32, env, i32, i32, i32) +DEF_HELPER_4(mqracc_h11, i32, env, i32, i32, i32) +DEF_HELPER_4(mqacc_w00, i64, env, i64, i64, i64) +DEF_HELPER_4(mqacc_w01, i64, env, i64, i64, i64) +DEF_HELPER_4(mqacc_w11, i64, env, i64, i64, i64) +DEF_HELPER_4(mqracc_w00, i64, env, i64, i64, i64) +DEF_HELPER_4(mqracc_w01, i64, env, i64, i64, i64) +DEF_HELPER_4(mqracc_w11, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqacc_w_h00, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqacc_w_h01, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqacc_w_h11, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqracc_w_h00, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqracc_w_h01, i64, env, i64, i64, i64) +DEF_HELPER_4 (pmqracc_w_h11, i64, env, i64, i64, i64)
target/riscv/insn32.decode +43 -0
46 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +28 -0
@@ -830,3 +830,31 @@ GEN_SIMD_TRANS_ACC_64(maccsu_w11) GEN_SIMD_TRANS_ACC_64(maccu_w00) GEN_SIMD_TRANS_ACC_64(maccu_w01) GEN_SIMD_TRANS_ACC_64(maccu_w11) + +/* Packed SIMD - Q-Format Multiplication Operations */ +GEN_SIMD_TRANS(pmulq_h) +GEN_SIMD_TRANS(pmulqr_h) +GEN_SIMD_TRANS_64(pmulq_w) +GEN_SIMD_TRANS_64(pmulqr_w) +GEN_SIMD_TRANS_32(mulq) +GEN_SIMD_TRANS_32(mulqr) + +/* Packed SIMD - Q-Format Multiply-Accumulate Operations */ +GEN_SIMD_TRANS_ACC_32(mqacc_h00) +GEN_SIMD_TRANS_ACC_32(mqacc_h01) +GEN_SIMD_TRANS_ACC_32(mqacc_h11) +GEN_SIMD_TRANS_ACC_32(mqracc_h00) +GEN_SIMD_TRANS_ACC_32(mqracc_h01) +GEN_SIMD_TRANS_ACC_32(mqracc_h11) +GEN_SIMD_TRANS_ACC_64(mqacc_w00) +GEN_SIMD_TRANS_ACC_64(mqacc_w01) +GEN_SIMD_TRANS_ACC_64(mqacc_w11) +GEN_SIMD_TRANS_ACC_64(mqracc_w00) +GEN_SIMD_TRANS_ACC_64(mqracc_w01) +GEN_SIMD_TRANS_ACC_64(mqracc_w11) +GEN_SIMD_TRANS_ACC_64(pmqacc_w_h00) +GEN_SIMD_TRANS_ACC_64(pmqacc_w_h01) +GEN_SIMD_TRANS_ACC_64(pmqacc_w_h11) +GEN_SIMD_TRANS_ACC_64(pmqracc_w_h00) +GEN_SIMD_TRANS_ACC_64(pmqracc_w_h01) +GEN_SIMD_TRANS_ACC_64(pmqracc_w_h11)
target/riscv/psimd_helper.c +446 -0
449 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 11/14 target/riscv: rvp: add two-way and four-way multiply and accumulate operations
In reply to Molly Chen--- target/riscv/helper.h | 48 ++ target/riscv/insn32.decode | 48 ++ target/riscv/insn_trans/trans_rvp.c.inc | 48 ++ target/riscv/psimd_helper.c | 938 ++++++++++++++++++++++++ 4 files changed, 1082 insertions(+)
target/riscv/helper.h +48 -0
@@ -1689,3 +1689,51 @@ DEF_HELPER_4(pmqacc_w_h11, i64, env, i64, i64, i64) DEF_HELPER_4(pmqracc_w_h00, i64, env, i64, i64, i64) DEF_HELPER_4(pmqracc_w_h01, i64, env, i64, i64, i64)DEF_HELPER_4 (pmqracc_w_h11, i64, env, i64, i64, i64) + +/* Packed SIMD - Two-Way Multiply and Accumulate Operations */ +DEF_HELPER_3(pmq2add_h, tl, env, tl, tl) +DEF_HELPER_3(pmqr2add_h, tl, env, tl, tl) +DEF_HELPER_4(pmq2adda_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pmqr2adda_h, tl, env, tl, tl, tl) +DEF_HELPER_3(pmq2add_w, i64, env, i64, i64) +DEF_HELPER_3(pmqr2add_w, i64, env, i64, i64) +DEF_HELPER_4(pmq2adda_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pmqr2adda_w, i64, env, i64, i64, i64) +DEF_HELPER_3(pm2add_h, tl, env, tl, tl) +DEF_HELPER_3(pm2addsu_h, tl, env, tl, tl) +DEF_HELPER_3(pm2addu_h, tl, env, tl, tl) +DEF_HELPER_3(pm2add_hx, tl, env, tl, tl) +DEF_HELPER_3(pm2sub_h, tl, env, tl, tl) +DEF_HELPER_3(pm2sub_hx, tl, env, tl, tl) +DEF_HELPER_4(pm2adda_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pm2addasu_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pm2addau_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pm2adda_hx, tl, env, tl, tl, tl) +DEF_HELPER_4(pm2suba_h, tl, env, tl, tl, tl) +DEF_HELPER_4(pm2suba_hx, tl, env, tl, tl, tl) +DEF_HELPER_3(pm2add_w, i64, env, i64, i64) +DEF_HELPER_3(pm2addsu_w, i64, env, i64, i64) +DEF_HELPER_3(pm2addu_w, i64, env, i64, i64) +DEF_HELPER_3(pm2add_wx, i64, env, i64, i64) +DEF_HELPER_3(pm2sub_w, i64, env, i64, i64) +DEF_HELPER_3(pm2sub_wx, i64, env, i64, i64) +DEF_HELPER_4(pm2adda_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pm2addasu_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pm2addau_w, i64, env, i64, i64, i64) +DEF_HELPER_4(pm2adda_wx, i64, env, i64, i64, i64) +DEF_HELPER_4(pm2suba_w, i64, env, i64, i64, i64) +DEF_HELPER_4 (pm2suba_wx, i64, env, i64, i64, i64) + +/* Packed SIMD - Four-Way Multiply and Accumulate Operations */ +DEF_HELPER_3(pm4add_b, tl, env, tl, tl) +DEF_HELPER_3(pm4addsu_b, tl, env, tl, tl) +DEF_HELPER_3(pm4addu_b, tl, env, tl, tl) +DEF_HELPER_4(pm4adda_b, tl, env, tl, tl, tl) +DEF_HELPER_4(pm4addasu_b, tl, env, tl, tl, tl) +DEF_HELPER_4(pm4addau_b, tl, env, tl, tl, tl) +DEF_HELPER_3(pm4add_h, i64, env, i64, i64) +DEF_HELPER_3(pm4addsu_h, i64, env, i64, i64) +DEF_HELPER_3(pm4addu_h, i64, env, i64, i64) +DEF_HELPER_4(pm4adda_h, i64, env, i64, i64, i64) +DEF_HELPER_4(pm4addasu_h, i64, env, i64, i64, i64) +DEF_HELPER_4 (pm4addau_h, i64, env, i64, i64, i64)
target/riscv/insn32.decode +48 -0
51 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +48 -0
51 lines not on this page · raw message
target/riscv/psimd_helper.c +938 -0
941 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 12/14 target/riscv: rvp: add load and replicate instructions.
In reply to Molly Chen--- target/riscv/insn32.decode | 16 ++++++ target/riscv/insn_trans/trans_rvp.c.inc | 67 +++++++++++++++++++++++++ target/riscv/translate.c | 2 + 3 files changed, 85 insertions(+)
target/riscv/insn32.decode +16 -0
@@ -44,6 +44,10 @@ %imm_p_ui16 20:4 %imm_p_ui32 20:5 %imm_p_ui64 20:6 +%imm_p_l1 16:8 +%imm_p_l2 15:s1 16:9 +%imm_p_l3 15:s9 24:1 !function=ex_shift_6 +%imm_p_l4 15:s9 24:1 !function=ex_shift_22 # Argument sets: &empty
@@ -64,6 +68,7 @@ &k_aes shamt rs2 rs1 rd &mop5 imm rd rs1 &mop3 imm rd rs1 rs2 +&p_l imm rd # Formats 32: @r ....... ..... ..... ... ..... ....... &r %rs2 %rs1 %rd
@@ -113,6 +118,10 @@ @p_ui16 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui16 %rs1 %rd @p_ui32 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui32 %rs1 %rd @p_ui64 ..... .... ... ..... ... ..... ....... &i imm=%imm_p_ui64 %rs1 %rd +@p_l1 ........ ........ .... ..... ....... &p_l imm=%imm_p_l1 %rd +@p_l2 ....... .......... ... ..... ....... &p_l imm=%imm_p_l2 %rd +@p_l3 ....... .......... ... ..... ....... &p_l imm=%imm_p_l3 %rd +@p_l4 ....... .......... ... ..... ....... &p_l imm=%imm_p_l4 %rd # Formats 64: @sh5 ....... ..... ..... ... ..... ....... &shift shamt=%sh5 %rs1 %rd
@@ -1596,3 +1605,10 @@ pm4addu_h 10100 11 ..... ..... 101 ..... 0111011 @r pm4adda_h 10001 11 ..... ..... 101 ..... 0111011 @r pm4addasu_h 11101 11 ..... ..... 101 ..... 0111011 @r pm4addau_h 10101 11 ..... ..... 101 ..... 0111011 @r + +# Packed SIMD - Load and Replicate instructions +pli_b 10110100 ........ 0010 ..... 0011011 @p_l1 +pli_h 1011000 .......... 010 ..... 0011011 @p_l2 +plui_h 1111000 .......... 010 ..... 0011011 @p_l3 +pli_w 1011001 ..... ..... 010 ..... 0011011 @p_l2 +plui_w 1111001 ..... ..... 010 ..... 0011011 @p_l4
target/riscv/insn_trans/trans_rvp.c.inc +67 -0
70 lines not on this page · raw message
target/riscv/translate.c +2 -0
@@ -796,7 +796,9 @@ EX_SH(1) EX_SH(2) EX_SH(3) EX_SH(4) +EX_SH(6) EX_SH(12) +EX_SH(22)#define REQUIRE_EXT (ctx, ext) do { \ if (!has_ext(ctx, ext)) { \
Signature · 1 line
2.34.1
PATCH 13/14 target/riscv: rvp: add rv32-only register-pair instructions
In reply to Molly Chen--- target/riscv/helper.h | 131 ++ target/riscv/insn32.decode | 279 +++ target/riscv/insn_trans/trans_rvp.c.inc | 786 ++++++++- target/riscv/psimd_helper.c | 2068 +++++++++++++++++++++++ 4 files changed, 3220 insertions(+), 44 deletions(-)
target/riscv/helper.h +131 -0
@@ -1737,3 +1737,134 @@ DEF_HELPER_3(pm4addu_h, i64, env, i64, i64) DEF_HELPER_4(pm4adda_h, i64, env, i64, i64, i64) DEF_HELPER_4(pm4addasu_h, i64, env, i64, i64, i64)DEF_HELPER_4 (pm4addau_h, i64, env, i64, i64, i64) + +/* Packed SIMD - Double-Width Operations (RV32 only, register pairs) */ +DEF_HELPER_3(pwadd_b, i64, env, i32, i32) +DEF_HELPER_4(pwadda_b, i64, env, i32, i32, i64) +DEF_HELPER_3(pwaddu_b, i64, env, i32, i32) +DEF_HELPER_4(pwaddau_b, i64, env, i32, i32, i64) +DEF_HELPER_3(pwsub_b, i64, env, i32, i32) +DEF_HELPER_4(pwsuba_b, i64, env, i32, i32, i64) +DEF_HELPER_3(pwsubu_b, i64, env, i32, i32) +DEF_HELPER_4(pwsubau_b, i64, env, i32, i32, i64) +DEF_HELPER_3(pwslli_b, i64, env, i32, i32) +DEF_HELPER_3(pwsll_bs, i64, env, i32, i32) +DEF_HELPER_3(pwslai_b, i64, env, i32, i32) +DEF_HELPER_3(pwsla_bs, i64, env, i32, i32) + +DEF_HELPER_3(pwadd_h, i64, env, i32, i32) +DEF_HELPER_4(pwadda_h, i64, env, i32, i32, i64) +DEF_HELPER_3(pwaddu_h, i64, env, i32, i32) +DEF_HELPER_4(pwaddau_h, i64, env, i32, i32, i64) +DEF_HELPER_3(pwsub_h, i64, env, i32, i32) +DEF_HELPER_4(pwsuba_h, i64, env, i32, i32, i64) +DEF_HELPER_3(pwsubu_h, i64, env, i32, i32) +DEF_HELPER_4(pwsubau_h, i64, env, i32, i32, i64) +DEF_HELPER_3(pwslli_h, i64, env, i32, i32) +DEF_HELPER_3(pwsll_hs, i64, env, i32, i32) +DEF_HELPER_3(pwslai_h, i64, env, i32, i32) +DEF_HELPER_3(pwsla_hs, i64, env, i32, i32) + +DEF_HELPER_3(wadd, i64, env, i32, i32) +DEF_HELPER_4(wadda, i64, env, i32, i32, i64) +DEF_HELPER_3(waddu, i64, env, i32, i32) +DEF_HELPER_4(waddau, i64, env, i32, i32, i64) +DEF_HELPER_3(wsub, i64, env, i32, i32) +DEF_HELPER_4(wsuba, i64, env, i32, i32, i64) +DEF_HELPER_3(wsubu, i64, env, i32, i32) +DEF_HELPER_4(wsubau, i64, env, i32, i32, i64) +DEF_HELPER_3(wslli, i64, env, i32, i32) +DEF_HELPER_3(wsll, i64, env, i32, i32) +DEF_HELPER_3(wslai, i64, env, i32, i32) +DEF_HELPER_3(wsla, i64, env, i32, i32) + +DEF_HELPER_3(wzip8p, i64, env, i32, i32) +DEF_HELPER_3(wzip16p, i64, env, i32, i32) + +DEF_HELPER_4(predsum_dbs, i32, env, i32, i32, i32) +DEF_HELPER_4(predsumu_dbs, i32, env, i32, i32, i32) +DEF_HELPER_4(predsum_dhs, i32, env, i32, i32, i32) +DEF_HELPER_4(predsumu_dhs, i32, env, i32, i32, i32) + +DEF_HELPER_3(pnsrli_b, i32, env, i64, i32) +DEF_HELPER_3(pnsrai_b, i32, env, i64, i32) +DEF_HELPER_3(pnsrari_b, i32, env, i64, i32) +DEF_HELPER_3(pnclipi_b, i32, env, i64, i32) +DEF_HELPER_3(pnclipri_b, i32, env, i64, i32) +DEF_HELPER_3(pnclipiu_b, i32, env, i64, i32) +DEF_HELPER_3(pnclipriu_b, i32, env, i64, i32) +DEF_HELPER_3(pnsrl_bs, i32, env, i64, i32) +DEF_HELPER_3(pnsra_bs, i32, env, i64, i32) +DEF_HELPER_3(pnsrar_bs, i32, env, i64, i32) +DEF_HELPER_3(pnclip_bs, i32, env, i64, i32) +DEF_HELPER_3(pnclipr_bs, i32, env, i64, i32) +DEF_HELPER_3(pnclipu_bs, i32, env, i64, i32) +DEF_HELPER_3(pnclipru_bs, i32, env, i64, i32) + +DEF_HELPER_3(pnsrli_h, i32, env, i64, i32) +DEF_HELPER_3(pnsrai_h, i32, env, i64, i32) +DEF_HELPER_3(pnsrari_h, i32, env, i64, i32) +DEF_HELPER_3(pnclipi_h, i32, env, i64, i32) +DEF_HELPER_3(pnclipri_h, i32, env, i64, i32) +DEF_HELPER_3(pnclipiu_h, i32, env, i64, i32) +DEF_HELPER_3(pnclipriu_h, i32, env, i64, i32) +DEF_HELPER_3(pnsrl_hs, i32, env, i64, i32) +DEF_HELPER_3(pnsra_hs, i32, env, i64, i32) +DEF_HELPER_3(pnsrar_hs, i32, env, i64, i32) +DEF_HELPER_3(pnclip_hs, i32, env, i64, i32) +DEF_HELPER_3(pnclipr_hs, i32, env, i64, i32) +DEF_HELPER_3(pnclipu_hs, i32, env, i64, i32) +DEF_HELPER_3(pnclipru_hs, i32, env, i64, i32) + +DEF_HELPER_3(nsrli, i32, env, i64, i32) +DEF_HELPER_3(nsrai, i32, env, i64, i32) +DEF_HELPER_3(nsrari, i32, env, i64, i32) +DEF_HELPER_3(nclipi, i32, env, i64, i32) +DEF_HELPER_3(nclipri, i32, env, i64, i32) +DEF_HELPER_3(nclipiu, i32, env, i64, i32) +DEF_HELPER_3(nclipriu, i32, env, i64, i32) +DEF_HELPER_3(nsrl, i32, env, i64, i32) +DEF_HELPER_3(nsra, i32, env, i64, i32) +DEF_HELPER_3(nsrar, i32, env, i64, i32) +DEF_HELPER_3(nclip, i32, env, i64, i32) +DEF_HELPER_3(nclipr, i32, env, i64, i32) +DEF_HELPER_3(nclipu, i32, env, i64, i32) +DEF_HELPER_3(nclipru, i32, env, i64, i32) + +DEF_HELPER_4(pmqwacc_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pmqrwacc_h, i64, env, i32, i32, i64) +DEF_HELPER_4(mqwacc, i64, env, i32, i32, i64) +DEF_HELPER_4(mqrwacc, i64, env, i32, i32, i64) + +DEF_HELPER_3(pwmul_b, i64, env, i32, i32) +DEF_HELPER_3(pwmulsu_b, i64, env, i32, i32) +DEF_HELPER_3(pwmulu_b, i64, env, i32, i32) +DEF_HELPER_3(pwmul_h, i64, env, i32, i32) +DEF_HELPER_3(pwmulsu_h, i64, env, i32, i32) +DEF_HELPER_3(pwmulu_h, i64, env, i32, i32) + +DEF_HELPER_4(pwmacc_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pwmaccsu_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pwmaccu_h, i64, env, i32, i32, i64) + +DEF_HELPER_3(wmul, i64, env, i32, i32) +DEF_HELPER_3(wmulsu, i64, env, i32, i32) +DEF_HELPER_3(wmulu, i64, env, i32, i32) + +DEF_HELPER_4(wmacc, i64, env, i32, i32, i64) +DEF_HELPER_4(wmaccsu, i64, env, i32, i32, i64) +DEF_HELPER_4(wmaccu, i64, env, i32, i32, i64) + +DEF_HELPER_3(pm2wadd_h, i64, env, i32, i32) +DEF_HELPER_3(pm2waddsu_h, i64, env, i32, i32) +DEF_HELPER_3(pm2waddu_h, i64, env, i32, i32) +DEF_HELPER_3(pm2wadd_hx, i64, env, i32, i32) +DEF_HELPER_4(pm2wadda_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pm2waddasu_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pm2waddau_h, i64, env, i32, i32, i64) +DEF_HELPER_4(pm2wadda_hx, i64, env, i32, i32, i64) + +DEF_HELPER_3(pm2wsub_h, i64, env, i32, i32) +DEF_HELPER_3(pm2wsub_hx, i64, env, i32, i32) +DEF_HELPER_4(pm2wsuba_h, i64, env, i32, i32, i64) +DEF_HELPER_4 (pm2wsuba_hx, i64, env, i32, i32, i64)
target/riscv/insn32.decode +279 -0
306 lines not on this page · raw message
target/riscv/insn_trans/trans_rvp.c.inc +742 -44
1,065 lines not on this page · raw message
target/riscv/psimd_helper.c +2068 -0
1,495 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.34.1
Part of the diff is not shown here. View the raw message.
PATCH 14/14 target/riscv: rvp: update to v020, add SHL and PNCLIP[U]P.* instructions
In reply to Molly Chen--- target/riscv/helper.h | 14 + target/riscv/insn32.decode | 22 ++ target/riscv/insn_trans/trans_rvp.c.inc | 18 ++ target/riscv/psimd_helper.c | 370 ++++++++++++++++++++++++ 4 files changed, 424 insertions(+)
target/riscv/helper.h +14 -0
@@ -1483,6 +1483,14 @@ DEF_HELPER_3(ssha, i32, env, i32, i32) DEF_HELPER_3(sshar, i32, env, i32, i32) DEF_HELPER_3(sha, i64, env, i64, i64) DEF_HELPER_3(shar, i64, env, i64, i64) +DEF_HELPER_3(psshl_hs, tl, env, tl, tl) +DEF_HELPER_3(psshlr_hs, tl, env, tl, tl) +DEF_HELPER_3(psshl_ws, i64, env, i64, i64) +DEF_HELPER_3(psshlr_ws, i64, env, i64, i64) +DEF_HELPER_3(sshl, i32, env, i32, i32) +DEF_HELPER_3(sshlr, i32, env, i32, i32) +DEF_HELPER_3(shl, i64, env, i64, i64) +DEF_HELPER_3 (shlr, i64, env, i64, i64) /* Packed SIMD - Exchange Operations */DEF_HELPER_3 (pas_hx, tl, env, tl, tl)
@@ -1538,6 +1546,12 @@ DEF_HELPER_4(srx, tl, env, tl, tl, tl) DEF_HELPER_4(mvm, tl, env, tl, tl, tl) DEF_HELPER_4(mvmn, tl, env, tl, tl, tl) DEF_HELPER_4(merge, tl, env, tl, tl, tl) +DEF_HELPER_3(pnclipp_b, i64, env, i64, i64) +DEF_HELPER_3(pnclipup_b, i64, env, i64, i64) +DEF_HELPER_3(pnclipp_h, i64, env, i64, i64) +DEF_HELPER_3(pnclipup_h, i64, env, i64, i64) +DEF_HELPER_3(pnclipp_w, i64, env, i64, i64) +DEF_HELPER_3 (pnclipup_w, i64, env, i64, i64) /* Packed SIMD - Count Leading Operations */DEF_HELPER_2 (cls, tl, env, tl)
target/riscv/insn32.decode +22 -0
@@ -1293,6 +1293,18 @@ psshar_hs 1111100 ..... ..... 010 ..... 0011011 @r } sha 1110111 ..... ..... 010 ..... 0011011 @r shar 1111111 ..... ..... 010 ..... 0011011 @r +psshl_hs 1010100 ..... ..... 010 ..... 0011011 @r +psshlr_hs 1011100 ..... ..... 010 ..... 0011011 @r +{ + psshl_ws 1010101 ..... ..... 010 ..... 0011011 @r + sshl 1010101 ..... ..... 010 ..... 0011011 @r +} +{ + psshlr_ws 1011101 ..... ..... 010 ..... 0011011 @r + sshlr 1011101 ..... ..... 010 ..... 0011011 @r +} +shl 1010111 ..... ..... 010 ..... 0011011 @r +shlr 1011111 ..... ..... 010 ..... 0011011 @r # Packed SIMD - Exchange Operations pas_hx 1000000 ..... ..... 110 ..... 0111011 @r
@@ -1346,6 +1358,12 @@ srx 1010111 ..... ..... 001 ..... 0111011 @r mvm 1010100 ..... ..... 001 ..... 0111011 @r mvmn 1010101 ..... ..... 001 ..... 0111011 @r merge 1010110 ..... ..... 001 ..... 0111011 @r +pnclipp_b 1100000 ..... ..... 010 ..... 0111011 @r +pnclipup_b 1000000 ..... ..... 010 ..... 0111011 @r +pnclipp_h 1100001 ..... ..... 010 ..... 0111011 @r +pnclipup_h 1000001 ..... ..... 010 ..... 0111011 @r +pnclipp_w 1100011 ..... ..... 010 ..... 0111011 @r +pnclipup_w 1000011 ..... ..... 010 ..... 0111011 @r # Packed SIMD - Count Leading Operations cls 01100 0000011 ..... 001 ..... 0010011 @r2
@@ -1790,12 +1808,16 @@ psrl_dhs 0000100 ..... .... 1110 .... 00011011 @r_p_3 psra_dhs 0100100 ..... .... 1110 .... 00011011 @r_p_3 pssha_dhs 0110100 ..... .... 0110 .... 00011011 @r_p_3 psshar_dhs 0111100 ..... .... 0110 .... 00011011 @r_p_3 +psshl_dhs 0010100 ..... .... 0110 .... 00011011 @r_p_3 +psshlr_dhs 0011100 ..... .... 0110 .... 00011011 @r_p_3 padd_dws 0001101 ..... .... 0110 .... 00011011 @r_p_3 psll_dws 0000101 ..... .... 0110 .... 00011011 @r_p_3 psrl_dws 0000101 ..... .... 1110 .... 00011011 @r_p_3 psra_dws 0100101 ..... .... 1110 .... 00011011 @r_p_3 pssha_dws 0110101 ..... .... 0110 .... 00011011 @r_p_3 psshar_dws 0111101 ..... .... 0110 .... 00011011 @r_p_3 +psshl_dws 0010101 ..... .... 0110 .... 00011011 @r_p_3 +psshlr_dws 0011101 ..... .... 0110 .... 00011011 @r_p_3 # register-pair operands ppaire_db 1000000 .... 0 .... 1110 .... 00011011 @r_p_2
target/riscv/insn_trans/trans_rvp.c.inc +18 -0
@@ -686,6 +686,14 @@ GEN_SIMD_TRANS_32(ssha) GEN_SIMD_TRANS_32(sshar) GEN_SIMD_TRANS_64(sha) GEN_SIMD_TRANS_64(shar) +GEN_SIMD_TRANS(psshl_hs) +GEN_SIMD_TRANS(psshlr_hs) +GEN_SIMD_TRANS_64(psshl_ws) +GEN_SIMD_TRANS_64(psshlr_ws) +GEN_SIMD_TRANS_32(sshl) +GEN_SIMD_TRANS_32(sshlr) +GEN_SIMD_TRANS_64(shl) +GEN_SIMD_TRANS_64(shlr) /* Packed SIMD - Exchange Operations */ GEN_SIMD_TRANS(pas_hx)
@@ -739,6 +747,12 @@ GEN_SIMD_TRANS_ACC(srx) GEN_SIMD_TRANS_ACC(mvm) GEN_SIMD_TRANS_ACC(mvmn) GEN_SIMD_TRANS_ACC(merge) +GEN_SIMD_TRANS_64(pnclipp_b) +GEN_SIMD_TRANS_64(pnclipup_b) +GEN_SIMD_TRANS_64(pnclipp_h) +GEN_SIMD_TRANS_64(pnclipup_h) +GEN_SIMD_TRANS_64(pnclipp_w) +GEN_SIMD_TRANS_64(pnclipup_w) /* Packed SIMD - Count Leading Operations */ GEN_SIMD_TRANS_R1(cls)
@@ -1066,8 +1080,12 @@ GEN_SIMD_TRANS_REG_PAIR_3(psrl_dhs, psrl_hs) GEN_SIMD_TRANS_REG_PAIR_3(psra_dhs, psra_hs) GEN_SIMD_TRANS_REG_PAIR_3(pssha_dhs, pssha_hs) GEN_SIMD_TRANS_REG_PAIR_3(psshar_dhs, psshar_hs) +GEN_SIMD_TRANS_REG_PAIR_3(psshl_dhs, psshl_hs) +GEN_SIMD_TRANS_REG_PAIR_3(psshlr_dhs, psshlr_hs) GEN_SIMD_TRANS_REG_PAIR_DW(pssha_dws, ssha) GEN_SIMD_TRANS_REG_PAIR_DW(psshar_dws, sshar) +GEN_SIMD_TRANS_REG_PAIR_DW(psshl_dws, sshl) +GEN_SIMD_TRANS_REG_PAIR_DW(psshlr_dws, sshlr) GEN_SIMD_TRANS_REG_PAIR_2(ppairo_db, ppairo_b) GEN_SIMD_TRANS_REG_PAIR_2(ppairo_dh, ppairo_h)
target/riscv/psimd_helper.c +370 -0
382 lines not on this page · raw message
Signature · 1 line
2.34.1
Hi,
Quoted 6 lines · Molly Chen
This series adds support for the RISC-V Packed SIMD (P) extension.
The P extension defines packed-SIMD fixed-point operations intended for DSP-style workloads such as multimedia and signal processing. These instructions operate on packed subword elements within general-purpose registers (GPRs).
Thanks for the work in putting all this together.
I did a review in patch 2 that I believe applies to all other patches:
we should strive to code re-use and avoid any code duplication as we can. This would be a good goal for a version 2.
Quoted 10 lines
The implementation follows the current development draft of the specification (v0.20):
https://www.jhauser.us/RISCV/ext-P/All instructions defined prior to the v0.20 update were verified using a simple instruction coverage test suite:
A testsuite is greatly appreciated, in particular due to the amount of code included in this work.
Quoted 3 lines
Instructions newly introduced in v0.20 were not tested yet because toolchain support for generating them is still under development.
This is fine. You're adding experimental support so it's ok if it's not fully tested/completed, as long as we take care to document the limitations and caveats of the existing emulation.
Also, I talked with RVI. I got told that this spec can move from 'draft' to 'frozen' later this year (Q3, maybe Q4). Depending on the time-frame of the spec status change, and the amount of progress we can make in the review process, the spec might move to 'frozen' and we can drop the experimental tag and upstream it as a regular extension. Let's see how we do.
Thanks,
Daniel
Quoted 45 lines
The implementation focuses on functional correctness and ISA coverage. Performance optimizations were not considered at this stage.
Feedback on implementation details would be highly appreciated.
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
Molly Chen (14):
target/riscv: rvp: Add option defines and dependency check for packed
simd extension
target/riscv: rvp: add arithmetic instructions,including saturating
and non-saturating operations
target/riscv: rvp: add averaging operations
target/riscv: rvp: add absolute value and difference,comparison and
mask generation operations
target/riscv: rvp: add shift operations
target/riscv: rvp: add exchange operations
target/riscv: rvp: add horizontal reduction, pack, merge and cout
leading operations
target/riscv: rvp: add pure multiplication operations target/riscv: rvp: add multiply-accumulate operations target/riscv: rvp: add Q-format multiplication operations target/riscv: rvp: add two-way and four-way multiply and accumulate
operations
target/riscv: rvp: add load and replicate instructions. target/riscv: rvp: add rv32-only register-pair instructions target/riscv: rvp: update to v020, add SHL and PNCLIP[U]P.*
instructionstarget/riscv/cpu.c | 5 +- target/riscv/cpu.h | 1 + target/riscv/helper.h | 531 ++ target/riscv/insn32.decode | 829 ++ target/riscv/insn_trans/trans_rvp.c.inc | 1691 ++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 9452 +++++++++++++++++++++++ target/riscv/tcg/tcg-cpu.c | 16 + target/riscv/translate.c | 3 + 9 files changed, 12528 insertions(+), 3 deletions(-)create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644
target/riscv/psimd_helper.c
Thank you for your very specific feedback. I fully understand your point about eliminating code repetition — not only to reduce lines of code, but also to have fewer points of logic to take care of. I will review and revise the code according to your suggestions, reusing code where possible to minimize redundancy. These changes will be reflected in v2.
Thanks again!
Best regards,
Molly
Quoted 94 lines · Daniel Henrique Barboza
Hi,
Quoted 6 lines · Molly Chen
This series adds support for the RISC-V Packed SIMD (P) extension.
The P extension defines packed-SIMD fixed-point operations intended for DSP-style workloads such as multimedia and signal processing. These instructions operate on packed subword elements within general-purpose registers (GPRs).
Thanks for the work in putting all this together.
I did a review in patch 2 that I believe applies to all other patches:
we should strive to code re-use and avoid any code duplication as we can. This would be a good goal for a version 2.Quoted 10 lines
The implementation follows the current development draft of the specification (v0.20):
https://www.jhauser.us/RISCV/ext-P/
All instructions defined prior to the v0.20 update were verified using a simple instruction coverage test suite:
A testsuite is greatly appreciated, in particular due to the amount of code included in this work.
Quoted 3 lines
Instructions newly introduced in v0.20 were not tested yet because toolchain support for generating them is still under development.
This is fine. You're adding experimental support so it's ok if it's not fully tested/completed, as long as we take care to document the limitations and caveats of the existing emulation.
Also, I talked with RVI. I got told that this spec can move from 'draft' to 'frozen' later this year (Q3, maybe Q4). Depending on the time-frame of the spec status change, and the amount of progress we can make in the review process, the spec might move to 'frozen' and we can drop the experimental tag and upstream it as a regular extension. Let's see how we do.
Thanks,
DanielQuoted 45 lines
The implementation focuses on functional correctness and ISA coverage. Performance optimizations were not considered at this stage.
Feedback on implementation details would be highly appreciated.
Co-Authored by: Yin Zhang <[email protected]> Co-Authored by: Dajun Huang <[email protected]> Co-Authored by: Zhiyuan Yang <[email protected]>
Molly Chen (14):
target/riscv: rvp: Add option defines and dependency check for packed simd extension
target/riscv: rvp: add arithmetic instructions,including saturating and non-saturating operations
target/riscv: rvp: add averaging operations
target/riscv: rvp: add absolute value and difference,comparison and mask generation operations
target/riscv: rvp: add shift operations
target/riscv: rvp: add exchange operations
target/riscv: rvp: add horizontal reduction, pack, merge and cout leading operations
target/riscv: rvp: add pure multiplication operations target/riscv: rvp: add multiply-accumulate operations target/riscv: rvp: add Q-format multiplication operations target/riscv: rvp: add two-way and four-way multiply and accumulate operations
target/riscv: rvp: add load and replicate instructions. target/riscv: rvp: add rv32-only register-pair instructions target/riscv: rvp: update to v020, add SHL and PNCLIP[U]P.* instructionstarget/riscv/cpu.c | 5 +- target/riscv/cpu.h | 1 + target/riscv/helper.h | 531 ++ target/riscv/insn32.decode | 829 ++ target/riscv/insn_trans/trans_rvp.c.inc | 1691 ++++ target/riscv/meson.build | 3 +- target/riscv/psimd_helper.c | 9452 +++++++++++++++++++++++ target/riscv/tcg/tcg-cpu.c | 16 + target/riscv/translate.c | 3 +
9 files changed, 12528 insertions(+), 3 deletions(-)create mode 100644 target/riscv/insn_trans/trans_rvp.c.inc create mode 100644
target/riscv/psimd_helper.c