]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/BitField.c
MdePkg/BaseLib: Use PcdSpeculationBarrierType
[mirror_edk2.git] / MdePkg / Library / BaseLib / BitField.c
index 167ee5b692c31784f8e154db94d2767e0846fc1d..0f48a036ba32e7310ea109c351e1fa7dbe5b9084 100644 (file)
@@ -1,14 +1,8 @@
 /** @file\r
   Bit field functions of BaseLib.\r
 \r
-  Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>\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
-  http://opensource.org/licenses/bsd-license.php.\r
-\r
-  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -69,13 +63,13 @@ InternalBaseLibBitFieldOrUint (
   )\r
 {\r
   //\r
-  // Higher bits in OrData those are not used must be zero. \r
+  // Higher bits in OrData those are not used must be zero.\r
   //\r
-  // EndBit \96 StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,\r
-  // So the logic is updated to right shift (EndBit \96 StartBit) bits and compare the last bit directly.\r
+  // EndBit - StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,\r
+  // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.\r
   //\r
   ASSERT ((OrData >> (EndBit - StartBit)) == ((OrData >> (EndBit - StartBit)) & 1));\r
-  \r
+\r
   //\r
   // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]\r
   // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.\r
@@ -111,10 +105,10 @@ InternalBaseLibBitFieldAndUint (
   )\r
 {\r
   //\r
-  // Higher bits in AndData those are not used must be zero. \r
+  // Higher bits in AndData those are not used must be zero.\r
   //\r
-  // EndBit \96 StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,\r
-  // So the logic is updated to right shift (EndBit \96 StartBit) bits and compare the last bit directly.\r
+  // EndBit - StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,\r
+  // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.\r
   //\r
   ASSERT ((AndData >> (EndBit - StartBit)) == ((AndData >> (EndBit - StartBit)) & 1));\r
 \r
@@ -275,7 +269,7 @@ BitFieldAnd8 (
   bitwise OR, and returns the result.\r
 \r
   Performs a bitwise AND between the bit field specified by StartBit and EndBit\r
-  in Operand and the value specified by AndData, followed by a bitwise \r
+  in Operand and the value specified by AndData, followed by a bitwise\r
   OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 8-bit value is returned.\r
 \r
@@ -467,7 +461,7 @@ BitFieldAnd16 (
   bitwise OR, and returns the result.\r
 \r
   Performs a bitwise AND between the bit field specified by StartBit and EndBit\r
-  in Operand and the value specified by AndData, followed by a bitwise \r
+  in Operand and the value specified by AndData, followed by a bitwise\r
   OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 16-bit value is returned.\r
 \r
@@ -659,7 +653,7 @@ BitFieldAnd32 (
   bitwise OR, and returns the result.\r
 \r
   Performs a bitwise AND between the bit field specified by StartBit and EndBit\r
-  in Operand and the value specified by AndData, followed by a bitwise \r
+  in Operand and the value specified by AndData, followed by a bitwise\r
   OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 32-bit value is returned.\r
 \r
@@ -809,10 +803,10 @@ BitFieldOr64 (
   ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
   //\r
-  // Higher bits in OrData those are not used must be zero. \r
+  // Higher bits in OrData those are not used must be zero.\r
   //\r
-  // EndBit \96 StartBit + 1 might be 64 while the result right shifting 64 on RShiftU64() API is invalid,\r
-  // So the logic is updated to right shift (EndBit \96 StartBit) bits and compare the last bit directly.\r
+  // EndBit - StartBit + 1 might be 64 while the result right shifting 64 on RShiftU64() API is invalid,\r
+  // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.\r
   //\r
   ASSERT (RShiftU64 (OrData, EndBit - StartBit) == (RShiftU64 (OrData, EndBit - StartBit) & 1));\r
 \r
@@ -857,14 +851,14 @@ BitFieldAnd64 (
 {\r
   UINT64  Value1;\r
   UINT64  Value2;\r
-  \r
+\r
   ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
   //\r
-  // Higher bits in AndData those are not used must be zero. \r
+  // Higher bits in AndData those are not used must be zero.\r
   //\r
-  // EndBit \96 StartBit + 1 might be 64 while the right shifting 64 on RShiftU64() API is invalid,\r
-  // So the logic is updated to right shift (EndBit \96 StartBit) bits and compare the last bit directly.\r
+  // EndBit - StartBit + 1 might be 64 while the right shifting 64 on RShiftU64() API is invalid,\r
+  // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.\r
   //\r
   ASSERT (RShiftU64 (AndData, EndBit - StartBit) == (RShiftU64 (AndData, EndBit - StartBit) & 1));\r
 \r
@@ -879,7 +873,7 @@ BitFieldAnd64 (
   bitwise OR, and returns the result.\r
 \r
   Performs a bitwise AND between the bit field specified by StartBit and EndBit\r
-  in Operand and the value specified by AndData, followed by a bitwise \r
+  in Operand and the value specified by AndData, followed by a bitwise\r
   OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 64-bit value is returned.\r
 \r
@@ -920,3 +914,89 @@ BitFieldAndThenOr64 (
            OrData\r
            );\r
 }\r
+\r
+/**\r
+  Reads a bit field from a 32-bit value, counts and returns\r
+  the number of set bits.\r
+\r
+  Counts the number of set bits in the  bit field specified by\r
+  StartBit and EndBit in Operand. The count is returned.\r
+\r
+  If StartBit is greater than 31, then ASSERT().\r
+  If EndBit is greater than 31, then ASSERT().\r
+  If EndBit is less than StartBit, then ASSERT().\r
+\r
+  @param  Operand   Operand on which to perform the bitfield operation.\r
+  @param  StartBit  The ordinal of the least significant bit in the bit field.\r
+                    Range 0..31.\r
+  @param  EndBit    The ordinal of the most significant bit in the bit field.\r
+                    Range 0..31.\r
+\r
+  @return The number of bits set between StartBit and EndBit.\r
+\r
+**/\r
+UINT8\r
+EFIAPI\r
+BitFieldCountOnes32 (\r
+  IN       UINT32                   Operand,\r
+  IN       UINTN                    StartBit,\r
+  IN       UINTN                    EndBit\r
+  )\r
+{\r
+  UINT32 Count;\r
+\r
+  ASSERT (EndBit < 32);\r
+  ASSERT (StartBit <= EndBit);\r
+\r
+  Count = BitFieldRead32 (Operand, StartBit, EndBit);\r
+  Count -= ((Count >> 1) & 0x55555555);\r
+  Count = (Count & 0x33333333) + ((Count >> 2) & 0x33333333);\r
+  Count += Count >> 4;\r
+  Count &= 0x0F0F0F0F;\r
+  Count += Count >> 8;\r
+  Count += Count >> 16;\r
+\r
+  return (UINT8) Count & 0x3F;\r
+}\r
+\r
+/**\r
+   Reads a bit field from a 64-bit value, counts and returns\r
+   the number of set bits.\r
+\r
+   Counts the number of set bits in the  bit field specified by\r
+   StartBit and EndBit in Operand. The count is returned.\r
+\r
+   If StartBit is greater than 63, then ASSERT().\r
+   If EndBit is greater than 63, then ASSERT().\r
+   If EndBit is less than StartBit, then ASSERT().\r
+\r
+   @param  Operand   Operand on which to perform the bitfield operation.\r
+   @param  StartBit  The ordinal of the least significant bit in the bit field.\r
+   Range 0..63.\r
+   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
+   Range 0..63.\r
+\r
+   @return The number of bits set between StartBit and EndBit.\r
+\r
+**/\r
+UINT8\r
+EFIAPI\r
+BitFieldCountOnes64 (\r
+  IN       UINT64                   Operand,\r
+  IN       UINTN                    StartBit,\r
+  IN       UINTN                    EndBit\r
+  )\r
+{\r
+  UINT64 BitField;\r
+  UINT8 Count;\r
+\r
+  ASSERT (EndBit < 64);\r
+  ASSERT (StartBit <= EndBit);\r
+\r
+  BitField = BitFieldRead64 (Operand, StartBit, EndBit);\r
+  Count = BitFieldCountOnes32 ((UINT32) BitField, 0, 31);\r
+  Count += BitFieldCountOnes32 ((UINT32) RShiftU64(BitField, 32), 0, 31);\r
+\r
+  return Count;\r
+}\r
+\r