1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 * Copyright (c) 2021 Loongson Technology Corporation Limited
6 static bool gen_rr(DisasContext *ctx, arg_rr *a,
7 DisasExtend src_ext, DisasExtend dst_ext,
8 void (*func)(TCGv, TCGv))
10 TCGv dest = gpr_dst(ctx, a->rd, dst_ext);
11 TCGv src1 = gpr_src(ctx, a->rj, src_ext);
14 gen_set_gpr(a->rd, dest, dst_ext);
19 static void gen_bytepick_w(TCGv dest, TCGv src1, TCGv src2, target_long sa)
21 tcg_gen_concat_tl_i64(dest, src1, src2);
22 tcg_gen_sextract_i64(dest, dest, (32 - sa * 8), 32);
25 static void gen_bytepick_d(TCGv dest, TCGv src1, TCGv src2, target_long sa)
27 tcg_gen_extract2_i64(dest, src1, src2, (64 - sa * 8));
30 static bool gen_bstrins(DisasContext *ctx, arg_rr_ms_ls *a,
33 TCGv src1 = gpr_src(ctx, a->rd, EXT_NONE);
34 TCGv src2 = gpr_src(ctx, a->rj, EXT_NONE);
35 TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
41 tcg_gen_deposit_tl(dest, src1, src2, a->ls, a->ms - a->ls + 1);
42 gen_set_gpr(a->rd, dest, dst_ext);
46 static bool gen_bstrpick(DisasContext *ctx, arg_rr_ms_ls *a,
49 TCGv dest = gpr_dst(ctx, a->rd, EXT_NONE);
50 TCGv src1 = gpr_src(ctx, a->rj, EXT_NONE);
56 tcg_gen_extract_tl(dest, src1, a->ls, a->ms - a->ls + 1);
57 gen_set_gpr(a->rd, dest, dst_ext);
61 static void gen_clz_w(TCGv dest, TCGv src1)
63 tcg_gen_clzi_tl(dest, src1, TARGET_LONG_BITS);
64 tcg_gen_subi_tl(dest, dest, TARGET_LONG_BITS - 32);
67 static void gen_clo_w(TCGv dest, TCGv src1)
69 tcg_gen_not_tl(dest, src1);
70 tcg_gen_ext32u_tl(dest, dest);
71 gen_clz_w(dest, dest);
74 static void gen_ctz_w(TCGv dest, TCGv src1)
76 tcg_gen_ori_tl(dest, src1, (target_ulong)MAKE_64BIT_MASK(32, 32));
77 tcg_gen_ctzi_tl(dest, dest, TARGET_LONG_BITS);
80 static void gen_cto_w(TCGv dest, TCGv src1)
82 tcg_gen_not_tl(dest, src1);
83 gen_ctz_w(dest, dest);
86 static void gen_clz_d(TCGv dest, TCGv src1)
88 tcg_gen_clzi_i64(dest, src1, TARGET_LONG_BITS);
91 static void gen_clo_d(TCGv dest, TCGv src1)
93 tcg_gen_not_tl(dest, src1);
94 gen_clz_d(dest, dest);
97 static void gen_ctz_d(TCGv dest, TCGv src1)
99 tcg_gen_ctzi_tl(dest, src1, TARGET_LONG_BITS);
102 static void gen_cto_d(TCGv dest, TCGv src1)
104 tcg_gen_not_tl(dest, src1);
105 gen_ctz_d(dest, dest);
108 static void gen_revb_2w(TCGv dest, TCGv src1)
110 tcg_gen_bswap64_i64(dest, src1);
111 tcg_gen_rotri_i64(dest, dest, 32);
114 static void gen_revb_2h(TCGv dest, TCGv src1)
116 TCGv mask = tcg_constant_tl(0x00FF00FF);
117 TCGv t0 = tcg_temp_new();
118 TCGv t1 = tcg_temp_new();
120 tcg_gen_shri_tl(t0, src1, 8);
121 tcg_gen_and_tl(t0, t0, mask);
122 tcg_gen_and_tl(t1, src1, mask);
123 tcg_gen_shli_tl(t1, t1, 8);
124 tcg_gen_or_tl(dest, t0, t1);
127 static void gen_revb_4h(TCGv dest, TCGv src1)
129 TCGv mask = tcg_constant_tl(0x00FF00FF00FF00FFULL);
130 TCGv t0 = tcg_temp_new();
131 TCGv t1 = tcg_temp_new();
133 tcg_gen_shri_tl(t0, src1, 8);
134 tcg_gen_and_tl(t0, t0, mask);
135 tcg_gen_and_tl(t1, src1, mask);
136 tcg_gen_shli_tl(t1, t1, 8);
137 tcg_gen_or_tl(dest, t0, t1);
140 static void gen_revh_2w(TCGv dest, TCGv src1)
142 TCGv_i64 t0 = tcg_temp_new_i64();
143 TCGv_i64 t1 = tcg_temp_new_i64();
144 TCGv_i64 mask = tcg_constant_i64(0x0000ffff0000ffffull);
146 tcg_gen_shri_i64(t0, src1, 16);
147 tcg_gen_and_i64(t1, src1, mask);
148 tcg_gen_and_i64(t0, t0, mask);
149 tcg_gen_shli_i64(t1, t1, 16);
150 tcg_gen_or_i64(dest, t1, t0);
153 static void gen_revh_d(TCGv dest, TCGv src1)
155 TCGv t0 = tcg_temp_new();
156 TCGv t1 = tcg_temp_new();
157 TCGv mask = tcg_constant_tl(0x0000FFFF0000FFFFULL);
159 tcg_gen_shri_tl(t1, src1, 16);
160 tcg_gen_and_tl(t1, t1, mask);
161 tcg_gen_and_tl(t0, src1, mask);
162 tcg_gen_shli_tl(t0, t0, 16);
163 tcg_gen_or_tl(t0, t0, t1);
164 tcg_gen_rotri_tl(dest, t0, 32);
167 static void gen_maskeqz(TCGv dest, TCGv src1, TCGv src2)
169 TCGv zero = tcg_constant_tl(0);
171 tcg_gen_movcond_tl(TCG_COND_EQ, dest, src2, zero, zero, src1);
174 static void gen_masknez(TCGv dest, TCGv src1, TCGv src2)
176 TCGv zero = tcg_constant_tl(0);
178 tcg_gen_movcond_tl(TCG_COND_NE, dest, src2, zero, zero, src1);
181 TRANS(ext_w_h, ALL, gen_rr, EXT_NONE, EXT_NONE, tcg_gen_ext16s_tl)
182 TRANS(ext_w_b, ALL, gen_rr, EXT_NONE, EXT_NONE, tcg_gen_ext8s_tl)
183 TRANS(clo_w, ALL, gen_rr, EXT_NONE, EXT_NONE, gen_clo_w)
184 TRANS(clz_w, ALL, gen_rr, EXT_ZERO, EXT_NONE, gen_clz_w)
185 TRANS(cto_w, ALL, gen_rr, EXT_NONE, EXT_NONE, gen_cto_w)
186 TRANS(ctz_w, ALL, gen_rr, EXT_NONE, EXT_NONE, gen_ctz_w)
187 TRANS(clo_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_clo_d)
188 TRANS(clz_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_clz_d)
189 TRANS(cto_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_cto_d)
190 TRANS(ctz_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_ctz_d)
191 TRANS(revb_2h, ALL, gen_rr, EXT_NONE, EXT_SIGN, gen_revb_2h)
192 TRANS(revb_4h, 64, gen_rr, EXT_NONE, EXT_NONE, gen_revb_4h)
193 TRANS(revb_2w, 64, gen_rr, EXT_NONE, EXT_NONE, gen_revb_2w)
194 TRANS(revb_d, 64, gen_rr, EXT_NONE, EXT_NONE, tcg_gen_bswap64_i64)
195 TRANS(revh_2w, 64, gen_rr, EXT_NONE, EXT_NONE, gen_revh_2w)
196 TRANS(revh_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_revh_d)
197 TRANS(bitrev_4b, ALL, gen_rr, EXT_ZERO, EXT_SIGN, gen_helper_bitswap)
198 TRANS(bitrev_8b, 64, gen_rr, EXT_NONE, EXT_NONE, gen_helper_bitswap)
199 TRANS(bitrev_w, ALL, gen_rr, EXT_NONE, EXT_SIGN, gen_helper_bitrev_w)
200 TRANS(bitrev_d, 64, gen_rr, EXT_NONE, EXT_NONE, gen_helper_bitrev_d)
201 TRANS(maskeqz, ALL, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_maskeqz)
202 TRANS(masknez, ALL, gen_rrr, EXT_NONE, EXT_NONE, EXT_NONE, gen_masknez)
203 TRANS(bytepick_w, ALL, gen_rrr_sa, EXT_NONE, EXT_NONE, gen_bytepick_w)
204 TRANS(bytepick_d, 64, gen_rrr_sa, EXT_NONE, EXT_NONE, gen_bytepick_d)
205 TRANS(bstrins_w, ALL, gen_bstrins, EXT_SIGN)
206 TRANS(bstrins_d, 64, gen_bstrins, EXT_NONE)
207 TRANS(bstrpick_w, ALL, gen_bstrpick, EXT_SIGN)
208 TRANS(bstrpick_d, 64, gen_bstrpick, EXT_NONE)