short circuit evaluation

Henry Spencer henry at utzoo.UUCP
Sat Jan 31 08:08:26 AEST 1987


>     x = f1(something) * f2(something) * f3(something) ...
> 
> Shouldn't I expect all the multiplications to be performed, even if one
> function returns zero?
> I don't think I've ever used a compiler that does such optimizations...
> ...  Has anyone else?

Yeah, some variants of the V7 pdp11 C compiler did, in cases where one of
the operands was a compile-time constant known to be 0.  (This can arise
legitimately when doing arithmetic on configuration-dependent #defined
constants, for example.)  Never bothered me, since I view side effects
inside expressions as being unjustifiable pornography except for some very
specific cases.  I did ask Dennis about it at one point, since strict
reading of K&R suggested it was a bug.  His reply, as I recall it, was
roughly "I think it is defensible in principle, but it caused so many
complaints that newer versions of the compiler don't do it".
-- 
Legalize			Henry Spencer @ U of Toronto Zoology
freedom!			{allegra,ihnp4,decvax,pyramid}!utzoo!henry



More information about the Comp.lang.c mailing list