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