vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

memcpy.c (864B)


      1 
      2 #include <string.h>
      3 #include <stdint.h>
      4 
      5 void *memcpy(void *dest, const void *s, size_t n)
      6 {
      7 	void *d = dest;
      8 	uint32_t nn;
      9 
     10 	// It's not worth trying to optimize really short copies.
     11 	if (n < 16)
     12 		goto bytecopy;
     13 
     14 	// Try to get the dest and src pointers on a 32-byte boundary
     15 	if ((uint32_t)d & 3) {
     16 
     17 		if (((uint32_t)d & 3) != ((uint32_t)s & 3))
     18 			goto bytecopy;
     19 
     20 		nn = 4 - ((uint32_t)d & 3);
     21 		if (nn > n)
     22 			nn = n;
     23 
     24 		asm volatile("rep movsb"
     25 				: "=D" (d), "=S" (s)
     26 				: "D" (d), "S" (s), "c" (nn)
     27 				: "memory");
     28 	}
     29 
     30 	// Copy 32-bit words
     31 	if (n >> 2) {
     32 		asm volatile("rep movsl"
     33 				: "=D" (d), "=S" (s)
     34 				: "D" (d), "S" (s), "c" (n >> 2)
     35 				: "memory");
     36 		n &= 3;
     37 	}
     38 
     39 	// Copy any remaining bytes
     40 	bytecopy:
     41 	if (n > 0) {
     42 		asm volatile("rep movsb"
     43 				: "=D" (d), "=S" (s)
     44 				: "D" (d), "S" (s), "c" (n)
     45 				: "memory");
     46 	}
     47 
     48 	return dest;
     49 }
     50