]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/BitField.c
MdePkg: Apply uncrustify changes
[mirror_edk2.git] / MdePkg / Library / BaseLib / BitField.c
index 0b517aa974f7f505cd7a59c472df3e03a7b32305..862af53e9c3f5e26115bf912b0ea394f98148dfa 100644 (file)
@@ -1,21 +1,15 @@
 /** @file\r
   Bit field functions of BaseLib.\r
 \r
-  Copyright (c) 2006, Intel Corporation<BR>\r
-  All rights reserved. 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
-\r
-  Module Name:  BitField.c\r
+  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
+#include "BaseLibInternals.h"\r
+\r
 /**\r
-  Worker function that returns a bit field from Operand\r
+  Worker function that returns a bit field from Operand.\r
 \r
   Returns the bitfield specified by the StartBit and the EndBit from Operand.\r
 \r
   @return The bit field read.\r
 \r
 **/\r
-unsigned int\r
-BitFieldReadUint (\r
-  IN      unsigned int              Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit\r
+UINTN\r
+EFIAPI\r
+InternalBaseLibBitFieldReadUint (\r
+  IN      UINTN  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit\r
   )\r
 {\r
   //\r
-  // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]\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
   //\r
-  return (Operand & ~((unsigned int)-2 << EndBit)) >> StartBit;\r
+  return (Operand & ~((UINTN)-2 << EndBit)) >> StartBit;\r
 }\r
 \r
 /**\r
-  Worker function that reads a bit field from Operand, performs a bitwise OR, \r
+  Worker function that reads a bit field from Operand, performs a bitwise OR,\r
   and returns the result.\r
 \r
   Performs a bitwise OR between the bit field specified by StartBit and EndBit\r
   in Operand and the value specified by AndData. All other bits in Operand are\r
   preserved. The new value is returned.\r
 \r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
-  @param  OrData    The value to OR with the read value from the value\r
+  @param  OrData    The value to OR with the read value from the value.\r
 \r
   @return The new value.\r
 \r
 **/\r
-unsigned int\r
-BitFieldOrUint (\r
-  IN      unsigned int              Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      unsigned int              OrData\r
+UINTN\r
+EFIAPI\r
+InternalBaseLibBitFieldOrUint (\r
+  IN      UINTN  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINTN  OrData\r
   )\r
 {\r
   //\r
-  // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]\r
+  // Higher bits in OrData those are not used must be zero.\r
+  //\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
+  // ~((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
   //\r
-  return Operand | ((OrData << StartBit) & ~((unsigned int) -2 << EndBit));\r
+  return Operand | ((OrData << StartBit) & ~((UINTN)-2 << EndBit));\r
 }\r
 \r
 /**\r
-  Worker function that reads a bit field from Operand, performs a bitwise AND, \r
+  Worker function that reads a bit field from Operand, performs a bitwise AND,\r
   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. All other bits in Operand are\r
   preserved. The new value is returned.\r
 \r
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, 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
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
-  @param  AndData    The value to And with the read value from the value\r
+  @param  AndData    The value to And with the read value from the value.\r
 \r
   @return The new value.\r
 \r
 **/\r
-unsigned int\r
-BitFieldAndUint (\r
-  IN      unsigned int              Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      unsigned int              AndData\r
+UINTN\r
+EFIAPI\r
+InternalBaseLibBitFieldAndUint (\r
+  IN      UINTN  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINTN  AndData\r
   )\r
 {\r
   //\r
-  // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]\r
+  // Higher bits in AndData those are not used must be zero.\r
+  //\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
+  //\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
   //\r
-  return Operand & ~((~AndData << StartBit) & ~((unsigned int) -2 << EndBit));\r
+  return Operand & ~((~AndData << StartBit) & ~((UINTN)-2 << EndBit));\r
 }\r
 \r
 /**\r
@@ -124,14 +141,14 @@ BitFieldAndUint (
 UINT8\r
 EFIAPI\r
 BitFieldRead8 (\r
-  IN      UINT8                     Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit\r
+  IN      UINT8  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 8);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT8)BitFieldReadUint (Operand, StartBit, EndBit);\r
+  return (UINT8)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);\r
 }\r
 \r
 /**\r
@@ -145,13 +162,14 @@ BitFieldRead8 (
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If Value is larger than the bitmask value range specified by StartBit and EndBit, 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..7.\r
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
                     Range 0..7.\r
-  @param  Value     New value of the bit field.\r
+  @param  Value     The new value of the bit field.\r
 \r
   @return The new 8-bit value.\r
 \r
@@ -159,13 +177,13 @@ BitFieldRead8 (
 UINT8\r
 EFIAPI\r
 BitFieldWrite8 (\r
-  IN      UINT8                     Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT8                     Value\r
+  IN      UINT8  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINT8  Value\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 8);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldAndThenOr8 (Operand, StartBit, EndBit, 0, Value);\r
 }\r
@@ -174,7 +192,7 @@ BitFieldWrite8 (
   Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the\r
   result.\r
 \r
-  Performs a bitwise inclusive OR between the bit field specified by StartBit\r
+  Performs a bitwise OR between the bit field specified by StartBit\r
   and EndBit in Operand and the value specified by OrData. All other bits in\r
   Operand are preserved. The new 8-bit value is returned.\r
 \r
@@ -182,13 +200,14 @@ BitFieldWrite8 (
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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..7.\r
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
                     Range 0..7.\r
-  @param  OrData    The value to OR with the read value from the value\r
+  @param  OrData    The value to OR with the read value from the value.\r
 \r
   @return The new 8-bit value.\r
 \r
@@ -196,15 +215,15 @@ BitFieldWrite8 (
 UINT8\r
 EFIAPI\r
 BitFieldOr8 (\r
-  IN      UINT8                     Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT8                     OrData\r
+  IN      UINT8  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINT8  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 8);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT8)BitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
+  return (UINT8)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
 }\r
 \r
 /**\r
@@ -219,6 +238,7 @@ BitFieldOr8 (
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -233,15 +253,15 @@ BitFieldOr8 (
 UINT8\r
 EFIAPI\r
 BitFieldAnd8 (\r
-  IN      UINT8                     Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT8                     AndData\r
+  IN      UINT8  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINT8  AndData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 8);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT8)BitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
+  return (UINT8)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
 }\r
 \r
 /**\r
@@ -250,13 +270,15 @@ BitFieldAnd8 (
 \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
-  inclusive OR with value specified by OrData. All other bits in Operand are\r
+  OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 8-bit value is returned.\r
 \r
   If 8-bit operations are not supported, then ASSERT().\r
   If StartBit is greater than 7, then ASSERT().\r
   If EndBit is greater than 7, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -272,14 +294,14 @@ BitFieldAnd8 (
 UINT8\r
 EFIAPI\r
 BitFieldAndThenOr8 (\r
-  IN      UINT8                     Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT8                     AndData,\r
-  IN      UINT8                     OrData\r
+  IN      UINT8  Operand,\r
+  IN      UINTN  StartBit,\r
+  IN      UINTN  EndBit,\r
+  IN      UINT8  AndData,\r
+  IN      UINT8  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 8);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldOr8 (\r
            BitFieldAnd8 (Operand, StartBit, EndBit, AndData),\r
@@ -311,14 +333,14 @@ BitFieldAndThenOr8 (
 UINT16\r
 EFIAPI\r
 BitFieldRead16 (\r
-  IN      UINT16                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit\r
+  IN      UINT16  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 16);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT16)BitFieldReadUint (Operand, StartBit, EndBit);\r
+  return (UINT16)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);\r
 }\r
 \r
 /**\r
@@ -332,13 +354,14 @@ BitFieldRead16 (
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If Value is larger than the bitmask value range specified by StartBit and EndBit, 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..15.\r
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
                     Range 0..15.\r
-  @param  Value     New value of the bit field.\r
+  @param  Value     The new value of the bit field.\r
 \r
   @return The new 16-bit value.\r
 \r
@@ -346,13 +369,13 @@ BitFieldRead16 (
 UINT16\r
 EFIAPI\r
 BitFieldWrite16 (\r
-  IN      UINT16                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT16                    Value\r
+  IN      UINT16  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT16  Value\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 16);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldAndThenOr16 (Operand, StartBit, EndBit, 0, Value);\r
 }\r
@@ -361,7 +384,7 @@ BitFieldWrite16 (
   Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the\r
   result.\r
 \r
-  Performs a bitwise inclusive OR between the bit field specified by StartBit\r
+  Performs a bitwise OR between the bit field specified by StartBit\r
   and EndBit in Operand and the value specified by OrData. All other bits in\r
   Operand are preserved. The new 16-bit value is returned.\r
 \r
@@ -369,13 +392,14 @@ BitFieldWrite16 (
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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..15.\r
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
                     Range 0..15.\r
-  @param  OrData    The value to OR with the read value from the value\r
+  @param  OrData    The value to OR with the read value from the value.\r
 \r
   @return The new 16-bit value.\r
 \r
@@ -383,15 +407,15 @@ BitFieldWrite16 (
 UINT16\r
 EFIAPI\r
 BitFieldOr16 (\r
-  IN      UINT16                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT16                    OrData\r
+  IN      UINT16  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT16  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 16);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT16)BitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
+  return (UINT16)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
 }\r
 \r
 /**\r
@@ -406,13 +430,14 @@ BitFieldOr16 (
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, 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..15.\r
   @param  EndBit    The ordinal of the most significant bit in the bit field.\r
                     Range 0..15.\r
-  @param  AndData   The value to AND with the read value from the value\r
+  @param  AndData   The value to AND with the read value from the value.\r
 \r
   @return The new 16-bit value.\r
 \r
@@ -420,15 +445,15 @@ BitFieldOr16 (
 UINT16\r
 EFIAPI\r
 BitFieldAnd16 (\r
-  IN      UINT16                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT16                    AndData\r
+  IN      UINT16  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT16  AndData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 16);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT16)BitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
+  return (UINT16)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
 }\r
 \r
 /**\r
@@ -437,13 +462,15 @@ BitFieldAnd16 (
 \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
-  inclusive OR with value specified by OrData. All other bits in Operand are\r
+  OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 16-bit value is returned.\r
 \r
   If 16-bit operations are not supported, then ASSERT().\r
   If StartBit is greater than 15, then ASSERT().\r
   If EndBit is greater than 15, then ASSERT().\r
   If EndBit is less than StartBit, then ASSERT().\r
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -459,14 +486,14 @@ BitFieldAnd16 (
 UINT16\r
 EFIAPI\r
 BitFieldAndThenOr16 (\r
-  IN      UINT16                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT16                    AndData,\r
-  IN      UINT16                    OrData\r
+  IN      UINT16  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT16  AndData,\r
+  IN      UINT16  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 16);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldOr16 (\r
            BitFieldAnd16 (Operand, StartBit, EndBit, AndData),\r
@@ -498,14 +525,14 @@ BitFieldAndThenOr16 (
 UINT32\r
 EFIAPI\r
 BitFieldRead32 (\r
-  IN      UINT32                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit\r
+  IN      UINT32  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 32);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT32)BitFieldReadUint (Operand, StartBit, EndBit);\r
+  return (UINT32)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);\r
 }\r
 \r
 /**\r
@@ -519,13 +546,14 @@ BitFieldRead32 (
   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
+  If Value is larger than the bitmask value range specified by StartBit and EndBit, 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
-  @param  Value     New value of the bit field.\r
+  @param  Value     The new value of the bit field.\r
 \r
   @return The new 32-bit value.\r
 \r
@@ -533,13 +561,13 @@ BitFieldRead32 (
 UINT32\r
 EFIAPI\r
 BitFieldWrite32 (\r
-  IN      UINT32                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT32                    Value\r
+  IN      UINT32  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT32  Value\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 32);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldAndThenOr32 (Operand, StartBit, EndBit, 0, Value);\r
 }\r
@@ -548,7 +576,7 @@ BitFieldWrite32 (
   Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the\r
   result.\r
 \r
-  Performs a bitwise inclusive OR between the bit field specified by StartBit\r
+  Performs a bitwise OR between the bit field specified by StartBit\r
   and EndBit in Operand and the value specified by OrData. All other bits in\r
   Operand are preserved. The new 32-bit value is returned.\r
 \r
@@ -556,13 +584,14 @@ BitFieldWrite32 (
   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
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
-  @param  OrData    The value to OR with the read value from the value\r
+  @param  OrData    The value to OR with the read value from the value.\r
 \r
   @return The new 32-bit value.\r
 \r
@@ -570,15 +599,15 @@ BitFieldWrite32 (
 UINT32\r
 EFIAPI\r
 BitFieldOr32 (\r
-  IN      UINT32                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT32                    OrData\r
+  IN      UINT32  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT32  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 32);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT32)BitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
+  return (UINT32)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);\r
 }\r
 \r
 /**\r
@@ -593,13 +622,14 @@ BitFieldOr32 (
   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
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, 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
-  @param  AndData   The value to AND with the read value from the value\r
+  @param  AndData   The value to AND with the read value from the value.\r
 \r
   @return The new 32-bit value.\r
 \r
@@ -607,15 +637,15 @@ BitFieldOr32 (
 UINT32\r
 EFIAPI\r
 BitFieldAnd32 (\r
-  IN      UINT32                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT32                    AndData\r
+  IN      UINT32  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT32  AndData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 32);\r
   ASSERT (StartBit <= EndBit);\r
-  return (UINT32)BitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
+  return (UINT32)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);\r
 }\r
 \r
 /**\r
@@ -624,13 +654,15 @@ BitFieldAnd32 (
 \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
-  inclusive OR with value specified by OrData. All other bits in Operand are\r
+  OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 32-bit value is returned.\r
 \r
   If 32-bit operations are not supported, then ASSERT().\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
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -646,14 +678,14 @@ BitFieldAnd32 (
 UINT32\r
 EFIAPI\r
 BitFieldAndThenOr32 (\r
-  IN      UINT32                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT32                    AndData,\r
-  IN      UINT32                    OrData\r
+  IN      UINT32  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT32  AndData,\r
+  IN      UINT32  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 32);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldOr32 (\r
            BitFieldAnd32 (Operand, StartBit, EndBit, AndData),\r
@@ -685,12 +717,12 @@ BitFieldAndThenOr32 (
 UINT64\r
 EFIAPI\r
 BitFieldRead64 (\r
-  IN      UINT64                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit\r
+  IN      UINT64  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
   return RShiftU64 (Operand & ~LShiftU64 ((UINT64)-2, EndBit), StartBit);\r
 }\r
@@ -706,13 +738,14 @@ BitFieldRead64 (
   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
+  If Value is larger than the bitmask value range specified by StartBit and EndBit, 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
-  @param  Value     New value of the bit field.\r
+  @param  Value     The new value of the bit field.\r
 \r
   @return The new 64-bit value.\r
 \r
@@ -720,13 +753,13 @@ BitFieldRead64 (
 UINT64\r
 EFIAPI\r
 BitFieldWrite64 (\r
-  IN      UINT64                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT64                    Value\r
+  IN      UINT64  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT64  Value\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldAndThenOr64 (Operand, StartBit, EndBit, 0, Value);\r
 }\r
@@ -735,7 +768,7 @@ BitFieldWrite64 (
   Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the\r
   result.\r
 \r
-  Performs a bitwise inclusive OR between the bit field specified by StartBit\r
+  Performs a bitwise OR between the bit field specified by StartBit\r
   and EndBit in Operand and the value specified by OrData. All other bits in\r
   Operand are preserved. The new 64-bit value is returned.\r
 \r
@@ -743,6 +776,7 @@ BitFieldWrite64 (
   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
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -757,16 +791,29 @@ BitFieldWrite64 (
 UINT64\r
 EFIAPI\r
 BitFieldOr64 (\r
-  IN      UINT64                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT64                    OrData\r
+  IN      UINT64  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT64  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  UINT64  Value1;\r
+  UINT64  Value2;\r
+\r
+  ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
-  return Operand |\r
-         (LShiftU64 (OrData, StartBit) & ~LShiftU64 ((UINT64)-2, EndBit));\r
+  //\r
+  // Higher bits in OrData those are not used must be zero.\r
+  //\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
+  Value1 = LShiftU64 (OrData, StartBit);\r
+  Value2 = LShiftU64 ((UINT64)-2, EndBit);\r
+\r
+  return Operand | (Value1 & ~Value2);\r
 }\r
 \r
 /**\r
@@ -781,13 +828,14 @@ BitFieldOr64 (
   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
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, 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
-  @param  AndData   The value to AND with the read value from the value\r
+  @param  AndData   The value to AND with the read value from the value.\r
 \r
   @return The new 64-bit value.\r
 \r
@@ -795,16 +843,29 @@ BitFieldOr64 (
 UINT64\r
 EFIAPI\r
 BitFieldAnd64 (\r
-  IN      UINT64                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT64                    AndData\r
+  IN      UINT64  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT64  AndData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  UINT64  Value1;\r
+  UINT64  Value2;\r
+\r
+  ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
-  return Operand &\r
-         ~(LShiftU64 (~AndData, StartBit) & ~LShiftU64 ((UINT64)-2, EndBit));\r
+  //\r
+  // Higher bits in AndData those are not used must be zero.\r
+  //\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
+  Value1 = LShiftU64 (~AndData, StartBit);\r
+  Value2 = LShiftU64 ((UINT64)-2, EndBit);\r
+\r
+  return Operand & ~(Value1 & ~Value2);\r
 }\r
 \r
 /**\r
@@ -813,13 +874,15 @@ BitFieldAnd64 (
 \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
-  inclusive OR with value specified by OrData. All other bits in Operand are\r
+  OR with value specified by OrData. All other bits in Operand are\r
   preserved. The new 64-bit value is returned.\r
 \r
   If 64-bit operations are not supported, then ASSERT().\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
+  If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().\r
+  If OrData is larger than the bitmask value range specified by StartBit and EndBit, 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
@@ -835,14 +898,14 @@ BitFieldAnd64 (
 UINT64\r
 EFIAPI\r
 BitFieldAndThenOr64 (\r
-  IN      UINT64                    Operand,\r
-  IN      UINTN                     StartBit,\r
-  IN      UINTN                     EndBit,\r
-  IN      UINT64                    AndData,\r
-  IN      UINT64                    OrData\r
+  IN      UINT64  Operand,\r
+  IN      UINTN   StartBit,\r
+  IN      UINTN   EndBit,\r
+  IN      UINT64  AndData,\r
+  IN      UINT64  OrData\r
   )\r
 {\r
-  ASSERT (EndBit < sizeof (Operand) * 8);\r
+  ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
   return BitFieldOr64 (\r
            BitFieldAnd64 (Operand, StartBit, EndBit, AndData),\r
@@ -851,3 +914,88 @@ 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