Separated math and memory functions into their own files to limit the amount of objec...
[reactos.git] / reactos / lib / rtl / i386 / alldvrm_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/alldvrm.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 __alldvrm
39
40 /* DATA ********************************************************************/
41
42 fzero:
43 .long 0 // Floating point zero
44 .long 0 // Floating point zero
45
46 __fltused:
47 .long 0x9875
48
49 .intel_syntax noprefix
50
51 /* FUNCTIONS ***************************************************************/
52
53 __alldvrm:
54 push edi
55 push esi
56 push ebp
57
58 // Set up the local stack and save the index registers. When this is done
59 // the stack frame will look as follows (assuming that the expression a/b will
60 // generate a call to alldvrm(a, b)):
61 //
62 // -----------------
63 // | |
64 // |---------------|
65 // | |
66 // |--divisor (b)--|
67 // | |
68 // |---------------|
69 // | |
70 // |--dividend (a)-|
71 // | |
72 // |---------------|
73 // | return addr** |
74 // |---------------|
75 // | EDI |
76 // |---------------|
77 // | ESI |
78 // |---------------|
79 // ESP---->| EBP |
80 // -----------------
81 //
82
83 #undef DVNDLO
84 #undef DVNDHI
85 #undef DVSRLO
86 #undef DVSRHI
87 #define DVNDLO [esp + 16] // stack address of dividend (a)
88 #define DVNDHI [esp + 20] // stack address of dividend (a)
89 #define DVSRLO [esp + 24] // stack address of divisor (b)
90 #define DVSRHI [esp + 28] // stack address of divisor (b)
91
92 // Determine sign of the quotient (edi = 0 if result is positive, non-zero
93 // otherwise) and make operands positive.
94 // Sign of the remainder is kept in ebp.
95
96 xor edi,edi // result sign assumed positive
97 xor ebp,ebp // result sign assumed positive
98
99 mov eax,DVNDHI // hi word of a
100 or eax,eax // test to see if signed
101 jge short ....L1 // skip rest if a is already positive
102 inc edi // complement result sign flag
103 inc ebp // complement result sign flag
104 mov edx,DVNDLO // lo word of a
105 neg eax // make a positive
106 neg edx
107 sbb eax,0
108 mov DVNDHI,eax // save positive value
109 mov DVNDLO,edx
110 ....L1:
111 mov eax,DVSRHI // hi word of b
112 or eax,eax // test to see if signed
113 jge short ....L2 // skip rest if b is already positive
114 inc edi // complement the result sign flag
115 mov edx,DVSRLO // lo word of a
116 neg eax // make b positive
117 neg edx
118 sbb eax,0
119 mov DVSRHI,eax // save positive value
120 mov DVSRLO,edx
121 ....L2:
122
123 //
124 // Now do the divide. First look to see if the divisor is less than 4194304K.
125 // If so, then we can use a simple algorithm with word divides, otherwise
126 // things get a little more complex.
127 //
128 // NOTE - eax currently contains the high order word of DVSR
129 //
130
131 or eax,eax // check to see if divisor < 4194304K
132 jnz short ....L3 // nope, gotta do this the hard way
133 mov ecx,DVSRLO // load divisor
134 mov eax,DVNDHI // load high word of dividend
135 xor edx,edx
136 div ecx // eax <- high order bits of quotient
137 mov ebx,eax // save high bits of quotient
138 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
139 div ecx // eax <- low order bits of quotient
140 mov esi,eax // ebx:esi <- quotient
141 //
142 // Now we need to do a multiply so that we can compute the remainder.
143 //
144 mov eax,ebx // set up high word of quotient
145 mul dword ptr DVSRLO // HIWORD(QUOT) * DVSR
146 mov ecx,eax // save the result in ecx
147 mov eax,esi // set up low word of quotient
148 mul dword ptr DVSRLO // LOWORD(QUOT) * DVSR
149 add edx,ecx // EDX:EAX = QUOT * DVSR
150 jmp short ....L4 // complete remainder calculation
151
152 //
153 // Here we do it the hard way. Remember, eax contains the high word of DVSR
154 //
155
156 ....L3:
157 mov ebx,eax // ebx:ecx <- divisor
158 mov ecx,DVSRLO
159 mov edx,DVNDHI // edx:eax <- dividend
160 mov eax,DVNDLO
161 ....L5:
162 shr ebx,1 // shift divisor right one bit
163 rcr ecx,1
164 shr edx,1 // shift dividend right one bit
165 rcr eax,1
166 or ebx,ebx
167 jnz short ....L5 // loop until divisor < 4194304K
168 div ecx // now divide, ignore remainder
169 mov esi,eax // save quotient
170
171 //
172 // We may be off by one, so to check, we will multiply the quotient
173 // by the divisor and check the result against the orignal dividend
174 // Note that we must also check for overflow, which can occur if the
175 // dividend is close to 2**64 and the quotient is off by 1.
176 //
177
178 mul dword ptr DVSRHI // QUOT * DVSRHI
179 mov ecx,eax
180 mov eax,DVSRLO
181 mul esi // QUOT * DVSRLO
182 add edx,ecx // EDX:EAX = QUOT * DVSR
183 jc short ....L6 // carry means Quotient is off by 1
184
185 //
186 // do long compare here between original dividend and the result of the
187 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
188 // subtract one (1) from the quotient.
189 //
190
191 cmp edx,DVNDHI // compare hi words of result and original
192 ja short ....L6 // if result > original, do subtract
193 jb short ....L7 // if result < original, we are ok
194 cmp eax,DVNDLO // hi words are equal, compare lo words
195 jbe short ....L7 // if less or equal we are ok, else subtract
196 ....L6:
197 dec esi // subtract 1 from quotient
198 sub eax,DVSRLO // subtract divisor from result
199 sbb edx,DVSRHI
200 ....L7:
201 xor ebx,ebx // ebx:esi <- quotient
202
203 ....L4:
204 //
205 // Calculate remainder by subtracting the result from the original dividend.
206 // Since the result is already in a register, we will do the subtract in the
207 // opposite direction and negate the result if necessary.
208 //
209
210 sub eax,DVNDLO // subtract dividend from result
211 sbb edx,DVNDHI
212
213 //
214 // Now check the result sign flag to see if the result is supposed to be positive
215 // or negative. It is currently negated (because we subtracted in the 'wrong'
216 // direction), so if the sign flag is set we are done, otherwise we must negate
217 // the result to make it positive again.
218 //
219
220 dec ebp // check result sign flag
221 jns short ....L9 // result is ok, set up the quotient
222 neg edx // otherwise, negate the result
223 neg eax
224 sbb edx,0
225
226 //
227 // Now we need to get the quotient into edx:eax and the remainder into ebx:ecx.
228 //
229 ....L9:
230 mov ecx,edx
231 mov edx,ebx
232 mov ebx,ecx
233 mov ecx,eax
234 mov eax,esi
235
236 //
237 // Just the cleanup left to do. edx:eax contains the quotient. Set the sign
238 // according to the save value, cleanup the stack, and return.
239 //
240
241 dec edi // check to see if result is negative
242 jnz short ....L8 // if EDI == 0, result should be negative
243 neg edx // otherwise, negate the result
244 neg eax
245 sbb edx,0
246
247 //
248 // Restore the saved registers and return.
249 //
250
251 ....L8:
252 pop ebp
253 pop esi
254 pop edi
255
256 ret 16