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