1 /** @file
2 
3   Copyright (c) 2012-2013, ARM Ltd. All rights reserved.<BR>
4 
5   This program and the accompanying materials
6   are licensed and made available under the terms and conditions of the BSD License
7   which accompanies this distribution.  The full text of the license may be found at
8   http://opensource.org/licenses/bsd-license.php
9 
10   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 
13 **/
14 
15 #include "MemLibInternals.h"
16 
17 /**
18   Set Buffer to Value for Size bytes.
19 
20   @param  Buffer   Memory to set.
21   @param  Length   Number of bytes to set
22   @param  Value    Value of the set operation.
23 
24   @return Buffer
25 
26 **/
27 VOID *
28 EFIAPI
InternalMemSetMem(OUT VOID * Buffer,IN UINTN Length,IN UINT8 Value)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 = (((UINT64)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     while (Length-- > 0) {
60       *(Pointer8++) = Value;
61     }
62   } else if ((((UINTN)Buffer & 0x3) == 0) && (Length >= 4)) {
63     // Generate the 32bit value
64     Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;
65 
66     Pointer32 = (UINT32*)Buffer;
67     while (Length >= 4) {
68       *(Pointer32++) = Value32;
69       Length -= 4;
70     }
71 
72     // Finish with bytes if needed
73     Pointer8 = (UINT8*)Pointer32;
74     while (Length-- > 0) {
75       *(Pointer8++) = Value;
76     }
77   } else {
78     Pointer8 = (UINT8*)Buffer;
79     while (Length-- > 0) {
80       *(Pointer8++) = Value;
81     }
82   }
83   return Buffer;
84 }
85