130423Smckusick /* 2*38485Skarels * Copyright (c) 1987, 1989 Regents of the University of California. 335110Sbostic * All rights reserved. 435110Sbostic * 535110Sbostic * Redistribution and use in source and binary forms are permitted 635110Sbostic * provided that the above copyright notice and this paragraph are 735110Sbostic * duplicated in all such forms and that any documentation, 835110Sbostic * advertising materials, and other materials related to such 935110Sbostic * distribution and use acknowledge that the software was developed 1035110Sbostic * by the University of California, Berkeley. The name of the 1135110Sbostic * University may not be used to endorse or promote products derived 1235110Sbostic * from this software without specific prior written permission. 1335110Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435110Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535110Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1630423Smckusick */ 1730423Smckusick 1830423Smckusick #if defined(LIBC_SCCS) && !defined(lint) 19*38485Skarels static char sccsid[] = "@(#)bcopy.c 5.5 (Berkeley) 07/23/89"; 2035110Sbostic #endif /* LIBC_SCCS and not lint */ 2130423Smckusick 2237105Sbostic #include <sys/types.h> 2337105Sbostic 2430423Smckusick /* 25*38485Skarels * bcopy -- copy memory block, handling overlap of source and destination 26*38485Skarels * (vax movc3 instruction) 2730423Smckusick */ 28*38485Skarels 29*38485Skarels typedef int word; /* size of "word" used for optimal copy speed */ 30*38485Skarels 3130423Smckusick bcopy(src, dst, length) 32*38485Skarels register char *src, *dst; 3330423Smckusick register int length; 3430423Smckusick { 3530423Smckusick if (length && src != dst) 3630423Smckusick if ((u_int)dst < (u_int)src) 37*38485Skarels if (((int)src | (int)dst | length) & (sizeof(word) - 1)) 3830423Smckusick do /* copy by bytes */ 3930423Smckusick *dst++ = *src++; 4030423Smckusick while (--length); 4130423Smckusick else { 42*38485Skarels length /= sizeof(word); 43*38485Skarels do { /* copy by words */ 44*38485Skarels *(word *)dst = *(word *)src; 45*38485Skarels src += sizeof(word); 46*38485Skarels dst += sizeof(word); 47*38485Skarels } while (--length); 4830423Smckusick } 4930423Smckusick else { /* copy backwards */ 5030423Smckusick src += length; 5130423Smckusick dst += length; 52*38485Skarels if (((int)src | (int)dst | length) & (sizeof(word) - 1)) 5330423Smckusick do /* copy by bytes */ 5430423Smckusick *--dst = *--src; 5530423Smckusick while (--length); 5630423Smckusick else { 57*38485Skarels length /= sizeof(word); 58*38485Skarels do { /* copy by words */ 59*38485Skarels *(word *)dst = *(word *)src; 60*38485Skarels src -= sizeof(word); 61*38485Skarels dst -= sizeof(word); 62*38485Skarels } while (--length); 6330423Smckusick } 6430423Smckusick } 6530423Smckusick } 66