AR# 61480

|

SDK, GCC - How to use VCVTR instruction on GCC toolchain

描述

When the ELF is viewed with the FPU enabled, GCC will use the VCVT instruction when a floating-point variable is assigned (and hence converted) to an integer.


 

As per the Arm Architecture Reference Manual:

"The floating-point to integer operation normally uses the Round towards Zero rounding mode, but can optionally use the rounding mode specified by the FPSCR. "

The FPSCR Register bit function FPSCR[23:22] defines different rounding modes.

解决方案

There is no currently known way to direct GCC ARM FPU conversions, so assembly must be used in this case.

Round to Zero is used because the C specification states that truncation (round to zero) is sufficient for the conversion, as per section 6.3.1.4 of the C programming language standard.

"When a value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero)."

As an example, the following code makes use of inline assembly to use VCVTR instruction:


int main (void) {
    int integer = 0;
    float floating = 1.8;

    integer = (int)floating;
    printf("Float: %.6f, Integer: %d\n", floating, integer);

    __asm__ __volatile__("flds    s15, %0" : "=m" (floating));
    __asm__ __volatile__("VCVTR.S32.F32 s15, s15;");
    __asm__ __volatile__("fsts    s15, %0" : "=m" (integer));

    printf("Float: %f, Integer: %d\n", floating, integer);

    return 0;
};
AR# 61480
日期 06/08/2018
状态 Active
Type 综合文章
Tools
People Also Viewed