Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!henry From: henry@utzoo.UUCP (Henry Spencer) Newsgroups: comp.lang.c Subject: Re: Style [++i vs i++] Message-ID: <8311@utzoo.UUCP> Date: Fri, 17-Jul-87 12:27:03 EDT Article-I.D.: utzoo.8311 Posted: Fri Jul 17 12:27:03 1987 Date-Received: Fri, 17-Jul-87 12:27:03 EDT References: <17310@amdcad.AMD.COM> <246@hubcap.UUCP>, <259@hwee.UUCP> Organization: U of Toronto Zoology Lines: 39 > >There is no reason a compiler needs to generate code differently for i++; and > >++i; if that is the entire expression (and statement). > > As I remember the original PDP-11 C compiler did treat the two cases > differently. This was because the PDP had an autoincrement addressing mode > of the form > > Op (Rn)+ > > which meant that register n was used as a pointer to a location and then > incremented. This made i++ more efficient that ++i since it could make use > of this mode.... > This is probably no longer true of many (or all) systems... Actually, it's not true of the 11 either. Sorry, but you have misunderstood. The PDP11 C compiler generates identical code for "i++" and "++i" when they are found in isolation, and it doesn't use autoincrement addressing for either. The autoincrement addressing modes are really useful only in constructs of the form "*i++", because these modes necessarily cause a memory reference to the location pointed at (considering an integer operand as if it were a pointer). Except in the case of "*i++" and suchlike, the compiler has absolutely no guarantee that an integer variable being incremented would be valid as a pointer -- trying to indirect through it could easily cause a trap. So the compiler dares not use autoincrement and autodecrement as increment and decrement primitives except in certain special cases. Incidentally, at least on modern 11s (I can't be bothered digging out all the old timing tables and comparing), autoincrement is slower than an INC instruction if the increment is the sole purpose of the instruction. (Well, true, things are a bit more complex for incrementing pointers, where using autoincrement can be a slight win.) The real win of autoincrement is that IF indirection is already present, autoincrementing costs very little extra. It is indeed true that "*i++" and "*--i" are substantially faster than "*++i" and "*i--" on 11s and many 11-derived architectures (68000, VAX). But removing the "*" makes a big difference. -- Support sustained spaceflight: fight | Henry Spencer @ U of Toronto Zoology the soi-disant "Planetary Society"! | {allegra,ihnp4,decvax,utai}!utzoo!henry