fcvt.h.s
Convert single-precision float to a half-precision float
- Extension
- Zfhmin
- XLEN
- RV32 and RV64
- Assembly (riscv-unified-db)
fcvt.h.s fd, fs1, rm- Length
- 32-bit
Encoding
| 3127 | 2625 | 2420 | 1915 | 1412 | 117 | 62 | 10 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 01000 | 10 | 00000 | rs1 | rm | rd | 10100 | 11 | ||||||||||||||||||||||||
Match 0x44000053, mask 0xfff0007f. Source: fcvt.h.s rd rs1 24..20=0 31..27=0x08 rm 26..25=2 6..2=0x14 1..0=3 in riscv-opcodes/extensions/rv_zfhmin.
| Field | Bits | riscv-opcodes name |
|---|---|---|
| rs1 | 19:15 | rs1 |
| rm | 14:12 | rm |
| rd | 11:7 | rd |
Description
Converts a single-precision number in floating-point register fs1 into a half-precision floating-point number in floating-point register fd.
fcvt.h.s rounds according to the rm field.
All floating-point conversion instructions set the Inexact exception flag if the rounded result differs from the operand value and the Invalid exception flag is not set.
Description from riscv-unified-db, reformatted.
Operation
check_f_ok($encoding);
# STATUS: INCOMPLETE. Narrowing single -> half loses precision, so it needs
# IEEE 754 rounding (per _rm_), overflow/underflow and inexact handling.
# Only the NaN, infinity and zero cases below are handled. Every other
# input hits the assert(false) TODO. softfloat_roundPackToF16 does not exist
# in fp.idl yet.
Bits<32> sp_value = F[fs1][31:0];
Bits<1> sign = sp_value[31];
Bits<8> exp = sp_value[30:23];
Bits<23> frac = sp_value[22:0];
if (exp == 0xFF) {
if (frac != 0) {
# NaN. A signaling NaN has the quiet bit (frac[22]) clear and raises NV.
if ((sp_value & 0x00400000) == 0) {
set_fp_flag(FpFlag::NV);
}
F[fd] = nan_box(16, FLEN, HP_CANONICAL_NAN);
} else {
F[fd] = nan_box(16, FLEN, packToF16UI( sign, 0x1F, 0 ));
}
} else {
# frac is a 24-bit significand, the bottom 9 bits LSB are extracted and OR-red
# into a sticky flag, the top 15 MSBs are extracted, the LSB of this top slice
# is OR-red with the sticky
Bits<16> frac16 = (frac >> 9) | ((frac & 0x1ff) != 0 ? 1 : 0);
if ((exp | frac16) == 0) {
F[fd] = nan_box(16, FLEN, packToF16UI( sign, 0, 0 ));
} else {
# TODO: finite nonzero inputs need softfloat_roundPackToF16, which is not implemented.
# The frac16/sticky computation above is unverified draft code.
assert(false, "TODO: implement roundPackToF16");
# F[fd] = softfloat_roundPackToF16(sign, exp - 0x71, frac16 | 0x4000);
}
}
mark_f_state_dirty();Pseudocode in riscv-unified-db’s IDL, quoted unchanged.