xref: /haiku/src/system/libroot/posix/string/arch/generic/memcpy.c (revision 379ad6d0372d6b3242b4a56925f5b8512d63500f)
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 /* do not build for for arches that have overridden this with an assembly version */
10 #if !defined(ARCH_x86)
11 
12 typedef int word;
13 
14 #define lsize sizeof(word)
15 #define lmask (lsize - 1)
16 
17 void *memcpy(void *dest, const void *src, size_t count)
18 {
19 	char *d = (char *)dest;
20 	const char *s = (const char *)src;
21 	int len;
22 
23 	if(count == 0 || dest == src)
24 		return dest;
25 
26 	if(((long)d | (long)s) & lmask) {
27 		// src and/or dest do not align on word boundary
28 		if((((long)d ^ (long)s) & lmask) || (count < lsize))
29 			len = count; // copy the rest of the buffer with the byte mover
30 		else
31 			len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
32 
33 		count -= len;
34 		for(; len > 0; len--)
35 			*d++ = *s++;
36 	}
37 	for(len = count / lsize; len > 0; len--) {
38 		*(word *)d = *(word *)s;
39 		d += lsize;
40 		s += lsize;
41 	}
42 	for(len = count & lmask; len > 0; len--)
43 		*d++ = *s++;
44 
45 	return dest;
46 }
47 
48 #endif
49 
50