My embedded C code is running on Cortex M4F
which has single precision FPU
. I am concerned about how often the compiler puts software-based double-precision math in places like
**
float_var1 = 3.0 * int_var / float_var_2;
(3.0 instead of 3.0f)
**
I am afraid that I would miss some of these double constants. How can I locate all occurrences of slower double-precision math? Disabling double-precision or generating an error/warning with either sourcery GCC or IAR would do it.
Please guide me the correct way to achieve my objective.
How can I locate all occurrences of slower double-precision math? Disabling double-precision or generating an error/warning with either sourcery GCC or IAR would do it.
-Wdouble-promotion
does exactly what you want, see the doc, under Warning Options. The example in the doc is quite similar to yours by the way.
Here is basically your example:
float f(int int_var, float float_var_2) {
return 3.0 * int_var / float_var_2;
}
And here is what happens when I pass the -Wdouble-promotion
flag to gcc:
gcc -c -Wdouble-promotion float.c
float.c: In function ‘f’:
float.c:2:24: warning: implicit conversion from ‘float’ to ‘double’ to match other operand of binary expression [-Wdouble-promotion]
If you pass the -Werror
flag as well, you can turn all warnings into errors. If that is too strict, you can selectively turn warnings into errors by passing -Werror=foo
, see the docs under Warning Options
gcc has an optimization option -fsingle-precision-constant
that treats ordinary floating point constants as single precision:
-fsingle-precision-constant
Treat floating-point constants as single precision instead of implicitly converting them to double-precision constants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With