bfcb0efb2a04f859fa81649d71b1a77c2cafb9c8
[reactos.git] / reactos / lib / sdk / crt / math / i386 / aullrem_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/aullrem.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 __aullrem
38
39 .intel_syntax noprefix
40
41 /* FUNCTIONS ***************************************************************/
42
43 //
44 // ullrem - unsigned long remainder
45 //
46 // Purpose:
47 // Does a unsigned long remainder of the arguments. Arguments are
48 // not changed.
49 //
50 // Entry:
51 // Arguments are passed on the stack:
52 // 1st pushed: divisor (QWORD)
53 // 2nd pushed: dividend (QWORD)
54 //
55 // Exit:
56 // EDX:EAX contains the remainder (dividend%divisor)
57 // NOTE: this routine removes the parameters from the stack.
58 //
59 // Uses:
60 // ECX
61 //
62
63 __aullrem:
64
65 push ebx
66
67 // Set up the local stack and save the index registers. When this is done
68 // the stack frame will look as follows (assuming that the expression a%b will
69 // generate a call to ullrem(a, b)):
70 //
71 // -----------------
72 // | |
73 // |---------------|
74 // | |
75 // |--divisor (b)--|
76 // | |
77 // |---------------|
78 // | |
79 // |--dividend (a)-|
80 // | |
81 // |---------------|
82 // | return addr** |
83 // |---------------|
84 // ESP---->| EBX |
85 // -----------------
86 //
87
88 #undef DVNDLO
89 #undef DVNDHI
90 #undef DVSRLO
91 #undef DVSRHI
92 #define DVNDLO [esp + 8] // stack address of dividend (a)
93 #define DVNDHI [esp + 12] // stack address of dividend (a)
94 #define DVSRLO [esp + 16] // stack address of divisor (b)
95 #define DVSRHI [esp + 20] // stack address of divisor (b)
96
97 // Now do the divide. First look to see if the divisor is less than 4194304K.
98 // If so, then we can use a simple algorithm with word divides, otherwise
99 // things get a little more complex.
100 //
101
102 mov eax,DVSRHI // check to see if divisor < 4194304K
103 or eax,eax
104 jnz short ...L1 // nope, gotta do this the hard way
105 mov ecx,DVSRLO // load divisor
106 mov eax,DVNDHI // load high word of dividend
107 xor edx,edx
108 div ecx // edx <- remainder, eax <- quotient
109 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
110 div ecx // edx <- final remainder
111 mov eax,edx // edx:eax <- remainder
112 xor edx,edx
113 jmp short ...L2 // restore stack and return
114
115 //
116 // Here we do it the hard way. Remember, eax contains DVSRHI
117 //
118
119 ...L1:
120 mov ecx,eax // ecx:ebx <- divisor
121 mov ebx,DVSRLO
122 mov edx,DVNDHI // edx:eax <- dividend
123 mov eax,DVNDLO
124 ...L3:
125 shr ecx,1 // shift divisor right one bit// hi bit <- 0
126 rcr ebx,1
127 shr edx,1 // shift dividend right one bit// hi bit <- 0
128 rcr eax,1
129 or ecx,ecx
130 jnz short ...L3 // loop until divisor < 4194304K
131 div ebx // now divide, ignore remainder
132
133 //
134 // We may be off by one, so to check, we will multiply the quotient
135 // by the divisor and check the result against the orignal dividend
136 // Note that we must also check for overflow, which can occur if the
137 // dividend is close to 2**64 and the quotient is off by 1.
138 //
139
140 mov ecx,eax // save a copy of quotient in ECX
141 mul dword ptr DVSRHI
142 xchg ecx,eax // put partial product in ECX, get quotient in EAX
143 mul dword ptr DVSRLO
144 add edx,ecx // EDX:EAX = QUOT * DVSR
145 jc short ...L4 // carry means Quotient is off by 1
146
147 //
148 // do long compare here between original dividend and the result of the
149 // multiply in edx:eax. If original is larger or equal, we're ok, otherwise
150 // subtract the original divisor from the result.
151 //
152
153 cmp edx,DVNDHI // compare hi words of result and original
154 ja short ...L4 // if result > original, do subtract
155 jb short ...L5 // if result < original, we're ok
156 cmp eax,DVNDLO // hi words are equal, compare lo words
157 jbe short ...L5 // if less or equal we're ok, else subtract
158 ...L4:
159 sub eax,DVSRLO // subtract divisor from result
160 sbb edx,DVSRHI
161 ...L5:
162
163 //
164 // Calculate remainder by subtracting the result from the original dividend.
165 // Since the result is already in a register, we will perform the subtract in
166 // the opposite direction and negate the result to make it positive.
167 //
168
169 sub eax,DVNDLO // subtract original dividend from result
170 sbb edx,DVNDHI
171 neg edx // and negate it
172 neg eax
173 sbb edx,0
174
175 //
176 // Just the cleanup left to do. dx:ax contains the remainder.
177 // Restore the saved registers and return.
178 //
179
180 ...L2:
181
182 pop ebx
183
184 ret 16