/**************************************************************************/
/*                                                                        */
/*                                 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: r_divd.c,v 1.2 1997/09/25 17:45:05 ae59 Exp $ */
/*
$Log: r_divd.c,v $
 * Revision 1.2  1997/09/25  17:45:05  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:23:55  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : r_divd.c                              */
/*                                                              */
/*      Entries         : a_real r_divd(a,b)                    */
/*                        a_real a,b;                           */
/*                                                              */
/*      Arguments       : a = IEEE numerator                    */
/*                        b = IEEE denominator                  */
/*                                                              */
/*      Description     : Division of IEEE numbers.             */
/*                        Result is rounded downwards.          */
/*                                                              */
/*      Include         : divbody.h                             */
/*                                                              */
/*      Date            : 1991-05-02                            */
/*      1991-05-02 : use status flags                           */
/*      1991-01-07 : IEEE_HARDWARE considered / union q removed */
/*      1990-11-16 : E_TPUSH+E_TPOPP                            */
/*      1990-11-12 : INV_OP                                     */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
extern a_real *r_minf;
extern a_real *r_pinf;
extern a_real *r_sero;
extern a_real *r_zero;
#ifndef IEEE_HARDWARE
extern a_bool e_efdz;
extern a_bool e_efie;
extern a_bool e_efof;
extern a_bool e_efuf;
extern a_bool e_ofdz;
extern a_bool e_ofie;
extern a_bool e_ofof;
extern a_bool e_ofuf;
#endif
#endif

#ifdef LINT_ARGS
local a_real r_divd(a_real a,a_real b)
#else
local a_real r_divd(a,b)

a_real a;
a_real b;
#endif
        {
        a_real res;
#ifdef IEEE_HARDWARE
        a_btyp rc;
        int msg;

        E_TPUSH("r_divd")

        a_sets(ROUND_DOWN);

        res = a/b;

        if (a_gets(&rc)!=ZERO)
           {
           if (rc==INV_OP)
              {
              if (r_clss(a)==E_CLS5 || r_clss(a)==E_CLS6) msg = 2;
              else msg = (r_clss(a)==E_CLS2 || r_clss(a)==E_CLS9) ? 4 : 5;
              e_trap(rc,8,E_TMSG,msg,E_TDBL+E_TEXT(1),&a,
                                     E_TDBL+E_TEXT(2),&b,
                                     E_TDBL+E_TRES,&res);
              }
           else
              e_trap(rc,8,E_TDBL+E_TEXT(1),&a,E_TDBL+E_TEXT(2),&b,
                          E_TDBL+E_TEXT(3),&res,E_TDBL+E_TRES,&res);
           }
#else
        a_btyp manta[D_U_RATIO];
        a_btyp mantb[D_U_RATIO];
        a_btyp mantc[BSIZE];
        a_intg expoa,expob,expoc,i;
        a_bool vza,vzb,vzc,zeroa,zerob;
        a_bool trap;
        a_btyp rc;

        E_TPUSH("r_divd")

        /* initialize unused result positions                   */
        for (i=D_U_RATIO+1;i<BSIZE;i++) mantc[i] = ZERO;

        /* decomposition                                        */
        zeroa = b_deko( a, &expoa, manta, &vza );
        zerob = b_deko( b, &expob, mantb, &vzb );

        /* sign of quotient                                     */
        vzc = vza^vzb;

        /* NaN, infinity                                        */
        if ( expoa>EXPO_MAX || expob>EXPO_MAX )
                {

/* dividing NaNs or infinity                                    */
#ifdef AIX
#include "/u/p88c/runtime/real/divtrap.h"
#else
#include "divtrap.h"
#endif

        /* numerator is zero                                    */
        if ( zeroa ) {

           /* zero/zero                        */
           if (zerob) {
              e_trap(INV_OP+E_IEEE,8,E_TMSG,2,E_TDBL+E_TEXT(1),&a,
                                              E_TDBL+E_TEXT(2),&b,
                                              E_TDBL+E_TRES,&res);
              E_TPOPP("r_divd")
              return(res);
              }

           E_TPOPP("r_divd")
           return(*r_zero);
           }

        /* denominator is zero                                  */
        if (zerob) {
            res = (vzc) ? *r_minf : *r_pinf;
            if (e_efdz)
               e_trap(DIV_BY_ZERO+E_IEEE,6,E_TDBL+E_TEXT(1),&a,
                                           E_TDBL+E_TEXT(2),&b,
                                           E_TDBL+E_TRES,&res);
            else e_ofdz = TRUE;

            E_TPOPP("r_divd")
            return(res);
            }

/* divide IEEE numbers                                          */
#ifdef AIX
#include "/u/p88c/runtime/real/divbody.h"
#else
#include "divbody.h"
#endif

        /* rounding                                             */
        if (rc==NO_ERROR) rc = b_rndd( mantc, &expoc, vzc );
        else (void)b_rndd( mantc, &expoc, vzc );

        /* composition                                          */
        b_comp( &res, expoc, mantc, vzc );

        if (rc) {
            switch ((int)rc)
               {
               case INEXACT   : if ((trap = e_efie)==FALSE)
                                   e_ofie = TRUE;
                                break;
               case OVERFLOW  : if ((trap = e_efof)==FALSE)
                                   e_ofof = TRUE;
                                break;
               case UNDERFLOW : if ((trap = e_efuf)==FALSE)
                                   e_ofuf = TRUE;
                                break;
               default : trap = TRUE;
               }
            if (trap) e_trap(rc+E_IEEE,8,E_TDBL+E_TEXT(1),&a,
                                         E_TDBL+E_TEXT(2),&b,
                                         E_TDBL+E_TEXT(3),&res,
                                         E_TDBL+E_TRES,&res);
            }
#endif
        E_TPOPP("r_divd")
        return(res);
        }

