4 ms·
Plenty of modern compilers will optimize e.g. a/c + b/c to (a+b)/c when dealing with floating point. There is nothing in the C standard which makes that invali
by aidenn0 12y ago
Plenty of modern compilers will optimize e.g. a/c + b/c to (a+b)/c when dealing with floating point. There is nothing in the C standard which makes that invalid.
- Joky 12y agoThis seems IEEE754 invalid to me. Any reference to a compiler which would do this transformation (without fast-math of course)?
- aidenn0 12y ago60559 specifically allows value-changing optimizations, so long as optimizations are not disabled. I have seen more than one compiler that allows at least some value-changing optimizations with their equivalent to gcc -O2. Also, as far as I can tell, C99 (even with Annex F) does not follow parts of IEC 60559 with regards to: > A language standard should also define, and require implementations to provide, attributes that allow and disallow value-changing optimizations, separately or collectively, for a block. [edit] And note that one example "value-changing optimization" is applying associative and distributed rules. Fused multiply-add is another that is really common.
- kps 12y ago> There is nothing in the C standard which makes that invalid. C only permits¹ optimizations that do not change semantics, so changing the result of a floating point expression is invalid. Example 5 on p16 of C11² makes this explicit: “Rearrangement for floating-point expressions is often restricted because of limitations in precision as well as range. The implementation cannot generally apply the mathematical associative rules for addition or multiplication, nor the distributive rule, because of roundoff error, even in the absence of overflow and underflow. Likewise, implementations cannot generally replace decimal constants in order to rearrange expressions.” ¹ Of course many compilers have options that invoke something other than standard-conforming C. ² C11 final draft: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf
- aidenn0 12y agoThanks for that; I've purchased the C11 standard, but haven't gotten around to reading it yet since I'm not likely to write anything in C11 for a few years yet.