]> git.proxmox.com Git - mirror_edk2.git/blob - MdePkg/Library/BaseLib/BitField.c
Removed MdePkg usage of ModuleName: in file headers
[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 **/
14
15 //
16 // Include common header file for this module.
17 //
18
19
20 #include "BaseLibInternals.h"
21
22 /**
23 Worker function that returns a bit field from Operand
24
25 Returns the bitfield specified by the StartBit and the EndBit from Operand.
26
27 @param Operand Operand on which to perform the bitfield operation.
28 @param StartBit The ordinal of the least significant bit in the bit field.
29 @param EndBit The ordinal of the most significant bit in the bit field.
30
31 @return The bit field read.
32
33 **/
34 unsigned int
35 BitFieldReadUint (
36 IN unsigned int Operand,
37 IN UINTN StartBit,
38 IN UINTN EndBit
39 )
40 {
41 //
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.
44 //
45 return (Operand & ~((unsigned int)-2 << EndBit)) >> StartBit;
46 }
47
48 /**
49 Worker function that reads a bit field from Operand, performs a bitwise OR,
50 and returns the result.
51
52 Performs a bitwise OR between the bit field specified by StartBit and EndBit
53 in Operand and the value specified by AndData. All other bits in Operand are
54 preserved. The new value is returned.
55
56 @param Operand Operand on which to perform the bitfield operation.
57 @param StartBit The ordinal of the least significant bit in the bit field.
58 @param EndBit The ordinal of the most significant bit in the bit field.
59 @param OrData The value to OR with the read value from the value
60
61 @return The new value.
62
63 **/
64 unsigned int
65 BitFieldOrUint (
66 IN unsigned int Operand,
67 IN UINTN StartBit,
68 IN UINTN EndBit,
69 IN unsigned int OrData
70 )
71 {
72 //
73 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
74 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
75 //
76 return Operand | ((OrData << StartBit) & ~((unsigned int) -2 << EndBit));
77 }
78
79 /**
80 Worker function that reads a bit field from Operand, performs a bitwise AND,
81 and returns the result.
82
83 Performs a bitwise AND between the bit field specified by StartBit and EndBit
84 in Operand and the value specified by AndData. All other bits in Operand are
85 preserved. The new value is returned.
86
87 @param Operand Operand on which to perform the bitfield operation.
88 @param StartBit The ordinal of the least significant bit in the bit field.
89 @param EndBit The ordinal of the most significant bit in the bit field.
90 @param AndData The value to And with the read value from the value
91
92 @return The new value.
93
94 **/
95 unsigned int
96 BitFieldAndUint (
97 IN unsigned int Operand,
98 IN UINTN StartBit,
99 IN UINTN EndBit,
100 IN unsigned int AndData
101 )
102 {
103 //
104 // ~((unsigned int)-2 << EndBit) is a mask in which bit[0] thru bit[EndBit]
105 // are 1's while bit[EndBit + 1] thru the most significant bit are 0's.
106 //
107 return Operand & ~((~AndData << StartBit) & ~((unsigned int) -2 << EndBit));
108 }
109
110 /**
111 Returns a bit field from an 8-bit value.
112
113 Returns the bitfield specified by the StartBit and the EndBit from Operand.
114
115 If 8-bit operations are not supported, then ASSERT().
116 If StartBit is greater than 7, then ASSERT().
117 If EndBit is greater than 7, then ASSERT().
118 If EndBit is less than StartBit, then ASSERT().
119
120 @param Operand Operand on which to perform the bitfield operation.
121 @param StartBit The ordinal of the least significant bit in the bit field.
122 Range 0..7.
123 @param EndBit The ordinal of the most significant bit in the bit field.
124 Range 0..7.
125
126 @return The bit field read.
127
128 **/
129 UINT8
130 EFIAPI
131 BitFieldRead8 (
132 IN UINT8 Operand,
133 IN UINTN StartBit,
134 IN UINTN EndBit
135 )
136 {
137 ASSERT (EndBit < sizeof (Operand) * 8);
138 ASSERT (StartBit <= EndBit);
139 return (UINT8)BitFieldReadUint (Operand, StartBit, EndBit);
140 }
141
142 /**
143 Writes a bit field to an 8-bit value, and returns the result.
144
145 Writes Value to the bit field specified by the StartBit and the EndBit in
146 Operand. All other bits in Operand are preserved. The new 8-bit value is
147 returned.
148
149 If 8-bit operations are not supported, then ASSERT().
150 If StartBit is greater than 7, then ASSERT().
151 If EndBit is greater than 7, then ASSERT().
152 If EndBit is less than StartBit, then ASSERT().
153
154 @param Operand Operand on which to perform the bitfield operation.
155 @param StartBit The ordinal of the least significant bit in the bit field.
156 Range 0..7.
157 @param EndBit The ordinal of the most significant bit in the bit field.
158 Range 0..7.
159 @param Value New value of the bit field.
160
161 @return The new 8-bit value.
162
163 **/
164 UINT8
165 EFIAPI
166 BitFieldWrite8 (
167 IN UINT8 Operand,
168 IN UINTN StartBit,
169 IN UINTN EndBit,
170 IN UINT8 Value
171 )
172 {
173 ASSERT (EndBit < sizeof (Operand) * 8);
174 ASSERT (StartBit <= EndBit);
175 return BitFieldAndThenOr8 (Operand, StartBit, EndBit, 0, Value);
176 }
177
178 /**
179 Reads a bit field from an 8-bit value, performs a bitwise OR, and returns the
180 result.
181
182 Performs a bitwise inclusive OR between the bit field specified by StartBit
183 and EndBit in Operand and the value specified by OrData. All other bits in
184 Operand are preserved. The new 8-bit value is returned.
185
186 If 8-bit operations are not supported, then ASSERT().
187 If StartBit is greater than 7, then ASSERT().
188 If EndBit is greater than 7, then ASSERT().
189 If EndBit is less than StartBit, then ASSERT().
190
191 @param Operand Operand on which to perform the bitfield operation.
192 @param StartBit The ordinal of the least significant bit in the bit field.
193 Range 0..7.
194 @param EndBit The ordinal of the most significant bit in the bit field.
195 Range 0..7.
196 @param OrData The value to OR with the read value from the value
197
198 @return The new 8-bit value.
199
200 **/
201 UINT8
202 EFIAPI
203 BitFieldOr8 (
204 IN UINT8 Operand,
205 IN UINTN StartBit,
206 IN UINTN EndBit,
207 IN UINT8 OrData
208 )
209 {
210 ASSERT (EndBit < sizeof (Operand) * 8);
211 ASSERT (StartBit <= EndBit);
212 return (UINT8)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
213 }
214
215 /**
216 Reads a bit field from an 8-bit value, performs a bitwise AND, and returns
217 the result.
218
219 Performs a bitwise AND between the bit field specified by StartBit and EndBit
220 in Operand and the value specified by AndData. All other bits in Operand are
221 preserved. The new 8-bit value is returned.
222
223 If 8-bit operations are not supported, then ASSERT().
224 If StartBit is greater than 7, then ASSERT().
225 If EndBit is greater than 7, then ASSERT().
226 If EndBit is less than StartBit, then ASSERT().
227
228 @param Operand Operand on which to perform the bitfield operation.
229 @param StartBit The ordinal of the least significant bit in the bit field.
230 Range 0..7.
231 @param EndBit The ordinal of the most significant bit in the bit field.
232 Range 0..7.
233 @param AndData The value to AND with the read value from the value.
234
235 @return The new 8-bit value.
236
237 **/
238 UINT8
239 EFIAPI
240 BitFieldAnd8 (
241 IN UINT8 Operand,
242 IN UINTN StartBit,
243 IN UINTN EndBit,
244 IN UINT8 AndData
245 )
246 {
247 ASSERT (EndBit < sizeof (Operand) * 8);
248 ASSERT (StartBit <= EndBit);
249 return (UINT8)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
250 }
251
252 /**
253 Reads a bit field from an 8-bit value, performs a bitwise AND followed by a
254 bitwise OR, and returns the result.
255
256 Performs a bitwise AND between the bit field specified by StartBit and EndBit
257 in Operand and the value specified by AndData, followed by a bitwise
258 inclusive OR with value specified by OrData. All other bits in Operand are
259 preserved. The new 8-bit value is returned.
260
261 If 8-bit operations are not supported, then ASSERT().
262 If StartBit is greater than 7, then ASSERT().
263 If EndBit is greater than 7, then ASSERT().
264 If EndBit is less than StartBit, then ASSERT().
265
266 @param Operand Operand on which to perform the bitfield operation.
267 @param StartBit The ordinal of the least significant bit in the bit field.
268 Range 0..7.
269 @param EndBit The ordinal of the most significant bit in the bit field.
270 Range 0..7.
271 @param AndData The value to AND with the read value from the value.
272 @param OrData The value to OR with the result of the AND operation.
273
274 @return The new 8-bit value.
275
276 **/
277 UINT8
278 EFIAPI
279 BitFieldAndThenOr8 (
280 IN UINT8 Operand,
281 IN UINTN StartBit,
282 IN UINTN EndBit,
283 IN UINT8 AndData,
284 IN UINT8 OrData
285 )
286 {
287 ASSERT (EndBit < sizeof (Operand) * 8);
288 ASSERT (StartBit <= EndBit);
289 return BitFieldOr8 (
290 BitFieldAnd8 (Operand, StartBit, EndBit, AndData),
291 StartBit,
292 EndBit,
293 OrData
294 );
295 }
296
297 /**
298 Returns a bit field from a 16-bit value.
299
300 Returns the bitfield specified by the StartBit and the EndBit from Operand.
301
302 If 16-bit operations are not supported, then ASSERT().
303 If StartBit is greater than 15, then ASSERT().
304 If EndBit is greater than 15, then ASSERT().
305 If EndBit is less than StartBit, then ASSERT().
306
307 @param Operand Operand on which to perform the bitfield operation.
308 @param StartBit The ordinal of the least significant bit in the bit field.
309 Range 0..15.
310 @param EndBit The ordinal of the most significant bit in the bit field.
311 Range 0..15.
312
313 @return The bit field read.
314
315 **/
316 UINT16
317 EFIAPI
318 BitFieldRead16 (
319 IN UINT16 Operand,
320 IN UINTN StartBit,
321 IN UINTN EndBit
322 )
323 {
324 ASSERT (EndBit < sizeof (Operand) * 8);
325 ASSERT (StartBit <= EndBit);
326 return (UINT16)BitFieldReadUint (Operand, StartBit, EndBit);
327 }
328
329 /**
330 Writes a bit field to a 16-bit value, and returns the result.
331
332 Writes Value to the bit field specified by the StartBit and the EndBit in
333 Operand. All other bits in Operand are preserved. The new 16-bit value is
334 returned.
335
336 If 16-bit operations are not supported, then ASSERT().
337 If StartBit is greater than 15, then ASSERT().
338 If EndBit is greater than 15, then ASSERT().
339 If EndBit is less than StartBit, then ASSERT().
340
341 @param Operand Operand on which to perform the bitfield operation.
342 @param StartBit The ordinal of the least significant bit in the bit field.
343 Range 0..15.
344 @param EndBit The ordinal of the most significant bit in the bit field.
345 Range 0..15.
346 @param Value New value of the bit field.
347
348 @return The new 16-bit value.
349
350 **/
351 UINT16
352 EFIAPI
353 BitFieldWrite16 (
354 IN UINT16 Operand,
355 IN UINTN StartBit,
356 IN UINTN EndBit,
357 IN UINT16 Value
358 )
359 {
360 ASSERT (EndBit < sizeof (Operand) * 8);
361 ASSERT (StartBit <= EndBit);
362 return BitFieldAndThenOr16 (Operand, StartBit, EndBit, 0, Value);
363 }
364
365 /**
366 Reads a bit field from a 16-bit value, performs a bitwise OR, and returns the
367 result.
368
369 Performs a bitwise inclusive OR between the bit field specified by StartBit
370 and EndBit in Operand and the value specified by OrData. All other bits in
371 Operand are preserved. The new 16-bit value is returned.
372
373 If 16-bit operations are not supported, then ASSERT().
374 If StartBit is greater than 15, then ASSERT().
375 If EndBit is greater than 15, then ASSERT().
376 If EndBit is less than StartBit, then ASSERT().
377
378 @param Operand Operand on which to perform the bitfield operation.
379 @param StartBit The ordinal of the least significant bit in the bit field.
380 Range 0..15.
381 @param EndBit The ordinal of the most significant bit in the bit field.
382 Range 0..15.
383 @param OrData The value to OR with the read value from the value
384
385 @return The new 16-bit value.
386
387 **/
388 UINT16
389 EFIAPI
390 BitFieldOr16 (
391 IN UINT16 Operand,
392 IN UINTN StartBit,
393 IN UINTN EndBit,
394 IN UINT16 OrData
395 )
396 {
397 ASSERT (EndBit < sizeof (Operand) * 8);
398 ASSERT (StartBit <= EndBit);
399 return (UINT16)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
400 }
401
402 /**
403 Reads a bit field from a 16-bit value, performs a bitwise AND, and returns
404 the result.
405
406 Performs a bitwise AND between the bit field specified by StartBit and EndBit
407 in Operand and the value specified by AndData. All other bits in Operand are
408 preserved. The new 16-bit value is returned.
409
410 If 16-bit operations are not supported, then ASSERT().
411 If StartBit is greater than 15, then ASSERT().
412 If EndBit is greater than 15, then ASSERT().
413 If EndBit is less than StartBit, then ASSERT().
414
415 @param Operand Operand on which to perform the bitfield operation.
416 @param StartBit The ordinal of the least significant bit in the bit field.
417 Range 0..15.
418 @param EndBit The ordinal of the most significant bit in the bit field.
419 Range 0..15.
420 @param AndData The value to AND with the read value from the value
421
422 @return The new 16-bit value.
423
424 **/
425 UINT16
426 EFIAPI
427 BitFieldAnd16 (
428 IN UINT16 Operand,
429 IN UINTN StartBit,
430 IN UINTN EndBit,
431 IN UINT16 AndData
432 )
433 {
434 ASSERT (EndBit < sizeof (Operand) * 8);
435 ASSERT (StartBit <= EndBit);
436 return (UINT16)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
437 }
438
439 /**
440 Reads a bit field from a 16-bit value, performs a bitwise AND followed by a
441 bitwise OR, and returns the result.
442
443 Performs a bitwise AND between the bit field specified by StartBit and EndBit
444 in Operand and the value specified by AndData, followed by a bitwise
445 inclusive OR with value specified by OrData. All other bits in Operand are
446 preserved. The new 16-bit value is returned.
447
448 If 16-bit operations are not supported, then ASSERT().
449 If StartBit is greater than 15, then ASSERT().
450 If EndBit is greater than 15, then ASSERT().
451 If EndBit is less than StartBit, then ASSERT().
452
453 @param Operand Operand on which to perform the bitfield operation.
454 @param StartBit The ordinal of the least significant bit in the bit field.
455 Range 0..15.
456 @param EndBit The ordinal of the most significant bit in the bit field.
457 Range 0..15.
458 @param AndData The value to AND with the read value from the value.
459 @param OrData The value to OR with the result of the AND operation.
460
461 @return The new 16-bit value.
462
463 **/
464 UINT16
465 EFIAPI
466 BitFieldAndThenOr16 (
467 IN UINT16 Operand,
468 IN UINTN StartBit,
469 IN UINTN EndBit,
470 IN UINT16 AndData,
471 IN UINT16 OrData
472 )
473 {
474 ASSERT (EndBit < sizeof (Operand) * 8);
475 ASSERT (StartBit <= EndBit);
476 return BitFieldOr16 (
477 BitFieldAnd16 (Operand, StartBit, EndBit, AndData),
478 StartBit,
479 EndBit,
480 OrData
481 );
482 }
483
484 /**
485 Returns a bit field from a 32-bit value.
486
487 Returns the bitfield specified by the StartBit and the EndBit from Operand.
488
489 If 32-bit operations are not supported, then ASSERT().
490 If StartBit is greater than 31, then ASSERT().
491 If EndBit is greater than 31, then ASSERT().
492 If EndBit is less than StartBit, then ASSERT().
493
494 @param Operand Operand on which to perform the bitfield operation.
495 @param StartBit The ordinal of the least significant bit in the bit field.
496 Range 0..31.
497 @param EndBit The ordinal of the most significant bit in the bit field.
498 Range 0..31.
499
500 @return The bit field read.
501
502 **/
503 UINT32
504 EFIAPI
505 BitFieldRead32 (
506 IN UINT32 Operand,
507 IN UINTN StartBit,
508 IN UINTN EndBit
509 )
510 {
511 ASSERT (EndBit < sizeof (Operand) * 8);
512 ASSERT (StartBit <= EndBit);
513 return (UINT32)BitFieldReadUint (Operand, StartBit, EndBit);
514 }
515
516 /**
517 Writes a bit field to a 32-bit value, and returns the result.
518
519 Writes Value to the bit field specified by the StartBit and the EndBit in
520 Operand. All other bits in Operand are preserved. The new 32-bit value is
521 returned.
522
523 If 32-bit operations are not supported, then ASSERT().
524 If StartBit is greater than 31, then ASSERT().
525 If EndBit is greater than 31, then ASSERT().
526 If EndBit is less than StartBit, then ASSERT().
527
528 @param Operand Operand on which to perform the bitfield operation.
529 @param StartBit The ordinal of the least significant bit in the bit field.
530 Range 0..31.
531 @param EndBit The ordinal of the most significant bit in the bit field.
532 Range 0..31.
533 @param Value New value of the bit field.
534
535 @return The new 32-bit value.
536
537 **/
538 UINT32
539 EFIAPI
540 BitFieldWrite32 (
541 IN UINT32 Operand,
542 IN UINTN StartBit,
543 IN UINTN EndBit,
544 IN UINT32 Value
545 )
546 {
547 ASSERT (EndBit < sizeof (Operand) * 8);
548 ASSERT (StartBit <= EndBit);
549 return BitFieldAndThenOr32 (Operand, StartBit, EndBit, 0, Value);
550 }
551
552 /**
553 Reads a bit field from a 32-bit value, performs a bitwise OR, and returns the
554 result.
555
556 Performs a bitwise inclusive OR between the bit field specified by StartBit
557 and EndBit in Operand and the value specified by OrData. All other bits in
558 Operand are preserved. The new 32-bit value is returned.
559
560 If 32-bit operations are not supported, then ASSERT().
561 If StartBit is greater than 31, then ASSERT().
562 If EndBit is greater than 31, then ASSERT().
563 If EndBit is less than StartBit, then ASSERT().
564
565 @param Operand Operand on which to perform the bitfield operation.
566 @param StartBit The ordinal of the least significant bit in the bit field.
567 Range 0..31.
568 @param EndBit The ordinal of the most significant bit in the bit field.
569 Range 0..31.
570 @param OrData The value to OR with the read value from the value
571
572 @return The new 32-bit value.
573
574 **/
575 UINT32
576 EFIAPI
577 BitFieldOr32 (
578 IN UINT32 Operand,
579 IN UINTN StartBit,
580 IN UINTN EndBit,
581 IN UINT32 OrData
582 )
583 {
584 ASSERT (EndBit < sizeof (Operand) * 8);
585 ASSERT (StartBit <= EndBit);
586 return (UINT32)BitFieldOrUint (Operand, StartBit, EndBit, OrData);
587 }
588
589 /**
590 Reads a bit field from a 32-bit value, performs a bitwise AND, and returns
591 the result.
592
593 Performs a bitwise AND between the bit field specified by StartBit and EndBit
594 in Operand and the value specified by AndData. All other bits in Operand are
595 preserved. The new 32-bit value is returned.
596
597 If 32-bit operations are not supported, then ASSERT().
598 If StartBit is greater than 31, then ASSERT().
599 If EndBit is greater than 31, then ASSERT().
600 If EndBit is less than StartBit, then ASSERT().
601
602 @param Operand Operand on which to perform the bitfield operation.
603 @param StartBit The ordinal of the least significant bit in the bit field.
604 Range 0..31.
605 @param EndBit The ordinal of the most significant bit in the bit field.
606 Range 0..31.
607 @param AndData The value to AND with the read value from the value
608
609 @return The new 32-bit value.
610
611 **/
612 UINT32
613 EFIAPI
614 BitFieldAnd32 (
615 IN UINT32 Operand,
616 IN UINTN StartBit,
617 IN UINTN EndBit,
618 IN UINT32 AndData
619 )
620 {
621 ASSERT (EndBit < sizeof (Operand) * 8);
622 ASSERT (StartBit <= EndBit);
623 return (UINT32)BitFieldAndUint (Operand, StartBit, EndBit, AndData);
624 }
625
626 /**
627 Reads a bit field from a 32-bit value, performs a bitwise AND followed by a
628 bitwise OR, and returns the result.
629
630 Performs a bitwise AND between the bit field specified by StartBit and EndBit
631 in Operand and the value specified by AndData, followed by a bitwise
632 inclusive OR with value specified by OrData. All other bits in Operand are
633 preserved. The new 32-bit value is returned.
634
635 If 32-bit operations are not supported, then ASSERT().
636 If StartBit is greater than 31, then ASSERT().
637 If EndBit is greater than 31, then ASSERT().
638 If EndBit is less than StartBit, then ASSERT().
639
640 @param Operand Operand on which to perform the bitfield operation.
641 @param StartBit The ordinal of the least significant bit in the bit field.
642 Range 0..31.
643 @param EndBit The ordinal of the most significant bit in the bit field.
644 Range 0..31.
645 @param AndData The value to AND with the read value from the value.
646 @param OrData The value to OR with the result of the AND operation.
647
648 @return The new 32-bit value.
649
650 **/
651 UINT32
652 EFIAPI
653 BitFieldAndThenOr32 (
654 IN UINT32 Operand,
655 IN UINTN StartBit,
656 IN UINTN EndBit,
657 IN UINT32 AndData,
658 IN UINT32 OrData
659 )
660 {
661 ASSERT (EndBit < sizeof (Operand) * 8);
662 ASSERT (StartBit <= EndBit);
663 return BitFieldOr32 (
664 BitFieldAnd32 (Operand, StartBit, EndBit, AndData),
665 StartBit,
666 EndBit,
667 OrData
668 );
669 }
670
671 /**
672 Returns a bit field from a 64-bit value.
673
674 Returns the bitfield specified by the StartBit and the EndBit from Operand.
675
676 If 64-bit operations are not supported, then ASSERT().
677 If StartBit is greater than 63, then ASSERT().
678 If EndBit is greater than 63, then ASSERT().
679 If EndBit is less than StartBit, then ASSERT().
680
681 @param Operand Operand on which to perform the bitfield operation.
682 @param StartBit The ordinal of the least significant bit in the bit field.
683 Range 0..63.
684 @param EndBit The ordinal of the most significant bit in the bit field.
685 Range 0..63.
686
687 @return The bit field read.
688
689 **/
690 UINT64
691 EFIAPI
692 BitFieldRead64 (
693 IN UINT64 Operand,
694 IN UINTN StartBit,
695 IN UINTN EndBit
696 )
697 {
698 ASSERT (EndBit < sizeof (Operand) * 8);
699 ASSERT (StartBit <= EndBit);
700 return RShiftU64 (Operand & ~LShiftU64 ((UINT64)-2, EndBit), StartBit);
701 }
702
703 /**
704 Writes a bit field to a 64-bit value, and returns the result.
705
706 Writes Value to the bit field specified by the StartBit and the EndBit in
707 Operand. All other bits in Operand are preserved. The new 64-bit value is
708 returned.
709
710 If 64-bit operations are not supported, then ASSERT().
711 If StartBit is greater than 63, then ASSERT().
712 If EndBit is greater than 63, then ASSERT().
713 If EndBit is less than StartBit, then ASSERT().
714
715 @param Operand Operand on which to perform the bitfield operation.
716 @param StartBit The ordinal of the least significant bit in the bit field.
717 Range 0..63.
718 @param EndBit The ordinal of the most significant bit in the bit field.
719 Range 0..63.
720 @param Value New value of the bit field.
721
722 @return The new 64-bit value.
723
724 **/
725 UINT64
726 EFIAPI
727 BitFieldWrite64 (
728 IN UINT64 Operand,
729 IN UINTN StartBit,
730 IN UINTN EndBit,
731 IN UINT64 Value
732 )
733 {
734 ASSERT (EndBit < sizeof (Operand) * 8);
735 ASSERT (StartBit <= EndBit);
736 return BitFieldAndThenOr64 (Operand, StartBit, EndBit, 0, Value);
737 }
738
739 /**
740 Reads a bit field from a 64-bit value, performs a bitwise OR, and returns the
741 result.
742
743 Performs a bitwise inclusive OR between the bit field specified by StartBit
744 and EndBit in Operand and the value specified by OrData. All other bits in
745 Operand are preserved. The new 64-bit value is returned.
746
747 If 64-bit operations are not supported, then ASSERT().
748 If StartBit is greater than 63, then ASSERT().
749 If EndBit is greater than 63, then ASSERT().
750 If EndBit is less than StartBit, then ASSERT().
751
752 @param Operand Operand on which to perform the bitfield operation.
753 @param StartBit The ordinal of the least significant bit in the bit field.
754 Range 0..63.
755 @param EndBit The ordinal of the most significant bit in the bit field.
756 Range 0..63.
757 @param OrData The value to OR with the read value from the value
758
759 @return The new 64-bit value.
760
761 **/
762 UINT64
763 EFIAPI
764 BitFieldOr64 (
765 IN UINT64 Operand,
766 IN UINTN StartBit,
767 IN UINTN EndBit,
768 IN UINT64 OrData
769 )
770 {
771 UINT64 Value1;
772 UINT64 Value2;
773
774 ASSERT (EndBit < sizeof (Operand) * 8);
775 ASSERT (StartBit <= EndBit);
776
777 Value1 = LShiftU64 (OrData, StartBit);
778 Value2 = LShiftU64 ((UINT64) - 2, EndBit);
779
780 return Operand | (Value1 & ~Value2);
781 }
782
783 /**
784 Reads a bit field from a 64-bit value, performs a bitwise AND, and returns
785 the result.
786
787 Performs a bitwise AND between the bit field specified by StartBit and EndBit
788 in Operand and the value specified by AndData. All other bits in Operand are
789 preserved. The new 64-bit value is returned.
790
791 If 64-bit operations are not supported, then ASSERT().
792 If StartBit is greater than 63, then ASSERT().
793 If EndBit is greater than 63, then ASSERT().
794 If EndBit is less than StartBit, then ASSERT().
795
796 @param Operand Operand on which to perform the bitfield operation.
797 @param StartBit The ordinal of the least significant bit in the bit field.
798 Range 0..63.
799 @param EndBit The ordinal of the most significant bit in the bit field.
800 Range 0..63.
801 @param AndData The value to AND with the read value from the value
802
803 @return The new 64-bit value.
804
805 **/
806 UINT64
807 EFIAPI
808 BitFieldAnd64 (
809 IN UINT64 Operand,
810 IN UINTN StartBit,
811 IN UINTN EndBit,
812 IN UINT64 AndData
813 )
814 {
815 UINT64 Value1;
816 UINT64 Value2;
817
818 ASSERT (EndBit < sizeof (Operand) * 8);
819 ASSERT (StartBit <= EndBit);
820
821 Value1 = LShiftU64 (~AndData, StartBit);
822 Value2 = LShiftU64 ((UINT64)-2, EndBit);
823
824 return Operand & ~(Value1 & ~Value2);
825 }
826
827 /**
828 Reads a bit field from a 64-bit value, performs a bitwise AND followed by a
829 bitwise OR, and returns the result.
830
831 Performs a bitwise AND between the bit field specified by StartBit and EndBit
832 in Operand and the value specified by AndData, followed by a bitwise
833 inclusive OR with value specified by OrData. All other bits in Operand are
834 preserved. The new 64-bit value is returned.
835
836 If 64-bit operations are not supported, then ASSERT().
837 If StartBit is greater than 63, then ASSERT().
838 If EndBit is greater than 63, then ASSERT().
839 If EndBit is less than StartBit, then ASSERT().
840
841 @param Operand Operand on which to perform the bitfield operation.
842 @param StartBit The ordinal of the least significant bit in the bit field.
843 Range 0..63.
844 @param EndBit The ordinal of the most significant bit in the bit field.
845 Range 0..63.
846 @param AndData The value to AND with the read value from the value.
847 @param OrData The value to OR with the result of the AND operation.
848
849 @return The new 64-bit value.
850
851 **/
852 UINT64
853 EFIAPI
854 BitFieldAndThenOr64 (
855 IN UINT64 Operand,
856 IN UINTN StartBit,
857 IN UINTN EndBit,
858 IN UINT64 AndData,
859 IN UINT64 OrData
860 )
861 {
862 ASSERT (EndBit < sizeof (Operand) * 8);
863 ASSERT (StartBit <= EndBit);
864 return BitFieldOr64 (
865 BitFieldAnd64 (Operand, StartBit, EndBit, AndData),
866 StartBit,
867 EndBit,
868 OrData
869 );
870 }