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-exception
5 #include "../assembly.h"
7 // di_int __divdi3(di_int a, di_int b);
10 // both inputs and the output are 64-bit signed 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.
14 // This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
15 // on x86_64. The performance goal is ~40 cycles per divide, which is faster than
16 // currently possible via simulation of integer divides on the x87 unit.
18 // Stephen Canon, December 2008
24 DEFINE_COMPILERRT_FUNCTION(__divdi3)
26 // This is currently implemented by wrapping the unsigned divide up in an absolute
27 // value, then restoring the correct sign at the end of the computation. This could
28 // certainly be improved upon.
31 movl 20(%esp), %edx // high word of b
32 movl 16(%esp), %eax // low word of b
34 sarl $31, %ecx // (b < 0) ? -1 : 0
36 xorl %ecx, %edx // EDX:EAX = (b < 0) ? not(b) : b
38 sbbl %ecx, %edx // EDX:EAX = abs(b)
40 movl %eax, 16(%esp) // store abs(b) back to stack
41 movl %ecx, %esi // set aside sign of b
43 movl 12(%esp), %edx // high word of b
44 movl 8(%esp), %eax // low word of b
46 sarl $31, %ecx // (a < 0) ? -1 : 0
48 xorl %ecx, %edx // EDX:EAX = (a < 0) ? not(a) : a
50 sbbl %ecx, %edx // EDX:EAX = abs(a)
52 movl %eax, 8(%esp) // store abs(a) back to stack
53 xorl %ecx, %esi // sign of result = (sign of a) ^ (sign of b)
56 movl 24(%esp), %ebx // Find the index i of the leading bit in b.
57 bsrl %ebx, %ecx // If the high word of b is zero, jump to
58 jz 9f // the code to handle that special case [9].
60 // High word of b is known to be non-zero on this branch
62 movl 20(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b
64 shrl %cl, %eax // Practically, this means that bhi is given by:
66 notl %ecx // bhi = (high word of b) << (31 - i) |
67 shll %cl, %ebx // (low word of b) >> (1 + i)
69 movl 16(%esp), %edx // Load the high and low words of a, and jump
70 movl 12(%esp), %eax // to [1] if the high word is larger than bhi
71 cmpl %ebx, %edx // to avoid overflowing the upcoming divide.
74 // High word of a is greater than or equal to (b >> (1 + i)) on this branch
76 divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
81 shrl %cl, %eax // q = qs >> (1 + i)
83 mull 24(%esp) // q*blo
85 movl 20(%esp), %ecx // ECX:EBX = a
87 sbbl %edx, %ecx // ECX:EBX = a - q*blo
89 imull %edi, %eax // q*bhi
90 subl %eax, %ecx // ECX:EBX = a - q*b
91 sbbl $0, %edi // decrement q if remainder is negative
95 addl %esi, %eax // Restore correct sign to result
99 popl %edi // Restore callee-save registers
105 1: // High word of a is greater than or equal to (b >> (1 + i)) on this branch
107 subl %ebx, %edx // subtract bhi from ahi so that divide will not
108 divl %ebx // overflow, and find q and r such that
110 // ahi:alo = (1:q)*bhi + r
112 // Note that q is a number in (31-i).(1+i)
118 orl $0x80000000, %eax
119 shrl %cl, %eax // q = (1:qs) >> (1 + i)
121 mull 24(%esp) // q*blo
123 movl 20(%esp), %ecx // ECX:EBX = a
125 sbbl %edx, %ecx // ECX:EBX = a - q*blo
127 imull %edi, %eax // q*bhi
128 subl %eax, %ecx // ECX:EBX = a - q*b
129 sbbl $0, %edi // decrement q if remainder is negative
133 addl %esi, %eax // Restore correct sign to result
137 popl %edi // Restore callee-save registers
143 9: // High word of b is zero on this branch
145 movl 16(%esp), %eax // Find qhi and rhi such that
146 movl 20(%esp), %ecx //
147 xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b
150 movl 12(%esp), %eax // Find qlo such that
152 movl %ebx, %edx // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b
154 addl %esi, %eax // Restore correct sign to result
158 popl %ebx // Restore callee-save registers
161 END_COMPILERRT_FUNCTION(__divdi3)
165 NO_EXEC_STACK_DIRECTIVE