]>
Commit | Line | Data |
---|---|---|
23086ba8 | 1 | /** @file\r |
2 | Calculate the quotient of a 64-bit integer by a 64-bit integer and returns\r | |
3 | both the quotient and the remainderSet error flag for all division functions\r | |
4 | \r | |
5 | Copyright (c) 2006, Intel Corporation<BR>\r | |
6 | All rights reserved. This program and the accompanying materials\r | |
7 | are licensed and made available under the terms and conditions of the BSD License\r | |
8 | which accompanies this distribution. The full text of the license may be found at\r | |
9 | http://opensource.org/licenses/bsd-license.php\r | |
10 | \r | |
11 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
12 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
13 | \r | |
14 | **/\r | |
15 | \r | |
16 | #if _MSC_EXTENSIONS\r | |
17 | \r | |
18 | UINT64\r | |
19 | EFIAPI\r | |
20 | InternalMathDivRemU64x64 (\r | |
21 | IN UINT64 Dividend,\r | |
22 | IN UINT64 Divisor,\r | |
23 | OUT UINT64 *Remainder OPTIONAL\r | |
24 | )\r | |
25 | {\r | |
26 | _asm {\r | |
27 | mov edx, dword ptr [Dividend + 4]\r | |
28 | mov eax, dword ptr [Dividend + 0] // edx:eax <- dividend\r | |
29 | mov edi, edx\r | |
30 | mov esi, eax // edi:esi <- dividend\r | |
2f8df870 | 31 | mov ecx, dword ptr [Divisor + 4]\r |
23086ba8 | 32 | mov ebx, dword ptr [Divisor + 0] // ecx:ebx <- divisor\r |
33 | BitLoop:\r | |
34 | shr edx, 1\r | |
35 | rcr eax, 1\r | |
36 | shrd ebx, ecx, 1\r | |
37 | shr ecx, 1\r | |
38 | jnz BitLoop\r | |
39 | div ebx\r | |
40 | mov ebx, eax // ebx <- quotient\r | |
41 | mov ecx, dword ptr [Divisor + 4]\r | |
42 | mul dword ptr [Divisor]\r | |
43 | imul ecx, ebx\r | |
44 | add edx, ecx\r | |
45 | mov ecx, Remainder\r | |
46 | jc TooLarge // product > 2^64\r | |
47 | cmp edi, edx // compare high 32 bits\r | |
48 | ja Correct\r | |
49 | jb TooLarge // product > dividend\r | |
50 | cmp esi, eax\r | |
51 | jae Correct // product <= dividend\r | |
52 | TooLarge:\r | |
53 | dec ebx // adjust quotient by -1\r | |
54 | jecxz Return // return if Remainder == NULL\r | |
55 | sub eax, dword ptr [Divisor + 0]\r | |
56 | sbb edx, dword ptr [Divisor + 4]\r | |
57 | Correct:\r | |
58 | jecxz Return\r | |
59 | sub esi, eax\r | |
60 | sbb edi, edx // edi:esi <- remainder\r | |
61 | mov [ecx], esi\r | |
62 | mov [ecx + 4], edi\r | |
63 | Return:\r | |
64 | mov eax, ebx // eax <- quotient\r | |
65 | xor edx, edx\r | |
66 | }\r | |
67 | }\r | |
68 | \r | |
69 | #endif\r |