Memcpy implementation in Linux x86 Kernel

Source: Internet
Author: User
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 )))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.