Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!zaphod.mps.ohio-state.edu!think.com!snorkelwacker.mit.edu!ai-lab!life!burley From: burley@pogo.gnu.ai.mit.edu (Craig Burley) Newsgroups: comp.lang.fortran Subject: Re: Questions about IF Message-ID: Date: 25 Jun 91 14:08:29 GMT References: <25663@well.sf.ca.us> Sender: news@ai.mit.edu Organization: Free Software Foundation 545 Tech Square Cambridge, MA 02139 Lines: 38 In-reply-to: rchrd@well.sf.ca.us's message of 25 Jun 91 03:04:31 GMT In article <25663@well.sf.ca.us> rchrd@well.sf.ca.us (Richard Friedman) writes: Another compiler-dependent problem with IF statements has to do with optimizations. Supposing the IF is within a loop and the compiler has smartly removed loop independent computations outside the loop, but some of the items in the IF expression should not be computed if earlier expressions are false because they will cause out of bounds array references: IF(SW1 .and. BAR(I+N)-BAR(I) .eq. 0)... Suppose I and N are not defined if SW1 is false (say its some sort of processing option). If the IF were in a loop, the compiler might want to precompute the BAR()-BAR(), or atleast maybe just prefetch them. This could generate referencing errors if SW1 is false. And, it could be order dependent (left-right r/t right-left), which raises portability issues. Gotta be careful outthere. Yes, but it should be made clear the problem is really with the program, not the compiler. The IF statement should really be two IFs: IF (SW1) THEN IF (BAR(I+N)-BAR(I) .eq. 0)... END IF (or equivalent). This is one of the many reasons I like coding in C; the kind of tasks I write programs for, I often need to represent these kinds of conditionals, and the C construct, "if (sw1 && ...)", does exactly what I want. On the other hand, C provides no proper equivalent for Fortran's .AND. and .OR., which is unfortunate since it would be handy at times. (It provides bitwise AND and OR, but the compiler must evaluate both operands, it does not have the option of short-circuiting; so either you must code using left-to- right short-circuiting, where you have to decide which operand takes longer to evaluate for yourself, or you don't get short-circuiting at all.) -- James Craig Burley, Software Craftsperson burley@gnu.ai.mit.edu