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