xref: /haiku/src/system/libroot/posix/string/arch/generic/memcpy.c (revision 21258e2674226d6aa732321b6f8494841895af5f)
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 
6 #include <sys/types.h>
7 #include <string.h>
8 
9 
10 typedef int word;
11 
12 #define lsize sizeof(word)
13 #define lmask (lsize - 1)
14 
15 void *memcpy(void *dest, const void *src, size_t count)
16 {
17 	char *d = (char *)dest;
18 	const char *s = (const char *)src;
19 	int len;
20 
21 	if(count == 0 || dest == src)
22 		return dest;
23 
24 	if(((long)d | (long)s) & lmask) {
25 		// src and/or dest do not align on word boundary
26 		if((((long)d ^ (long)s) & lmask) || (count < lsize))
27 			len = count; // copy the rest of the buffer with the byte mover
28 		else
29 			len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
30 
31 		count -= len;
32 		for(; len > 0; len--)
33 			*d++ = *s++;
34 	}
35 	for(len = count / lsize; len > 0; len--) {
36 		*(word *)d = *(word *)s;
37 		d += lsize;
38 		s += lsize;
39 	}
40 	for(len = count & lmask; len > 0; len--)
41 		*d++ = *s++;
42 
43 	return dest;
44 }
45 
46