faster bcopy using duffs device (source)

Chris Torek chris at mimsy.UUCP
Tue Sep 12 22:33:11 AEST 1989


>In article <19491 at mimsy.UUCP> chris at mimsy.UUCP (Chris Torek) writes:
>>Just for fun, here is my bcopy-in-C for (gcc+68010)
>>
>>#ifndef __GNUC__
>>			/* for some reason, gcc does not optimise this */
>>			*(short *)dst = *(short *)src;
>>			dst -= sizeof (short);
>>			src -= sizeof (short);
>>#else
>>			/* so we use a gcc extension */
>>			*--(short *)dst = *--(short *)src;
>>#endif

In article <16902 at rphroy.UUCP> tkacik at rphroy.UUCP (Tom Tkacik) writes:
>Do these two program fragments really do the same thing?

OOPS!  I did not need the gcc-specific grungy hack after all.  The
first three (#ifndef __GNUC__) lines *should* have read:

	dst -= sizeof (short);
	src -= sizeof (short);
	*(short *)dst = *(short *)src;

which gcc is perfectly willing to optimise.

>I think that the gcc code should be
>			*(short *)dst-- = *(short *)src--;

No, this is legal C, and means to:

	evaluate dst, evaluate src;
	sometime in the near future, assign dst-1 to dst and
		src-1 to src;
	fetch a short from the location named by the
		conversion of the original value of the
		character pointer `src' to a pointer-to-short
		(a machine-dependent operation);
	store that short to the location named by the
		conversion of ... `dst' (another machine-dependent
		operation);
	<sequence point>: finish side effects on dst and src.

The gcc-specific hack is not legal C; it means to

	pretend dst and src are pointers to short, rather than
		pointers to char (this operation is not only
		machine-dependent, it is hopeless on some machines,
		e.g., those where `char *' is 48 bits long but
		`short *' is 32 bits long);
	assign dst-sizeof(short) to dst and src-sizeof(short)
		to src, sometime in the near future;
	fetch a short from the location named by the result of
		the decrement of the (pretended word pointer) src;
	store that short in the location named by the result ... dst;
	<sequence point>: finish side effects on dst and src.

>I just did that and do not see what extenstion Chris is referring to.
>Chris, which is it?

The one that says that a cast on a pointer is a type pun, rather than
a conversion.  This, if nothing else, tells why gcc cannot be used to
compile C code for a Prime (32 bit word pointers, 48 bit char pointers).
(In fact, gcc assumes internally that sizeof(any-pointer) == sizeof(int)
and that all pointers are `byte pointers', as far as I can tell.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list