]>
git.proxmox.com Git - mirror_edk2.git/blob - BitField.c
9c9981e31457b26ebb7cf1350cbc4b165dde6464
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 // Include common header file for this module.
20 #include "CommonHeader.h"
22 #include "BaseLibInternals.h"
25 Worker function that returns a bit field from Operand
27 Returns the bitfield specified by the StartBit and the EndBit from Operand.
29 @param Operand Operand on which to perform the bitfield operation.
30 @param StartBit The ordinal of the least significant bit in the bit field.
31 @param EndBit The ordinal of the most significant bit in the bit field.
33 @return The bit field read.
38 IN
unsigned int Operand
,
44 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
45 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
47 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
51 Worker function that reads a bit field from Operand, performs a bitwise OR,
52 and returns the result.
54 Performs a bitwise OR between the bit field specified by StartBit and EndBit
55 in Operand and the value specified by AndData. All other bits in Operand are
56 preserved. The new value is returned.
58 @param Operand Operand on which to perform the bitfield operation.
59 @param StartBit The ordinal of the least significant bit in the bit field.
60 @param EndBit The ordinal of the most significant bit in the bit field.
61 @param OrData The value to OR with the read value from the value
63 @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.
99 IN
unsigned int Operand
,
102 IN
unsigned int AndData
106 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
107 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
109 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
113 Returns a bit field from an 8-bit value.
115 Returns the bitfield specified by the StartBit and the EndBit from Operand.
117 If 8-bit operations are not supported, then ASSERT().
118 If StartBit is greater than 7, then ASSERT().
119 If EndBit is greater than 7, then ASSERT().
120 If EndBit is less than StartBit, then ASSERT().
122 @param Operand Operand on which to perform the bitfield operation.
123 @param StartBit The ordinal of the least significant bit in the bit field.
125 @param EndBit The ordinal of the most significant bit in the bit field.
128 @return The bit field read.
139 ASSERT (EndBit
< sizeof (Operand
) * 8);
140 ASSERT (StartBit
<= EndBit
);
141 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
145 Writes a bit field to an 8-bit value, and returns the result.
147 Writes Value to the bit field specified by the StartBit and the EndBit in
148 Operand. All other bits in Operand are preserved. The new 8-bit value is
151 If 8-bit operations are not supported, then ASSERT().
152 If StartBit is greater than 7, then ASSERT().
153 If EndBit is greater than 7, then ASSERT().
154 If EndBit is less than StartBit, then ASSERT().
156 @param Operand Operand on which to perform the bitfield operation.
157 @param StartBit The ordinal of the least significant bit in the bit field.
159 @param EndBit The ordinal of the most significant bit in the bit field.
161 @param Value New value of the bit field.
163 @return The new 8-bit value.
175 ASSERT (EndBit
< sizeof (Operand
) * 8);
176 ASSERT (StartBit
<= EndBit
);
177 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
181 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
184 Performs a bitwise inclusive OR between the bit field specified by StartBit
185 and EndBit in Operand and the value specified by OrData. All other bits in
186 Operand are preserved. The new 8-bit value is returned.
188 If 8-bit operations are not supported, then ASSERT().
189 If StartBit is greater than 7, then ASSERT().
190 If EndBit is greater than 7, then ASSERT().
191 If EndBit is less than StartBit, then ASSERT().
193 @param Operand Operand on which to perform the bitfield operation.
194 @param StartBit The ordinal of the least significant bit in the bit field.
196 @param EndBit The ordinal of the most significant bit in the bit field.
198 @param OrData The value to OR with the read value from the value
200 @return The new 8-bit value.
212 ASSERT (EndBit
< sizeof (Operand
) * 8);
213 ASSERT (StartBit
<= EndBit
);
214 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
218 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
221 Performs a bitwise AND between the bit field specified by StartBit and EndBit
222 in Operand and the value specified by AndData. All other bits in Operand are
223 preserved. The new 8-bit value is returned.
225 If 8-bit operations are not supported, then ASSERT().
226 If StartBit is greater than 7, then ASSERT().
227 If EndBit is greater than 7, then ASSERT().
228 If EndBit is less than StartBit, then ASSERT().
230 @param Operand Operand on which to perform the bitfield operation.
231 @param StartBit The ordinal of the least significant bit in the bit field.
233 @param EndBit The ordinal of the most significant bit in the bit field.
235 @param AndData The value to AND with the read value from the value.
237 @return The new 8-bit value.
249 ASSERT (EndBit
< sizeof (Operand
) * 8);
250 ASSERT (StartBit
<= EndBit
);
251 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
255 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
256 bitwise OR, and returns the result.
258 Performs a bitwise AND between the bit field specified by StartBit and EndBit
259 in Operand and the value specified by AndData, followed by a bitwise
260 inclusive OR with value specified by OrData. All other bits in Operand are
261 preserved. The new 8-bit value is returned.
263 If 8-bit operations are not supported, then ASSERT().
264 If StartBit is greater than 7, then ASSERT().
265 If EndBit is greater than 7, then ASSERT().
266 If EndBit is less than StartBit, then ASSERT().
268 @param Operand Operand on which to perform the bitfield operation.
269 @param StartBit The ordinal of the least significant bit in the bit field.
271 @param EndBit The ordinal of the most significant bit in the bit field.
273 @param AndData The value to AND with the read value from the value.
274 @param OrData The value to OR with the result of the AND operation.
276 @return The new 8-bit value.
289 ASSERT (EndBit
< sizeof (Operand
) * 8);
290 ASSERT (StartBit
<= EndBit
);
292 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
300 Returns a bit field from a 16-bit value.
302 Returns the bitfield specified by the StartBit and the EndBit from Operand.
304 If 16-bit operations are not supported, then ASSERT().
305 If StartBit is greater than 15, then ASSERT().
306 If EndBit is greater than 15, then ASSERT().
307 If EndBit is less than StartBit, then ASSERT().
309 @param Operand Operand on which to perform the bitfield operation.
310 @param StartBit The ordinal of the least significant bit in the bit field.
312 @param EndBit The ordinal of the most significant bit in the bit field.
315 @return The bit field read.
326 ASSERT (EndBit
< sizeof (Operand
) * 8);
327 ASSERT (StartBit
<= EndBit
);
328 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
332 Writes a bit field to a 16-bit value, and returns the result.
334 Writes Value to the bit field specified by the StartBit and the EndBit in
335 Operand. All other bits in Operand are preserved. The new 16-bit value is
338 If 16-bit operations are not supported, then ASSERT().
339 If StartBit is greater than 15, then ASSERT().
340 If EndBit is greater than 15, then ASSERT().
341 If EndBit is less than StartBit, then ASSERT().
343 @param Operand Operand on which to perform the bitfield operation.
344 @param StartBit The ordinal of the least significant bit in the bit field.
346 @param EndBit The ordinal of the most significant bit in the bit field.
348 @param Value New value of the bit field.
350 @return The new 16-bit value.
362 ASSERT (EndBit
< sizeof (Operand
) * 8);
363 ASSERT (StartBit
<= EndBit
);
364 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
368 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
371 Performs a bitwise inclusive OR between the bit field specified by StartBit
372 and EndBit in Operand and the value specified by OrData. All other bits in
373 Operand are preserved. The new 16-bit value is returned.
375 If 16-bit operations are not supported, then ASSERT().
376 If StartBit is greater than 15, then ASSERT().
377 If EndBit is greater than 15, then ASSERT().
378 If EndBit is less than StartBit, then ASSERT().
380 @param Operand Operand on which to perform the bitfield operation.
381 @param StartBit The ordinal of the least significant bit in the bit field.
383 @param EndBit The ordinal of the most significant bit in the bit field.
385 @param OrData The value to OR with the read value from the value
387 @return The new 16-bit value.
399 ASSERT (EndBit
< sizeof (Operand
) * 8);
400 ASSERT (StartBit
<= EndBit
);
401 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
405 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
408 Performs a bitwise AND between the bit field specified by StartBit and EndBit
409 in Operand and the value specified by AndData. All other bits in Operand are
410 preserved. The new 16-bit value is returned.
412 If 16-bit operations are not supported, then ASSERT().
413 If StartBit is greater than 15, then ASSERT().
414 If EndBit is greater than 15, then ASSERT().
415 If EndBit is less than StartBit, then ASSERT().
417 @param Operand Operand on which to perform the bitfield operation.
418 @param StartBit The ordinal of the least significant bit in the bit field.
420 @param EndBit The ordinal of the most significant bit in the bit field.
422 @param AndData The value to AND with the read value from the value
424 @return The new 16-bit value.
436 ASSERT (EndBit
< sizeof (Operand
) * 8);
437 ASSERT (StartBit
<= EndBit
);
438 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
442 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
443 bitwise OR, and returns the result.
445 Performs a bitwise AND between the bit field specified by StartBit and EndBit
446 in Operand and the value specified by AndData, followed by a bitwise
447 inclusive OR with value specified by OrData. All other bits in Operand are
448 preserved. The new 16-bit value is returned.
450 If 16-bit operations are not supported, then ASSERT().
451 If StartBit is greater than 15, then ASSERT().
452 If EndBit is greater than 15, then ASSERT().
453 If EndBit is less than StartBit, then ASSERT().
455 @param Operand Operand on which to perform the bitfield operation.
456 @param StartBit The ordinal of the least significant bit in the bit field.
458 @param EndBit The ordinal of the most significant bit in the bit field.
460 @param AndData The value to AND with the read value from the value.
461 @param OrData The value to OR with the result of the AND operation.
463 @return The new 16-bit value.
468 BitFieldAndThenOr16 (
476 ASSERT (EndBit
< sizeof (Operand
) * 8);
477 ASSERT (StartBit
<= EndBit
);
478 return BitFieldOr16 (
479 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
487 Returns a bit field from a 32-bit value.
489 Returns the bitfield specified by the StartBit and the EndBit from Operand.
491 If 32-bit operations are not supported, then ASSERT().
492 If StartBit is greater than 31, then ASSERT().
493 If EndBit is greater than 31, then ASSERT().
494 If EndBit is less than StartBit, then ASSERT().
496 @param Operand Operand on which to perform the bitfield operation.
497 @param StartBit The ordinal of the least significant bit in the bit field.
499 @param EndBit The ordinal of the most significant bit in the bit field.
502 @return The bit field read.
513 ASSERT (EndBit
< sizeof (Operand
) * 8);
514 ASSERT (StartBit
<= EndBit
);
515 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
519 Writes a bit field to a 32-bit value, and returns the result.
521 Writes Value to the bit field specified by the StartBit and the EndBit in
522 Operand. All other bits in Operand are preserved. The new 32-bit value is
525 If 32-bit operations are not supported, then ASSERT().
526 If StartBit is greater than 31, then ASSERT().
527 If EndBit is greater than 31, then ASSERT().
528 If EndBit is less than StartBit, then ASSERT().
530 @param Operand Operand on which to perform the bitfield operation.
531 @param StartBit The ordinal of the least significant bit in the bit field.
533 @param EndBit The ordinal of the most significant bit in the bit field.
535 @param Value New value of the bit field.
537 @return The new 32-bit value.
549 ASSERT (EndBit
< sizeof (Operand
) * 8);
550 ASSERT (StartBit
<= EndBit
);
551 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
555 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
558 Performs a bitwise inclusive OR between the bit field specified by StartBit
559 and EndBit in Operand and the value specified by OrData. All other bits in
560 Operand are preserved. The new 32-bit value is returned.
562 If 32-bit operations are not supported, then ASSERT().
563 If StartBit is greater than 31, then ASSERT().
564 If EndBit is greater than 31, then ASSERT().
565 If EndBit is less than StartBit, then ASSERT().
567 @param Operand Operand on which to perform the bitfield operation.
568 @param StartBit The ordinal of the least significant bit in the bit field.
570 @param EndBit The ordinal of the most significant bit in the bit field.
572 @param OrData The value to OR with the read value from the value
574 @return The new 32-bit value.
586 ASSERT (EndBit
< sizeof (Operand
) * 8);
587 ASSERT (StartBit
<= EndBit
);
588 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
592 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
595 Performs a bitwise AND between the bit field specified by StartBit and EndBit
596 in Operand and the value specified by AndData. All other bits in Operand are
597 preserved. The new 32-bit value is returned.
599 If 32-bit operations are not supported, then ASSERT().
600 If StartBit is greater than 31, then ASSERT().
601 If EndBit is greater than 31, then ASSERT().
602 If EndBit is less than StartBit, then ASSERT().
604 @param Operand Operand on which to perform the bitfield operation.
605 @param StartBit The ordinal of the least significant bit in the bit field.
607 @param EndBit The ordinal of the most significant bit in the bit field.
609 @param AndData The value to AND with the read value from the value
611 @return The new 32-bit value.
623 ASSERT (EndBit
< sizeof (Operand
) * 8);
624 ASSERT (StartBit
<= EndBit
);
625 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
629 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
630 bitwise OR, and returns the result.
632 Performs a bitwise AND between the bit field specified by StartBit and EndBit
633 in Operand and the value specified by AndData, followed by a bitwise
634 inclusive OR with value specified by OrData. All other bits in Operand are
635 preserved. The new 32-bit value is returned.
637 If 32-bit operations are not supported, then ASSERT().
638 If StartBit is greater than 31, then ASSERT().
639 If EndBit is greater than 31, then ASSERT().
640 If EndBit is less than StartBit, then ASSERT().
642 @param Operand Operand on which to perform the bitfield operation.
643 @param StartBit The ordinal of the least significant bit in the bit field.
645 @param EndBit The ordinal of the most significant bit in the bit field.
647 @param AndData The value to AND with the read value from the value.
648 @param OrData The value to OR with the result of the AND operation.
650 @return The new 32-bit value.
655 BitFieldAndThenOr32 (
663 ASSERT (EndBit
< sizeof (Operand
) * 8);
664 ASSERT (StartBit
<= EndBit
);
665 return BitFieldOr32 (
666 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
674 Returns a bit field from a 64-bit value.
676 Returns the bitfield specified by the StartBit and the EndBit from Operand.
678 If 64-bit operations are not supported, then ASSERT().
679 If StartBit is greater than 63, then ASSERT().
680 If EndBit is greater than 63, then ASSERT().
681 If EndBit is less than StartBit, then ASSERT().
683 @param Operand Operand on which to perform the bitfield operation.
684 @param StartBit The ordinal of the least significant bit in the bit field.
686 @param EndBit The ordinal of the most significant bit in the bit field.
689 @return The bit field read.
700 ASSERT (EndBit
< sizeof (Operand
) * 8);
701 ASSERT (StartBit
<= EndBit
);
702 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
706 Writes a bit field to a 64-bit value, and returns the result.
708 Writes Value to the bit field specified by the StartBit and the EndBit in
709 Operand. All other bits in Operand are preserved. The new 64-bit value is
712 If 64-bit operations are not supported, then ASSERT().
713 If StartBit is greater than 63, then ASSERT().
714 If EndBit is greater than 63, then ASSERT().
715 If EndBit is less than StartBit, then ASSERT().
717 @param Operand Operand on which to perform the bitfield operation.
718 @param StartBit The ordinal of the least significant bit in the bit field.
720 @param EndBit The ordinal of the most significant bit in the bit field.
722 @param Value New value of the bit field.
724 @return The new 64-bit value.
736 ASSERT (EndBit
< sizeof (Operand
) * 8);
737 ASSERT (StartBit
<= EndBit
);
738 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
742 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
745 Performs a bitwise inclusive OR between the bit field specified by StartBit
746 and EndBit in Operand and the value specified by OrData. All other bits in
747 Operand are preserved. The new 64-bit value is returned.
749 If 64-bit operations are not supported, then ASSERT().
750 If StartBit is greater than 63, then ASSERT().
751 If EndBit is greater than 63, then ASSERT().
752 If EndBit is less than StartBit, then ASSERT().
754 @param Operand Operand on which to perform the bitfield operation.
755 @param StartBit The ordinal of the least significant bit in the bit field.
757 @param EndBit The ordinal of the most significant bit in the bit field.
759 @param OrData The value to OR with the read value from the value
761 @return The new 64-bit value.
776 ASSERT (EndBit
< sizeof (Operand
) * 8);
777 ASSERT (StartBit
<= EndBit
);
779 Value1
= LShiftU64 (OrData
, StartBit
);
780 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
782 return Operand
| (Value1
& ~Value2
);
786 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
789 Performs a bitwise AND between the bit field specified by StartBit and EndBit
790 in Operand and the value specified by AndData. All other bits in Operand are
791 preserved. The new 64-bit value is returned.
793 If 64-bit operations are not supported, then ASSERT().
794 If StartBit is greater than 63, then ASSERT().
795 If EndBit is greater than 63, then ASSERT().
796 If EndBit is less than StartBit, then ASSERT().
798 @param Operand Operand on which to perform the bitfield operation.
799 @param StartBit The ordinal of the least significant bit in the bit field.
801 @param EndBit The ordinal of the most significant bit in the bit field.
803 @param AndData The value to AND with the read value from the value
805 @return The new 64-bit value.
820 ASSERT (EndBit
< sizeof (Operand
) * 8);
821 ASSERT (StartBit
<= EndBit
);
823 Value1
= LShiftU64 (~AndData
, StartBit
);
824 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
826 return Operand
& ~(Value1
& ~Value2
);
830 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
831 bitwise OR, and returns the result.
833 Performs a bitwise AND between the bit field specified by StartBit and EndBit
834 in Operand and the value specified by AndData, followed by a bitwise
835 inclusive OR with value specified by OrData. All other bits in Operand are
836 preserved. The new 64-bit value is returned.
838 If 64-bit operations are not supported, then ASSERT().
839 If StartBit is greater than 63, then ASSERT().
840 If EndBit is greater than 63, then ASSERT().
841 If EndBit is less than StartBit, then ASSERT().
843 @param Operand Operand on which to perform the bitfield operation.
844 @param StartBit The ordinal of the least significant bit in the bit field.
846 @param EndBit The ordinal of the most significant bit in the bit field.
848 @param AndData The value to AND with the read value from the value.
849 @param OrData The value to OR with the result of the AND operation.
851 @return The new 64-bit value.
856 BitFieldAndThenOr64 (
864 ASSERT (EndBit
< sizeof (Operand
) * 8);
865 ASSERT (StartBit
<= EndBit
);
866 return BitFieldOr64 (
867 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),