Static _ always_inline void * _ memcpy (void * To, const void * From, size_t N) {int D0, D1, D2; ASM volatile ("rep; movsl \ n \ t "" movl % 4, % ECx \ n \ t "" andl $3, % ECx \ n \ t "" JZ 1f \ n \ t "" rep; movsb \ n \ t "" 1: ":" = & C "(D0 ), "= & D" (D1), "= & S" (D2): "0" (N/4), "G" (N ), "1" (long) to), "2" (long) from): "Memory"); return to;}/** this looks uugly, but the compiler can optimize it totally, * as the Count is const Ant. */static _ always_inline void * _ constant_memcpy (void * To, const void * From, size_t N) {long ESI, EDI; If (! N) return to; Switch (n) {Case 1: * (char *) to = * (char *) from; return to; Case 2: * (short *) to = * (short *) from; return to; Case 4: * (int *) to = * (int *) from; return to; Case 3: * (short *) to = * (short *) from; * (char *) to + 2) = * (char *) from + 2); return to; Case 5: * (int *) to = * (int *) from; * (char *) to + 4) = * (char *) from + 4); return; case 6: * (int *) to = * (int *) from; * (short *) to + 2) = * (short *) from + 2 ); return to; case 8: * (int *) to = * (int *) from; * (int *) to + 1) = * (int *) from + 1); return to;} ESI = (long) from; EDI = (long) to; If (n> = 5*4) {/* large block: use rep prefix */INT ECx; ASM volatile ("rep; movsl": "= & C" (ECx), "= & D" (EDI ), "= & S" (Esi): "0" (N/4), "1" (EDI), "2" (Esi): "Memory ");} else {/* Small block: Don't clobber ECx + smaller Code */If (n> = 4*4) ASM volatile ("movsl ": "= & D" (EDI), "= & S" (Esi): "0" (EDI), "1" (Esi): "Memory "); if (n> = 3*4) ASM volatile ("movsl": "= & D" (EDI), "= & S" (Esi ): "0" (EDI), "1" (Esi): "Memory"); If (n> = 2*4) ASM volatile ("movsl ": "= & D" (EDI), "= & S" (Esi): "0" (EDI), "1" (Esi): "Memory "); if (n> = 1*4) ASM volatile ("movsl": "= & D" (EDI), "= & S" (Esi ): "0" (EDI), "1" (Esi): "Memory");} switch (N % 4) {/* tail */case 0: Return; case 1: ASM volatile ("movsb": "= & D" (EDI), "= & S" (Esi): "0" (EDI ), "1" (Esi): "Memory"); return to; Case 2: ASM volatile ("movsw": "= & D" (EDI ), "= & S" (Esi): "0" (EDI), "1" (Esi): "Memory"); return to; default: ASM volatile ("movsw \ n \ tmovsb": "= & D" (EDI), "= & S" (Esi): "0" (EDI ), "1" (Esi): "Memory"); return ;}}
Final Call Statement
Include/asm-x86/string_32.h
# Define memcpy (T, F, n) \ (_ builtin_constant_p (n ))\? _ Constant_memcpy (t), (f), (n) \: _ memcpy (t), (f), (n )))