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 __umoddi3(du_int a, du_int b);
      7      1.1  joerg 
      8      1.1  joerg // result = remainder of 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 
     18      1.1  joerg // Stephen Canon, December 2008
     19      1.1  joerg 
     20      1.1  joerg #ifdef __i386__
     21      1.1  joerg 
     22      1.1  joerg .text
     23  1.1.1.2  joerg .balign 4
     24      1.1  joerg DEFINE_COMPILERRT_FUNCTION(__umoddi3)
     25      1.1  joerg 
     26      1.1  joerg 	pushl		%ebx
     27      1.1  joerg 	movl	 20(%esp),			%ebx	// Find the index i of the leading bit in b.
     28      1.1  joerg 	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to
     29      1.1  joerg 	jz			9f						// the code to handle that special case [9].
     30      1.1  joerg 
     31      1.1  joerg 	/* High word of b is known to be non-zero on this branch */
     32      1.1  joerg 
     33      1.1  joerg 	movl	 16(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b
     34      1.1  joerg 
     35      1.1  joerg 	shrl		%cl,			%eax	// Practically, this means that bhi is given by:
     36      1.1  joerg 	shrl		%eax					//
     37      1.1  joerg 	notl		%ecx					//		bhi = (high word of b) << (31 - i) |
     38      1.1  joerg 	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)
     39      1.1  joerg 	orl			%eax,			%ebx	//
     40      1.1  joerg 	movl	 12(%esp),			%edx	// Load the high and low words of a, and jump
     41      1.1  joerg 	movl	  8(%esp),			%eax	// to [2] if the high word is larger than bhi
     42      1.1  joerg 	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.
     43      1.1  joerg 	jae			2f
     44      1.1  joerg 
     45      1.1  joerg 	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
     46      1.1  joerg 
     47      1.1  joerg 	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r
     48      1.1  joerg 
     49      1.1  joerg 	pushl		%edi
     50      1.1  joerg 	notl		%ecx
     51      1.1  joerg 	shrl		%eax
     52      1.1  joerg 	shrl		%cl,			%eax	// q = qs >> (1 + i)
     53      1.1  joerg 	movl		%eax,			%edi
     54      1.1  joerg 	mull	 20(%esp)					// q*blo
     55      1.1  joerg 	movl	 12(%esp),			%ebx
     56      1.1  joerg 	movl	 16(%esp),			%ecx	// ECX:EBX = a
     57      1.1  joerg 	subl		%eax,			%ebx
     58      1.1  joerg 	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
     59      1.1  joerg 	movl	 24(%esp),			%eax
     60      1.1  joerg 	imull		%edi,			%eax	// q*bhi
     61      1.1  joerg 	subl		%eax,			%ecx	// ECX:EBX = a - q*b
     62      1.1  joerg 
     63      1.1  joerg 	jnc			1f						// if positive, this is the result.
     64      1.1  joerg 	addl	 20(%esp),			%ebx	// otherwise
     65      1.1  joerg 	adcl	 24(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
     66      1.1  joerg 1:	movl		%ebx,			%eax
     67      1.1  joerg 	movl		%ecx,			%edx
     68      1.1  joerg 
     69      1.1  joerg 	popl		%edi
     70      1.1  joerg 	popl		%ebx
     71      1.1  joerg 	retl
     72      1.1  joerg 
     73      1.1  joerg 
     74      1.1  joerg 2:	/* High word of a is greater than or equal to (b >> (1 + i)) on this branch */
     75      1.1  joerg 
     76      1.1  joerg 	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not
     77      1.1  joerg 	divl		%ebx					// overflow, and find q and r such that
     78      1.1  joerg 										//
     79      1.1  joerg 										//		ahi:alo = (1:q)*bhi + r
     80      1.1  joerg 										//
     81      1.1  joerg 										// Note that q is a number in (31-i).(1+i)
     82      1.1  joerg 										// fix point.
     83      1.1  joerg 
     84      1.1  joerg 	pushl		%edi
     85      1.1  joerg 	notl		%ecx
     86      1.1  joerg 	shrl		%eax
     87      1.1  joerg 	orl			$0x80000000,	%eax
     88      1.1  joerg 	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)
     89      1.1  joerg 	movl		%eax,			%edi
     90      1.1  joerg 	mull	 20(%esp)					// q*blo
     91      1.1  joerg 	movl	 12(%esp),			%ebx
     92      1.1  joerg 	movl	 16(%esp),			%ecx	// ECX:EBX = a
     93      1.1  joerg 	subl		%eax,			%ebx
     94      1.1  joerg 	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo
     95      1.1  joerg 	movl	 24(%esp),			%eax
     96      1.1  joerg 	imull		%edi,			%eax	// q*bhi
     97      1.1  joerg 	subl		%eax,			%ecx	// ECX:EBX = a - q*b
     98      1.1  joerg 
     99      1.1  joerg 	jnc			3f						// if positive, this is the result.
    100      1.1  joerg 	addl	 20(%esp),			%ebx	// otherwise
    101      1.1  joerg 	adcl	 24(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result
    102      1.1  joerg 3:	movl		%ebx,			%eax
    103      1.1  joerg 	movl		%ecx,			%edx
    104      1.1  joerg 
    105      1.1  joerg 	popl		%edi
    106      1.1  joerg 	popl		%ebx
    107      1.1  joerg 	retl
    108      1.1  joerg 
    109      1.1  joerg 
    110      1.1  joerg 
    111      1.1  joerg 9:	/* High word of b is zero on this branch */
    112      1.1  joerg 
    113      1.1  joerg 	movl	 12(%esp),			%eax	// Find qhi and rhi such that
    114      1.1  joerg 	movl	 16(%esp),			%ecx	//
    115      1.1  joerg 	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0  rhi < b
    116      1.1  joerg 	divl		%ecx					//
    117      1.1  joerg 	movl		%eax,			%ebx	//
    118      1.1  joerg 	movl	  8(%esp),			%eax	// Find rlo such that
    119      1.1  joerg 	divl		%ecx					//
    120      1.1  joerg 	movl		%edx,			%eax	//		rhi:alo = qlo*b + rlo  with 0  rlo < b
    121      1.1  joerg 	popl		%ebx					//
    122      1.1  joerg 	xorl		%edx,			%edx	// and return 0:rlo
    123      1.1  joerg 	retl								//
    124      1.1  joerg END_COMPILERRT_FUNCTION(__umoddi3)
    125      1.1  joerg 
    126      1.1  joerg #endif // __i386__
    127