From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 1164f8,45b50f14dadfce5b X-Google-Attributes: gid1164f8,public X-Google-Thread: 110292,45b50f14dadfce5b X-Google-Attributes: gid110292,public X-Google-Thread: 102b75,45b50f14dadfce5b X-Google-Attributes: gid102b75,public From: ok@goanna.cs.rmit.edu.au (Richard A. O'Keefe) Subject: Re: Final kick at the "Merced Guessing Game" Date: 1997/10/14 Message-ID: <61uoiu$19e$1@goanna.cs.rmit.edu.au>#1/1 X-Deja-AN: 280266478 References: <61g30t$o2n@news-rocq.inria.fr> <343C34A7.B715A348@tonys.ecology.umn.edu> <3440F805.C98BB347@tonys.ecology.umn.edu> <61tfaa$9fr@sis.cambridge.arm.com> Organization: Comp Sci, RMIT University, Melbourne, Australia. NNTP-Posting-User: ok Newsgroups: comp.sys.intel,comp.arch,comp.sys.dec Paul Rubin writes: >> BOOL = F() .AND. (G() .OR. H()) >Is this C statement equivalent to the Fortran? >My Fortran is very rusty.... :-) > bool = !(!f() | (!g() & !h())); In a word, NO. The C version absolutely requires all of the function calls to be performed. A C compiler is not allowed to discard a function call simply because it knows the result. A simpler example is #include INTEGER I, F extern int f(void); EXTERNAL F int main(void) { int i; I = 0*F() i = 0*f(); PRINT *, I printf("%d\n", i); END return 0; } A Fortran compiler is allowed to say "hey, I know the result of 0*F() without calling F, so I won't call F". A C compiler is not. Sun's f77 compiler actually does this. I put that example in a file called 'foo.f' and did f77 -O5 foo.f; a.out The program compiled, linked, ran, and printed 0. The C version, however, doesn't link, because it must call f. Whatever code a C compiler generates for the > bool = !(!f() | (!g() & !h())); fragment, it must arrange for f, g, and h to all be called exactly once, whatever results are returned. To be perfectly frank, I am not convinced that _this_ particular freedom in Fortran is a good idea. Pascal has the same freedom. The machine I used as an undergraduate, a B6700, had Fortran and Pascal compilers that said 'avoid conditional branches' so that BOOL = 1.LE.I .AND. I.LE.N .AND. A(I).LT.0. !Fortran Bool := (1<=I) and (I<=N) and (A[I]<0.0); {Pascal} evaluated _all_ the subexpressions. This wasn't a problem for me writing new code for these machines, but it was a truly horrible problem when I tried to port code written by sloppy programmers for other machines with compilers that used short-circuit AND and OR. I typically had to examine every AND and OR to see whether they needed to be rewritten to conform to the strictures of the standard. The really sad thing here is that for the 1.LT.I .AND. I.LE.N bit, compare =1, rI, R1 compare rI, rN, R2 and R1, R2, R1 branch_if_false R1, L is easier for modern machines to deal with, having fewer conditional branches, than compare =1, rI, R1 branch_if_false R1, L compare rI, rN, R1 branch_if_false R1, L so 'strict' evaluation of AND and OR can be useful, and it generalises very nicely to arrays, which short-circuit AND and OR do not. Burroughs Algol in this case would have had me write BOOL := IF 1 <= I AND I <= N THEN A[I] < 0.0 ELSE FALSE; where the AND is strict, and the short-circuit AND is expressed with an IF. There are three issues here. (1) Strict and short-circuit AND/OR can have different consequences when some of the operands may be undefined, raise exceptions, or have side effects, *whatever* the architecture. (2) For the 'portable' Pascal and Fortran formulas, which _do_ yield the same outcomes either way, there are *efficiency* differences, and it is _this_ that the Fortran and Pascal standards were trying to exploit, by giving the compiler freedom to choose what is best for this model of this machine today. (3) Leaving the interpretation open in the other cases created nasty porting problems, as leaving interpretations open generally does. I don't pretend to have any generally optimal solution to this. Ada, with its 'and' -vs- 'and then' and 'or' -vs- 'or else', seems to be a very workable compromise here. -- John �neas Byron O'Keefe; 1921/02/04-1997/09/27; TLG,TLTA,BBTNOTL. Richard A. O'Keefe; RMIT Comp.Sci; http://www.cs.rmit.edu.au/%7Eok