This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52 FPU single precision floating point - GCC compiler flags

I just came upon a blog post from ARM about the use of the Cortex-M4's FPU: 

https://community.arm.com/developer/ip-products/processors/b/processors-ip-blog/posts/10-useful-tips-to-using-the-floating-point-unit-on-the-arm-cortex--m4-processor?pi353792392=2

In the comments 2 compiler flags for the GCC compiler that seem to be important are discussed:

-fsingle-precision-constant

 Treat floating-point constants as single precision instead of implicitly converting them to double-precision constants. 

-Wdouble-promotion

Give a warning when a value of type float is implicitly promoted to double. CPUs with a 32-bit “single-precision” floating-point unit implement float in hardware, but emulate double in software. On such a machine, doing computations using double values is much more expensive because of the overhead required for software emulation.

It is easy to accidentally do computations with double because floating-point literals are implicitly of type double. For example, in:

float area(float radius)
{
 return 3.14159 * radius * radius;
}

the compiler will perform the entire computation with double because the floating-point literal is a double. 

I've looked into the makefiles from the SDK's examples but haven't found them. Has Nordic looked into those? I don't have the time right now to evaluate the impact but it looks like something that should be added by everyone that uses the FPU of the M4 (and cares about efficiency).

If indeed useful, I suggest that these flags should be added to all the examples that also have the

-mfloat-abi=hard
setting

Parents Reply Children
Related