X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBaseLib%2FBitField.c;h=1c1fbdfeb781d3f1599f97d0039ca36356f22fae;hb=4e57f2409839ca854c898f2acafa6b893f096f1a;hp=0b517aa974f7f505cd7a59c472df3e03a7b32305;hpb=1ea5ca46c7eefe66a01cb4db3b72fc0e5a04e2cb;p=mirror_edk2.git diff --git a/MdePkg/Library/BaseLib/BitField.c b/MdePkg/Library/BaseLib/BitField.c index 0b517aa974..1c1fbdfeb7 100644 --- a/MdePkg/Library/BaseLib/BitField.c +++ b/MdePkg/Library/BaseLib/BitField.c @@ -1,7 +1,7 @@ /** @file Bit field functions of BaseLib. - Copyright (c) 2006, Intel Corporation
+ Copyright (c) 2006 - 2008, Intel Corporation
All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -10,12 +10,12 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: BitField.c - **/ +#include "BaseLibInternals.h" + /** - Worker function that returns a bit field from Operand + Worker function that returns a bit field from Operand. Returns the bitfield specified by the StartBit and the EndBit from Operand. @@ -26,22 +26,23 @@ @return The bit field read. **/ -unsigned int -BitFieldReadUint ( - IN unsigned int Operand, +UINTN +EFIAPI +InternalBaseLibBitFieldReadUint ( + IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit ) { // - // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] + // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] // are 1's while bit[EndBit + 1] thru the most significant bit are 0's. // - return (Operand & ~((unsigned int)-2 << EndBit)) >> StartBit; + return (Operand & ~((UINTN)-2 << EndBit)) >> StartBit; } /** - Worker function that reads a bit field from Operand, performs a bitwise OR, + Worker function that reads a bit field from Operand, performs a bitwise OR, and returns the result. Performs a bitwise OR between the bit field specified by StartBit and EndBit @@ -56,23 +57,24 @@ BitFieldReadUint ( @return The new value. **/ -unsigned int -BitFieldOrUint ( - IN unsigned int Operand, +UINTN +EFIAPI +InternalBaseLibBitFieldOrUint ( + IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit, - IN unsigned int OrData + IN UINTN OrData ) { // - // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] + // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] // are 1's while bit[EndBit + 1] thru the most significant bit are 0's. // - return Operand | ((OrData << StartBit) & ~((unsigned int) -2 << EndBit)); + return Operand | ((OrData << StartBit) & ~((UINTN) -2 << EndBit)); } /** - Worker function that reads a bit field from Operand, performs a bitwise AND, + Worker function that reads a bit field from Operand, performs a bitwise AND, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit @@ -87,19 +89,20 @@ BitFieldOrUint ( @return The new value. **/ -unsigned int -BitFieldAndUint ( - IN unsigned int Operand, +UINTN +EFIAPI +InternalBaseLibBitFieldAndUint ( + IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit, - IN unsigned int AndData + IN UINTN AndData ) { // - // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] + // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit] // are 1's while bit[EndBit + 1] thru the most significant bit are 0's. // - return Operand & ~((~AndData << StartBit) & ~((unsigned int) -2 << EndBit)); + return Operand & ~((~AndData << StartBit) & ~((UINTN)-2 << EndBit)); } /** @@ -129,9 +132,9 @@ BitFieldRead8 ( IN UINTN EndBit ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 8); ASSERT (StartBit <= EndBit); - return (UINT8)BitFieldReadUint (Operand, StartBit, EndBit); + return (UINT8)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit); } /** @@ -165,7 +168,7 @@ BitFieldWrite8 ( IN UINT8 Value ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 8); ASSERT (StartBit <= EndBit); return BitFieldAndThenOr8 (Operand, StartBit, EndBit, 0, Value); } @@ -174,7 +177,7 @@ BitFieldWrite8 ( Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the result. - Performs a bitwise inclusive OR between the bit field specified by StartBit + Performs a bitwise OR between the bit field specified by StartBit and EndBit in Operand and the value specified by OrData. All other bits in Operand are preserved. The new 8-bit value is returned. @@ -202,9 +205,9 @@ BitFieldOr8 ( IN UINT8 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 8); ASSERT (StartBit <= EndBit); - return (UINT8)BitFieldOrUint (Operand, StartBit, EndBit, OrData); + return (UINT8)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData); } /** @@ -239,9 +242,9 @@ BitFieldAnd8 ( IN UINT8 AndData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 8); ASSERT (StartBit <= EndBit); - return (UINT8)BitFieldAndUint (Operand, StartBit, EndBit, AndData); + return (UINT8)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData); } /** @@ -249,8 +252,8 @@ BitFieldAnd8 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise - inclusive OR with value specified by OrData. All other bits in Operand are + in Operand and the value specified by AndData, followed by a bitwise + OR with value specified by OrData. All other bits in Operand are preserved. The new 8-bit value is returned. If 8-bit operations are not supported, then ASSERT(). @@ -279,7 +282,7 @@ BitFieldAndThenOr8 ( IN UINT8 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 8); ASSERT (StartBit <= EndBit); return BitFieldOr8 ( BitFieldAnd8 (Operand, StartBit, EndBit, AndData), @@ -316,9 +319,9 @@ BitFieldRead16 ( IN UINTN EndBit ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 16); ASSERT (StartBit <= EndBit); - return (UINT16)BitFieldReadUint (Operand, StartBit, EndBit); + return (UINT16)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit); } /** @@ -352,7 +355,7 @@ BitFieldWrite16 ( IN UINT16 Value ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 16); ASSERT (StartBit <= EndBit); return BitFieldAndThenOr16 (Operand, StartBit, EndBit, 0, Value); } @@ -361,7 +364,7 @@ BitFieldWrite16 ( Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the result. - Performs a bitwise inclusive OR between the bit field specified by StartBit + Performs a bitwise OR between the bit field specified by StartBit and EndBit in Operand and the value specified by OrData. All other bits in Operand are preserved. The new 16-bit value is returned. @@ -389,9 +392,9 @@ BitFieldOr16 ( IN UINT16 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 16); ASSERT (StartBit <= EndBit); - return (UINT16)BitFieldOrUint (Operand, StartBit, EndBit, OrData); + return (UINT16)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData); } /** @@ -426,9 +429,9 @@ BitFieldAnd16 ( IN UINT16 AndData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 16); ASSERT (StartBit <= EndBit); - return (UINT16)BitFieldAndUint (Operand, StartBit, EndBit, AndData); + return (UINT16)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData); } /** @@ -436,8 +439,8 @@ BitFieldAnd16 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise - inclusive OR with value specified by OrData. All other bits in Operand are + in Operand and the value specified by AndData, followed by a bitwise + OR with value specified by OrData. All other bits in Operand are preserved. The new 16-bit value is returned. If 16-bit operations are not supported, then ASSERT(). @@ -466,7 +469,7 @@ BitFieldAndThenOr16 ( IN UINT16 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 16); ASSERT (StartBit <= EndBit); return BitFieldOr16 ( BitFieldAnd16 (Operand, StartBit, EndBit, AndData), @@ -503,9 +506,9 @@ BitFieldRead32 ( IN UINTN EndBit ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 32); ASSERT (StartBit <= EndBit); - return (UINT32)BitFieldReadUint (Operand, StartBit, EndBit); + return (UINT32)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit); } /** @@ -539,7 +542,7 @@ BitFieldWrite32 ( IN UINT32 Value ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 32); ASSERT (StartBit <= EndBit); return BitFieldAndThenOr32 (Operand, StartBit, EndBit, 0, Value); } @@ -548,7 +551,7 @@ BitFieldWrite32 ( Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the result. - Performs a bitwise inclusive OR between the bit field specified by StartBit + Performs a bitwise OR between the bit field specified by StartBit and EndBit in Operand and the value specified by OrData. All other bits in Operand are preserved. The new 32-bit value is returned. @@ -576,9 +579,9 @@ BitFieldOr32 ( IN UINT32 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 32); ASSERT (StartBit <= EndBit); - return (UINT32)BitFieldOrUint (Operand, StartBit, EndBit, OrData); + return (UINT32)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData); } /** @@ -613,9 +616,9 @@ BitFieldAnd32 ( IN UINT32 AndData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 32); ASSERT (StartBit <= EndBit); - return (UINT32)BitFieldAndUint (Operand, StartBit, EndBit, AndData); + return (UINT32)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData); } /** @@ -623,8 +626,8 @@ BitFieldAnd32 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise - inclusive OR with value specified by OrData. All other bits in Operand are + in Operand and the value specified by AndData, followed by a bitwise + OR with value specified by OrData. All other bits in Operand are preserved. The new 32-bit value is returned. If 32-bit operations are not supported, then ASSERT(). @@ -653,7 +656,7 @@ BitFieldAndThenOr32 ( IN UINT32 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 32); ASSERT (StartBit <= EndBit); return BitFieldOr32 ( BitFieldAnd32 (Operand, StartBit, EndBit, AndData), @@ -690,7 +693,7 @@ BitFieldRead64 ( IN UINTN EndBit ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 64); ASSERT (StartBit <= EndBit); return RShiftU64 (Operand & ~LShiftU64 ((UINT64)-2, EndBit), StartBit); } @@ -726,7 +729,7 @@ BitFieldWrite64 ( IN UINT64 Value ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 64); ASSERT (StartBit <= EndBit); return BitFieldAndThenOr64 (Operand, StartBit, EndBit, 0, Value); } @@ -735,7 +738,7 @@ BitFieldWrite64 ( Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the result. - Performs a bitwise inclusive OR between the bit field specified by StartBit + Performs a bitwise OR between the bit field specified by StartBit and EndBit in Operand and the value specified by OrData. All other bits in Operand are preserved. The new 64-bit value is returned. @@ -763,10 +766,16 @@ BitFieldOr64 ( IN UINT64 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + UINT64 Value1; + UINT64 Value2; + + ASSERT (EndBit < 64); ASSERT (StartBit <= EndBit); - return Operand | - (LShiftU64 (OrData, StartBit) & ~LShiftU64 ((UINT64)-2, EndBit)); + + Value1 = LShiftU64 (OrData, StartBit); + Value2 = LShiftU64 ((UINT64) - 2, EndBit); + + return Operand | (Value1 & ~Value2); } /** @@ -801,10 +810,16 @@ BitFieldAnd64 ( IN UINT64 AndData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + UINT64 Value1; + UINT64 Value2; + + ASSERT (EndBit < 64); ASSERT (StartBit <= EndBit); - return Operand & - ~(LShiftU64 (~AndData, StartBit) & ~LShiftU64 ((UINT64)-2, EndBit)); + + Value1 = LShiftU64 (~AndData, StartBit); + Value2 = LShiftU64 ((UINT64)-2, EndBit); + + return Operand & ~(Value1 & ~Value2); } /** @@ -812,8 +827,8 @@ BitFieldAnd64 ( bitwise OR, and returns the result. Performs a bitwise AND between the bit field specified by StartBit and EndBit - in Operand and the value specified by AndData, followed by a bitwise - inclusive OR with value specified by OrData. All other bits in Operand are + in Operand and the value specified by AndData, followed by a bitwise + OR with value specified by OrData. All other bits in Operand are preserved. The new 64-bit value is returned. If 64-bit operations are not supported, then ASSERT(). @@ -842,7 +857,7 @@ BitFieldAndThenOr64 ( IN UINT64 OrData ) { - ASSERT (EndBit < sizeof (Operand) * 8); + ASSERT (EndBit < 64); ASSERT (StartBit <= EndBit); return BitFieldOr64 ( BitFieldAnd64 (Operand, StartBit, EndBit, AndData),