1 /* $Id: bcopy.c,v 1.4 2020-09-27 22:06:16 phil Exp $ */
2 
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Chris Torek.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include "config.h"
41 #endif /* HAVE_CONFIG_H defined */
42 
43 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid[] = "@(#)bcopy.c	5.11 (Berkeley) 6/21/91";
45 #endif /* LIBC_SCCS and not lint */
46 
47 #if 0
48 #include <sys/cdefs.h>
49 #include <string.h>
50 #else
51 
52 #include "h.h"
53 typedef int size_t;
54 #endif
55 
56 
57 /*
58  * sizeof(word) MUST BE A POWER OF TWO
59  * SO THAT wmask BELOW IS ALL ONES
60  */
61 typedef	int word;		/* "word" used for optimal copy speed */
62 
63 #define	wsize	sizeof(word)
64 #define	wmask	(wsize - 1)
65 
66 /*
67  * Copy a block of memory, handling overlap.
68  * This is the routine that actually implements
69  * (the portable versions of) bcopy, memcpy, and memmove.
70  */
71 #ifdef MEMCOPY
72 void *
memcpy(void * dst0,const void * src0,register size_t length)73 memcpy(void *dst0, const void *src0, register size_t length)
74 #else
75 #ifdef MEMMOVE
76 void *
77 memmove(void *dst0, const void *src0, register size_t length)
78 #else
79 void
80 bcopy(const void *src0, void *dst0, register size_t length)
81 #endif
82 #endif
83 {
84 	register char *dst = dst0;
85 	register const char *src = src0;
86 	register size_t t;
87 
88 	if (length == 0 || dst == src)		/* nothing to do */
89 		goto done;
90 
91 	/*
92 	 * Macros: loop-t-times; and loop-t-times, t>0
93 	 */
94 #define	TLOOP(s) if (t) TLOOP1(s)
95 #define	TLOOP1(s) do { s; } while (--t)
96 
97 	if ((unsigned long)dst < (unsigned long)src) {
98 		/*
99 		 * Copy forward.
100 		 */
101 		t = (long)src;	/* only need low bits */
102 		if ((t | (long)dst) & wmask) {
103 			/*
104 			 * Try to align operands.  This cannot be done
105 			 * unless the low bits match.
106 			 */
107 			if ((t ^ (long)dst) & wmask || length < wsize)
108 				t = length;
109 			else
110 				t = wsize - (t & wmask);
111 			length -= t;
112 			TLOOP1(*dst++ = *src++);
113 		}
114 		/*
115 		 * Copy whole words, then mop up any trailing bytes.
116 		 */
117 		t = length / wsize;
118 		TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize);
119 		t = length & wmask;
120 		TLOOP(*dst++ = *src++);
121 	} else {
122 		/*
123 		 * Copy backwards.  Otherwise essentially the same.
124 		 * Alignment works as before, except that it takes
125 		 * (t&wmask) bytes to align, not wsize-(t&wmask).
126 		 */
127 		src += length;
128 		dst += length;
129 		t = (long)src;
130 		if ((t | (long)dst) & wmask) {
131 			if ((t ^ (long)dst) & wmask || length <= wsize)
132 				t = length;
133 			else
134 				t &= wmask;
135 			length -= t;
136 			TLOOP1(*--dst = *--src);
137 		}
138 		t = length / wsize;
139 		TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src);
140 		t = length & wmask;
141 		TLOOP(*--dst = *--src);
142 	}
143 done:
144 #if defined(MEMCOPY) || defined(MEMMOVE)
145 	return (dst0);
146 #else
147 	return;
148 #endif
149 }
150