]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseLib: Add bit field population calculating methods
authorTomas Pilar (tpilar) <tpilar@solarflare.com>
Fri, 6 Jul 2018 13:40:43 +0000 (21:40 +0800)
committerLiming Gao <liming.gao@intel.com>
Mon, 9 Jul 2018 02:29:45 +0000 (10:29 +0800)
Hopefully this should tidy the conversion warnings.

----

Add 32-bit and 64-bit functions that count number of set bits in a bitfield
using a divide-and-count method.

Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Tomas Pilar <tpilar@solarflare.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
Reviewed-by: Michael Kinney <michael.d.kinney@intel.com>
MdePkg/Include/Library/BaseLib.h
MdePkg/Library/BaseLib/BitField.c

index 1db3a0475d9366f63f749b65f88913857a73297b..123ae19dc263dc98e73174e877b37473b7d28a2a 100644 (file)
@@ -4609,6 +4609,62 @@ BitFieldAndThenOr64 (
   IN      UINT64                    OrData\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
+/**\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
 //\r
 // Base Library Checksum Functions\r
 //\r
index d2d3150f2a08675387ac195c86c4e0ba4123814a..645a662e810b01ed0491f8d8bd22c5660d9596e9 100644 (file)
@@ -920,3 +920,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