]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseMemoryLib/SetMem.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / MdePkg / Library / BaseMemoryLib / SetMem.c
CommitLineData
e1f414b6 1/** @file\r
2 Implementation of the EfiSetMem routine. This function is broken\r
3 out into its own source file so that it can be excluded from a\r
4 build for a particular platform easily if an optimized version\r
5 is desired.\r
6\r
2fcf0abf 7 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
01f688be
AB
8 Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
9 Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>\r
10\r
9344f092 11 SPDX-License-Identifier: BSD-2-Clause-Patent\r
e1f414b6 12\r
e1f414b6 13**/\r
14\r
e1f414b6 15#include "MemLibInternals.h"\r
16\r
17/**\r
18 Set Buffer to Value for Size bytes.\r
19\r
15c952e7 20 @param Buffer The memory to set.\r
21 @param Length The number of bytes to set.\r
22 @param Value The value of the set operation.\r
e1f414b6 23\r
24 @return Buffer\r
25\r
26**/\r
27VOID *\r
28EFIAPI\r
29InternalMemSetMem (\r
2f88bd3a
MK
30 OUT VOID *Buffer,\r
31 IN UINTN Length,\r
32 IN UINT8 Value\r
e1f414b6 33 )\r
34{\r
35 //\r
36 // Declare the local variables that actually move the data elements as\r
37 // volatile to prevent the optimizer from replacing this function with\r
38 // the intrinsic memset()\r
39 //\r
2f88bd3a
MK
40 volatile UINT8 *Pointer8;\r
41 volatile UINT32 *Pointer32;\r
42 volatile UINT64 *Pointer64;\r
43 UINT32 Value32;\r
44 UINT64 Value64;\r
01f688be
AB
45\r
46 if ((((UINTN)Buffer & 0x7) == 0) && (Length >= 8)) {\r
47 // Generate the 64bit value\r
48 Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;\r
49 Value64 = LShiftU64 (Value32, 32) | Value32;\r
50\r
2f88bd3a 51 Pointer64 = (UINT64 *)Buffer;\r
01f688be
AB
52 while (Length >= 8) {\r
53 *(Pointer64++) = Value64;\r
2f88bd3a 54 Length -= 8;\r
01f688be 55 }\r
e1f414b6 56\r
01f688be 57 // Finish with bytes if needed\r
2f88bd3a 58 Pointer8 = (UINT8 *)Pointer64;\r
01f688be
AB
59 } else if ((((UINTN)Buffer & 0x3) == 0) && (Length >= 4)) {\r
60 // Generate the 32bit value\r
61 Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;\r
62\r
2f88bd3a 63 Pointer32 = (UINT32 *)Buffer;\r
01f688be
AB
64 while (Length >= 4) {\r
65 *(Pointer32++) = Value32;\r
2f88bd3a 66 Length -= 4;\r
01f688be
AB
67 }\r
68\r
69 // Finish with bytes if needed\r
2f88bd3a 70 Pointer8 = (UINT8 *)Pointer32;\r
01f688be 71 } else {\r
2f88bd3a 72 Pointer8 = (UINT8 *)Buffer;\r
01f688be 73 }\r
2f88bd3a 74\r
7efb1d89 75 while (Length-- > 0) {\r
01f688be 76 *(Pointer8++) = Value;\r
e1f414b6 77 }\r
2f88bd3a 78\r
e1f414b6 79 return Buffer;\r
80}\r