1353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2353358Sdim// See https://llvm.org/LICENSE.txt for license information.
3353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4276789Sdim
5276789Sdim#include "../assembly.h"
6276789Sdim
7276789Sdim// di_int __moddi3(di_int a, di_int b);
8276789Sdim
9276789Sdim// result = remainder of a / b.
10276789Sdim// both inputs and the output are 64-bit signed integers.
11276789Sdim// This will do whatever the underlying hardware is set to do on division by zero.
12276789Sdim// No other exceptions are generated, as the divide cannot overflow.
13276789Sdim//
14276789Sdim// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
15276789Sdim// on x86_64.  The performance goal is ~40 cycles per divide, which is faster than
16276789Sdim// currently possible via simulation of integer divides on the x87 unit.
17276789Sdim//
18276789Sdim
19276789Sdim// Stephen Canon, December 2008
20276789Sdim
21276789Sdim#ifdef __i386__
22276789Sdim
23276789Sdim.text
24276789Sdim.balign 4
25276789SdimDEFINE_COMPILERRT_FUNCTION(__moddi3)
26276789Sdim
27353358Sdim// This is currently implemented by wrapping the unsigned modulus up in an absolute
28353358Sdim// value.  This could certainly be improved upon.
29276789Sdim
30276789Sdim	pushl		%esi
31276789Sdim	movl	 20(%esp),			%edx	// high word of b
32276789Sdim	movl	 16(%esp),			%eax	// low word of b
33276789Sdim	movl		%edx,			%ecx
34276789Sdim	sarl		$31,			%ecx	// (b < 0) ? -1 : 0
35276789Sdim	xorl		%ecx,			%eax
36276789Sdim	xorl		%ecx,			%edx	// EDX:EAX = (b < 0) ? not(b) : b
37276789Sdim	subl		%ecx,			%eax
38276789Sdim	sbbl		%ecx,			%edx	// EDX:EAX = abs(b)
39276789Sdim	movl		%edx,		 20(%esp)
40276789Sdim	movl		%eax,		 16(%esp)	// store abs(b) back to stack
41353358Sdim
42276789Sdim	movl	 12(%esp),			%edx	// high word of b
43276789Sdim	movl	  8(%esp),			%eax	// low word of b
44276789Sdim	movl		%edx,			%ecx
45276789Sdim	sarl		$31,			%ecx	// (a < 0) ? -1 : 0
46276789Sdim	xorl		%ecx,			%eax
47276789Sdim	xorl		%ecx,			%edx	// EDX:EAX = (a < 0) ? not(a) : a
48276789Sdim	subl		%ecx,			%eax
49276789Sdim	sbbl		%ecx,			%edx	// EDX:EAX = abs(a)
50276789Sdim	movl		%edx,		 12(%esp)
51276789Sdim	movl		%eax,		  8(%esp)	// store abs(a) back to stack
52276789Sdim	movl		%ecx,			%esi	// set aside sign of a
53276789Sdim
54276789Sdim	pushl		%ebx
55276789Sdim	movl	 24(%esp),			%ebx	// Find the index i of the leading bit in b.
56276789Sdim	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
57276789Sdim	jz			9f						// the code to handle that special case [9].
58353358Sdim
59353358Sdim	// High word of b is known to be non-zero on this branch
60353358Sdim
61276789Sdim	movl	 20(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
62353358Sdim
63276789Sdim	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
64276789Sdim	shrl		%eax					//
65276789Sdim	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
66276789Sdim	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
67276789Sdim	orl			%eax,			%ebx	//
68276789Sdim	movl	 16(%esp),			%edx	// Load the high and low words of a, and jump
69276789Sdim	movl	 12(%esp),			%eax	// to [2] if the high word is larger than bhi
70276789Sdim	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
71353358Sdim	jae			2f
72353358Sdim
73353358Sdim	// High word of a is greater than or equal to (b >> (1 + i)) on this branch
74353358Sdim
75276789Sdim	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
76276789Sdim
77276789Sdim	pushl		%edi
78276789Sdim	notl		%ecx
79276789Sdim	shrl		%eax
80276789Sdim	shrl		%cl,			%eax	// q = qs >> (1 + i)
81276789Sdim	movl		%eax,			%edi
82276789Sdim	mull	 24(%esp)					// q*blo
83276789Sdim	movl	 16(%esp),			%ebx
84276789Sdim	movl	 20(%esp),			%ecx	// ECX:EBX = a
85276789Sdim	subl		%eax,			%ebx
86276789Sdim	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
87276789Sdim	movl	 28(%esp),			%eax
88276789Sdim	imull		%edi,			%eax	// q*bhi
89276789Sdim	subl		%eax,			%ecx	// ECX:EBX = a - q*b
90353358Sdim
91276789Sdim	jnc			1f						// if positive, this is the result.
92276789Sdim	addl	 24(%esp),			%ebx	// otherwise
93276789Sdim	adcl	 28(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
94276789Sdim1:	movl		%ebx,			%eax
95276789Sdim	movl		%ecx,			%edx
96353358Sdim
97276789Sdim	addl		%esi,			%eax	// Restore correct sign to result
98276789Sdim	adcl		%esi,			%edx
99276789Sdim	xorl		%esi,			%eax
100276789Sdim	xorl		%esi,			%edx
101276789Sdim	popl		%edi					// Restore callee-save registers
102276789Sdim	popl		%ebx
103276789Sdim	popl		%esi
104276789Sdim	retl								// Return
105276789Sdim
106353358Sdim2:	// High word of a is greater than or equal to (b >> (1 + i)) on this branch
107353358Sdim
108276789Sdim	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
109276789Sdim	divl		%ebx					// overflow, and find q and r such that
110276789Sdim										//
111276789Sdim										//		ahi:alo = (1:q)*bhi + r
112276789Sdim										//
113276789Sdim										// Note that q is a number in (31-i).(1+i)
114276789Sdim										// fix point.
115276789Sdim
116276789Sdim	pushl		%edi
117276789Sdim	notl		%ecx
118276789Sdim	shrl		%eax
119276789Sdim	orl			$0x80000000,	%eax
120276789Sdim	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
121276789Sdim	movl		%eax,			%edi
122276789Sdim	mull	 24(%esp)					// q*blo
123276789Sdim	movl	 16(%esp),			%ebx
124276789Sdim	movl	 20(%esp),			%ecx	// ECX:EBX = a
125276789Sdim	subl		%eax,			%ebx
126276789Sdim	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
127276789Sdim	movl	 28(%esp),			%eax
128276789Sdim	imull		%edi,			%eax	// q*bhi
129276789Sdim	subl		%eax,			%ecx	// ECX:EBX = a - q*b
130276789Sdim
131276789Sdim	jnc			3f						// if positive, this is the result.
132276789Sdim	addl	 24(%esp),			%ebx	// otherwise
133276789Sdim	adcl	 28(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
134276789Sdim3:	movl		%ebx,			%eax
135276789Sdim	movl		%ecx,			%edx
136353358Sdim
137276789Sdim	addl		%esi,			%eax	// Restore correct sign to result
138276789Sdim	adcl		%esi,			%edx
139276789Sdim	xorl		%esi,			%eax
140276789Sdim	xorl		%esi,			%edx
141276789Sdim	popl		%edi					// Restore callee-save registers
142276789Sdim	popl		%ebx
143276789Sdim	popl		%esi
144276789Sdim	retl								// Return
145276789Sdim
146353358Sdim9:	// High word of b is zero on this branch
147353358Sdim
148276789Sdim	movl	 16(%esp),			%eax	// Find qhi and rhi such that
149276789Sdim	movl	 20(%esp),			%ecx	//
150276789Sdim	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ��� rhi < b
151276789Sdim	divl		%ecx					//
152276789Sdim	movl		%eax,			%ebx	//
153276789Sdim	movl	 12(%esp),			%eax	// Find rlo such that
154276789Sdim	divl		%ecx					//
155276789Sdim	movl		%edx,			%eax	//		rhi:alo = qlo*b + rlo  with 0 ��� rlo < b
156276789Sdim	popl		%ebx					//
157276789Sdim	xorl		%edx,			%edx	// and return 0:rlo
158276789Sdim
159276789Sdim	addl		%esi,			%eax	// Restore correct sign to result
160276789Sdim	adcl		%esi,			%edx
161276789Sdim	xorl		%esi,			%eax
162276789Sdim	xorl		%esi,			%edx
163276789Sdim	popl		%esi
164276789Sdim	retl								// Return
165276789SdimEND_COMPILERRT_FUNCTION(__moddi3)
166276789Sdim
167276789Sdim#endif // __i386__
168309124Sdim
169309124SdimNO_EXEC_STACK_DIRECTIVE
170309124Sdim
171