Webcentive RISC-V Reference

fcvt.s.h

Convert half-precision float to a single-precision float

Extension
Zfhmin
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
fcvt.s.h fd, fs1, rm
Length
32-bit

Encoding

312726252420191514121176210
010000000010rs1rmrd1010011

Match 0x40200053, mask 0xfff0007f. Source: fcvt.s.h rd rs1 24..20=2 31..27=0x08 rm 26..25=0 6..2=0x14 1..0=3 in riscv-opcodes/extensions/rv_zfhmin.

FieldBitsriscv-opcodes name
rs119:15rs1
rm14:12rm
rd11:7rd

Description

Converts a half-precision number in floating-point register fs1 into a single-precision floating-point number in floating-point register fd.

fcvt.s.h will never round, and so the 'rm' field is effectively ignored.

Description from riscv-unified-db, reformatted.

Operation

check_f_ok($encoding);

Bits<16> hp_value = F[fs1][15:0];

Bits<1> sign = hp_value[15];
Bits<5> exp = hp_value[14:10];
Bits<10> frac = hp_value[9:0];

Bits<32> sp_value = 0;

# Widening half -> single is always exact, so no rounding is needed.
if (exp == 0x1F) {
  if (frac != 0) {
    # NaN. A signaling NaN has the quiet bit (frac[9]) clear and raises NV.
    # Either way the result is the canonical single-precision NaN.
    if ((hp_value & 0x0200) == 0) {
      set_fp_flag(FpFlag::NV);
    }
    sp_value = SP_CANONICAL_NAN;
  } else {
    sp_value = packToF32UI(sign, 0xFF, 0);
  }
} else if (exp == 0) {
  if (frac == 0) {
    sp_value = packToF32UI(sign, 0, 0);
  } else {
    # Subnormal half: value = frac * 2^-24. Shift the leading 1 of frac up to
    # bit 10 (the implicit-bit position), drop it, and rebias the exponent.
    Bits<16> frac16 = frac;
    Bits<8> shift_dist = count_leading_zeros(16, frac16) - 5;
    Bits<32> norm_frac = ((frac16 << shift_dist) & 0x3FF);
    sp_value = packToF32UI(sign, 0x71 - shift_dist, norm_frac << 13);
  }
} else {
  # Normal half: rebias the exponent (127 - 15 = 0x70) and widen the fraction
  # from 10 to 23 bits.
  Bits<8> sp_exp = exp;
  Bits<32> frac32 = frac;
  sp_value = packToF32UI(sign, sp_exp + 0x70, frac32 << 13);
}

if (implemented?(ExtensionName::D)) {
  F[fd] = nan_box(32, 64, sp_value);
} else {
  F[fd] = sp_value;
}

mark_f_state_dirty();

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages