]> git.proxmox.com Git - mirror_edk2.git/blobdiff - OldMdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.c
Moved the MdePkg to OldMdePkg so that new code in MdePkg does not break existing...
[mirror_edk2.git] / OldMdePkg / Library / BaseLib / Ia32 / DivU64x64Remainder.c
diff --git a/OldMdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.c b/OldMdePkg/Library/BaseLib/Ia32/DivU64x64Remainder.c
new file mode 100644 (file)
index 0000000..14b2f27
--- /dev/null
@@ -0,0 +1,66 @@
+/** @file\r
+  Calculate the quotient of a 64-bit integer by a 64-bit integer and returns\r
+  both the quotient and the remainderSet error flag for all division functions\r
+\r
+  Copyright (c) 2006 - 2007, Intel Corporation<BR>\r
+  All rights reserved. This program and the accompanying materials\r
+  are licensed and made available under the terms and conditions of the BSD License\r
+  which accompanies this distribution.  The full text of the license may be found at\r
+  http://opensource.org/licenses/bsd-license.php\r
+\r
+  THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+  WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+\r
+**/\r
+\r
+UINT64\r
+EFIAPI\r
+InternalMathDivRemU64x64 (\r
+  IN      UINT64                    Dividend,\r
+  IN      UINT64                    Divisor,\r
+  OUT     UINT64                    *Remainder    OPTIONAL\r
+  )\r
+{\r
+  _asm {\r
+    mov     edx, dword ptr [Dividend + 4]\r
+    mov     eax, dword ptr [Dividend + 0]   // edx:eax <- dividend\r
+    mov     edi, edx\r
+    mov     esi, eax                    // edi:esi <- dividend\r
+    mov     ecx, dword ptr [Divisor + 4]\r
+    mov     ebx, dword ptr [Divisor + 0]   // ecx:ebx <- divisor\r
+BitLoop:\r
+    shr     edx, 1\r
+    rcr     eax, 1\r
+    shrd    ebx, ecx, 1\r
+    shr     ecx, 1\r
+    jnz     BitLoop\r
+    div     ebx\r
+    mov     ebx, eax                    // ebx <- quotient\r
+    mov     ecx, dword ptr [Divisor + 4]\r
+    mul     dword ptr [Divisor]\r
+    imul    ecx, ebx\r
+    add     edx, ecx\r
+    mov     ecx, Remainder\r
+    jc      TooLarge                   // product > 2^64\r
+    cmp     edi, edx                    // compare high 32 bits\r
+    ja      Correct\r
+    jb      TooLarge                   // product > dividend\r
+    cmp     esi, eax\r
+    jae     Correct                    // product <= dividend\r
+TooLarge:\r
+    dec     ebx                         // adjust quotient by -1\r
+    jecxz   Return                     // return if Remainder == NULL\r
+    sub     eax, dword ptr [Divisor + 0]\r
+    sbb     edx, dword ptr [Divisor + 4]\r
+Correct:\r
+    jecxz   Return\r
+    sub     esi, eax\r
+    sbb     edi, edx                    // edi:esi <- remainder\r
+    mov     [ecx], esi\r
+    mov     [ecx + 4], edi\r
+Return:\r
+    mov     eax, ebx                    // eax <- quotient\r
+    xor     edx, edx\r
+  }\r
+}\r
+\r