]>
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.
36 IN
unsigned int Operand
,
42 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
43 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
45 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
49 Worker function that reads a bit field from Operand, performs a bitwise OR,
50 and returns the result.
52 Performs a bitwise OR between the bit field specified by StartBit and EndBit
53 in Operand and the value specified by AndData. All other bits in Operand are
54 preserved. The new value is returned.
56 @param Operand Operand on which to perform the bitfield operation.
57 @param StartBit The ordinal of the least significant bit in the bit field.
58 @param EndBit The ordinal of the most significant bit in the bit field.
59 @param OrData The value to OR with the read value from the value
61 @return The new value.
66 IN
unsigned int Operand
,
69 IN
unsigned int OrData
73 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
74 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
76 return Operand
| ((OrData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
80 Worker function that reads a bit field from Operand, performs a bitwise AND,
81 and returns the result.
83 Performs a bitwise AND between the bit field specified by StartBit and EndBit
84 in Operand and the value specified by AndData. All other bits in Operand are
85 preserved. The new value is returned.
87 @param Operand Operand on which to perform the bitfield operation.
88 @param StartBit The ordinal of the least significant bit in the bit field.
89 @param EndBit The ordinal of the most significant bit in the bit field.
90 @param AndData The value to And with the read value from the value
92 @return The new value.
97 IN
unsigned int Operand
,
100 IN
unsigned int AndData
104 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
105 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
107 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
111 Returns a bit field from an 8-bit value.
113 Returns the bitfield specified by the StartBit and the EndBit from Operand.
115 If 8-bit operations are not supported, then ASSERT().
116 If StartBit is greater than 7, then ASSERT().
117 If EndBit is greater than 7, then ASSERT().
118 If EndBit is less than StartBit, then ASSERT().
120 @param Operand Operand on which to perform the bitfield operation.
121 @param StartBit The ordinal of the least significant bit in the bit field.
123 @param EndBit The ordinal of the most significant bit in the bit field.
126 @return The bit field read.
137 ASSERT (EndBit
< sizeof (Operand
) * 8);
138 ASSERT (StartBit
<= EndBit
);
139 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
143 Writes a bit field to an 8-bit value, and returns the result.
145 Writes Value to the bit field specified by the StartBit and the EndBit in
146 Operand. All other bits in Operand are preserved. The new 8-bit value is
149 If 8-bit operations are not supported, then ASSERT().
150 If StartBit is greater than 7, then ASSERT().
151 If EndBit is greater than 7, then ASSERT().
152 If EndBit is less than StartBit, then ASSERT().
154 @param Operand Operand on which to perform the bitfield operation.
155 @param StartBit The ordinal of the least significant bit in the bit field.
157 @param EndBit The ordinal of the most significant bit in the bit field.
159 @param Value New value of the bit field.
161 @return The new 8-bit value.
173 ASSERT (EndBit
< sizeof (Operand
) * 8);
174 ASSERT (StartBit
<= EndBit
);
175 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
179 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
182 Performs a bitwise inclusive OR between the bit field specified by StartBit
183 and EndBit in Operand and the value specified by OrData. All other bits in
184 Operand are preserved. The new 8-bit value is returned.
186 If 8-bit operations are not supported, then ASSERT().
187 If StartBit is greater than 7, then ASSERT().
188 If EndBit is greater than 7, then ASSERT().
189 If EndBit is less than StartBit, then ASSERT().
191 @param Operand Operand on which to perform the bitfield operation.
192 @param StartBit The ordinal of the least significant bit in the bit field.
194 @param EndBit The ordinal of the most significant bit in the bit field.
196 @param OrData The value to OR with the read value from the value
198 @return The new 8-bit value.
210 ASSERT (EndBit
< sizeof (Operand
) * 8);
211 ASSERT (StartBit
<= EndBit
);
212 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
216 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
219 Performs a bitwise AND between the bit field specified by StartBit and EndBit
220 in Operand and the value specified by AndData. All other bits in Operand are
221 preserved. The new 8-bit value is returned.
223 If 8-bit operations are not supported, then ASSERT().
224 If StartBit is greater than 7, then ASSERT().
225 If EndBit is greater than 7, then ASSERT().
226 If EndBit is less than StartBit, then ASSERT().
228 @param Operand Operand on which to perform the bitfield operation.
229 @param StartBit The ordinal of the least significant bit in the bit field.
231 @param EndBit The ordinal of the most significant bit in the bit field.
233 @param AndData The value to AND with the read value from the value.
235 @return The new 8-bit value.
247 ASSERT (EndBit
< sizeof (Operand
) * 8);
248 ASSERT (StartBit
<= EndBit
);
249 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
253 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
254 bitwise OR, and returns the result.
256 Performs a bitwise AND between the bit field specified by StartBit and EndBit
257 in Operand and the value specified by AndData, followed by a bitwise
258 inclusive OR with value specified by OrData. All other bits in Operand are
259 preserved. The new 8-bit value is returned.
261 If 8-bit operations are not supported, then ASSERT().
262 If StartBit is greater than 7, then ASSERT().
263 If EndBit is greater than 7, then ASSERT().
264 If EndBit is less than StartBit, then ASSERT().
266 @param Operand Operand on which to perform the bitfield operation.
267 @param StartBit The ordinal of the least significant bit in the bit field.
269 @param EndBit The ordinal of the most significant bit in the bit field.
271 @param AndData The value to AND with the read value from the value.
272 @param OrData The value to OR with the result of the AND operation.
274 @return The new 8-bit value.
287 ASSERT (EndBit
< sizeof (Operand
) * 8);
288 ASSERT (StartBit
<= EndBit
);
290 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
298 Returns a bit field from a 16-bit value.
300 Returns the bitfield specified by the StartBit and the EndBit from Operand.
302 If 16-bit operations are not supported, then ASSERT().
303 If StartBit is greater than 15, then ASSERT().
304 If EndBit is greater than 15, then ASSERT().
305 If EndBit is less than StartBit, then ASSERT().
307 @param Operand Operand on which to perform the bitfield operation.
308 @param StartBit The ordinal of the least significant bit in the bit field.
310 @param EndBit The ordinal of the most significant bit in the bit field.
313 @return The bit field read.
324 ASSERT (EndBit
< sizeof (Operand
) * 8);
325 ASSERT (StartBit
<= EndBit
);
326 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
330 Writes a bit field to a 16-bit value, and returns the result.
332 Writes Value to the bit field specified by the StartBit and the EndBit in
333 Operand. All other bits in Operand are preserved. The new 16-bit value is
336 If 16-bit operations are not supported, then ASSERT().
337 If StartBit is greater than 15, then ASSERT().
338 If EndBit is greater than 15, then ASSERT().
339 If EndBit is less than StartBit, then ASSERT().
341 @param Operand Operand on which to perform the bitfield operation.
342 @param StartBit The ordinal of the least significant bit in the bit field.
344 @param EndBit The ordinal of the most significant bit in the bit field.
346 @param Value New value of the bit field.
348 @return The new 16-bit value.
360 ASSERT (EndBit
< sizeof (Operand
) * 8);
361 ASSERT (StartBit
<= EndBit
);
362 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
366 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
369 Performs a bitwise inclusive OR between the bit field specified by StartBit
370 and EndBit in Operand and the value specified by OrData. All other bits in
371 Operand are preserved. The new 16-bit value is returned.
373 If 16-bit operations are not supported, then ASSERT().
374 If StartBit is greater than 15, then ASSERT().
375 If EndBit is greater than 15, then ASSERT().
376 If EndBit is less than StartBit, then ASSERT().
378 @param Operand Operand on which to perform the bitfield operation.
379 @param StartBit The ordinal of the least significant bit in the bit field.
381 @param EndBit The ordinal of the most significant bit in the bit field.
383 @param OrData The value to OR with the read value from the value
385 @return The new 16-bit value.
397 ASSERT (EndBit
< sizeof (Operand
) * 8);
398 ASSERT (StartBit
<= EndBit
);
399 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
403 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
406 Performs a bitwise AND between the bit field specified by StartBit and EndBit
407 in Operand and the value specified by AndData. All other bits in Operand are
408 preserved. The new 16-bit value is returned.
410 If 16-bit operations are not supported, then ASSERT().
411 If StartBit is greater than 15, then ASSERT().
412 If EndBit is greater than 15, then ASSERT().
413 If EndBit is less than StartBit, then ASSERT().
415 @param Operand Operand on which to perform the bitfield operation.
416 @param StartBit The ordinal of the least significant bit in the bit field.
418 @param EndBit The ordinal of the most significant bit in the bit field.
420 @param AndData The value to AND with the read value from the value
422 @return The new 16-bit value.
434 ASSERT (EndBit
< sizeof (Operand
) * 8);
435 ASSERT (StartBit
<= EndBit
);
436 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
440 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
441 bitwise OR, and returns the result.
443 Performs a bitwise AND between the bit field specified by StartBit and EndBit
444 in Operand and the value specified by AndData, followed by a bitwise
445 inclusive OR with value specified by OrData. All other bits in Operand are
446 preserved. The new 16-bit value is returned.
448 If 16-bit operations are not supported, then ASSERT().
449 If StartBit is greater than 15, then ASSERT().
450 If EndBit is greater than 15, then ASSERT().
451 If EndBit is less than StartBit, then ASSERT().
453 @param Operand Operand on which to perform the bitfield operation.
454 @param StartBit The ordinal of the least significant bit in the bit field.
456 @param EndBit The ordinal of the most significant bit in the bit field.
458 @param AndData The value to AND with the read value from the value.
459 @param OrData The value to OR with the result of the AND operation.
461 @return The new 16-bit value.
466 BitFieldAndThenOr16 (
474 ASSERT (EndBit
< sizeof (Operand
) * 8);
475 ASSERT (StartBit
<= EndBit
);
476 return BitFieldOr16 (
477 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
485 Returns a bit field from a 32-bit value.
487 Returns the bitfield specified by the StartBit and the EndBit from Operand.
489 If 32-bit operations are not supported, then ASSERT().
490 If StartBit is greater than 31, then ASSERT().
491 If EndBit is greater than 31, then ASSERT().
492 If EndBit is less than StartBit, then ASSERT().
494 @param Operand Operand on which to perform the bitfield operation.
495 @param StartBit The ordinal of the least significant bit in the bit field.
497 @param EndBit The ordinal of the most significant bit in the bit field.
500 @return The bit field read.
511 ASSERT (EndBit
< sizeof (Operand
) * 8);
512 ASSERT (StartBit
<= EndBit
);
513 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
517 Writes a bit field to a 32-bit value, and returns the result.
519 Writes Value to the bit field specified by the StartBit and the EndBit in
520 Operand. All other bits in Operand are preserved. The new 32-bit value is
523 If 32-bit operations are not supported, then ASSERT().
524 If StartBit is greater than 31, then ASSERT().
525 If EndBit is greater than 31, then ASSERT().
526 If EndBit is less than StartBit, then ASSERT().
528 @param Operand Operand on which to perform the bitfield operation.
529 @param StartBit The ordinal of the least significant bit in the bit field.
531 @param EndBit The ordinal of the most significant bit in the bit field.
533 @param Value New value of the bit field.
535 @return The new 32-bit value.
547 ASSERT (EndBit
< sizeof (Operand
) * 8);
548 ASSERT (StartBit
<= EndBit
);
549 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
553 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
556 Performs a bitwise inclusive OR between the bit field specified by StartBit
557 and EndBit in Operand and the value specified by OrData. All other bits in
558 Operand are preserved. The new 32-bit value is returned.
560 If 32-bit operations are not supported, then ASSERT().
561 If StartBit is greater than 31, then ASSERT().
562 If EndBit is greater than 31, then ASSERT().
563 If EndBit is less than StartBit, then ASSERT().
565 @param Operand Operand on which to perform the bitfield operation.
566 @param StartBit The ordinal of the least significant bit in the bit field.
568 @param EndBit The ordinal of the most significant bit in the bit field.
570 @param OrData The value to OR with the read value from the value
572 @return The new 32-bit value.
584 ASSERT (EndBit
< sizeof (Operand
) * 8);
585 ASSERT (StartBit
<= EndBit
);
586 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
590 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
593 Performs a bitwise AND between the bit field specified by StartBit and EndBit
594 in Operand and the value specified by AndData. All other bits in Operand are
595 preserved. The new 32-bit value is returned.
597 If 32-bit operations are not supported, then ASSERT().
598 If StartBit is greater than 31, then ASSERT().
599 If EndBit is greater than 31, then ASSERT().
600 If EndBit is less than StartBit, then ASSERT().
602 @param Operand Operand on which to perform the bitfield operation.
603 @param StartBit The ordinal of the least significant bit in the bit field.
605 @param EndBit The ordinal of the most significant bit in the bit field.
607 @param AndData The value to AND with the read value from the value
609 @return The new 32-bit value.
621 ASSERT (EndBit
< sizeof (Operand
) * 8);
622 ASSERT (StartBit
<= EndBit
);
623 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
627 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
628 bitwise OR, and returns the result.
630 Performs a bitwise AND between the bit field specified by StartBit and EndBit
631 in Operand and the value specified by AndData, followed by a bitwise
632 inclusive OR with value specified by OrData. All other bits in Operand are
633 preserved. The new 32-bit value is returned.
635 If 32-bit operations are not supported, then ASSERT().
636 If StartBit is greater than 31, then ASSERT().
637 If EndBit is greater than 31, then ASSERT().
638 If EndBit is less than StartBit, then ASSERT().
640 @param Operand Operand on which to perform the bitfield operation.
641 @param StartBit The ordinal of the least significant bit in the bit field.
643 @param EndBit The ordinal of the most significant bit in the bit field.
645 @param AndData The value to AND with the read value from the value.
646 @param OrData The value to OR with the result of the AND operation.
648 @return The new 32-bit value.
653 BitFieldAndThenOr32 (
661 ASSERT (EndBit
< sizeof (Operand
) * 8);
662 ASSERT (StartBit
<= EndBit
);
663 return BitFieldOr32 (
664 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
672 Returns a bit field from a 64-bit value.
674 Returns the bitfield specified by the StartBit and the EndBit from Operand.
676 If 64-bit operations are not supported, then ASSERT().
677 If StartBit is greater than 63, then ASSERT().
678 If EndBit is greater than 63, then ASSERT().
679 If EndBit is less than StartBit, then ASSERT().
681 @param Operand Operand on which to perform the bitfield operation.
682 @param StartBit The ordinal of the least significant bit in the bit field.
684 @param EndBit The ordinal of the most significant bit in the bit field.
687 @return The bit field read.
698 ASSERT (EndBit
< sizeof (Operand
) * 8);
699 ASSERT (StartBit
<= EndBit
);
700 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
704 Writes a bit field to a 64-bit value, and returns the result.
706 Writes Value to the bit field specified by the StartBit and the EndBit in
707 Operand. All other bits in Operand are preserved. The new 64-bit value is
710 If 64-bit operations are not supported, then ASSERT().
711 If StartBit is greater than 63, then ASSERT().
712 If EndBit is greater than 63, then ASSERT().
713 If EndBit is less than StartBit, then ASSERT().
715 @param Operand Operand on which to perform the bitfield operation.
716 @param StartBit The ordinal of the least significant bit in the bit field.
718 @param EndBit The ordinal of the most significant bit in the bit field.
720 @param Value New value of the bit field.
722 @return The new 64-bit value.
734 ASSERT (EndBit
< sizeof (Operand
) * 8);
735 ASSERT (StartBit
<= EndBit
);
736 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
740 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
743 Performs a bitwise inclusive OR between the bit field specified by StartBit
744 and EndBit in Operand and the value specified by OrData. All other bits in
745 Operand are preserved. The new 64-bit value is returned.
747 If 64-bit operations are not supported, then ASSERT().
748 If StartBit is greater than 63, then ASSERT().
749 If EndBit is greater than 63, then ASSERT().
750 If EndBit is less than StartBit, then ASSERT().
752 @param Operand Operand on which to perform the bitfield operation.
753 @param StartBit The ordinal of the least significant bit in the bit field.
755 @param EndBit The ordinal of the most significant bit in the bit field.
757 @param OrData The value to OR with the read value from the value
759 @return The new 64-bit value.
774 ASSERT (EndBit
< sizeof (Operand
) * 8);
775 ASSERT (StartBit
<= EndBit
);
777 Value1
= LShiftU64 (OrData
, StartBit
);
778 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
780 return Operand
| (Value1
& ~Value2
);
784 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
787 Performs a bitwise AND between the bit field specified by StartBit and EndBit
788 in Operand and the value specified by AndData. All other bits in Operand are
789 preserved. The new 64-bit value is returned.
791 If 64-bit operations are not supported, then ASSERT().
792 If StartBit is greater than 63, then ASSERT().
793 If EndBit is greater than 63, then ASSERT().
794 If EndBit is less than StartBit, then ASSERT().
796 @param Operand Operand on which to perform the bitfield operation.
797 @param StartBit The ordinal of the least significant bit in the bit field.
799 @param EndBit The ordinal of the most significant bit in the bit field.
801 @param AndData The value to AND with the read value from the value
803 @return The new 64-bit value.
818 ASSERT (EndBit
< sizeof (Operand
) * 8);
819 ASSERT (StartBit
<= EndBit
);
821 Value1
= LShiftU64 (~AndData
, StartBit
);
822 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
824 return Operand
& ~(Value1
& ~Value2
);
828 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
829 bitwise OR, and returns the result.
831 Performs a bitwise AND between the bit field specified by StartBit and EndBit
832 in Operand and the value specified by AndData, followed by a bitwise
833 inclusive OR with value specified by OrData. All other bits in Operand are
834 preserved. The new 64-bit value is returned.
836 If 64-bit operations are not supported, then ASSERT().
837 If StartBit is greater than 63, then ASSERT().
838 If EndBit is greater than 63, then ASSERT().
839 If EndBit is less than StartBit, then ASSERT().
841 @param Operand Operand on which to perform the bitfield operation.
842 @param StartBit The ordinal of the least significant bit in the bit field.
844 @param EndBit The ordinal of the most significant bit in the bit field.
846 @param AndData The value to AND with the read value from the value.
847 @param OrData The value to OR with the result of the AND operation.
849 @return The new 64-bit value.
854 BitFieldAndThenOr64 (
862 ASSERT (EndBit
< sizeof (Operand
) * 8);
863 ASSERT (StartBit
<= EndBit
);
864 return BitFieldOr64 (
865 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),