[CRT]
[reactos.git] / lib / sdk / crt / math / i386 / alldiv_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/alldiv_asm.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 #include <reactos/asm.h>
38
39 PUBLIC __alldiv
40 PUBLIC __fltused
41
42 /* DATA ********************************************************************/
43 .data
44 ASSUME CS:NOTHING, DS:NOTHING, ES:NOTHING, FS:NOTHING, GS:NOTHING
45
46 __fltused:
47 .long HEX(9875)
48
49
50 /* FUNCTIONS ***************************************************************/
51 .code
52
53 //
54 // lldiv - signed long divide
55 //
56 // Purpose:
57 // Does a signed long divide of the arguments. Arguments are
58 // not changed.
59 //
60 // Entry:
61 // Arguments are passed on the stack:
62 // 1st pushed: divisor (QWORD)
63 // 2nd pushed: dividend (QWORD)
64 //
65 // Exit:
66 // EDX:EAX contains the quotient (dividend/divisor)
67 // NOTE: this routine removes the parameters from the stack.
68 //
69 // Uses:
70 // ECX
71 //
72
73 __alldiv:
74
75 push edi
76 push esi
77 push ebx
78
79 // Set up the local stack and save the index registers. When this is done
80 // the stack frame will look as follows (assuming that the expression a/b will
81 // generate a call to lldiv(a, b)):
82 //
83 // -----------------
84 // | |
85 // |---------------|
86 // | |
87 // |--divisor (b)--|
88 // | |
89 // |---------------|
90 // | |
91 // |--dividend (a)-|
92 // | |
93 // |---------------|
94 // | return addr** |
95 // |---------------|
96 // | EDI |
97 // |---------------|
98 // | ESI |
99 // |---------------|
100 // ESP---->| EBX |
101 // -----------------
102 //
103
104 #define DVNDLO [esp + 16] // stack address of dividend (a)
105 #define DVNDHI [esp + 20] // stack address of dividend (a)
106 #define DVSRLO [esp + 24] // stack address of divisor (b)
107 #define DVSRHI [esp + 28] // stack address of divisor (b)
108
109 // Determine sign of the result (edi = 0 if result is positive, non-zero
110 // otherwise) and make operands positive.
111
112 xor edi,edi // result sign assumed positive
113
114 mov eax,DVNDHI // hi word of a
115 or eax,eax // test to see if signed
116 jge short L1 // skip rest if a is already positive
117 inc edi // complement result sign flag
118 mov edx,DVNDLO // lo word of a
119 neg eax // make a positive
120 neg edx
121 sbb eax,0
122 mov DVNDHI,eax // save positive value
123 mov DVNDLO,edx
124 L1:
125 mov eax,DVSRHI // hi word of b
126 or eax,eax // test to see if signed
127 jge short L2 // skip rest if b is already positive
128 inc edi // complement the result sign flag
129 mov edx,DVSRLO // lo word of a
130 neg eax // make b positive
131 neg edx
132 sbb eax,0
133 mov DVSRHI,eax // save positive value
134 mov DVSRLO,edx
135 L2:
136
137 //
138 // Now do the divide. First look to see if the divisor is less than 4194304K.
139 // If so, then we can use a simple algorithm with word divides, otherwise
140 // things get a little more complex.
141 //
142 // NOTE - eax currently contains the high order word of DVSR
143 //
144
145 or eax,eax // check to see if divisor < 4194304K
146 jnz short L3 // nope, gotta do this the hard way
147 mov ecx,DVSRLO // load divisor
148 mov eax,DVNDHI // load high word of dividend
149 xor edx,edx
150 div ecx // eax <- high order bits of quotient
151 mov ebx,eax // save high bits of quotient
152 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
153 div ecx // eax <- low order bits of quotient
154 mov edx,ebx // edx:eax <- quotient
155 jmp short L4 // set sign, restore stack and return
156
157 //
158 // Here we do it the hard way. Remember, eax contains the high word of DVSR
159 //
160
161 L3:
162 mov ebx,eax // ebx:ecx <- divisor
163 mov ecx,DVSRLO
164 mov edx,DVNDHI // edx:eax <- dividend
165 mov eax,DVNDLO
166 L5:
167 shr ebx,1 // shift divisor right one bit
168 rcr ecx,1
169 shr edx,1 // shift dividend right one bit
170 rcr eax,1
171 or ebx,ebx
172 jnz short L5 // loop until divisor < 4194304K
173 div ecx // now divide, ignore remainder
174 mov esi,eax // save quotient
175
176 //
177 // We may be off by one, so to check, we will multiply the quotient
178 // by the divisor and check the result against the orignal dividend
179 // Note that we must also check for overflow, which can occur if the
180 // dividend is close to 2**64 and the quotient is off by 1.
181 //
182
183 mul dword ptr DVSRHI // QUOT * DVSRHI
184 mov ecx,eax
185 mov eax,DVSRLO
186 mul esi // QUOT * DVSRLO
187 add edx,ecx // EDX:EAX = QUOT * DVSR
188 jc short L6 // carry means Quotient is off by 1
189
190 //
191 // do long compare here between original dividend and the result of the
192 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
193 // subtract one (1) from the quotient.
194 //
195
196 cmp edx,DVNDHI // compare hi words of result and original
197 ja short L6 // if result > original, do subtract
198 jb short L7 // if result < original, we are ok
199 cmp eax,DVNDLO // hi words are equal, compare lo words
200 jbe short L7 // if less or equal we are ok, else subtract
201 L6:
202 dec esi // subtract 1 from quotient
203 L7:
204 xor edx,edx // edx:eax <- quotient
205 mov eax,esi
206
207 //
208 // Just the cleanup left to do. edx:eax contains the quotient. Set the sign
209 // according to the save value, cleanup the stack, and return.
210 //
211
212 L4:
213 dec edi // check to see if result is negative
214 jnz short L8 // if EDI == 0, result should be negative
215 neg edx // otherwise, negate the result
216 neg eax
217 sbb edx,0
218
219 //
220 // Restore the saved registers and return.
221 //
222
223 L8:
224 pop ebx
225 pop esi
226 pop edi
227
228 ret 16
229
230 END