2 * RISC-V translation routines for the RV64F Standard Extension.
4 * Copyright (c) 2016-2017 Sagar Karandikar, sagark@eecs.berkeley.edu
5 * Copyright (c) 2018 Peer Adelt, peer.adelt@hni.uni-paderborn.de
6 * Bastian Koppelmann, kbastian@mail.uni-paderborn.de
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2 or later, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
21 #define REQUIRE_FPU do {\
22 if (ctx->mstatus_fs == EXT_STATUS_DISABLED) { \
23 ctx->virt_inst_excp = ctx->virt_enabled && ctx->cfg_ptr->ext_zfinx; \
28 #define REQUIRE_ZFINX_OR_F(ctx) do {\
29 if (!ctx->cfg_ptr->ext_zfinx) { \
30 REQUIRE_EXT(ctx, RVF); \
34 #define REQUIRE_ZCF_OR_FC(ctx) do { \
35 if (!ctx->cfg_ptr->ext_zcf) { \
36 if (!has_ext(ctx, RVF) || !has_ext(ctx, RVC)) { \
42 static bool trans_flw(DisasContext *ctx, arg_flw *a)
48 REQUIRE_EXT(ctx, RVF);
51 addr = get_address(ctx, a->rs1, a->imm);
52 dest = cpu_fpr[a->rd];
53 tcg_gen_qemu_ld_i64(dest, addr, ctx->mem_idx, MO_TEUL);
54 gen_nanbox_s(dest, dest);
60 static bool trans_fsw(DisasContext *ctx, arg_fsw *a)
65 REQUIRE_EXT(ctx, RVF);
68 addr = get_address(ctx, a->rs1, a->imm);
69 tcg_gen_qemu_st_i64(cpu_fpr[a->rs2], addr, ctx->mem_idx, MO_TEUL);
73 static bool trans_c_flw(DisasContext *ctx, arg_flw *a)
75 REQUIRE_ZCF_OR_FC(ctx);
76 return trans_flw(ctx, a);
79 static bool trans_c_fsw(DisasContext *ctx, arg_fsw *a)
81 REQUIRE_ZCF_OR_FC(ctx);
82 return trans_fsw(ctx, a);
85 static bool trans_fmadd_s(DisasContext *ctx, arg_fmadd_s *a)
88 REQUIRE_ZFINX_OR_F(ctx);
90 TCGv_i64 dest = dest_fpr(ctx, a->rd);
91 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
92 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
93 TCGv_i64 src3 = get_fpr_hs(ctx, a->rs3);
95 gen_set_rm(ctx, a->rm);
96 gen_helper_fmadd_s(dest, cpu_env, src1, src2, src3);
97 gen_set_fpr_hs(ctx, a->rd, dest);
102 static bool trans_fmsub_s(DisasContext *ctx, arg_fmsub_s *a)
105 REQUIRE_ZFINX_OR_F(ctx);
107 TCGv_i64 dest = dest_fpr(ctx, a->rd);
108 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
109 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
110 TCGv_i64 src3 = get_fpr_hs(ctx, a->rs3);
112 gen_set_rm(ctx, a->rm);
113 gen_helper_fmsub_s(dest, cpu_env, src1, src2, src3);
114 gen_set_fpr_hs(ctx, a->rd, dest);
119 static bool trans_fnmsub_s(DisasContext *ctx, arg_fnmsub_s *a)
122 REQUIRE_ZFINX_OR_F(ctx);
124 TCGv_i64 dest = dest_fpr(ctx, a->rd);
125 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
126 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
127 TCGv_i64 src3 = get_fpr_hs(ctx, a->rs3);
129 gen_set_rm(ctx, a->rm);
130 gen_helper_fnmsub_s(dest, cpu_env, src1, src2, src3);
131 gen_set_fpr_hs(ctx, a->rd, dest);
136 static bool trans_fnmadd_s(DisasContext *ctx, arg_fnmadd_s *a)
139 REQUIRE_ZFINX_OR_F(ctx);
141 TCGv_i64 dest = dest_fpr(ctx, a->rd);
142 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
143 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
144 TCGv_i64 src3 = get_fpr_hs(ctx, a->rs3);
146 gen_set_rm(ctx, a->rm);
147 gen_helper_fnmadd_s(dest, cpu_env, src1, src2, src3);
148 gen_set_fpr_hs(ctx, a->rd, dest);
153 static bool trans_fadd_s(DisasContext *ctx, arg_fadd_s *a)
156 REQUIRE_ZFINX_OR_F(ctx);
158 TCGv_i64 dest = dest_fpr(ctx, a->rd);
159 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
160 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
162 gen_set_rm(ctx, a->rm);
163 gen_helper_fadd_s(dest, cpu_env, src1, src2);
164 gen_set_fpr_hs(ctx, a->rd, dest);
169 static bool trans_fsub_s(DisasContext *ctx, arg_fsub_s *a)
172 REQUIRE_ZFINX_OR_F(ctx);
174 TCGv_i64 dest = dest_fpr(ctx, a->rd);
175 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
176 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
178 gen_set_rm(ctx, a->rm);
179 gen_helper_fsub_s(dest, cpu_env, src1, src2);
180 gen_set_fpr_hs(ctx, a->rd, dest);
185 static bool trans_fmul_s(DisasContext *ctx, arg_fmul_s *a)
188 REQUIRE_ZFINX_OR_F(ctx);
190 TCGv_i64 dest = dest_fpr(ctx, a->rd);
191 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
192 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
194 gen_set_rm(ctx, a->rm);
195 gen_helper_fmul_s(dest, cpu_env, src1, src2);
196 gen_set_fpr_hs(ctx, a->rd, dest);
201 static bool trans_fdiv_s(DisasContext *ctx, arg_fdiv_s *a)
204 REQUIRE_ZFINX_OR_F(ctx);
206 TCGv_i64 dest = dest_fpr(ctx, a->rd);
207 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
208 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
210 gen_set_rm(ctx, a->rm);
211 gen_helper_fdiv_s(dest, cpu_env, src1, src2);
212 gen_set_fpr_hs(ctx, a->rd, dest);
217 static bool trans_fsqrt_s(DisasContext *ctx, arg_fsqrt_s *a)
220 REQUIRE_ZFINX_OR_F(ctx);
222 TCGv_i64 dest = dest_fpr(ctx, a->rd);
223 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
225 gen_set_rm(ctx, a->rm);
226 gen_helper_fsqrt_s(dest, cpu_env, src1);
227 gen_set_fpr_hs(ctx, a->rd, dest);
232 static bool trans_fsgnj_s(DisasContext *ctx, arg_fsgnj_s *a)
235 REQUIRE_ZFINX_OR_F(ctx);
237 TCGv_i64 dest = dest_fpr(ctx, a->rd);
238 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
240 if (a->rs1 == a->rs2) { /* FMOV */
241 if (!ctx->cfg_ptr->ext_zfinx) {
242 gen_check_nanbox_s(dest, src1);
244 tcg_gen_ext32s_i64(dest, src1);
247 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
249 if (!ctx->cfg_ptr->ext_zfinx) {
250 TCGv_i64 rs1 = tcg_temp_new_i64();
251 TCGv_i64 rs2 = tcg_temp_new_i64();
252 gen_check_nanbox_s(rs1, src1);
253 gen_check_nanbox_s(rs2, src2);
255 /* This formulation retains the nanboxing of rs2 in normal 'F'. */
256 tcg_gen_deposit_i64(dest, rs2, rs1, 0, 31);
258 tcg_gen_deposit_i64(dest, src2, src1, 0, 31);
259 tcg_gen_ext32s_i64(dest, dest);
262 gen_set_fpr_hs(ctx, a->rd, dest);
267 static bool trans_fsgnjn_s(DisasContext *ctx, arg_fsgnjn_s *a)
269 TCGv_i64 rs1, rs2, mask;
272 REQUIRE_ZFINX_OR_F(ctx);
274 TCGv_i64 dest = dest_fpr(ctx, a->rd);
275 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
277 rs1 = tcg_temp_new_i64();
278 if (!ctx->cfg_ptr->ext_zfinx) {
279 gen_check_nanbox_s(rs1, src1);
281 tcg_gen_mov_i64(rs1, src1);
283 if (a->rs1 == a->rs2) { /* FNEG */
284 tcg_gen_xori_i64(dest, rs1, MAKE_64BIT_MASK(31, 1));
286 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
287 rs2 = tcg_temp_new_i64();
288 if (!ctx->cfg_ptr->ext_zfinx) {
289 gen_check_nanbox_s(rs2, src2);
291 tcg_gen_mov_i64(rs2, src2);
295 * Replace bit 31 in rs1 with inverse in rs2.
296 * This formulation retains the nanboxing of rs1.
298 mask = tcg_constant_i64(~MAKE_64BIT_MASK(31, 1));
299 tcg_gen_nor_i64(rs2, rs2, mask);
300 tcg_gen_and_i64(dest, mask, rs1);
301 tcg_gen_or_i64(dest, dest, rs2);
303 /* signed-extended intead of nanboxing for result if enable zfinx */
304 if (ctx->cfg_ptr->ext_zfinx) {
305 tcg_gen_ext32s_i64(dest, dest);
307 gen_set_fpr_hs(ctx, a->rd, dest);
312 static bool trans_fsgnjx_s(DisasContext *ctx, arg_fsgnjx_s *a)
317 REQUIRE_ZFINX_OR_F(ctx);
319 TCGv_i64 dest = dest_fpr(ctx, a->rd);
320 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
321 rs1 = tcg_temp_new_i64();
323 if (!ctx->cfg_ptr->ext_zfinx) {
324 gen_check_nanbox_s(rs1, src1);
326 tcg_gen_mov_i64(rs1, src1);
329 if (a->rs1 == a->rs2) { /* FABS */
330 tcg_gen_andi_i64(dest, rs1, ~MAKE_64BIT_MASK(31, 1));
332 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
333 rs2 = tcg_temp_new_i64();
335 if (!ctx->cfg_ptr->ext_zfinx) {
336 gen_check_nanbox_s(rs2, src2);
338 tcg_gen_mov_i64(rs2, src2);
342 * Xor bit 31 in rs1 with that in rs2.
343 * This formulation retains the nanboxing of rs1.
345 tcg_gen_andi_i64(dest, rs2, MAKE_64BIT_MASK(31, 1));
346 tcg_gen_xor_i64(dest, rs1, dest);
348 /* signed-extended intead of nanboxing for result if enable zfinx */
349 if (ctx->cfg_ptr->ext_zfinx) {
350 tcg_gen_ext32s_i64(dest, dest);
352 gen_set_fpr_hs(ctx, a->rd, dest);
357 static bool trans_fmin_s(DisasContext *ctx, arg_fmin_s *a)
360 REQUIRE_ZFINX_OR_F(ctx);
362 TCGv_i64 dest = dest_fpr(ctx, a->rd);
363 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
364 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
366 gen_helper_fmin_s(dest, cpu_env, src1, src2);
367 gen_set_fpr_hs(ctx, a->rd, dest);
372 static bool trans_fmax_s(DisasContext *ctx, arg_fmax_s *a)
375 REQUIRE_ZFINX_OR_F(ctx);
377 TCGv_i64 dest = dest_fpr(ctx, a->rd);
378 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
379 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
381 gen_helper_fmax_s(dest, cpu_env, src1, src2);
382 gen_set_fpr_hs(ctx, a->rd, dest);
387 static bool trans_fcvt_w_s(DisasContext *ctx, arg_fcvt_w_s *a)
390 REQUIRE_ZFINX_OR_F(ctx);
392 TCGv dest = dest_gpr(ctx, a->rd);
393 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
395 gen_set_rm(ctx, a->rm);
396 gen_helper_fcvt_w_s(dest, cpu_env, src1);
397 gen_set_gpr(ctx, a->rd, dest);
401 static bool trans_fcvt_wu_s(DisasContext *ctx, arg_fcvt_wu_s *a)
404 REQUIRE_ZFINX_OR_F(ctx);
406 TCGv dest = dest_gpr(ctx, a->rd);
407 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
409 gen_set_rm(ctx, a->rm);
410 gen_helper_fcvt_wu_s(dest, cpu_env, src1);
411 gen_set_gpr(ctx, a->rd, dest);
415 static bool trans_fmv_x_w(DisasContext *ctx, arg_fmv_x_w *a)
417 /* NOTE: This was FMV.X.S in an earlier version of the ISA spec! */
419 REQUIRE_ZFINX_OR_F(ctx);
421 TCGv dest = dest_gpr(ctx, a->rd);
422 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
423 #if defined(TARGET_RISCV64)
424 tcg_gen_ext32s_tl(dest, src1);
426 tcg_gen_extrl_i64_i32(dest, src1);
429 gen_set_gpr(ctx, a->rd, dest);
433 static bool trans_feq_s(DisasContext *ctx, arg_feq_s *a)
436 REQUIRE_ZFINX_OR_F(ctx);
438 TCGv dest = dest_gpr(ctx, a->rd);
439 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
440 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
442 gen_helper_feq_s(dest, cpu_env, src1, src2);
443 gen_set_gpr(ctx, a->rd, dest);
447 static bool trans_flt_s(DisasContext *ctx, arg_flt_s *a)
450 REQUIRE_ZFINX_OR_F(ctx);
452 TCGv dest = dest_gpr(ctx, a->rd);
453 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
454 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
456 gen_helper_flt_s(dest, cpu_env, src1, src2);
457 gen_set_gpr(ctx, a->rd, dest);
461 static bool trans_fle_s(DisasContext *ctx, arg_fle_s *a)
464 REQUIRE_ZFINX_OR_F(ctx);
466 TCGv dest = dest_gpr(ctx, a->rd);
467 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
468 TCGv_i64 src2 = get_fpr_hs(ctx, a->rs2);
470 gen_helper_fle_s(dest, cpu_env, src1, src2);
471 gen_set_gpr(ctx, a->rd, dest);
475 static bool trans_fclass_s(DisasContext *ctx, arg_fclass_s *a)
478 REQUIRE_ZFINX_OR_F(ctx);
480 TCGv dest = dest_gpr(ctx, a->rd);
481 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
483 gen_helper_fclass_s(dest, cpu_env, src1);
484 gen_set_gpr(ctx, a->rd, dest);
488 static bool trans_fcvt_s_w(DisasContext *ctx, arg_fcvt_s_w *a)
491 REQUIRE_ZFINX_OR_F(ctx);
493 TCGv_i64 dest = dest_fpr(ctx, a->rd);
494 TCGv src = get_gpr(ctx, a->rs1, EXT_SIGN);
496 gen_set_rm(ctx, a->rm);
497 gen_helper_fcvt_s_w(dest, cpu_env, src);
498 gen_set_fpr_hs(ctx, a->rd, dest);
503 static bool trans_fcvt_s_wu(DisasContext *ctx, arg_fcvt_s_wu *a)
506 REQUIRE_ZFINX_OR_F(ctx);
508 TCGv_i64 dest = dest_fpr(ctx, a->rd);
509 TCGv src = get_gpr(ctx, a->rs1, EXT_ZERO);
511 gen_set_rm(ctx, a->rm);
512 gen_helper_fcvt_s_wu(dest, cpu_env, src);
513 gen_set_fpr_hs(ctx, a->rd, dest);
518 static bool trans_fmv_w_x(DisasContext *ctx, arg_fmv_w_x *a)
520 /* NOTE: This was FMV.S.X in an earlier version of the ISA spec! */
522 REQUIRE_ZFINX_OR_F(ctx);
524 TCGv_i64 dest = dest_fpr(ctx, a->rd);
525 TCGv src = get_gpr(ctx, a->rs1, EXT_ZERO);
527 tcg_gen_extu_tl_i64(dest, src);
528 gen_nanbox_s(dest, dest);
529 gen_set_fpr_hs(ctx, a->rd, dest);
534 static bool trans_fcvt_l_s(DisasContext *ctx, arg_fcvt_l_s *a)
538 REQUIRE_ZFINX_OR_F(ctx);
540 TCGv dest = dest_gpr(ctx, a->rd);
541 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
543 gen_set_rm(ctx, a->rm);
544 gen_helper_fcvt_l_s(dest, cpu_env, src1);
545 gen_set_gpr(ctx, a->rd, dest);
549 static bool trans_fcvt_lu_s(DisasContext *ctx, arg_fcvt_lu_s *a)
553 REQUIRE_ZFINX_OR_F(ctx);
555 TCGv dest = dest_gpr(ctx, a->rd);
556 TCGv_i64 src1 = get_fpr_hs(ctx, a->rs1);
558 gen_set_rm(ctx, a->rm);
559 gen_helper_fcvt_lu_s(dest, cpu_env, src1);
560 gen_set_gpr(ctx, a->rd, dest);
564 static bool trans_fcvt_s_l(DisasContext *ctx, arg_fcvt_s_l *a)
568 REQUIRE_ZFINX_OR_F(ctx);
570 TCGv_i64 dest = dest_fpr(ctx, a->rd);
571 TCGv src = get_gpr(ctx, a->rs1, EXT_SIGN);
573 gen_set_rm(ctx, a->rm);
574 gen_helper_fcvt_s_l(dest, cpu_env, src);
575 gen_set_fpr_hs(ctx, a->rd, dest);
580 static bool trans_fcvt_s_lu(DisasContext *ctx, arg_fcvt_s_lu *a)
584 REQUIRE_ZFINX_OR_F(ctx);
586 TCGv_i64 dest = dest_fpr(ctx, a->rd);
587 TCGv src = get_gpr(ctx, a->rs1, EXT_ZERO);
589 gen_set_rm(ctx, a->rm);
590 gen_helper_fcvt_s_lu(dest, cpu_env, src);
591 gen_set_fpr_hs(ctx, a->rd, dest);