]>
Commit | Line | Data |
---|---|---|
3eb9473e | 1 | ; Copyright (c) 2004, Intel Corporation \r |
2 | ; All rights reserved. This program and the accompanying materials \r | |
3 | ; are licensed and made available under the terms and conditions of the BSD License \r | |
4 | ; which accompanies this distribution. The full text of the license may be found at \r | |
5 | ; http://opensource.org/licenses/bsd-license.php \r | |
6 | ; \r | |
7 | ; THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r | |
8 | ; WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r | |
9 | ;\r | |
10 | ; Module Name:\r | |
11 | ;\r | |
12 | ; DivU64x64Remainder.asm\r | |
13 | ;\r | |
14 | ; Abstract:\r | |
15 | ;\r | |
16 | ; Calculate the quotient of a 64-bit integer by a 64-bit integer and returns\r | |
17 | ; both the quotient and the remainder\r | |
18 | ;\r | |
19 | ;------------------------------------------------------------------------------\r | |
20 | \r | |
21 | .386\r | |
22 | .model flat,C\r | |
23 | .code\r | |
24 | \r | |
25 | EXTERN InternalMathDivRemU64x32:PROC\r | |
26 | \r | |
27 | ;------------------------------------------------------------------------------\r | |
28 | ; UINT64\r | |
29 | ; EFIAPI\r | |
30 | ; InternalMathDivRemU64x64 (\r | |
31 | ; IN UINT64 Dividend,\r | |
32 | ; IN UINT64 Divisor,\r | |
33 | ; OUT UINT64 *Remainder OPTIONAL\r | |
34 | ; );\r | |
35 | ;------------------------------------------------------------------------------\r | |
36 | InternalMathDivRemU64x64 PROC\r | |
37 | mov ecx, [esp + 16] ; ecx <- divisor[32..63]\r | |
38 | test ecx, ecx\r | |
39 | jnz _@DivRemU64x64 ; call _@DivRemU64x64 if Divisor > 2^32\r | |
40 | mov ecx, [esp + 20]\r | |
41 | jecxz @F\r | |
42 | and dword ptr [ecx + 4], 0 ; zero high dword of remainder\r | |
43 | mov [esp + 16], ecx ; set up stack frame to match DivRemU64x32\r | |
44 | @@:\r | |
45 | jmp InternalMathDivRemU64x32\r | |
46 | InternalMathDivRemU64x64 ENDP\r | |
47 | \r | |
48 | _@DivRemU64x64 PROC USES ebx esi edi\r | |
49 | mov edx, dword ptr [esp + 20]\r | |
50 | mov eax, dword ptr [esp + 16] ; edx:eax <- dividend\r | |
51 | mov edi, edx\r | |
52 | mov esi, eax ; edi:esi <- dividend\r | |
53 | mov ebx, dword ptr [esp + 24] ; ecx:ebx <- divisor\r | |
54 | @@:\r | |
55 | shr edx, 1\r | |
56 | rcr eax, 1\r | |
57 | shrd ebx, ecx, 1\r | |
58 | shr ecx, 1\r | |
59 | jnz @B\r | |
60 | div ebx\r | |
61 | mov ebx, eax ; ebx <- quotient\r | |
62 | mov ecx, [esp + 28] ; ecx <- high dword of divisor\r | |
63 | mul dword ptr [esp + 24] ; edx:eax <- quotient * divisor[0..31]\r | |
64 | imul ecx, ebx ; ecx <- quotient * divisor[32..63]\r | |
65 | add edx, ecx ; edx <- (quotient * divisor)[32..63]\r | |
66 | mov ecx, dword ptr [esp + 32] ; ecx <- addr for Remainder\r | |
67 | jc @TooLarge ; product > 2^64\r | |
68 | cmp edi, edx ; compare high 32 bits\r | |
69 | ja @Correct\r | |
70 | jb @TooLarge ; product > dividend\r | |
71 | cmp esi, eax\r | |
72 | jae @Correct ; product <= dividend\r | |
73 | @TooLarge:\r | |
74 | dec ebx ; adjust quotient by -1\r | |
75 | jecxz @Return ; return if Remainder == NULL\r | |
76 | sub eax, dword ptr [esp + 24]\r | |
77 | sbb edx, dword ptr [esp + 28] ; edx:eax <- (quotient - 1) * divisor\r | |
78 | @Correct:\r | |
79 | jecxz @Return\r | |
80 | sub esi, eax\r | |
81 | sbb edi, edx ; edi:esi <- remainder\r | |
82 | mov [ecx], esi\r | |
83 | mov [ecx + 4], edi\r | |
84 | @Return:\r | |
85 | mov eax, ebx ; eax <- quotient\r | |
86 | xor edx, edx ; quotient is 32 bits long\r | |
87 | ret\r | |
88 | _@DivRemU64x64 ENDP\r | |
89 | \r | |
90 | END\r |