/**************************************************************************/
/*                                                                        */
/*                                 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_tmdv.c,v 1.2 1997/09/25 17:09:28 ae59 Exp $ */
/*
$Log: b_tmdv.c,v $
 * Revision 1.2  1997/09/25  17:09:28  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:21:50  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : b_tmdv.c                              */
/*                                                              */
/*      Entries         : void b_tmdv(ma,mb,mc,expo)            */
/*                        a_btyp *ma,*mb,*mc;                   */
/*                        a_intg *expo;                         */
/*                                                              */
/*      Arguments       : ma = mantissa of length D_U_RATIO     */
/*                        mb = mantissa of length D_U_RATIO     */
/*                        mc = mantissa of length D_U_RATIO+1   */
/*                        expo = exponent of quotient           */
/*                                                              */
/*      Description     : Division of basetype mantissas        */
/*                                                              */
/*      External        :                                       */
/*                        b_shlu - shift basetype left          */
/*                        b_shl1 - shift left 1 position        */
/*                        b_subm - subtract basetype arrays     */
/*                                                              */
/*      Date            : 1991-06-20                            */
/*      1991-06-20 : new routine                                */
/****************************************************************/

#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 void b_tmdv(a_btyp *ma,a_btyp *mb,a_btyp *mc,a_intg *expo)
#else
local void b_tmdv(ma,mb,mc,expo)

a_btyp ma[BSIZE];
a_btyp mb[BSIZE];
a_btyp mc[BSIZE];
a_intg *expo;
#endif
        {

        a_intg i,j,block;
        a_btyp m[BSIZE],mm[BSIZE],f,h;
        a_bool  exact = FALSE;

        E_TPUSH("b_tmdv")

        /* initialization of mc[]                               */
        B_CLEAR(mc,BSIZE)
        B_CLEAR(m, BSIZE)
        B_CLEAR(mm,BSIZE) 

        /* shift mantissa of numerator left if mb[]>ma[]        */
        for (i=0; i<BSIZE; i++)
           {
           if (ma[i]>mb[i]) break;
           else if (ma[i]<mb[i])
              {
              b_shl1(ma,BSIZE);
              (*expo)--;
              break;
              }
           }

        *mc = MSB;
        (void)b_subm(BSIZE,ma,mb);

        block = B_LENGTH-1;

        /* division loop                                        */
        for (j=1;j<tMANTL+1;j += tSHIFT) {

                /* shift mantissa of numerator                  */
                b_shlu(ma,BSIZE,tSHIFT);

                /* subtract multiple if non-zero factor         */
                if ( (f = *ma/(*mb+1)) >1) {
                    mm[2] = m[2] = ZERO;
                    h = GETHIGH(mb[2])*f;
                    mm[2] |= MOVEHIGH(h);
                    mm[1] = GETHIGH(h);
                    m[1] = GETLOW(mb[1])*f;
                    h = GETHIGH(mb[1])*f;
                    mm[1] |= MOVEHIGH(h);
                    mm[0] = GETHIGH(h);
                    m[0] = mb[0]*f;
                    (void)b_subm(BSIZE,ma,&m[0]);
                    (void)b_subm(BSIZE,ma,&mm[0]);
                    }
                else if (f==1) {
                    (void)b_subm(BSIZE,ma,mb);
                    }

                /* compare mantissas (at most one subtraction)  */
                for (i=0;i<BSIZE;i++)
                    if (ma[i]<mb[i]) break;
                    else if (ma[i]>mb[i]) {
                        (void)b_subm(BSIZE,ma,mb);
                        f++;
                        break;
                        }
                if (i==BSIZE) {
                    (void)b_subm(BSIZE,ma,mb);
                    f++;
                    exact = TRUE;
                    }

                /* determine bits of quotient                   */
                if ( (block -= tSHIFT) <0) {
                        *mc |= (f>>-block);
                        block += B_LENGTH;
                        mc++;
                        }
                *mc |= (f<<block);
                }

        /* set lsb of quotient if remainder is non-zero         */
        if (NOT(exact))
            {
            if (NOT(b_test(BSIZE,ma)))
                {
                *mc |= LSB;
                E_TPOPP("b_tmdv")
                return;
                }
            }

        E_TPOPP("b_tmdv")
        return;
        }
