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