/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: b_tadd.c,v 1.2 1997/09/25 17:08:21 ae59 Exp $ */
/*
$Log: b_tadd.c,v $
 * Revision 1.2  1997/09/25  17:08:21  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:46  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_tadd.c                              */
/*                                                              */
/*      Entries         : int  b_tadd(a,b,res)                  */
/*                        tenbyte *a,*b,*res;                   */
/*                                                              */
/*      Arguments       : a = first operand of addition         */
/*                        b = second operand of addition        */
/*                        res = sum of operands                 */
/*                                                              */
/*      Description     : Addition of numbers.                  */
/*                        Result is rounded according to        */
/*                        flag "b_rflg".                        */
/*                                                              */
/*      Date            : 1991-09-10                            */
/*      1992-07-22 : cast to a_intg, a_bool in vza exchnging =b=*/
/*      1991-09-10 : reduced max. line length                   */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local int b_tadd(tenbyte *a,tenbyte *b,tenbyte *res)
#else
local int b_tadd(a,b,res)

tenbyte *a;
tenbyte *b;
tenbyte *res;
#endif
        {
        a_btyp manta[BSIZE];
        a_btyp mantb[BSIZE];
        a_intg expoa,expob;
        a_bool  vza,vzb;
        a_btyp hu;
        a_intg i;

        E_TPUSH("b_tadd")

        /* decompose IEEE numbers, return if zero               */
        if (b_tdek(a,&expoa,manta,&vza))
           {
           C_COPY(res->c,b->c,t_size)

           E_TPOPP("b_tadd")
           return(0);
           }
        if (b_tdek(b,&expob,mantb,&vzb))
           {
           C_COPY(res->c,a->c,t_size)

           E_TPOPP("b_tadd")
           return(0);
           }

        /* order operands in order to get |a|>=|b|              */
        if ( expob > expoa ) {
                i = expoa; expoa = expob; expob = i;
                i = (a_intg) vza;   vza = vzb;  vzb = (a_bool) i;
                hu = manta[0];
                manta[0] = mantb[0]; mantb[0] = hu;
                hu = manta[1];
                manta[1] = mantb[1]; mantb[1] = hu;
                hu = manta[2];
                manta[2] = mantb[2]; mantb[2] = hu;
                }
        else if ( expoa == expob ) {
                for ( i=0; i<3; i++ )
                    if ( manta[i]<mantb[i] ) {
                        i = (a_intg) vza; vza = vzb; vzb = (a_bool) i;
                        hu = manta[0];
                        manta[0] = mantb[0]; mantb[0] = hu;
                        hu = manta[1];
                        manta[1] = mantb[1]; mantb[1] = hu;
                        hu = manta[2];
                        manta[2] = mantb[2]; mantb[2] = hu;
                        break;
                        }
                    else if ( manta[i]>mantb[i] ) break;
                }

        /* 1. case                                              */
        /* difference of exponents >= length of mantissa+2      */
        if ( expoa-expob>=tMANTL+2 ) {

                /* propagate a borrow to mantissa               */
                if (vza!=vzb) b_subc(manta+2);

                /* set lsb of of result mantissa                */
                else manta[2] |= LSB;
                }

        /* 2. case                                              */
        /* difference of exponents < length of mantissa+2       */
        else {

                /* adjust mantissa of mantb to manta            */
                b_shru( mantb, BSIZE, expoa-expob );

                /* add basetype mantissas                       */
                if ( vza==vzb ) {
                        (void)b_addm(BSIZE,manta,mantb);
                        }

                /* subtract basetype mantissas                  */
                else {
                        (void)b_subm(BSIZE,manta,mantb);

                        /* return zero if no bits are set            */
                        if (b_test(BSIZE,manta)) {
                            for (i=0;i<t_size;i++) res->c[i] = 0;

                            E_TPOPP("b_tadd")
                            return(0);
                            }
                        }
                }

        /* normalization of mantissa                            */
        expoa += BITS_PER_CHAR;
        while ( (MSB & *manta)==ZERO ) {
                b_shl1( manta, BSIZE );
                expoa--;
                }

        /* adjust denormalized number                           */
        b_tadj(manta,&expoa);

        /* round                                                */
        b_trnd(manta,&expoa,vza);

        /* compose result                                       */
        b_tcom(res,expoa,manta,vza);

        E_TPOPP("b_tadd")
        return(0);
        }
