]> git.proxmox.com Git - mirror_edk2.git/blame - MdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.nasm
MdePkg: Replace BSD License with BSD+Patent License
[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
9344f092 4; SPDX-License-Identifier: BSD-2-Clause-Patent\r
6074ca70
JJ
5;\r
6; Module Name:\r
7;\r
8; DivU64x64Remainder.nasm\r
9;\r
10; Abstract:\r
11;\r
12; Calculate the quotient of a 64-bit integer by a 64-bit integer and returns\r
13; both the quotient and the remainder\r
14;\r
15;------------------------------------------------------------------------------\r
16\r
17 SECTION .text\r
18\r
19extern ASM_PFX(InternalMathDivRemU64x32)\r
20\r
21;------------------------------------------------------------------------------\r
22; UINT64\r
23; EFIAPI\r
24; InternalMathDivRemU64x64 (\r
25; IN UINT64 Dividend,\r
26; IN UINT64 Divisor,\r
27; OUT UINT64 *Remainder OPTIONAL\r
28; );\r
29;------------------------------------------------------------------------------\r
30global ASM_PFX(InternalMathDivRemU64x64)\r
31ASM_PFX(InternalMathDivRemU64x64):\r
32 mov ecx, [esp + 16] ; ecx <- divisor[32..63]\r
33 test ecx, ecx\r
34 jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32\r
35 mov ecx, [esp + 20]\r
36 jecxz .0\r
37 and dword [ecx + 4], 0 ; zero high dword of remainder\r
38 mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32\r
39.0:\r
40 jmp ASM_PFX(InternalMathDivRemU64x32)\r
41\r
42_@DivRemU64x64:\r
43 push ebx\r
44 push esi\r
45 push edi\r
46 mov edx, dword [esp + 20]\r
47 mov eax, dword [esp + 16] ; edx:eax <- dividend\r
48 mov edi, edx\r
49 mov esi, eax ; edi:esi <- dividend\r
50 mov ebx, dword [esp + 24] ; ecx:ebx <- divisor\r
51.1:\r
52 shr edx, 1\r
53 rcr eax, 1\r
54 shrd ebx, ecx, 1\r
55 shr ecx, 1\r
56 jnz .1\r
57 div ebx\r
58 mov ebx, eax ; ebx <- quotient\r
59 mov ecx, [esp + 28] ; ecx <- high dword of divisor\r
60 mul dword [esp + 24] ; edx:eax <- quotient * divisor[0..31]\r
61 imul ecx, ebx ; ecx <- quotient * divisor[32..63]\r
62 add edx, ecx ; edx <- (quotient * divisor)[32..63]\r
63 mov ecx, dword [esp + 32] ; ecx <- addr for Remainder\r
64 jc @TooLarge ; product > 2^64\r
65 cmp edi, edx ; compare high 32 bits\r
66 ja @Correct\r
67 jb @TooLarge ; product > dividend\r
68 cmp esi, eax\r
69 jae @Correct ; product <= dividend\r
70@TooLarge:\r
71 dec ebx ; adjust quotient by -1\r
72 jecxz @Return ; return if Remainder == NULL\r
73 sub eax, dword [esp + 24]\r
74 sbb edx, dword [esp + 28] ; edx:eax <- (quotient - 1) * divisor\r
75@Correct:\r
76 jecxz @Return\r
77 sub esi, eax\r
78 sbb edi, edx ; edi:esi <- remainder\r
79 mov [ecx], esi\r
80 mov [ecx + 4], edi\r
81@Return:\r
82 mov eax, ebx ; eax <- quotient\r
83 xor edx, edx ; quotient is 32 bits long\r
84 pop edi\r
85 pop esi\r
86 pop ebx\r
87 ret\r
88\r