TianoCore EDK2 master
Loading...
Searching...
No Matches
BitField.c
Go to the documentation of this file.
1
9#include "BaseLibInternals.h"
10
24EFIAPI
26 IN UINTN Operand,
27 IN UINTN StartBit,
28 IN UINTN EndBit
29 )
30{
31 //
32 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
33 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
34 //
35 return (Operand & ~((UINTN)-2 << EndBit)) >> StartBit;
36}
37
57EFIAPI
59 IN UINTN Operand,
60 IN UINTN StartBit,
61 IN UINTN EndBit,
62 IN UINTN OrData
63 )
64{
65 //
66 // Higher bits in OrData those are not used must be zero.
67 //
68 // EndBit - StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,
69 // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.
70 //
71 ASSERT ((OrData >> (EndBit - StartBit)) == ((OrData >> (EndBit - StartBit)) & 1));
72
73 //
74 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
75 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
76 //
77 return Operand | ((OrData << StartBit) & ~((UINTN)-2 << EndBit));
78}
79
99EFIAPI
101 IN UINTN Operand,
102 IN UINTN StartBit,
103 IN UINTN EndBit,
104 IN UINTN AndData
105 )
106{
107 //
108 // Higher bits in AndData those are not used must be zero.
109 //
110 // EndBit - StartBit + 1 might be 32 while the result right shifting 32 on a 32bit integer is undefined,
111 // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.
112 //
113 ASSERT ((AndData >> (EndBit - StartBit)) == ((AndData >> (EndBit - StartBit)) & 1));
114
115 //
116 // ~((UINTN)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
117 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
118 //
119 return Operand & ~((~AndData << StartBit) & ~((UINTN)-2 << EndBit));
120}
121
141UINT8
142EFIAPI
144 IN UINT8 Operand,
145 IN UINTN StartBit,
146 IN UINTN EndBit
147 )
148{
149 ASSERT (EndBit < 8);
150 ASSERT (StartBit <= EndBit);
151 return (UINT8)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);
152}
153
177UINT8
178EFIAPI
180 IN UINT8 Operand,
181 IN UINTN StartBit,
182 IN UINTN EndBit,
183 IN UINT8 Value
184 )
185{
186 ASSERT (EndBit < 8);
187 ASSERT (StartBit <= EndBit);
188 return BitFieldAndThenOr8 (Operand, StartBit, EndBit, 0, Value);
189}
190
215UINT8
216EFIAPI
218 IN UINT8 Operand,
219 IN UINTN StartBit,
220 IN UINTN EndBit,
221 IN UINT8 OrData
222 )
223{
224 ASSERT (EndBit < 8);
225 ASSERT (StartBit <= EndBit);
226 return (UINT8)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);
227}
228
253UINT8
254EFIAPI
256 IN UINT8 Operand,
257 IN UINTN StartBit,
258 IN UINTN EndBit,
259 IN UINT8 AndData
260 )
261{
262 ASSERT (EndBit < 8);
263 ASSERT (StartBit <= EndBit);
264 return (UINT8)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);
265}
266
294UINT8
295EFIAPI
297 IN UINT8 Operand,
298 IN UINTN StartBit,
299 IN UINTN EndBit,
300 IN UINT8 AndData,
301 IN UINT8 OrData
302 )
303{
304 ASSERT (EndBit < 8);
305 ASSERT (StartBit <= EndBit);
306 return BitFieldOr8 (
307 BitFieldAnd8 (Operand, StartBit, EndBit, AndData),
308 StartBit,
309 EndBit,
310 OrData
311 );
312}
313
333UINT16
334EFIAPI
336 IN UINT16 Operand,
337 IN UINTN StartBit,
338 IN UINTN EndBit
339 )
340{
341 ASSERT (EndBit < 16);
342 ASSERT (StartBit <= EndBit);
343 return (UINT16)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);
344}
345
369UINT16
370EFIAPI
372 IN UINT16 Operand,
373 IN UINTN StartBit,
374 IN UINTN EndBit,
375 IN UINT16 Value
376 )
377{
378 ASSERT (EndBit < 16);
379 ASSERT (StartBit <= EndBit);
380 return BitFieldAndThenOr16 (Operand, StartBit, EndBit, 0, Value);
381}
382
407UINT16
408EFIAPI
410 IN UINT16 Operand,
411 IN UINTN StartBit,
412 IN UINTN EndBit,
413 IN UINT16 OrData
414 )
415{
416 ASSERT (EndBit < 16);
417 ASSERT (StartBit <= EndBit);
418 return (UINT16)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);
419}
420
445UINT16
446EFIAPI
448 IN UINT16 Operand,
449 IN UINTN StartBit,
450 IN UINTN EndBit,
451 IN UINT16 AndData
452 )
453{
454 ASSERT (EndBit < 16);
455 ASSERT (StartBit <= EndBit);
456 return (UINT16)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);
457}
458
486UINT16
487EFIAPI
489 IN UINT16 Operand,
490 IN UINTN StartBit,
491 IN UINTN EndBit,
492 IN UINT16 AndData,
493 IN UINT16 OrData
494 )
495{
496 ASSERT (EndBit < 16);
497 ASSERT (StartBit <= EndBit);
498 return BitFieldOr16 (
499 BitFieldAnd16 (Operand, StartBit, EndBit, AndData),
500 StartBit,
501 EndBit,
502 OrData
503 );
504}
505
525UINT32
526EFIAPI
528 IN UINT32 Operand,
529 IN UINTN StartBit,
530 IN UINTN EndBit
531 )
532{
533 ASSERT (EndBit < 32);
534 ASSERT (StartBit <= EndBit);
535 return (UINT32)InternalBaseLibBitFieldReadUint (Operand, StartBit, EndBit);
536}
537
561UINT32
562EFIAPI
564 IN UINT32 Operand,
565 IN UINTN StartBit,
566 IN UINTN EndBit,
567 IN UINT32 Value
568 )
569{
570 ASSERT (EndBit < 32);
571 ASSERT (StartBit <= EndBit);
572 return BitFieldAndThenOr32 (Operand, StartBit, EndBit, 0, Value);
573}
574
599UINT32
600EFIAPI
602 IN UINT32 Operand,
603 IN UINTN StartBit,
604 IN UINTN EndBit,
605 IN UINT32 OrData
606 )
607{
608 ASSERT (EndBit < 32);
609 ASSERT (StartBit <= EndBit);
610 return (UINT32)InternalBaseLibBitFieldOrUint (Operand, StartBit, EndBit, OrData);
611}
612
637UINT32
638EFIAPI
640 IN UINT32 Operand,
641 IN UINTN StartBit,
642 IN UINTN EndBit,
643 IN UINT32 AndData
644 )
645{
646 ASSERT (EndBit < 32);
647 ASSERT (StartBit <= EndBit);
648 return (UINT32)InternalBaseLibBitFieldAndUint (Operand, StartBit, EndBit, AndData);
649}
650
678UINT32
679EFIAPI
681 IN UINT32 Operand,
682 IN UINTN StartBit,
683 IN UINTN EndBit,
684 IN UINT32 AndData,
685 IN UINT32 OrData
686 )
687{
688 ASSERT (EndBit < 32);
689 ASSERT (StartBit <= EndBit);
690 return BitFieldOr32 (
691 BitFieldAnd32 (Operand, StartBit, EndBit, AndData),
692 StartBit,
693 EndBit,
694 OrData
695 );
696}
697
717UINT64
718EFIAPI
720 IN UINT64 Operand,
721 IN UINTN StartBit,
722 IN UINTN EndBit
723 )
724{
725 ASSERT (EndBit < 64);
726 ASSERT (StartBit <= EndBit);
727 return RShiftU64 (Operand & ~LShiftU64 ((UINT64)-2, EndBit), StartBit);
728}
729
753UINT64
754EFIAPI
756 IN UINT64 Operand,
757 IN UINTN StartBit,
758 IN UINTN EndBit,
759 IN UINT64 Value
760 )
761{
762 ASSERT (EndBit < 64);
763 ASSERT (StartBit <= EndBit);
764 return BitFieldAndThenOr64 (Operand, StartBit, EndBit, 0, Value);
765}
766
791UINT64
792EFIAPI
794 IN UINT64 Operand,
795 IN UINTN StartBit,
796 IN UINTN EndBit,
797 IN UINT64 OrData
798 )
799{
800 UINT64 Value1;
801 UINT64 Value2;
802
803 ASSERT (EndBit < 64);
804 ASSERT (StartBit <= EndBit);
805 //
806 // Higher bits in OrData those are not used must be zero.
807 //
808 // EndBit - StartBit + 1 might be 64 while the result right shifting 64 on RShiftU64() API is invalid,
809 // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.
810 //
811 ASSERT (RShiftU64 (OrData, EndBit - StartBit) == (RShiftU64 (OrData, EndBit - StartBit) & 1));
812
813 Value1 = LShiftU64 (OrData, StartBit);
814 Value2 = LShiftU64 ((UINT64)-2, EndBit);
815
816 return Operand | (Value1 & ~Value2);
817}
818
843UINT64
844EFIAPI
846 IN UINT64 Operand,
847 IN UINTN StartBit,
848 IN UINTN EndBit,
849 IN UINT64 AndData
850 )
851{
852 UINT64 Value1;
853 UINT64 Value2;
854
855 ASSERT (EndBit < 64);
856 ASSERT (StartBit <= EndBit);
857 //
858 // Higher bits in AndData those are not used must be zero.
859 //
860 // EndBit - StartBit + 1 might be 64 while the right shifting 64 on RShiftU64() API is invalid,
861 // So the logic is updated to right shift (EndBit - StartBit) bits and compare the last bit directly.
862 //
863 ASSERT (RShiftU64 (AndData, EndBit - StartBit) == (RShiftU64 (AndData, EndBit - StartBit) & 1));
864
865 Value1 = LShiftU64 (~AndData, StartBit);
866 Value2 = LShiftU64 ((UINT64)-2, EndBit);
867
868 return Operand & ~(Value1 & ~Value2);
869}
870
898UINT64
899EFIAPI
901 IN UINT64 Operand,
902 IN UINTN StartBit,
903 IN UINTN EndBit,
904 IN UINT64 AndData,
905 IN UINT64 OrData
906 )
907{
908 ASSERT (EndBit < 64);
909 ASSERT (StartBit <= EndBit);
910 return BitFieldOr64 (
911 BitFieldAnd64 (Operand, StartBit, EndBit, AndData),
912 StartBit,
913 EndBit,
914 OrData
915 );
916}
917
938UINT8
939EFIAPI
941 IN UINT32 Operand,
942 IN UINTN StartBit,
943 IN UINTN EndBit
944 )
945{
946 UINT32 Count;
947
948 ASSERT (EndBit < 32);
949 ASSERT (StartBit <= EndBit);
950
951 Count = BitFieldRead32 (Operand, StartBit, EndBit);
952 Count -= ((Count >> 1) & 0x55555555);
953 Count = (Count & 0x33333333) + ((Count >> 2) & 0x33333333);
954 Count += Count >> 4;
955 Count &= 0x0F0F0F0F;
956 Count += Count >> 8;
957 Count += Count >> 16;
958
959 return (UINT8)Count & 0x3F;
960}
961
982UINT8
983EFIAPI
985 IN UINT64 Operand,
986 IN UINTN StartBit,
987 IN UINTN EndBit
988 )
989{
990 UINT64 BitField;
991 UINT8 Count;
992
993 ASSERT (EndBit < 64);
994 ASSERT (StartBit <= EndBit);
995
996 BitField = BitFieldRead64 (Operand, StartBit, EndBit);
997 Count = BitFieldCountOnes32 ((UINT32)BitField, 0, 31);
998 Count += BitFieldCountOnes32 ((UINT32)RShiftU64 (BitField, 32), 0, 31);
999
1000 return Count;
1001}
UINT64 UINTN
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: RShiftU64.c:28
UINT64 EFIAPI LShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition: LShiftU64.c:28
UINT32 EFIAPI BitFieldAnd32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT32 AndData)
Definition: BitField.c:639
UINT64 EFIAPI BitFieldOr64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT64 OrData)
Definition: BitField.c:793
UINT8 EFIAPI BitFieldCountOnes64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:984
UINT16 EFIAPI BitFieldAnd16(IN UINT16 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT16 AndData)
Definition: BitField.c:447
UINT64 EFIAPI BitFieldAndThenOr64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT64 AndData, IN UINT64 OrData)
Definition: BitField.c:900
UINT8 EFIAPI BitFieldAndThenOr8(IN UINT8 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT8 AndData, IN UINT8 OrData)
Definition: BitField.c:296
UINT32 EFIAPI BitFieldAndThenOr32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT32 AndData, IN UINT32 OrData)
Definition: BitField.c:680
UINTN EFIAPI InternalBaseLibBitFieldAndUint(IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINTN AndData)
Definition: BitField.c:100
UINTN EFIAPI InternalBaseLibBitFieldReadUint(IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:25
UINT32 EFIAPI BitFieldWrite32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT32 Value)
Definition: BitField.c:563
UINT16 EFIAPI BitFieldAndThenOr16(IN UINT16 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT16 AndData, IN UINT16 OrData)
Definition: BitField.c:488
UINT32 EFIAPI BitFieldOr32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT32 OrData)
Definition: BitField.c:601
UINT64 EFIAPI BitFieldWrite64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT64 Value)
Definition: BitField.c:755
UINT8 EFIAPI BitFieldWrite8(IN UINT8 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT8 Value)
Definition: BitField.c:179
UINT8 EFIAPI BitFieldRead8(IN UINT8 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:143
UINT8 EFIAPI BitFieldAnd8(IN UINT8 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT8 AndData)
Definition: BitField.c:255
UINTN EFIAPI InternalBaseLibBitFieldOrUint(IN UINTN Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINTN OrData)
Definition: BitField.c:58
UINT16 EFIAPI BitFieldWrite16(IN UINT16 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT16 Value)
Definition: BitField.c:371
UINT64 EFIAPI BitFieldRead64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:719
UINT64 EFIAPI BitFieldAnd64(IN UINT64 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT64 AndData)
Definition: BitField.c:845
UINT16 EFIAPI BitFieldOr16(IN UINT16 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT16 OrData)
Definition: BitField.c:409
UINT8 EFIAPI BitFieldCountOnes32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:940
UINT16 EFIAPI BitFieldRead16(IN UINT16 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:335
UINT8 EFIAPI BitFieldOr8(IN UINT8 Operand, IN UINTN StartBit, IN UINTN EndBit, IN UINT8 OrData)
Definition: BitField.c:217
UINT32 EFIAPI BitFieldRead32(IN UINT32 Operand, IN UINTN StartBit, IN UINTN EndBit)
Definition: BitField.c:527
#define IN
Definition: Base.h:279