From 67395906490afb8240f7ec1faaacbd35fde6c78b Mon Sep 17 00:00:00 2001 From: Hung-Chun Tseng Date: Thu, 10 Sep 2026 23:17:42 +0800 Subject: [PATCH] Implement RVV narrowing floating-point conversions Add the eight vfncvt forms (V 1.0 section 13.19), producing a 32-bit destination element from a 64-bit source. Narrowing shares the overlap constraints of the other narrowing instructions, so the existing rvv_cross_eew_overlap_illegal() applies unchanged. vfncvt.rod.f.f.w rounds towards odd; SoftFloat is already built with SOFTFLOAT_ROUND_ODD, so softfloat_round_odd can be selected directly. --- src/decode.h | 8 +++ src/decode_v.c | 44 ++++++++++++++- src/rv32_v_constopt.c | 8 +++ src/rv32_v_template.c | 122 ++++++++++++++++++++++++++++++++++++++++++ tests/rvv-smoke.S | 107 ++++++++++++++++++++++++++++++++++++ 5 files changed, 287 insertions(+), 2 deletions(-) diff --git a/src/decode.h b/src/decode.h index 08974fd79..bded67785 100644 --- a/src/decode.h +++ b/src/decode.h @@ -827,6 +827,14 @@ enum op_field { _(vfwmsac_vf, 0, 4, 0, ENC(rs1, rs2, vd)) \ _(vfwnmsac_vv, 0, 4, 0, ENC(rs1, rs2, vd)) \ _(vfwnmsac_vf, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_xu_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_x_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_f_xu_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_f_x_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_f_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_rod_f_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_rtz_xu_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ + _(vfncvt_rtz_x_f_w, 0, 4, 0, ENC(rs1, rs2, vd)) \ ) /* clang-format on */ diff --git a/src/decode_v.c b/src/decode_v.c index 84ff5c49d..d93c68ca9 100644 --- a/src/decode_v.c +++ b/src/decode_v.c @@ -980,8 +980,48 @@ static inline bool op_010010(rv_insn_t *ir, const uint32_t insn) ir->opcode = rv_insn_vsbc_vvm; break; case 1: - /* FIXME: Implement the decoding for VFUNARY0. */ - return false; + /* OPFVV / VFUNARY0 dispatch (V 1.0 §13.19): vs1 carries the + * sub-opcode. The single-width (vs1 0x00-0x07) and widening + * (vfwcvt, vs1 0x08-0x0f) forms are not implemented yet and fall + * through to the reject below rather than being misdecoded. + */ + switch (decode_rs1(insn)) { + case 0b10000: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_xu_f_w; + break; + case 0b10001: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_x_f_w; + break; + case 0b10010: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_f_xu_w; + break; + case 0b10011: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_f_x_w; + break; + case 0b10100: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_f_f_w; + break; + case 0b10101: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_rod_f_f_w; + break; + case 0b10110: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_rtz_xu_f_w; + break; + case 0b10111: + decode_vvtype(ir, insn); + ir->opcode = rv_insn_vfncvt_rtz_x_f_w; + break; + default: + return false; + } + break; case 2: /* OPMVV / VXUNARY0 (vzext.vf{2,4,8} and vsext.vf{2,4,8}). The * RVOPs for these are not implemented yet, so refuse the encoding diff --git a/src/rv32_v_constopt.c b/src/rv32_v_constopt.c index 17c56d471..545b1c1b4 100644 --- a/src/rv32_v_constopt.c +++ b/src/rv32_v_constopt.c @@ -538,6 +538,14 @@ CONSTOPT(vmsof_m, {}) CONSTOPT(vmsif_m, {}) CONSTOPT(viota_m, {}) CONSTOPT(vid_v, {}) +CONSTOPT(vfncvt_xu_f_w, {}) +CONSTOPT(vfncvt_x_f_w, {}) +CONSTOPT(vfncvt_f_xu_w, {}) +CONSTOPT(vfncvt_f_x_w, {}) +CONSTOPT(vfncvt_f_f_w, {}) +CONSTOPT(vfncvt_rod_f_f_w, {}) +CONSTOPT(vfncvt_rtz_xu_f_w, {}) +CONSTOPT(vfncvt_rtz_x_f_w, {}) CONSTOPT(vfadd_vv, {}) CONSTOPT(vfadd_vf, {}) CONSTOPT(vfredusum_vs, {}) diff --git a/src/rv32_v_template.c b/src/rv32_v_template.c index e30263fc4..2208dc72a 100644 --- a/src/rv32_v_template.c +++ b/src/rv32_v_template.c @@ -6695,6 +6695,49 @@ static inline uint32_t rvv_fp_fnmsub32(uint32_t dest, .v; } +/* Narrowing conversions for the vfncvt family (V 1.0 §13.19): a 64-bit + * source element produces a 32-bit destination element. The rounding + * mode in force is whatever the RVOP installed - the dynamic frm for the + * plain forms, round-towards-zero for .rtz, or round-towards-odd for + * vfncvt.rod.f.f.w. The trailing "exact" argument is false because these + * conversions report inexact results through fflags. + */ +static inline uint32_t rvv_fp_ncvt_xu_f64(uint64_t bits) +{ + return f64_to_ui32(rvv_fp64_from_raw(bits), softfloat_roundingMode, false); +} + +static inline uint32_t rvv_fp_ncvt_x_f64(uint64_t bits) +{ + return (uint32_t) f64_to_i32(rvv_fp64_from_raw(bits), + softfloat_roundingMode, false); +} + +static inline uint32_t rvv_fp_ncvt_rtz_xu_f64(uint64_t bits) +{ + return f64_to_ui32_r_minMag(rvv_fp64_from_raw(bits), false); +} + +static inline uint32_t rvv_fp_ncvt_rtz_x_f64(uint64_t bits) +{ + return (uint32_t) f64_to_i32_r_minMag(rvv_fp64_from_raw(bits), false); +} + +static inline uint32_t rvv_fp_ncvt_f_xu64(uint64_t bits) +{ + return ui64_to_f32(bits).v; +} + +static inline uint32_t rvv_fp_ncvt_f_x64(uint64_t bits) +{ + return i64_to_f32((int64_t) bits).v; +} + +static inline uint32_t rvv_fp_ncvt_f_f64(uint64_t bits) +{ + return f64_to_f32(rvv_fp64_from_raw(bits)).v; +} + static inline uint64_t rvv_fp_widen32(uint32_t bits) { return f32_to_f64(rvv_fp32_from_raw(bits)).v; @@ -6828,6 +6871,36 @@ static inline void rvv_exec_fp32_vf(riscv_t *rv, rv->csr_vstart = 0; } +/* Narrowing unary element loop for the vfncvt family (V 1.0 §13.19): a + * single 64-bit source element produces one 32-bit destination element. + */ +typedef uint32_t (*rvv_fp_ncvt_fn)(uint64_t src); + +static inline void rvv_exec_fp_ncvt(riscv_t *rv, + const rv_insn_t *ir, + uint32_t dest, + rvv_fp_ncvt_fn op) +{ + uint32_t vlmax = rvv_vlmax(rv->csr_vtype); + uint8_t vma = (rv->csr_vtype >> 7) & 0x1; + uint8_t vta = (rv->csr_vtype >> 6) & 0x1; + + for (uint32_t elem = rv->csr_vstart; elem < rv->csr_vl; elem++) { + if (!rvv_mask_enabled_for_elem(rv, ir, elem)) { + if (vma) + rvv_set_elem(rv, dest, elem, 32, 0xFFFFFFFFU); + continue; + } + rvv_set_elem(rv, dest, elem, 32, + op(rvv_get_elem_ext(rv, ir->vs2, elem, 64))); + } + if (vta) { + for (uint32_t elem = rv->csr_vl; elem < vlmax; elem++) + rvv_set_elem(rv, dest, elem, 32, 0xFFFFFFFFU); + } + rv->csr_vstart = 0; +} + static inline void rvv_exec_fp32_mask_vv(riscv_t *rv, const rv_insn_t *ir, uint32_t dest, @@ -7448,6 +7521,47 @@ static inline void rvv_exec_vfmv_v_f(riscv_t *rv, set_fflag(rv); \ }) +/* round_mode: RVV_NCVT_RM_DYN follows frm, RVV_NCVT_RM_ROD pins + * round-towards-odd for vfncvt.rod.f.f.w, and RVV_NCVT_RM_NONE leaves the + * mode alone for the .rtz forms, whose softfloat helpers carry their own. + */ +#define RVV_NCVT_RM_NONE 0 +#define RVV_NCVT_RM_DYN 1 +#define RVV_NCVT_RM_ROD 2 + +#define RVV_FP_NCVT_OP(name, opfn, round_mode) \ + RVOP(name, { \ + uint32_t wide_span; \ + uint32_t narrow_span = rvv_group_regs(rv->csr_vtype); \ + if (rvv_require_operable(rv)) \ + return false; \ + if ((rvv_sew_bits(rv->csr_vtype) != 32) || \ + !rvv_wide_group_span(rv, &wide_span) || \ + !rvv_validate_data_reg(rv->csr_vtype, ir->vd) || \ + !rvv_validate_wide_reg(rv, ir->vs2) || \ + rvv_cross_eew_overlap_illegal(ir->vd, narrow_span, ir->vs2, \ + wide_span)) \ + return rvv_trap_illegal_state(rv, 0); \ + if (round_mode == RVV_NCVT_RM_DYN) { \ + rvv_fp_begin_round(rv); \ + } else { \ + rvv_fp_begin_flags(); \ + if (round_mode == RVV_NCVT_RM_ROD) \ + softfloat_roundingMode = softfloat_round_odd; \ + } \ + rvv_exec_fp_ncvt(rv, ir, ir->vd, opfn); \ + set_fflag(rv); \ + }) + +RVV_FP_NCVT_OP(vfncvt_xu_f_w, rvv_fp_ncvt_xu_f64, RVV_NCVT_RM_DYN); +RVV_FP_NCVT_OP(vfncvt_x_f_w, rvv_fp_ncvt_x_f64, RVV_NCVT_RM_DYN); +RVV_FP_NCVT_OP(vfncvt_f_xu_w, rvv_fp_ncvt_f_xu64, RVV_NCVT_RM_DYN); +RVV_FP_NCVT_OP(vfncvt_f_x_w, rvv_fp_ncvt_f_x64, RVV_NCVT_RM_DYN); +RVV_FP_NCVT_OP(vfncvt_f_f_w, rvv_fp_ncvt_f_f64, RVV_NCVT_RM_DYN); +RVV_FP_NCVT_OP(vfncvt_rod_f_f_w, rvv_fp_ncvt_f_f64, RVV_NCVT_RM_ROD); +RVV_FP_NCVT_OP(vfncvt_rtz_xu_f_w, rvv_fp_ncvt_rtz_xu_f64, RVV_NCVT_RM_NONE); +RVV_FP_NCVT_OP(vfncvt_rtz_x_f_w, rvv_fp_ncvt_rtz_x_f64, RVV_NCVT_RM_NONE); + RVV_FP32_VV_OP(vfadd_vv, rvv_fp_add32, true); RVV_FP32_VF_OP(vfadd_vf, rvv_fp_add32, true); RVV_FP32_RED_OP(vfredusum_vs, rvv_fp_add32, true); @@ -7619,6 +7733,14 @@ RVV_FP64_MAC_VF_OP(vfwmsac_vf, rvv_fp_wmsac64); RVV_FP64_MAC_VV_OP(vfwnmsac_vv, rvv_fp_wnmsac64); RVV_FP64_MAC_VF_OP(vfwnmsac_vf, rvv_fp_wnmsac64); #else +RVOP(vfncvt_xu_f_w, { V_NOP; }) +RVOP(vfncvt_x_f_w, { V_NOP; }) +RVOP(vfncvt_f_xu_w, { V_NOP; }) +RVOP(vfncvt_f_x_w, { V_NOP; }) +RVOP(vfncvt_f_f_w, { V_NOP; }) +RVOP(vfncvt_rod_f_f_w, { V_NOP; }) +RVOP(vfncvt_rtz_xu_f_w, { V_NOP; }) +RVOP(vfncvt_rtz_x_f_w, { V_NOP; }) RVOP(vfadd_vv, { V_NOP; }) RVOP(vfadd_vf, { V_NOP; }) RVOP(vfredusum_vs, { V_NOP; }) diff --git a/tests/rvv-smoke.S b/tests/rvv-smoke.S index 70ce8c892..6c59a831e 100644 --- a/tests/rvv-smoke.S +++ b/tests/rvv-smoke.S @@ -98,6 +98,16 @@ mulfix_lhs: mulfix_rhs: .byte 0x40, 0x80, 0x40, 0x20 +ncvt_f_src: + .word 0x00000000, 0x40040000 /* 2.5 */ + .word 0x00000000, 0xbff80000 /* -1.5 */ + .word 0x10000000, 0x3ff00000 /* 1.0+2^-24, ties between floats */ + .word 0x00000000, 0x401c0000 /* 7.0 */ + +ncvt_i_src: + .word 3, 0, 0xfffffffd, 0xffffffff + .word 5, 0, 0, 0 + narrow16_a: .short 0x8000, 0x0002 @@ -296,6 +306,8 @@ fixed_out: .space 16 wide_out: .space 32 +ncvt_out: + .space 32 red_out: .space 16 wide_red_out: @@ -1813,6 +1825,83 @@ _start: csrr t1, vl assert_reg_imm t1, 4, fail_fractional + # Narrowing FP conversions (V 1.0 13.19): 64-bit source elements produce + # 32-bit destination elements, so the source group is 2*LMUL. + li t2, 0 + csrw frm, t2 + li a0, 4 + vsetvli t0, a0, e32, m1, tu, mu + la a1, ncvt_f_src + vle64.v v22, (a1) + + # vfncvt.f.f.w: 2.5 -> 0x40200000, -1.5 -> 0xbfc00000 + vfncvt.f.f.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 0(a1) + li t6, 0x40200000 + bne t1, t6, fail_vfncvt_f_f + lw t1, 4(a1) + li t6, 0xbfc00000 + bne t1, t6, fail_vfncvt_f_f + # element 2 is exactly halfway between two floats: RNE rounds to even + lw t1, 8(a1) + li t6, 0x3f800000 + bne t1, t6, fail_vfncvt_f_f + + # vfncvt.rod.f.f.w rounds that same element towards odd instead + vfncvt.rod.f.f.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 8(a1) + li t6, 0x3f800001 + bne t1, t6, fail_vfncvt_rod + lw t1, 0(a1) + li t6, 0x40200000 + bne t1, t6, fail_vfncvt_rod + + # vfncvt.x.f.w: RNE gives -1.5 -> -2 + vfncvt.x.f.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 0(a1) + assert_reg_imm t1, 2, fail_vfncvt_x_f + lw t1, 4(a1) + assert_reg_imm t1, -2, fail_vfncvt_x_f + lw t1, 12(a1) + assert_reg_imm t1, 7, fail_vfncvt_x_f + + # vfncvt.rtz.x.f.w truncates instead: -1.5 -> -1 + vfncvt.rtz.x.f.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 4(a1) + assert_reg_imm t1, -1, fail_vfncvt_rtz_x_f + lw t1, 12(a1) + assert_reg_imm t1, 7, fail_vfncvt_rtz_x_f + + # Signed vs unsigned 64-bit integer source: -3 as i64 becomes -3.0f, + # but the same bits as u64 are a huge positive value. + la a1, ncvt_i_src + vle64.v v22, (a1) + + vfncvt.f.x.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 0(a1) + li t6, 0x40400000 + bne t1, t6, fail_vfncvt_f_x + lw t1, 4(a1) + li t6, 0xc0400000 + bne t1, t6, fail_vfncvt_f_x + + vfncvt.f.xu.w v20, v22 + la a1, ncvt_out + vse32.v v20, (a1) + lw t1, 4(a1) + li t6, 0x5f800000 + bne t1, t6, fail_vfncvt_f_xu + la a1, ok_msg li a2, ok_msg_len j write_and_exit_ok @@ -1911,6 +2000,24 @@ fail_vsmul4: fail_vwadd: li a3, 0xd5 j write_and_exit_fail +fail_vfncvt_f_f: + li a3, 0x91 + j write_and_exit_fail +fail_vfncvt_rod: + li a3, 0x92 + j write_and_exit_fail +fail_vfncvt_x_f: + li a3, 0x93 + j write_and_exit_fail +fail_vfncvt_rtz_x_f: + li a3, 0x94 + j write_and_exit_fail +fail_vfncvt_f_x: + li a3, 0x95 + j write_and_exit_fail +fail_vfncvt_f_xu: + li a3, 0x96 + j write_and_exit_fail fail_vwmulu: li a3, 0xd6 j write_and_exit_fail