How to shave yet another cycle off getc/putc macros

Dave Yost day at kovacs.UUCP
Wed Jun 5 16:29:03 AEST 1985


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



More information about the Comp.unix.wizards mailing list