linux x86 kernel中的 memcpy 實現

來源:互聯網
上載者: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 ugly, but the compiler can optimize it totally, * as the count is constant. */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 to;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 to;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 to;}}

最終調用聲明

include/asm-x86/string_32.h

#define memcpy(t, f, n)\(__builtin_constant_p((n))\ ? __constant_memcpy((t), (f), (n))\ : __memcpy((t), (f), (n)))

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.