/**************************************************************************/
/*                                                                        */
/*                                 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: t_dnrm.c,v 1.2 1997/09/25 18:11:52 ae59 Exp $ */
/*
$Log: t_dnrm.c,v $
 * Revision 1.2  1997/09/25  18:11:52  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:25:16  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : t_dnrm.c                              */
/*                                                              */
/*      Date    : 1991-09-06                                    */
/*      1991-09-06 : reduce maximum line length                 */
/*      1991-08-05 : remove unused variables ret, i , rnd       */
/*      1991-06-21 : new routine                                */
/****************************************************************/

#ifdef AIX
#include "/u/p88c/runtime/tbyte/t_ddev.h"
#else
#include "t_ddev.h"
#endif

#ifdef LINT_ARGS
static int ret_zero(int sgn, DReal *res);
#else
static int ret_zero();
#endif
/*--------------------------------------------------------------*/
/* normalize DReal                                              */
/*--------------------------------------------------------------*/

#ifdef ANSI_C
#ifdef LINT_ARGS
int normd(const DReal *arg, DReal *res)
#else
int normd(arg, res)
const DReal *arg;
      DReal *res;
#endif /* LINT_ARGS */
#else  /* NOT ANSI_C */
#ifdef LINT_ARGS
int normd(DReal *arg, DReal *res)
#else
int normd(arg, res)
DReal *arg;
DReal *res;
#endif /* LINT_ARGS */
#endif /* ANSI_C */
{
   DReal d;
   DExp  e;
   Digit highdigit;

   if(arg->s == 0) return ret_zero(0, res);
   copydd(arg, &d);                     /* falls *res == *arg */

   if(d.m.digit[DMantLen]==0) {
      if(1==dmadjust(&d.m, DMantLen, &(res->m), &e))
         return ret_zero(d.s, res);
   }
   else {
      highdigit = d.m.digit[DMantLen];
      for(e=0; highdigit; highdigit >>=1, e++);
      dmshift(e, &d.m, &(res->m));
   }
   res->s = d.s;
   res->e = d.e + e;

   return NoErr;
} /* normd() */

/*--------------------------------------------------------------*/
#ifdef LINT_ARGS
static int ret_zero(int sgn, DReal *res)
#else
static int ret_zero(sgn, res)
int   sgn;
DReal *res;
#endif
{
   initd(res);
   if(sgn!=NEG)
      return ExcPZero;
   else
      return ExcMZero;
} /* ret_zero */
