brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.0 KiB · 09e1e42 Raw
167 lines · plain
1// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.2// See https://llvm.org/LICENSE.txt for license information.3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception4 5#include "../assembly.h"6 7// di_int __divdi3(di_int a, di_int b);8 9// result = a / b.10// both inputs and the output are 64-bit signed integers.11// This will do whatever the underlying hardware is set to do on division by zero.12// No other exceptions are generated, as the divide cannot overflow.13//14// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware15// on x86_64.  The performance goal is ~40 cycles per divide, which is faster than16// currently possible via simulation of integer divides on the x87 unit.17//18// Stephen Canon, December 200819 20#ifdef __i386__21 22.text23.balign 424DEFINE_COMPILERRT_FUNCTION(__divdi3)25 26// This is currently implemented by wrapping the unsigned divide up in an absolute27// value, then restoring the correct sign at the end of the computation.  This could28// certainly be improved upon.29 30	pushl		%esi31	movl	 20(%esp),			%edx	// high word of b32	movl	 16(%esp),			%eax	// low word of b33	movl		%edx,			%ecx34	sarl		$31,			%ecx	// (b < 0) ? -1 : 035	xorl		%ecx,			%eax36	xorl		%ecx,			%edx	// EDX:EAX = (b < 0) ? not(b) : b37	subl		%ecx,			%eax38	sbbl		%ecx,			%edx	// EDX:EAX = abs(b)39	movl		%edx,		 20(%esp)40	movl		%eax,		 16(%esp)	// store abs(b) back to stack41	movl		%ecx,			%esi	// set aside sign of b42 43	movl	 12(%esp),			%edx	// high word of b44	movl	  8(%esp),			%eax	// low word of b45	movl		%edx,			%ecx46	sarl		$31,			%ecx	// (a < 0) ? -1 : 047	xorl		%ecx,			%eax48	xorl		%ecx,			%edx	// EDX:EAX = (a < 0) ? not(a) : a49	subl		%ecx,			%eax50	sbbl		%ecx,			%edx	// EDX:EAX = abs(a)51	movl		%edx,		 12(%esp)52	movl		%eax,		  8(%esp)	// store abs(a) back to stack53	xorl		%ecx,			%esi	// sign of result = (sign of a) ^ (sign of b)54 55	pushl		%ebx56	movl	 24(%esp),			%ebx	// Find the index i of the leading bit in b.57	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to58	jz			9f						// the code to handle that special case [9].59 60	// High word of b is known to be non-zero on this branch61 62	movl	 20(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b63 64	shrl		%cl,			%eax	// Practically, this means that bhi is given by:65	shrl		%eax					//66	notl		%ecx					//		bhi = (high word of b) << (31 - i) |67	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)68	orl			%eax,			%ebx	//69	movl	 16(%esp),			%edx	// Load the high and low words of a, and jump70	movl	 12(%esp),			%eax	// to [1] if the high word is larger than bhi71	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.72	jae			1f73 74	// High word of a is greater than or equal to (b >> (1 + i)) on this branch75 76	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r77 78	pushl		%edi79	notl		%ecx80	shrl		%eax81	shrl		%cl,			%eax	// q = qs >> (1 + i)82	movl		%eax,			%edi83	mull	 24(%esp)					// q*blo84	movl	 16(%esp),			%ebx85	movl	 20(%esp),			%ecx	// ECX:EBX = a86	subl		%eax,			%ebx87	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo88	movl	 28(%esp),			%eax89	imull		%edi,			%eax	// q*bhi90	subl		%eax,			%ecx	// ECX:EBX = a - q*b91	sbbl		$0,				%edi	// decrement q if remainder is negative92	xorl		%edx,			%edx93	movl		%edi,			%eax94 95	addl		%esi,			%eax	// Restore correct sign to result96	adcl		%esi,			%edx97	xorl		%esi,			%eax98	xorl		%esi,			%edx99	popl		%edi					// Restore callee-save registers100	popl		%ebx101	popl		%esi102	retl								// Return103 104 1051:	// High word of a is greater than or equal to (b >> (1 + i)) on this branch106 107	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not108	divl		%ebx					// overflow, and find q and r such that109										//110										//		ahi:alo = (1:q)*bhi + r111										//112										// Note that q is a number in (31-i).(1+i)113										// fix point.114 115	pushl		%edi116	notl		%ecx117	shrl		%eax118	orl			$0x80000000,	%eax119	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)120	movl		%eax,			%edi121	mull	 24(%esp)					// q*blo122	movl	 16(%esp),			%ebx123	movl	 20(%esp),			%ecx	// ECX:EBX = a124	subl		%eax,			%ebx125	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo126	movl	 28(%esp),			%eax127	imull		%edi,			%eax	// q*bhi128	subl		%eax,			%ecx	// ECX:EBX = a - q*b129	sbbl		$0,				%edi	// decrement q if remainder is negative130	xorl		%edx,			%edx131	movl		%edi,			%eax132 133	addl		%esi,			%eax	// Restore correct sign to result134	adcl		%esi,			%edx135	xorl		%esi,			%eax136	xorl		%esi,			%edx137	popl		%edi					// Restore callee-save registers138	popl		%ebx139	popl		%esi140	retl								// Return141 142 1439:	// High word of b is zero on this branch144 145	movl	 16(%esp),			%eax	// Find qhi and rhi such that146	movl	 20(%esp),			%ecx	//147	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ≤ rhi < b148	divl		%ecx					//149	movl		%eax,			%ebx	//150	movl	 12(%esp),			%eax	// Find qlo such that151	divl		%ecx					//152	movl		%ebx,			%edx	//		rhi:alo = qlo*b + rlo  with 0 ≤ rlo < b153 154	addl		%esi,			%eax	// Restore correct sign to result155	adcl		%esi,			%edx156	xorl		%esi,			%eax157	xorl		%esi,			%edx158	popl		%ebx					// Restore callee-save registers159	popl		%esi160	retl								// Return161END_COMPILERRT_FUNCTION(__divdi3)162 163#endif // __i386__164 165NO_EXEC_STACK_DIRECTIVE166 167