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