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