Will GCC leave arithmetic with fixed values ​​for runtime or compile the output?

I am wondering if GCC will leave arithmetic with fixed values ​​that will be executed at runtime, or if it will set an answer for it, for example.

const float halfPi = M_PI/2;

Will it "collapse" the equation and establish

const float halfPi = 1.57079;

Or leave arithmetic for runtime?

+5
source share
2 answers

Well ... if we were talking about integrals , the answer would be a clear yes (under the general term Constant Folding). Even long calculations can be performed at compile time ... and this is actually required to evaluate the parameters of non-config parameters of templates and (now) constexpr.

, . , (, , ) .

, , float , , 5 :

   5.0000 + 0.00001
-> 5.00001
-> 5.0000 (truncation to 5 digits)

   5.0000 + 0.00001 + ... + 0.00001 (10 times)
-> 5.0000 + 0.00001 + ... + 0.00001 (9 times)
-> 5.0000 + 0.00001 + ... + 0.00001 (8 times)
-> ...
-> 5.0000

... ? , , ( ) .

, , , . , gcc, -freciprocal-math ( ). , , , , ().

, - ; , . , .

+8

, Agner Fog c++ ! 8.2 , , .

, , , ( -S). - asm("MY_LABEL:"); , , , , , , . , - .

+5

All Articles