Use fclass.{s|d|q}
instruction for float point classification in RISC-V targets
#73015
Labels
A-floating-point
Area: Floating point numbers and arithmetic
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
I-slow
Issue: Problems and improvements with respect to performance of generated code.
O-riscv
Target: RISC-V architecture
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Recently I came up with floating point variable classification. I found the function
f32::classify
useful. Regardless of instruction set architecture, this function is implemented currently like this in standard library:However, this standard library's function compiles to very long bunches of instructions. On RISC-V RV64GC, it compiles into:
very long assembly code
To solve this problem, RISC-V provided with
fclass.{s|d|q}
instructions. According to RISC-V's spec Section 11.9, instructionfclass.s rd, rs1
examinesrs1
as 32-bit floating point number, and store its type intord
. By this way we use registerrd
and to judge which enum value of Rust standard library we should return.I'd like to explain this procedure in Rust code. The new way looks like this:
It compiles into the following assembly code which is shorter and could be executed faster:
For
f64
types, we could usefclass.d
instruction other thanfclass.s
forf32
s. If in the future we had a chance to introducef128
primitive type, there's alsofclass.q
instruction. After using these instructions, it improves speed on this function in RISC-V platforms. This enhancement is especially significant for embedded devices. I suggest to change the implementation of this function in the standard library. We may implement it by any of following ways:fclassf32
andfclassf64
intrinsics function intocore::instrinsics
, and call them inf32::classify
orf64::classify
. These functions can be implemented with special instruction or fallback in other platforms;#[cfg(..)]
to compile it only in RISC-V targets with floating extensionF
orD
respectively, or fallback in other platforms.The text was updated successfully, but these errors were encountered: