]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BaseLib/BitField.c
Fixed the issue that BitFieldWrite32, BitFieldAnd32, BitFieldOr32, BitFieldAndThenOr3...
[mirror_edk2.git] / MdePkg / Library / BaseLib / BitField.c
index eb5fcd938c9a6ed9a3e00e4a3d59623e3c1dd73f..167ee5b692c31784f8e154db94d2767e0846fc1d 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Bit field functions of BaseLib.\r
 \r
-  Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\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
@@ -71,7 +71,10 @@ InternalBaseLibBitFieldOrUint (
   //\r
   // Higher bits in OrData those are not used must be zero. \r
   //\r
-  ASSERT ((OrData >> (EndBit - StartBit + 1)) == 0);\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
+  //\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
@@ -110,7 +113,10 @@ InternalBaseLibBitFieldAndUint (
   //\r
   // Higher bits in AndData those are not used must be zero. \r
   //\r
-  ASSERT ((AndData >> (EndBit - StartBit + 1)) == 0);\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
+  //\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
@@ -802,7 +808,13 @@ BitFieldOr64 (
 \r
   ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
-  ASSERT (RShiftU64 (OrData, EndBit - StartBit + 1) == 0);\r
+  //\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
+  //\r
+  ASSERT (RShiftU64 (OrData, EndBit - StartBit) == (RShiftU64 (OrData, EndBit - StartBit) & 1));\r
 \r
   Value1 = LShiftU64 (OrData, StartBit);\r
   Value2 = LShiftU64 ((UINT64) - 2, EndBit);\r
@@ -848,7 +860,13 @@ BitFieldAnd64 (
   \r
   ASSERT (EndBit < 64);\r
   ASSERT (StartBit <= EndBit);\r
-  ASSERT (RShiftU64 (AndData, EndBit - StartBit + 1) == 0);\r
+  //\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
+  //\r
+  ASSERT (RShiftU64 (AndData, EndBit - StartBit) == (RShiftU64 (AndData, EndBit - StartBit) & 1));\r
 \r
   Value1 = LShiftU64 (~AndData, StartBit);\r
   Value2 = LShiftU64 ((UINT64)-2, EndBit);\r