]>
git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/BitField.c
2 Bit field functions of BaseLib.
4 Copyright (c) 2006, 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.
16 // Include common header file for this module.
20 #include "BaseLibInternals.h"
23 Worker function that returns a bit field from Operand
25 Returns the bitfield specified by the StartBit and the EndBit from Operand.
27 @param Operand Operand on which to perform the bitfield operation.
28 @param StartBit The ordinal of the least significant bit in the bit field.
29 @param EndBit The ordinal of the most significant bit in the bit field.
31 @return The bit field read.
37 IN
unsigned int Operand
,
43 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
44 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
46 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
50 Worker function that reads a bit field from Operand, performs a bitwise OR,
51 and returns the result.
53 Performs a bitwise OR between the bit field specified by StartBit and EndBit
54 in Operand and the value specified by AndData. All other bits in Operand are
55 preserved. The new value is returned.
57 @param Operand Operand on which to perform the bitfield operation.
58 @param StartBit The ordinal of the least significant bit in the bit field.
59 @param EndBit The ordinal of the most significant bit in the bit field.
60 @param OrData The value to OR with the read value from the value
62 @return The new value.
68 IN
unsigned int Operand
,
71 IN
unsigned int OrData
75 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
76 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
78 return Operand
| ((OrData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
82 Worker function that reads a bit field from Operand, performs a bitwise AND,
83 and returns the result.
85 Performs a bitwise AND between the bit field specified by StartBit and EndBit
86 in Operand and the value specified by AndData. All other bits in Operand are
87 preserved. The new value is returned.
89 @param Operand Operand on which to perform the bitfield operation.
90 @param StartBit The ordinal of the least significant bit in the bit field.
91 @param EndBit The ordinal of the most significant bit in the bit field.
92 @param AndData The value to And with the read value from the value
94 @return The new value.
100 IN
unsigned int Operand
,
103 IN
unsigned int AndData
107 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
108 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
110 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
114 Returns a bit field from an 8-bit value.
116 Returns the bitfield specified by the StartBit and the EndBit from Operand.
118 If 8-bit operations are not supported, then ASSERT().
119 If StartBit is greater than 7, then ASSERT().
120 If EndBit is greater than 7, then ASSERT().
121 If EndBit is less than StartBit, then ASSERT().
123 @param Operand Operand on which to perform the bitfield operation.
124 @param StartBit The ordinal of the least significant bit in the bit field.
126 @param EndBit The ordinal of the most significant bit in the bit field.
129 @return The bit field read.
140 ASSERT (EndBit
< sizeof (Operand
) * 8);
141 ASSERT (StartBit
<= EndBit
);
142 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
146 Writes a bit field to an 8-bit value, and returns the result.
148 Writes Value to the bit field specified by the StartBit and the EndBit in
149 Operand. All other bits in Operand are preserved. The new 8-bit value is
152 If 8-bit operations are not supported, then ASSERT().
153 If StartBit is greater than 7, then ASSERT().
154 If EndBit is greater than 7, then ASSERT().
155 If EndBit is less than StartBit, then ASSERT().
157 @param Operand Operand on which to perform the bitfield operation.
158 @param StartBit The ordinal of the least significant bit in the bit field.
160 @param EndBit The ordinal of the most significant bit in the bit field.
162 @param Value New value of the bit field.
164 @return The new 8-bit value.
176 ASSERT (EndBit
< sizeof (Operand
) * 8);
177 ASSERT (StartBit
<= EndBit
);
178 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
182 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
185 Performs a bitwise inclusive OR between the bit field specified by StartBit
186 and EndBit in Operand and the value specified by OrData. All other bits in
187 Operand are preserved. The new 8-bit value is returned.
189 If 8-bit operations are not supported, then ASSERT().
190 If StartBit is greater than 7, then ASSERT().
191 If EndBit is greater than 7, then ASSERT().
192 If EndBit is less than StartBit, then ASSERT().
194 @param Operand Operand on which to perform the bitfield operation.
195 @param StartBit The ordinal of the least significant bit in the bit field.
197 @param EndBit The ordinal of the most significant bit in the bit field.
199 @param OrData The value to OR with the read value from the value
201 @return The new 8-bit value.
213 ASSERT (EndBit
< sizeof (Operand
) * 8);
214 ASSERT (StartBit
<= EndBit
);
215 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
219 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
222 Performs a bitwise AND between the bit field specified by StartBit and EndBit
223 in Operand and the value specified by AndData. All other bits in Operand are
224 preserved. The new 8-bit value is returned.
226 If 8-bit operations are not supported, then ASSERT().
227 If StartBit is greater than 7, then ASSERT().
228 If EndBit is greater than 7, then ASSERT().
229 If EndBit is less than StartBit, then ASSERT().
231 @param Operand Operand on which to perform the bitfield operation.
232 @param StartBit The ordinal of the least significant bit in the bit field.
234 @param EndBit The ordinal of the most significant bit in the bit field.
236 @param AndData The value to AND with the read value from the value.
238 @return The new 8-bit value.
250 ASSERT (EndBit
< sizeof (Operand
) * 8);
251 ASSERT (StartBit
<= EndBit
);
252 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
256 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
257 bitwise OR, and returns the result.
259 Performs a bitwise AND between the bit field specified by StartBit and EndBit
260 in Operand and the value specified by AndData, followed by a bitwise
261 inclusive OR with value specified by OrData. All other bits in Operand are
262 preserved. The new 8-bit value is returned.
264 If 8-bit operations are not supported, then ASSERT().
265 If StartBit is greater than 7, then ASSERT().
266 If EndBit is greater than 7, then ASSERT().
267 If EndBit is less than StartBit, then ASSERT().
269 @param Operand Operand on which to perform the bitfield operation.
270 @param StartBit The ordinal of the least significant bit in the bit field.
272 @param EndBit The ordinal of the most significant bit in the bit field.
274 @param AndData The value to AND with the read value from the value.
275 @param OrData The value to OR with the result of the AND operation.
277 @return The new 8-bit value.
290 ASSERT (EndBit
< sizeof (Operand
) * 8);
291 ASSERT (StartBit
<= EndBit
);
293 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
301 Returns a bit field from a 16-bit value.
303 Returns the bitfield specified by the StartBit and the EndBit from Operand.
305 If 16-bit operations are not supported, then ASSERT().
306 If StartBit is greater than 15, then ASSERT().
307 If EndBit is greater than 15, then ASSERT().
308 If EndBit is less than StartBit, then ASSERT().
310 @param Operand Operand on which to perform the bitfield operation.
311 @param StartBit The ordinal of the least significant bit in the bit field.
313 @param EndBit The ordinal of the most significant bit in the bit field.
316 @return The bit field read.
327 ASSERT (EndBit
< sizeof (Operand
) * 8);
328 ASSERT (StartBit
<= EndBit
);
329 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
333 Writes a bit field to a 16-bit value, and returns the result.
335 Writes Value to the bit field specified by the StartBit and the EndBit in
336 Operand. All other bits in Operand are preserved. The new 16-bit value is
339 If 16-bit operations are not supported, then ASSERT().
340 If StartBit is greater than 15, then ASSERT().
341 If EndBit is greater than 15, then ASSERT().
342 If EndBit is less than StartBit, then ASSERT().
344 @param Operand Operand on which to perform the bitfield operation.
345 @param StartBit The ordinal of the least significant bit in the bit field.
347 @param EndBit The ordinal of the most significant bit in the bit field.
349 @param Value New value of the bit field.
351 @return The new 16-bit value.
363 ASSERT (EndBit
< sizeof (Operand
) * 8);
364 ASSERT (StartBit
<= EndBit
);
365 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
369 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
372 Performs a bitwise inclusive OR between the bit field specified by StartBit
373 and EndBit in Operand and the value specified by OrData. All other bits in
374 Operand are preserved. The new 16-bit value is returned.
376 If 16-bit operations are not supported, then ASSERT().
377 If StartBit is greater than 15, then ASSERT().
378 If EndBit is greater than 15, then ASSERT().
379 If EndBit is less than StartBit, then ASSERT().
381 @param Operand Operand on which to perform the bitfield operation.
382 @param StartBit The ordinal of the least significant bit in the bit field.
384 @param EndBit The ordinal of the most significant bit in the bit field.
386 @param OrData The value to OR with the read value from the value
388 @return The new 16-bit value.
400 ASSERT (EndBit
< sizeof (Operand
) * 8);
401 ASSERT (StartBit
<= EndBit
);
402 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
406 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
409 Performs a bitwise AND between the bit field specified by StartBit and EndBit
410 in Operand and the value specified by AndData. All other bits in Operand are
411 preserved. The new 16-bit value is returned.
413 If 16-bit operations are not supported, then ASSERT().
414 If StartBit is greater than 15, then ASSERT().
415 If EndBit is greater than 15, then ASSERT().
416 If EndBit is less than StartBit, then ASSERT().
418 @param Operand Operand on which to perform the bitfield operation.
419 @param StartBit The ordinal of the least significant bit in the bit field.
421 @param EndBit The ordinal of the most significant bit in the bit field.
423 @param AndData The value to AND with the read value from the value
425 @return The new 16-bit value.
437 ASSERT (EndBit
< sizeof (Operand
) * 8);
438 ASSERT (StartBit
<= EndBit
);
439 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
443 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
444 bitwise OR, and returns the result.
446 Performs a bitwise AND between the bit field specified by StartBit and EndBit
447 in Operand and the value specified by AndData, followed by a bitwise
448 inclusive OR with value specified by OrData. All other bits in Operand are
449 preserved. The new 16-bit value is returned.
451 If 16-bit operations are not supported, then ASSERT().
452 If StartBit is greater than 15, then ASSERT().
453 If EndBit is greater than 15, then ASSERT().
454 If EndBit is less than StartBit, then ASSERT().
456 @param Operand Operand on which to perform the bitfield operation.
457 @param StartBit The ordinal of the least significant bit in the bit field.
459 @param EndBit The ordinal of the most significant bit in the bit field.
461 @param AndData The value to AND with the read value from the value.
462 @param OrData The value to OR with the result of the AND operation.
464 @return The new 16-bit value.
469 BitFieldAndThenOr16 (
477 ASSERT (EndBit
< sizeof (Operand
) * 8);
478 ASSERT (StartBit
<= EndBit
);
479 return BitFieldOr16 (
480 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
488 Returns a bit field from a 32-bit value.
490 Returns the bitfield specified by the StartBit and the EndBit from Operand.
492 If 32-bit operations are not supported, then ASSERT().
493 If StartBit is greater than 31, then ASSERT().
494 If EndBit is greater than 31, then ASSERT().
495 If EndBit is less than StartBit, then ASSERT().
497 @param Operand Operand on which to perform the bitfield operation.
498 @param StartBit The ordinal of the least significant bit in the bit field.
500 @param EndBit The ordinal of the most significant bit in the bit field.
503 @return The bit field read.
514 ASSERT (EndBit
< sizeof (Operand
) * 8);
515 ASSERT (StartBit
<= EndBit
);
516 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
520 Writes a bit field to a 32-bit value, and returns the result.
522 Writes Value to the bit field specified by the StartBit and the EndBit in
523 Operand. All other bits in Operand are preserved. The new 32-bit value is
526 If 32-bit operations are not supported, then ASSERT().
527 If StartBit is greater than 31, then ASSERT().
528 If EndBit is greater than 31, then ASSERT().
529 If EndBit is less than StartBit, then ASSERT().
531 @param Operand Operand on which to perform the bitfield operation.
532 @param StartBit The ordinal of the least significant bit in the bit field.
534 @param EndBit The ordinal of the most significant bit in the bit field.
536 @param Value New value of the bit field.
538 @return The new 32-bit value.
550 ASSERT (EndBit
< sizeof (Operand
) * 8);
551 ASSERT (StartBit
<= EndBit
);
552 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
556 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
559 Performs a bitwise inclusive OR between the bit field specified by StartBit
560 and EndBit in Operand and the value specified by OrData. All other bits in
561 Operand are preserved. The new 32-bit value is returned.
563 If 32-bit operations are not supported, then ASSERT().
564 If StartBit is greater than 31, then ASSERT().
565 If EndBit is greater than 31, then ASSERT().
566 If EndBit is less than StartBit, then ASSERT().
568 @param Operand Operand on which to perform the bitfield operation.
569 @param StartBit The ordinal of the least significant bit in the bit field.
571 @param EndBit The ordinal of the most significant bit in the bit field.
573 @param OrData The value to OR with the read value from the value
575 @return The new 32-bit value.
587 ASSERT (EndBit
< sizeof (Operand
) * 8);
588 ASSERT (StartBit
<= EndBit
);
589 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
593 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
596 Performs a bitwise AND between the bit field specified by StartBit and EndBit
597 in Operand and the value specified by AndData. All other bits in Operand are
598 preserved. The new 32-bit value is returned.
600 If 32-bit operations are not supported, then ASSERT().
601 If StartBit is greater than 31, then ASSERT().
602 If EndBit is greater than 31, then ASSERT().
603 If EndBit is less than StartBit, then ASSERT().
605 @param Operand Operand on which to perform the bitfield operation.
606 @param StartBit The ordinal of the least significant bit in the bit field.
608 @param EndBit The ordinal of the most significant bit in the bit field.
610 @param AndData The value to AND with the read value from the value
612 @return The new 32-bit value.
624 ASSERT (EndBit
< sizeof (Operand
) * 8);
625 ASSERT (StartBit
<= EndBit
);
626 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
630 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
631 bitwise OR, and returns the result.
633 Performs a bitwise AND between the bit field specified by StartBit and EndBit
634 in Operand and the value specified by AndData, followed by a bitwise
635 inclusive OR with value specified by OrData. All other bits in Operand are
636 preserved. The new 32-bit value is returned.
638 If 32-bit operations are not supported, then ASSERT().
639 If StartBit is greater than 31, then ASSERT().
640 If EndBit is greater than 31, then ASSERT().
641 If EndBit is less than StartBit, then ASSERT().
643 @param Operand Operand on which to perform the bitfield operation.
644 @param StartBit The ordinal of the least significant bit in the bit field.
646 @param EndBit The ordinal of the most significant bit in the bit field.
648 @param AndData The value to AND with the read value from the value.
649 @param OrData The value to OR with the result of the AND operation.
651 @return The new 32-bit value.
656 BitFieldAndThenOr32 (
664 ASSERT (EndBit
< sizeof (Operand
) * 8);
665 ASSERT (StartBit
<= EndBit
);
666 return BitFieldOr32 (
667 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
675 Returns a bit field from a 64-bit value.
677 Returns the bitfield specified by the StartBit and the EndBit from Operand.
679 If 64-bit operations are not supported, then ASSERT().
680 If StartBit is greater than 63, then ASSERT().
681 If EndBit is greater than 63, then ASSERT().
682 If EndBit is less than StartBit, then ASSERT().
684 @param Operand Operand on which to perform the bitfield operation.
685 @param StartBit The ordinal of the least significant bit in the bit field.
687 @param EndBit The ordinal of the most significant bit in the bit field.
690 @return The bit field read.
701 ASSERT (EndBit
< sizeof (Operand
) * 8);
702 ASSERT (StartBit
<= EndBit
);
703 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
707 Writes a bit field to a 64-bit value, and returns the result.
709 Writes Value to the bit field specified by the StartBit and the EndBit in
710 Operand. All other bits in Operand are preserved. The new 64-bit value is
713 If 64-bit operations are not supported, then ASSERT().
714 If StartBit is greater than 63, then ASSERT().
715 If EndBit is greater than 63, then ASSERT().
716 If EndBit is less than StartBit, then ASSERT().
718 @param Operand Operand on which to perform the bitfield operation.
719 @param StartBit The ordinal of the least significant bit in the bit field.
721 @param EndBit The ordinal of the most significant bit in the bit field.
723 @param Value New value of the bit field.
725 @return The new 64-bit value.
737 ASSERT (EndBit
< sizeof (Operand
) * 8);
738 ASSERT (StartBit
<= EndBit
);
739 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
743 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
746 Performs a bitwise inclusive OR between the bit field specified by StartBit
747 and EndBit in Operand and the value specified by OrData. All other bits in
748 Operand are preserved. The new 64-bit value is returned.
750 If 64-bit operations are not supported, then ASSERT().
751 If StartBit is greater than 63, then ASSERT().
752 If EndBit is greater than 63, then ASSERT().
753 If EndBit is less than StartBit, then ASSERT().
755 @param Operand Operand on which to perform the bitfield operation.
756 @param StartBit The ordinal of the least significant bit in the bit field.
758 @param EndBit The ordinal of the most significant bit in the bit field.
760 @param OrData The value to OR with the read value from the value
762 @return The new 64-bit value.
777 ASSERT (EndBit
< sizeof (Operand
) * 8);
778 ASSERT (StartBit
<= EndBit
);
780 Value1
= LShiftU64 (OrData
, StartBit
);
781 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
783 return Operand
| (Value1
& ~Value2
);
787 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
790 Performs a bitwise AND between the bit field specified by StartBit and EndBit
791 in Operand and the value specified by AndData. All other bits in Operand are
792 preserved. The new 64-bit value is returned.
794 If 64-bit operations are not supported, then ASSERT().
795 If StartBit is greater than 63, then ASSERT().
796 If EndBit is greater than 63, then ASSERT().
797 If EndBit is less than StartBit, then ASSERT().
799 @param Operand Operand on which to perform the bitfield operation.
800 @param StartBit The ordinal of the least significant bit in the bit field.
802 @param EndBit The ordinal of the most significant bit in the bit field.
804 @param AndData The value to AND with the read value from the value
806 @return The new 64-bit value.
821 ASSERT (EndBit
< sizeof (Operand
) * 8);
822 ASSERT (StartBit
<= EndBit
);
824 Value1
= LShiftU64 (~AndData
, StartBit
);
825 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
827 return Operand
& ~(Value1
& ~Value2
);
831 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
832 bitwise OR, and returns the result.
834 Performs a bitwise AND between the bit field specified by StartBit and EndBit
835 in Operand and the value specified by AndData, followed by a bitwise
836 inclusive OR with value specified by OrData. All other bits in Operand are
837 preserved. The new 64-bit value is returned.
839 If 64-bit operations are not supported, then ASSERT().
840 If StartBit is greater than 63, then ASSERT().
841 If EndBit is greater than 63, then ASSERT().
842 If EndBit is less than StartBit, then ASSERT().
844 @param Operand Operand on which to perform the bitfield operation.
845 @param StartBit The ordinal of the least significant bit in the bit field.
847 @param EndBit The ordinal of the most significant bit in the bit field.
849 @param AndData The value to AND with the read value from the value.
850 @param OrData The value to OR with the result of the AND operation.
852 @return The new 64-bit value.
857 BitFieldAndThenOr64 (
865 ASSERT (EndBit
< sizeof (Operand
) * 8);
866 ASSERT (StartBit
<= EndBit
);
867 return BitFieldOr64 (
868 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),