+;------------------------------------------------------------------------------\r
+;\r
+; Copyright (c) 2006, Intel Corporation\r
+; All rights reserved. This program and the accompanying materials\r
+; are licensed and made available under the terms and conditions of the BSD License\r
+; which accompanies this distribution. The full text of the license may be found at\r
+; http://opensource.org/licenses/bsd-license.php\r
+;\r
+; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+;\r
+; Module Name:\r
+;\r
+; CopyMem.asm\r
+;\r
+; Abstract:\r
+;\r
+; CopyMem function\r
+;\r
+; Notes:\r
+;\r
+;------------------------------------------------------------------------------\r
+\r
+ .code\r
+\r
+InternalMemCopyMem PROC USES rsi rdi\r
+ mov rsi, rdx ; rsi <- Source\r
+ mov rdi, rcx ; rdi <- Destination\r
+ lea r9, [rdi + r8 - 1] ; r9 <- End of Destination\r
+ cmp rsi, rdi\r
+ mov rax, rdi ; rax <- Destination as return value\r
+ jae @F\r
+ cmp r9, rsi\r
+ jae @CopyBackward ; Copy backward if overlapped\r
+@@:\r
+ xor rcx, rcx\r
+ sub rcx, rsi\r
+ and rcx, 7 ; rcx + rsi aligns on 8-byte boundary\r
+ jz @F\r
+ cmp rcx, r8\r
+ cmova rcx, r8\r
+ sub r8, rcx ; r8 <- remaining bytes to copy\r
+ rep movsb\r
+@@:\r
+ mov rcx, r8\r
+ and r8, 7\r
+ shr rcx, 3 ; rcx <- # of Qwords to copy\r
+ jz @CopyBytes\r
+@@:\r
+ DB 48h, 0fh, 6fh, 06h ; movq mm0, [rsi]\r
+ DB 48h, 0fh, 0e7h, 07h ; movntq [rdi], mm0\r
+ add rsi, 8\r
+ add rdi, 8\r
+ loop @B\r
+ mfence\r
+ jmp @CopyBytes\r
+@CopyBackward:\r
+ mov rdi, r9 ; rdi <- End of Destination\r
+ lea rsi, [rsi + r8 - 1] ; rsi <- End of Source\r
+ std ; set direction flag\r
+@CopyBytes:\r
+ mov rcx, r8\r
+ rep movsb ; Copy bytes backward\r
+ cld\r
+ ret\r
+InternalMemCopyMem ENDP\r
+\r
+ END\r