1156cd587Sjoerg// This file is dual licensed under the MIT and the University of Illinois Open 2156cd587Sjoerg// Source Licenses. See LICENSE.TXT for details. 3156cd587Sjoerg 4156cd587Sjoerg#include "../assembly.h" 5156cd587Sjoerg 6156cd587Sjoerg// di_int __moddi3(di_int a, di_int b); 7156cd587Sjoerg 8156cd587Sjoerg// result = remainder of a / b. 9156cd587Sjoerg// both inputs and the output are 64-bit signed integers. 10156cd587Sjoerg// This will do whatever the underlying hardware is set to do on division by zero. 11156cd587Sjoerg// No other exceptions are generated, as the divide cannot overflow. 12156cd587Sjoerg// 13156cd587Sjoerg// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware 14156cd587Sjoerg// on x86_64. The performance goal is ~40 cycles per divide, which is faster than 15156cd587Sjoerg// currently possible via simulation of integer divides on the x87 unit. 16156cd587Sjoerg// 17156cd587Sjoerg 18156cd587Sjoerg// Stephen Canon, December 2008 19156cd587Sjoerg 20156cd587Sjoerg#ifdef __i386__ 21156cd587Sjoerg 22156cd587Sjoerg.text 23*61f2f256Sjoerg.balign 4 24156cd587SjoergDEFINE_COMPILERRT_FUNCTION(__moddi3) 25156cd587Sjoerg 26156cd587Sjoerg/* This is currently implemented by wrapping the unsigned modulus up in an absolute 27156cd587Sjoerg value. This could certainly be improved upon. */ 28156cd587Sjoerg 29156cd587Sjoerg pushl %esi 30156cd587Sjoerg movl 20(%esp), %edx // high word of b 31156cd587Sjoerg movl 16(%esp), %eax // low word of b 32156cd587Sjoerg movl %edx, %ecx 33156cd587Sjoerg sarl $31, %ecx // (b < 0) ? -1 : 0 34156cd587Sjoerg xorl %ecx, %eax 35156cd587Sjoerg xorl %ecx, %edx // EDX:EAX = (b < 0) ? not(b) : b 36156cd587Sjoerg subl %ecx, %eax 37156cd587Sjoerg sbbl %ecx, %edx // EDX:EAX = abs(b) 38156cd587Sjoerg movl %edx, 20(%esp) 39156cd587Sjoerg movl %eax, 16(%esp) // store abs(b) back to stack 40156cd587Sjoerg 41156cd587Sjoerg movl 12(%esp), %edx // high word of b 42156cd587Sjoerg movl 8(%esp), %eax // low word of b 43156cd587Sjoerg movl %edx, %ecx 44156cd587Sjoerg sarl $31, %ecx // (a < 0) ? -1 : 0 45156cd587Sjoerg xorl %ecx, %eax 46156cd587Sjoerg xorl %ecx, %edx // EDX:EAX = (a < 0) ? not(a) : a 47156cd587Sjoerg subl %ecx, %eax 48156cd587Sjoerg sbbl %ecx, %edx // EDX:EAX = abs(a) 49156cd587Sjoerg movl %edx, 12(%esp) 50156cd587Sjoerg movl %eax, 8(%esp) // store abs(a) back to stack 51156cd587Sjoerg movl %ecx, %esi // set aside sign of a 52156cd587Sjoerg 53156cd587Sjoerg pushl %ebx 54156cd587Sjoerg movl 24(%esp), %ebx // Find the index i of the leading bit in b. 55156cd587Sjoerg bsrl %ebx, %ecx // If the high word of b is zero, jump to 56156cd587Sjoerg jz 9f // the code to handle that special case [9]. 57156cd587Sjoerg 58156cd587Sjoerg /* High word of b is known to be non-zero on this branch */ 59156cd587Sjoerg 60156cd587Sjoerg movl 20(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b 61156cd587Sjoerg 62156cd587Sjoerg shrl %cl, %eax // Practically, this means that bhi is given by: 63156cd587Sjoerg shrl %eax // 64156cd587Sjoerg notl %ecx // bhi = (high word of b) << (31 - i) | 65156cd587Sjoerg shll %cl, %ebx // (low word of b) >> (1 + i) 66156cd587Sjoerg orl %eax, %ebx // 67156cd587Sjoerg movl 16(%esp), %edx // Load the high and low words of a, and jump 68156cd587Sjoerg movl 12(%esp), %eax // to [2] if the high word is larger than bhi 69156cd587Sjoerg cmpl %ebx, %edx // to avoid overflowing the upcoming divide. 70156cd587Sjoerg jae 2f 71156cd587Sjoerg 72156cd587Sjoerg /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 73156cd587Sjoerg 74156cd587Sjoerg divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r 75156cd587Sjoerg 76156cd587Sjoerg pushl %edi 77156cd587Sjoerg notl %ecx 78156cd587Sjoerg shrl %eax 79156cd587Sjoerg shrl %cl, %eax // q = qs >> (1 + i) 80156cd587Sjoerg movl %eax, %edi 81156cd587Sjoerg mull 24(%esp) // q*blo 82156cd587Sjoerg movl 16(%esp), %ebx 83156cd587Sjoerg movl 20(%esp), %ecx // ECX:EBX = a 84156cd587Sjoerg subl %eax, %ebx 85156cd587Sjoerg sbbl %edx, %ecx // ECX:EBX = a - q*blo 86156cd587Sjoerg movl 28(%esp), %eax 87156cd587Sjoerg imull %edi, %eax // q*bhi 88156cd587Sjoerg subl %eax, %ecx // ECX:EBX = a - q*b 89156cd587Sjoerg 90156cd587Sjoerg jnc 1f // if positive, this is the result. 91156cd587Sjoerg addl 24(%esp), %ebx // otherwise 92156cd587Sjoerg adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 93156cd587Sjoerg1: movl %ebx, %eax 94156cd587Sjoerg movl %ecx, %edx 95156cd587Sjoerg 96156cd587Sjoerg addl %esi, %eax // Restore correct sign to result 97156cd587Sjoerg adcl %esi, %edx 98156cd587Sjoerg xorl %esi, %eax 99156cd587Sjoerg xorl %esi, %edx 100156cd587Sjoerg popl %edi // Restore callee-save registers 101156cd587Sjoerg popl %ebx 102156cd587Sjoerg popl %esi 103156cd587Sjoerg retl // Return 104156cd587Sjoerg 105156cd587Sjoerg2: /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 106156cd587Sjoerg 107156cd587Sjoerg subl %ebx, %edx // subtract bhi from ahi so that divide will not 108156cd587Sjoerg divl %ebx // overflow, and find q and r such that 109156cd587Sjoerg // 110156cd587Sjoerg // ahi:alo = (1:q)*bhi + r 111156cd587Sjoerg // 112156cd587Sjoerg // Note that q is a number in (31-i).(1+i) 113156cd587Sjoerg // fix point. 114156cd587Sjoerg 115156cd587Sjoerg pushl %edi 116156cd587Sjoerg notl %ecx 117156cd587Sjoerg shrl %eax 118156cd587Sjoerg orl $0x80000000, %eax 119156cd587Sjoerg shrl %cl, %eax // q = (1:qs) >> (1 + i) 120156cd587Sjoerg movl %eax, %edi 121156cd587Sjoerg mull 24(%esp) // q*blo 122156cd587Sjoerg movl 16(%esp), %ebx 123156cd587Sjoerg movl 20(%esp), %ecx // ECX:EBX = a 124156cd587Sjoerg subl %eax, %ebx 125156cd587Sjoerg sbbl %edx, %ecx // ECX:EBX = a - q*blo 126156cd587Sjoerg movl 28(%esp), %eax 127156cd587Sjoerg imull %edi, %eax // q*bhi 128156cd587Sjoerg subl %eax, %ecx // ECX:EBX = a - q*b 129156cd587Sjoerg 130156cd587Sjoerg jnc 3f // if positive, this is the result. 131156cd587Sjoerg addl 24(%esp), %ebx // otherwise 132156cd587Sjoerg adcl 28(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 133156cd587Sjoerg3: movl %ebx, %eax 134156cd587Sjoerg movl %ecx, %edx 135156cd587Sjoerg 136156cd587Sjoerg addl %esi, %eax // Restore correct sign to result 137156cd587Sjoerg adcl %esi, %edx 138156cd587Sjoerg xorl %esi, %eax 139156cd587Sjoerg xorl %esi, %edx 140156cd587Sjoerg popl %edi // Restore callee-save registers 141156cd587Sjoerg popl %ebx 142156cd587Sjoerg popl %esi 143156cd587Sjoerg retl // Return 144156cd587Sjoerg 145156cd587Sjoerg9: /* High word of b is zero on this branch */ 146156cd587Sjoerg 147156cd587Sjoerg movl 16(%esp), %eax // Find qhi and rhi such that 148156cd587Sjoerg movl 20(%esp), %ecx // 149156cd587Sjoerg xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b 150156cd587Sjoerg divl %ecx // 151156cd587Sjoerg movl %eax, %ebx // 152156cd587Sjoerg movl 12(%esp), %eax // Find rlo such that 153156cd587Sjoerg divl %ecx // 154156cd587Sjoerg movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b 155156cd587Sjoerg popl %ebx // 156156cd587Sjoerg xorl %edx, %edx // and return 0:rlo 157156cd587Sjoerg 158156cd587Sjoerg addl %esi, %eax // Restore correct sign to result 159156cd587Sjoerg adcl %esi, %edx 160156cd587Sjoerg xorl %esi, %eax 161156cd587Sjoerg xorl %esi, %edx 162156cd587Sjoerg popl %esi 163156cd587Sjoerg retl // Return 164156cd587SjoergEND_COMPILERRT_FUNCTION(__moddi3) 165156cd587Sjoerg 166156cd587Sjoerg#endif // __i386__ 167