xref: /openbmc/linux/arch/parisc/math-emu/dfadd.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1660662f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Linux/PA-RISC Project (http://www.parisc-linux.org/)
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Floating-point emulation code
61da177e4SLinus Torvalds  *  Copyright (C) 2001 Hewlett-Packard (Paul Bame) <bame@debian.org>
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * BEGIN_DESC
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  *  File:
121da177e4SLinus Torvalds  *	@(#)	pa/spmath/dfadd.c		$Revision: 1.1 $
131da177e4SLinus Torvalds  *
141da177e4SLinus Torvalds  *  Purpose:
151da177e4SLinus Torvalds  *	Double_add: add two double precision values.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  *  External Interfaces:
181da177e4SLinus Torvalds  *	dbl_fadd(leftptr, rightptr, dstptr, status)
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *  Internal Interfaces:
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  *  Theory:
231da177e4SLinus Torvalds  *	<<please update with a overview of the operation of this file>>
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  * END_DESC
261da177e4SLinus Torvalds */
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include "float.h"
301da177e4SLinus Torvalds #include "dbl_float.h"
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds /*
331da177e4SLinus Torvalds  * Double_add: add two double precision values.
341da177e4SLinus Torvalds  */
dbl_fadd(dbl_floating_point * leftptr,dbl_floating_point * rightptr,dbl_floating_point * dstptr,unsigned int * status)351da177e4SLinus Torvalds dbl_fadd(
361da177e4SLinus Torvalds     dbl_floating_point *leftptr,
371da177e4SLinus Torvalds     dbl_floating_point *rightptr,
381da177e4SLinus Torvalds     dbl_floating_point *dstptr,
391da177e4SLinus Torvalds     unsigned int *status)
401da177e4SLinus Torvalds {
411da177e4SLinus Torvalds     register unsigned int signless_upper_left, signless_upper_right, save;
421da177e4SLinus Torvalds     register unsigned int leftp1, leftp2, rightp1, rightp2, extent;
431da177e4SLinus Torvalds     register unsigned int resultp1 = 0, resultp2 = 0;
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds     register int result_exponent, right_exponent, diff_exponent;
461da177e4SLinus Torvalds     register int sign_save, jumpsize;
471da177e4SLinus Torvalds     register boolean inexact = FALSE;
481da177e4SLinus Torvalds     register boolean underflowtrap;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds     /* Create local copies of the numbers */
511da177e4SLinus Torvalds     Dbl_copyfromptr(leftptr,leftp1,leftp2);
521da177e4SLinus Torvalds     Dbl_copyfromptr(rightptr,rightp1,rightp2);
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds     /* A zero "save" helps discover equal operands (for later),  *
551da177e4SLinus Torvalds      * and is used in swapping operands (if needed).             */
561da177e4SLinus Torvalds     Dbl_xortointp1(leftp1,rightp1,/*to*/save);
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds     /*
591da177e4SLinus Torvalds      * check first operand for NaN's or infinity
601da177e4SLinus Torvalds      */
611da177e4SLinus Torvalds     if ((result_exponent = Dbl_exponent(leftp1)) == DBL_INFINITY_EXPONENT)
621da177e4SLinus Torvalds 	{
631da177e4SLinus Torvalds 	if (Dbl_iszero_mantissa(leftp1,leftp2))
641da177e4SLinus Torvalds 	    {
651da177e4SLinus Torvalds 	    if (Dbl_isnotnan(rightp1,rightp2))
661da177e4SLinus Torvalds 		{
671da177e4SLinus Torvalds 		if (Dbl_isinfinity(rightp1,rightp2) && save!=0)
681da177e4SLinus Torvalds 		    {
691da177e4SLinus Torvalds 		    /*
701da177e4SLinus Torvalds 		     * invalid since operands are opposite signed infinity's
711da177e4SLinus Torvalds 		     */
721da177e4SLinus Torvalds 		    if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
731da177e4SLinus Torvalds                     Set_invalidflag();
741da177e4SLinus Torvalds                     Dbl_makequietnan(resultp1,resultp2);
751da177e4SLinus Torvalds 		    Dbl_copytoptr(resultp1,resultp2,dstptr);
761da177e4SLinus Torvalds 		    return(NOEXCEPTION);
771da177e4SLinus Torvalds 		    }
781da177e4SLinus Torvalds 		/*
791da177e4SLinus Torvalds 	 	 * return infinity
801da177e4SLinus Torvalds 	 	 */
811da177e4SLinus Torvalds 		Dbl_copytoptr(leftp1,leftp2,dstptr);
821da177e4SLinus Torvalds 		return(NOEXCEPTION);
831da177e4SLinus Torvalds 		}
841da177e4SLinus Torvalds 	    }
851da177e4SLinus Torvalds 	else
861da177e4SLinus Torvalds 	    {
871da177e4SLinus Torvalds             /*
881da177e4SLinus Torvalds              * is NaN; signaling or quiet?
891da177e4SLinus Torvalds              */
901da177e4SLinus Torvalds             if (Dbl_isone_signaling(leftp1))
911da177e4SLinus Torvalds 		{
921da177e4SLinus Torvalds                	/* trap if INVALIDTRAP enabled */
931da177e4SLinus Torvalds 		if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
941da177e4SLinus Torvalds         	/* make NaN quiet */
951da177e4SLinus Torvalds         	Set_invalidflag();
961da177e4SLinus Torvalds         	Dbl_set_quiet(leftp1);
971da177e4SLinus Torvalds         	}
981da177e4SLinus Torvalds 	    /*
991da177e4SLinus Torvalds 	     * is second operand a signaling NaN?
1001da177e4SLinus Torvalds 	     */
1011da177e4SLinus Torvalds 	    else if (Dbl_is_signalingnan(rightp1))
1021da177e4SLinus Torvalds 		{
1031da177e4SLinus Torvalds         	/* trap if INVALIDTRAP enabled */
1041da177e4SLinus Torvalds                	if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
1051da177e4SLinus Torvalds 		/* make NaN quiet */
1061da177e4SLinus Torvalds 		Set_invalidflag();
1071da177e4SLinus Torvalds 		Dbl_set_quiet(rightp1);
1081da177e4SLinus Torvalds 		Dbl_copytoptr(rightp1,rightp2,dstptr);
1091da177e4SLinus Torvalds 		return(NOEXCEPTION);
1101da177e4SLinus Torvalds 		}
1111da177e4SLinus Torvalds 	    /*
1121da177e4SLinus Torvalds  	     * return quiet NaN
1131da177e4SLinus Torvalds  	     */
1141da177e4SLinus Torvalds 	    Dbl_copytoptr(leftp1,leftp2,dstptr);
1151da177e4SLinus Torvalds  	    return(NOEXCEPTION);
1161da177e4SLinus Torvalds 	    }
1171da177e4SLinus Torvalds 	} /* End left NaN or Infinity processing */
1181da177e4SLinus Torvalds     /*
1191da177e4SLinus Torvalds      * check second operand for NaN's or infinity
1201da177e4SLinus Torvalds      */
1211da177e4SLinus Torvalds     if (Dbl_isinfinity_exponent(rightp1))
1221da177e4SLinus Torvalds 	{
1231da177e4SLinus Torvalds 	if (Dbl_iszero_mantissa(rightp1,rightp2))
1241da177e4SLinus Torvalds 	    {
1251da177e4SLinus Torvalds 	    /* return infinity */
1261da177e4SLinus Torvalds 	    Dbl_copytoptr(rightp1,rightp2,dstptr);
1271da177e4SLinus Torvalds 	    return(NOEXCEPTION);
1281da177e4SLinus Torvalds 	    }
1291da177e4SLinus Torvalds         /*
1301da177e4SLinus Torvalds          * is NaN; signaling or quiet?
1311da177e4SLinus Torvalds          */
1321da177e4SLinus Torvalds         if (Dbl_isone_signaling(rightp1))
1331da177e4SLinus Torvalds 	    {
1341da177e4SLinus Torvalds             /* trap if INVALIDTRAP enabled */
1351da177e4SLinus Torvalds 	    if (Is_invalidtrap_enabled()) return(INVALIDEXCEPTION);
1361da177e4SLinus Torvalds 	    /* make NaN quiet */
1371da177e4SLinus Torvalds 	    Set_invalidflag();
1381da177e4SLinus Torvalds 	    Dbl_set_quiet(rightp1);
1391da177e4SLinus Torvalds 	    }
1401da177e4SLinus Torvalds 	/*
1411da177e4SLinus Torvalds 	 * return quiet NaN
1421da177e4SLinus Torvalds  	 */
1431da177e4SLinus Torvalds 	Dbl_copytoptr(rightp1,rightp2,dstptr);
1441da177e4SLinus Torvalds 	return(NOEXCEPTION);
1451da177e4SLinus Torvalds     	} /* End right NaN or Infinity processing */
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds     /* Invariant: Must be dealing with finite numbers */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds     /* Compare operands by removing the sign */
1501da177e4SLinus Torvalds     Dbl_copytoint_exponentmantissap1(leftp1,signless_upper_left);
1511da177e4SLinus Torvalds     Dbl_copytoint_exponentmantissap1(rightp1,signless_upper_right);
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds     /* sign difference selects add or sub operation. */
1541da177e4SLinus Torvalds     if(Dbl_ismagnitudeless(leftp2,rightp2,signless_upper_left,signless_upper_right))
1551da177e4SLinus Torvalds 	{
1561da177e4SLinus Torvalds 	/* Set the left operand to the larger one by XOR swap *
1571da177e4SLinus Torvalds 	 *  First finish the first word using "save"          */
1581da177e4SLinus Torvalds 	Dbl_xorfromintp1(save,rightp1,/*to*/rightp1);
1591da177e4SLinus Torvalds 	Dbl_xorfromintp1(save,leftp1,/*to*/leftp1);
1601da177e4SLinus Torvalds      	Dbl_swap_lower(leftp2,rightp2);
1611da177e4SLinus Torvalds 	result_exponent = Dbl_exponent(leftp1);
1621da177e4SLinus Torvalds 	}
1631da177e4SLinus Torvalds     /* Invariant:  left is not smaller than right. */
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds     if((right_exponent = Dbl_exponent(rightp1)) == 0)
1661da177e4SLinus Torvalds         {
1671da177e4SLinus Torvalds 	/* Denormalized operands.  First look for zeroes */
1681da177e4SLinus Torvalds 	if(Dbl_iszero_mantissa(rightp1,rightp2))
1691da177e4SLinus Torvalds 	    {
1701da177e4SLinus Torvalds 	    /* right is zero */
1711da177e4SLinus Torvalds 	    if(Dbl_iszero_exponentmantissa(leftp1,leftp2))
1721da177e4SLinus Torvalds 		{
1731da177e4SLinus Torvalds 		/* Both operands are zeros */
1741da177e4SLinus Torvalds 		if(Is_rounding_mode(ROUNDMINUS))
1751da177e4SLinus Torvalds 		    {
1761da177e4SLinus Torvalds 		    Dbl_or_signs(leftp1,/*with*/rightp1);
1771da177e4SLinus Torvalds 		    }
1781da177e4SLinus Torvalds 		else
1791da177e4SLinus Torvalds 		    {
1801da177e4SLinus Torvalds 		    Dbl_and_signs(leftp1,/*with*/rightp1);
1811da177e4SLinus Torvalds 		    }
1821da177e4SLinus Torvalds 		}
1831da177e4SLinus Torvalds 	    else
1841da177e4SLinus Torvalds 		{
1851da177e4SLinus Torvalds 		/* Left is not a zero and must be the result.  Trapped
1861da177e4SLinus Torvalds 		 * underflows are signaled if left is denormalized.  Result
1871da177e4SLinus Torvalds 		 * is always exact. */
1881da177e4SLinus Torvalds 		if( (result_exponent == 0) && Is_underflowtrap_enabled() )
1891da177e4SLinus Torvalds 		    {
1901da177e4SLinus Torvalds 		    /* need to normalize results mantissa */
1911da177e4SLinus Torvalds 	    	    sign_save = Dbl_signextendedsign(leftp1);
1921da177e4SLinus Torvalds 		    Dbl_leftshiftby1(leftp1,leftp2);
1931da177e4SLinus Torvalds 		    Dbl_normalize(leftp1,leftp2,result_exponent);
1941da177e4SLinus Torvalds 		    Dbl_set_sign(leftp1,/*using*/sign_save);
1951da177e4SLinus Torvalds                     Dbl_setwrapped_exponent(leftp1,result_exponent,unfl);
1961da177e4SLinus Torvalds 		    Dbl_copytoptr(leftp1,leftp2,dstptr);
1971da177e4SLinus Torvalds 		    /* inexact = FALSE */
1981da177e4SLinus Torvalds 		    return(UNDERFLOWEXCEPTION);
1991da177e4SLinus Torvalds 		    }
2001da177e4SLinus Torvalds 		}
2011da177e4SLinus Torvalds 	    Dbl_copytoptr(leftp1,leftp2,dstptr);
2021da177e4SLinus Torvalds 	    return(NOEXCEPTION);
2031da177e4SLinus Torvalds 	    }
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	/* Neither are zeroes */
2061da177e4SLinus Torvalds 	Dbl_clear_sign(rightp1);	/* Exponent is already cleared */
2071da177e4SLinus Torvalds 	if(result_exponent == 0 )
2081da177e4SLinus Torvalds 	    {
2091da177e4SLinus Torvalds 	    /* Both operands are denormalized.  The result must be exact
2101da177e4SLinus Torvalds 	     * and is simply calculated.  A sum could become normalized and a
2111da177e4SLinus Torvalds 	     * difference could cancel to a true zero. */
2121da177e4SLinus Torvalds 	    if( (/*signed*/int) save < 0 )
2131da177e4SLinus Torvalds 		{
2141da177e4SLinus Torvalds 		Dbl_subtract(leftp1,leftp2,/*minus*/rightp1,rightp2,
2151da177e4SLinus Torvalds 		/*into*/resultp1,resultp2);
2161da177e4SLinus Torvalds 		if(Dbl_iszero_mantissa(resultp1,resultp2))
2171da177e4SLinus Torvalds 		    {
2181da177e4SLinus Torvalds 		    if(Is_rounding_mode(ROUNDMINUS))
2191da177e4SLinus Torvalds 			{
2201da177e4SLinus Torvalds 			Dbl_setone_sign(resultp1);
2211da177e4SLinus Torvalds 			}
2221da177e4SLinus Torvalds 		    else
2231da177e4SLinus Torvalds 			{
2241da177e4SLinus Torvalds 			Dbl_setzero_sign(resultp1);
2251da177e4SLinus Torvalds 			}
2261da177e4SLinus Torvalds 		    Dbl_copytoptr(resultp1,resultp2,dstptr);
2271da177e4SLinus Torvalds 		    return(NOEXCEPTION);
2281da177e4SLinus Torvalds 		    }
2291da177e4SLinus Torvalds 		}
2301da177e4SLinus Torvalds 	    else
2311da177e4SLinus Torvalds 		{
2321da177e4SLinus Torvalds 		Dbl_addition(leftp1,leftp2,rightp1,rightp2,
2331da177e4SLinus Torvalds 		/*into*/resultp1,resultp2);
2341da177e4SLinus Torvalds 		if(Dbl_isone_hidden(resultp1))
2351da177e4SLinus Torvalds 		    {
2361da177e4SLinus Torvalds 		    Dbl_copytoptr(resultp1,resultp2,dstptr);
2371da177e4SLinus Torvalds 		    return(NOEXCEPTION);
2381da177e4SLinus Torvalds 		    }
2391da177e4SLinus Torvalds 		}
2401da177e4SLinus Torvalds 	    if(Is_underflowtrap_enabled())
2411da177e4SLinus Torvalds 		{
2421da177e4SLinus Torvalds 		/* need to normalize result */
2431da177e4SLinus Torvalds 	    	sign_save = Dbl_signextendedsign(resultp1);
2441da177e4SLinus Torvalds 		Dbl_leftshiftby1(resultp1,resultp2);
2451da177e4SLinus Torvalds 		Dbl_normalize(resultp1,resultp2,result_exponent);
2461da177e4SLinus Torvalds 		Dbl_set_sign(resultp1,/*using*/sign_save);
2471da177e4SLinus Torvalds                 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
2481da177e4SLinus Torvalds 	        Dbl_copytoptr(resultp1,resultp2,dstptr);
2491da177e4SLinus Torvalds 		/* inexact = FALSE */
2501da177e4SLinus Torvalds 	        return(UNDERFLOWEXCEPTION);
2511da177e4SLinus Torvalds 		}
2521da177e4SLinus Torvalds 	    Dbl_copytoptr(resultp1,resultp2,dstptr);
2531da177e4SLinus Torvalds 	    return(NOEXCEPTION);
2541da177e4SLinus Torvalds 	    }
2551da177e4SLinus Torvalds 	right_exponent = 1;	/* Set exponent to reflect different bias
256*a65bcad5SJulia Lawall 				 * with denormalized numbers. */
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds     else
2591da177e4SLinus Torvalds 	{
2601da177e4SLinus Torvalds 	Dbl_clear_signexponent_set_hidden(rightp1);
2611da177e4SLinus Torvalds 	}
2621da177e4SLinus Torvalds     Dbl_clear_exponent_set_hidden(leftp1);
2631da177e4SLinus Torvalds     diff_exponent = result_exponent - right_exponent;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds     /*
2661da177e4SLinus Torvalds      * Special case alignment of operands that would force alignment
2671da177e4SLinus Torvalds      * beyond the extent of the extension.  A further optimization
2681da177e4SLinus Torvalds      * could special case this but only reduces the path length for this
2691da177e4SLinus Torvalds      * infrequent case.
2701da177e4SLinus Torvalds      */
2711da177e4SLinus Torvalds     if(diff_exponent > DBL_THRESHOLD)
2721da177e4SLinus Torvalds 	{
2731da177e4SLinus Torvalds 	diff_exponent = DBL_THRESHOLD;
2741da177e4SLinus Torvalds 	}
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds     /* Align right operand by shifting to right */
2771da177e4SLinus Torvalds     Dbl_right_align(/*operand*/rightp1,rightp2,/*shifted by*/diff_exponent,
2781da177e4SLinus Torvalds     /*and lower to*/extent);
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds     /* Treat sum and difference of the operands separately. */
2811da177e4SLinus Torvalds     if( (/*signed*/int) save < 0 )
2821da177e4SLinus Torvalds 	{
2831da177e4SLinus Torvalds 	/*
2841da177e4SLinus Torvalds 	 * Difference of the two operands.  Their can be no overflow.  A
2851da177e4SLinus Torvalds 	 * borrow can occur out of the hidden bit and force a post
2861da177e4SLinus Torvalds 	 * normalization phase.
2871da177e4SLinus Torvalds 	 */
2881da177e4SLinus Torvalds 	Dbl_subtract_withextension(leftp1,leftp2,/*minus*/rightp1,rightp2,
2891da177e4SLinus Torvalds 	/*with*/extent,/*into*/resultp1,resultp2);
2901da177e4SLinus Torvalds 	if(Dbl_iszero_hidden(resultp1))
2911da177e4SLinus Torvalds 	    {
2921da177e4SLinus Torvalds 	    /* Handle normalization */
29325985edcSLucas De Marchi 	    /* A straight forward algorithm would now shift the result
2941da177e4SLinus Torvalds 	     * and extension left until the hidden bit becomes one.  Not
2951da177e4SLinus Torvalds 	     * all of the extension bits need participate in the shift.
2961da177e4SLinus Torvalds 	     * Only the two most significant bits (round and guard) are
2971da177e4SLinus Torvalds 	     * needed.  If only a single shift is needed then the guard
2981da177e4SLinus Torvalds 	     * bit becomes a significant low order bit and the extension
2991da177e4SLinus Torvalds 	     * must participate in the rounding.  If more than a single
3001da177e4SLinus Torvalds 	     * shift is needed, then all bits to the right of the guard
3011da177e4SLinus Torvalds 	     * bit are zeros, and the guard bit may or may not be zero. */
3021da177e4SLinus Torvalds 	    sign_save = Dbl_signextendedsign(resultp1);
3031da177e4SLinus Torvalds             Dbl_leftshiftby1_withextent(resultp1,resultp2,extent,resultp1,resultp2);
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds             /* Need to check for a zero result.  The sign and exponent
3061da177e4SLinus Torvalds 	     * fields have already been zeroed.  The more efficient test
3071da177e4SLinus Torvalds 	     * of the full object can be used.
3081da177e4SLinus Torvalds 	     */
3091da177e4SLinus Torvalds     	    if(Dbl_iszero(resultp1,resultp2))
3101da177e4SLinus Torvalds 		/* Must have been "x-x" or "x+(-x)". */
3111da177e4SLinus Torvalds 		{
3121da177e4SLinus Torvalds 		if(Is_rounding_mode(ROUNDMINUS)) Dbl_setone_sign(resultp1);
3131da177e4SLinus Torvalds 		Dbl_copytoptr(resultp1,resultp2,dstptr);
3141da177e4SLinus Torvalds 		return(NOEXCEPTION);
3151da177e4SLinus Torvalds 		}
3161da177e4SLinus Torvalds 	    result_exponent--;
3171da177e4SLinus Torvalds 	    /* Look to see if normalization is finished. */
3181da177e4SLinus Torvalds 	    if(Dbl_isone_hidden(resultp1))
3191da177e4SLinus Torvalds 		{
3201da177e4SLinus Torvalds 		if(result_exponent==0)
3211da177e4SLinus Torvalds 		    {
3221da177e4SLinus Torvalds 		    /* Denormalized, exponent should be zero.  Left operand *
3231da177e4SLinus Torvalds 		     * was normalized, so extent (guard, round) was zero    */
3241da177e4SLinus Torvalds 		    goto underflow;
3251da177e4SLinus Torvalds 		    }
3261da177e4SLinus Torvalds 		else
3271da177e4SLinus Torvalds 		    {
3281da177e4SLinus Torvalds 		    /* No further normalization is needed. */
3291da177e4SLinus Torvalds 		    Dbl_set_sign(resultp1,/*using*/sign_save);
3301da177e4SLinus Torvalds 	    	    Ext_leftshiftby1(extent);
3311da177e4SLinus Torvalds 		    goto round;
3321da177e4SLinus Torvalds 		    }
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	    /* Check for denormalized, exponent should be zero.  Left    *
3361da177e4SLinus Torvalds 	     * operand was normalized, so extent (guard, round) was zero */
3371da177e4SLinus Torvalds 	    if(!(underflowtrap = Is_underflowtrap_enabled()) &&
3381da177e4SLinus Torvalds 	       result_exponent==0) goto underflow;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	    /* Shift extension to complete one bit of normalization and
3411da177e4SLinus Torvalds 	     * update exponent. */
3421da177e4SLinus Torvalds 	    Ext_leftshiftby1(extent);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds 	    /* Discover first one bit to determine shift amount.  Use a
3451da177e4SLinus Torvalds 	     * modified binary search.  We have already shifted the result
3461da177e4SLinus Torvalds 	     * one position right and still not found a one so the remainder
3471da177e4SLinus Torvalds 	     * of the extension must be zero and simplifies rounding. */
3481da177e4SLinus Torvalds 	    /* Scan bytes */
3491da177e4SLinus Torvalds 	    while(Dbl_iszero_hiddenhigh7mantissa(resultp1))
3501da177e4SLinus Torvalds 		{
3511da177e4SLinus Torvalds 		Dbl_leftshiftby8(resultp1,resultp2);
3521da177e4SLinus Torvalds 		if((result_exponent -= 8) <= 0  && !underflowtrap)
3531da177e4SLinus Torvalds 		    goto underflow;
3541da177e4SLinus Torvalds 		}
3551da177e4SLinus Torvalds 	    /* Now narrow it down to the nibble */
3561da177e4SLinus Torvalds 	    if(Dbl_iszero_hiddenhigh3mantissa(resultp1))
3571da177e4SLinus Torvalds 		{
3581da177e4SLinus Torvalds 		/* The lower nibble contains the normalizing one */
3591da177e4SLinus Torvalds 		Dbl_leftshiftby4(resultp1,resultp2);
3601da177e4SLinus Torvalds 		if((result_exponent -= 4) <= 0 && !underflowtrap)
3611da177e4SLinus Torvalds 		    goto underflow;
3621da177e4SLinus Torvalds 		}
3631da177e4SLinus Torvalds 	    /* Select case were first bit is set (already normalized)
3641da177e4SLinus Torvalds 	     * otherwise select the proper shift. */
3651da177e4SLinus Torvalds 	    if((jumpsize = Dbl_hiddenhigh3mantissa(resultp1)) > 7)
3661da177e4SLinus Torvalds 		{
3671da177e4SLinus Torvalds 		/* Already normalized */
3681da177e4SLinus Torvalds 		if(result_exponent <= 0) goto underflow;
3691da177e4SLinus Torvalds 		Dbl_set_sign(resultp1,/*using*/sign_save);
3701da177e4SLinus Torvalds 		Dbl_set_exponent(resultp1,/*using*/result_exponent);
3711da177e4SLinus Torvalds 		Dbl_copytoptr(resultp1,resultp2,dstptr);
3721da177e4SLinus Torvalds 		return(NOEXCEPTION);
3731da177e4SLinus Torvalds 		}
3741da177e4SLinus Torvalds 	    Dbl_sethigh4bits(resultp1,/*using*/sign_save);
3751da177e4SLinus Torvalds 	    switch(jumpsize)
3761da177e4SLinus Torvalds 		{
3771da177e4SLinus Torvalds 		case 1:
3781da177e4SLinus Torvalds 		    {
3791da177e4SLinus Torvalds 		    Dbl_leftshiftby3(resultp1,resultp2);
3801da177e4SLinus Torvalds 		    result_exponent -= 3;
3811da177e4SLinus Torvalds 		    break;
3821da177e4SLinus Torvalds 		    }
3831da177e4SLinus Torvalds 		case 2:
3841da177e4SLinus Torvalds 		case 3:
3851da177e4SLinus Torvalds 		    {
3861da177e4SLinus Torvalds 		    Dbl_leftshiftby2(resultp1,resultp2);
3871da177e4SLinus Torvalds 		    result_exponent -= 2;
3881da177e4SLinus Torvalds 		    break;
3891da177e4SLinus Torvalds 		    }
3901da177e4SLinus Torvalds 		case 4:
3911da177e4SLinus Torvalds 		case 5:
3921da177e4SLinus Torvalds 		case 6:
3931da177e4SLinus Torvalds 		case 7:
3941da177e4SLinus Torvalds 		    {
3951da177e4SLinus Torvalds 		    Dbl_leftshiftby1(resultp1,resultp2);
3961da177e4SLinus Torvalds 		    result_exponent -= 1;
3971da177e4SLinus Torvalds 		    break;
3981da177e4SLinus Torvalds 		    }
3991da177e4SLinus Torvalds 		}
4001da177e4SLinus Torvalds 	    if(result_exponent > 0)
4011da177e4SLinus Torvalds 		{
4021da177e4SLinus Torvalds 		Dbl_set_exponent(resultp1,/*using*/result_exponent);
4031da177e4SLinus Torvalds 		Dbl_copytoptr(resultp1,resultp2,dstptr);
4041da177e4SLinus Torvalds 		return(NOEXCEPTION); 	/* Sign bit is already set */
4051da177e4SLinus Torvalds 		}
4061da177e4SLinus Torvalds 	    /* Fixup potential underflows */
4071da177e4SLinus Torvalds 	  underflow:
4081da177e4SLinus Torvalds 	    if(Is_underflowtrap_enabled())
4091da177e4SLinus Torvalds 		{
4101da177e4SLinus Torvalds 		Dbl_set_sign(resultp1,sign_save);
4111da177e4SLinus Torvalds                 Dbl_setwrapped_exponent(resultp1,result_exponent,unfl);
4121da177e4SLinus Torvalds 		Dbl_copytoptr(resultp1,resultp2,dstptr);
4131da177e4SLinus Torvalds 		/* inexact = FALSE */
4141da177e4SLinus Torvalds 		return(UNDERFLOWEXCEPTION);
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	    /*
4171da177e4SLinus Torvalds 	     * Since we cannot get an inexact denormalized result,
4181da177e4SLinus Torvalds 	     * we can now return.
4191da177e4SLinus Torvalds 	     */
4201da177e4SLinus Torvalds 	    Dbl_fix_overshift(resultp1,resultp2,(1-result_exponent),extent);
4211da177e4SLinus Torvalds 	    Dbl_clear_signexponent(resultp1);
4221da177e4SLinus Torvalds 	    Dbl_set_sign(resultp1,sign_save);
4231da177e4SLinus Torvalds 	    Dbl_copytoptr(resultp1,resultp2,dstptr);
4241da177e4SLinus Torvalds 	    return(NOEXCEPTION);
4251da177e4SLinus Torvalds 	    } /* end if(hidden...)... */
4261da177e4SLinus Torvalds 	/* Fall through and round */
4271da177e4SLinus Torvalds 	} /* end if(save < 0)... */
4281da177e4SLinus Torvalds     else
4291da177e4SLinus Torvalds 	{
4301da177e4SLinus Torvalds 	/* Add magnitudes */
4311da177e4SLinus Torvalds 	Dbl_addition(leftp1,leftp2,rightp1,rightp2,/*to*/resultp1,resultp2);
4321da177e4SLinus Torvalds 	if(Dbl_isone_hiddenoverflow(resultp1))
4331da177e4SLinus Torvalds 	    {
4341da177e4SLinus Torvalds 	    /* Prenormalization required. */
4351da177e4SLinus Torvalds 	    Dbl_rightshiftby1_withextent(resultp2,extent,extent);
4361da177e4SLinus Torvalds 	    Dbl_arithrightshiftby1(resultp1,resultp2);
4371da177e4SLinus Torvalds 	    result_exponent++;
4381da177e4SLinus Torvalds 	    } /* end if hiddenoverflow... */
4391da177e4SLinus Torvalds 	} /* end else ...add magnitudes... */
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds     /* Round the result.  If the extension is all zeros,then the result is
4421da177e4SLinus Torvalds      * exact.  Otherwise round in the correct direction.  No underflow is
4431da177e4SLinus Torvalds      * possible. If a postnormalization is necessary, then the mantissa is
4441da177e4SLinus Torvalds      * all zeros so no shift is needed. */
4451da177e4SLinus Torvalds   round:
4461da177e4SLinus Torvalds     if(Ext_isnotzero(extent))
4471da177e4SLinus Torvalds 	{
4481da177e4SLinus Torvalds 	inexact = TRUE;
4491da177e4SLinus Torvalds 	switch(Rounding_mode())
4501da177e4SLinus Torvalds 	    {
4511da177e4SLinus Torvalds 	    case ROUNDNEAREST: /* The default. */
4521da177e4SLinus Torvalds 	    if(Ext_isone_sign(extent))
4531da177e4SLinus Torvalds 		{
4541da177e4SLinus Torvalds 		/* at least 1/2 ulp */
4551da177e4SLinus Torvalds 		if(Ext_isnotzero_lower(extent)  ||
4561da177e4SLinus Torvalds 		  Dbl_isone_lowmantissap2(resultp2))
4571da177e4SLinus Torvalds 		    {
4581da177e4SLinus Torvalds 		    /* either exactly half way and odd or more than 1/2ulp */
4591da177e4SLinus Torvalds 		    Dbl_increment(resultp1,resultp2);
4601da177e4SLinus Torvalds 		    }
4611da177e4SLinus Torvalds 		}
4621da177e4SLinus Torvalds 	    break;
4631da177e4SLinus Torvalds 
4641da177e4SLinus Torvalds 	    case ROUNDPLUS:
4651da177e4SLinus Torvalds 	    if(Dbl_iszero_sign(resultp1))
4661da177e4SLinus Torvalds 		{
4671da177e4SLinus Torvalds 		/* Round up positive results */
4681da177e4SLinus Torvalds 		Dbl_increment(resultp1,resultp2);
4691da177e4SLinus Torvalds 		}
4701da177e4SLinus Torvalds 	    break;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	    case ROUNDMINUS:
4731da177e4SLinus Torvalds 	    if(Dbl_isone_sign(resultp1))
4741da177e4SLinus Torvalds 		{
4751da177e4SLinus Torvalds 		/* Round down negative results */
4761da177e4SLinus Torvalds 		Dbl_increment(resultp1,resultp2);
4771da177e4SLinus Torvalds 		}
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	    case ROUNDZERO:;
4801da177e4SLinus Torvalds 	    /* truncate is simple */
4811da177e4SLinus Torvalds 	    } /* end switch... */
4821da177e4SLinus Torvalds 	if(Dbl_isone_hiddenoverflow(resultp1)) result_exponent++;
4831da177e4SLinus Torvalds 	}
4841da177e4SLinus Torvalds     if(result_exponent == DBL_INFINITY_EXPONENT)
4851da177e4SLinus Torvalds         {
4861da177e4SLinus Torvalds         /* Overflow */
4871da177e4SLinus Torvalds         if(Is_overflowtrap_enabled())
4881da177e4SLinus Torvalds 	    {
4891da177e4SLinus Torvalds 	    Dbl_setwrapped_exponent(resultp1,result_exponent,ovfl);
4901da177e4SLinus Torvalds 	    Dbl_copytoptr(resultp1,resultp2,dstptr);
4911da177e4SLinus Torvalds 	    if (inexact)
4921da177e4SLinus Torvalds 		if (Is_inexacttrap_enabled())
4931da177e4SLinus Torvalds 			return(OVERFLOWEXCEPTION | INEXACTEXCEPTION);
4941da177e4SLinus Torvalds 		else Set_inexactflag();
4951da177e4SLinus Torvalds 	    return(OVERFLOWEXCEPTION);
4961da177e4SLinus Torvalds 	    }
4971da177e4SLinus Torvalds         else
4981da177e4SLinus Torvalds 	    {
4991da177e4SLinus Torvalds 	    inexact = TRUE;
5001da177e4SLinus Torvalds 	    Set_overflowflag();
5011da177e4SLinus Torvalds 	    Dbl_setoverflow(resultp1,resultp2);
5021da177e4SLinus Torvalds 	    }
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds     else Dbl_set_exponent(resultp1,result_exponent);
5051da177e4SLinus Torvalds     Dbl_copytoptr(resultp1,resultp2,dstptr);
5061da177e4SLinus Torvalds     if(inexact)
5071da177e4SLinus Torvalds 	if(Is_inexacttrap_enabled())
5081da177e4SLinus Torvalds 	    return(INEXACTEXCEPTION);
5091da177e4SLinus Torvalds 	else Set_inexactflag();
5101da177e4SLinus Torvalds     return(NOEXCEPTION);
5111da177e4SLinus Torvalds }
512