1 /* SPDX-License-Identifier: GPL-2.0 */
3 /*---------------------------------------------------------------------------+
6 | Division subroutine for 96 bit quantities |
8 | Copyright (C) 1994,1995 |
9 | W. Metzenthen, 22 Parker St, Ormond, Vic 3163, |
10 | Australia. E-mail billm@jacobi.maths.monash.edu.au |
13 +---------------------------------------------------------------------------*/
15 /*---------------------------------------------------------------------------+
16 | Divide the 96 bit quantity pointed to by a, by that pointed to by b, and |
17 | put the 96 bit result at the location d. |
19 | The result may not be accurate to 96 bits. It is intended for use where |
20 | a result better than 64 bits is required. The result should usually be |
21 | good to at least 94 bits. |
22 | The returned result is actually divided by one half. This is done to |
25 | .aaaaaaaaaaaaaa / .bbbbbbbbbbbbb -> .dddddddddddd |
27 | void div_Xsig(Xsig *a, Xsig *b, Xsig *dest) |
29 +---------------------------------------------------------------------------*/
31 #include "exception.h"
40 #ifndef NON_REENTRANT_FPU
42 Local storage on the stack:
43 Accumulator: FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
45 #define FPU_accum_3 -4(%ebp)
46 #define FPU_accum_2 -8(%ebp)
47 #define FPU_accum_1 -12(%ebp)
48 #define FPU_accum_0 -16(%ebp)
49 #define FPU_result_3 -20(%ebp)
50 #define FPU_result_2 -24(%ebp)
51 #define FPU_result_1 -28(%ebp)
56 Local storage in a static area:
57 Accumulator: FPU_accum_3:FPU_accum_2:FPU_accum_1:FPU_accum_0
74 #endif /* NON_REENTRANT_FPU */
81 #ifndef NON_REENTRANT_FPU
83 #endif /* NON_REENTRANT_FPU */
89 movl PARAM1,%esi /* pointer to num */
90 movl PARAM2,%ebx /* pointer to denom */
93 testl $0x80000000, XsigH(%ebx) /* Divisor */
98 /*---------------------------------------------------------------------------+
99 | Divide: Return arg1/arg2 to arg3. |
101 | The maximum returned value is (ignoring exponents) |
102 | .ffffffff ffffffff |
103 | ------------------ = 1.ffffffff fffffffe |
104 | .80000000 00000000 |
105 | and the minimum is |
106 | .80000000 00000000 |
107 | ------------------ = .80000000 00000001 (rounded) |
108 | .ffffffff ffffffff |
110 +---------------------------------------------------------------------------*/
112 /* Save extended dividend in local register */
114 /* Divide by 2 to prevent overflow */
116 movl XsigH(%esi),%eax
118 movl %eax,FPU_accum_3
119 movl XsigL(%esi),%eax
121 movl %eax,FPU_accum_2
122 movl XsigLL(%esi),%eax
124 movl %eax,FPU_accum_1
127 movl %eax,FPU_accum_0
129 movl FPU_accum_2,%eax /* Get the current num */
130 movl FPU_accum_3,%edx
132 /*----------------------------------------------------------------------*/
133 /* Initialization done.
134 Do the first 32 bits. */
136 /* We will divide by a number which is too large */
137 movl XsigH(%ebx),%ecx
141 /* here we need to divide by 100000000h,
142 i.e., no division at all.. */
147 divl %ecx /* Divide the numerator by the augmented
151 movl %eax,FPU_result_3 /* Put the result in the answer */
153 mull XsigH(%ebx) /* mul by the ms dw of the denom */
155 subl %eax,FPU_accum_2 /* Subtract from the num local reg */
156 sbbl %edx,FPU_accum_3
158 movl FPU_result_3,%eax /* Get the result back */
159 mull XsigL(%ebx) /* now mul the ls dw of the denom */
161 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
162 sbbl %edx,FPU_accum_2
164 je LDo_2nd_32_bits /* Must check for non-zero result here */
168 #endif /* PARANOID */
170 /* need to subtract another once of the denom */
171 incl FPU_result_3 /* Correct the answer */
173 movl XsigL(%ebx),%eax
174 movl XsigH(%ebx),%edx
175 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
176 sbbl %edx,FPU_accum_2
180 jne L_bugged_1 /* Must check for non-zero result here */
181 #endif /* PARANOID */
183 /*----------------------------------------------------------------------*/
184 /* Half of the main problem is done, there is just a reduced numerator
186 Work with the second 32 bits, FPU_accum_0 not used from now on */
188 movl FPU_accum_2,%edx /* get the reduced num */
189 movl FPU_accum_1,%eax
191 /* need to check for possible subsequent overflow */
192 cmpl XsigH(%ebx),%edx
194 ja LPrevent_2nd_overflow
196 cmpl XsigL(%ebx),%eax
199 LPrevent_2nd_overflow:
200 /* The numerator is greater or equal, would cause overflow */
201 /* prevent overflow */
202 subl XsigL(%ebx),%eax
203 sbbl XsigH(%ebx),%edx
204 movl %edx,FPU_accum_2
205 movl %eax,FPU_accum_1
207 incl FPU_result_3 /* Reflect the subtraction in the answer */
210 je L_bugged_2 /* Can't bump the result to 1.0 */
211 #endif /* PARANOID */
214 cmpl $0,%ecx /* augmented denom msw */
215 jnz LSecond_div_not_1
217 /* %ecx == 0, we are dividing by 1.0 */
222 divl %ecx /* Divide the numerator by the denom ms dw */
225 movl %eax,FPU_result_2 /* Put the result in the answer */
227 mull XsigH(%ebx) /* mul by the ms dw of the denom */
229 subl %eax,FPU_accum_1 /* Subtract from the num local reg */
230 sbbl %edx,FPU_accum_2
234 #endif /* PARANOID */
236 movl FPU_result_2,%eax /* Get the result back */
237 mull XsigL(%ebx) /* now mul the ls dw of the denom */
239 subl %eax,FPU_accum_0 /* Subtract from the num local reg */
240 sbbl %edx,FPU_accum_1 /* Subtract from the num local reg */
245 #endif /* PARANOID */
252 #endif /* PARANOID */
254 /* need to subtract another once of the denom */
255 movl XsigL(%ebx),%eax
256 movl XsigH(%ebx),%edx
257 subl %eax,FPU_accum_0 /* Subtract from the num local reg */
258 sbbl %edx,FPU_accum_1
264 #endif /* PARANOID */
266 addl $1,FPU_result_2 /* Correct the answer */
270 jc L_bugged_2 /* Must check for non-zero result here */
271 #endif /* PARANOID */
273 /*----------------------------------------------------------------------*/
274 /* The division is essentially finished here, we just need to perform
276 Deal with the 3rd 32 bits */
278 /* We use an approximation for the third 32 bits.
279 To take account of the 3rd 32 bits of the divisor
280 (call them del), we subtract del * (a/b) */
282 movl FPU_result_3,%eax /* a/b */
283 mull XsigLL(%ebx) /* del */
285 subl %edx,FPU_accum_1
287 /* A borrow indicates that the result is negative */
290 movl XsigH(%ebx),%edx
291 addl %edx,FPU_accum_1
293 subl $1,FPU_result_2 /* Adjust the answer */
296 /* The above addition might not have been enough, check again. */
297 movl FPU_accum_1,%edx /* get the reduced num */
298 cmpl XsigH(%ebx),%edx /* denom */
301 movl XsigH(%ebx),%edx
302 addl %edx,FPU_accum_1
304 subl $1,FPU_result_2 /* Adjust the answer */
309 movl FPU_accum_1,%edx /* get the reduced num */
311 /* need to check for possible subsequent overflow */
312 cmpl XsigH(%ebx),%edx /* denom */
315 /* prevent overflow */
316 subl XsigH(%ebx),%edx
317 movl %edx,FPU_accum_1
319 addl $1,FPU_result_2 /* Reflect the subtraction in the answer */
323 movl FPU_accum_0,%eax
324 movl FPU_accum_1,%edx
327 movl %eax,FPU_result_1 /* Rough estimate of third word */
329 movl PARAM3,%esi /* pointer to answer */
331 movl FPU_result_1,%eax
332 movl %eax,XsigLL(%esi)
333 movl FPU_result_2,%eax
334 movl %eax,XsigL(%esi)
335 movl FPU_result_3,%eax
336 movl %eax,XsigH(%esi)
348 /* The logic is wrong if we got here */
350 pushl EX_INTERNAL|0x240
356 pushl EX_INTERNAL|0x241
362 pushl EX_INTERNAL|0x242
366 #endif /* PARANOID */