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