]>
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
20 IN
unsigned int Operand
,
26 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
27 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
29 return (Operand
& ~((unsigned int)-2 << EndBit
)) >> StartBit
;
35 IN
unsigned int Operand
,
38 IN
unsigned int OrData
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
| ((OrData
<< StartBit
) & ~((unsigned int)-2 << EndBit
));
51 IN
unsigned int Operand
,
54 IN
unsigned int AndData
58 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
59 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
61 return Operand
& ~((~AndData
<< StartBit
) & ~((unsigned int)-2 << EndBit
));
65 Returns a bit field from an 8-bit value.
67 Returns the bitfield specified by the StartBit and the EndBit from Operand.
69 If 8-bit operations are not supported, then ASSERT().
70 If StartBit is greater than 7, then ASSERT().
71 If EndBit is greater than 7, then ASSERT().
72 If EndBit is less than StartBit, then ASSERT().
74 @param Operand Operand on which to perform the bitfield operation.
75 @param StartBit The ordinal of the least significant bit in the bit field.
77 @param EndBit The ordinal of the most significant bit in the bit field.
80 @return The bit field read.
91 ASSERT (EndBit
< sizeof (Operand
) * 8);
92 ASSERT (StartBit
<= EndBit
);
93 return (UINT8
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
97 Writes a bit field to an 8-bit value, and returns the result.
99 Writes Value to the bit field specified by the StartBit and the EndBit in
100 Operand. All other bits in Operand are preserved. The new 8-bit value is
103 If 8-bit operations are not supported, then ASSERT().
104 If StartBit is greater than 7, then ASSERT().
105 If EndBit is greater than 7, then ASSERT().
106 If EndBit is less than StartBit, then ASSERT().
108 @param Operand Operand on which to perform the bitfield operation.
109 @param StartBit The ordinal of the least significant bit in the bit field.
111 @param EndBit The ordinal of the most significant bit in the bit field.
113 @param Value New value of the bit field.
115 @return The new 8-bit value.
127 ASSERT (EndBit
< sizeof (Operand
) * 8);
128 ASSERT (StartBit
<= EndBit
);
129 return BitFieldAndThenOr8 (Operand
, StartBit
, EndBit
, 0, Value
);
133 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
136 Performs a bitwise inclusive OR between the bit field specified by StartBit
137 and EndBit in Operand and the value specified by OrData. All other bits in
138 Operand are preserved. The new 8-bit value is returned.
140 If 8-bit operations are not supported, then ASSERT().
141 If StartBit is greater than 7, then ASSERT().
142 If EndBit is greater than 7, then ASSERT().
143 If EndBit is less than StartBit, then ASSERT().
145 @param Operand Operand on which to perform the bitfield operation.
146 @param StartBit The ordinal of the least significant bit in the bit field.
148 @param EndBit The ordinal of the most significant bit in the bit field.
150 @param OrData The value to OR with the read value from the value
152 @return The new 8-bit value.
164 ASSERT (EndBit
< sizeof (Operand
) * 8);
165 ASSERT (StartBit
<= EndBit
);
166 return (UINT8
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
170 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
173 Performs a bitwise AND between the bit field specified by StartBit and EndBit
174 in Operand and the value specified by AndData. All other bits in Operand are
175 preserved. The new 8-bit value is returned.
177 If 8-bit operations are not supported, then ASSERT().
178 If StartBit is greater than 7, then ASSERT().
179 If EndBit is greater than 7, then ASSERT().
180 If EndBit is less than StartBit, then ASSERT().
182 @param Operand Operand on which to perform the bitfield operation.
183 @param StartBit The ordinal of the least significant bit in the bit field.
185 @param EndBit The ordinal of the most significant bit in the bit field.
187 @param AndData The value to AND with the read value from the value.
189 @return The new 8-bit value.
201 ASSERT (EndBit
< sizeof (Operand
) * 8);
202 ASSERT (StartBit
<= EndBit
);
203 return (UINT8
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
207 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
208 bitwise OR, and returns the result.
210 Performs a bitwise AND between the bit field specified by StartBit and EndBit
211 in Operand and the value specified by AndData, followed by a bitwise
212 inclusive OR with value specified by OrData. All other bits in Operand are
213 preserved. The new 8-bit value is returned.
215 If 8-bit operations are not supported, then ASSERT().
216 If StartBit is greater than 7, then ASSERT().
217 If EndBit is greater than 7, then ASSERT().
218 If EndBit is less than StartBit, then ASSERT().
220 @param Operand Operand on which to perform the bitfield operation.
221 @param StartBit The ordinal of the least significant bit in the bit field.
223 @param EndBit The ordinal of the most significant bit in the bit field.
225 @param AndData The value to AND with the read value from the value.
226 @param OrData The value to OR with the result of the AND operation.
228 @return The new 8-bit value.
241 ASSERT (EndBit
< sizeof (Operand
) * 8);
242 ASSERT (StartBit
<= EndBit
);
244 BitFieldAnd8 (Operand
, StartBit
, EndBit
, AndData
),
252 Returns a bit field from a 16-bit value.
254 Returns the bitfield specified by the StartBit and the EndBit from Operand.
256 If 16-bit operations are not supported, then ASSERT().
257 If StartBit is greater than 15, then ASSERT().
258 If EndBit is greater than 15, 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.
267 @return The bit field read.
278 ASSERT (EndBit
< sizeof (Operand
) * 8);
279 ASSERT (StartBit
<= EndBit
);
280 return (UINT16
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
284 Writes a bit field to a 16-bit value, and returns the result.
286 Writes Value to the bit field specified by the StartBit and the EndBit in
287 Operand. All other bits in Operand are preserved. The new 16-bit value is
290 If 16-bit operations are not supported, then ASSERT().
291 If StartBit is greater than 15, then ASSERT().
292 If EndBit is greater than 15, then ASSERT().
293 If EndBit is less than StartBit, then ASSERT().
295 @param Operand Operand on which to perform the bitfield operation.
296 @param StartBit The ordinal of the least significant bit in the bit field.
298 @param EndBit The ordinal of the most significant bit in the bit field.
300 @param Value New value of the bit field.
302 @return The new 16-bit value.
314 ASSERT (EndBit
< sizeof (Operand
) * 8);
315 ASSERT (StartBit
<= EndBit
);
316 return BitFieldAndThenOr16 (Operand
, StartBit
, EndBit
, 0, Value
);
320 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
323 Performs a bitwise inclusive OR between the bit field specified by StartBit
324 and EndBit in Operand and the value specified by OrData. All other bits in
325 Operand are preserved. The new 16-bit value is returned.
327 If 16-bit operations are not supported, then ASSERT().
328 If StartBit is greater than 15, then ASSERT().
329 If EndBit is greater than 15, then ASSERT().
330 If EndBit is less than StartBit, then ASSERT().
332 @param Operand Operand on which to perform the bitfield operation.
333 @param StartBit The ordinal of the least significant bit in the bit field.
335 @param EndBit The ordinal of the most significant bit in the bit field.
337 @param OrData The value to OR with the read value from the value
339 @return The new 16-bit value.
351 ASSERT (EndBit
< sizeof (Operand
) * 8);
352 ASSERT (StartBit
<= EndBit
);
353 return (UINT16
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
357 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
360 Performs a bitwise AND between the bit field specified by StartBit and EndBit
361 in Operand and the value specified by AndData. All other bits in Operand are
362 preserved. The new 16-bit value is returned.
364 If 16-bit operations are not supported, then ASSERT().
365 If StartBit is greater than 15, then ASSERT().
366 If EndBit is greater than 15, then ASSERT().
367 If EndBit is less than StartBit, then ASSERT().
369 @param Operand Operand on which to perform the bitfield operation.
370 @param StartBit The ordinal of the least significant bit in the bit field.
372 @param EndBit The ordinal of the most significant bit in the bit field.
374 @param AndData The value to AND with the read value from the value
376 @return The new 16-bit value.
388 ASSERT (EndBit
< sizeof (Operand
) * 8);
389 ASSERT (StartBit
<= EndBit
);
390 return (UINT16
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
394 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
395 bitwise OR, and returns the result.
397 Performs a bitwise AND between the bit field specified by StartBit and EndBit
398 in Operand and the value specified by AndData, followed by a bitwise
399 inclusive OR with value specified by OrData. All other bits in Operand are
400 preserved. The new 16-bit value is returned.
402 If 16-bit operations are not supported, then ASSERT().
403 If StartBit is greater than 15, then ASSERT().
404 If EndBit is greater than 15, then ASSERT().
405 If EndBit is less than StartBit, then ASSERT().
407 @param Operand Operand on which to perform the bitfield operation.
408 @param StartBit The ordinal of the least significant bit in the bit field.
410 @param EndBit The ordinal of the most significant bit in the bit field.
412 @param AndData The value to AND with the read value from the value.
413 @param OrData The value to OR with the result of the AND operation.
415 @return The new 16-bit value.
420 BitFieldAndThenOr16 (
428 ASSERT (EndBit
< sizeof (Operand
) * 8);
429 ASSERT (StartBit
<= EndBit
);
430 return BitFieldOr16 (
431 BitFieldAnd16 (Operand
, StartBit
, EndBit
, AndData
),
439 Returns a bit field from a 32-bit value.
441 Returns the bitfield specified by the StartBit and the EndBit from Operand.
443 If 32-bit operations are not supported, then ASSERT().
444 If StartBit is greater than 31, then ASSERT().
445 If EndBit is greater than 31, 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.
454 @return The bit field read.
465 ASSERT (EndBit
< sizeof (Operand
) * 8);
466 ASSERT (StartBit
<= EndBit
);
467 return (UINT32
)BitFieldReadUint (Operand
, StartBit
, EndBit
);
471 Writes a bit field to a 32-bit value, and returns the result.
473 Writes Value to the bit field specified by the StartBit and the EndBit in
474 Operand. All other bits in Operand are preserved. The new 32-bit value is
477 If 32-bit operations are not supported, then ASSERT().
478 If StartBit is greater than 31, then ASSERT().
479 If EndBit is greater than 31, then ASSERT().
480 If EndBit is less than StartBit, then ASSERT().
482 @param Operand Operand on which to perform the bitfield operation.
483 @param StartBit The ordinal of the least significant bit in the bit field.
485 @param EndBit The ordinal of the most significant bit in the bit field.
487 @param Value New value of the bit field.
489 @return The new 32-bit value.
501 ASSERT (EndBit
< sizeof (Operand
) * 8);
502 ASSERT (StartBit
<= EndBit
);
503 return BitFieldAndThenOr32 (Operand
, StartBit
, EndBit
, 0, Value
);
507 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
510 Performs a bitwise inclusive OR between the bit field specified by StartBit
511 and EndBit in Operand and the value specified by OrData. All other bits in
512 Operand are preserved. The new 32-bit value is returned.
514 If 32-bit operations are not supported, then ASSERT().
515 If StartBit is greater than 31, then ASSERT().
516 If EndBit is greater than 31, then ASSERT().
517 If EndBit is less than StartBit, then ASSERT().
519 @param Operand Operand on which to perform the bitfield operation.
520 @param StartBit The ordinal of the least significant bit in the bit field.
522 @param EndBit The ordinal of the most significant bit in the bit field.
524 @param OrData The value to OR with the read value from the value
526 @return The new 32-bit value.
538 ASSERT (EndBit
< sizeof (Operand
) * 8);
539 ASSERT (StartBit
<= EndBit
);
540 return (UINT32
)BitFieldOrUint (Operand
, StartBit
, EndBit
, OrData
);
544 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
547 Performs a bitwise AND between the bit field specified by StartBit and EndBit
548 in Operand and the value specified by AndData. All other bits in Operand are
549 preserved. The new 32-bit value is returned.
551 If 32-bit operations are not supported, then ASSERT().
552 If StartBit is greater than 31, then ASSERT().
553 If EndBit is greater than 31, then ASSERT().
554 If EndBit is less than StartBit, then ASSERT().
556 @param Operand Operand on which to perform the bitfield operation.
557 @param StartBit The ordinal of the least significant bit in the bit field.
559 @param EndBit The ordinal of the most significant bit in the bit field.
561 @param AndData The value to AND with the read value from the value
563 @return The new 32-bit value.
575 ASSERT (EndBit
< sizeof (Operand
) * 8);
576 ASSERT (StartBit
<= EndBit
);
577 return (UINT32
)BitFieldAndUint (Operand
, StartBit
, EndBit
, AndData
);
581 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
582 bitwise OR, and returns the result.
584 Performs a bitwise AND between the bit field specified by StartBit and EndBit
585 in Operand and the value specified by AndData, followed by a bitwise
586 inclusive OR with value specified by OrData. All other bits in Operand are
587 preserved. The new 32-bit value is returned.
589 If 32-bit operations are not supported, then ASSERT().
590 If StartBit is greater than 31, then ASSERT().
591 If EndBit is greater than 31, then ASSERT().
592 If EndBit is less than StartBit, then ASSERT().
594 @param Operand Operand on which to perform the bitfield operation.
595 @param StartBit The ordinal of the least significant bit in the bit field.
597 @param EndBit The ordinal of the most significant bit in the bit field.
599 @param AndData The value to AND with the read value from the value.
600 @param OrData The value to OR with the result of the AND operation.
602 @return The new 32-bit value.
607 BitFieldAndThenOr32 (
615 ASSERT (EndBit
< sizeof (Operand
) * 8);
616 ASSERT (StartBit
<= EndBit
);
617 return BitFieldOr32 (
618 BitFieldAnd32 (Operand
, StartBit
, EndBit
, AndData
),
626 Returns a bit field from a 64-bit value.
628 Returns the bitfield specified by the StartBit and the EndBit from Operand.
630 If 64-bit operations are not supported, then ASSERT().
631 If StartBit is greater than 63, then ASSERT().
632 If EndBit is greater than 63, 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.
641 @return The bit field read.
652 ASSERT (EndBit
< sizeof (Operand
) * 8);
653 ASSERT (StartBit
<= EndBit
);
654 return RShiftU64 (Operand
& ~LShiftU64 ((UINT64
)-2, EndBit
), StartBit
);
658 Writes a bit field to a 64-bit value, and returns the result.
660 Writes Value to the bit field specified by the StartBit and the EndBit in
661 Operand. All other bits in Operand are preserved. The new 64-bit value is
664 If 64-bit operations are not supported, then ASSERT().
665 If StartBit is greater than 63, then ASSERT().
666 If EndBit is greater than 63, then ASSERT().
667 If EndBit is less than StartBit, then ASSERT().
669 @param Operand Operand on which to perform the bitfield operation.
670 @param StartBit The ordinal of the least significant bit in the bit field.
672 @param EndBit The ordinal of the most significant bit in the bit field.
674 @param Value New value of the bit field.
676 @return The new 64-bit value.
688 ASSERT (EndBit
< sizeof (Operand
) * 8);
689 ASSERT (StartBit
<= EndBit
);
690 return BitFieldAndThenOr64 (Operand
, StartBit
, EndBit
, 0, Value
);
694 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
697 Performs a bitwise inclusive OR between the bit field specified by StartBit
698 and EndBit in Operand and the value specified by OrData. All other bits in
699 Operand are preserved. The new 64-bit value is returned.
701 If 64-bit operations are not supported, then ASSERT().
702 If StartBit is greater than 63, then ASSERT().
703 If EndBit is greater than 63, then ASSERT().
704 If EndBit is less than StartBit, then ASSERT().
706 @param Operand Operand on which to perform the bitfield operation.
707 @param StartBit The ordinal of the least significant bit in the bit field.
709 @param EndBit The ordinal of the most significant bit in the bit field.
711 @param OrData The value to OR with the read value from the value
713 @return The new 64-bit value.
725 ASSERT (EndBit
< sizeof (Operand
) * 8);
726 ASSERT (StartBit
<= EndBit
);
728 (LShiftU64 (OrData
, StartBit
) & ~LShiftU64 ((UINT64
)-2, EndBit
));
732 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
735 Performs a bitwise AND between the bit field specified by StartBit and EndBit
736 in Operand and the value specified by AndData. All other bits in Operand are
737 preserved. The new 64-bit value is returned.
739 If 64-bit operations are not supported, then ASSERT().
740 If StartBit is greater than 63, then ASSERT().
741 If EndBit is greater than 63, then ASSERT().
742 If EndBit is less than StartBit, then ASSERT().
744 @param Operand Operand on which to perform the bitfield operation.
745 @param StartBit The ordinal of the least significant bit in the bit field.
747 @param EndBit The ordinal of the most significant bit in the bit field.
749 @param AndData The value to AND with the read value from the value
751 @return The new 64-bit value.
763 ASSERT (EndBit
< sizeof (Operand
) * 8);
764 ASSERT (StartBit
<= EndBit
);
766 ~(LShiftU64 (~AndData
, StartBit
) & ~LShiftU64 ((UINT64
)-2, EndBit
));
770 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
771 bitwise OR, and returns the result.
773 Performs a bitwise AND between the bit field specified by StartBit and EndBit
774 in Operand and the value specified by AndData, followed by a bitwise
775 inclusive OR with value specified by OrData. All other bits in Operand are
776 preserved. The new 64-bit value is returned.
778 If 64-bit operations are not supported, then ASSERT().
779 If StartBit is greater than 63, then ASSERT().
780 If EndBit is greater than 63, then ASSERT().
781 If EndBit is less than StartBit, then ASSERT().
783 @param Operand Operand on which to perform the bitfield operation.
784 @param StartBit The ordinal of the least significant bit in the bit field.
786 @param EndBit The ordinal of the most significant bit in the bit field.
788 @param AndData The value to AND with the read value from the value.
789 @param OrData The value to OR with the result of the AND operation.
791 @return The new 64-bit value.
796 BitFieldAndThenOr64 (
804 ASSERT (EndBit
< sizeof (Operand
) * 8);
805 ASSERT (StartBit
<= EndBit
);
806 return BitFieldOr64 (
807 BitFieldAnd64 (Operand
, StartBit
, EndBit
, AndData
),