1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
3 * Copyright (c) 2021 Loongson Technology Corporation Limited
4 */
5
6static void gen_sll_w(TCGv dest, TCGv src1, TCGv src2)
7{
8    TCGv t0 = tcg_temp_new();
9    tcg_gen_andi_tl(t0, src2, 0x1f);
10    tcg_gen_shl_tl(dest, src1, t0);
11}
12
13static void gen_srl_w(TCGv dest, TCGv src1, TCGv src2)
14{
15    TCGv t0 = tcg_temp_new();
16    tcg_gen_andi_tl(t0, src2, 0x1f);
17    tcg_gen_shr_tl(dest, src1, t0);
18}
19
20static void gen_sra_w(TCGv dest, TCGv src1, TCGv src2)
21{
22    TCGv t0 = tcg_temp_new();
23    tcg_gen_andi_tl(t0, src2, 0x1f);
24    tcg_gen_sar_tl(dest, src1, t0);
25}
26
27static void gen_sll_d(TCGv dest, TCGv src1, TCGv src2)
28{
29    TCGv t0 = tcg_temp_new();
30    tcg_gen_andi_tl(t0, src2, 0x3f);
31    tcg_gen_shl_tl(dest, src1, t0);
32}
33
34static void gen_srl_d(TCGv dest, TCGv src1, TCGv src2)
35{
36    TCGv t0 = tcg_temp_new();
37    tcg_gen_andi_tl(t0, src2, 0x3f);
38    tcg_gen_shr_tl(dest, src1, t0);
39}
40
41static void gen_sra_d(TCGv dest, TCGv src1, TCGv src2)
42{
43    TCGv t0 = tcg_temp_new();
44    tcg_gen_andi_tl(t0, src2, 0x3f);
45    tcg_gen_sar_tl(dest, src1, t0);
46}
47
48static void gen_rotr_w(TCGv dest, TCGv src1, TCGv src2)
49{
50    TCGv_i32 t1 = tcg_temp_new_i32();
51    TCGv_i32 t2 = tcg_temp_new_i32();
52    TCGv t0 = tcg_temp_new();
53
54    tcg_gen_andi_tl(t0, src2, 0x1f);
55
56    tcg_gen_trunc_tl_i32(t1, src1);
57    tcg_gen_trunc_tl_i32(t2, t0);
58
59    tcg_gen_rotr_i32(t1, t1, t2);
60    tcg_gen_ext_i32_tl(dest, t1);
61}
62
63static void gen_rotr_d(TCGv dest, TCGv src1, TCGv src2)
64{
65    TCGv t0 = tcg_temp_new();
66    tcg_gen_andi_tl(t0, src2, 0x3f);
67    tcg_gen_rotr_tl(dest, src1, t0);
68}
69
70static bool trans_srai_w(DisasContext *ctx, arg_srai_w *a)
71{
72    TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
73    TCGv src1 = gpr_src(ctx, a->rj, EXT_ZERO);
74
75    if (!avail_64(ctx)) {
76        return false;
77    }
78
79    tcg_gen_sextract_tl(dest, src1, a->imm, 32 - a->imm);
80    gen_set_gpr(a->rd, dest, EXT_NONE);
81
82    return true;
83}
84
85TRANS(sll_w, ALL, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_sll_w)
86TRANS(srl_w, ALL, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_srl_w)
87TRANS(sra_w, ALL, gen_rrr, EXT_SIGN, EXT_NONE, EXT_SIGN, gen_sra_w)
88TRANS(sll_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_sll_d)
89TRANS(srl_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_srl_d)
90TRANS(sra_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_sra_d)
91TRANS(rotr_w, 64, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_rotr_w)
92TRANS(rotr_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_rotr_d)
93TRANS(slli_w, ALL, gen_rri_c, EXT_NONE, EXT_SIGN, tcg_gen_shli_tl)
94TRANS(slli_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_shli_tl)
95TRANS(srli_w, ALL, gen_rri_c, EXT_ZERO, EXT_SIGN, tcg_gen_shri_tl)
96TRANS(srli_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_shri_tl)
97TRANS(srai_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_sari_tl)
98TRANS(rotri_w, 64, gen_rri_v, EXT_NONE, EXT_NONE, gen_rotr_w)
99TRANS(rotri_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_rotri_tl)
100