]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/BitField.c
2 Bit field functions of BaseLib.
4 Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php.
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
15 #include "BaseLibInternals.h"
18 Worker function that returns a bit field from Operand.
20 Returns the bitfield specified by the StartBit and the EndBit from Operand.
22 @param Operand Operand on which to perform the bitfield operation.
23 @param StartBit The ordinal of the least significant bit in the bit field.
24 @param EndBit The ordinal of the most significant bit in the bit field.
26 @return The bit field read.
31 InternalBaseLibBitFieldReadUint (
38 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
39 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
41 return (Operand
& ~((UINTN
)-2 << EndBit
)) >> StartBit
;
45 Worker function that reads a bit field from Operand, performs a bitwise OR,
46 and returns the result.
48 Performs a bitwise OR between the bit field specified by StartBit and EndBit
49 in Operand and the value specified by AndData. All other bits in Operand are
50 preserved. The new value is returned.
52 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
54 @param Operand Operand on which to perform the bitfield operation.
55 @param StartBit The ordinal of the least significant bit in the bit field.
56 @param EndBit The ordinal of the most significant bit in the bit field.
57 @param OrData The value to OR with the read value from the value.
59 @return The new value.
64 InternalBaseLibBitFieldOrUint (
72 // Higher bits in OrData those are not used must be zero.
74 ASSERT ((OrData
>> (EndBit
- StartBit
+ 1)) == 0);
77 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
78 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
80 return Operand
| ((OrData
<< StartBit
) & ~((UINTN
) -2 << EndBit
));
84 Worker function that reads a bit field from Operand, performs a bitwise AND,
85 and returns the result.
87 Performs a bitwise AND between the bit field specified by StartBit and EndBit
88 in Operand and the value specified by AndData. All other bits in Operand are
89 preserved. The new value is returned.
91 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
93 @param Operand Operand on which to perform the bitfield operation.
94 @param StartBit The ordinal of the least significant bit in the bit field.
95 @param EndBit The ordinal of the most significant bit in the bit field.
96 @param AndData The value to And with the read value from the value.
98 @return The new value.
103 InternalBaseLibBitFieldAndUint (
111 // Higher bits in AndData those are not used must be zero.
113 ASSERT ((AndData
>> (EndBit
- StartBit
+ 1)) == 0);
116 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
117 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
119 return Operand
& ~((~AndData
<< StartBit
) & ~((UINTN
)-2 << EndBit
));
123 Returns a bit field from an 8-bit value.
125 Returns the bitfield specified by the StartBit and the EndBit from Operand.
127 If 8-bit operations are not supported, then ASSERT().
128 If StartBit is greater than 7, then ASSERT().
129 If EndBit is greater than 7, then ASSERT().
130 If EndBit is less than StartBit, then ASSERT().
132 @param Operand Operand on which to perform the bitfield operation.
133 @param StartBit The ordinal of the least significant bit in the bit field.
135 @param EndBit The ordinal of the most significant bit in the bit field.
138 @return The bit field read.
150 ASSERT (StartBit
<= EndBit
);
151 return (UINT8
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
155 Writes a bit field to an 8-bit value, and returns the result.
157 Writes Value to the bit field specified by the StartBit and the EndBit in
158 Operand. All other bits in Operand are preserved. The new 8-bit value is
161 If 8-bit operations are not supported, then ASSERT().
162 If StartBit is greater than 7, then ASSERT().
163 If EndBit is greater than 7, then ASSERT().
164 If EndBit is less than StartBit, then ASSERT().
165 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
167 @param Operand Operand on which to perform the bitfield operation.
168 @param StartBit The ordinal of the least significant bit in the bit field.
170 @param EndBit The ordinal of the most significant bit in the bit field.
172 @param Value The new value of the bit field.
174 @return The new 8-bit value.
187 ASSERT (StartBit
<= EndBit
);
188 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
192 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
195 Performs a bitwise OR between the bit field specified by StartBit
196 and EndBit in Operand and the value specified by OrData. All other bits in
197 Operand are preserved. The new 8-bit value is returned.
199 If 8-bit operations are not supported, then ASSERT().
200 If StartBit is greater than 7, then ASSERT().
201 If EndBit is greater than 7, then ASSERT().
202 If EndBit is less than StartBit, then ASSERT().
203 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
205 @param Operand Operand on which to perform the bitfield operation.
206 @param StartBit The ordinal of the least significant bit in the bit field.
208 @param EndBit The ordinal of the most significant bit in the bit field.
210 @param OrData The value to OR with the read value from the value.
212 @return The new 8-bit value.
225 ASSERT (StartBit
<= EndBit
);
226 return (UINT8
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
230 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
233 Performs a bitwise AND between the bit field specified by StartBit and EndBit
234 in Operand and the value specified by AndData. All other bits in Operand are
235 preserved. The new 8-bit value is returned.
237 If 8-bit operations are not supported, then ASSERT().
238 If StartBit is greater than 7, then ASSERT().
239 If EndBit is greater than 7, then ASSERT().
240 If EndBit is less than StartBit, then ASSERT().
241 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
243 @param Operand Operand on which to perform the bitfield operation.
244 @param StartBit The ordinal of the least significant bit in the bit field.
246 @param EndBit The ordinal of the most significant bit in the bit field.
248 @param AndData The value to AND with the read value from the value.
250 @return The new 8-bit value.
263 ASSERT (StartBit
<= EndBit
);
264 return (UINT8
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
268 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
269 bitwise OR, and returns the result.
271 Performs a bitwise AND between the bit field specified by StartBit and EndBit
272 in Operand and the value specified by AndData, followed by a bitwise
273 OR with value specified by OrData. All other bits in Operand are
274 preserved. The new 8-bit value is returned.
276 If 8-bit operations are not supported, then ASSERT().
277 If StartBit is greater than 7, then ASSERT().
278 If EndBit is greater than 7, then ASSERT().
279 If EndBit is less than StartBit, then ASSERT().
280 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
281 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
283 @param Operand Operand on which to perform the bitfield operation.
284 @param StartBit The ordinal of the least significant bit in the bit field.
286 @param EndBit The ordinal of the most significant bit in the bit field.
288 @param AndData The value to AND with the read value from the value.
289 @param OrData The value to OR with the result of the AND operation.
291 @return The new 8-bit value.
305 ASSERT (StartBit
<= EndBit
);
307 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
315 Returns a bit field from a 16-bit value.
317 Returns the bitfield specified by the StartBit and the EndBit from Operand.
319 If 16-bit operations are not supported, then ASSERT().
320 If StartBit is greater than 15, then ASSERT().
321 If EndBit is greater than 15, then ASSERT().
322 If EndBit is less than StartBit, then ASSERT().
324 @param Operand Operand on which to perform the bitfield operation.
325 @param StartBit The ordinal of the least significant bit in the bit field.
327 @param EndBit The ordinal of the most significant bit in the bit field.
330 @return The bit field read.
341 ASSERT (EndBit
< 16);
342 ASSERT (StartBit
<= EndBit
);
343 return (UINT16
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
347 Writes a bit field to a 16-bit value, and returns the result.
349 Writes Value to the bit field specified by the StartBit and the EndBit in
350 Operand. All other bits in Operand are preserved. The new 16-bit value is
353 If 16-bit operations are not supported, then ASSERT().
354 If StartBit is greater than 15, then ASSERT().
355 If EndBit is greater than 15, then ASSERT().
356 If EndBit is less than StartBit, then ASSERT().
357 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
359 @param Operand Operand on which to perform the bitfield operation.
360 @param StartBit The ordinal of the least significant bit in the bit field.
362 @param EndBit The ordinal of the most significant bit in the bit field.
364 @param Value The new value of the bit field.
366 @return The new 16-bit value.
378 ASSERT (EndBit
< 16);
379 ASSERT (StartBit
<= EndBit
);
380 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
384 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
387 Performs a bitwise OR between the bit field specified by StartBit
388 and EndBit in Operand and the value specified by OrData. All other bits in
389 Operand are preserved. The new 16-bit value is returned.
391 If 16-bit operations are not supported, then ASSERT().
392 If StartBit is greater than 15, then ASSERT().
393 If EndBit is greater than 15, then ASSERT().
394 If EndBit is less than StartBit, then ASSERT().
395 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
397 @param Operand Operand on which to perform the bitfield operation.
398 @param StartBit The ordinal of the least significant bit in the bit field.
400 @param EndBit The ordinal of the most significant bit in the bit field.
402 @param OrData The value to OR with the read value from the value.
404 @return The new 16-bit value.
416 ASSERT (EndBit
< 16);
417 ASSERT (StartBit
<= EndBit
);
418 return (UINT16
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
422 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
425 Performs a bitwise AND between the bit field specified by StartBit and EndBit
426 in Operand and the value specified by AndData. All other bits in Operand are
427 preserved. The new 16-bit value is returned.
429 If 16-bit operations are not supported, then ASSERT().
430 If StartBit is greater than 15, then ASSERT().
431 If EndBit is greater than 15, then ASSERT().
432 If EndBit is less than StartBit, then ASSERT().
433 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
435 @param Operand Operand on which to perform the bitfield operation.
436 @param StartBit The ordinal of the least significant bit in the bit field.
438 @param EndBit The ordinal of the most significant bit in the bit field.
440 @param AndData The value to AND with the read value from the value.
442 @return The new 16-bit value.
454 ASSERT (EndBit
< 16);
455 ASSERT (StartBit
<= EndBit
);
456 return (UINT16
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
460 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
461 bitwise OR, and returns the result.
463 Performs a bitwise AND between the bit field specified by StartBit and EndBit
464 in Operand and the value specified by AndData, followed by a bitwise
465 OR with value specified by OrData. All other bits in Operand are
466 preserved. The new 16-bit value is returned.
468 If 16-bit operations are not supported, then ASSERT().
469 If StartBit is greater than 15, then ASSERT().
470 If EndBit is greater than 15, then ASSERT().
471 If EndBit is less than StartBit, then ASSERT().
472 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
473 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
475 @param Operand Operand on which to perform the bitfield operation.
476 @param StartBit The ordinal of the least significant bit in the bit field.
478 @param EndBit The ordinal of the most significant bit in the bit field.
480 @param AndData The value to AND with the read value from the value.
481 @param OrData The value to OR with the result of the AND operation.
483 @return The new 16-bit value.
488 BitFieldAndThenOr16 (
496 ASSERT (EndBit
< 16);
497 ASSERT (StartBit
<= EndBit
);
498 return BitFieldOr16 (
499 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
507 Returns a bit field from a 32-bit value.
509 Returns the bitfield specified by the StartBit and the EndBit from Operand.
511 If 32-bit operations are not supported, then ASSERT().
512 If StartBit is greater than 31, then ASSERT().
513 If EndBit is greater than 31, then ASSERT().
514 If EndBit is less than StartBit, then ASSERT().
516 @param Operand Operand on which to perform the bitfield operation.
517 @param StartBit The ordinal of the least significant bit in the bit field.
519 @param EndBit The ordinal of the most significant bit in the bit field.
522 @return The bit field read.
533 ASSERT (EndBit
< 32);
534 ASSERT (StartBit
<= EndBit
);
535 return (UINT32
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
539 Writes a bit field to a 32-bit value, and returns the result.
541 Writes Value to the bit field specified by the StartBit and the EndBit in
542 Operand. All other bits in Operand are preserved. The new 32-bit value is
545 If 32-bit operations are not supported, then ASSERT().
546 If StartBit is greater than 31, then ASSERT().
547 If EndBit is greater than 31, then ASSERT().
548 If EndBit is less than StartBit, then ASSERT().
549 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
551 @param Operand Operand on which to perform the bitfield operation.
552 @param StartBit The ordinal of the least significant bit in the bit field.
554 @param EndBit The ordinal of the most significant bit in the bit field.
556 @param Value The new value of the bit field.
558 @return The new 32-bit value.
570 ASSERT (EndBit
< 32);
571 ASSERT (StartBit
<= EndBit
);
572 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
576 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
579 Performs a bitwise OR between the bit field specified by StartBit
580 and EndBit in Operand and the value specified by OrData. All other bits in
581 Operand are preserved. The new 32-bit value is returned.
583 If 32-bit operations are not supported, then ASSERT().
584 If StartBit is greater than 31, then ASSERT().
585 If EndBit is greater than 31, then ASSERT().
586 If EndBit is less than StartBit, then ASSERT().
587 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
589 @param Operand Operand on which to perform the bitfield operation.
590 @param StartBit The ordinal of the least significant bit in the bit field.
592 @param EndBit The ordinal of the most significant bit in the bit field.
594 @param OrData The value to OR with the read value from the value.
596 @return The new 32-bit value.
608 ASSERT (EndBit
< 32);
609 ASSERT (StartBit
<= EndBit
);
610 return (UINT32
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
614 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
617 Performs a bitwise AND between the bit field specified by StartBit and EndBit
618 in Operand and the value specified by AndData. All other bits in Operand are
619 preserved. The new 32-bit value is returned.
621 If 32-bit operations are not supported, then ASSERT().
622 If StartBit is greater than 31, then ASSERT().
623 If EndBit is greater than 31, then ASSERT().
624 If EndBit is less than StartBit, then ASSERT().
625 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
627 @param Operand Operand on which to perform the bitfield operation.
628 @param StartBit The ordinal of the least significant bit in the bit field.
630 @param EndBit The ordinal of the most significant bit in the bit field.
632 @param AndData The value to AND with the read value from the value.
634 @return The new 32-bit value.
646 ASSERT (EndBit
< 32);
647 ASSERT (StartBit
<= EndBit
);
648 return (UINT32
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
652 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
653 bitwise OR, and returns the result.
655 Performs a bitwise AND between the bit field specified by StartBit and EndBit
656 in Operand and the value specified by AndData, followed by a bitwise
657 OR with value specified by OrData. All other bits in Operand are
658 preserved. The new 32-bit value is returned.
660 If 32-bit operations are not supported, then ASSERT().
661 If StartBit is greater than 31, then ASSERT().
662 If EndBit is greater than 31, then ASSERT().
663 If EndBit is less than StartBit, then ASSERT().
664 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
665 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
667 @param Operand Operand on which to perform the bitfield operation.
668 @param StartBit The ordinal of the least significant bit in the bit field.
670 @param EndBit The ordinal of the most significant bit in the bit field.
672 @param AndData The value to AND with the read value from the value.
673 @param OrData The value to OR with the result of the AND operation.
675 @return The new 32-bit value.
680 BitFieldAndThenOr32 (
688 ASSERT (EndBit
< 32);
689 ASSERT (StartBit
<= EndBit
);
690 return BitFieldOr32 (
691 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
699 Returns a bit field from a 64-bit value.
701 Returns the bitfield specified by the StartBit and the EndBit from Operand.
703 If 64-bit operations are not supported, then ASSERT().
704 If StartBit is greater than 63, then ASSERT().
705 If EndBit is greater than 63, then ASSERT().
706 If EndBit is less than StartBit, then ASSERT().
708 @param Operand Operand on which to perform the bitfield operation.
709 @param StartBit The ordinal of the least significant bit in the bit field.
711 @param EndBit The ordinal of the most significant bit in the bit field.
714 @return The bit field read.
725 ASSERT (EndBit
< 64);
726 ASSERT (StartBit
<= EndBit
);
727 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
731 Writes a bit field to a 64-bit value, and returns the result.
733 Writes Value to the bit field specified by the StartBit and the EndBit in
734 Operand. All other bits in Operand are preserved. The new 64-bit value is
737 If 64-bit operations are not supported, then ASSERT().
738 If StartBit is greater than 63, then ASSERT().
739 If EndBit is greater than 63, then ASSERT().
740 If EndBit is less than StartBit, then ASSERT().
741 If Value is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
743 @param Operand Operand on which to perform the bitfield operation.
744 @param StartBit The ordinal of the least significant bit in the bit field.
746 @param EndBit The ordinal of the most significant bit in the bit field.
748 @param Value The new value of the bit field.
750 @return The new 64-bit value.
762 ASSERT (EndBit
< 64);
763 ASSERT (StartBit
<= EndBit
);
764 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
768 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
771 Performs a bitwise OR between the bit field specified by StartBit
772 and EndBit in Operand and the value specified by OrData. All other bits in
773 Operand are preserved. The new 64-bit value is returned.
775 If 64-bit operations are not supported, then ASSERT().
776 If StartBit is greater than 63, then ASSERT().
777 If EndBit is greater than 63, then ASSERT().
778 If EndBit is less than StartBit, then ASSERT().
779 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
781 @param Operand Operand on which to perform the bitfield operation.
782 @param StartBit The ordinal of the least significant bit in the bit field.
784 @param EndBit The ordinal of the most significant bit in the bit field.
786 @param OrData The value to OR with the read value from the value
788 @return The new 64-bit value.
803 ASSERT (EndBit
< 64);
804 ASSERT (StartBit
<= EndBit
);
805 ASSERT (RShiftU64 (OrData
, EndBit
- StartBit
+ 1) == 0);
807 Value1
= LShiftU64 (OrData
, StartBit
);
808 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
810 return Operand
| (Value1
& ~Value2
);
814 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
817 Performs a bitwise AND between the bit field specified by StartBit and EndBit
818 in Operand and the value specified by AndData. All other bits in Operand are
819 preserved. The new 64-bit value is returned.
821 If 64-bit operations are not supported, then ASSERT().
822 If StartBit is greater than 63, then ASSERT().
823 If EndBit is greater than 63, then ASSERT().
824 If EndBit is less than StartBit, then ASSERT().
825 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
827 @param Operand Operand on which to perform the bitfield operation.
828 @param StartBit The ordinal of the least significant bit in the bit field.
830 @param EndBit The ordinal of the most significant bit in the bit field.
832 @param AndData The value to AND with the read value from the value.
834 @return The new 64-bit value.
849 ASSERT (EndBit
< 64);
850 ASSERT (StartBit
<= EndBit
);
851 ASSERT (RShiftU64 (AndData
, EndBit
- StartBit
+ 1) == 0);
853 Value1
= LShiftU64 (~AndData
, StartBit
);
854 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
856 return Operand
& ~(Value1
& ~Value2
);
860 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
861 bitwise OR, and returns the result.
863 Performs a bitwise AND between the bit field specified by StartBit and EndBit
864 in Operand and the value specified by AndData, followed by a bitwise
865 OR with value specified by OrData. All other bits in Operand are
866 preserved. The new 64-bit value is returned.
868 If 64-bit operations are not supported, then ASSERT().
869 If StartBit is greater than 63, then ASSERT().
870 If EndBit is greater than 63, then ASSERT().
871 If EndBit is less than StartBit, then ASSERT().
872 If AndData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
873 If OrData is larger than the bitmask value range specified by StartBit and EndBit, then ASSERT().
875 @param Operand Operand on which to perform the bitfield operation.
876 @param StartBit The ordinal of the least significant bit in the bit field.
878 @param EndBit The ordinal of the most significant bit in the bit field.
880 @param AndData The value to AND with the read value from the value.
881 @param OrData The value to OR with the result of the AND operation.
883 @return The new 64-bit value.
888 BitFieldAndThenOr64 (
896 ASSERT (EndBit
< 64);
897 ASSERT (StartBit
<= EndBit
);
898 return BitFieldOr64 (
899 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),