bcopy.c revision 99035
177957Sbenno/*-
277957Sbenno * Copyright (c) 1990 The Regents of the University of California.
377957Sbenno * All rights reserved.
477957Sbenno *
577957Sbenno * This code is derived from software contributed to Berkeley by
677957Sbenno * Chris Torek.
777957Sbenno *
877957Sbenno * Redistribution and use in source and binary forms, with or without
977957Sbenno * modification, are permitted provided that the following conditions
1077957Sbenno * are met:
1177957Sbenno * 1. Redistributions of source code must retain the above copyright
1277957Sbenno *    notice, this list of conditions and the following disclaimer.
1377957Sbenno * 2. Redistributions in binary form must reproduce the above copyright
1477957Sbenno *    notice, this list of conditions and the following disclaimer in the
1577957Sbenno *    documentation and/or other materials provided with the distribution.
1677957Sbenno * 3. All advertising materials mentioning features or use of this software
1777957Sbenno *    must display the following acknowledgement:
1877957Sbenno *	This product includes software developed by the University of
1977957Sbenno *	California, Berkeley and its contributors.
2077957Sbenno * 4. Neither the name of the University nor the names of its contributors
2177957Sbenno *    may be used to endorse or promote products derived from this software
2277957Sbenno *    without specific prior written permission.
2377957Sbenno *
2477957Sbenno * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2577957Sbenno * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2677957Sbenno * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2777957Sbenno * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2877957Sbenno * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2977957Sbenno * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3077957Sbenno * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3177957Sbenno * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3277957Sbenno * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3377957Sbenno * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3477957Sbenno * SUCH DAMAGE.
3577957Sbenno */
3677957Sbenno
3777957Sbenno#if defined(LIBC_SCCS) && !defined(lint)
3877957Sbenno#if 0
3977957Sbennostatic char *sccsid = "from: @(#)bcopy.c      5.11 (Berkeley) 6/21/91";
4077957Sbenno#endif
4177957Sbenno#if 0
4277957Sbennostatic char *rcsid = "$NetBSD: bcopy.c,v 1.2 1997/04/16 22:09:41 thorpej Exp $";
4377957Sbenno#endif
4477957Sbenno#endif /* LIBC_SCCS and not lint */
4577957Sbenno#ifndef lint
4677957Sbennostatic char *rcsid =
4777957Sbenno  "$FreeBSD: head/sys/powerpc/powerpc/bcopy.c 99035 2002-06-29 09:34:54Z benno $";
4877957Sbenno#endif
4977957Sbenno
5077957Sbenno#include <sys/param.h>
5177957Sbenno#ifdef _KERNEL
5277957Sbenno#include <sys/systm.h>
5377957Sbenno#else
5477957Sbenno#include <string.h>
5577957Sbenno#endif
5677957Sbenno
5777957Sbenno/*
5877957Sbenno * sizeof(word) MUST BE A POWER OF TWO
5977957Sbenno * SO THAT wmask BELOW IS ALL ONES
6077957Sbenno */
6177957Sbennotypedef	int	word;		/* "word" used for optimal copy speed */
6277957Sbenno
6377957Sbenno#define	wsize	sizeof(word)
6477957Sbenno#define wmask	(wsize - 1)
6577957Sbenno
6677957Sbenno/*
6777957Sbenno * Copy a block of memory, handling overlap.
6877957Sbenno * This is the routine that actually implements
6977957Sbenno * (the portable versions of) bcopy, memcpy, and memmove.
7077957Sbenno */
7177957Sbennovoid *
7277957Sbennomemcpy(void *dst0, const void *src0, size_t length)
7377957Sbenno{
7477957Sbenno	char		*dst;
7577957Sbenno	const char	*src;
7677957Sbenno	size_t		t;
7777957Sbenno
7877957Sbenno	dst = dst0;
7977957Sbenno	src = src0;
8077957Sbenno
8177957Sbenno	if (length == 0 || dst == src) {	/* nothing to do */
8277957Sbenno		goto done;
8377957Sbenno	}
8477957Sbenno
8577957Sbenno	/*
8677957Sbenno	 * Macros: loop-t-times; and loop-t-times, t>0
8777957Sbenno	 */
8877957Sbenno#define	TLOOP(s) if (t) TLOOP1(s)
8977957Sbenno#define	TLOOP1(s) do { s; } while (--t)
9077957Sbenno
9177957Sbenno	if ((unsigned long)dst < (unsigned long)src) {
9277957Sbenno		/*
9377957Sbenno		 * Copy forward.
9477957Sbenno		 */
9577957Sbenno		t = (int)src;	/* only need low bits */
9677957Sbenno
9777957Sbenno		if ((t | (int)dst) & wmask) {
9877957Sbenno			/*
9977957Sbenno			 * Try to align operands.  This cannot be done
10077957Sbenno			 * unless the low bits match.
10177957Sbenno			 */
10277957Sbenno			if ((t ^ (int)dst) & wmask || length < wsize) {
10377957Sbenno				t = length;
10477957Sbenno			} else {
10577957Sbenno				t = wsize - (t & wmask);
10677957Sbenno			}
10777957Sbenno
10877957Sbenno			length -= t;
10977957Sbenno			TLOOP1(*dst++ = *src++);
11077957Sbenno		}
11177957Sbenno		/*
11277957Sbenno		 * Copy whole words, then mop up any trailing bytes.
11377957Sbenno		 */
11477957Sbenno		t = length / wsize;
11577957Sbenno		TLOOP(*(word *)dst = *(const word *)src; src += wsize;
11677957Sbenno		    dst += wsize);
11777957Sbenno		t = length & wmask;
11877957Sbenno		TLOOP(*dst++ = *src++);
11977957Sbenno	} else {
12077957Sbenno		/*
12177957Sbenno		 * Copy backwards.  Otherwise essentially the same.
12277957Sbenno		 * Alignment works as before, except that it takes
12377957Sbenno		 * (t&wmask) bytes to align, not wsize-(t&wmask).
12477957Sbenno		 */
12577957Sbenno		src += length;
12677957Sbenno		dst += length;
12777957Sbenno		t = (int)src;
12877957Sbenno
12977957Sbenno		if ((t | (int)dst) & wmask) {
13077957Sbenno			if ((t ^ (int)dst) & wmask || length <= wsize) {
13177957Sbenno				t = length;
13277957Sbenno			} else {
13377957Sbenno				t &= wmask;
13477957Sbenno			}
13577957Sbenno
13677957Sbenno			length -= t;
13777957Sbenno			TLOOP1(*--dst = *--src);
13877957Sbenno		}
13977957Sbenno		t = length / wsize;
14077957Sbenno		TLOOP(src -= wsize; dst -= wsize;
14177957Sbenno		    *(word *)dst = *(const word *)src);
14277957Sbenno		t = length & wmask;
14377957Sbenno		TLOOP(*--dst = *--src);
14477957Sbenno	}
14577957Sbennodone:
14677957Sbenno	return (dst0);
14777957Sbenno}
14877957Sbenno
14977957Sbennovoid
15077957Sbennobcopy(const void *src0, void *dst0, size_t length)
15177957Sbenno{
15277957Sbenno
15377957Sbenno	memcpy(dst0, src0, length);
15477957Sbenno}
15599035Sbenno
15699035Sbennovoid
15799035Sbennoovbcopy(const void *src0, void *dst0, size_t length)
15899035Sbenno{
15999035Sbenno
16099035Sbenno	memcpy(dst0, src0, length);
16199035Sbenno}
162