]>
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.
18 #include "BaseLibInternals.h"
21 Worker function that returns a bit field from Operand
23 Returns the bitfield specified by the StartBit and the EndBit from Operand.
25 @param Operand Operand on which to perform the bitfield operation.
26 @param StartBit The ordinal of the least significant bit in the bit field.
27 @param EndBit The ordinal of the most significant bit in the bit field.
29 @return The bit field read.
35 IN
unsigned int Operand
,
41 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
42 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
44 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
48 Worker function that reads a bit field from Operand, performs a bitwise OR,
49 and returns the result.
51 Performs a bitwise OR between the bit field specified by StartBit and EndBit
52 in Operand and the value specified by AndData. All other bits in Operand are
53 preserved. The new value is returned.
55 @param Operand Operand on which to perform the bitfield operation.
56 @param StartBit The ordinal of the least significant bit in the bit field.
57 @param EndBit The ordinal of the most significant bit in the bit field.
58 @param OrData The value to OR with the read value from the value
60 @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.
98 IN
unsigned int Operand
,
101 IN
unsigned int AndData
105 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
106 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
108 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
112 Returns a bit field from an 8-bit value.
114 Returns the bitfield specified by the StartBit and the EndBit from Operand.
116 If 8-bit operations are not supported, then ASSERT().
117 If StartBit is greater than 7, then ASSERT().
118 If EndBit is greater than 7, then ASSERT().
119 If EndBit is less than StartBit, then ASSERT().
121 @param Operand Operand on which to perform the bitfield operation.
122 @param StartBit The ordinal of the least significant bit in the bit field.
124 @param EndBit The ordinal of the most significant bit in the bit field.
127 @return The bit field read.
138 ASSERT (EndBit
< sizeof (Operand
) * 8);
139 ASSERT (StartBit
<= EndBit
);
140 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
144 Writes a bit field to an 8-bit value, and returns the result.
146 Writes Value to the bit field specified by the StartBit and the EndBit in
147 Operand. All other bits in Operand are preserved. The new 8-bit value is
150 If 8-bit operations are not supported, then ASSERT().
151 If StartBit is greater than 7, then ASSERT().
152 If EndBit is greater than 7, then ASSERT().
153 If EndBit is less than StartBit, then ASSERT().
155 @param Operand Operand on which to perform the bitfield operation.
156 @param StartBit The ordinal of the least significant bit in the bit field.
158 @param EndBit The ordinal of the most significant bit in the bit field.
160 @param Value New value of the bit field.
162 @return The new 8-bit value.
174 ASSERT (EndBit
< sizeof (Operand
) * 8);
175 ASSERT (StartBit
<= EndBit
);
176 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
180 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
183 Performs a bitwise inclusive OR between the bit field specified by StartBit
184 and EndBit in Operand and the value specified by OrData. All other bits in
185 Operand are preserved. The new 8-bit value is returned.
187 If 8-bit operations are not supported, then ASSERT().
188 If StartBit is greater than 7, then ASSERT().
189 If EndBit is greater than 7, then ASSERT().
190 If EndBit is less than StartBit, then ASSERT().
192 @param Operand Operand on which to perform the bitfield operation.
193 @param StartBit The ordinal of the least significant bit in the bit field.
195 @param EndBit The ordinal of the most significant bit in the bit field.
197 @param OrData The value to OR with the read value from the value
199 @return The new 8-bit value.
211 ASSERT (EndBit
< sizeof (Operand
) * 8);
212 ASSERT (StartBit
<= EndBit
);
213 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
217 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
220 Performs a bitwise AND between the bit field specified by StartBit and EndBit
221 in Operand and the value specified by AndData. All other bits in Operand are
222 preserved. The new 8-bit value is returned.
224 If 8-bit operations are not supported, then ASSERT().
225 If StartBit is greater than 7, then ASSERT().
226 If EndBit is greater than 7, then ASSERT().
227 If EndBit is less than StartBit, then ASSERT().
229 @param Operand Operand on which to perform the bitfield operation.
230 @param StartBit The ordinal of the least significant bit in the bit field.
232 @param EndBit The ordinal of the most significant bit in the bit field.
234 @param AndData The value to AND with the read value from the value.
236 @return The new 8-bit value.
248 ASSERT (EndBit
< sizeof (Operand
) * 8);
249 ASSERT (StartBit
<= EndBit
);
250 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
254 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
255 bitwise OR, and returns the result.
257 Performs a bitwise AND between the bit field specified by StartBit and EndBit
258 in Operand and the value specified by AndData, followed by a bitwise
259 inclusive OR with value specified by OrData. All other bits in Operand are
260 preserved. The new 8-bit value is returned.
262 If 8-bit operations are not supported, then ASSERT().
263 If StartBit is greater than 7, then ASSERT().
264 If EndBit is greater than 7, then ASSERT().
265 If EndBit is less than StartBit, then ASSERT().
267 @param Operand Operand on which to perform the bitfield operation.
268 @param StartBit The ordinal of the least significant bit in the bit field.
270 @param EndBit The ordinal of the most significant bit in the bit field.
272 @param AndData The value to AND with the read value from the value.
273 @param OrData The value to OR with the result of the AND operation.
275 @return The new 8-bit value.
288 ASSERT (EndBit
< sizeof (Operand
) * 8);
289 ASSERT (StartBit
<= EndBit
);
291 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
299 Returns a bit field from a 16-bit value.
301 Returns the bitfield specified by the StartBit and the EndBit from Operand.
303 If 16-bit operations are not supported, then ASSERT().
304 If StartBit is greater than 15, then ASSERT().
305 If EndBit is greater than 15, then ASSERT().
306 If EndBit is less than StartBit, then ASSERT().
308 @param Operand Operand on which to perform the bitfield operation.
309 @param StartBit The ordinal of the least significant bit in the bit field.
311 @param EndBit The ordinal of the most significant bit in the bit field.
314 @return The bit field read.
325 ASSERT (EndBit
< sizeof (Operand
) * 8);
326 ASSERT (StartBit
<= EndBit
);
327 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
331 Writes a bit field to a 16-bit value, and returns the result.
333 Writes Value to the bit field specified by the StartBit and the EndBit in
334 Operand. All other bits in Operand are preserved. The new 16-bit value is
337 If 16-bit operations are not supported, then ASSERT().
338 If StartBit is greater than 15, then ASSERT().
339 If EndBit is greater than 15, then ASSERT().
340 If EndBit is less than StartBit, then ASSERT().
342 @param Operand Operand on which to perform the bitfield operation.
343 @param StartBit The ordinal of the least significant bit in the bit field.
345 @param EndBit The ordinal of the most significant bit in the bit field.
347 @param Value New value of the bit field.
349 @return The new 16-bit value.
361 ASSERT (EndBit
< sizeof (Operand
) * 8);
362 ASSERT (StartBit
<= EndBit
);
363 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
367 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
370 Performs a bitwise inclusive OR between the bit field specified by StartBit
371 and EndBit in Operand and the value specified by OrData. All other bits in
372 Operand are preserved. The new 16-bit value is returned.
374 If 16-bit operations are not supported, then ASSERT().
375 If StartBit is greater than 15, then ASSERT().
376 If EndBit is greater than 15, then ASSERT().
377 If EndBit is less than StartBit, then ASSERT().
379 @param Operand Operand on which to perform the bitfield operation.
380 @param StartBit The ordinal of the least significant bit in the bit field.
382 @param EndBit The ordinal of the most significant bit in the bit field.
384 @param OrData The value to OR with the read value from the value
386 @return The new 16-bit value.
398 ASSERT (EndBit
< sizeof (Operand
) * 8);
399 ASSERT (StartBit
<= EndBit
);
400 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
404 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
407 Performs a bitwise AND between the bit field specified by StartBit and EndBit
408 in Operand and the value specified by AndData. All other bits in Operand are
409 preserved. The new 16-bit value is returned.
411 If 16-bit operations are not supported, then ASSERT().
412 If StartBit is greater than 15, then ASSERT().
413 If EndBit is greater than 15, then ASSERT().
414 If EndBit is less than StartBit, then ASSERT().
416 @param Operand Operand on which to perform the bitfield operation.
417 @param StartBit The ordinal of the least significant bit in the bit field.
419 @param EndBit The ordinal of the most significant bit in the bit field.
421 @param AndData The value to AND with the read value from the value
423 @return The new 16-bit value.
435 ASSERT (EndBit
< sizeof (Operand
) * 8);
436 ASSERT (StartBit
<= EndBit
);
437 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
441 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
442 bitwise OR, and returns the result.
444 Performs a bitwise AND between the bit field specified by StartBit and EndBit
445 in Operand and the value specified by AndData, followed by a bitwise
446 inclusive OR with value specified by OrData. All other bits in Operand are
447 preserved. The new 16-bit value is returned.
449 If 16-bit operations are not supported, then ASSERT().
450 If StartBit is greater than 15, then ASSERT().
451 If EndBit is greater than 15, then ASSERT().
452 If EndBit is less than StartBit, then ASSERT().
454 @param Operand Operand on which to perform the bitfield operation.
455 @param StartBit The ordinal of the least significant bit in the bit field.
457 @param EndBit The ordinal of the most significant bit in the bit field.
459 @param AndData The value to AND with the read value from the value.
460 @param OrData The value to OR with the result of the AND operation.
462 @return The new 16-bit value.
467 BitFieldAndThenOr16 (
475 ASSERT (EndBit
< sizeof (Operand
) * 8);
476 ASSERT (StartBit
<= EndBit
);
477 return BitFieldOr16 (
478 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
486 Returns a bit field from a 32-bit value.
488 Returns the bitfield specified by the StartBit and the EndBit from Operand.
490 If 32-bit operations are not supported, then ASSERT().
491 If StartBit is greater than 31, then ASSERT().
492 If EndBit is greater than 31, then ASSERT().
493 If EndBit is less than StartBit, then ASSERT().
495 @param Operand Operand on which to perform the bitfield operation.
496 @param StartBit The ordinal of the least significant bit in the bit field.
498 @param EndBit The ordinal of the most significant bit in the bit field.
501 @return The bit field read.
512 ASSERT (EndBit
< sizeof (Operand
) * 8);
513 ASSERT (StartBit
<= EndBit
);
514 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
518 Writes a bit field to a 32-bit value, and returns the result.
520 Writes Value to the bit field specified by the StartBit and the EndBit in
521 Operand. All other bits in Operand are preserved. The new 32-bit value is
524 If 32-bit operations are not supported, then ASSERT().
525 If StartBit is greater than 31, then ASSERT().
526 If EndBit is greater than 31, then ASSERT().
527 If EndBit is less than StartBit, then ASSERT().
529 @param Operand Operand on which to perform the bitfield operation.
530 @param StartBit The ordinal of the least significant bit in the bit field.
532 @param EndBit The ordinal of the most significant bit in the bit field.
534 @param Value New value of the bit field.
536 @return The new 32-bit value.
548 ASSERT (EndBit
< sizeof (Operand
) * 8);
549 ASSERT (StartBit
<= EndBit
);
550 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
554 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
557 Performs a bitwise inclusive OR between the bit field specified by StartBit
558 and EndBit in Operand and the value specified by OrData. All other bits in
559 Operand are preserved. The new 32-bit value is returned.
561 If 32-bit operations are not supported, then ASSERT().
562 If StartBit is greater than 31, then ASSERT().
563 If EndBit is greater than 31, then ASSERT().
564 If EndBit is less than StartBit, then ASSERT().
566 @param Operand Operand on which to perform the bitfield operation.
567 @param StartBit The ordinal of the least significant bit in the bit field.
569 @param EndBit The ordinal of the most significant bit in the bit field.
571 @param OrData The value to OR with the read value from the value
573 @return The new 32-bit value.
585 ASSERT (EndBit
< sizeof (Operand
) * 8);
586 ASSERT (StartBit
<= EndBit
);
587 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
591 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
594 Performs a bitwise AND between the bit field specified by StartBit and EndBit
595 in Operand and the value specified by AndData. All other bits in Operand are
596 preserved. The new 32-bit value is returned.
598 If 32-bit operations are not supported, then ASSERT().
599 If StartBit is greater than 31, then ASSERT().
600 If EndBit is greater than 31, then ASSERT().
601 If EndBit is less than StartBit, then ASSERT().
603 @param Operand Operand on which to perform the bitfield operation.
604 @param StartBit The ordinal of the least significant bit in the bit field.
606 @param EndBit The ordinal of the most significant bit in the bit field.
608 @param AndData The value to AND with the read value from the value
610 @return The new 32-bit value.
622 ASSERT (EndBit
< sizeof (Operand
) * 8);
623 ASSERT (StartBit
<= EndBit
);
624 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
628 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
629 bitwise OR, and returns the result.
631 Performs a bitwise AND between the bit field specified by StartBit and EndBit
632 in Operand and the value specified by AndData, followed by a bitwise
633 inclusive OR with value specified by OrData. All other bits in Operand are
634 preserved. The new 32-bit value is returned.
636 If 32-bit operations are not supported, then ASSERT().
637 If StartBit is greater than 31, then ASSERT().
638 If EndBit is greater than 31, then ASSERT().
639 If EndBit is less than StartBit, then ASSERT().
641 @param Operand Operand on which to perform the bitfield operation.
642 @param StartBit The ordinal of the least significant bit in the bit field.
644 @param EndBit The ordinal of the most significant bit in the bit field.
646 @param AndData The value to AND with the read value from the value.
647 @param OrData The value to OR with the result of the AND operation.
649 @return The new 32-bit value.
654 BitFieldAndThenOr32 (
662 ASSERT (EndBit
< sizeof (Operand
) * 8);
663 ASSERT (StartBit
<= EndBit
);
664 return BitFieldOr32 (
665 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
673 Returns a bit field from a 64-bit value.
675 Returns the bitfield specified by the StartBit and the EndBit from Operand.
677 If 64-bit operations are not supported, then ASSERT().
678 If StartBit is greater than 63, then ASSERT().
679 If EndBit is greater than 63, then ASSERT().
680 If EndBit is less than StartBit, then ASSERT().
682 @param Operand Operand on which to perform the bitfield operation.
683 @param StartBit The ordinal of the least significant bit in the bit field.
685 @param EndBit The ordinal of the most significant bit in the bit field.
688 @return The bit field read.
699 ASSERT (EndBit
< sizeof (Operand
) * 8);
700 ASSERT (StartBit
<= EndBit
);
701 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
705 Writes a bit field to a 64-bit value, and returns the result.
707 Writes Value to the bit field specified by the StartBit and the EndBit in
708 Operand. All other bits in Operand are preserved. The new 64-bit value is
711 If 64-bit operations are not supported, then ASSERT().
712 If StartBit is greater than 63, then ASSERT().
713 If EndBit is greater than 63, then ASSERT().
714 If EndBit is less than StartBit, then ASSERT().
716 @param Operand Operand on which to perform the bitfield operation.
717 @param StartBit The ordinal of the least significant bit in the bit field.
719 @param EndBit The ordinal of the most significant bit in the bit field.
721 @param Value New value of the bit field.
723 @return The new 64-bit value.
735 ASSERT (EndBit
< sizeof (Operand
) * 8);
736 ASSERT (StartBit
<= EndBit
);
737 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
741 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
744 Performs a bitwise inclusive OR between the bit field specified by StartBit
745 and EndBit in Operand and the value specified by OrData. All other bits in
746 Operand are preserved. The new 64-bit value is returned.
748 If 64-bit operations are not supported, then ASSERT().
749 If StartBit is greater than 63, then ASSERT().
750 If EndBit is greater than 63, then ASSERT().
751 If EndBit is less than StartBit, then ASSERT().
753 @param Operand Operand on which to perform the bitfield operation.
754 @param StartBit The ordinal of the least significant bit in the bit field.
756 @param EndBit The ordinal of the most significant bit in the bit field.
758 @param OrData The value to OR with the read value from the value
760 @return The new 64-bit value.
775 ASSERT (EndBit
< sizeof (Operand
) * 8);
776 ASSERT (StartBit
<= EndBit
);
778 Value1
= LShiftU64 (OrData
, StartBit
);
779 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
781 return Operand
| (Value1
& ~Value2
);
785 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
788 Performs a bitwise AND between the bit field specified by StartBit and EndBit
789 in Operand and the value specified by AndData. All other bits in Operand are
790 preserved. The new 64-bit value is returned.
792 If 64-bit operations are not supported, then ASSERT().
793 If StartBit is greater than 63, then ASSERT().
794 If EndBit is greater than 63, then ASSERT().
795 If EndBit is less than StartBit, then ASSERT().
797 @param Operand Operand on which to perform the bitfield operation.
798 @param StartBit The ordinal of the least significant bit in the bit field.
800 @param EndBit The ordinal of the most significant bit in the bit field.
802 @param AndData The value to AND with the read value from the value
804 @return The new 64-bit value.
819 ASSERT (EndBit
< sizeof (Operand
) * 8);
820 ASSERT (StartBit
<= EndBit
);
822 Value1
= LShiftU64 (~AndData
, StartBit
);
823 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
825 return Operand
& ~(Value1
& ~Value2
);
829 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
830 bitwise OR, and returns the result.
832 Performs a bitwise AND between the bit field specified by StartBit and EndBit
833 in Operand and the value specified by AndData, followed by a bitwise
834 inclusive OR with value specified by OrData. All other bits in Operand are
835 preserved. The new 64-bit value is returned.
837 If 64-bit operations are not supported, then ASSERT().
838 If StartBit is greater than 63, then ASSERT().
839 If EndBit is greater than 63, then ASSERT().
840 If EndBit is less than StartBit, then ASSERT().
842 @param Operand Operand on which to perform the bitfield operation.
843 @param StartBit The ordinal of the least significant bit in the bit field.
845 @param EndBit The ordinal of the most significant bit in the bit field.
847 @param AndData The value to AND with the read value from the value.
848 @param OrData The value to OR with the result of the AND operation.
850 @return The new 64-bit value.
855 BitFieldAndThenOr64 (
863 ASSERT (EndBit
< sizeof (Operand
) * 8);
864 ASSERT (StartBit
<= EndBit
);
865 return BitFieldOr64 (
866 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),