1 /* 2 * memmove.c 3 */ 4 5 #include <string.h> 6 7 void *memmove(void *dst, const void *src, size_t n) 8 { 9 const char *p = src; 10 char *q = dst; 11 #if defined(__i386__) || defined(__x86_64__) 12 if (q < p) { 13 asm volatile("cld; rep; movsb" 14 : "+c" (n), "+S"(p), "+D"(q)); 15 } else { 16 p += (n - 1); 17 q += (n - 1); 18 asm volatile("std; rep; movsb; cld" 19 : "+c" (n), "+S"(p), "+D"(q)); 20 } 21 #else 22 if (q < p) { 23 while (n--) { 24 *q++ = *p++; 25 } 26 } else { 27 p += n; 28 q += n; 29 while (n--) { 30 *--q = *--p; 31 } 32 } 33 #endif 34 35 return dst; 36 } 37