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