5 * Core division routines
8 * Copyright (C) 1992,1993,1994
9 * W. Metzenthen, 22 Parker St, Ormond, Vic 3163,
10 * Australia. E-mail billm@vaxc.cc.monash.edu.au
11 * All rights reserved.
13 * This copyright notice covers the redistribution and use of the
14 * FPU emulator developed by W. Metzenthen. It covers only its use
15 * in the 386BSD, FreeBSD and NetBSD operating systems. Any other
16 * use is not permitted under this copyright.
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must include information specifying
24 * that source code for the emulator is freely available and include
26 * a) an offer to provide the source code for a nominal distribution
28 * b) list at least two alternative methods whereby the source
29 * can be obtained, e.g. a publically accessible bulletin board
30 * and an anonymous ftp site from which the software can be
32 * 3. All advertising materials specifically mentioning features or use of
33 * this emulator must acknowledge that it was developed by W. Metzenthen.
34 * 4. The name of W. Metzenthen may not be used to endorse or promote
35 * products derived from this software without specific prior written
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
40 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
41 * W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
45 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
46 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 * The purpose of this copyright, based upon the Berkeley copyright, is to
51 * ensure that the covered software remains freely available to everyone.
53 * The software (with necessary differences) is also available, but under
54 * the terms of the GNU copyleft, for the Linux operating system and for
55 * the djgpp ms-dos extender.
57 * W. Metzenthen June 1994.
60 * $FreeBSD: src/sys/gnu/i386/fpemul/reg_u_div.s,v 1.9 1999/08/28 00:42:58 peter Exp $
61 * $DragonFly: src/sys/platform/pc32/gnu/fpemul/Attic/reg_u_div.s,v 1.4 2003/08/07 21:17:20 dillon Exp $
65 /*---------------------------------------------------------------------------+
66 | Kernel for the division routines. |
68 | void reg_u_div(FPU_REG *a, FPU_REG *a, |
69 | FPU_REG *dest, unsigned int control_word) |
71 | Does not compute the destination exponent, but does adjust it. |
72 +---------------------------------------------------------------------------*/
77 /* #define dSIGL(x) (x) */
78 /* #define dSIGH(x) 4(x) */
84 Result: accum_3:accum_2:accum_1:accum_0
85 Overflow flag: ovfl_flag
106 .globl _divide_kernel
116 movl PARAM1,%esi /* pointer to num */
117 movl PARAM2,%ebx /* pointer to denom */
118 movl PARAM3,%edi /* pointer to answer */
120 #ifdef DENORM_OPERAND
125 call denormal_operand
134 call denormal_operand
139 #endif DENORM_OPERAND
143 /* testl $0x80000000, SIGH(%esi) *//* Dividend */
145 testl $0x80000000, SIGH(%ebx) /* Divisor*/
149 /* Check if the divisor can be treated as having just 32 bits */
151 jnz L_Full_Division /* Can't do a quick divide */
153 /* We should be able to zip through the division here */
154 movl SIGH(%ebx),%ecx /* The divisor */
155 movl SIGH(%esi),%edx /* Dividend */
156 movl SIGL(%esi),%eax /* Dividend */
159 setaeb ovfl_flag /* Keep a record */
162 subl %ecx,%edx /* Prevent the overflow */
165 /* Divide the 64 bit number by the 32 bit denominator */
169 /* Work on the remainder of the first division */
174 /* Work on the remainder of the 64 bit division */
178 testb $255,ovfl_flag /* was the num > denom ? */
181 /* Do the shifting here */
182 /* increase the exponent */
185 /* shift the mantissa right one bit */
186 stc /* To set the ms bit */
192 jmp LRound_precision /* Do the rounding as required*/
195 /*---------------------------------------------------------------------------+
196 | Divide: Return arg1/arg2 to arg3. |
198 | This routine does not use the exponents of arg1 and arg2, but does |
199 | adjust the exponent of arg3. |
201 | The maximum returned value is (ignoring exponents) |
202 | .ffffffff ffffffff |
203 | ------------------ = 1.ffffffff fffffffe |
204 | .80000000 00000000 |
205 | and the minimum is |
206 | .80000000 00000000 |
207 | ------------------ = .80000000 00000001 (rounded) |
208 | .ffffffff ffffffff |
210 +---------------------------------------------------------------------------*/
214 /* Save extended dividend in local register*/
220 movl %eax,accum_1 /* zero the extension */
221 movl %eax,accum_0 /* zero the extension */
223 movl SIGL(%esi),%eax /* Get the current num */
226 /*----------------------------------------------------------------------*/
227 /* Initialization done */
228 /* Do the first 32 bits */
231 cmpl SIGH(%ebx),%edx /* Test for imminent overflow */
239 /* The dividend is greater or equal, would cause overflow */
240 setaeb ovfl_flag /* Keep a record */
243 sbbl SIGH(%ebx),%edx /* Prevent the overflow */
248 /* At this point, we have a dividend < divisor, with a record of
249 adjustment in ovfl_flag */
251 /* We will divide by a number which is too large */
256 /* here we need to divide by 100000000h,
257 i.e., no division at all.. */
262 divl %ecx /* Divide the numerator by the augmented
266 movl %eax,result_2 /* Put the result in the answer */
268 mull SIGH(%ebx) /* mul by the ms dw of the denom */
270 subl %eax,accum_2 /* Subtract from the num local reg */
273 movl result_2,%eax /* Get the result back */
274 mull SIGL(%ebx) /* now mul the ls dw of the denom */
276 subl %eax,accum_1 /* Subtract from the num local reg */
279 je LDo_2nd_32_bits /* Must check for non-zero result here */
285 /* need to subtract another once of the denom */
286 incl result_2 /* Correct the answer */
290 subl %eax,accum_1 /* Subtract from the num local reg */
295 jne L_bugged_1 /* Must check for non-zero result here */
298 /*----------------------------------------------------------------------*/
299 /* Half of the main problem is done, there is just a reduced numerator
301 /* Work with the second 32 bits, accum_0 not used from now on */
303 movl accum_2,%edx /* get the reduced num */
306 /* need to check for possible subsequent overflow */
309 ja LPrevent_2nd_overflow
314 LPrevent_2nd_overflow:
315 /* The numerator is greater or equal, would cause overflow */
316 /* prevent overflow */
322 incl result_2 /* Reflect the subtraction in the answer */
325 je L_bugged_2 /* Can't bump the result to 1.0 */
329 cmpl $0,%ecx /* augmented denom msw*/
330 jnz LSecond_div_not_1
332 /* %ecx == 0, we are dividing by 1.0 */
337 divl %ecx /* Divide the numerator by the denom ms dw */
340 movl %eax,result_1 /* Put the result in the answer */
342 mull SIGH(%ebx) /* mul by the ms dw of the denom */
344 subl %eax,accum_1 /* Subtract from the num local reg */
351 movl result_1,%eax /* Get the result back */
352 mull SIGL(%ebx) /* now mul the ls dw of the denom */
354 subl %eax,accum_0 /* Subtract from the num local reg */
355 sbbl %edx,accum_1 /* Subtract from the num local reg */
369 /* need to subtract another once of the denom */
372 subl %eax,accum_0 /* Subtract from the num local reg */
381 addl $1,result_1 /* Correct the answer */
385 jc L_bugged_2 /* Must check for non-zero result here */
388 /*----------------------------------------------------------------------*/
389 /* The division is essentially finished here, we just need to perform
390 tidying operations. */
391 /* deal with the 3rd 32 bits */
393 movl accum_1,%edx /* get the reduced num */
396 /* need to check for possible subsequent overflow */
397 cmpl SIGH(%ebx),%edx /* denom*/
399 ja LPrevent_3rd_overflow
401 cmpl SIGL(%ebx),%eax /* denom */
404 LPrevent_3rd_overflow:
405 /* prevent overflow */
411 addl $1,result_1 /* Reflect the subtraction in the answer */
416 /* This is a tricky spot, there is an overflow of the answer */
417 movb $255,ovfl_flag /* Overflow -> 1.000 */
420 /* Prepare for rounding.
421 // To test for rounding, we just need to compare 2*accum with the
427 jz LRound_ovfl /* The accumulator contains zero.*/
433 jc LRound_large /* No need to compare, denom smaller */
439 movl $0x70000000,%eax /* Denom was larger */
445 movl $0x80000000,%eax /* Remainder was exactly 1/2 denom */
449 movl $0xff000000,%eax /* Denom was smaller */
452 /* We are now ready to deal with rounding, but first we must get
453 the bits properly aligned */
454 testb $255,ovfl_flag /* was the num > denom ? */
459 /* shift the mantissa right one bit */
460 stc /* Will set the ms bit */
465 /* Round the result as required */
467 decl EXP(%edi) /* binary point between 1st & 2nd bits */
476 /* The logic is wrong if we got here */
478 pushl EX_INTERNAL|0x202
484 pushl EX_INTERNAL|0x203
490 pushl EX_INTERNAL|0x204