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 void gen_sari_w(TCGv dest, TCGv src1, target_long imm)
71{
72    tcg_gen_sextract_tl(dest, src1, imm, 32 - imm);
73}
74
75TRANS(sll_w, ALL, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_sll_w)
76TRANS(srl_w, ALL, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_srl_w)
77TRANS(sra_w, ALL, gen_rrr, EXT_SIGN, EXT_NONE, EXT_SIGN, gen_sra_w)
78TRANS(sll_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_sll_d)
79TRANS(srl_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_srl_d)
80TRANS(sra_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_sra_d)
81TRANS(rotr_w, 64, gen_rrr, EXT_ZERO, EXT_NONE, EXT_SIGN, gen_rotr_w)
82TRANS(rotr_d, 64, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_rotr_d)
83TRANS(slli_w, ALL, gen_rri_c, EXT_NONE, EXT_SIGN, tcg_gen_shli_tl)
84TRANS(slli_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_shli_tl)
85TRANS(srli_w, ALL, gen_rri_c, EXT_ZERO, EXT_SIGN, tcg_gen_shri_tl)
86TRANS(srli_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_shri_tl)
87TRANS(srai_w, ALL, gen_rri_c, EXT_NONE, EXT_NONE, gen_sari_w)
88TRANS(srai_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_sari_tl)
89TRANS(rotri_w, 64, gen_rri_v, EXT_NONE, EXT_NONE, gen_rotr_w)
90TRANS(rotri_d, 64, gen_rri_c, EXT_NONE, EXT_NONE, tcg_gen_rotri_tl)
91