]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseMemoryLib: widen aligned accesses to 32 or 64 bits
authorArd Biesheuvel <ard.biesheuvel@linaro.org>
Fri, 2 Sep 2016 07:26:23 +0000 (08:26 +0100)
committerArd Biesheuvel <ard.biesheuvel@linaro.org>
Tue, 13 Sep 2016 15:28:15 +0000 (16:28 +0100)
Since the default BaseMemoryLib should be callable from any context,
including ones where unaligned accesses are not allowed, it implements
InternalCopyMem() and InternalSetMem() using byte accesses only.
However, especially in a context where the MMU is off, such narrow
accesses may be disproportionately costly, and so if the size and
alignment of the access allow it, use 32-bit or even 64-bit loads and
stores (the latter may be beneficial even on a 32-bit architectures like
ARM, which has load pair/store pair instructions)

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdePkg/Library/BaseMemoryLib/BaseMemoryLib.inf
MdePkg/Library/BaseMemoryLib/CopyMem.c
MdePkg/Library/BaseMemoryLib/SetMem.c

index 6d906e93faf32adeff38157556f8f57b52b2274c..358eeed4f44959f90db5600ebc1d553d525337de 100644 (file)
@@ -26,7 +26,7 @@
 \r
 \r
 #\r
-#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC\r
+#  VALID_ARCHITECTURES           = IA32 X64 IPF EBC ARM AARCH64\r
 #\r
 \r
 [Sources]\r
index 37f03660df5f6870c9e6126325f547be87a5e72f..6f4fd900df5d2e50729f27115646a8ea3cd789cb 100644 (file)
@@ -4,6 +4,9 @@
   particular platform easily if an optimized version is desired.\r
 \r
   Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>\r
+\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -44,18 +47,107 @@ InternalMemCopyMem (
   //\r
   volatile UINT8                    *Destination8;\r
   CONST UINT8                       *Source8;\r
+  volatile UINT32                   *Destination32;\r
+  CONST UINT32                      *Source32;\r
+  volatile UINT64                   *Destination64;\r
+  CONST UINT64                      *Source64;\r
+  UINTN                             Alignment;\r
+\r
+  if ((((UINTN)DestinationBuffer & 0x7) == 0) && (((UINTN)SourceBuffer & 0x7) == 0) && (Length >= 8)) {\r
+    if (SourceBuffer > DestinationBuffer) {\r
+      Destination64 = (UINT64*)DestinationBuffer;\r
+      Source64 = (CONST UINT64*)SourceBuffer;\r
+      while (Length >= 8) {\r
+        *(Destination64++) = *(Source64++);\r
+        Length -= 8;\r
+      }\r
+\r
+      // Finish if there are still some bytes to copy\r
+      Destination8 = (UINT8*)Destination64;\r
+      Source8 = (CONST UINT8*)Source64;\r
+      while (Length-- != 0) {\r
+        *(Destination8++) = *(Source8++);\r
+      }\r
+    } else if (SourceBuffer < DestinationBuffer) {\r
+      Destination64 = (UINT64*)((UINTN)DestinationBuffer + Length);\r
+      Source64 = (CONST UINT64*)((UINTN)SourceBuffer + Length);\r
+\r
+      // Destination64 and Source64 were aligned on a 64-bit boundary\r
+      // but if length is not a multiple of 8 bytes then they won't be\r
+      // anymore.\r
+\r
+      Alignment = Length & 0x7;\r
+      if (Alignment != 0) {\r
+        Destination8 = (UINT8*)Destination64;\r
+        Source8 = (CONST UINT8*)Source64;\r
+\r
+        while (Alignment-- != 0) {\r
+          *(--Destination8) = *(--Source8);\r
+          --Length;\r
+        }\r
+        Destination64 = (UINT64*)Destination8;\r
+        Source64 = (CONST UINT64*)Source8;\r
+      }\r
+\r
+      while (Length > 0) {\r
+        *(--Destination64) = *(--Source64);\r
+        Length -= 8;\r
+      }\r
+    }\r
+  } else if ((((UINTN)DestinationBuffer & 0x3) == 0) && (((UINTN)SourceBuffer & 0x3) == 0) && (Length >= 4)) {\r
+    if (SourceBuffer > DestinationBuffer) {\r
+      Destination32 = (UINT32*)DestinationBuffer;\r
+      Source32 = (CONST UINT32*)SourceBuffer;\r
+      while (Length >= 4) {\r
+        *(Destination32++) = *(Source32++);\r
+        Length -= 4;\r
+      }\r
+\r
+      // Finish if there are still some bytes to copy\r
+      Destination8 = (UINT8*)Destination32;\r
+      Source8 = (CONST UINT8*)Source32;\r
+      while (Length-- != 0) {\r
+        *(Destination8++) = *(Source8++);\r
+      }\r
+    } else if (SourceBuffer < DestinationBuffer) {\r
+      Destination32 = (UINT32*)((UINTN)DestinationBuffer + Length);\r
+      Source32 = (CONST UINT32*)((UINTN)SourceBuffer + Length);\r
+\r
+      // Destination32 and Source32 were aligned on a 32-bit boundary\r
+      // but if length is not a multiple of 4 bytes then they won't be\r
+      // anymore.\r
+\r
+      Alignment = Length & 0x3;\r
+      if (Alignment != 0) {\r
+        Destination8 = (UINT8*)Destination32;\r
+        Source8 = (CONST UINT8*)Source32;\r
+\r
+        while (Alignment-- != 0) {\r
+          *(--Destination8) = *(--Source8);\r
+          --Length;\r
+        }\r
+        Destination32 = (UINT32*)Destination8;\r
+        Source32 = (CONST UINT32*)Source8;\r
+      }\r
 \r
-  if (SourceBuffer > DestinationBuffer) {\r
-    Destination8 = (UINT8*)DestinationBuffer;\r
-    Source8 = (CONST UINT8*)SourceBuffer;\r
-    while (Length-- != 0) {\r
-      *(Destination8++) = *(Source8++);\r
+      while (Length > 0) {\r
+        *(--Destination32) = *(--Source32);\r
+        Length -= 4;\r
+      }\r
     }\r
-  } else if (SourceBuffer < DestinationBuffer) {\r
-    Destination8 = (UINT8*)DestinationBuffer + Length;\r
-    Source8 = (CONST UINT8*)SourceBuffer + Length;\r
-    while (Length-- != 0) {\r
-      *(--Destination8) = *(--Source8);\r
+  } else {\r
+    if (SourceBuffer > DestinationBuffer) {\r
+      Destination8 = (UINT8*)DestinationBuffer;\r
+      Source8 = (CONST UINT8*)SourceBuffer;\r
+      while (Length-- != 0) {\r
+        *(Destination8++) = *(Source8++);\r
+      }\r
+    } else if (SourceBuffer < DestinationBuffer) {\r
+      Destination8 = (UINT8*)DestinationBuffer + Length;\r
+      Source8 = (CONST UINT8*)SourceBuffer + Length;\r
+      while (Length-- != 0) {\r
+        *(--Destination8) = *(--Source8);\r
+      }\r
     }\r
   }\r
   return DestinationBuffer;\r
index 5e74085c56f01860ff9b45457355e14a3f52b5cc..b6fb811c388a0e1115ae2ef080d24b15f9199712 100644 (file)
@@ -5,6 +5,9 @@
   is desired.\r
 \r
   Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2012 - 2013, ARM Ltd. All rights reserved.<BR>\r
+  Copyright (c) 2016, Linaro Ltd. All rights reserved.<BR>\r
+\r
   This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
@@ -43,11 +46,42 @@ InternalMemSetMem (
   // volatile to prevent the optimizer from replacing this function with\r
   // the intrinsic memset()\r
   //\r
-  volatile UINT8                    *Pointer;\r
+  volatile UINT8                    *Pointer8;\r
+  volatile UINT32                   *Pointer32;\r
+  volatile UINT64                   *Pointer64;\r
+  UINT32                            Value32;\r
+  UINT64                            Value64;\r
+\r
+  if ((((UINTN)Buffer & 0x7) == 0) && (Length >= 8)) {\r
+    // Generate the 64bit value\r
+    Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;\r
+    Value64 = LShiftU64 (Value32, 32) | Value32;\r
+\r
+    Pointer64 = (UINT64*)Buffer;\r
+    while (Length >= 8) {\r
+      *(Pointer64++) = Value64;\r
+      Length -= 8;\r
+    }\r
 \r
-  Pointer = (UINT8*)Buffer;\r
+    // Finish with bytes if needed\r
+    Pointer8 = (UINT8*)Pointer64;\r
+  } else if ((((UINTN)Buffer & 0x3) == 0) && (Length >= 4)) {\r
+    // Generate the 32bit value\r
+    Value32 = (Value << 24) | (Value << 16) | (Value << 8) | Value;\r
+\r
+    Pointer32 = (UINT32*)Buffer;\r
+    while (Length >= 4) {\r
+      *(Pointer32++) = Value32;\r
+      Length -= 4;\r
+    }\r
+\r
+    // Finish with bytes if needed\r
+    Pointer8 = (UINT8*)Pointer32;\r
+  } else {\r
+    Pointer8 = (UINT8*)Buffer;\r
+  }\r
   while (Length-- > 0) {\r
-    *(Pointer++) = Value;\r
+    *(Pointer8++) = Value;\r
   }\r
   return Buffer;\r
 }\r