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