]> git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Foundation/Library/EdkIIGlueLib/Library/BaseLib/Ia32/DivU64x64Remainder.asm
1) Sync EdkCompatibilityPkg with EDK 1.04. The changes includes:
[mirror_edk2.git] / EdkCompatibilityPkg / Foundation / Library / EdkIIGlueLib / Library / BaseLib / Ia32 / DivU64x64Remainder.asm
1 ; Copyright (c) 2004, Intel Corporation
2 ; All rights reserved. This program and the accompanying materials
3 ; are licensed and made available under the terms and conditions of the BSD License
4 ; which accompanies this distribution. The full text of the license may be found at
5 ; http://opensource.org/licenses/bsd-license.php
6 ;
7 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
9 ;
10 ; Module Name:
11 ;
12 ; DivU64x64Remainder.asm
13 ;
14 ; Abstract:
15 ;
16 ; Calculate the quotient of a 64-bit integer by a 64-bit integer and returns
17 ; both the quotient and the remainder
18 ;
19 ;------------------------------------------------------------------------------
20
21 .386
22 .model flat,C
23 .code
24
25 EXTERN InternalMathDivRemU64x32:PROC
26
27 ;------------------------------------------------------------------------------
28 ; UINT64
29 ; EFIAPI
30 ; InternalMathDivRemU64x64 (
31 ; IN UINT64 Dividend,
32 ; IN UINT64 Divisor,
33 ; OUT UINT64 *Remainder OPTIONAL
34 ; );
35 ;------------------------------------------------------------------------------
36 InternalMathDivRemU64x64 PROC
37 mov ecx, [esp + 16] ; ecx <- divisor[32..63]
38 test ecx, ecx
39 jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32
40 mov ecx, [esp + 20]
41 jecxz @F
42 and dword ptr [ecx + 4], 0 ; zero high dword of remainder
43 mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32
44 @@:
45 jmp InternalMathDivRemU64x32
46 InternalMathDivRemU64x64 ENDP
47
48 _@DivRemU64x64 PROC USES ebx esi edi
49 mov edx, dword ptr [esp + 20]
50 mov eax, dword ptr [esp + 16] ; edx:eax <- dividend
51 mov edi, edx
52 mov esi, eax ; edi:esi <- dividend
53 mov ebx, dword ptr [esp + 24] ; ecx:ebx <- divisor
54 @@:
55 shr edx, 1
56 rcr eax, 1
57 shrd ebx, ecx, 1
58 shr ecx, 1
59 jnz @B
60 div ebx
61 mov ebx, eax ; ebx <- quotient
62 mov ecx, [esp + 28] ; ecx <- high dword of divisor
63 mul dword ptr [esp + 24] ; edx:eax <- quotient * divisor[0..31]
64 imul ecx, ebx ; ecx <- quotient * divisor[32..63]
65 add edx, ecx ; edx <- (quotient * divisor)[32..63]
66 mov ecx, dword ptr [esp + 32] ; ecx <- addr for Remainder
67 jc @TooLarge ; product > 2^64
68 cmp edi, edx ; compare high 32 bits
69 ja @Correct
70 jb @TooLarge ; product > dividend
71 cmp esi, eax
72 jae @Correct ; product <= dividend
73 @TooLarge:
74 dec ebx ; adjust quotient by -1
75 jecxz @Return ; return if Remainder == NULL
76 sub eax, dword ptr [esp + 24]
77 sbb edx, dword ptr [esp + 28] ; edx:eax <- (quotient - 1) * divisor
78 @Correct:
79 jecxz @Return
80 sub esi, eax
81 sbb edi, edx ; edi:esi <- remainder
82 mov [ecx], esi
83 mov [ecx + 4], edi
84 @Return:
85 mov eax, ebx ; eax <- quotient
86 xor edx, edx ; quotient is 32 bits long
87 ret
88 _@DivRemU64x64 ENDP
89
90 END