]> git.proxmox.com Git - mirror_edk2.git/blame_incremental - MdePkg/Library/BaseMemoryLib/CopyMem.c
MdePkg/BaseMemoryLib: widen aligned accesses to 32 or 64 bits
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / CopyMem.c
... / ...
CommitLineData
1/** @file\r
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
5\r
6 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
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
10 This program and the accompanying materials\r
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
13 http://opensource.org/licenses/bsd-license.php.\r
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
18**/\r
19\r
20\r
21\r
22\r
23#include "MemLibInternals.h"\r
24\r
25/**\r
26 Copy Length bytes from Source to Destination.\r
27\r
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
31\r
32 @return Destination\r
33\r
34**/\r
35VOID *\r
36EFIAPI\r
37InternalMemCopyMem (\r
38 OUT VOID *DestinationBuffer,\r
39 IN CONST VOID *SourceBuffer,\r
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
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
132\r
133 while (Length > 0) {\r
134 *(--Destination32) = *(--Source32);\r
135 Length -= 4;\r
136 }\r
137 }\r
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
146 Destination8 = (UINT8*)DestinationBuffer + Length;\r
147 Source8 = (CONST UINT8*)SourceBuffer + Length;\r
148 while (Length-- != 0) {\r
149 *(--Destination8) = *(--Source8);\r
150 }\r
151 }\r
152 }\r
153 return DestinationBuffer;\r
154}\r