Home | History | Annotate | Download | only in BaseMemoryLib
      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   This program and the accompanying materials
     12   are licensed and made available under the terms and conditions of the BSD License
     13   which accompanies this distribution.  The full text of the license may be found at
     14   http://opensource.org/licenses/bsd-license.php.
     15 
     16   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
     17   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
     18 
     19 **/
     20 
     21 
     22 
     23 
     24 #include "MemLibInternals.h"
     25 
     26 /**
     27   Set Buffer to Value for Size bytes.
     28 
     29   @param  Buffer   The memory to set.
     30   @param  Length   The number of bytes to set.
     31   @param  Value    The value of the set operation.
     32 
     33   @return Buffer
     34 
     35 **/
     36 VOID *
     37 EFIAPI
     38 InternalMemSetMem (
     39   OUT     VOID                      *Buffer,
     40   IN      UINTN                     Length,
     41   IN      UINT8                     Value
     42   )
     43 {
     44   //
     45   // Declare the local variables that actually move the data elements as
     46   // volatile to prevent the optimizer from replacing this function with
     47   // the intrinsic memset()
     48   //
     49   volatile UINT8                    *Pointer8;
     50   volatile UINT32                   *Pointer32;
     51   volatile UINT64                   *Pointer64;
     52   UINT32                            Value32;
     53   UINT64                            Value64;
     54 
     55   if ((((UINTN)Buffer & 0x7) == 0) && (Length >= 8)) {
     56     // Generate the 64bit value
     57     Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;
     58     Value64 = LShiftU64 (Value32, 32) | Value32;
     59 
     60     Pointer64 = (UINT64*)Buffer;
     61     while (Length >= 8) {
     62       *(Pointer64++) = Value64;
     63       Length -= 8;
     64     }
     65 
     66     // Finish with bytes if needed
     67     Pointer8 = (UINT8*)Pointer64;
     68   } else if ((((UINTN)Buffer & 0x3) == 0) && (Length >= 4)) {
     69     // Generate the 32bit value
     70     Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;
     71 
     72     Pointer32 = (UINT32*)Buffer;
     73     while (Length >= 4) {
     74       *(Pointer32++) = Value32;
     75       Length -= 4;
     76     }
     77 
     78     // Finish with bytes if needed
     79     Pointer8 = (UINT8*)Pointer32;
     80   } else {
     81     Pointer8 = (UINT8*)Buffer;
     82   }
     83   while (Length-- > 0) {
     84     *(Pointer8++) = Value;
     85   }
     86   return Buffer;
     87 }
     88