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