]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseMemoryLib/CopyMem.c
MdePkg/BaseMemoryLib: Fix VS2015 build error
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / CopyMem.c
CommitLineData
e1f414b6 1/** @file\r
d531bfee 2 Implementation of the InternalMemCopyMem routine. This function is broken\r
3 out into its own source file so that it can be excluded from a build for a\r
4 particular platform easily if an optimized version is desired.\r
e1f414b6 5\r
5ea2bad0 6 Copyright (c) 2006 - 2016, Intel Corporation. All rights reserved.<BR>\r
01f688be
AB
7 Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
8 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>\r
9\r
2fcf0abf 10 This program and the accompanying materials\r
e1f414b6 11 are licensed and made available under the terms and conditions of the BSD License\r
12 which accompanies this distribution. The full text of the license may be found at\r
15c952e7 13 http://opensource.org/licenses/bsd-license.php.\r
e1f414b6 14\r
15 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
16 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
17\r
e1f414b6 18**/\r
19\r
1efcc4ae 20\r
f734a10a 21\r
e1f414b6 22\r
23#include "MemLibInternals.h"\r
24\r
25/**\r
26 Copy Length bytes from Source to Destination.\r
27\r
15c952e7 28 @param DestinationBuffer The target of the copy request.\r
29 @param SourceBuffer The place to copy from.\r
30 @param Length The number of bytes to copy.\r
e1f414b6 31\r
32 @return Destination\r
33\r
34**/\r
35VOID *\r
36EFIAPI\r
37InternalMemCopyMem (\r
efb23117 38 OUT VOID *DestinationBuffer,\r
39 IN CONST VOID *SourceBuffer,\r
e1f414b6 40 IN UINTN Length\r
41 )\r
42{\r
43 //\r
44 // Declare the local variables that actually move the data elements as\r
45 // volatile to prevent the optimizer from replacing this function with\r
46 // the intrinsic memcpy()\r
47 //\r
48 volatile UINT8 *Destination8;\r
49 CONST UINT8 *Source8;\r
01f688be
AB
50 volatile UINT32 *Destination32;\r
51 CONST UINT32 *Source32;\r
52 volatile UINT64 *Destination64;\r
53 CONST UINT64 *Source64;\r
54 UINTN Alignment;\r
55\r
56 if ((((UINTN)DestinationBuffer & 0x7) == 0) && (((UINTN)SourceBuffer & 0x7) == 0) && (Length >= 8)) {\r
57 if (SourceBuffer > DestinationBuffer) {\r
58 Destination64 = (UINT64*)DestinationBuffer;\r
59 Source64 = (CONST UINT64*)SourceBuffer;\r
60 while (Length >= 8) {\r
61 *(Destination64++) = *(Source64++);\r
62 Length -= 8;\r
63 }\r
64\r
65 // Finish if there are still some bytes to copy\r
66 Destination8 = (UINT8*)Destination64;\r
67 Source8 = (CONST UINT8*)Source64;\r
68 while (Length-- != 0) {\r
69 *(Destination8++) = *(Source8++);\r
70 }\r
71 } else if (SourceBuffer < DestinationBuffer) {\r
72 Destination64 = (UINT64*)((UINTN)DestinationBuffer + Length);\r
73 Source64 = (CONST UINT64*)((UINTN)SourceBuffer + Length);\r
74\r
75 // Destination64 and Source64 were aligned on a 64-bit boundary\r
76 // but if length is not a multiple of 8 bytes then they won't be\r
77 // anymore.\r
78\r
79 Alignment = Length & 0x7;\r
80 if (Alignment != 0) {\r
81 Destination8 = (UINT8*)Destination64;\r
82 Source8 = (CONST UINT8*)Source64;\r
83\r
84 while (Alignment-- != 0) {\r
85 *(--Destination8) = *(--Source8);\r
86 --Length;\r
87 }\r
88 Destination64 = (UINT64*)Destination8;\r
89 Source64 = (CONST UINT64*)Source8;\r
90 }\r
91\r
92 while (Length > 0) {\r
93 *(--Destination64) = *(--Source64);\r
94 Length -= 8;\r
95 }\r
96 }\r
97 } else if ((((UINTN)DestinationBuffer & 0x3) == 0) && (((UINTN)SourceBuffer & 0x3) == 0) && (Length >= 4)) {\r
98 if (SourceBuffer > DestinationBuffer) {\r
99 Destination32 = (UINT32*)DestinationBuffer;\r
100 Source32 = (CONST UINT32*)SourceBuffer;\r
101 while (Length >= 4) {\r
102 *(Destination32++) = *(Source32++);\r
103 Length -= 4;\r
104 }\r
105\r
106 // Finish if there are still some bytes to copy\r
107 Destination8 = (UINT8*)Destination32;\r
108 Source8 = (CONST UINT8*)Source32;\r
109 while (Length-- != 0) {\r
110 *(Destination8++) = *(Source8++);\r
111 }\r
112 } else if (SourceBuffer < DestinationBuffer) {\r
113 Destination32 = (UINT32*)((UINTN)DestinationBuffer + Length);\r
114 Source32 = (CONST UINT32*)((UINTN)SourceBuffer + Length);\r
115\r
116 // Destination32 and Source32 were aligned on a 32-bit boundary\r
117 // but if length is not a multiple of 4 bytes then they won't be\r
118 // anymore.\r
119\r
120 Alignment = Length & 0x3;\r
121 if (Alignment != 0) {\r
122 Destination8 = (UINT8*)Destination32;\r
123 Source8 = (CONST UINT8*)Source32;\r
124\r
125 while (Alignment-- != 0) {\r
126 *(--Destination8) = *(--Source8);\r
127 --Length;\r
128 }\r
129 Destination32 = (UINT32*)Destination8;\r
130 Source32 = (CONST UINT32*)Source8;\r
131 }\r
e1f414b6 132\r
01f688be
AB
133 while (Length > 0) {\r
134 *(--Destination32) = *(--Source32);\r
135 Length -= 4;\r
136 }\r
e1f414b6 137 }\r
01f688be
AB
138 } else {\r
139 if (SourceBuffer > DestinationBuffer) {\r
140 Destination8 = (UINT8*)DestinationBuffer;\r
141 Source8 = (CONST UINT8*)SourceBuffer;\r
142 while (Length-- != 0) {\r
143 *(Destination8++) = *(Source8++);\r
144 }\r
145 } else if (SourceBuffer < DestinationBuffer) {\r
5ea2bad0
MK
146 Destination8 = (UINT8*)DestinationBuffer + (Length - 1);\r
147 Source8 = (CONST UINT8*)SourceBuffer + (Length - 1);\r
01f688be 148 while (Length-- != 0) {\r
5ea2bad0 149 *(Destination8--) = *(Source8--);\r
01f688be 150 }\r
e1f414b6 151 }\r
152 }\r
efb23117 153 return DestinationBuffer;\r
e1f414b6 154}\r