1386aef8594517491d6e8e829552085476b7356b
[reactos.git] / reactos / lib / rtl / i386 / aulldiv_asm.s
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Run-Time Library
5 * FILE: lib/rtl/i386/aulldiv_asm.S
6 * PROGRAMER: Alex Ionescu (alex@relsoft.net)
7 * Eric Kohl (ekohl@rz-online.de)
8 *
9 * Copyright (C) 2002 Michael Ringgaard.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the project nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES// LOSS OF USE, DATA, OR PROFITS// OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38 .globl __aulldiv
39
40 .intel_syntax noprefix
41
42 /* FUNCTIONS ***************************************************************/
43
44 //
45 // ulldiv - unsigned long divide
46 //
47 // Purpose:
48 // Does a unsigned long divide of the arguments. Arguments are
49 // not changed.
50 //
51 // Entry:
52 // Arguments are passed on the stack:
53 // 1st pushed: divisor (QWORD)
54 // 2nd pushed: dividend (QWORD)
55 //
56 // Exit:
57 // EDX:EAX contains the quotient (dividend/divisor)
58 // NOTE: this routine removes the parameters from the stack.
59 //
60 // Uses:
61 // ECX
62 //
63
64 __aulldiv:
65
66 push ebx
67 push esi
68
69 // Set up the local stack and save the index registers. When this is done
70 // the stack frame will look as follows (assuming that the expression a/b will
71 // generate a call to uldiv(a, b)):
72 //
73 // -----------------
74 // | |
75 // |---------------|
76 // | |
77 // |--divisor (b)--|
78 // | |
79 // |---------------|
80 // | |
81 // |--dividend (a)-|
82 // | |
83 // |---------------|
84 // | return addr** |
85 // |---------------|
86 // | EBX |
87 // |---------------|
88 // ESP---->| ESI |
89 // -----------------
90 //
91
92 #undef DVNDLO
93 #undef DVNDHI
94 #undef DVSRLO
95 #undef DVSRHI
96 #define DVNDLO [esp + 12] // stack address of dividend (a)
97 #define DVNDHI [esp + 16] // stack address of dividend (a)
98 #define DVSRLO [esp + 20] // stack address of divisor (b)
99 #define DVSRHI [esp + 24] // stack address of divisor (b)
100
101 //
102 // Now do the divide. First look to see if the divisor is less than 4194304K.
103 // If so, then we can use a simple algorithm with word divides, otherwise
104 // things get a little more complex.
105 //
106
107 mov eax,DVSRHI // check to see if divisor < 4194304K
108 or eax,eax
109 jnz short ..L1 // nope, gotta do this the hard way
110 mov ecx,DVSRLO // load divisor
111 mov eax,DVNDHI // load high word of dividend
112 xor edx,edx
113 div ecx // get high order bits of quotient
114 mov ebx,eax // save high bits of quotient
115 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
116 div ecx // get low order bits of quotient
117 mov edx,ebx // edx:eax <- quotient hi:quotient lo
118 jmp short ..L2 // restore stack and return
119
120 //
121 // Here we do it the hard way. Remember, eax contains DVSRHI
122 //
123
124 ..L1:
125 mov ecx,eax // ecx:ebx <- divisor
126 mov ebx,DVSRLO
127 mov edx,DVNDHI // edx:eax <- dividend
128 mov eax,DVNDLO
129 ..L3:
130 shr ecx,1 // shift divisor right one bit// hi bit <- 0
131 rcr ebx,1
132 shr edx,1 // shift dividend right one bit// hi bit <- 0
133 rcr eax,1
134 or ecx,ecx
135 jnz short ..L3 // loop until divisor < 4194304K
136 div ebx // now divide, ignore remainder
137 mov esi,eax // save quotient
138
139 //
140 // We may be off by one, so to check, we will multiply the quotient
141 // by the divisor and check the result against the orignal dividend
142 // Note that we must also check for overflow, which can occur if the
143 // dividend is close to 2**64 and the quotient is off by 1.
144 //
145
146 mul dword ptr DVSRHI // QUOT * DVSRHI
147 mov ecx,eax
148 mov eax,DVSRLO
149 mul esi // QUOT * DVSRLO
150 add edx,ecx // EDX:EAX = QUOT * DVSR
151 jc short ..L4 // carry means Quotient is off by 1
152
153 //
154 // do long compare here between original dividend and the result of the
155 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
156 // subtract one (1) from the quotient.
157 //
158
159 cmp edx,DVNDHI // compare hi words of result and original
160 ja short ..L4 // if result > original, do subtract
161 jb short ..L5 // if result < original, we are ok
162 cmp eax,DVNDLO // hi words are equal, compare lo words
163 jbe short ..L5 // if less or equal we are ok, else subtract
164 ..L4:
165 dec esi // subtract 1 from quotient
166 ..L5:
167 xor edx,edx // edx:eax <- quotient
168 mov eax,esi
169
170 //
171 // Just the cleanup left to do. edx:eax contains the quotient.
172 // Restore the saved registers and return.
173 //
174
175 ..L2:
176
177 pop esi
178 pop ebx
179
180 ret 16