2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Run-Time Library
5 * FILE: lib/sdk/crt/math/i386/allrem_asm.s
6 * PROGRAMER: Alex Ionescu (alex@relsoft.net)
8 * Copyright (C) 2002 Michael Ringgaard.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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
41 /* FUNCTIONS ***************************************************************/
45 // llrem - signed long remainder
48 // Does a signed long remainder of the arguments. Arguments are
52 // Arguments are passed on the stack:
53 // 1st pushed: divisor (QWORD)
54 // 2nd pushed: dividend (QWORD)
57 // EDX:EAX contains the remainder (dividend%divisor)
58 // NOTE: this routine removes the parameters from the stack.
69 // Set up the local stack and save the index registers. When this is done
70 // the stack frame will look as follows (assuming that the expression a%b will
71 // generate a call to lrem(a, b)):
96 #define DVNDLO [esp + 12] // stack address of dividend (a)
97 #define DVNDHI [esp + 16] // stack address of dividend (a)
98 #define DVSRLO [esp + 20] // stack address of divisor (b)
99 #define DVSRHI [esp + 24] // stack address of divisor (b)
101 // Determine sign of the result (edi = 0 if result is positive, non-zero
102 // otherwise) and make operands positive.
104 xor edi,edi // result sign assumed positive
106 mov eax,DVNDHI // hi word of a
107 or eax,eax // test to see if signed
108 jge short .L1 // skip rest if a is already positive
109 inc edi // complement result sign flag bit
110 mov edx,DVNDLO // lo word of a
111 neg eax // make a positive
114 mov DVNDHI,eax // save positive value
117 mov eax,DVSRHI // hi word of b
118 or eax,eax // test to see if signed
119 jge short .L2 // skip rest if b is already positive
120 mov edx,DVSRLO // lo word of b
121 neg eax // make b positive
124 mov DVSRHI,eax // save positive value
129 // Now do the divide. First look to see if the divisor is less than 4194304K.
130 // If so, then we can use a simple algorithm with word divides, otherwise
131 // things get a little more complex.
133 // NOTE - eax currently contains the high order word of DVSR
136 or eax,eax // check to see if divisor < 4194304K
137 jnz short .L3 // nope, gotta do this the hard way
138 mov ecx,DVSRLO // load divisor
139 mov eax,DVNDHI // load high word of dividend
141 div ecx // edx <- remainder
142 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
143 div ecx // edx <- final remainder
144 mov eax,edx // edx:eax <- remainder
146 dec edi // check result sign flag
147 jns short .L4 // negate result, restore stack and return
148 jmp short .L8 // result sign ok, restore stack and return
151 // Here we do it the hard way. Remember, eax contains the high word of DVSR
155 mov ebx,eax // ebx:ecx <- divisor
157 mov edx,DVNDHI // edx:eax <- dividend
160 shr ebx,1 // shift divisor right one bit
162 shr edx,1 // shift dividend right one bit
165 jnz short .L5 // loop until divisor < 4194304K
166 div ecx // now divide, ignore remainder
169 // We may be off by one, so to check, we will multiply the quotient
170 // by the divisor and check the result against the orignal dividend
171 // Note that we must also check for overflow, which can occur if the
172 // dividend is close to 2**64 and the quotient is off by 1.
175 mov ecx,eax // save a copy of quotient in ECX
177 xchg ecx,eax // save product, get quotient in EAX
179 add edx,ecx // EDX:EAX = QUOT * DVSR
180 jc short .L6 // carry means Quotient is off by 1
183 // do long compare here between original dividend and the result of the
184 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
185 // subtract the original divisor from the result.
188 cmp edx,DVNDHI // compare hi words of result and original
189 ja short .L6 // if result > original, do subtract
190 jb short .L7 // if result < original, we are ok
191 cmp eax,DVNDLO // hi words are equal, compare lo words
192 jbe short .L7 // if less or equal we are ok, else subtract
194 sub eax,DVSRLO // subtract divisor from result
199 // Calculate remainder by subtracting the result from the original dividend.
200 // Since the result is already in a register, we will do the subtract in the
201 // opposite direction and negate the result if necessary.
204 sub eax,DVNDLO // subtract dividend from result
208 // Now check the result sign flag to see if the result is supposed to be positive
209 // or negative. It is currently negated (because we subtracted in the 'wrong'
210 // direction), so if the sign flag is set we are done, otherwise we must negate
211 // the result to make it positive again.
214 dec edi // check result sign flag
215 jns short .L8 // result is ok, restore stack and return
217 neg edx // otherwise, negate the result
222 // Just the cleanup left to do. edx:eax contains the quotient.
223 // Restore the saved registers and return.