Separated math and memory functions into their own files to limit the amount of objec...
[reactos.git] / reactos / lib / rtl / i386 / allshr_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/aulldiv.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 __aulldiv
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 // ulldiv - unsigned long divide
55 //
56 // Purpose:
57 // Does a unsigned long divide 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 quotient (dividend/divisor)
67 // NOTE: this routine removes the parameters from the stack.
68 //
69 // Uses:
70 // ECX
71 //
72
73 __aulldiv:
74
75 push ebx
76 push esi
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 uldiv(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---->| ESI |
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 //
111 // Now do the divide. First look to see if the divisor is less than 4194304K.
112 // If so, then we can use a simple algorithm with word divides, otherwise
113 // things get a little more complex.
114 //
115
116 mov eax,DVSRHI // check to see if divisor < 4194304K
117 or eax,eax
118 jnz short ..L1 // nope, gotta do this the hard way
119 mov ecx,DVSRLO // load divisor
120 mov eax,DVNDHI // load high word of dividend
121 xor edx,edx
122 div ecx // get high order bits of quotient
123 mov ebx,eax // save high bits of quotient
124 mov eax,DVNDLO // edx:eax <- remainder:lo word of dividend
125 div ecx // get low order bits of quotient
126 mov edx,ebx // edx:eax <- quotient hi:quotient lo
127 jmp short ..L2 // restore stack and return
128
129 //
130 // Here we do it the hard way. Remember, eax contains DVSRHI
131 //
132
133 ..L1:
134 mov ecx,eax // ecx:ebx <- divisor
135 mov ebx,DVSRLO
136 mov edx,DVNDHI // edx:eax <- dividend
137 mov eax,DVNDLO
138 ..L3:
139 shr ecx,1 // shift divisor right one bit// hi bit <- 0
140 rcr ebx,1
141 shr edx,1 // shift dividend right one bit// hi bit <- 0
142 rcr eax,1
143 or ecx,ecx
144 jnz short ..L3 // loop until divisor < 4194304K
145 div ebx // now divide, ignore remainder
146 mov esi,eax // save quotient
147
148 //
149 // We may be off by one, so to check, we will multiply the quotient
150 // by the divisor and check the result against the orignal dividend
151 // Note that we must also check for overflow, which can occur if the
152 // dividend is close to 2**64 and the quotient is off by 1.
153 //
154
155 mul dword ptr DVSRHI // QUOT * DVSRHI
156 mov ecx,eax
157 mov eax,DVSRLO
158 mul esi // QUOT * DVSRLO
159 add edx,ecx // EDX:EAX = QUOT * DVSR
160 jc short ..L4 // carry means Quotient is off by 1
161
162 //
163 // do long compare here between original dividend and the result of the
164 // multiply in edx:eax. If original is larger or equal, we are ok, otherwise
165 // subtract one (1) from the quotient.
166 //
167
168 cmp edx,DVNDHI // compare hi words of result and original
169 ja short ..L4 // if result > original, do subtract
170 jb short ..L5 // if result < original, we are ok
171 cmp eax,DVNDLO // hi words are equal, compare lo words
172 jbe short ..L5 // if less or equal we are ok, else subtract
173 ..L4:
174 dec esi // subtract 1 from quotient
175 ..L5:
176 xor edx,edx // edx:eax <- quotient
177 mov eax,esi
178
179 //
180 // Just the cleanup left to do. edx:eax contains the quotient.
181 // Restore the saved registers and return.
182 //
183
184 ..L2:
185
186 pop esi
187 pop ebx
188
189 ret 16