1222656Sed// This file is dual licensed under the MIT and the University of Illinois Open 2222656Sed// Source Licenses. See LICENSE.TXT for details. 3214152Sed 4214152Sed#include "../assembly.h" 5214152Sed 6214152Sed// di_int __moddi3(di_int a, di_int b); 7214152Sed 8214152Sed// result = remainder of a / b. 9214152Sed// both inputs and the output are 64-bit signed integers. 10214152Sed// This will do whatever the underlying hardware is set to do on division by zero. 11214152Sed// No other exceptions are generated, as the divide cannot overflow. 12214152Sed// 13214152Sed// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware 14214152Sed// on x86_64. The performance goal is ~40 cycles per divide, which is faster than 15214152Sed// currently possible via simulation of integer divides on the x87 unit. 16214152Sed// 17214152Sed 18214152Sed// Stephen Canon, December 2008 19214152Sed 20214152Sed#ifdef __i386__ 21214152Sed 22214152Sed.text 23214152Sed.align 4 24214152SedDEFINE_COMPILERRT_FUNCTION(__moddi3) 25214152Sed 26214152Sed/* This is currently implemented by wrapping the unsigned modulus up in an absolute 27214152Sed value. This could certainly be improved upon. */ 28214152Sed 29214152Sed pushl %esi 30214152Sed movl 20(%esp), %edx // high word of b 31214152Sed movl 16(%esp), %eax // low word of b 32214152Sed movl %edx, %ecx 33214152Sed sarl $31, %ecx // (b < 0) ? -1 : 0 34214152Sed xorl %ecx, %eax 35214152Sed xorl %ecx, %edx // EDX:EAX = (b < 0) ? not(b) : b 36214152Sed subl %ecx, %eax 37214152Sed sbbl %ecx, %edx // EDX:EAX = abs(b) 38214152Sed movl %edx, 20(%esp) 39214152Sed movl %eax, 16(%esp) // store abs(b) back to stack 40214152Sed 41214152Sed movl 12(%esp), %edx // high word of b 42214152Sed movl 8(%esp), %eax // low word of b 43214152Sed movl %edx, %ecx 44214152Sed sarl $31, %ecx // (a < 0) ? -1 : 0 45214152Sed xorl %ecx, %eax 46214152Sed xorl %ecx, %edx // EDX:EAX = (a < 0) ? not(a) : a 47214152Sed subl %ecx, %eax 48214152Sed sbbl %ecx, %edx // EDX:EAX = abs(a) 49214152Sed movl %edx, 12(%esp) 50214152Sed movl %eax, 8(%esp) // store abs(a) back to stack 51214152Sed movl %ecx, %esi // set aside sign of a 52214152Sed 53214152Sed pushl %ebx 54214152Sed movl 24(%esp), %ebx // Find the index i of the leading bit in b. 55214152Sed bsrl %ebx, %ecx // If the high word of b is zero, jump to 56214152Sed jz 9f // the code to handle that special case [9]. 57214152Sed 58214152Sed /* High word of b is known to be non-zero on this branch */ 59214152Sed 60214152Sed movl 20(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b 61214152Sed 62214152Sed shrl %cl, %eax // Practically, this means that bhi is given by: 63214152Sed shrl %eax // 64214152Sed notl %ecx // bhi = (high word of b) << (31 - i) | 65214152Sed shll %cl, %ebx // (low word of b) >> (1 + i) 66214152Sed orl %eax, %ebx // 67214152Sed movl 16(%esp), %edx // Load the high and low words of a, and jump 68214152Sed movl 12(%esp), %eax // to [2] if the high word is larger than bhi 69214152Sed cmpl %ebx, %edx // to avoid overflowing the upcoming divide. 70214152Sed jae 2f 71214152Sed 72214152Sed /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 73214152Sed 74214152Sed divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r 75214152Sed 76214152Sed pushl %edi 77214152Sed notl %ecx 78214152Sed shrl %eax 79214152Sed shrl %cl, %eax // q = qs >> (1 + i) 80214152Sed movl %eax, %edi 81214152Sed mull 24(%esp) // q*blo 82214152Sed movl 16(%esp), %ebx 83214152Sed movl 20(%esp), %ecx // ECX:EBX = a 84214152Sed subl %eax, %ebx 85214152Sed sbbl %edx, %ecx // ECX:EBX = a - q*blo 86214152Sed movl 28(%esp), %eax 87214152Sed imull %edi, %eax // q*bhi 88214152Sed subl %eax, %ecx // ECX:EBX = a - q*b 89214152Sed 90214152Sed jnc 1f // if positive, this is the result. 91214152Sed addl 24(%esp), %ebx // otherwise 92214152Sed adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 93214152Sed1: movl %ebx, %eax 94214152Sed movl %ecx, %edx 95214152Sed 96214152Sed addl %esi, %eax // Restore correct sign to result 97214152Sed adcl %esi, %edx 98214152Sed xorl %esi, %eax 99214152Sed xorl %esi, %edx 100214152Sed popl %edi // Restore callee-save registers 101214152Sed popl %ebx 102214152Sed popl %esi 103214152Sed retl // Return 104214152Sed 105214152Sed2: /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 106214152Sed 107214152Sed subl %ebx, %edx // subtract bhi from ahi so that divide will not 108214152Sed divl %ebx // overflow, and find q and r such that 109214152Sed // 110214152Sed // ahi:alo = (1:q)*bhi + r 111214152Sed // 112214152Sed // Note that q is a number in (31-i).(1+i) 113214152Sed // fix point. 114214152Sed 115214152Sed pushl %edi 116214152Sed notl %ecx 117214152Sed shrl %eax 118214152Sed orl $0x80000000, %eax 119214152Sed shrl %cl, %eax // q = (1:qs) >> (1 + i) 120214152Sed movl %eax, %edi 121214152Sed mull 24(%esp) // q*blo 122214152Sed movl 16(%esp), %ebx 123214152Sed movl 20(%esp), %ecx // ECX:EBX = a 124214152Sed subl %eax, %ebx 125214152Sed sbbl %edx, %ecx // ECX:EBX = a - q*blo 126214152Sed movl 28(%esp), %eax 127214152Sed imull %edi, %eax // q*bhi 128214152Sed subl %eax, %ecx // ECX:EBX = a - q*b 129214152Sed 130214152Sed jnc 3f // if positive, this is the result. 131214152Sed addl 24(%esp), %ebx // otherwise 132214152Sed adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 133214152Sed3: movl %ebx, %eax 134214152Sed movl %ecx, %edx 135214152Sed 136214152Sed addl %esi, %eax // Restore correct sign to result 137214152Sed adcl %esi, %edx 138214152Sed xorl %esi, %eax 139214152Sed xorl %esi, %edx 140214152Sed popl %edi // Restore callee-save registers 141214152Sed popl %ebx 142214152Sed popl %esi 143214152Sed retl // Return 144214152Sed 145214152Sed9: /* High word of b is zero on this branch */ 146214152Sed 147214152Sed movl 16(%esp), %eax // Find qhi and rhi such that 148214152Sed movl 20(%esp), %ecx // 149214152Sed xorl %edx, %edx // ahi = qhi*b + rhi with 0 ��� rhi < b 150214152Sed divl %ecx // 151214152Sed movl %eax, %ebx // 152214152Sed movl 12(%esp), %eax // Find rlo such that 153214152Sed divl %ecx // 154214152Sed movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ��� rlo < b 155214152Sed popl %ebx // 156214152Sed xorl %edx, %edx // and return 0:rlo 157214152Sed 158214152Sed addl %esi, %eax // Restore correct sign to result 159214152Sed adcl %esi, %edx 160214152Sed xorl %esi, %eax 161214152Sed xorl %esi, %edx 162214152Sed popl %esi 163214152Sed retl // Return 164214152Sed 165214152Sed 166214152Sed#endif // __i386__ 167