* Copyright (C) 1992, 1993 W. Metzenthen, 22 Parker St, Ormond,
* E-mail apm233m@vaxc.cc.monash.edu.au
* This copyright notice covers the redistribution and use of the
* FPU emulator developed by W. Metzenthen. It covers only its use
* in the 386BSD operating system. Any other use is not permitted
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must include information specifying
* that source code for the emulator is freely available and include
* a) an offer to provide the source code for a nominal distribution
* b) list at least two alternative methods whereby the source
* can be obtained, e.g. a publically accessible bulletin board
* and an anonymous ftp site from which the software can be
* 3. All advertising materials specifically mentioning features or use of
* this emulator must acknowledge that it was developed by W. Metzenthen.
* 4. The name of W. Metzenthen may not be used to endorse or promote
* products derived from this software without specific prior written
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* W. METZENTHEN BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*---------------------------------------------------------------------------+
| Kernel for the division routines. |
| void reg_u_div(FPU_REG *a, FPU_REG *a, |
| FPU_REG *dest, unsigned int control_word) |
| Does not compute the destination exponent, but does adjust it. |
+---------------------------------------------------------------------------*/
/* #define dSIGL(x) (x) */
/* #define dSIGH(x) 4(x) */
Result: accum_3:accum_2:accum_1:accum_0
movl PARAM1,%esi /* pointer to num */
movl PARAM2,%ebx /* pointer to denom */
movl PARAM3,%edi /* pointer to answer */
/* testl $0x80000000, SIGH(%esi) *//* Dividend */
testl $0x80000000, SIGH(%ebx) /* Divisor*/
/* Check if the divisor can be treated as having just 32 bits */
jnz L_Full_Division /* Can't do a quick divide */
/* We should be able to zip through the division here */
movl SIGH(%ebx),%ecx /* The divisor */
movl SIGH(%esi),%edx /* Dividend */
movl SIGL(%esi),%eax /* Dividend */
setaeb ovfl_flag /* Keep a record */
subl %ecx,%edx /* Prevent the overflow */
/* Divide the 64 bit number by the 32 bit denominator */
/* Work on the remainder of the first division */
/* Work on the remainder of the 64 bit division */
testb $255,ovfl_flag /* was the num > denom ? */
/* Do the shifting here */
/* increase the exponent */
/* shift the mantissa right one bit */
stc /* To set the ms bit */
jmp LRound_precision /* Do the rounding as required*/
/*---------------------------------------------------------------------------+
| Divide: Return arg1/arg2 to arg3. |
| This routine does not use the exponents of arg1 and arg2, but does |
| adjust the exponent of arg3. |
| The maximum returned value is (ignoring exponents) |
| ------------------ = 1.ffffffff fffffffe |
| ------------------ = .80000000 00000001 (rounded) |
+---------------------------------------------------------------------------*/
/* Save extended dividend in local register*/
movl %eax,accum_1 /* zero the extension */
movl %eax,accum_0 /* zero the extension */
movl SIGL(%esi),%eax /* Get the current num */
/*----------------------------------------------------------------------*/
/* Initialization done */
/* Do the first 32 bits */
cmpl SIGH(%ebx),%edx /* Test for imminent overflow */
/* The dividend is greater or equal, would cause overflow */
setaeb ovfl_flag /* Keep a record */
sbbl SIGH(%ebx),%edx /* Prevent the overflow */
/* At this point, we have a dividend < divisor, with a record of
adjustment in ovfl_flag */
/* We will divide by a number which is too large */
/* here we need to divide by 100000000h,
i.e., no division at all.. */
divl %ecx /* Divide the numerator by the augmented
movl %eax,result_2 /* Put the result in the answer */
mull SIGH(%ebx) /* mul by the ms dw of the denom */
subl %eax,accum_2 /* Subtract from the num local reg */
movl result_2,%eax /* Get the result back */
mull SIGL(%ebx) /* now mul the ls dw of the denom */
subl %eax,accum_1 /* Subtract from the num local reg */
je LDo_2nd_32_bits /* Must check for non-zero result here */
/* need to subtract another once of the denom */
incl result_2 /* Correct the answer */
subl %eax,accum_1 /* Subtract from the num local reg */
jne L_bugged_1 /* Must check for non-zero result here */
/*----------------------------------------------------------------------*/
/* Half of the main problem is done, there is just a reduced numerator
/* Work with the second 32 bits, accum_0 not used from now on */
movl accum_2,%edx /* get the reduced num */
/* need to check for possible subsequent overflow */
/* The numerator is greater or equal, would cause overflow */
incl result_2 /* Reflect the subtraction in the answer */
je L_bugged_2 /* Can't bump the result to 1.0 */
cmpl $0,%ecx /* augmented denom msw*/
/* %ecx == 0, we are dividing by 1.0 */
divl %ecx /* Divide the numerator by the denom ms dw */
movl %eax,result_1 /* Put the result in the answer */
mull SIGH(%ebx) /* mul by the ms dw of the denom */
subl %eax,accum_1 /* Subtract from the num local reg */
movl result_1,%eax /* Get the result back */
mull SIGL(%ebx) /* now mul the ls dw of the denom */
subl %eax,accum_0 /* Subtract from the num local reg */
sbbl %edx,accum_1 /* Subtract from the num local reg */
/* need to subtract another once of the denom */
subl %eax,accum_0 /* Subtract from the num local reg */
addl $1,result_1 /* Correct the answer */
jc L_bugged_2 /* Must check for non-zero result here */
/*----------------------------------------------------------------------*/
/* The division is essentially finished here, we just need to perform
/* deal with the 3rd 32 bits */
movl accum_1,%edx /* get the reduced num */
/* need to check for possible subsequent overflow */
cmpl SIGH(%ebx),%edx /* denom*/
cmpl SIGL(%ebx),%eax /* denom */
addl $1,result_1 /* Reflect the subtraction in the answer */
/* This is a tricky spot, there is an overflow of the answer */
movb $255,ovfl_flag /* Overflow -> 1.000 */
// To test for rounding, we just need to compare 2*accum with the
jz LRound_ovfl /* The accumulator contains zero.*/
jc LRound_large /* No need to compare, denom smaller */
movl $0x70000000,%eax /* Denom was larger */
movl $0x80000000,%eax /* Remainder was exactly 1/2 denom */
movl $0xff000000,%eax /* Denom was smaller */
/* We are now ready to deal with rounding, but first we must get
the bits properly aligned */
testb $255,ovfl_flag /* was the num > denom ? */
/* shift the mantissa right one bit */
stc /* Will set the ms bit */
/* Round the result as required */
decl EXP(%edi) /* binary point between 1st & 2nd bits */
/* The logic is wrong if we got here */