1 #------------------------------------------------------------------------------
3 # Copyright (c) 2006 - 2008, Intel Corporation
4 # All rights reserved. This program and the accompanying materials
5 # are licensed and made available under the terms and conditions of the BSD License
6 # which accompanies this distribution. The full text of the license may be found at
7 # http://opensource.org/licenses/bsd-license.php
9 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14 # DivU64x64Remainder.S
18 # Calculate the quotient of a 64-bit integer by a 64-bit integer and returns
19 # both the quotient and the remainder
21 #------------------------------------------------------------------------------
23 .globl ASM_PFX(InternalMathDivRemU64x32), ASM_PFX(InternalMathDivRemU64x64)
25 #------------------------------------------------------------------------------
28 # InternalMathDivRemU64x64 (
31 # OUT UINT64 *Remainder OPTIONAL
33 #------------------------------------------------------------------------------
34 ASM_PFX(InternalMathDivRemU64x64):
35 movl 16(%esp), %ecx # ecx <- divisor[32..63]
37 jnz Hard # call _@DivRemU64x64 if Divisor > 2^32
40 and $0, 4(%ecx) # zero high dword of remainder
41 movl %ecx, 16(%esp) # set up stack frame to match DivRemU64x32
43 jmp ASM_PFX(InternalMathDivRemU64x32)
49 mov 16(%esp), %eax # edx:eax <- dividend
51 movl %eax, %esi # edi:esi <- dividend
52 mov 24(%esp), %ebx # ecx:ebx <- divisor
60 movl %eax, %ebx # ebx <- quotient
61 movl 28(%esp), %ecx # ecx <- high dword of divisor
62 mull 24(%esp) # edx:eax <- quotient * divisor[0..31]
63 imull %ebx, %ecx # ecx <- quotient * divisor[32..63]
64 addl %ecx, %edx # edx <- (quotient * divisor)[32..63]
65 mov 32(%esp), %ecx # ecx <- addr for Remainder
66 jc TooLarge # product > 2^64
67 cmpl %edx, %edi # compare high 32 bits
69 jb TooLarge # product > dividend
71 jae Correct # product <= dividend
73 decl %ebx # adjust quotient by -1
74 jecxz Return # return if Remainder == NULL
76 sbb 28(%esp), %edx # edx:eax <- (quotient - 1) * divisor
80 sbbl %edx, %edi # edi:esi <- remainder
84 movl %ebx, %eax # eax <- quotient
85 xorl %edx, %edx # quotient is 32 bits long