xref: /openbmc/qemu/target/riscv/insn_trans/trans_rvk.c.inc (revision f003dd8d81f7d88f4b1f8802309eaa76f6eb223a)
168d19b58SWeiwei Li/*
268d19b58SWeiwei Li * RISC-V translation routines for the Zk[nd,ne,nh,sed,sh] Standard Extension.
368d19b58SWeiwei Li *
468d19b58SWeiwei Li * Copyright (c) 2021 Ruibo Lu, luruibo2000@163.com
568d19b58SWeiwei Li * Copyright (c) 2021 Zewen Ye, lustrew@foxmail.com
668d19b58SWeiwei Li *
768d19b58SWeiwei Li * This program is free software; you can redistribute it and/or modify it
868d19b58SWeiwei Li * under the terms and conditions of the GNU General Public License,
968d19b58SWeiwei Li * version 2 or later, as published by the Free Software Foundation.
1068d19b58SWeiwei Li *
1168d19b58SWeiwei Li * This program is distributed in the hope it will be useful, but WITHOUT
1268d19b58SWeiwei Li * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1368d19b58SWeiwei Li * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
1468d19b58SWeiwei Li * more details.
1568d19b58SWeiwei Li *
1668d19b58SWeiwei Li * You should have received a copy of the GNU General Public License along with
1768d19b58SWeiwei Li * this program.  If not, see <http://www.gnu.org/licenses/>.
1868d19b58SWeiwei Li */
1968d19b58SWeiwei Li
2068d19b58SWeiwei Li#define REQUIRE_ZKND(ctx) do {                  \
2168d19b58SWeiwei Li    if (!ctx->cfg_ptr->ext_zknd) {              \
2268d19b58SWeiwei Li        return false;                           \
2368d19b58SWeiwei Li    }                                           \
2468d19b58SWeiwei Li} while (0)
2568d19b58SWeiwei Li
2668d19b58SWeiwei Li#define REQUIRE_ZKNE(ctx) do {                  \
2768d19b58SWeiwei Li    if (!ctx->cfg_ptr->ext_zkne) {              \
2868d19b58SWeiwei Li        return false;                           \
2968d19b58SWeiwei Li    }                                           \
3068d19b58SWeiwei Li} while (0)
3168d19b58SWeiwei Li
32387e5d92SWeiwei Li#define REQUIRE_ZKNH(ctx) do {                  \
33387e5d92SWeiwei Li    if (!ctx->cfg_ptr->ext_zknh) {              \
34387e5d92SWeiwei Li        return false;                           \
35387e5d92SWeiwei Li    }                                           \
36387e5d92SWeiwei Li} while (0)
37387e5d92SWeiwei Li
38*0976083dSWeiwei Li#define REQUIRE_ZKSED(ctx) do {                 \
39*0976083dSWeiwei Li    if (!ctx->cfg_ptr->ext_zksed) {             \
40*0976083dSWeiwei Li        return false;                           \
41*0976083dSWeiwei Li    }                                           \
42*0976083dSWeiwei Li} while (0)
43*0976083dSWeiwei Li
44*0976083dSWeiwei Li#define REQUIRE_ZKSH(ctx) do {                  \
45*0976083dSWeiwei Li    if (!ctx->cfg_ptr->ext_zksh) {              \
46*0976083dSWeiwei Li        return false;                           \
47*0976083dSWeiwei Li    }                                           \
48*0976083dSWeiwei Li} while (0)
49*0976083dSWeiwei Li
5068d19b58SWeiwei Listatic bool gen_aes32_sm4(DisasContext *ctx, arg_k_aes *a,
5168d19b58SWeiwei Li                          void (*func)(TCGv, TCGv, TCGv, TCGv))
5268d19b58SWeiwei Li{
5368d19b58SWeiwei Li    TCGv shamt = tcg_constant_tl(a->shamt);
5468d19b58SWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
5568d19b58SWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
5668d19b58SWeiwei Li    TCGv src2 = get_gpr(ctx, a->rs2, EXT_NONE);
5768d19b58SWeiwei Li
5868d19b58SWeiwei Li    func(dest, src1, src2, shamt);
5968d19b58SWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
6068d19b58SWeiwei Li    return true;
6168d19b58SWeiwei Li}
6268d19b58SWeiwei Li
6368d19b58SWeiwei Listatic bool trans_aes32esmi(DisasContext *ctx, arg_aes32esmi *a)
6468d19b58SWeiwei Li{
6568d19b58SWeiwei Li    REQUIRE_32BIT(ctx);
6668d19b58SWeiwei Li    REQUIRE_ZKNE(ctx);
6768d19b58SWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_aes32esmi);
6868d19b58SWeiwei Li}
6968d19b58SWeiwei Li
7068d19b58SWeiwei Listatic bool trans_aes32esi(DisasContext *ctx, arg_aes32esi *a)
7168d19b58SWeiwei Li{
7268d19b58SWeiwei Li    REQUIRE_32BIT(ctx);
7368d19b58SWeiwei Li    REQUIRE_ZKNE(ctx);
7468d19b58SWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_aes32esi);
7568d19b58SWeiwei Li}
7668d19b58SWeiwei Li
7768d19b58SWeiwei Listatic bool trans_aes32dsmi(DisasContext *ctx, arg_aes32dsmi *a)
7868d19b58SWeiwei Li{
7968d19b58SWeiwei Li    REQUIRE_32BIT(ctx);
8068d19b58SWeiwei Li    REQUIRE_ZKND(ctx);
8168d19b58SWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_aes32dsmi);
8268d19b58SWeiwei Li}
8368d19b58SWeiwei Li
8468d19b58SWeiwei Listatic bool trans_aes32dsi(DisasContext *ctx, arg_aes32dsi *a)
8568d19b58SWeiwei Li{
8668d19b58SWeiwei Li    REQUIRE_32BIT(ctx);
8768d19b58SWeiwei Li    REQUIRE_ZKND(ctx);
8868d19b58SWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_aes32dsi);
8968d19b58SWeiwei Li}
909e33e175SWeiwei Li
919e33e175SWeiwei Listatic bool trans_aes64es(DisasContext *ctx, arg_aes64es *a)
929e33e175SWeiwei Li{
939e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
949e33e175SWeiwei Li    REQUIRE_ZKNE(ctx);
959e33e175SWeiwei Li    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64es, NULL);
969e33e175SWeiwei Li}
979e33e175SWeiwei Li
989e33e175SWeiwei Listatic bool trans_aes64esm(DisasContext *ctx, arg_aes64esm *a)
999e33e175SWeiwei Li{
1009e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1019e33e175SWeiwei Li    REQUIRE_ZKNE(ctx);
1029e33e175SWeiwei Li    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64esm, NULL);
1039e33e175SWeiwei Li}
1049e33e175SWeiwei Li
1059e33e175SWeiwei Listatic bool trans_aes64ds(DisasContext *ctx, arg_aes64ds *a)
1069e33e175SWeiwei Li{
1079e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1089e33e175SWeiwei Li    REQUIRE_ZKND(ctx);
1099e33e175SWeiwei Li    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64ds, NULL);
1109e33e175SWeiwei Li}
1119e33e175SWeiwei Li
1129e33e175SWeiwei Listatic bool trans_aes64dsm(DisasContext *ctx, arg_aes64dsm *a)
1139e33e175SWeiwei Li{
1149e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1159e33e175SWeiwei Li    REQUIRE_ZKND(ctx);
1169e33e175SWeiwei Li    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64dsm, NULL);
1179e33e175SWeiwei Li}
1189e33e175SWeiwei Li
1199e33e175SWeiwei Listatic bool trans_aes64ks2(DisasContext *ctx, arg_aes64ks2 *a)
1209e33e175SWeiwei Li{
1219e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1229e33e175SWeiwei Li    REQUIRE_EITHER_EXT(ctx, zknd, zkne);
1239e33e175SWeiwei Li    return gen_arith(ctx, a, EXT_NONE, gen_helper_aes64ks2, NULL);
1249e33e175SWeiwei Li}
1259e33e175SWeiwei Li
1269e33e175SWeiwei Listatic bool trans_aes64ks1i(DisasContext *ctx, arg_aes64ks1i *a)
1279e33e175SWeiwei Li{
1289e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1299e33e175SWeiwei Li    REQUIRE_EITHER_EXT(ctx, zknd, zkne);
1309e33e175SWeiwei Li
1319e33e175SWeiwei Li    if (a->imm > 0xA) {
1329e33e175SWeiwei Li        return false;
1339e33e175SWeiwei Li    }
1349e33e175SWeiwei Li
1359e33e175SWeiwei Li    return gen_arith_imm_tl(ctx, a, EXT_NONE, gen_helper_aes64ks1i, NULL);
1369e33e175SWeiwei Li}
1379e33e175SWeiwei Li
1389e33e175SWeiwei Listatic bool trans_aes64im(DisasContext *ctx, arg_aes64im *a)
1399e33e175SWeiwei Li{
1409e33e175SWeiwei Li    REQUIRE_64BIT(ctx);
1419e33e175SWeiwei Li    REQUIRE_ZKND(ctx);
1429e33e175SWeiwei Li    return gen_unary(ctx, a, EXT_NONE, gen_helper_aes64im);
1439e33e175SWeiwei Li}
144387e5d92SWeiwei Li
145387e5d92SWeiwei Listatic bool gen_sha256(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
146387e5d92SWeiwei Li                       void (*func)(TCGv_i32, TCGv_i32, int32_t),
147387e5d92SWeiwei Li                       int32_t num1, int32_t num2, int32_t num3)
148387e5d92SWeiwei Li{
149387e5d92SWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
150387e5d92SWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, ext);
151387e5d92SWeiwei Li    TCGv_i32 t0 = tcg_temp_new_i32();
152387e5d92SWeiwei Li    TCGv_i32 t1 = tcg_temp_new_i32();
153387e5d92SWeiwei Li    TCGv_i32 t2 = tcg_temp_new_i32();
154387e5d92SWeiwei Li
155387e5d92SWeiwei Li    tcg_gen_trunc_tl_i32(t0, src1);
156387e5d92SWeiwei Li    tcg_gen_rotri_i32(t1, t0, num1);
157387e5d92SWeiwei Li    tcg_gen_rotri_i32(t2, t0, num2);
158387e5d92SWeiwei Li    tcg_gen_xor_i32(t1, t1, t2);
159387e5d92SWeiwei Li    func(t2, t0, num3);
160387e5d92SWeiwei Li    tcg_gen_xor_i32(t1, t1, t2);
161387e5d92SWeiwei Li    tcg_gen_ext_i32_tl(dest, t1);
162387e5d92SWeiwei Li
163387e5d92SWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
164387e5d92SWeiwei Li    return true;
165387e5d92SWeiwei Li}
166387e5d92SWeiwei Li
167387e5d92SWeiwei Listatic bool trans_sha256sig0(DisasContext *ctx, arg_sha256sig0 *a)
168387e5d92SWeiwei Li{
169387e5d92SWeiwei Li    REQUIRE_ZKNH(ctx);
170387e5d92SWeiwei Li    return gen_sha256(ctx, a, EXT_NONE, tcg_gen_shri_i32, 7, 18, 3);
171387e5d92SWeiwei Li}
172387e5d92SWeiwei Li
173387e5d92SWeiwei Listatic bool trans_sha256sig1(DisasContext *ctx, arg_sha256sig1 *a)
174387e5d92SWeiwei Li{
175387e5d92SWeiwei Li    REQUIRE_ZKNH(ctx);
176387e5d92SWeiwei Li    return gen_sha256(ctx, a, EXT_NONE, tcg_gen_shri_i32, 17, 19, 10);
177387e5d92SWeiwei Li}
178387e5d92SWeiwei Li
179387e5d92SWeiwei Listatic bool trans_sha256sum0(DisasContext *ctx, arg_sha256sum0 *a)
180387e5d92SWeiwei Li{
181387e5d92SWeiwei Li    REQUIRE_ZKNH(ctx);
182387e5d92SWeiwei Li    return gen_sha256(ctx, a, EXT_NONE, tcg_gen_rotri_i32, 2, 13, 22);
183387e5d92SWeiwei Li}
184387e5d92SWeiwei Li
185387e5d92SWeiwei Listatic bool trans_sha256sum1(DisasContext *ctx, arg_sha256sum1 *a)
186387e5d92SWeiwei Li{
187387e5d92SWeiwei Li    REQUIRE_ZKNH(ctx);
188387e5d92SWeiwei Li    return gen_sha256(ctx, a, EXT_NONE, tcg_gen_rotri_i32, 6, 11, 25);
189387e5d92SWeiwei Li}
190e9a7ef5dSWeiwei Li
191e9a7ef5dSWeiwei Listatic bool gen_sha512_rv32(DisasContext *ctx, arg_r *a, DisasExtend ext,
192e9a7ef5dSWeiwei Li                            void (*func1)(TCGv_i64, TCGv_i64, int64_t),
193e9a7ef5dSWeiwei Li                            void (*func2)(TCGv_i64, TCGv_i64, int64_t),
194e9a7ef5dSWeiwei Li                            int64_t num1, int64_t num2, int64_t num3)
195e9a7ef5dSWeiwei Li{
196e9a7ef5dSWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
197e9a7ef5dSWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, ext);
198e9a7ef5dSWeiwei Li    TCGv src2 = get_gpr(ctx, a->rs2, ext);
199e9a7ef5dSWeiwei Li    TCGv_i64 t0 = tcg_temp_new_i64();
200e9a7ef5dSWeiwei Li    TCGv_i64 t1 = tcg_temp_new_i64();
201e9a7ef5dSWeiwei Li    TCGv_i64 t2 = tcg_temp_new_i64();
202e9a7ef5dSWeiwei Li
203e9a7ef5dSWeiwei Li    tcg_gen_concat_tl_i64(t0, src1, src2);
204e9a7ef5dSWeiwei Li    func1(t1, t0, num1);
205e9a7ef5dSWeiwei Li    func2(t2, t0, num2);
206e9a7ef5dSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
207e9a7ef5dSWeiwei Li    tcg_gen_rotri_i64(t2, t0, num3);
208e9a7ef5dSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
209e9a7ef5dSWeiwei Li    tcg_gen_trunc_i64_tl(dest, t1);
210e9a7ef5dSWeiwei Li
211e9a7ef5dSWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
212e9a7ef5dSWeiwei Li    return true;
213e9a7ef5dSWeiwei Li}
214e9a7ef5dSWeiwei Li
215e9a7ef5dSWeiwei Listatic bool trans_sha512sum0r(DisasContext *ctx, arg_sha512sum0r *a)
216e9a7ef5dSWeiwei Li{
217e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
218e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
219e9a7ef5dSWeiwei Li    return gen_sha512_rv32(ctx, a, EXT_NONE, tcg_gen_rotli_i64,
220e9a7ef5dSWeiwei Li                           tcg_gen_rotli_i64, 25, 30, 28);
221e9a7ef5dSWeiwei Li}
222e9a7ef5dSWeiwei Li
223e9a7ef5dSWeiwei Listatic bool trans_sha512sum1r(DisasContext *ctx, arg_sha512sum1r *a)
224e9a7ef5dSWeiwei Li{
225e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
226e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
227e9a7ef5dSWeiwei Li    return gen_sha512_rv32(ctx, a, EXT_NONE, tcg_gen_rotli_i64,
228e9a7ef5dSWeiwei Li                           tcg_gen_rotri_i64, 23, 14, 18);
229e9a7ef5dSWeiwei Li}
230e9a7ef5dSWeiwei Li
231e9a7ef5dSWeiwei Listatic bool trans_sha512sig0l(DisasContext *ctx, arg_sha512sig0l *a)
232e9a7ef5dSWeiwei Li{
233e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
234e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
235e9a7ef5dSWeiwei Li    return gen_sha512_rv32(ctx, a, EXT_NONE, tcg_gen_rotri_i64,
236e9a7ef5dSWeiwei Li                           tcg_gen_rotri_i64, 1, 7, 8);
237e9a7ef5dSWeiwei Li}
238e9a7ef5dSWeiwei Li
239e9a7ef5dSWeiwei Listatic bool trans_sha512sig1l(DisasContext *ctx, arg_sha512sig1l *a)
240e9a7ef5dSWeiwei Li{
241e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
242e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
243e9a7ef5dSWeiwei Li    return gen_sha512_rv32(ctx, a, EXT_NONE, tcg_gen_rotli_i64,
244e9a7ef5dSWeiwei Li                           tcg_gen_rotri_i64, 3, 6, 19);
245e9a7ef5dSWeiwei Li}
246e9a7ef5dSWeiwei Li
247e9a7ef5dSWeiwei Listatic bool gen_sha512h_rv32(DisasContext *ctx, arg_r *a, DisasExtend ext,
248e9a7ef5dSWeiwei Li                             void (*func)(TCGv_i64, TCGv_i64, int64_t),
249e9a7ef5dSWeiwei Li                             int64_t num1, int64_t num2, int64_t num3)
250e9a7ef5dSWeiwei Li{
251e9a7ef5dSWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
252e9a7ef5dSWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, ext);
253e9a7ef5dSWeiwei Li    TCGv src2 = get_gpr(ctx, a->rs2, ext);
254e9a7ef5dSWeiwei Li    TCGv_i64 t0 = tcg_temp_new_i64();
255e9a7ef5dSWeiwei Li    TCGv_i64 t1 = tcg_temp_new_i64();
256e9a7ef5dSWeiwei Li    TCGv_i64 t2 = tcg_temp_new_i64();
257e9a7ef5dSWeiwei Li
258e9a7ef5dSWeiwei Li    tcg_gen_concat_tl_i64(t0, src1, src2);
259e9a7ef5dSWeiwei Li    func(t1, t0, num1);
260e9a7ef5dSWeiwei Li    tcg_gen_ext32u_i64(t2, t0);
261e9a7ef5dSWeiwei Li    tcg_gen_shri_i64(t2, t2, num2);
262e9a7ef5dSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
263e9a7ef5dSWeiwei Li    tcg_gen_rotri_i64(t2, t0, num3);
264e9a7ef5dSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
265e9a7ef5dSWeiwei Li    tcg_gen_trunc_i64_tl(dest, t1);
266e9a7ef5dSWeiwei Li
267e9a7ef5dSWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
268e9a7ef5dSWeiwei Li    return true;
269e9a7ef5dSWeiwei Li}
270e9a7ef5dSWeiwei Li
271e9a7ef5dSWeiwei Listatic bool trans_sha512sig0h(DisasContext *ctx, arg_sha512sig0h *a)
272e9a7ef5dSWeiwei Li{
273e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
274e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
275e9a7ef5dSWeiwei Li    return gen_sha512h_rv32(ctx, a, EXT_NONE, tcg_gen_rotri_i64, 1, 7, 8);
276e9a7ef5dSWeiwei Li}
277e9a7ef5dSWeiwei Li
278e9a7ef5dSWeiwei Listatic bool trans_sha512sig1h(DisasContext *ctx, arg_sha512sig1h *a)
279e9a7ef5dSWeiwei Li{
280e9a7ef5dSWeiwei Li    REQUIRE_32BIT(ctx);
281e9a7ef5dSWeiwei Li    REQUIRE_ZKNH(ctx);
282e9a7ef5dSWeiwei Li    return gen_sha512h_rv32(ctx, a, EXT_NONE, tcg_gen_rotli_i64, 3, 6, 19);
283e9a7ef5dSWeiwei Li}
2841f7f7b5eSWeiwei Li
2851f7f7b5eSWeiwei Listatic bool gen_sha512_rv64(DisasContext *ctx, arg_r2 *a, DisasExtend ext,
2861f7f7b5eSWeiwei Li                            void (*func)(TCGv_i64, TCGv_i64, int64_t),
2871f7f7b5eSWeiwei Li                            int64_t num1, int64_t num2, int64_t num3)
2881f7f7b5eSWeiwei Li{
2891f7f7b5eSWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
2901f7f7b5eSWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, ext);
2911f7f7b5eSWeiwei Li    TCGv_i64 t0 = tcg_temp_new_i64();
2921f7f7b5eSWeiwei Li    TCGv_i64 t1 = tcg_temp_new_i64();
2931f7f7b5eSWeiwei Li    TCGv_i64 t2 = tcg_temp_new_i64();
2941f7f7b5eSWeiwei Li
2951f7f7b5eSWeiwei Li    tcg_gen_extu_tl_i64(t0, src1);
2961f7f7b5eSWeiwei Li    tcg_gen_rotri_i64(t1, t0, num1);
2971f7f7b5eSWeiwei Li    tcg_gen_rotri_i64(t2, t0, num2);
2981f7f7b5eSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
2991f7f7b5eSWeiwei Li    func(t2, t0, num3);
3001f7f7b5eSWeiwei Li    tcg_gen_xor_i64(t1, t1, t2);
3011f7f7b5eSWeiwei Li    tcg_gen_trunc_i64_tl(dest, t1);
3021f7f7b5eSWeiwei Li
3031f7f7b5eSWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
3041f7f7b5eSWeiwei Li    return true;
3051f7f7b5eSWeiwei Li}
3061f7f7b5eSWeiwei Li
3071f7f7b5eSWeiwei Listatic bool trans_sha512sig0(DisasContext *ctx, arg_sha512sig0 *a)
3081f7f7b5eSWeiwei Li{
3091f7f7b5eSWeiwei Li    REQUIRE_64BIT(ctx);
3101f7f7b5eSWeiwei Li    REQUIRE_ZKNH(ctx);
3111f7f7b5eSWeiwei Li    return gen_sha512_rv64(ctx, a, EXT_NONE, tcg_gen_shri_i64, 1, 8, 7);
3121f7f7b5eSWeiwei Li}
3131f7f7b5eSWeiwei Li
3141f7f7b5eSWeiwei Listatic bool trans_sha512sig1(DisasContext *ctx, arg_sha512sig1 *a)
3151f7f7b5eSWeiwei Li{
3161f7f7b5eSWeiwei Li    REQUIRE_64BIT(ctx);
3171f7f7b5eSWeiwei Li    REQUIRE_ZKNH(ctx);
3181f7f7b5eSWeiwei Li    return gen_sha512_rv64(ctx, a, EXT_NONE, tcg_gen_shri_i64, 19, 61, 6);
3191f7f7b5eSWeiwei Li}
3201f7f7b5eSWeiwei Li
3211f7f7b5eSWeiwei Listatic bool trans_sha512sum0(DisasContext *ctx, arg_sha512sum0 *a)
3221f7f7b5eSWeiwei Li{
3231f7f7b5eSWeiwei Li    REQUIRE_64BIT(ctx);
3241f7f7b5eSWeiwei Li    REQUIRE_ZKNH(ctx);
3251f7f7b5eSWeiwei Li    return gen_sha512_rv64(ctx, a, EXT_NONE, tcg_gen_rotri_i64, 28, 34, 39);
3261f7f7b5eSWeiwei Li}
3271f7f7b5eSWeiwei Li
3281f7f7b5eSWeiwei Listatic bool trans_sha512sum1(DisasContext *ctx, arg_sha512sum1 *a)
3291f7f7b5eSWeiwei Li{
3301f7f7b5eSWeiwei Li    REQUIRE_64BIT(ctx);
3311f7f7b5eSWeiwei Li    REQUIRE_ZKNH(ctx);
3321f7f7b5eSWeiwei Li    return gen_sha512_rv64(ctx, a, EXT_NONE, tcg_gen_rotri_i64, 14, 18, 41);
3331f7f7b5eSWeiwei Li}
334*0976083dSWeiwei Li
335*0976083dSWeiwei Li/* SM3 */
336*0976083dSWeiwei Listatic bool gen_sm3(DisasContext *ctx, arg_r2 *a, int32_t b, int32_t c)
337*0976083dSWeiwei Li{
338*0976083dSWeiwei Li    TCGv dest = dest_gpr(ctx, a->rd);
339*0976083dSWeiwei Li    TCGv src1 = get_gpr(ctx, a->rs1, EXT_NONE);
340*0976083dSWeiwei Li    TCGv_i32 t0 = tcg_temp_new_i32();
341*0976083dSWeiwei Li    TCGv_i32 t1 = tcg_temp_new_i32();
342*0976083dSWeiwei Li
343*0976083dSWeiwei Li    tcg_gen_trunc_tl_i32(t0, src1);
344*0976083dSWeiwei Li    tcg_gen_rotli_i32(t1, t0, b);
345*0976083dSWeiwei Li    tcg_gen_xor_i32(t1, t0, t1);
346*0976083dSWeiwei Li    tcg_gen_rotli_i32(t0, t0, c);
347*0976083dSWeiwei Li    tcg_gen_xor_i32(t1, t1, t0);
348*0976083dSWeiwei Li    tcg_gen_ext_i32_tl(dest, t1);
349*0976083dSWeiwei Li    gen_set_gpr(ctx, a->rd, dest);
350*0976083dSWeiwei Li    return true;
351*0976083dSWeiwei Li}
352*0976083dSWeiwei Li
353*0976083dSWeiwei Listatic bool trans_sm3p0(DisasContext *ctx, arg_sm3p0 *a)
354*0976083dSWeiwei Li{
355*0976083dSWeiwei Li    REQUIRE_ZKSH(ctx);
356*0976083dSWeiwei Li    return gen_sm3(ctx, a, 9, 17);
357*0976083dSWeiwei Li}
358*0976083dSWeiwei Li
359*0976083dSWeiwei Listatic bool trans_sm3p1(DisasContext *ctx, arg_sm3p1 *a)
360*0976083dSWeiwei Li{
361*0976083dSWeiwei Li    REQUIRE_ZKSH(ctx);
362*0976083dSWeiwei Li    return gen_sm3(ctx, a, 15, 23);
363*0976083dSWeiwei Li}
364*0976083dSWeiwei Li
365*0976083dSWeiwei Li/* SM4 */
366*0976083dSWeiwei Listatic bool trans_sm4ed(DisasContext *ctx, arg_sm4ed *a)
367*0976083dSWeiwei Li{
368*0976083dSWeiwei Li    REQUIRE_ZKSED(ctx);
369*0976083dSWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_sm4ed);
370*0976083dSWeiwei Li}
371*0976083dSWeiwei Li
372*0976083dSWeiwei Listatic bool trans_sm4ks(DisasContext *ctx, arg_sm4ks *a)
373*0976083dSWeiwei Li{
374*0976083dSWeiwei Li    REQUIRE_ZKSED(ctx);
375*0976083dSWeiwei Li    return gen_aes32_sm4(ctx, a, gen_helper_sm4ks);
376*0976083dSWeiwei Li}
377