]>
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.
13 Module Name: BitField.c
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 IN
unsigned int Operand
,
37 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
38 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
40 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
44 Worker function that reads a bit field from Operand, performs a bitwise OR,
45 and returns the result.
47 Performs a bitwise OR between the bit field specified by StartBit and EndBit
48 in Operand and the value specified by AndData. All other bits in Operand are
49 preserved. The new value is returned.
51 @param Operand Operand on which to perform the bitfield operation.
52 @param StartBit The ordinal of the least significant bit in the bit field.
53 @param EndBit The ordinal of the most significant bit in the bit field.
54 @param OrData The value to OR with the read value from the value
56 @return The new value.
61 IN
unsigned int Operand
,
64 IN
unsigned int OrData
68 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
69 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
71 return Operand
| ((OrData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
75 Worker function that reads a bit field from Operand, performs a bitwise AND,
76 and returns the result.
78 Performs a bitwise AND between the bit field specified by StartBit and EndBit
79 in Operand and the value specified by AndData. All other bits in Operand are
80 preserved. The new value is returned.
82 @param Operand Operand on which to perform the bitfield operation.
83 @param StartBit The ordinal of the least significant bit in the bit field.
84 @param EndBit The ordinal of the most significant bit in the bit field.
85 @param AndData The value to And with the read value from the value
87 @return The new value.
92 IN
unsigned int Operand
,
95 IN
unsigned int AndData
99 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
100 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
102 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
106 Returns a bit field from an 8-bit value.
108 Returns the bitfield specified by the StartBit and the EndBit from Operand.
110 If 8-bit operations are not supported, then ASSERT().
111 If StartBit is greater than 7, then ASSERT().
112 If EndBit is greater than 7, then ASSERT().
113 If EndBit is less than StartBit, then ASSERT().
115 @param Operand Operand on which to perform the bitfield operation.
116 @param StartBit The ordinal of the least significant bit in the bit field.
118 @param EndBit The ordinal of the most significant bit in the bit field.
121 @return The bit field read.
132 ASSERT (EndBit
< sizeof (Operand
) * 8);
133 ASSERT (StartBit
<= EndBit
);
134 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
138 Writes a bit field to an 8-bit value, and returns the result.
140 Writes Value to the bit field specified by the StartBit and the EndBit in
141 Operand. All other bits in Operand are preserved. The new 8-bit value is
144 If 8-bit operations are not supported, then ASSERT().
145 If StartBit is greater than 7, then ASSERT().
146 If EndBit is greater than 7, then ASSERT().
147 If EndBit is less than StartBit, then ASSERT().
149 @param Operand Operand on which to perform the bitfield operation.
150 @param StartBit The ordinal of the least significant bit in the bit field.
152 @param EndBit The ordinal of the most significant bit in the bit field.
154 @param Value New value of the bit field.
156 @return The new 8-bit value.
168 ASSERT (EndBit
< sizeof (Operand
) * 8);
169 ASSERT (StartBit
<= EndBit
);
170 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
174 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
177 Performs a bitwise inclusive OR between the bit field specified by StartBit
178 and EndBit in Operand and the value specified by OrData. All other bits in
179 Operand are preserved. The new 8-bit value is returned.
181 If 8-bit operations are not supported, then ASSERT().
182 If StartBit is greater than 7, then ASSERT().
183 If EndBit is greater than 7, then ASSERT().
184 If EndBit is less than StartBit, then ASSERT().
186 @param Operand Operand on which to perform the bitfield operation.
187 @param StartBit The ordinal of the least significant bit in the bit field.
189 @param EndBit The ordinal of the most significant bit in the bit field.
191 @param OrData The value to OR with the read value from the value
193 @return The new 8-bit value.
205 ASSERT (EndBit
< sizeof (Operand
) * 8);
206 ASSERT (StartBit
<= EndBit
);
207 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
211 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
214 Performs a bitwise AND between the bit field specified by StartBit and EndBit
215 in Operand and the value specified by AndData. All other bits in Operand are
216 preserved. The new 8-bit value is returned.
218 If 8-bit operations are not supported, then ASSERT().
219 If StartBit is greater than 7, then ASSERT().
220 If EndBit is greater than 7, then ASSERT().
221 If EndBit is less than StartBit, then ASSERT().
223 @param Operand Operand on which to perform the bitfield operation.
224 @param StartBit The ordinal of the least significant bit in the bit field.
226 @param EndBit The ordinal of the most significant bit in the bit field.
228 @param AndData The value to AND with the read value from the value.
230 @return The new 8-bit value.
242 ASSERT (EndBit
< sizeof (Operand
) * 8);
243 ASSERT (StartBit
<= EndBit
);
244 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
248 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
249 bitwise OR, and returns the result.
251 Performs a bitwise AND between the bit field specified by StartBit and EndBit
252 in Operand and the value specified by AndData, followed by a bitwise
253 inclusive OR with value specified by OrData. All other bits in Operand are
254 preserved. The new 8-bit value is returned.
256 If 8-bit operations are not supported, then ASSERT().
257 If StartBit is greater than 7, then ASSERT().
258 If EndBit is greater than 7, then ASSERT().
259 If EndBit is less than StartBit, then ASSERT().
261 @param Operand Operand on which to perform the bitfield operation.
262 @param StartBit The ordinal of the least significant bit in the bit field.
264 @param EndBit The ordinal of the most significant bit in the bit field.
266 @param AndData The value to AND with the read value from the value.
267 @param OrData The value to OR with the result of the AND operation.
269 @return The new 8-bit value.
282 ASSERT (EndBit
< sizeof (Operand
) * 8);
283 ASSERT (StartBit
<= EndBit
);
285 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
293 Returns a bit field from a 16-bit value.
295 Returns the bitfield specified by the StartBit and the EndBit from Operand.
297 If 16-bit operations are not supported, then ASSERT().
298 If StartBit is greater than 15, then ASSERT().
299 If EndBit is greater than 15, then ASSERT().
300 If EndBit is less than StartBit, then ASSERT().
302 @param Operand Operand on which to perform the bitfield operation.
303 @param StartBit The ordinal of the least significant bit in the bit field.
305 @param EndBit The ordinal of the most significant bit in the bit field.
308 @return The bit field read.
319 ASSERT (EndBit
< sizeof (Operand
) * 8);
320 ASSERT (StartBit
<= EndBit
);
321 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
325 Writes a bit field to a 16-bit value, and returns the result.
327 Writes Value to the bit field specified by the StartBit and the EndBit in
328 Operand. All other bits in Operand are preserved. The new 16-bit value is
331 If 16-bit operations are not supported, then ASSERT().
332 If StartBit is greater than 15, then ASSERT().
333 If EndBit is greater than 15, then ASSERT().
334 If EndBit is less than StartBit, then ASSERT().
336 @param Operand Operand on which to perform the bitfield operation.
337 @param StartBit The ordinal of the least significant bit in the bit field.
339 @param EndBit The ordinal of the most significant bit in the bit field.
341 @param Value New value of the bit field.
343 @return The new 16-bit value.
355 ASSERT (EndBit
< sizeof (Operand
) * 8);
356 ASSERT (StartBit
<= EndBit
);
357 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
361 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
364 Performs a bitwise inclusive OR between the bit field specified by StartBit
365 and EndBit in Operand and the value specified by OrData. All other bits in
366 Operand are preserved. The new 16-bit value is returned.
368 If 16-bit operations are not supported, then ASSERT().
369 If StartBit is greater than 15, then ASSERT().
370 If EndBit is greater than 15, then ASSERT().
371 If EndBit is less than StartBit, then ASSERT().
373 @param Operand Operand on which to perform the bitfield operation.
374 @param StartBit The ordinal of the least significant bit in the bit field.
376 @param EndBit The ordinal of the most significant bit in the bit field.
378 @param OrData The value to OR with the read value from the value
380 @return The new 16-bit value.
392 ASSERT (EndBit
< sizeof (Operand
) * 8);
393 ASSERT (StartBit
<= EndBit
);
394 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
398 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
401 Performs a bitwise AND between the bit field specified by StartBit and EndBit
402 in Operand and the value specified by AndData. All other bits in Operand are
403 preserved. The new 16-bit value is returned.
405 If 16-bit operations are not supported, then ASSERT().
406 If StartBit is greater than 15, then ASSERT().
407 If EndBit is greater than 15, then ASSERT().
408 If EndBit is less than StartBit, then ASSERT().
410 @param Operand Operand on which to perform the bitfield operation.
411 @param StartBit The ordinal of the least significant bit in the bit field.
413 @param EndBit The ordinal of the most significant bit in the bit field.
415 @param AndData The value to AND with the read value from the value
417 @return The new 16-bit value.
429 ASSERT (EndBit
< sizeof (Operand
) * 8);
430 ASSERT (StartBit
<= EndBit
);
431 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
435 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
436 bitwise OR, and returns the result.
438 Performs a bitwise AND between the bit field specified by StartBit and EndBit
439 in Operand and the value specified by AndData, followed by a bitwise
440 inclusive OR with value specified by OrData. All other bits in Operand are
441 preserved. The new 16-bit value is returned.
443 If 16-bit operations are not supported, then ASSERT().
444 If StartBit is greater than 15, then ASSERT().
445 If EndBit is greater than 15, then ASSERT().
446 If EndBit is less than StartBit, then ASSERT().
448 @param Operand Operand on which to perform the bitfield operation.
449 @param StartBit The ordinal of the least significant bit in the bit field.
451 @param EndBit The ordinal of the most significant bit in the bit field.
453 @param AndData The value to AND with the read value from the value.
454 @param OrData The value to OR with the result of the AND operation.
456 @return The new 16-bit value.
461 BitFieldAndThenOr16 (
469 ASSERT (EndBit
< sizeof (Operand
) * 8);
470 ASSERT (StartBit
<= EndBit
);
471 return BitFieldOr16 (
472 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
480 Returns a bit field from a 32-bit value.
482 Returns the bitfield specified by the StartBit and the EndBit from Operand.
484 If 32-bit operations are not supported, then ASSERT().
485 If StartBit is greater than 31, then ASSERT().
486 If EndBit is greater than 31, then ASSERT().
487 If EndBit is less than StartBit, then ASSERT().
489 @param Operand Operand on which to perform the bitfield operation.
490 @param StartBit The ordinal of the least significant bit in the bit field.
492 @param EndBit The ordinal of the most significant bit in the bit field.
495 @return The bit field read.
506 ASSERT (EndBit
< sizeof (Operand
) * 8);
507 ASSERT (StartBit
<= EndBit
);
508 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
512 Writes a bit field to a 32-bit value, and returns the result.
514 Writes Value to the bit field specified by the StartBit and the EndBit in
515 Operand. All other bits in Operand are preserved. The new 32-bit value is
518 If 32-bit operations are not supported, then ASSERT().
519 If StartBit is greater than 31, then ASSERT().
520 If EndBit is greater than 31, then ASSERT().
521 If EndBit is less than StartBit, then ASSERT().
523 @param Operand Operand on which to perform the bitfield operation.
524 @param StartBit The ordinal of the least significant bit in the bit field.
526 @param EndBit The ordinal of the most significant bit in the bit field.
528 @param Value New value of the bit field.
530 @return The new 32-bit value.
542 ASSERT (EndBit
< sizeof (Operand
) * 8);
543 ASSERT (StartBit
<= EndBit
);
544 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
548 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
551 Performs a bitwise inclusive OR between the bit field specified by StartBit
552 and EndBit in Operand and the value specified by OrData. All other bits in
553 Operand are preserved. The new 32-bit value is returned.
555 If 32-bit operations are not supported, then ASSERT().
556 If StartBit is greater than 31, then ASSERT().
557 If EndBit is greater than 31, then ASSERT().
558 If EndBit is less than StartBit, then ASSERT().
560 @param Operand Operand on which to perform the bitfield operation.
561 @param StartBit The ordinal of the least significant bit in the bit field.
563 @param EndBit The ordinal of the most significant bit in the bit field.
565 @param OrData The value to OR with the read value from the value
567 @return The new 32-bit value.
579 ASSERT (EndBit
< sizeof (Operand
) * 8);
580 ASSERT (StartBit
<= EndBit
);
581 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
585 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
588 Performs a bitwise AND between the bit field specified by StartBit and EndBit
589 in Operand and the value specified by AndData. All other bits in Operand are
590 preserved. The new 32-bit value is returned.
592 If 32-bit operations are not supported, then ASSERT().
593 If StartBit is greater than 31, then ASSERT().
594 If EndBit is greater than 31, then ASSERT().
595 If EndBit is less than StartBit, then ASSERT().
597 @param Operand Operand on which to perform the bitfield operation.
598 @param StartBit The ordinal of the least significant bit in the bit field.
600 @param EndBit The ordinal of the most significant bit in the bit field.
602 @param AndData The value to AND with the read value from the value
604 @return The new 32-bit value.
616 ASSERT (EndBit
< sizeof (Operand
) * 8);
617 ASSERT (StartBit
<= EndBit
);
618 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
622 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
623 bitwise OR, and returns the result.
625 Performs a bitwise AND between the bit field specified by StartBit and EndBit
626 in Operand and the value specified by AndData, followed by a bitwise
627 inclusive OR with value specified by OrData. All other bits in Operand are
628 preserved. The new 32-bit value is returned.
630 If 32-bit operations are not supported, then ASSERT().
631 If StartBit is greater than 31, then ASSERT().
632 If EndBit is greater than 31, then ASSERT().
633 If EndBit is less than StartBit, then ASSERT().
635 @param Operand Operand on which to perform the bitfield operation.
636 @param StartBit The ordinal of the least significant bit in the bit field.
638 @param EndBit The ordinal of the most significant bit in the bit field.
640 @param AndData The value to AND with the read value from the value.
641 @param OrData The value to OR with the result of the AND operation.
643 @return The new 32-bit value.
648 BitFieldAndThenOr32 (
656 ASSERT (EndBit
< sizeof (Operand
) * 8);
657 ASSERT (StartBit
<= EndBit
);
658 return BitFieldOr32 (
659 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
667 Returns a bit field from a 64-bit value.
669 Returns the bitfield specified by the StartBit and the EndBit from Operand.
671 If 64-bit operations are not supported, then ASSERT().
672 If StartBit is greater than 63, then ASSERT().
673 If EndBit is greater than 63, then ASSERT().
674 If EndBit is less than StartBit, then ASSERT().
676 @param Operand Operand on which to perform the bitfield operation.
677 @param StartBit The ordinal of the least significant bit in the bit field.
679 @param EndBit The ordinal of the most significant bit in the bit field.
682 @return The bit field read.
693 ASSERT (EndBit
< sizeof (Operand
) * 8);
694 ASSERT (StartBit
<= EndBit
);
695 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
699 Writes a bit field to a 64-bit value, and returns the result.
701 Writes Value to the bit field specified by the StartBit and the EndBit in
702 Operand. All other bits in Operand are preserved. The new 64-bit value is
705 If 64-bit operations are not supported, then ASSERT().
706 If StartBit is greater than 63, then ASSERT().
707 If EndBit is greater than 63, then ASSERT().
708 If EndBit is less than StartBit, then ASSERT().
710 @param Operand Operand on which to perform the bitfield operation.
711 @param StartBit The ordinal of the least significant bit in the bit field.
713 @param EndBit The ordinal of the most significant bit in the bit field.
715 @param Value New value of the bit field.
717 @return The new 64-bit value.
729 ASSERT (EndBit
< sizeof (Operand
) * 8);
730 ASSERT (StartBit
<= EndBit
);
731 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
735 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
738 Performs a bitwise inclusive OR between the bit field specified by StartBit
739 and EndBit in Operand and the value specified by OrData. All other bits in
740 Operand are preserved. The new 64-bit value is returned.
742 If 64-bit operations are not supported, then ASSERT().
743 If StartBit is greater than 63, then ASSERT().
744 If EndBit is greater than 63, then ASSERT().
745 If EndBit is less than StartBit, then ASSERT().
747 @param Operand Operand on which to perform the bitfield operation.
748 @param StartBit The ordinal of the least significant bit in the bit field.
750 @param EndBit The ordinal of the most significant bit in the bit field.
752 @param OrData The value to OR with the read value from the value
754 @return The new 64-bit value.
766 ASSERT (EndBit
< sizeof (Operand
) * 8);
767 ASSERT (StartBit
<= EndBit
);
769 (LShiftU64 (OrData
, StartBit
) & ~LShiftU64 ((UINT64
)-2, EndBit
));
773 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
776 Performs a bitwise AND between the bit field specified by StartBit and EndBit
777 in Operand and the value specified by AndData. All other bits in Operand are
778 preserved. The new 64-bit value is returned.
780 If 64-bit operations are not supported, then ASSERT().
781 If StartBit is greater than 63, then ASSERT().
782 If EndBit is greater than 63, then ASSERT().
783 If EndBit is less than StartBit, then ASSERT().
785 @param Operand Operand on which to perform the bitfield operation.
786 @param StartBit The ordinal of the least significant bit in the bit field.
788 @param EndBit The ordinal of the most significant bit in the bit field.
790 @param AndData The value to AND with the read value from the value
792 @return The new 64-bit value.
804 ASSERT (EndBit
< sizeof (Operand
) * 8);
805 ASSERT (StartBit
<= EndBit
);
807 ~(LShiftU64 (~AndData
, StartBit
) & ~LShiftU64 ((UINT64
)-2, EndBit
));
811 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
812 bitwise OR, and returns the result.
814 Performs a bitwise AND between the bit field specified by StartBit and EndBit
815 in Operand and the value specified by AndData, followed by a bitwise
816 inclusive OR with value specified by OrData. All other bits in Operand are
817 preserved. The new 64-bit value is returned.
819 If 64-bit operations are not supported, then ASSERT().
820 If StartBit is greater than 63, then ASSERT().
821 If EndBit is greater than 63, then ASSERT().
822 If EndBit is less than StartBit, then ASSERT().
824 @param Operand Operand on which to perform the bitfield operation.
825 @param StartBit The ordinal of the least significant bit in the bit field.
827 @param EndBit The ordinal of the most significant bit in the bit field.
829 @param AndData The value to AND with the read value from the value.
830 @param OrData The value to OR with the result of the AND operation.
832 @return The new 64-bit value.
837 BitFieldAndThenOr64 (
845 ASSERT (EndBit
< sizeof (Operand
) * 8);
846 ASSERT (StartBit
<= EndBit
);
847 return BitFieldOr64 (
848 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),