[net.unix-wizards] How to shave yet another cycle off getc/putc macros

day@kovacs.UUCP (Dave Yost) (06/05/85)

Imagine, after all these years, finding a way to
shave one more cycle off the getc and putc macros!

On some well-known compilers, maybe all of them, the
following getc() and putc() #defines will generate
functionally equivalent but slightly more efficient code.
How this works is left as an exercise for the reader.
(On the VAX, you also get a free extra added bonus savings
because of the Increased Instruction Set Computer [IISC]
architecture.)

NOTE: These are modified from 4.2bsd stdio.h.
      Other versions may differ.

# ifdef	slower

#define	getc(p)		(--(p)->_cnt>=0? *(p)->_ptr++&0377:_filbuf(p))
#define	putc(x,p)	(--(p)->_cnt>=0? ((int)(*(p)->_ptr++=(unsigned)(x))):_flsbuf((unsigned)(x),p))

# else	slower

#define	getc(p)		(--(p)->_cnt < 0 ? _filbuf(p) : *(p)->_ptr++&0377)
#define	putc(x,p)	(--(p)->_cnt < 0 ? \
	_flsbuf((unsigned)(x),p) : ((int)(*(p)->_ptr++=(unsigned)(x))))

# endif	slower

  - - - - - - -

I also noticed that all the compilers I looked at (4)
generate some almost-always-superfluous code to implement
the almost-never-used (int) cast in the putc macro.
they should be smart enough to refrain from generating
it or they should optimize it out.  On the vax:
	cvtbl   (r0),r0
or on the 68000
	movb    a0@,d0
	extw    d0
	extl    d0
Or else maybe we should take that cast out of the putc macro.

--dave

chris@umcp-cs.UUCP (Chris Torek) (06/09/85)

Or maybe instead of changing getc and putc, we should teach the
compiler/optimizer to invert decrement/branch<0 ?: expressions so that
the optimizer can turn it into a sobgeq... (or sobgtr for branch<=0).
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris@umcp-cs		ARPA:	chris@maryland