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