Phil said:
Well, I suppose FLT_ROUNDS could be coded to always be -1, and
fegetround and fesetround could always fail. In any case, by the above I
did not mean what the last call to fesetround was, I meant how the
processor works (which includes whether it is possible to change the
rounding mode, how many bits in a double etc).
What rounding mode is my FPU in currently on this linux/x86 box?
And on my OSX/POWER box? And on my BSD/C7 box? On my linux/Arm box?
My linux/Alpha box?
Irrelevant. Or at least, not as relevant as you think.
The opcodes to perform a dynamically-rounded division do not
change, yet the results do. So a cross-compiler can generate
them, yet not know what they will yield.
Unless FLT_ROUNDS returns -1 and fesetround and fegetround always fail
the author of the implementation needs to know how to set (if it can be
changed) and determine the rounding mode. However, in this case as it is
indeterminable it can do the rounding as it likes.
There is no difference in this for a cross compiler compared to a normal
compiler.
You cannot perform the 1.0/3.0 constant-folding optimisation
if you wish to honour the not-known-until-runtime settings of
the processor.
You (the writer of the implementation) can set the initial mode (if it
is setable, and if not you know what it is) during program
initialisation. Then if during compilation/linking you determine that
fesetround is never called you know exactly what rounding mode is in effect.
It's commonly an optimisation setting to permit
or forbid the compiler from performing such optimisations.
GCC's is -frounding-math. However, the behaviour of individual
compilers is best discussed in newsgroups specific to that
compiler.
I'm discussing what is allowed and possible, not the details of a
specific implementation.
The C standard gives implementations rather a lot of
flexibility in what they may do regarding rounding.
Yes, which also gives a lot of opportunities for doing optimisations.
Specifically it can also default to "#pragma FENV_ACCESS off" and then
unless it sees it being set to on it can assume that the default mode
(as defined by the implementation and as can be set by the
implementation during program initialisation) is in effect thus allowing
it to perform the optimisation.
So no, the compiler is not always allowed to do this optimisation.
However, the standard *does* allow it providing certain criteria are
met, and whether they are met can be determined by the author of the
implementation knowing how the target works together with some analysis
of the source code presented.