1222656Sed// This file is dual licensed under the MIT and the University of Illinois Open
2222656Sed// Source Licenses. See LICENSE.TXT for details.
3214152Sed
4214152Sed#include "../assembly.h"
5214152Sed
6214152Sed// du_int __udivdi3(du_int a, du_int b);
7214152Sed
8214152Sed// result = a / b.
9214152Sed// both inputs and the output are 64-bit unsigned integers.
10214152Sed// This will do whatever the underlying hardware is set to do on division by zero.
11214152Sed// No other exceptions are generated, as the divide cannot overflow.
12214152Sed//
13214152Sed// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
14214152Sed// on x86_64.  The performance goal is ~40 cycles per divide, which is faster than
15214152Sed// currently possible via simulation of integer divides on the x87 unit.
16214152Sed//
17214152Sed// Stephen Canon, December 2008
18214152Sed
19214152Sed#ifdef __i386__
20214152Sed
21214152Sed.text
22214152Sed.align 4
23214152SedDEFINE_COMPILERRT_FUNCTION(__udivdi3)
24214152Sed
25214152Sed	pushl		%ebx
26214152Sed	movl	 20(%esp),			%ebx	// Find the index i of the leading bit in b.
27214152Sed	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
28214152Sed	jz			9f						// the code to handle that special case [9].
29214152Sed
30214152Sed	/* High word of b is known to be non-zero on this branch */
31214152Sed
32214152Sed	movl	 16(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
33214152Sed
34214152Sed	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
35214152Sed	shrl		%eax					//
36214152Sed	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
37214152Sed	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
38214152Sed	orl			%eax,			%ebx	//
39214152Sed	movl	 12(%esp),			%edx	// Load the high and low words of a, and jump
40214152Sed	movl	  8(%esp),			%eax	// to [1] if the high word is larger than bhi
41214152Sed	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
42214152Sed	jae			1f
43214152Sed
44214152Sed	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
45214152Sed
46214152Sed	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
47214152Sed
48214152Sed	pushl		%edi
49214152Sed	notl		%ecx
50214152Sed	shrl		%eax
51214152Sed	shrl		%cl,			%eax	// q = qs >> (1 + i)
52214152Sed	movl		%eax,			%edi
53214152Sed	mull	 20(%esp)					// q*blo
54214152Sed	movl	 12(%esp),			%ebx
55214152Sed	movl	 16(%esp),			%ecx	// ECX:EBX = a
56214152Sed	subl		%eax,			%ebx
57214152Sed	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
58214152Sed	movl	 24(%esp),			%eax
59214152Sed	imull		%edi,			%eax	// q*bhi
60214152Sed	subl		%eax,			%ecx	// ECX:EBX = a - q*b
61214152Sed	sbbl		$0,				%edi	// decrement q if remainder is negative
62214152Sed	xorl		%edx,			%edx
63214152Sed	movl		%edi,			%eax
64214152Sed	popl		%edi
65214152Sed	popl		%ebx
66214152Sed	retl
67214152Sed
68214152Sed
69214152Sed1:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
70214152Sed
71214152Sed	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
72214152Sed	divl		%ebx					// overflow, and find q and r such that
73214152Sed										//
74214152Sed										//		ahi:alo = (1:q)*bhi + r
75214152Sed										//
76214152Sed										// Note that q is a number in (31-i).(1+i)
77214152Sed										// fix point.
78214152Sed
79214152Sed	pushl		%edi
80214152Sed	notl		%ecx
81214152Sed	shrl		%eax
82214152Sed	orl			$0x80000000,	%eax
83214152Sed	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
84214152Sed	movl		%eax,			%edi
85214152Sed	mull	 20(%esp)					// q*blo
86214152Sed	movl	 12(%esp),			%ebx
87214152Sed	movl	 16(%esp),			%ecx	// ECX:EBX = a
88214152Sed	subl		%eax,			%ebx
89214152Sed	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
90214152Sed	movl	 24(%esp),			%eax
91214152Sed	imull		%edi,			%eax	// q*bhi
92214152Sed	subl		%eax,			%ecx	// ECX:EBX = a - q*b
93214152Sed	sbbl		$0,				%edi	// decrement q if remainder is negative
94214152Sed	xorl		%edx,			%edx
95214152Sed	movl		%edi,			%eax
96214152Sed	popl		%edi
97214152Sed	popl		%ebx
98214152Sed	retl
99214152Sed
100214152Sed
101214152Sed9:	/* High word of b is zero on this branch */
102214152Sed
103214152Sed	movl	 12(%esp),			%eax	// Find qhi and rhi such that
104214152Sed	movl	 16(%esp),			%ecx	//
105214152Sed	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ��� rhi < b
106214152Sed	divl		%ecx					//
107214152Sed	movl		%eax,			%ebx	//
108214152Sed	movl	  8(%esp),			%eax	// Find qlo such that
109214152Sed	divl		%ecx					//
110214152Sed	movl		%ebx,			%edx	//		rhi:alo = qlo*b + rlo  with 0 ��� rlo < b
111214152Sed	popl		%ebx					//
112214152Sed	retl								// and return qhi:qlo
113214152Sed
114214152Sed#endif // __i386__
115