1 ;------------------------------------------------------------------------------
3 ; Copyright (c) 2006, Intel Corporation. All rights reserved.<BR>
4 ; SPDX-License-Identifier: BSD-2-Clause-Patent
8 ; DivU64x64Remainder.nasm
12 ; Calculate the quotient of a 64-bit integer by a 64-bit integer and returns
13 ; both the quotient and the remainder
15 ;------------------------------------------------------------------------------
19 extern ASM_PFX(InternalMathDivRemU64x32)
21 ;------------------------------------------------------------------------------
24 ; InternalMathDivRemU64x64 (
27 ; OUT UINT64 *Remainder OPTIONAL
29 ;------------------------------------------------------------------------------
30 global ASM_PFX(InternalMathDivRemU64x64)
31 ASM_PFX(InternalMathDivRemU64x64):
32 mov ecx, [esp + 16] ; ecx <- divisor[32..63]
34 jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32
37 and dword [ecx + 4], 0 ; zero high dword of remainder
38 mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32
40 jmp ASM_PFX(InternalMathDivRemU64x32)
46 mov edx, dword [esp + 20]
47 mov eax, dword [esp + 16] ; edx:eax <- dividend
49 mov esi, eax ; edi:esi <- dividend
50 mov ebx, dword [esp + 24] ; ecx:ebx <- divisor
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
67 jb @TooLarge ; product > dividend
69 jae @Correct ; product <= dividend
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
78 sbb edi, edx ; edi:esi <- remainder
82 mov eax, ebx ; eax <- quotient
83 xor edx, edx ; quotient is 32 bits long