public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH v4] MdePkg/BaseLib: Add bit field population calculating methods
@ 2018-07-06 13:40 Tomas Pilar (tpilar)
  2018-07-09  2:20 ` Gao, Liming
  0 siblings, 1 reply; 3+ messages in thread
From: Tomas Pilar (tpilar) @ 2018-07-06 13:40 UTC (permalink / raw)
  To: edk2-devel; +Cc: Michael D Kinney, Liming Gao

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>
---
 MdePkg/Include/Library/BaseLib.h  | 56 +++++++++++++++++++++++++
 MdePkg/Library/BaseLib/BitField.c | 86 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 142 insertions(+)

diff --git a/MdePkg/Include/Library/BaseLib.h b/MdePkg/Include/Library/BaseLib.h
index 1db3a04..123ae19 100644
--- a/MdePkg/Include/Library/BaseLib.h
+++ b/MdePkg/Include/Library/BaseLib.h
@@ -4609,6 +4609,62 @@ BitFieldAndThenOr64 (
   IN      UINT64                    OrData
   );
 
+/**
+  Reads a bit field from a 32-bit value, counts and returns
+  the number of set bits.
+
+  Counts the number of set bits in the  bit field specified by
+  StartBit and EndBit in Operand. The count is returned.
+
+  If StartBit is greater than 31, then ASSERT().
+  If EndBit is greater than 31, then ASSERT().
+  If EndBit is less than StartBit, then ASSERT().
+
+  @param  Operand   Operand on which to perform the bitfield operation.
+  @param  StartBit  The ordinal of the least significant bit in the bit field.
+                    Range 0..31.
+  @param  EndBit    The ordinal of the most significant bit in the bit field.
+                    Range 0..31.
+
+  @return The number of bits set between StartBit and EndBit.
+
+**/
+UINT8
+EFIAPI
+BitFieldCountOnes32 (
+  IN       UINT32                   Operand,
+  IN       UINTN                    StartBit,
+  IN       UINTN                    EndBit
+  );
+
+/**
+   Reads a bit field from a 64-bit value, counts and returns
+   the number of set bits.
+
+   Counts the number of set bits in the  bit field specified by
+   StartBit and EndBit in Operand. The count is returned.
+
+   If StartBit is greater than 63, then ASSERT().
+   If EndBit is greater than 63, then ASSERT().
+   If EndBit is less than StartBit, then ASSERT().
+
+   @param  Operand   Operand on which to perform the bitfield operation.
+   @param  StartBit  The ordinal of the least significant bit in the bit field.
+   Range 0..63.
+   @param  EndBit    The ordinal of the most significant bit in the bit field.
+   Range 0..63.
+
+   @return The number of bits set between StartBit and EndBit.
+
+**/
+UINT8
+EFIAPI
+BitFieldCountOnes64 (
+  IN       UINT64                   Operand,
+  IN       UINTN                    StartBit,
+  IN       UINTN                    EndBit
+  );
+
 //
 // Base Library Checksum Functions
 //
diff --git a/MdePkg/Library/BaseLib/BitField.c b/MdePkg/Library/BaseLib/BitField.c
index d2d3150..645a662 100644
--- a/MdePkg/Library/BaseLib/BitField.c
+++ b/MdePkg/Library/BaseLib/BitField.c
@@ -920,3 +920,89 @@ BitFieldAndThenOr64 (
            OrData
            );
 }
+
+/**
+  Reads a bit field from a 32-bit value, counts and returns
+  the number of set bits.
+
+  Counts the number of set bits in the  bit field specified by
+  StartBit and EndBit in Operand. The count is returned.
+
+  If StartBit is greater than 31, then ASSERT().
+  If EndBit is greater than 31, then ASSERT().
+  If EndBit is less than StartBit, then ASSERT().
+
+  @param  Operand   Operand on which to perform the bitfield operation.
+  @param  StartBit  The ordinal of the least significant bit in the bit field.
+                    Range 0..31.
+  @param  EndBit    The ordinal of the most significant bit in the bit field.
+                    Range 0..31.
+
+  @return The number of bits set between StartBit and EndBit.
+
+**/
+UINT8
+EFIAPI
+BitFieldCountOnes32 (
+  IN       UINT32                   Operand,
+  IN       UINTN                    StartBit,
+  IN       UINTN                    EndBit
+  )
+{
+  UINT32 Count;
+
+  ASSERT (EndBit < 32);
+  ASSERT (StartBit <= EndBit);
+
+  Count = BitFieldRead32 (Operand, StartBit, EndBit);
+  Count -= ((Count >> 1) & 0x55555555);
+  Count = (Count & 0x33333333) + ((Count >> 2) & 0x33333333);
+  Count += Count >> 4;
+  Count &= 0x0F0F0F0F;
+  Count += Count >> 8;
+  Count += Count >> 16;
+
+  return (UINT8) Count & 0x3F;
+}
+
+/**
+   Reads a bit field from a 64-bit value, counts and returns
+   the number of set bits.
+
+   Counts the number of set bits in the  bit field specified by
+   StartBit and EndBit in Operand. The count is returned.
+
+   If StartBit is greater than 63, then ASSERT().
+   If EndBit is greater than 63, then ASSERT().
+   If EndBit is less than StartBit, then ASSERT().
+
+   @param  Operand   Operand on which to perform the bitfield operation.
+   @param  StartBit  The ordinal of the least significant bit in the bit field.
+   Range 0..63.
+   @param  EndBit    The ordinal of the most significant bit in the bit field.
+   Range 0..63.
+
+   @return The number of bits set between StartBit and EndBit.
+
+**/
+UINT8
+EFIAPI
+BitFieldCountOnes64 (
+  IN       UINT64                   Operand,
+  IN       UINTN                    StartBit,
+  IN       UINTN                    EndBit
+  )
+{
+  UINT64 BitField;
+  UINT8 Count;
+
+  ASSERT (EndBit < 64);
+  ASSERT (StartBit <= EndBit);
+
+  BitField = BitFieldRead64 (Operand, StartBit, EndBit);
+  Count = BitFieldCountOnes32 ((UINT32) BitField, 0, 31);
+  Count += BitFieldCountOnes32 ((UINT32) RShiftU64(BitField, 32), 0, 31);
+
+  return Count;
+}
+
-- 
2.9.5



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] MdePkg/BaseLib: Add bit field population calculating methods
  2018-07-06 13:40 [PATCH v4] MdePkg/BaseLib: Add bit field population calculating methods Tomas Pilar (tpilar)
@ 2018-07-09  2:20 ` Gao, Liming
  2018-07-09  2:31   ` Gao, Liming
  0 siblings, 1 reply; 3+ messages in thread
From: Gao, Liming @ 2018-07-09  2:20 UTC (permalink / raw)
  To: Tomas Pilar (tpilar), edk2-devel@lists.01.org; +Cc: Kinney, Michael D

Reviewed-by: Liming Gao <liming.gao@intel.com>

>-----Original Message-----
>From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
>Tomas Pilar (tpilar)
>Sent: Friday, July 06, 2018 9:41 PM
>To: edk2-devel@lists.01.org
>Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
><liming.gao@intel.com>
>Subject: [edk2] [PATCH v4] MdePkg/BaseLib: Add bit field population
>calculating methods
>
>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>
>---
> MdePkg/Include/Library/BaseLib.h  | 56 +++++++++++++++++++++++++
> MdePkg/Library/BaseLib/BitField.c | 86
>+++++++++++++++++++++++++++++++++++++++
> 2 files changed, 142 insertions(+)
>
>diff --git a/MdePkg/Include/Library/BaseLib.h
>b/MdePkg/Include/Library/BaseLib.h
>index 1db3a04..123ae19 100644
>--- a/MdePkg/Include/Library/BaseLib.h
>+++ b/MdePkg/Include/Library/BaseLib.h
>@@ -4609,6 +4609,62 @@ BitFieldAndThenOr64 (
>   IN      UINT64                    OrData
>   );
>
>+/**
>+  Reads a bit field from a 32-bit value, counts and returns
>+  the number of set bits.
>+
>+  Counts the number of set bits in the  bit field specified by
>+  StartBit and EndBit in Operand. The count is returned.
>+
>+  If StartBit is greater than 31, then ASSERT().
>+  If EndBit is greater than 31, then ASSERT().
>+  If EndBit is less than StartBit, then ASSERT().
>+
>+  @param  Operand   Operand on which to perform the bitfield operation.
>+  @param  StartBit  The ordinal of the least significant bit in the bit field.
>+                    Range 0..31.
>+  @param  EndBit    The ordinal of the most significant bit in the bit field.
>+                    Range 0..31.
>+
>+  @return The number of bits set between StartBit and EndBit.
>+
>+**/
>+UINT8
>+EFIAPI
>+BitFieldCountOnes32 (
>+  IN       UINT32                   Operand,
>+  IN       UINTN                    StartBit,
>+  IN       UINTN                    EndBit
>+  );
>+
>+/**
>+   Reads a bit field from a 64-bit value, counts and returns
>+   the number of set bits.
>+
>+   Counts the number of set bits in the  bit field specified by
>+   StartBit and EndBit in Operand. The count is returned.
>+
>+   If StartBit is greater than 63, then ASSERT().
>+   If EndBit is greater than 63, then ASSERT().
>+   If EndBit is less than StartBit, then ASSERT().
>+
>+   @param  Operand   Operand on which to perform the bitfield operation.
>+   @param  StartBit  The ordinal of the least significant bit in the bit field.
>+   Range 0..63.
>+   @param  EndBit    The ordinal of the most significant bit in the bit field.
>+   Range 0..63.
>+
>+   @return The number of bits set between StartBit and EndBit.
>+
>+**/
>+UINT8
>+EFIAPI
>+BitFieldCountOnes64 (
>+  IN       UINT64                   Operand,
>+  IN       UINTN                    StartBit,
>+  IN       UINTN                    EndBit
>+  );
>+
> //
> // Base Library Checksum Functions
> //
>diff --git a/MdePkg/Library/BaseLib/BitField.c
>b/MdePkg/Library/BaseLib/BitField.c
>index d2d3150..645a662 100644
>--- a/MdePkg/Library/BaseLib/BitField.c
>+++ b/MdePkg/Library/BaseLib/BitField.c
>@@ -920,3 +920,89 @@ BitFieldAndThenOr64 (
>            OrData
>            );
> }
>+
>+/**
>+  Reads a bit field from a 32-bit value, counts and returns
>+  the number of set bits.
>+
>+  Counts the number of set bits in the  bit field specified by
>+  StartBit and EndBit in Operand. The count is returned.
>+
>+  If StartBit is greater than 31, then ASSERT().
>+  If EndBit is greater than 31, then ASSERT().
>+  If EndBit is less than StartBit, then ASSERT().
>+
>+  @param  Operand   Operand on which to perform the bitfield operation.
>+  @param  StartBit  The ordinal of the least significant bit in the bit field.
>+                    Range 0..31.
>+  @param  EndBit    The ordinal of the most significant bit in the bit field.
>+                    Range 0..31.
>+
>+  @return The number of bits set between StartBit and EndBit.
>+
>+**/
>+UINT8
>+EFIAPI
>+BitFieldCountOnes32 (
>+  IN       UINT32                   Operand,
>+  IN       UINTN                    StartBit,
>+  IN       UINTN                    EndBit
>+  )
>+{
>+  UINT32 Count;
>+
>+  ASSERT (EndBit < 32);
>+  ASSERT (StartBit <= EndBit);
>+
>+  Count = BitFieldRead32 (Operand, StartBit, EndBit);
>+  Count -= ((Count >> 1) & 0x55555555);
>+  Count = (Count & 0x33333333) + ((Count >> 2) & 0x33333333);
>+  Count += Count >> 4;
>+  Count &= 0x0F0F0F0F;
>+  Count += Count >> 8;
>+  Count += Count >> 16;
>+
>+  return (UINT8) Count & 0x3F;
>+}
>+
>+/**
>+   Reads a bit field from a 64-bit value, counts and returns
>+   the number of set bits.
>+
>+   Counts the number of set bits in the  bit field specified by
>+   StartBit and EndBit in Operand. The count is returned.
>+
>+   If StartBit is greater than 63, then ASSERT().
>+   If EndBit is greater than 63, then ASSERT().
>+   If EndBit is less than StartBit, then ASSERT().
>+
>+   @param  Operand   Operand on which to perform the bitfield operation.
>+   @param  StartBit  The ordinal of the least significant bit in the bit field.
>+   Range 0..63.
>+   @param  EndBit    The ordinal of the most significant bit in the bit field.
>+   Range 0..63.
>+
>+   @return The number of bits set between StartBit and EndBit.
>+
>+**/
>+UINT8
>+EFIAPI
>+BitFieldCountOnes64 (
>+  IN       UINT64                   Operand,
>+  IN       UINTN                    StartBit,
>+  IN       UINTN                    EndBit
>+  )
>+{
>+  UINT64 BitField;
>+  UINT8 Count;
>+
>+  ASSERT (EndBit < 64);
>+  ASSERT (StartBit <= EndBit);
>+
>+  BitField = BitFieldRead64 (Operand, StartBit, EndBit);
>+  Count = BitFieldCountOnes32 ((UINT32) BitField, 0, 31);
>+  Count += BitFieldCountOnes32 ((UINT32) RShiftU64(BitField, 32), 0, 31);
>+
>+  return Count;
>+}
>+
>--
>2.9.5
>
>_______________________________________________
>edk2-devel mailing list
>edk2-devel@lists.01.org
>https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v4] MdePkg/BaseLib: Add bit field population calculating methods
  2018-07-09  2:20 ` Gao, Liming
@ 2018-07-09  2:31   ` Gao, Liming
  0 siblings, 0 replies; 3+ messages in thread
From: Gao, Liming @ 2018-07-09  2:31 UTC (permalink / raw)
  To: Gao, Liming, Tomas Pilar (tpilar), edk2-devel@lists.01.org
  Cc: Kinney, Michael D

Push at d7634dc0c5a83360b3b6c155df29c078ad9c77ce

>-----Original Message-----
>From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of Gao,
>Liming
>Sent: Monday, July 09, 2018 10:21 AM
>To: Tomas Pilar (tpilar) <tpilar@solarflare.com>; edk2-devel@lists.01.org
>Cc: Kinney, Michael D <michael.d.kinney@intel.com>
>Subject: Re: [edk2] [PATCH v4] MdePkg/BaseLib: Add bit field population
>calculating methods
>
>Reviewed-by: Liming Gao <liming.gao@intel.com>
>
>>-----Original Message-----
>>From: edk2-devel [mailto:edk2-devel-bounces@lists.01.org] On Behalf Of
>>Tomas Pilar (tpilar)
>>Sent: Friday, July 06, 2018 9:41 PM
>>To: edk2-devel@lists.01.org
>>Cc: Kinney, Michael D <michael.d.kinney@intel.com>; Gao, Liming
>><liming.gao@intel.com>
>>Subject: [edk2] [PATCH v4] MdePkg/BaseLib: Add bit field population
>>calculating methods
>>
>>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>
>>---
>> MdePkg/Include/Library/BaseLib.h  | 56 +++++++++++++++++++++++++
>> MdePkg/Library/BaseLib/BitField.c | 86
>>+++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 142 insertions(+)
>>
>>diff --git a/MdePkg/Include/Library/BaseLib.h
>>b/MdePkg/Include/Library/BaseLib.h
>>index 1db3a04..123ae19 100644
>>--- a/MdePkg/Include/Library/BaseLib.h
>>+++ b/MdePkg/Include/Library/BaseLib.h
>>@@ -4609,6 +4609,62 @@ BitFieldAndThenOr64 (
>>   IN      UINT64                    OrData
>>   );
>>
>>+/**
>>+  Reads a bit field from a 32-bit value, counts and returns
>>+  the number of set bits.
>>+
>>+  Counts the number of set bits in the  bit field specified by
>>+  StartBit and EndBit in Operand. The count is returned.
>>+
>>+  If StartBit is greater than 31, then ASSERT().
>>+  If EndBit is greater than 31, then ASSERT().
>>+  If EndBit is less than StartBit, then ASSERT().
>>+
>>+  @param  Operand   Operand on which to perform the bitfield operation.
>>+  @param  StartBit  The ordinal of the least significant bit in the bit field.
>>+                    Range 0..31.
>>+  @param  EndBit    The ordinal of the most significant bit in the bit field.
>>+                    Range 0..31.
>>+
>>+  @return The number of bits set between StartBit and EndBit.
>>+
>>+**/
>>+UINT8
>>+EFIAPI
>>+BitFieldCountOnes32 (
>>+  IN       UINT32                   Operand,
>>+  IN       UINTN                    StartBit,
>>+  IN       UINTN                    EndBit
>>+  );
>>+
>>+/**
>>+   Reads a bit field from a 64-bit value, counts and returns
>>+   the number of set bits.
>>+
>>+   Counts the number of set bits in the  bit field specified by
>>+   StartBit and EndBit in Operand. The count is returned.
>>+
>>+   If StartBit is greater than 63, then ASSERT().
>>+   If EndBit is greater than 63, then ASSERT().
>>+   If EndBit is less than StartBit, then ASSERT().
>>+
>>+   @param  Operand   Operand on which to perform the bitfield operation.
>>+   @param  StartBit  The ordinal of the least significant bit in the bit field.
>>+   Range 0..63.
>>+   @param  EndBit    The ordinal of the most significant bit in the bit field.
>>+   Range 0..63.
>>+
>>+   @return The number of bits set between StartBit and EndBit.
>>+
>>+**/
>>+UINT8
>>+EFIAPI
>>+BitFieldCountOnes64 (
>>+  IN       UINT64                   Operand,
>>+  IN       UINTN                    StartBit,
>>+  IN       UINTN                    EndBit
>>+  );
>>+
>> //
>> // Base Library Checksum Functions
>> //
>>diff --git a/MdePkg/Library/BaseLib/BitField.c
>>b/MdePkg/Library/BaseLib/BitField.c
>>index d2d3150..645a662 100644
>>--- a/MdePkg/Library/BaseLib/BitField.c
>>+++ b/MdePkg/Library/BaseLib/BitField.c
>>@@ -920,3 +920,89 @@ BitFieldAndThenOr64 (
>>            OrData
>>            );
>> }
>>+
>>+/**
>>+  Reads a bit field from a 32-bit value, counts and returns
>>+  the number of set bits.
>>+
>>+  Counts the number of set bits in the  bit field specified by
>>+  StartBit and EndBit in Operand. The count is returned.
>>+
>>+  If StartBit is greater than 31, then ASSERT().
>>+  If EndBit is greater than 31, then ASSERT().
>>+  If EndBit is less than StartBit, then ASSERT().
>>+
>>+  @param  Operand   Operand on which to perform the bitfield operation.
>>+  @param  StartBit  The ordinal of the least significant bit in the bit field.
>>+                    Range 0..31.
>>+  @param  EndBit    The ordinal of the most significant bit in the bit field.
>>+                    Range 0..31.
>>+
>>+  @return The number of bits set between StartBit and EndBit.
>>+
>>+**/
>>+UINT8
>>+EFIAPI
>>+BitFieldCountOnes32 (
>>+  IN       UINT32                   Operand,
>>+  IN       UINTN                    StartBit,
>>+  IN       UINTN                    EndBit
>>+  )
>>+{
>>+  UINT32 Count;
>>+
>>+  ASSERT (EndBit < 32);
>>+  ASSERT (StartBit <= EndBit);
>>+
>>+  Count = BitFieldRead32 (Operand, StartBit, EndBit);
>>+  Count -= ((Count >> 1) & 0x55555555);
>>+  Count = (Count & 0x33333333) + ((Count >> 2) & 0x33333333);
>>+  Count += Count >> 4;
>>+  Count &= 0x0F0F0F0F;
>>+  Count += Count >> 8;
>>+  Count += Count >> 16;
>>+
>>+  return (UINT8) Count & 0x3F;
>>+}
>>+
>>+/**
>>+   Reads a bit field from a 64-bit value, counts and returns
>>+   the number of set bits.
>>+
>>+   Counts the number of set bits in the  bit field specified by
>>+   StartBit and EndBit in Operand. The count is returned.
>>+
>>+   If StartBit is greater than 63, then ASSERT().
>>+   If EndBit is greater than 63, then ASSERT().
>>+   If EndBit is less than StartBit, then ASSERT().
>>+
>>+   @param  Operand   Operand on which to perform the bitfield operation.
>>+   @param  StartBit  The ordinal of the least significant bit in the bit field.
>>+   Range 0..63.
>>+   @param  EndBit    The ordinal of the most significant bit in the bit field.
>>+   Range 0..63.
>>+
>>+   @return The number of bits set between StartBit and EndBit.
>>+
>>+**/
>>+UINT8
>>+EFIAPI
>>+BitFieldCountOnes64 (
>>+  IN       UINT64                   Operand,
>>+  IN       UINTN                    StartBit,
>>+  IN       UINTN                    EndBit
>>+  )
>>+{
>>+  UINT64 BitField;
>>+  UINT8 Count;
>>+
>>+  ASSERT (EndBit < 64);
>>+  ASSERT (StartBit <= EndBit);
>>+
>>+  BitField = BitFieldRead64 (Operand, StartBit, EndBit);
>>+  Count = BitFieldCountOnes32 ((UINT32) BitField, 0, 31);
>>+  Count += BitFieldCountOnes32 ((UINT32) RShiftU64(BitField, 32), 0, 31);
>>+
>>+  return Count;
>>+}
>>+
>>--
>>2.9.5
>>
>>_______________________________________________
>>edk2-devel mailing list
>>edk2-devel@lists.01.org
>>https://lists.01.org/mailman/listinfo/edk2-devel
>_______________________________________________
>edk2-devel mailing list
>edk2-devel@lists.01.org
>https://lists.01.org/mailman/listinfo/edk2-devel


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-07-09  2:31 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-07-06 13:40 [PATCH v4] MdePkg/BaseLib: Add bit field population calculating methods Tomas Pilar (tpilar)
2018-07-09  2:20 ` Gao, Liming
2018-07-09  2:31   ` Gao, Liming

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox