131 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// du_int __umoddi3(du_int a, du_int b);8 9// result = remainder of a / b.10// both inputs and the output are 64-bit unsigned 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(__umoddi3)26 27 pushl %ebx28 movl 20(%esp), %ebx // Find the index i of the leading bit in b.29 bsrl %ebx, %ecx // If the high word of b is zero, jump to30 jz 9f // the code to handle that special case [9].31 32 // High word of b is known to be non-zero on this branch33 34 movl 16(%esp), %eax // Construct bhi, containing bits [1+i:32+i] of b35 36 shrl %cl, %eax // Practically, this means that bhi is given by:37 shrl %eax //38 notl %ecx // bhi = (high word of b) << (31 - i) |39 shll %cl, %ebx // (low word of b) >> (1 + i)40 orl %eax, %ebx //41 movl 12(%esp), %edx // Load the high and low words of a, and jump42 movl 8(%esp), %eax // to [2] if the high word is larger than bhi43 cmpl %ebx, %edx // to avoid overflowing the upcoming divide.44 jae 2f45 46 // High word of a is greater than or equal to (b >> (1 + i)) on this branch47 48 divl %ebx // eax <-- qs, edx <-- r such that ahi:alo = bs*qs + r49 50 pushl %edi51 notl %ecx52 shrl %eax53 shrl %cl, %eax // q = qs >> (1 + i)54 movl %eax, %edi55 mull 20(%esp) // q*blo56 movl 12(%esp), %ebx57 movl 16(%esp), %ecx // ECX:EBX = a58 subl %eax, %ebx59 sbbl %edx, %ecx // ECX:EBX = a - q*blo60 movl 24(%esp), %eax61 imull %edi, %eax // q*bhi62 subl %eax, %ecx // ECX:EBX = a - q*b63 64 jnc 1f // if positive, this is the result.65 addl 20(%esp), %ebx // otherwise66 adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result671: movl %ebx, %eax68 movl %ecx, %edx69 70 popl %edi71 popl %ebx72 retl73 74 752: // High word of a is greater than or equal to (b >> (1 + i)) on this branch76 77 subl %ebx, %edx // subtract bhi from ahi so that divide will not78 divl %ebx // overflow, and find q and r such that79 //80 // ahi:alo = (1:q)*bhi + r81 //82 // Note that q is a number in (31-i).(1+i)83 // fix point.84 85 pushl %edi86 notl %ecx87 shrl %eax88 orl $0x80000000, %eax89 shrl %cl, %eax // q = (1:qs) >> (1 + i)90 movl %eax, %edi91 mull 20(%esp) // q*blo92 movl 12(%esp), %ebx93 movl 16(%esp), %ecx // ECX:EBX = a94 subl %eax, %ebx95 sbbl %edx, %ecx // ECX:EBX = a - q*blo96 movl 24(%esp), %eax97 imull %edi, %eax // q*bhi98 subl %eax, %ecx // ECX:EBX = a - q*b99 100 jnc 3f // if positive, this is the result.101 addl 20(%esp), %ebx // otherwise102 adcl 24(%esp), %ecx // ECX:EBX = a - (q-1)*b = result1033: movl %ebx, %eax104 movl %ecx, %edx105 106 popl %edi107 popl %ebx108 retl109 110 111 1129: // High word of b is zero on this branch113 114 movl 12(%esp), %eax // Find qhi and rhi such that115 movl 16(%esp), %ecx //116 xorl %edx, %edx // ahi = qhi*b + rhi with 0 ≤ rhi < b117 divl %ecx //118 movl %eax, %ebx //119 movl 8(%esp), %eax // Find rlo such that120 divl %ecx //121 movl %edx, %eax // rhi:alo = qlo*b + rlo with 0 ≤ rlo < b122 popl %ebx //123 xorl %edx, %edx // and return 0:rlo124 retl //125END_COMPILERRT_FUNCTION(__umoddi3)126 127#endif // __i386__128 129NO_EXEC_STACK_DIRECTIVE130 131