]>
git.proxmox.com Git - mirror_edk2.git/blob - OldMdePkg/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
17 #include "BaseLibInternals.h"
20 Worker function that returns a bit field from Operand
22 Returns the bitfield specified by the StartBit and the EndBit from Operand.
24 @param Operand Operand on which to perform the bitfield operation.
25 @param StartBit The ordinal of the least significant bit in the bit field.
26 @param EndBit The ordinal of the most significant bit in the bit field.
28 @return The bit field read.
33 IN
unsigned int Operand
,
39 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
40 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
42 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
46 Worker function that reads a bit field from Operand, performs a bitwise OR,
47 and returns the result.
49 Performs a bitwise OR between the bit field specified by StartBit and EndBit
50 in Operand and the value specified by AndData. All other bits in Operand are
51 preserved. The new value is returned.
53 @param Operand Operand on which to perform the bitfield operation.
54 @param StartBit The ordinal of the least significant bit in the bit field.
55 @param EndBit The ordinal of the most significant bit in the bit field.
56 @param OrData The value to OR with the read value from the value
58 @return The new value.
63 IN
unsigned int Operand
,
66 IN
unsigned int OrData
70 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
71 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
73 return Operand
| ((OrData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
77 Worker function that reads a bit field from Operand, performs a bitwise AND,
78 and returns the result.
80 Performs a bitwise AND between the bit field specified by StartBit and EndBit
81 in Operand and the value specified by AndData. All other bits in Operand are
82 preserved. The new value is returned.
84 @param Operand Operand on which to perform the bitfield operation.
85 @param StartBit The ordinal of the least significant bit in the bit field.
86 @param EndBit The ordinal of the most significant bit in the bit field.
87 @param AndData The value to And with the read value from the value
89 @return The new value.
94 IN
unsigned int Operand
,
97 IN
unsigned int AndData
101 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
102 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
104 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int) -2 << EndBit
));
108 Returns a bit field from an 8-bit value.
110 Returns the bitfield specified by the StartBit and the EndBit from Operand.
112 If 8-bit operations are not supported, then ASSERT().
113 If StartBit is greater than 7, then ASSERT().
114 If EndBit is greater than 7, then ASSERT().
115 If EndBit is less than StartBit, then ASSERT().
117 @param Operand Operand on which to perform the bitfield operation.
118 @param StartBit The ordinal of the least significant bit in the bit field.
120 @param EndBit The ordinal of the most significant bit in the bit field.
123 @return The bit field read.
134 ASSERT (EndBit
< sizeof (Operand
) * 8);
135 ASSERT (StartBit
<= EndBit
);
136 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
140 Writes a bit field to an 8-bit value, and returns the result.
142 Writes Value to the bit field specified by the StartBit and the EndBit in
143 Operand. All other bits in Operand are preserved. The new 8-bit value is
146 If 8-bit operations are not supported, then ASSERT().
147 If StartBit is greater than 7, then ASSERT().
148 If EndBit is greater than 7, then ASSERT().
149 If EndBit is less than StartBit, then ASSERT().
151 @param Operand Operand on which to perform the bitfield operation.
152 @param StartBit The ordinal of the least significant bit in the bit field.
154 @param EndBit The ordinal of the most significant bit in the bit field.
156 @param Value New value of the bit field.
158 @return The new 8-bit value.
170 ASSERT (EndBit
< sizeof (Operand
) * 8);
171 ASSERT (StartBit
<= EndBit
);
172 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
176 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
179 Performs a bitwise inclusive OR between the bit field specified by StartBit
180 and EndBit in Operand and the value specified by OrData. All other bits in
181 Operand are preserved. The new 8-bit value is returned.
183 If 8-bit operations are not supported, then ASSERT().
184 If StartBit is greater than 7, then ASSERT().
185 If EndBit is greater than 7, then ASSERT().
186 If EndBit is less than StartBit, then ASSERT().
188 @param Operand Operand on which to perform the bitfield operation.
189 @param StartBit The ordinal of the least significant bit in the bit field.
191 @param EndBit The ordinal of the most significant bit in the bit field.
193 @param OrData The value to OR with the read value from the value
195 @return The new 8-bit value.
207 ASSERT (EndBit
< sizeof (Operand
) * 8);
208 ASSERT (StartBit
<= EndBit
);
209 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
213 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
216 Performs a bitwise AND between the bit field specified by StartBit and EndBit
217 in Operand and the value specified by AndData. All other bits in Operand are
218 preserved. The new 8-bit value is returned.
220 If 8-bit operations are not supported, then ASSERT().
221 If StartBit is greater than 7, then ASSERT().
222 If EndBit is greater than 7, then ASSERT().
223 If EndBit is less than StartBit, then ASSERT().
225 @param Operand Operand on which to perform the bitfield operation.
226 @param StartBit The ordinal of the least significant bit in the bit field.
228 @param EndBit The ordinal of the most significant bit in the bit field.
230 @param AndData The value to AND with the read value from the value.
232 @return The new 8-bit value.
244 ASSERT (EndBit
< sizeof (Operand
) * 8);
245 ASSERT (StartBit
<= EndBit
);
246 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
250 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
251 bitwise OR, and returns the result.
253 Performs a bitwise AND between the bit field specified by StartBit and EndBit
254 in Operand and the value specified by AndData, followed by a bitwise
255 inclusive OR with value specified by OrData. All other bits in Operand are
256 preserved. The new 8-bit value is returned.
258 If 8-bit operations are not supported, then ASSERT().
259 If StartBit is greater than 7, then ASSERT().
260 If EndBit is greater than 7, then ASSERT().
261 If EndBit is less than StartBit, then ASSERT().
263 @param Operand Operand on which to perform the bitfield operation.
264 @param StartBit The ordinal of the least significant bit in the bit field.
266 @param EndBit The ordinal of the most significant bit in the bit field.
268 @param AndData The value to AND with the read value from the value.
269 @param OrData The value to OR with the result of the AND operation.
271 @return The new 8-bit value.
284 ASSERT (EndBit
< sizeof (Operand
) * 8);
285 ASSERT (StartBit
<= EndBit
);
287 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
295 Returns a bit field from a 16-bit value.
297 Returns the bitfield specified by the StartBit and the EndBit from Operand.
299 If 16-bit operations are not supported, then ASSERT().
300 If StartBit is greater than 15, then ASSERT().
301 If EndBit is greater than 15, then ASSERT().
302 If EndBit is less than StartBit, then ASSERT().
304 @param Operand Operand on which to perform the bitfield operation.
305 @param StartBit The ordinal of the least significant bit in the bit field.
307 @param EndBit The ordinal of the most significant bit in the bit field.
310 @return The bit field read.
321 ASSERT (EndBit
< sizeof (Operand
) * 8);
322 ASSERT (StartBit
<= EndBit
);
323 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
327 Writes a bit field to a 16-bit value, and returns the result.
329 Writes Value to the bit field specified by the StartBit and the EndBit in
330 Operand. All other bits in Operand are preserved. The new 16-bit value is
333 If 16-bit operations are not supported, then ASSERT().
334 If StartBit is greater than 15, then ASSERT().
335 If EndBit is greater than 15, then ASSERT().
336 If EndBit is less than StartBit, then ASSERT().
338 @param Operand Operand on which to perform the bitfield operation.
339 @param StartBit The ordinal of the least significant bit in the bit field.
341 @param EndBit The ordinal of the most significant bit in the bit field.
343 @param Value New value of the bit field.
345 @return The new 16-bit value.
357 ASSERT (EndBit
< sizeof (Operand
) * 8);
358 ASSERT (StartBit
<= EndBit
);
359 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
363 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
366 Performs a bitwise inclusive OR between the bit field specified by StartBit
367 and EndBit in Operand and the value specified by OrData. All other bits in
368 Operand are preserved. The new 16-bit value is returned.
370 If 16-bit operations are not supported, then ASSERT().
371 If StartBit is greater than 15, then ASSERT().
372 If EndBit is greater than 15, then ASSERT().
373 If EndBit is less than StartBit, then ASSERT().
375 @param Operand Operand on which to perform the bitfield operation.
376 @param StartBit The ordinal of the least significant bit in the bit field.
378 @param EndBit The ordinal of the most significant bit in the bit field.
380 @param OrData The value to OR with the read value from the value
382 @return The new 16-bit value.
394 ASSERT (EndBit
< sizeof (Operand
) * 8);
395 ASSERT (StartBit
<= EndBit
);
396 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
400 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
403 Performs a bitwise AND between the bit field specified by StartBit and EndBit
404 in Operand and the value specified by AndData. All other bits in Operand are
405 preserved. The new 16-bit value is returned.
407 If 16-bit operations are not supported, then ASSERT().
408 If StartBit is greater than 15, then ASSERT().
409 If EndBit is greater than 15, then ASSERT().
410 If EndBit is less than StartBit, then ASSERT().
412 @param Operand Operand on which to perform the bitfield operation.
413 @param StartBit The ordinal of the least significant bit in the bit field.
415 @param EndBit The ordinal of the most significant bit in the bit field.
417 @param AndData The value to AND with the read value from the value
419 @return The new 16-bit value.
431 ASSERT (EndBit
< sizeof (Operand
) * 8);
432 ASSERT (StartBit
<= EndBit
);
433 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
437 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
438 bitwise OR, and returns the result.
440 Performs a bitwise AND between the bit field specified by StartBit and EndBit
441 in Operand and the value specified by AndData, followed by a bitwise
442 inclusive OR with value specified by OrData. All other bits in Operand are
443 preserved. The new 16-bit value is returned.
445 If 16-bit operations are not supported, then ASSERT().
446 If StartBit is greater than 15, then ASSERT().
447 If EndBit is greater than 15, then ASSERT().
448 If EndBit is less than StartBit, then ASSERT().
450 @param Operand Operand on which to perform the bitfield operation.
451 @param StartBit The ordinal of the least significant bit in the bit field.
453 @param EndBit The ordinal of the most significant bit in the bit field.
455 @param AndData The value to AND with the read value from the value.
456 @param OrData The value to OR with the result of the AND operation.
458 @return The new 16-bit value.
463 BitFieldAndThenOr16 (
471 ASSERT (EndBit
< sizeof (Operand
) * 8);
472 ASSERT (StartBit
<= EndBit
);
473 return BitFieldOr16 (
474 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
482 Returns a bit field from a 32-bit value.
484 Returns the bitfield specified by the StartBit and the EndBit from Operand.
486 If 32-bit operations are not supported, then ASSERT().
487 If StartBit is greater than 31, then ASSERT().
488 If EndBit is greater than 31, then ASSERT().
489 If EndBit is less than StartBit, then ASSERT().
491 @param Operand Operand on which to perform the bitfield operation.
492 @param StartBit The ordinal of the least significant bit in the bit field.
494 @param EndBit The ordinal of the most significant bit in the bit field.
497 @return The bit field read.
508 ASSERT (EndBit
< sizeof (Operand
) * 8);
509 ASSERT (StartBit
<= EndBit
);
510 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
514 Writes a bit field to a 32-bit value, and returns the result.
516 Writes Value to the bit field specified by the StartBit and the EndBit in
517 Operand. All other bits in Operand are preserved. The new 32-bit value is
520 If 32-bit operations are not supported, then ASSERT().
521 If StartBit is greater than 31, then ASSERT().
522 If EndBit is greater than 31, then ASSERT().
523 If EndBit is less than StartBit, then ASSERT().
525 @param Operand Operand on which to perform the bitfield operation.
526 @param StartBit The ordinal of the least significant bit in the bit field.
528 @param EndBit The ordinal of the most significant bit in the bit field.
530 @param Value New value of the bit field.
532 @return The new 32-bit value.
544 ASSERT (EndBit
< sizeof (Operand
) * 8);
545 ASSERT (StartBit
<= EndBit
);
546 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
550 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
553 Performs a bitwise inclusive OR between the bit field specified by StartBit
554 and EndBit in Operand and the value specified by OrData. All other bits in
555 Operand are preserved. The new 32-bit value is returned.
557 If 32-bit operations are not supported, then ASSERT().
558 If StartBit is greater than 31, then ASSERT().
559 If EndBit is greater than 31, then ASSERT().
560 If EndBit is less than StartBit, then ASSERT().
562 @param Operand Operand on which to perform the bitfield operation.
563 @param StartBit The ordinal of the least significant bit in the bit field.
565 @param EndBit The ordinal of the most significant bit in the bit field.
567 @param OrData The value to OR with the read value from the value
569 @return The new 32-bit value.
581 ASSERT (EndBit
< sizeof (Operand
) * 8);
582 ASSERT (StartBit
<= EndBit
);
583 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
587 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
590 Performs a bitwise AND between the bit field specified by StartBit and EndBit
591 in Operand and the value specified by AndData. All other bits in Operand are
592 preserved. The new 32-bit value is returned.
594 If 32-bit operations are not supported, then ASSERT().
595 If StartBit is greater than 31, then ASSERT().
596 If EndBit is greater than 31, then ASSERT().
597 If EndBit is less than StartBit, then ASSERT().
599 @param Operand Operand on which to perform the bitfield operation.
600 @param StartBit The ordinal of the least significant bit in the bit field.
602 @param EndBit The ordinal of the most significant bit in the bit field.
604 @param AndData The value to AND with the read value from the value
606 @return The new 32-bit value.
618 ASSERT (EndBit
< sizeof (Operand
) * 8);
619 ASSERT (StartBit
<= EndBit
);
620 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
624 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
625 bitwise OR, and returns the result.
627 Performs a bitwise AND between the bit field specified by StartBit and EndBit
628 in Operand and the value specified by AndData, followed by a bitwise
629 inclusive OR with value specified by OrData. All other bits in Operand are
630 preserved. The new 32-bit value is returned.
632 If 32-bit operations are not supported, then ASSERT().
633 If StartBit is greater than 31, then ASSERT().
634 If EndBit is greater than 31, then ASSERT().
635 If EndBit is less than StartBit, then ASSERT().
637 @param Operand Operand on which to perform the bitfield operation.
638 @param StartBit The ordinal of the least significant bit in the bit field.
640 @param EndBit The ordinal of the most significant bit in the bit field.
642 @param AndData The value to AND with the read value from the value.
643 @param OrData The value to OR with the result of the AND operation.
645 @return The new 32-bit value.
650 BitFieldAndThenOr32 (
658 ASSERT (EndBit
< sizeof (Operand
) * 8);
659 ASSERT (StartBit
<= EndBit
);
660 return BitFieldOr32 (
661 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
669 Returns a bit field from a 64-bit value.
671 Returns the bitfield specified by the StartBit and the EndBit from Operand.
673 If 64-bit operations are not supported, then ASSERT().
674 If StartBit is greater than 63, then ASSERT().
675 If EndBit is greater than 63, then ASSERT().
676 If EndBit is less than StartBit, then ASSERT().
678 @param Operand Operand on which to perform the bitfield operation.
679 @param StartBit The ordinal of the least significant bit in the bit field.
681 @param EndBit The ordinal of the most significant bit in the bit field.
684 @return The bit field read.
695 ASSERT (EndBit
< sizeof (Operand
) * 8);
696 ASSERT (StartBit
<= EndBit
);
697 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
701 Writes a bit field to a 64-bit value, and returns the result.
703 Writes Value to the bit field specified by the StartBit and the EndBit in
704 Operand. All other bits in Operand are preserved. The new 64-bit value is
707 If 64-bit operations are not supported, then ASSERT().
708 If StartBit is greater than 63, then ASSERT().
709 If EndBit is greater than 63, then ASSERT().
710 If EndBit is less than StartBit, then ASSERT().
712 @param Operand Operand on which to perform the bitfield operation.
713 @param StartBit The ordinal of the least significant bit in the bit field.
715 @param EndBit The ordinal of the most significant bit in the bit field.
717 @param Value New value of the bit field.
719 @return The new 64-bit value.
731 ASSERT (EndBit
< sizeof (Operand
) * 8);
732 ASSERT (StartBit
<= EndBit
);
733 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
737 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
740 Performs a bitwise inclusive OR between the bit field specified by StartBit
741 and EndBit in Operand and the value specified by OrData. All other bits in
742 Operand are preserved. The new 64-bit value is returned.
744 If 64-bit operations are not supported, then ASSERT().
745 If StartBit is greater than 63, then ASSERT().
746 If EndBit is greater than 63, then ASSERT().
747 If EndBit is less than StartBit, then ASSERT().
749 @param Operand Operand on which to perform the bitfield operation.
750 @param StartBit The ordinal of the least significant bit in the bit field.
752 @param EndBit The ordinal of the most significant bit in the bit field.
754 @param OrData The value to OR with the read value from the value
756 @return The new 64-bit value.
771 ASSERT (EndBit
< sizeof (Operand
) * 8);
772 ASSERT (StartBit
<= EndBit
);
774 Value1
= LShiftU64 (OrData
, StartBit
);
775 Value2
= LShiftU64 ((UINT64
) - 2, EndBit
);
777 return Operand
| (Value1
& ~Value2
);
781 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
784 Performs a bitwise AND between the bit field specified by StartBit and EndBit
785 in Operand and the value specified by AndData. All other bits in Operand are
786 preserved. The new 64-bit value is returned.
788 If 64-bit operations are not supported, then ASSERT().
789 If StartBit is greater than 63, then ASSERT().
790 If EndBit is greater than 63, then ASSERT().
791 If EndBit is less than StartBit, then ASSERT().
793 @param Operand Operand on which to perform the bitfield operation.
794 @param StartBit The ordinal of the least significant bit in the bit field.
796 @param EndBit The ordinal of the most significant bit in the bit field.
798 @param AndData The value to AND with the read value from the value
800 @return The new 64-bit value.
815 ASSERT (EndBit
< sizeof (Operand
) * 8);
816 ASSERT (StartBit
<= EndBit
);
818 Value1
= LShiftU64 (~AndData
, StartBit
);
819 Value2
= LShiftU64 ((UINT64
)-2, EndBit
);
821 return Operand
& ~(Value1
& ~Value2
);
825 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
826 bitwise OR, and returns the result.
828 Performs a bitwise AND between the bit field specified by StartBit and EndBit
829 in Operand and the value specified by AndData, followed by a bitwise
830 inclusive OR with value specified by OrData. All other bits in Operand are
831 preserved. The new 64-bit value is returned.
833 If 64-bit operations are not supported, then ASSERT().
834 If StartBit is greater than 63, then ASSERT().
835 If EndBit is greater than 63, then ASSERT().
836 If EndBit is less than StartBit, then ASSERT().
838 @param Operand Operand on which to perform the bitfield operation.
839 @param StartBit The ordinal of the least significant bit in the bit field.
841 @param EndBit The ordinal of the most significant bit in the bit field.
843 @param AndData The value to AND with the read value from the value.
844 @param OrData The value to OR with the result of the AND operation.
846 @return The new 64-bit value.
851 BitFieldAndThenOr64 (
859 ASSERT (EndBit
< sizeof (Operand
) * 8);
860 ASSERT (StartBit
<= EndBit
);
861 return BitFieldOr64 (
862 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),