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