]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/BaseLibInternals.h
Optimized HighBitSetXX() functions
[mirror_edk2.git] / MdePkg / Library / BaseLib / BaseLibInternals.h
CommitLineData
878ddf1f 1/** @file\r
2 Declaration of internal functions in 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: BaseLibInternals.h\r
14\r
15**/\r
16\r
17#ifndef __BASE_LIB_INTERNALS__\r
18#define __BASE_LIB_INTERNALS__\r
19\r
20//\r
21// Math functions\r
22//\r
23\r
1ea5ca46 24/**\r
bda7fbcc 25 Shifts a 64-bit integer left between 0 and 63 bits. The low bits\r
1ea5ca46 26 are filled with zeros. The shifted value is returned.\r
27\r
28 This function shifts the 64-bit value Operand to the left by Count bits. The\r
29 low Count bits are set to zero. The shifted value is returned.\r
30\r
31 @param Operand The 64-bit operand to shift left.\r
32 @param Count The number of bits to shift left.\r
33\r
34 @return Operand << Count\r
35\r
36**/\r
878ddf1f 37UINT64\r
38EFIAPI\r
39InternalMathLShiftU64 (\r
40 IN UINT64 Operand,\r
41 IN UINTN Count\r
42 );\r
43\r
1ea5ca46 44/**\r
bda7fbcc 45 Shifts a 64-bit integer right between 0 and 63 bits. This high bits\r
1ea5ca46 46 are filled with zeros. The shifted value is returned.\r
47\r
48 This function shifts the 64-bit value Operand to the right by Count bits. The\r
49 high Count bits are set to zero. The shifted value is returned.\r
50\r
51 @param Operand The 64-bit operand to shift right.\r
52 @param Count The number of bits to shift right.\r
53\r
54 @return Operand >> Count\r
55\r
56**/\r
878ddf1f 57UINT64\r
58EFIAPI\r
59InternalMathRShiftU64 (\r
60 IN UINT64 Operand,\r
61 IN UINTN Count\r
62 );\r
63\r
1ea5ca46 64/**\r
bda7fbcc 65 Shifts a 64-bit integer right between 0 and 63 bits. The high bits\r
1ea5ca46 66 are filled with original integer's bit 63. The shifted value is returned.\r
67\r
68 This function shifts the 64-bit value Operand to the right by Count bits. The\r
69 high Count bits are set to bit 63 of Operand. The shifted value is returned.\r
70\r
71 @param Operand The 64-bit operand to shift right.\r
72 @param Count The number of bits to shift right.\r
73\r
74 @return Operand arithmetically shifted right by Count\r
75\r
76**/\r
878ddf1f 77UINT64\r
78EFIAPI\r
79InternalMathARShiftU64 (\r
80 IN UINT64 Operand,\r
81 IN UINTN Count\r
82 );\r
83\r
1ea5ca46 84/**\r
bda7fbcc 85 Rotates a 64-bit integer left between 0 and 63 bits, filling\r
1ea5ca46 86 the low bits with the high bits that were rotated.\r
87\r
88 This function rotates the 64-bit value Operand to the left by Count bits. The\r
89 low Count bits are fill with the high Count bits of Operand. The rotated\r
90 value is returned.\r
91\r
92 @param Operand The 64-bit operand to rotate left.\r
93 @param Count The number of bits to rotate left.\r
94\r
95 @return Operand <<< Count\r
96\r
97**/\r
878ddf1f 98UINT64\r
99EFIAPI\r
100InternalMathLRotU64 (\r
101 IN UINT64 Operand,\r
102 IN UINTN Count\r
103 );\r
104\r
1ea5ca46 105/**\r
bda7fbcc 106 Rotates a 64-bit integer right between 0 and 63 bits, filling\r
1ea5ca46 107 the high bits with the high low bits that were rotated.\r
108\r
109 This function rotates the 64-bit value Operand to the right by Count bits.\r
110 The high Count bits are fill with the low Count bits of Operand. The rotated\r
111 value is returned.\r
112\r
113 @param Operand The 64-bit operand to rotate right.\r
114 @param Count The number of bits to rotate right.\r
115\r
116 @return Operand >>> Count\r
117\r
118**/\r
878ddf1f 119UINT64\r
120EFIAPI\r
121InternalMathRRotU64 (\r
122 IN UINT64 Operand,\r
123 IN UINTN Count\r
124 );\r
125\r
1ea5ca46 126/**\r
bda7fbcc 127 Switches the endianess of a 64-bit integer.\r
1ea5ca46 128\r
129 This function swaps the bytes in a 64-bit unsigned value to switch the value\r
130 from little endian to big endian or vice versa. The byte swapped value is\r
131 returned.\r
132\r
133 @param Operand A 64-bit unsigned value.\r
134\r
135 @return The byte swaped Operand.\r
136\r
137**/\r
878ddf1f 138UINT64\r
139EFIAPI\r
140InternalMathSwapBytes64 (\r
141 IN UINT64 Operand\r
142 );\r
143\r
1ea5ca46 144/**\r
bda7fbcc 145 Multiples a 64-bit unsigned integer by a 32-bit unsigned integer\r
1ea5ca46 146 and generates a 64-bit unsigned result.\r
147\r
148 This function multiples the 64-bit unsigned value Multiplicand by the 32-bit\r
149 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
150 bit unsigned result is returned.\r
151\r
152 @param Multiplicand A 64-bit unsigned value.\r
153 @param Multiplier A 32-bit unsigned value.\r
154\r
155 @return Multiplicand * Multiplier\r
156\r
157**/\r
878ddf1f 158UINT64\r
159EFIAPI\r
160InternalMathMultU64x32 (\r
161 IN UINT64 Multiplicand,\r
162 IN UINT32 Multiplier\r
163 );\r
164\r
1ea5ca46 165/**\r
bda7fbcc 166 Multiples a 64-bit unsigned integer by a 64-bit unsigned integer\r
1ea5ca46 167 and generates a 64-bit unsigned result.\r
168\r
169 This function multiples the 64-bit unsigned value Multiplicand by the 64-bit\r
170 unsigned value Multiplier and generates a 64-bit unsigned result. This 64-\r
171 bit unsigned result is returned.\r
172\r
173 @param Multiplicand A 64-bit unsigned value.\r
174 @param Multiplier A 64-bit unsigned value.\r
175\r
176 @return Multiplicand * Multiplier\r
177\r
178**/\r
878ddf1f 179UINT64\r
180EFIAPI\r
181InternalMathMultU64x64 (\r
182 IN UINT64 Multiplicand,\r
183 IN UINT64 Multiplier\r
184 );\r
185\r
1ea5ca46 186/**\r
bda7fbcc 187 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
1ea5ca46 188 generates a 64-bit unsigned result.\r
bda7fbcc 189\r
1ea5ca46 190 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
191 unsigned value Divisor and generates a 64-bit unsigned quotient. This\r
192 function returns the 64-bit unsigned quotient.\r
193\r
194 @param Dividend A 64-bit unsigned value.\r
195 @param Divisor A 32-bit unsigned value.\r
196\r
197 @return Dividend / Divisor\r
198\r
199**/\r
878ddf1f 200UINT64\r
201EFIAPI\r
202InternalMathDivU64x32 (\r
203 IN UINT64 Dividend,\r
204 IN UINT32 Divisor\r
205 );\r
206\r
1ea5ca46 207/**\r
bda7fbcc 208 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
1ea5ca46 209 generates a 32-bit unsigned remainder.\r
210\r
211 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
212 unsigned value Divisor and generates a 32-bit remainder. This function\r
213 returns the 32-bit unsigned remainder.\r
214\r
215 @param Dividend A 64-bit unsigned value.\r
216 @param Divisor A 32-bit unsigned value.\r
217\r
218 @return Dividend % Divisor\r
219\r
220**/\r
878ddf1f 221UINT32\r
222EFIAPI\r
223InternalMathModU64x32 (\r
224 IN UINT64 Dividend,\r
225 IN UINT32 Divisor\r
226 );\r
227\r
1ea5ca46 228/**\r
bda7fbcc 229 Divides a 64-bit unsigned integer by a 32-bit unsigned integer and\r
1ea5ca46 230 generates a 64-bit unsigned result and an optional 32-bit unsigned remainder.\r
231\r
232 This function divides the 64-bit unsigned value Dividend by the 32-bit\r
233 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
234 is not NULL, then the 32-bit unsigned remainder is returned in Remainder.\r
235 This function returns the 64-bit unsigned quotient.\r
236\r
237 @param Dividend A 64-bit unsigned value.\r
238 @param Divisor A 32-bit unsigned value.\r
239 @param Remainder A pointer to a 32-bit unsigned value. This parameter is\r
240 optional and may be NULL.\r
241\r
242 @return Dividend / Divisor\r
243\r
244**/\r
878ddf1f 245UINT64\r
246EFIAPI\r
247InternalMathDivRemU64x32 (\r
248 IN UINT64 Dividend,\r
249 IN UINT32 Divisor,\r
250 OUT UINT32 *Remainder\r
251 );\r
252\r
1ea5ca46 253/**\r
bda7fbcc 254 Divides a 64-bit unsigned integer by a 64-bit unsigned integer and\r
1ea5ca46 255 generates a 64-bit unsigned result and an optional 64-bit unsigned remainder.\r
256\r
257 This function divides the 64-bit unsigned value Dividend by the 64-bit\r
258 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
259 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.\r
260 This function returns the 64-bit unsigned quotient.\r
261\r
262 @param Dividend A 64-bit unsigned value.\r
263 @param Divisor A 64-bit unsigned value.\r
264 @param Remainder A pointer to a 64-bit unsigned value. This parameter is\r
265 optional and may be NULL.\r
266\r
267 @return Dividend / Divisor\r
268\r
269**/\r
878ddf1f 270UINT64\r
271EFIAPI\r
272InternalMathDivRemU64x64 (\r
273 IN UINT64 Dividend,\r
274 IN UINT64 Divisor,\r
275 OUT UINT64 *Remainder\r
276 );\r
277\r
1ea5ca46 278/**\r
bda7fbcc 279 Divides a 64-bit signed integer by a 64-bit signed integer and\r
1ea5ca46 280 generates a 64-bit signed result and a optional 64-bit signed remainder.\r
281\r
282 This function divides the 64-bit unsigned value Dividend by the 64-bit\r
283 unsigned value Divisor and generates a 64-bit unsigned quotient. If Remainder\r
284 is not NULL, then the 64-bit unsigned remainder is returned in Remainder.\r
285 This function returns the 64-bit unsigned quotient.\r
286\r
287 @param Dividend A 64-bit signed value.\r
288 @param Divisor A 64-bit signed value.\r
289 @param Remainder A pointer to a 64-bit signed value. This parameter is\r
290 optional and may be NULL.\r
291\r
292 @return Dividend / Divisor\r
293\r
294**/\r
878ddf1f 295INT64\r
878ddf1f 296InternalMathDivRemS64x64 (\r
297 IN INT64 Dividend,\r
298 IN INT64 Divisor,\r
1ea5ca46 299 OUT INT64 *Remainder OPTIONAL\r
bda7fbcc 300 );\r
878ddf1f 301\r
1ea5ca46 302/**\r
303 Transfers control to a function starting with a new stack.\r
304\r
305 Transfers control to the function specified by EntryPoint using the new stack\r
306 specified by NewStack and passing in the parameters specified by Context1 and\r
307 Context2. Context1 and Context2 are optional and may be NULL. The function\r
308 EntryPoint must never return.\r
309\r
310 @param EntryPoint A pointer to function to call with the new stack.\r
311 @param Context1 A pointer to the context to pass into the EntryPoint\r
312 function.\r
313 @param Context2 A pointer to the context to pass into the EntryPoint\r
314 function.\r
315 @param NewStack A pointer to the new stack to use for the EntryPoint\r
316 function.\r
317\r
318**/\r
7d7c2b46 319VOID\r
bda7fbcc 320EFIAPI\r
7d7c2b46 321InternalSwitchStack (\r
322 IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
323 IN VOID *Context1,\r
324 IN VOID *Context2,\r
325 IN VOID *NewStack\r
326 );\r
327\r
878ddf1f 328//\r
329// Ia32 and x64 specific functions\r
330//\r
331\r
1ea5ca46 332/**\r
333 Reads the current Global Descriptor Table Register(GDTR) descriptor.\r
334\r
335 Reads and returns the current GDTR descriptor and returns it in Gdtr. This\r
336 function is only available on IA-32 and X64.\r
337\r
338 @param Gdtr Pointer to a GDTR descriptor.\r
339\r
340**/\r
878ddf1f 341VOID\r
342EFIAPI\r
343InternalX86ReadGdtr (\r
344 OUT IA32_DESCRIPTOR *Gdtr\r
345 );\r
346\r
1ea5ca46 347/**\r
348 Writes the current Global Descriptor Table Register (GDTR) descriptor.\r
349\r
350 Writes and the current GDTR descriptor specified by Gdtr. This function is\r
351 only available on IA-32 and X64.\r
352\r
353 @param Gdtr Pointer to a GDTR descriptor.\r
354\r
355**/\r
878ddf1f 356VOID\r
357EFIAPI\r
358InternalX86WriteGdtr (\r
359 IN CONST IA32_DESCRIPTOR *Gdtr\r
360 );\r
361\r
1ea5ca46 362/**\r
363 Reads the current Interrupt Descriptor Table Register(GDTR) descriptor.\r
364\r
365 Reads and returns the current IDTR descriptor and returns it in Idtr. This\r
366 function is only available on IA-32 and X64.\r
367\r
368 @param Idtr Pointer to a IDTR descriptor.\r
369\r
370**/\r
878ddf1f 371VOID\r
372EFIAPI\r
373InternalX86ReadIdtr (\r
374 OUT IA32_DESCRIPTOR *Idtr\r
375 );\r
376\r
1ea5ca46 377/**\r
378 Writes the current Interrupt Descriptor Table Register(GDTR) descriptor.\r
379\r
380 Writes the current IDTR descriptor and returns it in Idtr. This function is\r
381 only available on IA-32 and X64.\r
382\r
383 @param Idtr Pointer to a IDTR descriptor.\r
384\r
385**/\r
878ddf1f 386VOID\r
387EFIAPI\r
388InternalX86WriteIdtr (\r
389 IN CONST IA32_DESCRIPTOR *Idtr\r
390 );\r
391\r
1ea5ca46 392/**\r
393 Save the current floating point/SSE/SSE2 context to a buffer.\r
394\r
395 Saves the current floating point/SSE/SSE2 state to the buffer specified by\r
396 Buffer. Buffer must be aligned on a 16-byte boundary. This function is only\r
397 available on IA-32 and X64.\r
398\r
399 @param Buffer Pointer to a buffer to save the floating point/SSE/SSE2 context.\r
400\r
401**/\r
878ddf1f 402VOID\r
403EFIAPI\r
404InternalX86FxSave (\r
405 OUT IA32_FX_BUFFER *Buffer\r
406 );\r
407\r
1ea5ca46 408/**\r
409 Restores the current floating point/SSE/SSE2 context from a buffer.\r
410\r
411 Restores the current floating point/SSE/SSE2 state from the buffer specified\r
412 by Buffer. Buffer must be aligned on a 16-byte boundary. This function is\r
413 only available on IA-32 and X64.\r
414\r
415 @param Buffer Pointer to a buffer to save the floating point/SSE/SSE2 context.\r
416\r
417**/\r
878ddf1f 418VOID\r
419EFIAPI\r
420InternalX86FxRestore (\r
421 IN CONST IA32_FX_BUFFER *Buffer\r
422 );\r
423\r
1ea5ca46 424/**\r
425 Enables the 32-bit paging mode on the CPU.\r
426\r
427 Enables the 32-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables\r
428 must be properly initialized prior to calling this service. This function\r
429 assumes the current execution mode is 32-bit protected mode. This function is\r
430 only available on IA-32. After the 32-bit paging mode is enabled, control is\r
431 transferred to the function specified by EntryPoint using the new stack\r
432 specified by NewStack and passing in the parameters specified by Context1 and\r
433 Context2. Context1 and Context2 are optional and may be NULL. The function\r
434 EntryPoint must never return.\r
435\r
436 There are a number of constraints that must be followed before calling this\r
437 function:\r
438 1) Interrupts must be disabled.\r
439 2) The caller must be in 32-bit protected mode with flat descriptors. This\r
440 means all descriptors must have a base of 0 and a limit of 4GB.\r
441 3) CR0 and CR4 must be compatible with 32-bit protected mode with flat\r
442 descriptors.\r
443 4) CR3 must point to valid page tables that will be used once the transition\r
444 is complete, and those page tables must guarantee that the pages for this\r
445 function and the stack are identity mapped.\r
446\r
447 @param EntryPoint A pointer to function to call with the new stack after\r
448 paging is enabled.\r
449 @param Context1 A pointer to the context to pass into the EntryPoint\r
450 function as the first parameter after paging is enabled.\r
451 @param Context2 A pointer to the context to pass into the EntryPoint\r
452 function as the second parameter after paging is enabled.\r
453 @param NewStack A pointer to the new stack to use for the EntryPoint\r
454 function after paging is enabled.\r
455\r
456**/\r
878ddf1f 457VOID\r
458EFIAPI\r
459InternalX86EnablePaging32 (\r
460 IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
461 IN VOID *Context1, OPTIONAL\r
462 IN VOID *Context2, OPTIONAL\r
463 IN VOID *NewStack\r
464 );\r
465\r
1ea5ca46 466/**\r
467 Disables the 32-bit paging mode on the CPU.\r
468\r
469 Disables the 32-bit paging mode on the CPU and returns to 32-bit protected\r
470 mode. This function assumes the current execution mode is 32-paged protected\r
471 mode. This function is only available on IA-32. After the 32-bit paging mode\r
472 is disabled, control is transferred to the function specified by EntryPoint\r
473 using the new stack specified by NewStack and passing in the parameters\r
474 specified by Context1 and Context2. Context1 and Context2 are optional and\r
475 may be NULL. The function EntryPoint must never return.\r
476\r
477 There are a number of constraints that must be followed before calling this\r
478 function:\r
479 1) Interrupts must be disabled.\r
480 2) The caller must be in 32-bit paged mode.\r
481 3) CR0, CR3, and CR4 must be compatible with 32-bit paged mode.\r
482 4) CR3 must point to valid page tables that guarantee that the pages for\r
483 this function and the stack are identity mapped.\r
484\r
485 @param EntryPoint A pointer to function to call with the new stack after\r
486 paging is disabled.\r
487 @param Context1 A pointer to the context to pass into the EntryPoint\r
488 function as the first parameter after paging is disabled.\r
489 @param Context2 A pointer to the context to pass into the EntryPoint\r
490 function as the second parameter after paging is\r
491 disabled.\r
492 @param NewStack A pointer to the new stack to use for the EntryPoint\r
493 function after paging is disabled.\r
494\r
495**/\r
878ddf1f 496VOID\r
497EFIAPI\r
498InternalX86DisablePaging32 (\r
499 IN SWITCH_STACK_ENTRY_POINT EntryPoint,\r
500 IN VOID *Context1, OPTIONAL\r
501 IN VOID *Context2, OPTIONAL\r
502 IN VOID *NewStack\r
503 );\r
504\r
1ea5ca46 505/**\r
506 Enables the 64-bit paging mode on the CPU.\r
507\r
508 Enables the 64-bit paging mode on the CPU. CR0, CR3, CR4, and the page tables\r
509 must be properly initialized prior to calling this service. This function\r
510 assumes the current execution mode is 32-bit protected mode with flat\r
511 descriptors. This function is only available on IA-32. After the 64-bit\r
512 paging mode is enabled, control is transferred to the function specified by\r
513 EntryPoint using the new stack specified by NewStack and passing in the\r
514 parameters specified by Context1 and Context2. Context1 and Context2 are\r
515 optional and may be 0. The function EntryPoint must never return.\r
516\r
517 @param Cs The 16-bit selector to load in the CS before EntryPoint\r
518 is called. The descriptor in the GDT that this selector\r
519 references must be setup for long mode.\r
520 @param EntryPoint The 64-bit virtual address of the function to call with\r
521 the new stack after paging is enabled.\r
522 @param Context1 The 64-bit virtual address of the context to pass into\r
523 the EntryPoint function as the first parameter after\r
524 paging is enabled.\r
525 @param Context2 The 64-bit virtual address of the context to pass into\r
526 the EntryPoint function as the second parameter after\r
527 paging is enabled.\r
528 @param NewStack The 64-bit virtual address of the new stack to use for\r
529 the EntryPoint function after paging is enabled.\r
530\r
531**/\r
878ddf1f 532VOID\r
533EFIAPI\r
534InternalX86EnablePaging64 (\r
535 IN UINT16 Cs,\r
536 IN UINT64 EntryPoint,\r
537 IN UINT64 Context1, OPTIONAL\r
538 IN UINT64 Context2, OPTIONAL\r
539 IN UINT64 NewStack\r
540 );\r
541\r
1ea5ca46 542/**\r
543 Disables the 64-bit paging mode on the CPU.\r
544\r
545 Disables the 64-bit paging mode on the CPU and returns to 32-bit protected\r
546 mode. This function assumes the current execution mode is 64-paging mode.\r
547 This function is only available on X64. After the 64-bit paging mode is\r
548 disabled, control is transferred to the function specified by EntryPoint\r
549 using the new stack specified by NewStack and passing in the parameters\r
550 specified by Context1 and Context2. Context1 and Context2 are optional and\r
551 may be 0. The function EntryPoint must never return.\r
552\r
553 @param Cs The 16-bit selector to load in the CS before EntryPoint\r
554 is called. The descriptor in the GDT that this selector\r
555 references must be setup for 32-bit protected mode.\r
556 @param EntryPoint The 64-bit virtual address of the function to call with\r
557 the new stack after paging is disabled.\r
558 @param Context1 The 64-bit virtual address of the context to pass into\r
559 the EntryPoint function as the first parameter after\r
560 paging is disabled.\r
561 @param Context2 The 64-bit virtual address of the context to pass into\r
562 the EntryPoint function as the second parameter after\r
563 paging is disabled.\r
564 @param NewStack The 64-bit virtual address of the new stack to use for\r
565 the EntryPoint function after paging is disabled.\r
566\r
567**/\r
878ddf1f 568VOID\r
569EFIAPI\r
570InternalX86DisablePaging64 (\r
571 IN UINT16 Cs,\r
572 IN UINT32 EntryPoint,\r
573 IN UINT32 Context1, OPTIONAL\r
574 IN UINT32 Context2, OPTIONAL\r
575 IN UINT32 NewStack\r
576 );\r
577\r
578#endif\r