Why does armclang not use VCVT instruction for efficient integer to float conversion?

279 Views Asked by At

I need to convert an integer value into a float value on a Cortex-M4 with FPU; for example:

float convert(int n) {
    return (float) n;
}

armclang compiler translates this to:

        push    {r11, lr}
        mov     r11, sp
        sub     sp, sp, #8
        str     r0, [sp, #4]
        ldr     r0, [sp, #4]
        bl      __aeabi_i2f
        mov     sp, r11
        pop     {r11, lr}
        bx      lr

(Godbolt Link: https://godbolt.org/z/K59xGq78W)

The conversion from int to float is made by calling the library routine __aeabi_i2f which is much less efficient than using the FPU instruction VCVT.

For example, the GCC makes use of VCVT:

        push    {r7}
        sub     sp, sp, #12
        add     r7, sp, #0
        str     r0, [r7, #4]
        ldr     r3, [r7, #4]
        vmov    s15, r3 @ int
        vcvt.f32.s32    s15, s15
        vmov.f32        s0, s15
        adds    r7, r7, #12
        mov     sp, r7
        ldr     r7, [sp], #4
        bx      lr

(https://godbolt.org/z/Pdv3nEMYq)

Is there a way to tell armclang to use the VCVT instruction?

1

There are 1 best solutions below

1
On BEST ANSWER

Use the option -march=armv7+fp to tell the compiler to generate code for a machine with an FPU.

Godbolt