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