brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.1 KiB · 4580f20 Raw
171 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 __moddi3(di_int a, di_int b);8 9// result = remainder of 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 19// Stephen Canon, December 200820 21#ifdef __i386__22 23.text24.balign 425DEFINE_COMPILERRT_FUNCTION(__moddi3)26 27// This is currently implemented by wrapping the unsigned modulus up in an absolute28// value.  This could 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 42	movl	 12(%esp),			%edx	// high word of b43	movl	  8(%esp),			%eax	// low word of b44	movl		%edx,			%ecx45	sarl		$31,			%ecx	// (a < 0) ? -1 : 046	xorl		%ecx,			%eax47	xorl		%ecx,			%edx	// EDX:EAX = (a < 0) ? not(a) : a48	subl		%ecx,			%eax49	sbbl		%ecx,			%edx	// EDX:EAX = abs(a)50	movl		%edx,		 12(%esp)51	movl		%eax,		  8(%esp)	// store abs(a) back to stack52	movl		%ecx,			%esi	// set aside sign of a53 54	pushl		%ebx55	movl	 24(%esp),			%ebx	// Find the index i of the leading bit in b.56	bsrl		%ebx,			%ecx	// If the high word of b is zero, jump to57	jz			9f						// the code to handle that special case [9].58 59	// High word of b is known to be non-zero on this branch60 61	movl	 20(%esp),			%eax	// Construct bhi, containing bits [1+i:32+i] of b62 63	shrl		%cl,			%eax	// Practically, this means that bhi is given by:64	shrl		%eax					//65	notl		%ecx					//		bhi = (high word of b) << (31 - i) |66	shll		%cl,			%ebx	//			  (low word of b) >> (1 + i)67	orl			%eax,			%ebx	//68	movl	 16(%esp),			%edx	// Load the high and low words of a, and jump69	movl	 12(%esp),			%eax	// to [2] if the high word is larger than bhi70	cmpl		%ebx,			%edx	// to avoid overflowing the upcoming divide.71	jae			2f72 73	// High word of a is greater than or equal to (b >> (1 + i)) on this branch74 75	divl		%ebx					// eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r76 77	pushl		%edi78	notl		%ecx79	shrl		%eax80	shrl		%cl,			%eax	// q = qs >> (1 + i)81	movl		%eax,			%edi82	mull	 24(%esp)					// q*blo83	movl	 16(%esp),			%ebx84	movl	 20(%esp),			%ecx	// ECX:EBX = a85	subl		%eax,			%ebx86	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo87	movl	 28(%esp),			%eax88	imull		%edi,			%eax	// q*bhi89	subl		%eax,			%ecx	// ECX:EBX = a - q*b90 91	jnc			1f						// if positive, this is the result.92	addl	 24(%esp),			%ebx	// otherwise93	adcl	 28(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result941:	movl		%ebx,			%eax95	movl		%ecx,			%edx96 97	addl		%esi,			%eax	// Restore correct sign to result98	adcl		%esi,			%edx99	xorl		%esi,			%eax100	xorl		%esi,			%edx101	popl		%edi					// Restore callee-save registers102	popl		%ebx103	popl		%esi104	retl								// Return105 1062:	// High word of a is greater than or equal to (b >> (1 + i)) on this branch107 108	subl		%ebx,			%edx	// subtract bhi from ahi so that divide will not109	divl		%ebx					// overflow, and find q and r such that110										//111										//		ahi:alo = (1:q)*bhi + r112										//113										// Note that q is a number in (31-i).(1+i)114										// fix point.115 116	pushl		%edi117	notl		%ecx118	shrl		%eax119	orl			$0x80000000,	%eax120	shrl		%cl,			%eax	// q = (1:qs) >> (1 + i)121	movl		%eax,			%edi122	mull	 24(%esp)					// q*blo123	movl	 16(%esp),			%ebx124	movl	 20(%esp),			%ecx	// ECX:EBX = a125	subl		%eax,			%ebx126	sbbl		%edx,			%ecx	// ECX:EBX = a - q*blo127	movl	 28(%esp),			%eax128	imull		%edi,			%eax	// q*bhi129	subl		%eax,			%ecx	// ECX:EBX = a - q*b130 131	jnc			3f						// if positive, this is the result.132	addl	 24(%esp),			%ebx	// otherwise133	adcl	 28(%esp),			%ecx	// ECX:EBX = a - (q-1)*b = result1343:	movl		%ebx,			%eax135	movl		%ecx,			%edx136 137	addl		%esi,			%eax	// Restore correct sign to result138	adcl		%esi,			%edx139	xorl		%esi,			%eax140	xorl		%esi,			%edx141	popl		%edi					// Restore callee-save registers142	popl		%ebx143	popl		%esi144	retl								// Return145 1469:	// High word of b is zero on this branch147 148	movl	 16(%esp),			%eax	// Find qhi and rhi such that149	movl	 20(%esp),			%ecx	//150	xorl		%edx,			%edx	//		ahi = qhi*b + rhi	with	0 ≤ rhi < b151	divl		%ecx					//152	movl		%eax,			%ebx	//153	movl	 12(%esp),			%eax	// Find rlo such that154	divl		%ecx					//155	movl		%edx,			%eax	//		rhi:alo = qlo*b + rlo  with 0 ≤ rlo < b156	popl		%ebx					//157	xorl		%edx,			%edx	// and return 0:rlo158 159	addl		%esi,			%eax	// Restore correct sign to result160	adcl		%esi,			%edx161	xorl		%esi,			%eax162	xorl		%esi,			%edx163	popl		%esi164	retl								// Return165END_COMPILERRT_FUNCTION(__moddi3)166 167#endif // __i386__168 169NO_EXEC_STACK_DIRECTIVE170 171