]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.nasm
MdePkg BaseLib: Convert Ia32/WriteDr3.asm to NASM
[mirror_edk2.git] / MdePkg / Library / BaseLib / Ia32 / DivU64x64Remainder.nasm
CommitLineData
6074ca70
JJ
1;------------------------------------------------------------------------------\r
2;\r
3; Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>\r
4; 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.nasm\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 SECTION .text\r
24\r
25extern ASM_PFX(InternalMathDivRemU64x32)\r
26\r
27;------------------------------------------------------------------------------\r
28; UINT64\r
29; EFIAPI\r
30; InternalMathDivRemU64x64 (\r
31; IN UINT64 Dividend,\r
32; IN UINT64 Divisor,\r
33; OUT UINT64 *Remainder OPTIONAL\r
34; );\r
35;------------------------------------------------------------------------------\r
36global ASM_PFX(InternalMathDivRemU64x64)\r
37ASM_PFX(InternalMathDivRemU64x64):\r
38 mov ecx, [esp + 16] ; ecx <- divisor[32..63]\r
39 test ecx, ecx\r
40 jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32\r
41 mov ecx, [esp + 20]\r
42 jecxz .0\r
43 and dword [ecx + 4], 0 ; zero high dword of remainder\r
44 mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32\r
45.0:\r
46 jmp ASM_PFX(InternalMathDivRemU64x32)\r
47\r
48_@DivRemU64x64:\r
49 push ebx\r
50 push esi\r
51 push edi\r
52 mov edx, dword [esp + 20]\r
53 mov eax, dword [esp + 16] ; edx:eax <- dividend\r
54 mov edi, edx\r
55 mov esi, eax ; edi:esi <- dividend\r
56 mov ebx, dword [esp + 24] ; ecx:ebx <- divisor\r
57.1:\r
58 shr edx, 1\r
59 rcr eax, 1\r
60 shrd ebx, ecx, 1\r
61 shr ecx, 1\r
62 jnz .1\r
63 div ebx\r
64 mov ebx, eax ; ebx <- quotient\r
65 mov ecx, [esp + 28] ; ecx <- high dword of divisor\r
66 mul dword [esp + 24] ; edx:eax <- quotient * divisor[0..31]\r
67 imul ecx, ebx ; ecx <- quotient * divisor[32..63]\r
68 add edx, ecx ; edx <- (quotient * divisor)[32..63]\r
69 mov ecx, dword [esp + 32] ; ecx <- addr for Remainder\r
70 jc @TooLarge ; product > 2^64\r
71 cmp edi, edx ; compare high 32 bits\r
72 ja @Correct\r
73 jb @TooLarge ; product > dividend\r
74 cmp esi, eax\r
75 jae @Correct ; product <= dividend\r
76@TooLarge:\r
77 dec ebx ; adjust quotient by -1\r
78 jecxz @Return ; return if Remainder == NULL\r
79 sub eax, dword [esp + 24]\r
80 sbb edx, dword [esp + 28] ; edx:eax <- (quotient - 1) * divisor\r
81@Correct:\r
82 jecxz @Return\r
83 sub esi, eax\r
84 sbb edi, edx ; edi:esi <- remainder\r
85 mov [ecx], esi\r
86 mov [ecx + 4], edi\r
87@Return:\r
88 mov eax, ebx ; eax <- quotient\r
89 xor edx, edx ; quotient is 32 bits long\r
90 pop edi\r
91 pop esi\r
92 pop ebx\r
93 ret\r
94\r