Message Font: Serif | Sans-Serif
No. of Recommendations: 1
rev2217/norm: The standards of most programming languages, including Fortran and C/C++, explicitly forbid optimizations that fail to preserve the integrity of parentheses in order to give programmers control of the sequence of operations when it matters. Thus, such an optimization is illegal.

This is (or was) part of the FORTRAN (IV) standard; I can't say about newer FORTRANs, though I doubt it has changed. It is *not* part of the C standard, and never has been. C guarantees ordering of statements separated by &&, ||, ^^, and a few others, but guarantees nothing about optimization. From Harbison & Steele, Fifth Edition, Section 7.3.3, "Parenthesized Expressions": "Parentheses do not necessarily force a particular evaluation order.... The purpose of the parenthesized expression is simply to delimit the enclosed expression for grouping purposes, either to defeat the default precedence of operators or make the code more readable."

That said, I doubt that many C optimizers would apply this identity to optimize this expression.

Print the post  


When Life Gives You Lemons
We all have had hardships and made poor decisions. The important thing is how we respond and grow. Read the story of a Fool who started from nothing, and looks to gain everything.
Contact Us
Contact Customer Service and other Fool departments here.
Work for Fools?
Winner of the Washingtonian great places to work, and Glassdoor #1 Company to Work For 2015! Have access to all of TMF's online and email products for FREE, and be paid for your contributions to TMF! Click the link and start your Fool career.