2 /*---------------------------------------------------------------------------+
5 | Division subroutine for 96 bit quantities |
7 | Copyright (C) 1994,1995 |
8 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
9 | Australia. E-mail billm@jacobi.maths.monash.edu.au |
12 +---------------------------------------------------------------------------*/
14 /*---------------------------------------------------------------------------+
15 | Divide the 96 bit quantity pointed to by a, by that pointed to by b, and |
16 | put the 96 bit result at the location d. |
18 | The result may not be accurate to 96 bits. It is intended for use where |
19 | a result better than 64 bits is required. The result should usually be |
20 | good to at least 94 bits. |
21 | The returned result is actually divided by one half. This is done to |
24 | .aaaaaaaaaaaaaa / .bbbbbbbbbbbbb -> .dddddddddddd |
26 | void div_Xsig(Xsig *a, Xsig *b, Xsig *dest) |
28 +---------------------------------------------------------------------------*/
30 #include "exception.h"
39 #ifndef NON_REENTRANT_FPU
41 Local storage on the stack:
42 Accumulator: FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
44 #define FPU_accum_3 -4(%ebp)
45 #define FPU_accum_2 -8(%ebp)
46 #define FPU_accum_1 -12(%ebp)
47 #define FPU_accum_0 -16(%ebp)
48 #define FPU_result_3 -20(%ebp)
49 #define FPU_result_2 -24(%ebp)
50 #define FPU_result_1 -28(%ebp)
55 Local storage in a static area:
56 Accumulator: FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
73 #endif /* NON_REENTRANT_FPU */
80 #ifndef NON_REENTRANT_FPU
82 #endif /* NON_REENTRANT_FPU */
88 movl PARAM1,%esi /* pointer to num */
89 movl PARAM2,%ebx /* pointer to denom */
92 testl $0x80000000, XsigH(%ebx) /* Divisor */
97 /*---------------------------------------------------------------------------+
98 | Divide: Return arg1/arg2 to arg3. |
100 | The maximum returned value is (ignoring exponents) |
101 | .ffffffff ffffffff |
102 | ------------------ = 1.ffffffff fffffffe |
103 | .80000000 00000000 |
104 | and the minimum is |
105 | .80000000 00000000 |
106 | ------------------ = .80000000 00000001 (rounded) |
107 | .ffffffff ffffffff |
109 +---------------------------------------------------------------------------*/
111 /* Save extended dividend in local register */
113 /* Divide by 2 to prevent overflow */
115 movl XsigH(%esi),%eax
117 movl %eax,FPU_accum_3
118 movl XsigL(%esi),%eax
120 movl %eax,FPU_accum_2
121 movl XsigLL(%esi),%eax
123 movl %eax,FPU_accum_1
126 movl %eax,FPU_accum_0
128 movl FPU_accum_2,%eax /* Get the current num */
129 movl FPU_accum_3,%edx
131 /*----------------------------------------------------------------------*/
132 /* Initialization done.
133 Do the first 32 bits. */
135 /* We will divide by a number which is too large */
136 movl XsigH(%ebx),%ecx
140 /* here we need to divide by 100000000h,
141 i.e., no division at all.. */
146 divl %ecx /* Divide the numerator by the augmented
150 movl %eax,FPU_result_3 /* Put the result in the answer */
152 mull XsigH(%ebx) /* mul by the ms dw of the denom */
154 subl %eax,FPU_accum_2 /* Subtract from the num local reg */
155 sbbl %edx,FPU_accum_3
157 movl FPU_result_3,%eax /* Get the result back */
158 mull XsigL(%ebx) /* now mul the ls dw of the denom */
160 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
161 sbbl %edx,FPU_accum_2
163 je LDo_2nd_32_bits /* Must check for non-zero result here */
167 #endif /* PARANOID */
169 /* need to subtract another once of the denom */
170 incl FPU_result_3 /* Correct the answer */
172 movl XsigL(%ebx),%eax
173 movl XsigH(%ebx),%edx
174 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
175 sbbl %edx,FPU_accum_2
179 jne L_bugged_1 /* Must check for non-zero result here */
180 #endif /* PARANOID */
182 /*----------------------------------------------------------------------*/
183 /* Half of the main problem is done, there is just a reduced numerator
185 Work with the second 32 bits, FPU_accum_0 not used from now on */
187 movl FPU_accum_2,%edx /* get the reduced num */
188 movl FPU_accum_1,%eax
190 /* need to check for possible subsequent overflow */
191 cmpl XsigH(%ebx),%edx
193 ja LPrevent_2nd_overflow
195 cmpl XsigL(%ebx),%eax
198 LPrevent_2nd_overflow:
199 /* The numerator is greater or equal, would cause overflow */
200 /* prevent overflow */
201 subl XsigL(%ebx),%eax
202 sbbl XsigH(%ebx),%edx
203 movl %edx,FPU_accum_2
204 movl %eax,FPU_accum_1
206 incl FPU_result_3 /* Reflect the subtraction in the answer */
209 je L_bugged_2 /* Can't bump the result to 1.0 */
210 #endif /* PARANOID */
213 cmpl $0,%ecx /* augmented denom msw */
214 jnz LSecond_div_not_1
216 /* %ecx == 0, we are dividing by 1.0 */
221 divl %ecx /* Divide the numerator by the denom ms dw */
224 movl %eax,FPU_result_2 /* Put the result in the answer */
226 mull XsigH(%ebx) /* mul by the ms dw of the denom */
228 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
229 sbbl %edx,FPU_accum_2
233 #endif /* PARANOID */
235 movl FPU_result_2,%eax /* Get the result back */
236 mull XsigL(%ebx) /* now mul the ls dw of the denom */
238 subl %eax,FPU_accum_0 /* Subtract from the num local reg */
239 sbbl %edx,FPU_accum_1 /* Subtract from the num local reg */
244 #endif /* PARANOID */
251 #endif /* PARANOID */
253 /* need to subtract another once of the denom */
254 movl XsigL(%ebx),%eax
255 movl XsigH(%ebx),%edx
256 subl %eax,FPU_accum_0 /* Subtract from the num local reg */
257 sbbl %edx,FPU_accum_1
263 #endif /* PARANOID */
265 addl $1,FPU_result_2 /* Correct the answer */
269 jc L_bugged_2 /* Must check for non-zero result here */
270 #endif /* PARANOID */
272 /*----------------------------------------------------------------------*/
273 /* The division is essentially finished here, we just need to perform
275 Deal with the 3rd 32 bits */
277 /* We use an approximation for the third 32 bits.
278 To take account of the 3rd 32 bits of the divisor
279 (call them del), we subtract del * (a/b) */
281 movl FPU_result_3,%eax /* a/b */
282 mull XsigLL(%ebx) /* del */
284 subl %edx,FPU_accum_1
286 /* A borrow indicates that the result is negative */
289 movl XsigH(%ebx),%edx
290 addl %edx,FPU_accum_1
292 subl $1,FPU_result_2 /* Adjust the answer */
295 /* The above addition might not have been enough, check again. */
296 movl FPU_accum_1,%edx /* get the reduced num */
297 cmpl XsigH(%ebx),%edx /* denom */
300 movl XsigH(%ebx),%edx
301 addl %edx,FPU_accum_1
303 subl $1,FPU_result_2 /* Adjust the answer */
308 movl FPU_accum_1,%edx /* get the reduced num */
310 /* need to check for possible subsequent overflow */
311 cmpl XsigH(%ebx),%edx /* denom */
314 /* prevent overflow */
315 subl XsigH(%ebx),%edx
316 movl %edx,FPU_accum_1
318 addl $1,FPU_result_2 /* Reflect the subtraction in the answer */
322 movl FPU_accum_0,%eax
323 movl FPU_accum_1,%edx
326 movl %eax,FPU_result_1 /* Rough estimate of third word */
328 movl PARAM3,%esi /* pointer to answer */
330 movl FPU_result_1,%eax
331 movl %eax,XsigLL(%esi)
332 movl FPU_result_2,%eax
333 movl %eax,XsigL(%esi)
334 movl FPU_result_3,%eax
335 movl %eax,XsigH(%esi)
347 /* The logic is wrong if we got here */
349 pushl EX_INTERNAL|0x240
355 pushl EX_INTERNAL|0x241
361 pushl EX_INTERNAL|0x242
365 #endif /* PARANOID */