2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: Run-Time Library
5 * FILE: lib/rtl/i386/aullrem.S
6 * PROGRAMER: Alex Ionescu (alex@relsoft.net)
7 * Eric Kohl (ekohl@rz-online.de)
9 * Copyright (C) 2002 Michael Ringgaard.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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
40 /* DATA ********************************************************************/
43 .long 0 // Floating point zero
44 .long 0 // Floating point zero
49 .intel_syntax noprefix
51 /* FUNCTIONS ***************************************************************/
54 // ullrem - unsigned long remainder
57 // Does a unsigned long remainder of the arguments. Arguments are
61 // Arguments are passed on the stack:
62 // 1st pushed: divisor (QWORD)
63 // 2nd pushed: dividend (QWORD)
66 // EDX:EAX contains the remainder (dividend%divisor)
67 // NOTE: this routine removes the parameters from the stack.
77 // Set up the local stack and save the index registers. When this is done
78 // the stack frame will look as follows (assuming that the expression a%b will
79 // generate a call to ullrem(a, b)):
102 #define DVNDLO [esp + 8] // stack address of dividend (a)
103 #define DVNDHI [esp + 8] // stack address of dividend (a)
104 #define DVSRLO [esp + 16] // stack address of divisor (b)
105 #define DVSRHI [esp + 20] // stack address of divisor (b)
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.
112 mov eax,DVSRHI // check to see if divisor < 4194304K
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
118 div ecx // edx <- remainder, eax <- quotient
119 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
120 div ecx // edx <- final remainder
121 mov eax,edx // edx:eax <- remainder
123 jmp short ...L2 // restore stack and return
126 // Here we do it the hard way. Remember, eax contains DVSRHI
130 mov ecx,eax // ecx:ebx <- divisor
132 mov edx,DVNDHI // edx:eax <- dividend
135 shr ecx,1 // shift divisor right one bit// hi bit <- 0
137 shr edx,1 // shift dividend right one bit// hi bit <- 0
140 jnz short ...L3 // loop until divisor < 4194304K
141 div ebx // now divide, ignore remainder
144 // We may be off by one, so to check, we will multiply the quotient
145 // by the divisor and check the result against the orignal dividend
146 // Note that we must also check for overflow, which can occur if the
147 // dividend is close to 2**64 and the quotient is off by 1.
150 mov ecx,eax // save a copy of quotient in ECX
152 xchg ecx,eax // put partial product in ECX, get quotient in EAX
154 add edx,ecx // EDX:EAX = QUOT * DVSR
155 jc short ...L4 // carry means Quotient is off by 1
158 // do long compare here between original dividend and the result of the
159 // multiply in edx:eax. If original is larger or equal, we're ok, otherwise
160 // subtract the original divisor from the result.
163 cmp edx,DVNDHI // compare hi words of result and original
164 ja short ...L4 // if result > original, do subtract
165 jb short ...L5 // if result < original, we're ok
166 cmp eax,DVNDLO // hi words are equal, compare lo words
167 jbe short ...L5 // if less or equal we're ok, else subtract
169 sub eax,DVSRLO // subtract divisor from result
174 // Calculate remainder by subtracting the result from the original dividend.
175 // Since the result is already in a register, we will perform the subtract in
176 // the opposite direction and negate the result to make it positive.
179 sub eax,DVNDLO // subtract original dividend from result
181 neg edx // and negate it
186 // Just the cleanup left to do. dx:ax contains the remainder.
187 // Restore the saved registers and return.