120 lines · plain
1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.2// See https://llvm.org/LICENSE.txt for license information.3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception4 5#include "../assembly.h"6 7// du_int __udivdi3(du_int a, du_int b);8 9// result = a / b.10// both inputs and the output are 64-bit unsigned integers.11// This will do whatever the underlying hardware is set to do on division by zero.12// No other exceptions are generated, as the divide cannot overflow.13//14// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware15// on x86_64. The performance goal is ~40 cycles per divide, which is faster than16// currently possible via simulation of integer divides on the x87 unit.17//18// Stephen Canon, December 200819 20#ifdef __i386__21 22.text23.balign 424DEFINE_COMPILERRT_FUNCTION(__udivdi3)25 26 pushl %ebx27 movl 20(%esp), %ebx // Find the index i of the leading bit in b.28 bsrl %ebx, %ecx // If the high word of b is zero, jump to29 jz 9f // the code to handle that special case [9].30 31 // High word of b is known to be non-zero on this branch32 33 movl 16(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b34 35 shrl %cl, %eax // Practically, this means that bhi is given by:36 shrl %eax //37 notl %ecx // bhi = (high word of b) << (31 - i) |38 shll %cl, %ebx // (low word of b) >> (1 + i)39 orl %eax, %ebx //40 movl 12(%esp), %edx // Load the high and low words of a, and jump41 movl 8(%esp), %eax // to [1] if the high word is larger than bhi42 cmpl %ebx, %edx // to avoid overflowing the upcoming divide.43 jae 1f44 45 // High word of a is greater than or equal to (b >> (1 + i)) on this branch46 47 divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r48 49 pushl %edi50 notl %ecx51 shrl %eax52 shrl %cl, %eax // q = qs >> (1 + i)53 movl %eax, %edi54 mull 20(%esp) // q*blo55 movl 12(%esp), %ebx56 movl 16(%esp), %ecx // ECX:EBX = a57 subl %eax, %ebx58 sbbl %edx, %ecx // ECX:EBX = a - q*blo59 movl 24(%esp), %eax60 imull %edi, %eax // q*bhi61 subl %eax, %ecx // ECX:EBX = a - q*b62 sbbl $0, %edi // decrement q if remainder is negative63 xorl %edx, %edx64 movl %edi, %eax65 popl %edi66 popl %ebx67 retl68 69 701: // High word of a is greater than or equal to (b >> (1 + i)) on this branch71 72 subl %ebx, %edx // subtract bhi from ahi so that divide will not73 divl %ebx // overflow, and find q and r such that74 //75 // ahi:alo = (1:q)*bhi + r76 //77 // Note that q is a number in (31-i).(1+i)78 // fix point.79 80 pushl %edi81 notl %ecx82 shrl %eax83 orl $0x80000000, %eax84 shrl %cl, %eax // q = (1:qs) >> (1 + i)85 movl %eax, %edi86 mull 20(%esp) // q*blo87 movl 12(%esp), %ebx88 movl 16(%esp), %ecx // ECX:EBX = a89 subl %eax, %ebx90 sbbl %edx, %ecx // ECX:EBX = a - q*blo91 movl 24(%esp), %eax92 imull %edi, %eax // q*bhi93 subl %eax, %ecx // ECX:EBX = a - q*b94 sbbl $0, %edi // decrement q if remainder is negative95 xorl %edx, %edx96 movl %edi, %eax97 popl %edi98 popl %ebx99 retl100 101 1029: // High word of b is zero on this branch103 104 movl 12(%esp), %eax // Find qhi and rhi such that105 movl 16(%esp), %ecx //106 xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b107 divl %ecx //108 movl %eax, %ebx //109 movl 8(%esp), %eax // Find qlo such that110 divl %ecx //111 movl %ebx, %edx // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b112 popl %ebx //113 retl // and return qhi:qlo114END_COMPILERRT_FUNCTION(__udivdi3)115 116#endif // __i386__117 118NO_EXEC_STACK_DIRECTIVE119 120