xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/i386/udivdi3.S (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open
2*0a6a1f1dSLionel Sambuc// Source Licenses. See LICENSE.TXT for details.
3*0a6a1f1dSLionel Sambuc
4*0a6a1f1dSLionel Sambuc#include "../assembly.h"
5*0a6a1f1dSLionel Sambuc
6*0a6a1f1dSLionel Sambuc// du_int __udivdi3(du_int a, du_int b);
7*0a6a1f1dSLionel Sambuc
8*0a6a1f1dSLionel Sambuc// result = a / b.
9*0a6a1f1dSLionel Sambuc// both inputs and the output are 64-bit unsigned integers.
10*0a6a1f1dSLionel Sambuc// This will do whatever the underlying hardware is set to do on division by zero.
11*0a6a1f1dSLionel Sambuc// No other exceptions are generated, as the divide cannot overflow.
12*0a6a1f1dSLionel Sambuc//
13*0a6a1f1dSLionel Sambuc// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
14*0a6a1f1dSLionel Sambuc// on x86_64.  The performance goal is ~40 cycles per divide, which is faster than
15*0a6a1f1dSLionel Sambuc// currently possible via simulation of integer divides on the x87 unit.
16*0a6a1f1dSLionel Sambuc//
17*0a6a1f1dSLionel Sambuc// Stephen Canon, December 2008
18*0a6a1f1dSLionel Sambuc
19*0a6a1f1dSLionel Sambuc#ifdef __i386__
20*0a6a1f1dSLionel Sambuc
21*0a6a1f1dSLionel Sambuc.text
22*0a6a1f1dSLionel Sambuc.balign 4
23*0a6a1f1dSLionel SambucDEFINE_COMPILERRT_FUNCTION(__udivdi3)
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc	pushl		%ebx
26*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%ebx	// Find the index i of the leading bit in b.
27*0a6a1f1dSLionel Sambuc	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
28*0a6a1f1dSLionel Sambuc	jz			9f						// the code to handle that special case [9].
29*0a6a1f1dSLionel Sambuc
30*0a6a1f1dSLionel Sambuc	/* High word of b is known to be non-zero on this branch */
31*0a6a1f1dSLionel Sambuc
32*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
33*0a6a1f1dSLionel Sambuc
34*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
35*0a6a1f1dSLionel Sambuc	shrl		%eax					//
36*0a6a1f1dSLionel Sambuc	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
37*0a6a1f1dSLionel Sambuc	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
38*0a6a1f1dSLionel Sambuc	orl			%eax,			%ebx	//
39*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%edx	// Load the high and low words of a, and jump
40*0a6a1f1dSLionel Sambuc	movl	  8(%esp),			%eax	// to [1] if the high word is larger than bhi
41*0a6a1f1dSLionel Sambuc	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
42*0a6a1f1dSLionel Sambuc	jae			1f
43*0a6a1f1dSLionel Sambuc
44*0a6a1f1dSLionel Sambuc	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc	pushl		%edi
49*0a6a1f1dSLionel Sambuc	notl		%ecx
50*0a6a1f1dSLionel Sambuc	shrl		%eax
51*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// q = qs >> (1 + i)
52*0a6a1f1dSLionel Sambuc	movl		%eax,			%edi
53*0a6a1f1dSLionel Sambuc	mull	 20(%esp)					// q*blo
54*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%ebx
55*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%ecx	// ECX:EBX = a
56*0a6a1f1dSLionel Sambuc	subl		%eax,			%ebx
57*0a6a1f1dSLionel Sambuc	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
58*0a6a1f1dSLionel Sambuc	movl	 24(%esp),			%eax
59*0a6a1f1dSLionel Sambuc	imull		%edi,			%eax	// q*bhi
60*0a6a1f1dSLionel Sambuc	subl		%eax,			%ecx	// ECX:EBX = a - q*b
61*0a6a1f1dSLionel Sambuc	sbbl		$0,				%edi	// decrement q if remainder is negative
62*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx
63*0a6a1f1dSLionel Sambuc	movl		%edi,			%eax
64*0a6a1f1dSLionel Sambuc	popl		%edi
65*0a6a1f1dSLionel Sambuc	popl		%ebx
66*0a6a1f1dSLionel Sambuc	retl
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc1:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
70*0a6a1f1dSLionel Sambuc
71*0a6a1f1dSLionel Sambuc	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
72*0a6a1f1dSLionel Sambuc	divl		%ebx					// overflow, and find q and r such that
73*0a6a1f1dSLionel Sambuc										//
74*0a6a1f1dSLionel Sambuc										//		ahi:alo = (1:q)*bhi + r
75*0a6a1f1dSLionel Sambuc										//
76*0a6a1f1dSLionel Sambuc										// Note that q is a number in (31-i).(1+i)
77*0a6a1f1dSLionel Sambuc										// fix point.
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc	pushl		%edi
80*0a6a1f1dSLionel Sambuc	notl		%ecx
81*0a6a1f1dSLionel Sambuc	shrl		%eax
82*0a6a1f1dSLionel Sambuc	orl			$0x80000000,	%eax
83*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
84*0a6a1f1dSLionel Sambuc	movl		%eax,			%edi
85*0a6a1f1dSLionel Sambuc	mull	 20(%esp)					// q*blo
86*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%ebx
87*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%ecx	// ECX:EBX = a
88*0a6a1f1dSLionel Sambuc	subl		%eax,			%ebx
89*0a6a1f1dSLionel Sambuc	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
90*0a6a1f1dSLionel Sambuc	movl	 24(%esp),			%eax
91*0a6a1f1dSLionel Sambuc	imull		%edi,			%eax	// q*bhi
92*0a6a1f1dSLionel Sambuc	subl		%eax,			%ecx	// ECX:EBX = a - q*b
93*0a6a1f1dSLionel Sambuc	sbbl		$0,				%edi	// decrement q if remainder is negative
94*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx
95*0a6a1f1dSLionel Sambuc	movl		%edi,			%eax
96*0a6a1f1dSLionel Sambuc	popl		%edi
97*0a6a1f1dSLionel Sambuc	popl		%ebx
98*0a6a1f1dSLionel Sambuc	retl
99*0a6a1f1dSLionel Sambuc
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc9:	/* High word of b is zero on this branch */
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%eax	// Find qhi and rhi such that
104*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%ecx	//
105*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ≤ rhi < b
106*0a6a1f1dSLionel Sambuc	divl		%ecx					//
107*0a6a1f1dSLionel Sambuc	movl		%eax,			%ebx	//
108*0a6a1f1dSLionel Sambuc	movl	  8(%esp),			%eax	// Find qlo such that
109*0a6a1f1dSLionel Sambuc	divl		%ecx					//
110*0a6a1f1dSLionel Sambuc	movl		%ebx,			%edx	//		rhi:alo = qlo*b + rlo  with 0 ≤ rlo < b
111*0a6a1f1dSLionel Sambuc	popl		%ebx					//
112*0a6a1f1dSLionel Sambuc	retl								// and return qhi:qlo
113*0a6a1f1dSLionel SambucEND_COMPILERRT_FUNCTION(__udivdi3)
114*0a6a1f1dSLionel Sambuc
115*0a6a1f1dSLionel Sambuc#endif // __i386__
116