Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
44 changes: 42 additions & 2 deletions src/decode_v.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/rv32_v_constopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, {})
Expand Down
122 changes: 122 additions & 0 deletions src/rv32_v_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Floating-to-integer vfncvt forms do not set fflags.NX for inexact results because all four SoftFloat conversion calls pass exact=false. Pass true to the exact argument, as the existing scalar FCVT handlers do, so set_fflag() can report discarded fractional bits.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At src/rv32_v_template.c, line 6707:

<comment>Floating-to-integer `vfncvt` forms do not set `fflags.NX` for inexact results because all four SoftFloat conversion calls pass `exact=false`. Pass `true` to the exact argument, as the existing scalar FCVT handlers do, so `set_fflag()` can report discarded fractional bits.</comment>

<file context>
@@ -6695,6 +6695,49 @@ static inline uint32_t rvv_fp_fnmsub32(uint32_t dest,
+ */
+static inline uint32_t rvv_fp_ncvt_xu_f64(uint64_t bits)
+{
+    return f64_to_ui32(rvv_fp64_from_raw(bits), softfloat_roundingMode, false);
+}
+
</file context>

}

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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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; })
Expand Down
107 changes: 107 additions & 0 deletions tests/rvv-smoke.S
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -296,6 +306,8 @@ fixed_out:
.space 16
wide_out:
.space 32
ncvt_out:
.space 32
red_out:
.space 16
wide_red_out:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading