• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #if !__ARMEL__
2 #include <string.h>
3 #include <stdint.h>
4 #include <endian.h>
5 
6 #ifdef LOSCFG_KERNEL_LMS
__memcpy(void * restrict dest,const void * restrict src,size_t n)7 __attribute__((no_sanitize_address)) void *__memcpy(void *restrict dest, const void *restrict src, size_t n)
8 #else
9 void *memcpy(void *restrict dest, const void *restrict src, size_t n)
10 #endif
11 {
12 	unsigned char *d = dest;
13 	const unsigned char *s = src;
14 
15 #ifdef __GNUC__
16 
17 #if __BYTE_ORDER == __LITTLE_ENDIAN
18 #define LS >>
19 #define RS <<
20 #else
21 #define LS <<
22 #define RS >>
23 #endif
24 
25 	typedef uint32_t __attribute__((__may_alias__)) u32;
26 	uint32_t w, x;
27 
28 	for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++;
29 
30 	if ((uintptr_t)d % 4 == 0) {
31 		for (; n>=16; s+=16, d+=16, n-=16) {
32 			*(u32 *)(d+0) = *(u32 *)(s+0);
33 			*(u32 *)(d+4) = *(u32 *)(s+4);
34 			*(u32 *)(d+8) = *(u32 *)(s+8);
35 			*(u32 *)(d+12) = *(u32 *)(s+12);
36 		}
37 		if (n&8) {
38 			*(u32 *)(d+0) = *(u32 *)(s+0);
39 			*(u32 *)(d+4) = *(u32 *)(s+4);
40 			d += 8; s += 8;
41 		}
42 		if (n&4) {
43 			*(u32 *)(d+0) = *(u32 *)(s+0);
44 			d += 4; s += 4;
45 		}
46 		if (n&2) {
47 			*d++ = *s++; *d++ = *s++;
48 		}
49 		if (n&1) {
50 			*d = *s;
51 		}
52 		return dest;
53 	}
54 
55 	if (n >= 32) switch ((uintptr_t)d % 4) {
56 	case 1:
57 		w = *(u32 *)s;
58 		*d++ = *s++;
59 		*d++ = *s++;
60 		*d++ = *s++;
61 		n -= 3;
62 		for (; n>=17; s+=16, d+=16, n-=16) {
63 			x = *(u32 *)(s+1);
64 			*(u32 *)(d+0) = (w LS 24) | (x RS 8);
65 			w = *(u32 *)(s+5);
66 			*(u32 *)(d+4) = (x LS 24) | (w RS 8);
67 			x = *(u32 *)(s+9);
68 			*(u32 *)(d+8) = (w LS 24) | (x RS 8);
69 			w = *(u32 *)(s+13);
70 			*(u32 *)(d+12) = (x LS 24) | (w RS 8);
71 		}
72 		break;
73 	case 2:
74 		w = *(u32 *)s;
75 		*d++ = *s++;
76 		*d++ = *s++;
77 		n -= 2;
78 		for (; n>=18; s+=16, d+=16, n-=16) {
79 			x = *(u32 *)(s+2);
80 			*(u32 *)(d+0) = (w LS 16) | (x RS 16);
81 			w = *(u32 *)(s+6);
82 			*(u32 *)(d+4) = (x LS 16) | (w RS 16);
83 			x = *(u32 *)(s+10);
84 			*(u32 *)(d+8) = (w LS 16) | (x RS 16);
85 			w = *(u32 *)(s+14);
86 			*(u32 *)(d+12) = (x LS 16) | (w RS 16);
87 		}
88 		break;
89 	case 3:
90 		w = *(u32 *)s;
91 		*d++ = *s++;
92 		n -= 1;
93 		for (; n>=19; s+=16, d+=16, n-=16) {
94 			x = *(u32 *)(s+3);
95 			*(u32 *)(d+0) = (w LS 8) | (x RS 24);
96 			w = *(u32 *)(s+7);
97 			*(u32 *)(d+4) = (x LS 8) | (w RS 24);
98 			x = *(u32 *)(s+11);
99 			*(u32 *)(d+8) = (w LS 8) | (x RS 24);
100 			w = *(u32 *)(s+15);
101 			*(u32 *)(d+12) = (x LS 8) | (w RS 24);
102 		}
103 		break;
104 	}
105 	if (n&16) {
106 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
107 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
108 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
109 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
110 	}
111 	if (n&8) {
112 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
113 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
114 	}
115 	if (n&4) {
116 		*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
117 	}
118 	if (n&2) {
119 		*d++ = *s++; *d++ = *s++;
120 	}
121 	if (n&1) {
122 		*d = *s;
123 	}
124 	return dest;
125 #endif
126 
127 	for (; n; n--) *d++ = *s++;
128 	return dest;
129 }
130 #endif
131