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