]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/BitField.c
2 Bit field functions of BaseLib.
4 Copyright (c) 2006 - 2008, Intel Corporation<BR>
5 All rights reserved. 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 @param Operand Operand on which to perform the bitfield operation.
53 @param StartBit The ordinal of the least significant bit in the bit field.
54 @param EndBit The ordinal of the most significant bit in the bit field.
55 @param OrData The value to OR with the read value from the value
57 @return The new value.
62 InternalBaseLibBitFieldOrUint (
70 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
71 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
73 return Operand
| ((OrData
<< StartBit
) & ~((UINTN
) -2 << EndBit
));
77 Worker function that reads a bit field from Operand, performs a bitwise AND,
78 and returns the result.
80 Performs a bitwise AND between the bit field specified by StartBit and EndBit
81 in Operand and the value specified by AndData. All other bits in Operand are
82 preserved. The new value is returned.
84 @param Operand Operand on which to perform the bitfield operation.
85 @param StartBit The ordinal of the least significant bit in the bit field.
86 @param EndBit The ordinal of the most significant bit in the bit field.
87 @param AndData The value to And with the read value from the value
89 @return The new value.
94 InternalBaseLibBitFieldAndUint (
102 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
103 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
105 return Operand
& ~((~AndData
<< StartBit
) & ~((UINTN
)-2 << EndBit
));
109 Returns a bit field from an 8-bit value.
111 Returns the bitfield specified by the StartBit and the EndBit from Operand.
113 If 8-bit operations are not supported, then ASSERT().
114 If StartBit is greater than 7, then ASSERT().
115 If EndBit is greater than 7, then ASSERT().
116 If EndBit is less than StartBit, then ASSERT().
118 @param Operand Operand on which to perform the bitfield operation.
119 @param StartBit The ordinal of the least significant bit in the bit field.
121 @param EndBit The ordinal of the most significant bit in the bit field.
124 @return The bit field read.
136 ASSERT (StartBit
<= EndBit
);
137 return (UINT8
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
141 Writes a bit field to an 8-bit value, and returns the result.
143 Writes Value to the bit field specified by the StartBit and the EndBit in
144 Operand. All other bits in Operand are preserved. The new 8-bit value is
147 If 8-bit operations are not supported, then ASSERT().
148 If StartBit is greater than 7, then ASSERT().
149 If EndBit is greater than 7, then ASSERT().
150 If EndBit is less than StartBit, then ASSERT().
152 @param Operand Operand on which to perform the bitfield operation.
153 @param StartBit The ordinal of the least significant bit in the bit field.
155 @param EndBit The ordinal of the most significant bit in the bit field.
157 @param Value New value of the bit field.
159 @return The new 8-bit value.
172 ASSERT (StartBit
<= EndBit
);
173 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
177 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
180 Performs a bitwise OR between the bit field specified by StartBit
181 and EndBit in Operand and the value specified by OrData. All other bits in
182 Operand are preserved. The new 8-bit value is returned.
184 If 8-bit operations are not supported, then ASSERT().
185 If StartBit is greater than 7, then ASSERT().
186 If EndBit is greater than 7, then ASSERT().
187 If EndBit is less than StartBit, then ASSERT().
189 @param Operand Operand on which to perform the bitfield operation.
190 @param StartBit The ordinal of the least significant bit in the bit field.
192 @param EndBit The ordinal of the most significant bit in the bit field.
194 @param OrData The value to OR with the read value from the value
196 @return The new 8-bit value.
209 ASSERT (StartBit
<= EndBit
);
210 return (UINT8
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
214 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
217 Performs a bitwise AND between the bit field specified by StartBit and EndBit
218 in Operand and the value specified by AndData. All other bits in Operand are
219 preserved. The new 8-bit value is returned.
221 If 8-bit operations are not supported, then ASSERT().
222 If StartBit is greater than 7, then ASSERT().
223 If EndBit is greater than 7, then ASSERT().
224 If EndBit is less than StartBit, then ASSERT().
226 @param Operand Operand on which to perform the bitfield operation.
227 @param StartBit The ordinal of the least significant bit in the bit field.
229 @param EndBit The ordinal of the most significant bit in the bit field.
231 @param AndData The value to AND with the read value from the value.
233 @return The new 8-bit value.
246 ASSERT (StartBit
<= EndBit
);
247 return (UINT8
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
251 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
252 bitwise OR, and returns the result.
254 Performs a bitwise AND between the bit field specified by StartBit and EndBit
255 in Operand and the value specified by AndData, followed by a bitwise
256 OR with value specified by OrData. All other bits in Operand are
257 preserved. The new 8-bit value is returned.
259 If 8-bit operations are not supported, then ASSERT().
260 If StartBit is greater than 7, then ASSERT().
261 If EndBit is greater than 7, then ASSERT().
262 If EndBit is less than StartBit, then ASSERT().
264 @param Operand Operand on which to perform the bitfield operation.
265 @param StartBit The ordinal of the least significant bit in the bit field.
267 @param EndBit The ordinal of the most significant bit in the bit field.
269 @param AndData The value to AND with the read value from the value.
270 @param OrData The value to OR with the result of the AND operation.
272 @return The new 8-bit value.
286 ASSERT (StartBit
<= EndBit
);
288 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
296 Returns a bit field from a 16-bit value.
298 Returns the bitfield specified by the StartBit and the EndBit from Operand.
300 If 16-bit operations are not supported, then ASSERT().
301 If StartBit is greater than 15, then ASSERT().
302 If EndBit is greater than 15, then ASSERT().
303 If EndBit is less than StartBit, then ASSERT().
305 @param Operand Operand on which to perform the bitfield operation.
306 @param StartBit The ordinal of the least significant bit in the bit field.
308 @param EndBit The ordinal of the most significant bit in the bit field.
311 @return The bit field read.
322 ASSERT (EndBit
< 16);
323 ASSERT (StartBit
<= EndBit
);
324 return (UINT16
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
328 Writes a bit field to a 16-bit value, and returns the result.
330 Writes Value to the bit field specified by the StartBit and the EndBit in
331 Operand. All other bits in Operand are preserved. The new 16-bit value is
334 If 16-bit operations are not supported, then ASSERT().
335 If StartBit is greater than 15, then ASSERT().
336 If EndBit is greater than 15, then ASSERT().
337 If EndBit is less than StartBit, then ASSERT().
339 @param Operand Operand on which to perform the bitfield operation.
340 @param StartBit The ordinal of the least significant bit in the bit field.
342 @param EndBit The ordinal of the most significant bit in the bit field.
344 @param Value New value of the bit field.
346 @return The new 16-bit value.
358 ASSERT (EndBit
< 16);
359 ASSERT (StartBit
<= EndBit
);
360 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
364 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
367 Performs a bitwise OR between the bit field specified by StartBit
368 and EndBit in Operand and the value specified by OrData. All other bits in
369 Operand are preserved. The new 16-bit value is returned.
371 If 16-bit operations are not supported, then ASSERT().
372 If StartBit is greater than 15, then ASSERT().
373 If EndBit is greater than 15, then ASSERT().
374 If EndBit is less than StartBit, then ASSERT().
376 @param Operand Operand on which to perform the bitfield operation.
377 @param StartBit The ordinal of the least significant bit in the bit field.
379 @param EndBit The ordinal of the most significant bit in the bit field.
381 @param OrData The value to OR with the read value from the value
383 @return The new 16-bit value.
395 ASSERT (EndBit
< 16);
396 ASSERT (StartBit
<= EndBit
);
397 return (UINT16
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
401 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
404 Performs a bitwise AND between the bit field specified by StartBit and EndBit
405 in Operand and the value specified by AndData. All other bits in Operand are
406 preserved. The new 16-bit value is returned.
408 If 16-bit operations are not supported, then ASSERT().
409 If StartBit is greater than 15, then ASSERT().
410 If EndBit is greater than 15, then ASSERT().
411 If EndBit is less than StartBit, then ASSERT().
413 @param Operand Operand on which to perform the bitfield operation.
414 @param StartBit The ordinal of the least significant bit in the bit field.
416 @param EndBit The ordinal of the most significant bit in the bit field.
418 @param AndData The value to AND with the read value from the value
420 @return The new 16-bit value.
432 ASSERT (EndBit
< 16);
433 ASSERT (StartBit
<= EndBit
);
434 return (UINT16
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
438 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
439 bitwise OR, and returns the result.
441 Performs a bitwise AND between the bit field specified by StartBit and EndBit
442 in Operand and the value specified by AndData, followed by a bitwise
443 OR with value specified by OrData. All other bits in Operand are
444 preserved. The new 16-bit value is returned.
446 If 16-bit operations are not supported, then ASSERT().
447 If StartBit is greater than 15, then ASSERT().
448 If EndBit is greater than 15, then ASSERT().
449 If EndBit is less than StartBit, then ASSERT().
451 @param Operand Operand on which to perform the bitfield operation.
452 @param StartBit The ordinal of the least significant bit in the bit field.
454 @param EndBit The ordinal of the most significant bit in the bit field.
456 @param AndData The value to AND with the read value from the value.
457 @param OrData The value to OR with the result of the AND operation.
459 @return The new 16-bit value.
464 BitFieldAndThenOr16 (
472 ASSERT (EndBit
< 16);
473 ASSERT (StartBit
<= EndBit
);
474 return BitFieldOr16 (
475 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
483 Returns a bit field from a 32-bit value.
485 Returns the bitfield specified by the StartBit and the EndBit from Operand.
487 If 32-bit operations are not supported, then ASSERT().
488 If StartBit is greater than 31, then ASSERT().
489 If EndBit is greater than 31, then ASSERT().
490 If EndBit is less than StartBit, then ASSERT().
492 @param Operand Operand on which to perform the bitfield operation.
493 @param StartBit The ordinal of the least significant bit in the bit field.
495 @param EndBit The ordinal of the most significant bit in the bit field.
498 @return The bit field read.
509 ASSERT (EndBit
< 32);
510 ASSERT (StartBit
<= EndBit
);
511 return (UINT32
)InternalBaseLibBitFieldReadUint (Operand
, StartBit
, EndBit
);
515 Writes a bit field to a 32-bit value, and returns the result.
517 Writes Value to the bit field specified by the StartBit and the EndBit in
518 Operand. All other bits in Operand are preserved. The new 32-bit value is
521 If 32-bit operations are not supported, then ASSERT().
522 If StartBit is greater than 31, then ASSERT().
523 If EndBit is greater than 31, then ASSERT().
524 If EndBit is less than StartBit, then ASSERT().
526 @param Operand Operand on which to perform the bitfield operation.
527 @param StartBit The ordinal of the least significant bit in the bit field.
529 @param EndBit The ordinal of the most significant bit in the bit field.
531 @param Value New value of the bit field.
533 @return The new 32-bit value.
545 ASSERT (EndBit
< 32);
546 ASSERT (StartBit
<= EndBit
);
547 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
551 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
554 Performs a bitwise OR between the bit field specified by StartBit
555 and EndBit in Operand and the value specified by OrData. All other bits in
556 Operand are preserved. The new 32-bit value is returned.
558 If 32-bit operations are not supported, then ASSERT().
559 If StartBit is greater than 31, then ASSERT().
560 If EndBit is greater than 31, then ASSERT().
561 If EndBit is less than StartBit, then ASSERT().
563 @param Operand Operand on which to perform the bitfield operation.
564 @param StartBit The ordinal of the least significant bit in the bit field.
566 @param EndBit The ordinal of the most significant bit in the bit field.
568 @param OrData The value to OR with the read value from the value
570 @return The new 32-bit value.
582 ASSERT (EndBit
< 32);
583 ASSERT (StartBit
<= EndBit
);
584 return (UINT32
)InternalBaseLibBitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
588 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
591 Performs a bitwise AND between the bit field specified by StartBit and EndBit
592 in Operand and the value specified by AndData. All other bits in Operand are
593 preserved. The new 32-bit value is returned.
595 If 32-bit operations are not supported, then ASSERT().
596 If StartBit is greater than 31, then ASSERT().
597 If EndBit is greater than 31, then ASSERT().
598 If EndBit is less than StartBit, then ASSERT().
600 @param Operand Operand on which to perform the bitfield operation.
601 @param StartBit The ordinal of the least significant bit in the bit field.
603 @param EndBit The ordinal of the most significant bit in the bit field.
605 @param AndData The value to AND with the read value from the value
607 @return The new 32-bit value.
619 ASSERT (EndBit
< 32);
620 ASSERT (StartBit
<= EndBit
);
621 return (UINT32
)InternalBaseLibBitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
625 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
626 bitwise OR, and returns the result.
628 Performs a bitwise AND between the bit field specified by StartBit and EndBit
629 in Operand and the value specified by AndData, followed by a bitwise
630 OR with value specified by OrData. All other bits in Operand are
631 preserved. The new 32-bit value is returned.
633 If 32-bit operations are not supported, then ASSERT().
634 If StartBit is greater than 31, then ASSERT().
635 If EndBit is greater than 31, then ASSERT().
636 If EndBit is less than StartBit, then ASSERT().
638 @param Operand Operand on which to perform the bitfield operation.
639 @param StartBit The ordinal of the least significant bit in the bit field.
641 @param EndBit The ordinal of the most significant bit in the bit field.
643 @param AndData The value to AND with the read value from the value.
644 @param OrData The value to OR with the result of the AND operation.
646 @return The new 32-bit value.
651 BitFieldAndThenOr32 (
659 ASSERT (EndBit
< 32);
660 ASSERT (StartBit
<= EndBit
);
661 return BitFieldOr32 (
662 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
670 Returns a bit field from a 64-bit value.
672 Returns the bitfield specified by the StartBit and the EndBit from Operand.
674 If 64-bit operations are not supported, then ASSERT().
675 If StartBit is greater than 63, then ASSERT().
676 If EndBit is greater than 63, then ASSERT().
677 If EndBit is less than StartBit, then ASSERT().
679 @param Operand Operand on which to perform the bitfield operation.
680 @param StartBit The ordinal of the least significant bit in the bit field.
682 @param EndBit The ordinal of the most significant bit in the bit field.
685 @return The bit field read.
696 ASSERT (EndBit
< 64);
697 ASSERT (StartBit
<= EndBit
);
698 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
702 Writes a bit field to a 64-bit value, and returns the result.
704 Writes Value to the bit field specified by the StartBit and the EndBit in
705 Operand. All other bits in Operand are preserved. The new 64-bit value is
708 If 64-bit operations are not supported, then ASSERT().
709 If StartBit is greater than 63, then ASSERT().
710 If EndBit is greater than 63, then ASSERT().
711 If EndBit is less than StartBit, then ASSERT().
713 @param Operand Operand on which to perform the bitfield operation.
714 @param StartBit The ordinal of the least significant bit in the bit field.
716 @param EndBit The ordinal of the most significant bit in the bit field.
718 @param Value New value of the bit field.
720 @return The new 64-bit value.
732 ASSERT (EndBit
< 64);
733 ASSERT (StartBit
<= EndBit
);
734 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
738 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
741 Performs a bitwise OR between the bit field specified by StartBit
742 and EndBit in Operand and the value specified by OrData. All other bits in
743 Operand are preserved. The new 64-bit value is returned.
745 If 64-bit operations are not supported, then ASSERT().
746 If StartBit is greater than 63, then ASSERT().
747 If EndBit is greater than 63, then ASSERT().
748 If EndBit is less than StartBit, then ASSERT().
750 @param Operand Operand on which to perform the bitfield operation.
751 @param StartBit The ordinal of the least significant bit in the bit field.
753 @param EndBit The ordinal of the most significant bit in the bit field.
755 @param OrData The value to OR with the read value from the value
757 @return The new 64-bit value.
772 ASSERT (EndBit
< 64);
773 ASSERT (StartBit
<= EndBit
);
775 Value1
= LShiftU64 (OrData
, StartBit
);
776 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
778 return Operand
| (Value1
& ~Value2
);
782 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
785 Performs a bitwise AND between the bit field specified by StartBit and EndBit
786 in Operand and the value specified by AndData. All other bits in Operand are
787 preserved. The new 64-bit value is returned.
789 If 64-bit operations are not supported, then ASSERT().
790 If StartBit is greater than 63, then ASSERT().
791 If EndBit is greater than 63, then ASSERT().
792 If EndBit is less than StartBit, then ASSERT().
794 @param Operand Operand on which to perform the bitfield operation.
795 @param StartBit The ordinal of the least significant bit in the bit field.
797 @param EndBit The ordinal of the most significant bit in the bit field.
799 @param AndData The value to AND with the read value from the value
801 @return The new 64-bit value.
816 ASSERT (EndBit
< 64);
817 ASSERT (StartBit
<= EndBit
);
819 Value1
= LShiftU64 (~AndData
, StartBit
);
820 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
822 return Operand
& ~(Value1
& ~Value2
);
826 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
827 bitwise OR, and returns the result.
829 Performs a bitwise AND between the bit field specified by StartBit and EndBit
830 in Operand and the value specified by AndData, followed by a bitwise
831 OR with value specified by OrData. All other bits in Operand are
832 preserved. The new 64-bit value is returned.
834 If 64-bit operations are not supported, then ASSERT().
835 If StartBit is greater than 63, then ASSERT().
836 If EndBit is greater than 63, then ASSERT().
837 If EndBit is less than StartBit, then ASSERT().
839 @param Operand Operand on which to perform the bitfield operation.
840 @param StartBit The ordinal of the least significant bit in the bit field.
842 @param EndBit The ordinal of the most significant bit in the bit field.
844 @param AndData The value to AND with the read value from the value.
845 @param OrData The value to OR with the result of the AND operation.
847 @return The new 64-bit value.
852 BitFieldAndThenOr64 (
860 ASSERT (EndBit
< 64);
861 ASSERT (StartBit
<= EndBit
);
862 return BitFieldOr64 (
863 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),