xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/i386/divdi3.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// di_int __divdi3(di_int a, di_int b);
7*0a6a1f1dSLionel Sambuc
8*0a6a1f1dSLionel Sambuc// result = a / b.
9*0a6a1f1dSLionel Sambuc// both inputs and the output are 64-bit signed 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(__divdi3)
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc/* This is currently implemented by wrapping the unsigned divide up in an absolute
26*0a6a1f1dSLionel Sambuc   value, then restoring the correct sign at the end of the computation.  This could
27*0a6a1f1dSLionel Sambuc   certainly be improved upon. */
28*0a6a1f1dSLionel Sambuc
29*0a6a1f1dSLionel Sambuc	pushl		%esi
30*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%edx	// high word of b
31*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%eax	// low word of b
32*0a6a1f1dSLionel Sambuc	movl		%edx,			%ecx
33*0a6a1f1dSLionel Sambuc	sarl		$31,			%ecx	// (b < 0) ? -1 : 0
34*0a6a1f1dSLionel Sambuc	xorl		%ecx,			%eax
35*0a6a1f1dSLionel Sambuc	xorl		%ecx,			%edx	// EDX:EAX = (b < 0) ? not(b) : b
36*0a6a1f1dSLionel Sambuc	subl		%ecx,			%eax
37*0a6a1f1dSLionel Sambuc	sbbl		%ecx,			%edx	// EDX:EAX = abs(b)
38*0a6a1f1dSLionel Sambuc	movl		%edx,		 20(%esp)
39*0a6a1f1dSLionel Sambuc	movl		%eax,		 16(%esp)	// store abs(b) back to stack
40*0a6a1f1dSLionel Sambuc	movl		%ecx,			%esi	// set aside sign of b
41*0a6a1f1dSLionel Sambuc
42*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%edx	// high word of b
43*0a6a1f1dSLionel Sambuc	movl	  8(%esp),			%eax	// low word of b
44*0a6a1f1dSLionel Sambuc	movl		%edx,			%ecx
45*0a6a1f1dSLionel Sambuc	sarl		$31,			%ecx	// (a < 0) ? -1 : 0
46*0a6a1f1dSLionel Sambuc	xorl		%ecx,			%eax
47*0a6a1f1dSLionel Sambuc	xorl		%ecx,			%edx	// EDX:EAX = (a < 0) ? not(a) : a
48*0a6a1f1dSLionel Sambuc	subl		%ecx,			%eax
49*0a6a1f1dSLionel Sambuc	sbbl		%ecx,			%edx	// EDX:EAX = abs(a)
50*0a6a1f1dSLionel Sambuc	movl		%edx,		 12(%esp)
51*0a6a1f1dSLionel Sambuc	movl		%eax,		  8(%esp)	// store abs(a) back to stack
52*0a6a1f1dSLionel Sambuc	xorl		%ecx,			%esi	// sign of result = (sign of a) ^ (sign of b)
53*0a6a1f1dSLionel Sambuc
54*0a6a1f1dSLionel Sambuc	pushl		%ebx
55*0a6a1f1dSLionel Sambuc	movl	 24(%esp),			%ebx	// Find the index i of the leading bit in b.
56*0a6a1f1dSLionel Sambuc	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
57*0a6a1f1dSLionel Sambuc	jz			9f						// the code to handle that special case [9].
58*0a6a1f1dSLionel Sambuc
59*0a6a1f1dSLionel Sambuc	/* High word of b is known to be non-zero on this branch */
60*0a6a1f1dSLionel Sambuc
61*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
64*0a6a1f1dSLionel Sambuc	shrl		%eax					//
65*0a6a1f1dSLionel Sambuc	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
66*0a6a1f1dSLionel Sambuc	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
67*0a6a1f1dSLionel Sambuc	orl			%eax,			%ebx	//
68*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%edx	// Load the high and low words of a, and jump
69*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%eax	// to [1] if the high word is larger than bhi
70*0a6a1f1dSLionel Sambuc	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
71*0a6a1f1dSLionel Sambuc	jae			1f
72*0a6a1f1dSLionel Sambuc
73*0a6a1f1dSLionel Sambuc	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
74*0a6a1f1dSLionel Sambuc
75*0a6a1f1dSLionel Sambuc	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
76*0a6a1f1dSLionel Sambuc
77*0a6a1f1dSLionel Sambuc	pushl		%edi
78*0a6a1f1dSLionel Sambuc	notl		%ecx
79*0a6a1f1dSLionel Sambuc	shrl		%eax
80*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// q = qs >> (1 + i)
81*0a6a1f1dSLionel Sambuc	movl		%eax,			%edi
82*0a6a1f1dSLionel Sambuc	mull	 24(%esp)					// q*blo
83*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%ebx
84*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%ecx	// ECX:EBX = a
85*0a6a1f1dSLionel Sambuc	subl		%eax,			%ebx
86*0a6a1f1dSLionel Sambuc	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
87*0a6a1f1dSLionel Sambuc	movl	 28(%esp),			%eax
88*0a6a1f1dSLionel Sambuc	imull		%edi,			%eax	// q*bhi
89*0a6a1f1dSLionel Sambuc	subl		%eax,			%ecx	// ECX:EBX = a - q*b
90*0a6a1f1dSLionel Sambuc	sbbl		$0,				%edi	// decrement q if remainder is negative
91*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx
92*0a6a1f1dSLionel Sambuc	movl		%edi,			%eax
93*0a6a1f1dSLionel Sambuc
94*0a6a1f1dSLionel Sambuc	addl		%esi,			%eax	// Restore correct sign to result
95*0a6a1f1dSLionel Sambuc	adcl		%esi,			%edx
96*0a6a1f1dSLionel Sambuc	xorl		%esi,			%eax
97*0a6a1f1dSLionel Sambuc	xorl		%esi,			%edx
98*0a6a1f1dSLionel Sambuc	popl		%edi					// Restore callee-save registers
99*0a6a1f1dSLionel Sambuc	popl		%ebx
100*0a6a1f1dSLionel Sambuc	popl		%esi
101*0a6a1f1dSLionel Sambuc	retl								// Return
102*0a6a1f1dSLionel Sambuc
103*0a6a1f1dSLionel Sambuc
104*0a6a1f1dSLionel Sambuc1:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
107*0a6a1f1dSLionel Sambuc	divl		%ebx					// overflow, and find q and r such that
108*0a6a1f1dSLionel Sambuc										//
109*0a6a1f1dSLionel Sambuc										//		ahi:alo = (1:q)*bhi + r
110*0a6a1f1dSLionel Sambuc										//
111*0a6a1f1dSLionel Sambuc										// Note that q is a number in (31-i).(1+i)
112*0a6a1f1dSLionel Sambuc										// fix point.
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc	pushl		%edi
115*0a6a1f1dSLionel Sambuc	notl		%ecx
116*0a6a1f1dSLionel Sambuc	shrl		%eax
117*0a6a1f1dSLionel Sambuc	orl			$0x80000000,	%eax
118*0a6a1f1dSLionel Sambuc	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
119*0a6a1f1dSLionel Sambuc	movl		%eax,			%edi
120*0a6a1f1dSLionel Sambuc	mull	 24(%esp)					// q*blo
121*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%ebx
122*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%ecx	// ECX:EBX = a
123*0a6a1f1dSLionel Sambuc	subl		%eax,			%ebx
124*0a6a1f1dSLionel Sambuc	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
125*0a6a1f1dSLionel Sambuc	movl	 28(%esp),			%eax
126*0a6a1f1dSLionel Sambuc	imull		%edi,			%eax	// q*bhi
127*0a6a1f1dSLionel Sambuc	subl		%eax,			%ecx	// ECX:EBX = a - q*b
128*0a6a1f1dSLionel Sambuc	sbbl		$0,				%edi	// decrement q if remainder is negative
129*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx
130*0a6a1f1dSLionel Sambuc	movl		%edi,			%eax
131*0a6a1f1dSLionel Sambuc
132*0a6a1f1dSLionel Sambuc	addl		%esi,			%eax	// Restore correct sign to result
133*0a6a1f1dSLionel Sambuc	adcl		%esi,			%edx
134*0a6a1f1dSLionel Sambuc	xorl		%esi,			%eax
135*0a6a1f1dSLionel Sambuc	xorl		%esi,			%edx
136*0a6a1f1dSLionel Sambuc	popl		%edi					// Restore callee-save registers
137*0a6a1f1dSLionel Sambuc	popl		%ebx
138*0a6a1f1dSLionel Sambuc	popl		%esi
139*0a6a1f1dSLionel Sambuc	retl								// Return
140*0a6a1f1dSLionel Sambuc
141*0a6a1f1dSLionel Sambuc
142*0a6a1f1dSLionel Sambuc9:	/* High word of b is zero on this branch */
143*0a6a1f1dSLionel Sambuc
144*0a6a1f1dSLionel Sambuc	movl	 16(%esp),			%eax	// Find qhi and rhi such that
145*0a6a1f1dSLionel Sambuc	movl	 20(%esp),			%ecx	//
146*0a6a1f1dSLionel Sambuc	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ≤ rhi < b
147*0a6a1f1dSLionel Sambuc	divl		%ecx					//
148*0a6a1f1dSLionel Sambuc	movl		%eax,			%ebx	//
149*0a6a1f1dSLionel Sambuc	movl	 12(%esp),			%eax	// Find qlo such that
150*0a6a1f1dSLionel Sambuc	divl		%ecx					//
151*0a6a1f1dSLionel Sambuc	movl		%ebx,			%edx	//		rhi:alo = qlo*b + rlo  with 0 ≤ rlo < b
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc	addl		%esi,			%eax	// Restore correct sign to result
154*0a6a1f1dSLionel Sambuc	adcl		%esi,			%edx
155*0a6a1f1dSLionel Sambuc	xorl		%esi,			%eax
156*0a6a1f1dSLionel Sambuc	xorl		%esi,			%edx
157*0a6a1f1dSLionel Sambuc	popl		%ebx					// Restore callee-save registers
158*0a6a1f1dSLionel Sambuc	popl		%esi
159*0a6a1f1dSLionel Sambuc	retl								// Return
160*0a6a1f1dSLionel SambucEND_COMPILERRT_FUNCTION(__divdi3)
161*0a6a1f1dSLionel Sambuc
162*0a6a1f1dSLionel Sambuc#endif // __i386__
163