xref: /netbsd-src/sys/external/bsd/compiler_rt/dist/lib/builtins/i386/udivdi3.S (revision 61f2f2562dcc3e4eb50e7ec5dab0dfa1f093861a)
1156cd587Sjoerg// This file is dual licensed under the MIT and the University of Illinois Open
2156cd587Sjoerg// Source Licenses. See LICENSE.TXT for details.
3156cd587Sjoerg
4156cd587Sjoerg#include "../assembly.h"
5156cd587Sjoerg
6156cd587Sjoerg// du_int __udivdi3(du_int a, du_int b);
7156cd587Sjoerg
8156cd587Sjoerg// result = a / b.
9156cd587Sjoerg// both inputs and the output are 64-bit unsigned integers.
10156cd587Sjoerg// This will do whatever the underlying hardware is set to do on division by zero.
11156cd587Sjoerg// No other exceptions are generated, as the divide cannot overflow.
12156cd587Sjoerg//
13156cd587Sjoerg// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware
14156cd587Sjoerg// on x86_64.  The performance goal is ~40 cycles per divide, which is faster than
15156cd587Sjoerg// currently possible via simulation of integer divides on the x87 unit.
16156cd587Sjoerg//
17156cd587Sjoerg// Stephen Canon, December 2008
18156cd587Sjoerg
19156cd587Sjoerg#ifdef __i386__
20156cd587Sjoerg
21156cd587Sjoerg.text
22*61f2f256Sjoerg.balign 4
23156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__udivdi3)
24156cd587Sjoerg
25156cd587Sjoerg	pushl		%ebx
26156cd587Sjoerg	movl	 20(%esp),			%ebx	// Find the index i of the leading bit in b.
27156cd587Sjoerg	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
28156cd587Sjoerg	jz			9f						// the code to handle that special case [9].
29156cd587Sjoerg
30156cd587Sjoerg	/* High word of b is known to be non-zero on this branch */
31156cd587Sjoerg
32156cd587Sjoerg	movl	 16(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
33156cd587Sjoerg
34156cd587Sjoerg	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
35156cd587Sjoerg	shrl		%eax					//
36156cd587Sjoerg	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
37156cd587Sjoerg	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
38156cd587Sjoerg	orl			%eax,			%ebx	//
39156cd587Sjoerg	movl	 12(%esp),			%edx	// Load the high and low words of a, and jump
40156cd587Sjoerg	movl	  8(%esp),			%eax	// to [1] if the high word is larger than bhi
41156cd587Sjoerg	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
42156cd587Sjoerg	jae			1f
43156cd587Sjoerg
44156cd587Sjoerg	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
45156cd587Sjoerg
46156cd587Sjoerg	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
47156cd587Sjoerg
48156cd587Sjoerg	pushl		%edi
49156cd587Sjoerg	notl		%ecx
50156cd587Sjoerg	shrl		%eax
51156cd587Sjoerg	shrl		%cl,			%eax	// q = qs >> (1 + i)
52156cd587Sjoerg	movl		%eax,			%edi
53156cd587Sjoerg	mull	 20(%esp)					// q*blo
54156cd587Sjoerg	movl	 12(%esp),			%ebx
55156cd587Sjoerg	movl	 16(%esp),			%ecx	// ECX:EBX = a
56156cd587Sjoerg	subl		%eax,			%ebx
57156cd587Sjoerg	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
58156cd587Sjoerg	movl	 24(%esp),			%eax
59156cd587Sjoerg	imull		%edi,			%eax	// q*bhi
60156cd587Sjoerg	subl		%eax,			%ecx	// ECX:EBX = a - q*b
61156cd587Sjoerg	sbbl		$0,				%edi	// decrement q if remainder is negative
62156cd587Sjoerg	xorl		%edx,			%edx
63156cd587Sjoerg	movl		%edi,			%eax
64156cd587Sjoerg	popl		%edi
65156cd587Sjoerg	popl		%ebx
66156cd587Sjoerg	retl
67156cd587Sjoerg
68156cd587Sjoerg
69156cd587Sjoerg1:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
70156cd587Sjoerg
71156cd587Sjoerg	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
72156cd587Sjoerg	divl		%ebx					// overflow, and find q and r such that
73156cd587Sjoerg										//
74156cd587Sjoerg										//		ahi:alo = (1:q)*bhi + r
75156cd587Sjoerg										//
76156cd587Sjoerg										// Note that q is a number in (31-i).(1+i)
77156cd587Sjoerg										// fix point.
78156cd587Sjoerg
79156cd587Sjoerg	pushl		%edi
80156cd587Sjoerg	notl		%ecx
81156cd587Sjoerg	shrl		%eax
82156cd587Sjoerg	orl			$0x80000000,	%eax
83156cd587Sjoerg	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
84156cd587Sjoerg	movl		%eax,			%edi
85156cd587Sjoerg	mull	 20(%esp)					// q*blo
86156cd587Sjoerg	movl	 12(%esp),			%ebx
87156cd587Sjoerg	movl	 16(%esp),			%ecx	// ECX:EBX = a
88156cd587Sjoerg	subl		%eax,			%ebx
89156cd587Sjoerg	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
90156cd587Sjoerg	movl	 24(%esp),			%eax
91156cd587Sjoerg	imull		%edi,			%eax	// q*bhi
92156cd587Sjoerg	subl		%eax,			%ecx	// ECX:EBX = a - q*b
93156cd587Sjoerg	sbbl		$0,				%edi	// decrement q if remainder is negative
94156cd587Sjoerg	xorl		%edx,			%edx
95156cd587Sjoerg	movl		%edi,			%eax
96156cd587Sjoerg	popl		%edi
97156cd587Sjoerg	popl		%ebx
98156cd587Sjoerg	retl
99156cd587Sjoerg
100156cd587Sjoerg
101156cd587Sjoerg9:	/* High word of b is zero on this branch */
102156cd587Sjoerg
103156cd587Sjoerg	movl	 12(%esp),			%eax	// Find qhi and rhi such that
104156cd587Sjoerg	movl	 16(%esp),			%ecx	//
105156cd587Sjoerg	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ≤ rhi < b
106156cd587Sjoerg	divl		%ecx					//
107156cd587Sjoerg	movl		%eax,			%ebx	//
108156cd587Sjoerg	movl	  8(%esp),			%eax	// Find qlo such that
109156cd587Sjoerg	divl		%ecx					//
110156cd587Sjoerg	movl		%ebx,			%edx	//		rhi:alo = qlo*b + rlo  with 0 ≤ rlo < b
111156cd587Sjoerg	popl		%ebx					//
112156cd587Sjoerg	retl								// and return qhi:qlo
113156cd587SjoergEND_COMPILERRT_FUNCTION(__udivdi3)
114156cd587Sjoerg
115156cd587Sjoerg#endif // __i386__
116