1*0a6a1f1dSLionel Sambuc// This file is dual licensed under the MIT and the University of Illinois Open 2*0a6a1f1dSLionel Sambuc// Source Licenses. See LICENSE.TXT for details. 3*0a6a1f1dSLionel Sambuc 4*0a6a1f1dSLionel Sambuc#include "../assembly.h" 5*0a6a1f1dSLionel Sambuc 6*0a6a1f1dSLionel Sambuc// du_int __umoddi3(du_int a, du_int b); 7*0a6a1f1dSLionel Sambuc 8*0a6a1f1dSLionel Sambuc// result = remainder of a / b. 9*0a6a1f1dSLionel Sambuc// both inputs and the output are 64-bit unsigned integers. 10*0a6a1f1dSLionel Sambuc// This will do whatever the underlying hardware is set to do on division by zero. 11*0a6a1f1dSLionel Sambuc// No other exceptions are generated, as the divide cannot overflow. 12*0a6a1f1dSLionel Sambuc// 13*0a6a1f1dSLionel Sambuc// This is targeted at 32-bit x86 *only*, as this can be done directly in hardware 14*0a6a1f1dSLionel Sambuc// on x86_64. The performance goal is ~40 cycles per divide, which is faster than 15*0a6a1f1dSLionel Sambuc// currently possible via simulation of integer divides on the x87 unit. 16*0a6a1f1dSLionel Sambuc// 17*0a6a1f1dSLionel Sambuc 18*0a6a1f1dSLionel Sambuc// Stephen Canon, December 2008 19*0a6a1f1dSLionel Sambuc 20*0a6a1f1dSLionel Sambuc#ifdef __i386__ 21*0a6a1f1dSLionel Sambuc 22*0a6a1f1dSLionel Sambuc.text 23*0a6a1f1dSLionel Sambuc.balign 4 24*0a6a1f1dSLionel SambucDEFINE_COMPILERRT_FUNCTION(__umoddi3) 25*0a6a1f1dSLionel Sambuc 26*0a6a1f1dSLionel Sambuc pushl %ebx 27*0a6a1f1dSLionel Sambuc movl 20(%esp), %ebx // Find the index i of the leading bit in b. 28*0a6a1f1dSLionel Sambuc bsrl %ebx, %ecx // If the high word of b is zero, jump to 29*0a6a1f1dSLionel Sambuc jz 9f // the code to handle that special case [9]. 30*0a6a1f1dSLionel Sambuc 31*0a6a1f1dSLionel Sambuc /* High word of b is known to be non-zero on this branch */ 32*0a6a1f1dSLionel Sambuc 33*0a6a1f1dSLionel Sambuc movl 16(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b 34*0a6a1f1dSLionel Sambuc 35*0a6a1f1dSLionel Sambuc shrl %cl, %eax // Practically, this means that bhi is given by: 36*0a6a1f1dSLionel Sambuc shrl %eax // 37*0a6a1f1dSLionel Sambuc notl %ecx // bhi = (high word of b) << (31 - i) | 38*0a6a1f1dSLionel Sambuc shll %cl, %ebx // (low word of b) >> (1 + i) 39*0a6a1f1dSLionel Sambuc orl %eax, %ebx // 40*0a6a1f1dSLionel Sambuc movl 12(%esp), %edx // Load the high and low words of a, and jump 41*0a6a1f1dSLionel Sambuc movl 8(%esp), %eax // to [2] if the high word is larger than bhi 42*0a6a1f1dSLionel Sambuc cmpl %ebx, %edx // to avoid overflowing the upcoming divide. 43*0a6a1f1dSLionel Sambuc jae 2f 44*0a6a1f1dSLionel Sambuc 45*0a6a1f1dSLionel Sambuc /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 46*0a6a1f1dSLionel Sambuc 47*0a6a1f1dSLionel Sambuc divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r 48*0a6a1f1dSLionel Sambuc 49*0a6a1f1dSLionel Sambuc pushl %edi 50*0a6a1f1dSLionel Sambuc notl %ecx 51*0a6a1f1dSLionel Sambuc shrl %eax 52*0a6a1f1dSLionel Sambuc shrl %cl, %eax // q = qs >> (1 + i) 53*0a6a1f1dSLionel Sambuc movl %eax, %edi 54*0a6a1f1dSLionel Sambuc mull 20(%esp) // q*blo 55*0a6a1f1dSLionel Sambuc movl 12(%esp), %ebx 56*0a6a1f1dSLionel Sambuc movl 16(%esp), %ecx // ECX:EBX = a 57*0a6a1f1dSLionel Sambuc subl %eax, %ebx 58*0a6a1f1dSLionel Sambuc sbbl %edx, %ecx // ECX:EBX = a - q*blo 59*0a6a1f1dSLionel Sambuc movl 24(%esp), %eax 60*0a6a1f1dSLionel Sambuc imull %edi, %eax // q*bhi 61*0a6a1f1dSLionel Sambuc subl %eax, %ecx // ECX:EBX = a - q*b 62*0a6a1f1dSLionel Sambuc 63*0a6a1f1dSLionel Sambuc jnc 1f // if positive, this is the result. 64*0a6a1f1dSLionel Sambuc addl 20(%esp), %ebx // otherwise 65*0a6a1f1dSLionel Sambuc adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 66*0a6a1f1dSLionel Sambuc1: movl %ebx, %eax 67*0a6a1f1dSLionel Sambuc movl %ecx, %edx 68*0a6a1f1dSLionel Sambuc 69*0a6a1f1dSLionel Sambuc popl %edi 70*0a6a1f1dSLionel Sambuc popl %ebx 71*0a6a1f1dSLionel Sambuc retl 72*0a6a1f1dSLionel Sambuc 73*0a6a1f1dSLionel Sambuc 74*0a6a1f1dSLionel Sambuc2: /* High word of a is greater than or equal to (b >> (1 + i)) on this branch */ 75*0a6a1f1dSLionel Sambuc 76*0a6a1f1dSLionel Sambuc subl %ebx, %edx // subtract bhi from ahi so that divide will not 77*0a6a1f1dSLionel Sambuc divl %ebx // overflow, and find q and r such that 78*0a6a1f1dSLionel Sambuc // 79*0a6a1f1dSLionel Sambuc // ahi:alo = (1:q)*bhi + r 80*0a6a1f1dSLionel Sambuc // 81*0a6a1f1dSLionel Sambuc // Note that q is a number in (31-i).(1+i) 82*0a6a1f1dSLionel Sambuc // fix point. 83*0a6a1f1dSLionel Sambuc 84*0a6a1f1dSLionel Sambuc pushl %edi 85*0a6a1f1dSLionel Sambuc notl %ecx 86*0a6a1f1dSLionel Sambuc shrl %eax 87*0a6a1f1dSLionel Sambuc orl $0x80000000, %eax 88*0a6a1f1dSLionel Sambuc shrl %cl, %eax // q = (1:qs) >> (1 + i) 89*0a6a1f1dSLionel Sambuc movl %eax, %edi 90*0a6a1f1dSLionel Sambuc mull 20(%esp) // q*blo 91*0a6a1f1dSLionel Sambuc movl 12(%esp), %ebx 92*0a6a1f1dSLionel Sambuc movl 16(%esp), %ecx // ECX:EBX = a 93*0a6a1f1dSLionel Sambuc subl %eax, %ebx 94*0a6a1f1dSLionel Sambuc sbbl %edx, %ecx // ECX:EBX = a - q*blo 95*0a6a1f1dSLionel Sambuc movl 24(%esp), %eax 96*0a6a1f1dSLionel Sambuc imull %edi, %eax // q*bhi 97*0a6a1f1dSLionel Sambuc subl %eax, %ecx // ECX:EBX = a - q*b 98*0a6a1f1dSLionel Sambuc 99*0a6a1f1dSLionel Sambuc jnc 3f // if positive, this is the result. 100*0a6a1f1dSLionel Sambuc addl 20(%esp), %ebx // otherwise 101*0a6a1f1dSLionel Sambuc adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result 102*0a6a1f1dSLionel Sambuc3: movl %ebx, %eax 103*0a6a1f1dSLionel Sambuc movl %ecx, %edx 104*0a6a1f1dSLionel Sambuc 105*0a6a1f1dSLionel Sambuc popl %edi 106*0a6a1f1dSLionel Sambuc popl %ebx 107*0a6a1f1dSLionel Sambuc retl 108*0a6a1f1dSLionel Sambuc 109*0a6a1f1dSLionel Sambuc 110*0a6a1f1dSLionel Sambuc 111*0a6a1f1dSLionel Sambuc9: /* High word of b is zero on this branch */ 112*0a6a1f1dSLionel Sambuc 113*0a6a1f1dSLionel Sambuc movl 12(%esp), %eax // Find qhi and rhi such that 114*0a6a1f1dSLionel Sambuc movl 16(%esp), %ecx // 115*0a6a1f1dSLionel Sambuc xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b 116*0a6a1f1dSLionel Sambuc divl %ecx // 117*0a6a1f1dSLionel Sambuc movl %eax, %ebx // 118*0a6a1f1dSLionel Sambuc movl 8(%esp), %eax // Find rlo such that 119*0a6a1f1dSLionel Sambuc divl %ecx // 120*0a6a1f1dSLionel Sambuc movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b 121*0a6a1f1dSLionel Sambuc popl %ebx // 122*0a6a1f1dSLionel Sambuc xorl %edx, %edx // and return 0:rlo 123*0a6a1f1dSLionel Sambuc retl // 124*0a6a1f1dSLionel SambucEND_COMPILERRT_FUNCTION(__umoddi3) 125*0a6a1f1dSLionel Sambuc 126*0a6a1f1dSLionel Sambuc#endif // __i386__ 127