]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.c
1. Remove #ifdef _MSC_EXTENSION_ from all source files
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / DivU64x64Remainder.c
CommitLineData
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
e5f461a8 5 Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
23086ba8 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
23086ba8 16UINT64\r
17EFIAPI\r
18InternalMathDivRemU64x64 (\r
19 IN UINT64 Dividend,\r
20 IN UINT64 Divisor,\r
21 OUT UINT64 *Remainder OPTIONAL\r
22 )\r
23{\r
24 _asm {\r
25 mov edx, dword ptr [Dividend + 4]\r
26 mov eax, dword ptr [Dividend + 0] // edx:eax <- dividend\r
27 mov edi, edx\r
28 mov esi, eax // edi:esi <- dividend\r
2f8df870 29 mov ecx, dword ptr [Divisor + 4]\r
23086ba8 30 mov ebx, dword ptr [Divisor + 0] // ecx:ebx <- divisor\r
31BitLoop:\r
32 shr edx, 1\r
33 rcr eax, 1\r
34 shrd ebx, ecx, 1\r
35 shr ecx, 1\r
36 jnz BitLoop\r
37 div ebx\r
38 mov ebx, eax // ebx <- quotient\r
39 mov ecx, dword ptr [Divisor + 4]\r
40 mul dword ptr [Divisor]\r
41 imul ecx, ebx\r
42 add edx, ecx\r
43 mov ecx, Remainder\r
44 jc TooLarge // product > 2^64\r
45 cmp edi, edx // compare high 32 bits\r
46 ja Correct\r
47 jb TooLarge // product > dividend\r
48 cmp esi, eax\r
49 jae Correct // product <= dividend\r
50TooLarge:\r
51 dec ebx // adjust quotient by -1\r
52 jecxz Return // return if Remainder == NULL\r
53 sub eax, dword ptr [Divisor + 0]\r
54 sbb edx, dword ptr [Divisor + 4]\r
55Correct:\r
56 jecxz Return\r
57 sub esi, eax\r
58 sbb edi, edx // edi:esi <- remainder\r
59 mov [ecx], esi\r
60 mov [ecx + 4], edi\r
61Return:\r
62 mov eax, ebx // eax <- quotient\r
63 xor edx, edx\r
64 }\r
65}\r
66\r