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 __umoddi3(du_int a, du_int b);
7214152Sed
8214152Sed// result = remainder of 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
18214152Sed// Stephen Canon, December 2008
19214152Sed
20214152Sed#ifdef __i386__
21214152Sed
22214152Sed.text
23214152Sed.align 4
24214152SedDEFINE_COMPILERRT_FUNCTION(__umoddi3)
25214152Sed
26214152Sed	pushl		%ebx
27214152Sed	movl	 20(%esp),			%ebx	// Find the index i of the leading bit in b.
28214152Sed	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
29214152Sed	jz			9f						// the code to handle that special case [9].
30214152Sed
31214152Sed	/* High word of b is known to be non-zero on this branch */
32214152Sed
33214152Sed	movl	 16(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
34214152Sed
35214152Sed	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
36214152Sed	shrl		%eax					//
37214152Sed	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
38214152Sed	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
39214152Sed	orl			%eax,			%ebx	//
40214152Sed	movl	 12(%esp),			%edx	// Load the high and low words of a, and jump
41214152Sed	movl	  8(%esp),			%eax	// to [2] if the high word is larger than bhi
42214152Sed	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
43214152Sed	jae			2f
44214152Sed
45214152Sed	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
46214152Sed
47214152Sed	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
48214152Sed
49214152Sed	pushl		%edi
50214152Sed	notl		%ecx
51214152Sed	shrl		%eax
52214152Sed	shrl		%cl,			%eax	// q = qs >> (1 + i)
53214152Sed	movl		%eax,			%edi
54214152Sed	mull	 20(%esp)					// q*blo
55214152Sed	movl	 12(%esp),			%ebx
56214152Sed	movl	 16(%esp),			%ecx	// ECX:EBX = a
57214152Sed	subl		%eax,			%ebx
58214152Sed	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
59214152Sed	movl	 24(%esp),			%eax
60214152Sed	imull		%edi,			%eax	// q*bhi
61214152Sed	subl		%eax,			%ecx	// ECX:EBX = a - q*b
62214152Sed
63214152Sed	jnc			1f						// if positive, this is the result.
64214152Sed	addl	 20(%esp),			%ebx	// otherwise
65214152Sed	adcl	 24(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
66214152Sed1:	movl		%ebx,			%eax
67214152Sed	movl		%ecx,			%edx
68214152Sed
69214152Sed	popl		%edi
70214152Sed	popl		%ebx
71214152Sed	retl
72214152Sed
73214152Sed
74214152Sed2:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
75214152Sed
76214152Sed	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
77214152Sed	divl		%ebx					// overflow, and find q and r such that
78214152Sed										//
79214152Sed										//		ahi:alo = (1:q)*bhi + r
80214152Sed										//
81214152Sed										// Note that q is a number in (31-i).(1+i)
82214152Sed										// fix point.
83214152Sed
84214152Sed	pushl		%edi
85214152Sed	notl		%ecx
86214152Sed	shrl		%eax
87214152Sed	orl			$0x80000000,	%eax
88214152Sed	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
89214152Sed	movl		%eax,			%edi
90214152Sed	mull	 20(%esp)					// q*blo
91214152Sed	movl	 12(%esp),			%ebx
92214152Sed	movl	 16(%esp),			%ecx	// ECX:EBX = a
93214152Sed	subl		%eax,			%ebx
94214152Sed	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
95214152Sed	movl	 24(%esp),			%eax
96214152Sed	imull		%edi,			%eax	// q*bhi
97214152Sed	subl		%eax,			%ecx	// ECX:EBX = a - q*b
98214152Sed
99214152Sed	jnc			3f						// if positive, this is the result.
100214152Sed	addl	 20(%esp),			%ebx	// otherwise
101214152Sed	adcl	 24(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
102214152Sed3:	movl		%ebx,			%eax
103214152Sed	movl		%ecx,			%edx
104214152Sed
105214152Sed	popl		%edi
106214152Sed	popl		%ebx
107214152Sed	retl
108214152Sed
109214152Sed
110214152Sed
111214152Sed9:	/* High word of b is zero on this branch */
112214152Sed
113214152Sed	movl	 12(%esp),			%eax	// Find qhi and rhi such that
114214152Sed	movl	 16(%esp),			%ecx	//
115214152Sed	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ��� rhi < b
116214152Sed	divl		%ecx					//
117214152Sed	movl		%eax,			%ebx	//
118214152Sed	movl	  8(%esp),			%eax	// Find rlo such that
119214152Sed	divl		%ecx					//
120214152Sed	movl		%edx,			%eax	//		rhi:alo = qlo*b + rlo  with 0 ��� rlo < b
121214152Sed	popl		%ebx					//
122214152Sed	xorl		%edx,			%edx	// and return 0:rlo
123214152Sed	retl								//
124214152Sed
125214152Sed#endif // __i386__
126