Separated math and memory functions into their own files to limit the amount of objec...
[reactos.git] / reactos / lib / rtl / 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 * 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 __aulldvrm
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 __aulldvrm:
54
55 // ulldvrm - unsigned long divide and remainder
56 //
57 // Purpose:
58 // Does a unsigned long divide and remainder of the arguments. Arguments
59 // are not changed.
60 //
61 // Entry:
62 // Arguments are passed on the stack:
63 // 1st pushed: divisor (QWORD)
64 // 2nd pushed: dividend (QWORD)
65 //
66 // Exit:
67 // EDX:EAX contains the quotient (dividend/divisor)
68 // EBX:ECX contains the remainder (divided % divisor)
69 // NOTE: this routine removes the parameters from the stack.
70 //
71 // Uses:
72 // ECX
73 //
74 push esi
75
76 // Set up the local stack and save the index registers. When this is done
77 // the stack frame will look as follows (assuming that the expression a/b will
78 // generate a call to aulldvrm(a, b)):
79 //
80 // -----------------
81 // | |
82 // |---------------|
83 // | |
84 // |--divisor (b)--|
85 // | |
86 // |---------------|
87 // | |
88 // |--dividend (a)-|
89 // | |
90 // |---------------|
91 // | return addr** |
92 // |---------------|
93 // ESP---->| ESI |
94 // -----------------
95 //
96
97 #undef DVNDLO
98 #undef DVNDHI
99 #undef DVSRLO
100 #undef DVSRHI
101 #define DVNDLO [esp + 8] // stack address of dividend (a)
102 #define DVNDHI [esp + 8] // stack address of dividend (a)
103 #define DVSRLO [esp + 16] // stack address of divisor (b)
104 #define DVSRHI [esp + 20] // stack address of divisor (b)
105
106 //
107 // Now do the divide. First look to see if the divisor is less than 4194304K.
108 // If so, then we can use a simple algorithm with word divides, otherwise
109 // things get a little more complex.
110 //
111
112 mov eax,DVSRHI // check to see if divisor < 4194304K
113 or eax,eax
114 jnz short .....L1 // nope, gotta do this the hard way
115 mov ecx,DVSRLO // load divisor
116 mov eax,DVNDHI // load high word of dividend
117 xor edx,edx
118 div ecx // get high order bits of quotient
119 mov ebx,eax // save high bits of quotient
120 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
121 div ecx // get low order bits of quotient
122 mov esi,eax // ebx:esi <- quotient
123
124 //
125 // Now we need to do a multiply so that we can compute the remainder.
126 //
127 mov eax,ebx // set up high word of quotient
128 mul dword ptr DVSRLO // HIWORD(QUOT) * DVSR
129 mov ecx,eax // save the result in ecx
130 mov eax,esi // set up low word of quotient
131 mul dword ptr DVSRLO // LOWORD(QUOT) * DVSR
132 add edx,ecx // EDX:EAX = QUOT * DVSR
133 jmp short .....L2 // complete remainder calculation
134
135 //
136 // Here we do it the hard way. Remember, eax contains DVSRHI
137 //
138
139 .....L1:
140 mov ecx,eax // ecx:ebx <- divisor
141 mov ebx,DVSRLO
142 mov edx,DVNDHI // edx:eax <- dividend
143 mov eax,DVNDLO
144 .....L3:
145 shr ecx,1 // shift divisor right one bit// hi bit <- 0
146 rcr ebx,1
147 shr edx,1 // shift dividend right one bit// hi bit <- 0
148 rcr eax,1
149 or ecx,ecx
150 jnz short .....L3 // loop until divisor < 4194304K
151 div ebx // now divide, ignore remainder
152 mov esi,eax // save quotient
153
154 //
155 // We may be off by one, so to check, we will multiply the quotient
156 // by the divisor and check the result against the orignal dividend
157 // Note that we must also check for overflow, which can occur if the
158 // dividend is close to 2**64 and the quotient is off by 1.
159 //
160
161 mul dword ptr DVSRHI // QUOT * DVSRHI
162 mov ecx,eax
163 mov eax,DVSRLO
164 mul esi // QUOT * DVSRLO
165 add edx,ecx // EDX:EAX = QUOT * DVSR
166 jc short .....L4 // carry means Quotient is off by 1
167
168 //
169 // do long compare here between original dividend and the result of the
170 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
171 // subtract one (1) from the quotient.
172 //
173
174 cmp edx,DVNDHI // compare hi words of result and original
175 ja short .....L4 // if result > original, do subtract
176 jb short .....L5 // if result < original, we are ok
177 cmp eax,DVNDLO // hi words are equal, compare lo words
178 jbe short .....L5 // if less or equal we are ok, else subtract
179 .....L4:
180 dec esi // subtract 1 from quotient
181 sub eax,DVSRLO // subtract divisor from result
182 sbb edx,DVSRHI
183 .....L5:
184 xor ebx,ebx // ebx:esi <- quotient
185
186 .....L2:
187 //
188 // Calculate remainder by subtracting the result from the original dividend.
189 // Since the result is already in a register, we will do the subtract in the
190 // opposite direction and negate the result.
191 //
192
193 sub eax,DVNDLO // subtract dividend from result
194 sbb edx,DVNDHI
195 neg edx // otherwise, negate the result
196 neg eax
197 sbb edx,0
198
199 //
200 // Now we need to get the quotient into edx:eax and the remainder into ebx:ecx.
201 //
202 mov ecx,edx
203 mov edx,ebx
204 mov ebx,ecx
205 mov ecx,eax
206 mov eax,esi
207 //
208 // Just the cleanup left to do. edx:eax contains the quotient.
209 // Restore the saved registers and return.
210 //
211
212 pop esi
213
214 ret 16