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