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