"conventional" copy fragment?

Mark Jones jones at ingr.UUCP
Thu Sep 22 02:03:26 AEST 1988


In article <8809092109.AA06071 at tycho.yerkes.uchicago.edu>, pearce at TYCHO.YERKES.UCHICAGO.EDU ("Eric C. Pearce") writes:
> 
> Personally, I prefer this "conventional" copy fragment:
> 
>       A = array1;
>       B = array2;
> 
>       n = Count / 8;
>       for (i =  Count%8; i > 0; i--)
> 	 *A++ = *B++;
>       while (--n >= 0) {
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
> 	 *A++ = *B++; 
>       } 
> 
> >From a performance standpoint, it falls only 2% behind duff device and
> is 102% more readable and not "kludgy".

How about this "conventional" copy fragment

	memcpy(array2,array1,sizeof(*A)*Count);	/* copy array1 to array2 */

Almost every system has this call (I have never seen one that didn't)
and if it was implemented well, it could be done in as few as 7
instructions, on a brain damaged microprocessor.

	mov	cx,{Count|
	shl	cx,1			/* for 2 byte objects */
	shl	cx,1			/* for 4 byte objects */
	mov	si,{address of array1}
	mov	di,{address of array2}
	repnz
	movsw				/* done */

or
	mov.l	a0,?(a7)
	mov.l	a1,?(a7)
	mov.?	d0,Count;
	/* shift left once or twice */
loop:
	mov.b	(a0)+,(a1)+
	dbnz	loop		/* i don't remember the exact syntax here */


Mark Jones

PS I don't guarantee syntax, but you should get the idea.  Use the
functions in the C library.  If you need more speed, recode the library
routines in assembly, and link them in ahead of the standard library. 
Don't write trashy C code to try to get better speed.  It just ain't
worth it, now or later.


PPS Yes, I know the 68000 code could be made even better, very easily,
(should check for alignment, use bigger moves, etc...) but this is only
an example.



More information about the Comp.lang.c mailing list