Use FPU instruction VABS.F32 from C/C++

Asked by Raphael Ulrich

I want to get the absolute value of a float 32 value in a C/C++ code on a Cortex M4 MCU by making use of the FPU instruction VABS.F32.
IAR compiler offers macro __fabsf for that purpose.

Is there a equivalent available in gcc-arm-none-eabi-4_7-2012q4 ?
Or in general: How can I make sure the FPU instruction VABS.F32 is used?

Question information

Language:
English Edit question
Status:
Solved
For:
GNU Arm Embedded Toolchain Edit question
Assignee:
No assignee Edit question
Solved by:
Raphael Ulrich
Solved:
Last query:
Last reply:
Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#1

Well, in case the compiler doesn't use a given instruction but several instructions instead it might be that the series of instructions is faster to execute. However compiler may have bugs of course.

There is a builtin for this in GCC which I assume would be optimize to an instruction but the documentation does not give any guarantee about it. In fact the documentation explicitely states that builtin may be implemented as function call. I guess if you really want an instruction you could use inline assembly.

Something like: __asm__ ("vabs.f32 %0, %1" : "=r" (output) : "r" (input)) with input the input parameter and output the place where the result is written to.

Best regards.

Revision history for this message
Raphael Ulrich (raphael-ulrich) said :
#2

Thanks for your reply.
The usage inline assembly is of course an option which solves my problem. However I would be more interested in a C/C++ based solution.

What do you mean with builtin?
Could you provide me a link to the mentioned documentation? Unfortunately I was unable to locate a documentation for gcc-arm-none-eabi-4_7-2012q4 .

BR

Revision history for this message
Thomas Preud'homme (thomas-preudhomme) said :
#3

Hi Raphael,

Actually no need for builtin as there is an intrinsic for this: vabs_f32. It is described in section 6.56.3.38 of the gcc.pdf documentation. Such a documentation can be found in subdirectory share/doc/gcc-arm-none-eabi/pdf/gcc/gcc.pdf in the installation directory. On Windows the full path would be C:\Program Files (x86)\GNU Tools ARM Embedded\4.8 2014q2\share\doc\gcc-arm-none-eabi\pdf\gcc for the 4.8 2014Q2 release.

However I am not sure when this intrinsic was introduced and it might not be available in the 4.7 2012 Q2 release.

Best regards.

Revision history for this message
Terry Guo (terry.guo) said :
#4

Besides to what Thomas said, you can call gcc builtin function fabsf like below:

#include<math.h>

float
foo (float x)
{
  return fabsf(x);
}

With proper options like "arm-none-eabi-gcc -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -O2 y.c -S", the vabs.f32 instruction should be generated.

Revision history for this message
Terry Guo (terry.guo) said :
#5

When I post my comment#4, I didn't know Thomas was posting comment#3. So for "Besides to what Thomas said", I mean Thomas comment#1.

I thinks the vabs_f32 from comment#3 is just for neon, not for cortex-m4 fpu.

Revision history for this message
Raphael Ulrich (raphael-ulrich) said :
#6

Thank you guys for your useful posts.

Terrys comment #4 solved my problem.
Including math.h and simply use fabs would effectively transfer that into vabs.f32 instruction in my disassembly.

I didn't tried that before as I assumed this would include inherently (memory+time demanding) software implementation of fabsf.

Regards
Raphael