If I write a line of code for a math operation, such as:
x = 109.0f*768.0f/320.0f;
Is the result (261.6f) computed at compile time or run time? In other words, does Xcode’s optimization recognize that the result of a hard-coded math operation will always be the same, and thus can be pre-computed while compiling?
It is computed at compile time, at least using Xcode targetting iOS. This function:
compiles to these three instructions:
Computing the value at compile-time isn’t required by the C standard. In fact, if you set the
FENV_ACCESSpragma, there are cases where it’s forbidden from computing it at compile-time. Turning onFENV_ACCESSdidn’t affect the generated instructions in this test case.