Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable double-precision math while compiling with GCC or/and IAR?

Tags:

c++

c

gcc

embedded

arm

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.

like image 264
AndreyB Avatar asked Jun 22 '14 04:06

AndreyB


2 Answers

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

like image 172
Ali Avatar answered Sep 22 '22 07:09

Ali


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.

like image 31
Ber Avatar answered Sep 19 '22 07:09

Ber