]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.c
Removed CommonHeader.h generated file from the MdePkg.
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / DivU64x64Remainder.c
CommitLineData
e1f414b6 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 - 2007, 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//\r
17// Include common header file for this module.\r
18//\r
f734a10a 19\r
e1f414b6 20\r
21UINT64\r
22EFIAPI\r
23InternalMathDivRemU64x64 (\r
24 IN UINT64 Dividend,\r
25 IN UINT64 Divisor,\r
26 OUT UINT64 *Remainder OPTIONAL\r
27 )\r
28{\r
29 _asm {\r
30 mov edx, dword ptr [Dividend + 4]\r
31 mov eax, dword ptr [Dividend + 0] // edx:eax <- dividend\r
32 mov edi, edx\r
33 mov esi, eax // edi:esi <- dividend\r
34 mov ecx, dword ptr [Divisor + 4]\r
35 mov ebx, dword ptr [Divisor + 0] // ecx:ebx <- divisor\r
36BitLoop:\r
37 shr edx, 1\r
38 rcr eax, 1\r
39 shrd ebx, ecx, 1\r
40 shr ecx, 1\r
41 jnz BitLoop\r
42 div ebx\r
43 mov ebx, eax // ebx <- quotient\r
44 mov ecx, dword ptr [Divisor + 4]\r
45 mul dword ptr [Divisor]\r
46 imul ecx, ebx\r
47 add edx, ecx\r
48 mov ecx, Remainder\r
49 jc TooLarge // product > 2^64\r
50 cmp edi, edx // compare high 32 bits\r
51 ja Correct\r
52 jb TooLarge // product > dividend\r
53 cmp esi, eax\r
54 jae Correct // product <= dividend\r
55TooLarge:\r
56 dec ebx // adjust quotient by -1\r
57 jecxz Return // return if Remainder == NULL\r
58 sub eax, dword ptr [Divisor + 0]\r
59 sbb edx, dword ptr [Divisor + 4]\r
60Correct:\r
61 jecxz Return\r
62 sub esi, eax\r
63 sbb edi, edx // edi:esi <- remainder\r
64 mov [ecx], esi\r
65 mov [ecx + 4], edi\r
66Return:\r
67 mov eax, ebx // eax <- quotient\r
68 xor edx, edx\r
69 }\r
70}\r
71\r