Separated math and memory functions into their own files to limit the amount of objec...
[reactos.git] / reactos / lib / rtl / i386 / allrem_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/allrem.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 __allrem
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 //
54 // llrem - signed long remainder
55 //
56 // Purpose:
57 // Does a signed long remainder 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 remainder (dividend%divisor)
67 // NOTE: this routine removes the parameters from the stack.
68 //
69 // Uses:
70 // ECX
71 //
72
73 __allrem :
74
75 push ebx
76 push edi
77
78 // Set up the local stack and save the index registers. When this is done
79 // the stack frame will look as follows (assuming that the expression a%b will
80 // generate a call to lrem(a, b)):
81 //
82 // -----------------
83 // | |
84 // |---------------|
85 // | |
86 // |--divisor (b)--|
87 // | |
88 // |---------------|
89 // | |
90 // |--dividend (a)-|
91 // | |
92 // |---------------|
93 // | return addr** |
94 // |---------------|
95 // | EBX |
96 // |---------------|
97 // ESP---->| EDI |
98 // -----------------
99 //
100
101 #undef DVNDLO
102 #undef DVNDHI
103 #undef DVSRLO
104 #undef DVSRHI
105 #define DVNDLO [esp + 12] // stack address of dividend (a)
106 #define DVNDHI [esp + 16] // stack address of dividend (a)
107 #define DVSRLO [esp + 20] // stack address of divisor (b)
108 #define DVSRHI [esp + 24] // stack address of divisor (b)
109
110 // Determine sign of the result (edi = 0 if result is positive, non-zero
111 // otherwise) and make operands positive.
112
113 xor edi,edi // result sign assumed positive
114
115 mov eax,DVNDHI // hi word of a
116 or eax,eax // test to see if signed
117 jge short .L1 // skip rest if a is already positive
118 inc edi // complement result sign flag bit
119 mov edx,DVNDLO // lo word of a
120 neg eax // make a positive
121 neg edx
122 sbb eax,0
123 mov DVNDHI,eax // save positive value
124 mov DVNDLO,edx
125 .L1:
126 mov eax,DVSRHI // hi word of b
127 or eax,eax // test to see if signed
128 jge short .L2 // skip rest if b is already positive
129 mov edx,DVSRLO // lo word of b
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 // edx <- remainder
151 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
152 div ecx // edx <- final remainder
153 mov eax,edx // edx:eax <- remainder
154 xor edx,edx
155 dec edi // check result sign flag
156 jns short .L4 // negate result, restore stack and return
157 jmp short .L8 // result sign ok, restore stack and return
158
159 //
160 // Here we do it the hard way. Remember, eax contains the high word of DVSR
161 //
162
163 .L3:
164 mov ebx,eax // ebx:ecx <- divisor
165 mov ecx,DVSRLO
166 mov edx,DVNDHI // edx:eax <- dividend
167 mov eax,DVNDLO
168 .L5:
169 shr ebx,1 // shift divisor right one bit
170 rcr ecx,1
171 shr edx,1 // shift dividend right one bit
172 rcr eax,1
173 or ebx,ebx
174 jnz short .L5 // loop until divisor < 4194304K
175 div ecx // now divide, ignore remainder
176
177 //
178 // We may be off by one, so to check, we will multiply the quotient
179 // by the divisor and check the result against the orignal dividend
180 // Note that we must also check for overflow, which can occur if the
181 // dividend is close to 2**64 and the quotient is off by 1.
182 //
183
184 mov ecx,eax // save a copy of quotient in ECX
185 mul dword ptr DVSRHI
186 xchg ecx,eax // save product, get quotient in EAX
187 mul dword ptr DVSRLO
188 add edx,ecx // EDX:EAX = QUOT * DVSR
189 jc short .L6 // carry means Quotient is off by 1
190
191 //
192 // do long compare here between original dividend and the result of the
193 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
194 // subtract the original divisor from the result.
195 //
196
197 cmp edx,DVNDHI // compare hi words of result and original
198 ja short .L6 // if result > original, do subtract
199 jb short .L7 // if result < original, we are ok
200 cmp eax,DVNDLO // hi words are equal, compare lo words
201 jbe short .L7 // if less or equal we are ok, else subtract
202 .L6:
203 sub eax,DVSRLO // subtract divisor from result
204 sbb edx,DVSRHI
205 .L7:
206
207 //
208 // Calculate remainder by subtracting the result from the original dividend.
209 // Since the result is already in a register, we will do the subtract in the
210 // opposite direction and negate the result if necessary.
211 //
212
213 sub eax,DVNDLO // subtract dividend from result
214 sbb edx,DVNDHI
215
216 //
217 // Now check the result sign flag to see if the result is supposed to be positive
218 // or negative. It is currently negated (because we subtracted in the 'wrong'
219 // direction), so if the sign flag is set we are done, otherwise we must negate
220 // the result to make it positive again.
221 //
222
223 dec edi // check result sign flag
224 jns short .L8 // result is ok, restore stack and return
225 .L4:
226 neg edx // otherwise, negate the result
227 neg eax
228 sbb edx,0
229
230 //
231 // Just the cleanup left to do. edx:eax contains the quotient.
232 // Restore the saved registers and return.
233 //
234
235 .L8:
236 pop edi
237 pop ebx
238
239 ret 16