]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/BitField.c
9c9981e31457b26ebb7cf1350cbc4b165dde6464
[mirror_edk2.git] / MdePkg / Library / BaseLib / BitField.c
1 /** @file
2 Bit field functions of BaseLib.
3
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
9
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.
12
13 Module Name: BitField.c
14
15 **/
16
17 //
18 // Include common header file for this module.
19 //
20 #include "CommonHeader.h"
21
22 #include "BaseLibInternals.h"
23
24 /**
25 Worker function that returns a bit field from Operand
26
27 Returns the bitfield specified by the StartBit and the EndBit from Operand.
28
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.
32
33 @return The bit field read.
34
35 **/
36 unsigned int
37 BitFieldReadUint (
38 IN unsigned int Operand,
39 IN UINTN StartBit,
40 IN UINTN EndBit
41 )
42 {
43 //
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.
46 //
47 return (Operand & ~((unsigned int)-2 << EndBit)) >> StartBit;
48 }
49
50 /**
51 Worker function that reads a bit field from Operand, performs a bitwise OR,
52 and returns the result.
53
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.
57
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
62
63 @return The new value.
64
65 **/
66 unsigned int
67 BitFieldOrUint (
68 IN unsigned int Operand,
69 IN UINTN StartBit,
70 IN UINTN EndBit,
71 IN unsigned int OrData
72 )
73 {
74 //
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.
77 //
78 return Operand | ((OrData << StartBit) & ~((unsigned int) -2 << EndBit));
79 }
80
81 /**
82 Worker function that reads a bit field from Operand, performs a bitwise AND,
83 and returns the result.
84
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.
88
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
93
94 @return The new value.
95
96 **/
97 unsigned int
98 BitFieldAndUint (
99 IN unsigned int Operand,
100 IN UINTN StartBit,
101 IN UINTN EndBit,
102 IN unsigned int AndData
103 )
104 {
105 //
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.
108 //
109 return Operand & ~((~AndData << StartBit) & ~((unsigned int) -2 << EndBit));
110 }
111
112 /**
113 Returns a bit field from an 8-bit value.
114
115 Returns the bitfield specified by the StartBit and the EndBit from Operand.
116
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().
121
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.
124 Range 0..7.
125 @param EndBit The ordinal of the most significant bit in the bit field.
126 Range 0..7.
127
128 @return The bit field read.
129
130 **/
131 UINT8
132 EFIAPI
133 BitFieldRead8 (
134 IN UINT8 Operand,
135 IN UINTN StartBit,
136 IN UINTN EndBit
137 )
138 {
139 ASSERT (EndBit < sizeof (Operand) * 8);
140 ASSERT (StartBit <= EndBit);
141 return (UINT8)BitFieldReadUint (Operand, StartBit, EndBit);
142 }
143
144 /**
145 Writes a bit field to an 8-bit value, and returns the result.
146
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
149 returned.
150
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().
155
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.
158 Range 0..7.
159 @param EndBit The ordinal of the most significant bit in the bit field.
160 Range 0..7.
161 @param Value New value of the bit field.
162
163 @return The new 8-bit value.
164
165 **/
166 UINT8
167 EFIAPI
168 BitFieldWrite8 (
169 IN UINT8 Operand,
170 IN UINTN StartBit,
171 IN UINTN EndBit,
172 IN UINT8 Value
173 )
174 {
175 ASSERT (EndBit < sizeof (Operand) * 8);
176 ASSERT (StartBit <= EndBit);
177 return BitFieldAndThenOr8 (Operand, StartBit, EndBit, 0, Value);
178 }
179
180 /**
181 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
182 result.
183
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.
187
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().
192
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.
195 Range 0..7.
196 @param EndBit The ordinal of the most significant bit in the bit field.
197 Range 0..7.
198 @param OrData The value to OR with the read value from the value
199
200 @return The new 8-bit value.
201
202 **/
203 UINT8
204 EFIAPI
205 BitFieldOr8 (
206 IN UINT8 Operand,
207 IN UINTN StartBit,
208 IN UINTN EndBit,
209 IN UINT8 OrData
210 )
211 {
212 ASSERT (EndBit < sizeof (Operand) * 8);
213 ASSERT (StartBit <= EndBit);
214 return (UINT8)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
215 }
216
217 /**
218 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
219 the result.
220
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.
224
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().
229
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.
232 Range 0..7.
233 @param EndBit The ordinal of the most significant bit in the bit field.
234 Range 0..7.
235 @param AndData The value to AND with the read value from the value.
236
237 @return The new 8-bit value.
238
239 **/
240 UINT8
241 EFIAPI
242 BitFieldAnd8 (
243 IN UINT8 Operand,
244 IN UINTN StartBit,
245 IN UINTN EndBit,
246 IN UINT8 AndData
247 )
248 {
249 ASSERT (EndBit < sizeof (Operand) * 8);
250 ASSERT (StartBit <= EndBit);
251 return (UINT8)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
252 }
253
254 /**
255 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
256 bitwise OR, and returns the result.
257
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.
262
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().
267
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.
270 Range 0..7.
271 @param EndBit The ordinal of the most significant bit in the bit field.
272 Range 0..7.
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.
275
276 @return The new 8-bit value.
277
278 **/
279 UINT8
280 EFIAPI
281 BitFieldAndThenOr8 (
282 IN UINT8 Operand,
283 IN UINTN StartBit,
284 IN UINTN EndBit,
285 IN UINT8 AndData,
286 IN UINT8 OrData
287 )
288 {
289 ASSERT (EndBit < sizeof (Operand) * 8);
290 ASSERT (StartBit <= EndBit);
291 return BitFieldOr8 (
292 BitFieldAnd8 (Operand, StartBit, EndBit, AndData),
293 StartBit,
294 EndBit,
295 OrData
296 );
297 }
298
299 /**
300 Returns a bit field from a 16-bit value.
301
302 Returns the bitfield specified by the StartBit and the EndBit from Operand.
303
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().
308
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.
311 Range 0..15.
312 @param EndBit The ordinal of the most significant bit in the bit field.
313 Range 0..15.
314
315 @return The bit field read.
316
317 **/
318 UINT16
319 EFIAPI
320 BitFieldRead16 (
321 IN UINT16 Operand,
322 IN UINTN StartBit,
323 IN UINTN EndBit
324 )
325 {
326 ASSERT (EndBit < sizeof (Operand) * 8);
327 ASSERT (StartBit <= EndBit);
328 return (UINT16)BitFieldReadUint (Operand, StartBit, EndBit);
329 }
330
331 /**
332 Writes a bit field to a 16-bit value, and returns the result.
333
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
336 returned.
337
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().
342
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.
345 Range 0..15.
346 @param EndBit The ordinal of the most significant bit in the bit field.
347 Range 0..15.
348 @param Value New value of the bit field.
349
350 @return The new 16-bit value.
351
352 **/
353 UINT16
354 EFIAPI
355 BitFieldWrite16 (
356 IN UINT16 Operand,
357 IN UINTN StartBit,
358 IN UINTN EndBit,
359 IN UINT16 Value
360 )
361 {
362 ASSERT (EndBit < sizeof (Operand) * 8);
363 ASSERT (StartBit <= EndBit);
364 return BitFieldAndThenOr16 (Operand, StartBit, EndBit, 0, Value);
365 }
366
367 /**
368 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
369 result.
370
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.
374
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().
379
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.
382 Range 0..15.
383 @param EndBit The ordinal of the most significant bit in the bit field.
384 Range 0..15.
385 @param OrData The value to OR with the read value from the value
386
387 @return The new 16-bit value.
388
389 **/
390 UINT16
391 EFIAPI
392 BitFieldOr16 (
393 IN UINT16 Operand,
394 IN UINTN StartBit,
395 IN UINTN EndBit,
396 IN UINT16 OrData
397 )
398 {
399 ASSERT (EndBit < sizeof (Operand) * 8);
400 ASSERT (StartBit <= EndBit);
401 return (UINT16)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
402 }
403
404 /**
405 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
406 the result.
407
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.
411
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().
416
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.
419 Range 0..15.
420 @param EndBit The ordinal of the most significant bit in the bit field.
421 Range 0..15.
422 @param AndData The value to AND with the read value from the value
423
424 @return The new 16-bit value.
425
426 **/
427 UINT16
428 EFIAPI
429 BitFieldAnd16 (
430 IN UINT16 Operand,
431 IN UINTN StartBit,
432 IN UINTN EndBit,
433 IN UINT16 AndData
434 )
435 {
436 ASSERT (EndBit < sizeof (Operand) * 8);
437 ASSERT (StartBit <= EndBit);
438 return (UINT16)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
439 }
440
441 /**
442 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
443 bitwise OR, and returns the result.
444
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.
449
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().
454
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.
457 Range 0..15.
458 @param EndBit The ordinal of the most significant bit in the bit field.
459 Range 0..15.
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.
462
463 @return The new 16-bit value.
464
465 **/
466 UINT16
467 EFIAPI
468 BitFieldAndThenOr16 (
469 IN UINT16 Operand,
470 IN UINTN StartBit,
471 IN UINTN EndBit,
472 IN UINT16 AndData,
473 IN UINT16 OrData
474 )
475 {
476 ASSERT (EndBit < sizeof (Operand) * 8);
477 ASSERT (StartBit <= EndBit);
478 return BitFieldOr16 (
479 BitFieldAnd16 (Operand, StartBit, EndBit, AndData),
480 StartBit,
481 EndBit,
482 OrData
483 );
484 }
485
486 /**
487 Returns a bit field from a 32-bit value.
488
489 Returns the bitfield specified by the StartBit and the EndBit from Operand.
490
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().
495
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.
498 Range 0..31.
499 @param EndBit The ordinal of the most significant bit in the bit field.
500 Range 0..31.
501
502 @return The bit field read.
503
504 **/
505 UINT32
506 EFIAPI
507 BitFieldRead32 (
508 IN UINT32 Operand,
509 IN UINTN StartBit,
510 IN UINTN EndBit
511 )
512 {
513 ASSERT (EndBit < sizeof (Operand) * 8);
514 ASSERT (StartBit <= EndBit);
515 return (UINT32)BitFieldReadUint (Operand, StartBit, EndBit);
516 }
517
518 /**
519 Writes a bit field to a 32-bit value, and returns the result.
520
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
523 returned.
524
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().
529
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.
532 Range 0..31.
533 @param EndBit The ordinal of the most significant bit in the bit field.
534 Range 0..31.
535 @param Value New value of the bit field.
536
537 @return The new 32-bit value.
538
539 **/
540 UINT32
541 EFIAPI
542 BitFieldWrite32 (
543 IN UINT32 Operand,
544 IN UINTN StartBit,
545 IN UINTN EndBit,
546 IN UINT32 Value
547 )
548 {
549 ASSERT (EndBit < sizeof (Operand) * 8);
550 ASSERT (StartBit <= EndBit);
551 return BitFieldAndThenOr32 (Operand, StartBit, EndBit, 0, Value);
552 }
553
554 /**
555 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
556 result.
557
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.
561
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().
566
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.
569 Range 0..31.
570 @param EndBit The ordinal of the most significant bit in the bit field.
571 Range 0..31.
572 @param OrData The value to OR with the read value from the value
573
574 @return The new 32-bit value.
575
576 **/
577 UINT32
578 EFIAPI
579 BitFieldOr32 (
580 IN UINT32 Operand,
581 IN UINTN StartBit,
582 IN UINTN EndBit,
583 IN UINT32 OrData
584 )
585 {
586 ASSERT (EndBit < sizeof (Operand) * 8);
587 ASSERT (StartBit <= EndBit);
588 return (UINT32)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
589 }
590
591 /**
592 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
593 the result.
594
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.
598
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().
603
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.
606 Range 0..31.
607 @param EndBit The ordinal of the most significant bit in the bit field.
608 Range 0..31.
609 @param AndData The value to AND with the read value from the value
610
611 @return The new 32-bit value.
612
613 **/
614 UINT32
615 EFIAPI
616 BitFieldAnd32 (
617 IN UINT32 Operand,
618 IN UINTN StartBit,
619 IN UINTN EndBit,
620 IN UINT32 AndData
621 )
622 {
623 ASSERT (EndBit < sizeof (Operand) * 8);
624 ASSERT (StartBit <= EndBit);
625 return (UINT32)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
626 }
627
628 /**
629 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
630 bitwise OR, and returns the result.
631
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.
636
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().
641
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.
644 Range 0..31.
645 @param EndBit The ordinal of the most significant bit in the bit field.
646 Range 0..31.
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.
649
650 @return The new 32-bit value.
651
652 **/
653 UINT32
654 EFIAPI
655 BitFieldAndThenOr32 (
656 IN UINT32 Operand,
657 IN UINTN StartBit,
658 IN UINTN EndBit,
659 IN UINT32 AndData,
660 IN UINT32 OrData
661 )
662 {
663 ASSERT (EndBit < sizeof (Operand) * 8);
664 ASSERT (StartBit <= EndBit);
665 return BitFieldOr32 (
666 BitFieldAnd32 (Operand, StartBit, EndBit, AndData),
667 StartBit,
668 EndBit,
669 OrData
670 );
671 }
672
673 /**
674 Returns a bit field from a 64-bit value.
675
676 Returns the bitfield specified by the StartBit and the EndBit from Operand.
677
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().
682
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.
685 Range 0..63.
686 @param EndBit The ordinal of the most significant bit in the bit field.
687 Range 0..63.
688
689 @return The bit field read.
690
691 **/
692 UINT64
693 EFIAPI
694 BitFieldRead64 (
695 IN UINT64 Operand,
696 IN UINTN StartBit,
697 IN UINTN EndBit
698 )
699 {
700 ASSERT (EndBit < sizeof (Operand) * 8);
701 ASSERT (StartBit <= EndBit);
702 return RShiftU64 (Operand & ~LShiftU64 ((UINT64)-2, EndBit), StartBit);
703 }
704
705 /**
706 Writes a bit field to a 64-bit value, and returns the result.
707
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
710 returned.
711
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().
716
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.
719 Range 0..63.
720 @param EndBit The ordinal of the most significant bit in the bit field.
721 Range 0..63.
722 @param Value New value of the bit field.
723
724 @return The new 64-bit value.
725
726 **/
727 UINT64
728 EFIAPI
729 BitFieldWrite64 (
730 IN UINT64 Operand,
731 IN UINTN StartBit,
732 IN UINTN EndBit,
733 IN UINT64 Value
734 )
735 {
736 ASSERT (EndBit < sizeof (Operand) * 8);
737 ASSERT (StartBit <= EndBit);
738 return BitFieldAndThenOr64 (Operand, StartBit, EndBit, 0, Value);
739 }
740
741 /**
742 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
743 result.
744
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.
748
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().
753
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.
756 Range 0..63.
757 @param EndBit The ordinal of the most significant bit in the bit field.
758 Range 0..63.
759 @param OrData The value to OR with the read value from the value
760
761 @return The new 64-bit value.
762
763 **/
764 UINT64
765 EFIAPI
766 BitFieldOr64 (
767 IN UINT64 Operand,
768 IN UINTN StartBit,
769 IN UINTN EndBit,
770 IN UINT64 OrData
771 )
772 {
773 UINT64 Value1;
774 UINT64 Value2;
775
776 ASSERT (EndBit < sizeof (Operand) * 8);
777 ASSERT (StartBit <= EndBit);
778
779 Value1 = LShiftU64 (OrData, StartBit);
780 Value2 = LShiftU64 ((UINT64) - 2, EndBit);
781
782 return Operand | (Value1 & ~Value2);
783 }
784
785 /**
786 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
787 the result.
788
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.
792
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().
797
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.
800 Range 0..63.
801 @param EndBit The ordinal of the most significant bit in the bit field.
802 Range 0..63.
803 @param AndData The value to AND with the read value from the value
804
805 @return The new 64-bit value.
806
807 **/
808 UINT64
809 EFIAPI
810 BitFieldAnd64 (
811 IN UINT64 Operand,
812 IN UINTN StartBit,
813 IN UINTN EndBit,
814 IN UINT64 AndData
815 )
816 {
817 UINT64 Value1;
818 UINT64 Value2;
819
820 ASSERT (EndBit < sizeof (Operand) * 8);
821 ASSERT (StartBit <= EndBit);
822
823 Value1 = LShiftU64 (~AndData, StartBit);
824 Value2 = LShiftU64 ((UINT64)-2, EndBit);
825
826 return Operand & ~(Value1 & ~Value2);
827 }
828
829 /**
830 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
831 bitwise OR, and returns the result.
832
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.
837
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().
842
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.
845 Range 0..63.
846 @param EndBit The ordinal of the most significant bit in the bit field.
847 Range 0..63.
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.
850
851 @return The new 64-bit value.
852
853 **/
854 UINT64
855 EFIAPI
856 BitFieldAndThenOr64 (
857 IN UINT64 Operand,
858 IN UINTN StartBit,
859 IN UINTN EndBit,
860 IN UINT64 AndData,
861 IN UINT64 OrData
862 )
863 {
864 ASSERT (EndBit < sizeof (Operand) * 8);
865 ASSERT (StartBit <= EndBit);
866 return BitFieldOr64 (
867 BitFieldAnd64 (Operand, StartBit, EndBit, AndData),
868 StartBit,
869 EndBit,
870 OrData
871 );
872 }