1 ; Copyright (c) 2004, Intel Corporation
2 ; All rights reserved. This program and the accompanying materials
3 ; are licensed and made available under the terms and conditions of the BSD License
4 ; which accompanies this distribution. The full text of the license may be found at
5 ; http://opensource.org/licenses/bsd-license.php
7 ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12 ; DivU64x64Remainder.asm
16 ; Calculate the quotient of a 64-bit integer by a 64-bit integer and returns
17 ; both the quotient and the remainder
19 ;------------------------------------------------------------------------------
25 EXTERN InternalMathDivRemU64x32:PROC
27 ;------------------------------------------------------------------------------
30 ; InternalMathDivRemU64x64 (
33 ; OUT UINT64 *Remainder OPTIONAL
35 ;------------------------------------------------------------------------------
36 InternalMathDivRemU64x64 PROC
37 mov ecx, [esp + 16] ; ecx <- divisor[32..63]
39 jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32
42 and dword ptr [ecx + 4], 0 ; zero high dword of remainder
43 mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32
45 jmp InternalMathDivRemU64x32
46 InternalMathDivRemU64x64 ENDP
48 _@DivRemU64x64 PROC USES ebx esi edi
49 mov edx, dword ptr [esp + 20]
50 mov eax, dword ptr [esp + 16] ; edx:eax <- dividend
52 mov esi, eax ; edi:esi <- dividend
53 mov ebx, dword ptr [esp + 24] ; ecx:ebx <- divisor
61 mov ebx, eax ; ebx <- quotient
62 mov ecx, [esp + 28] ; ecx <- high dword of divisor
63 mul dword ptr [esp + 24] ; edx:eax <- quotient * divisor[0..31]
64 imul ecx, ebx ; ecx <- quotient * divisor[32..63]
65 add edx, ecx ; edx <- (quotient * divisor)[32..63]
66 mov ecx, dword ptr [esp + 32] ; ecx <- addr for Remainder
67 jc @TooLarge ; product > 2^64
68 cmp edi, edx ; compare high 32 bits
70 jb @TooLarge ; product > dividend
72 jae @Correct ; product <= dividend
74 dec ebx ; adjust quotient by -1
75 jecxz @Return ; return if Remainder == NULL
76 sub eax, dword ptr [esp + 24]
77 sbb edx, dword ptr [esp + 28] ; edx:eax <- (quotient - 1) * divisor
81 sbb edi, edx ; edi:esi <- remainder
85 mov eax, ebx ; eax <- quotient
86 xor edx, edx ; quotient is 32 bits long