/**************************************************************************/
/*                                                                        */
/*                                 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_dcmp.c,v 1.2 1997/09/25 18:10:05 ae59 Exp $ */
/*
$Log: t_dcmp.c,v $
 * Revision 1.2  1997/09/25  18:10:05  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:25:10  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : t_dcmp.c                              */
/*                                                              */
/*      Date    : 1991-09-06                                    */
/*      1991-09-06 : reduce maximum line length                 */
/*      1991-08-05 : cast (int) in front of sizeof()            */
/*      1991-06-21 : new routine                                */
/****************************************************************/

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

#ifdef ANSI_C
#ifdef LINT_ARGS
static int mantcmp(const DMant *arg1, const DMant *arg2);
#else
static int mantcmp();
#endif /* LINT_ARGS */
#else  /* NOT ANSI_C */
#ifdef LINT_ARGS
static int mantcmp(DMant *arg1, DMant *arg2);
#else
static int mantcmp();
#endif /* LINT_ARGS */
#endif /* ANSI_C */

/*--------------------------------------------------------------*/
/* cmpdd return 1 if arg1>arg2, 0 if ==, -1 if <                */
/*--------------------------------------------------------------*/
#ifdef LINT_ARGS
int cmpdd(DReal *arg1, DReal *arg2)
#else
int cmpdd(arg1, arg2)
DReal   *arg1;
DReal   *arg2;
#endif /* LINT_ARGS */
{
   Digit highdigit, h1, h2;
   DMant m;
   register DExp  e1;
   register DExp  e2;

   /* --- compare sign --- */
   if(arg1->s!=arg2->s)
      return (int)(arg1->s > arg2->s ? 1 : -1);

   if(arg1->s==0)
      return 0;

   /* --- compare exponent --- */
   highdigit = h1 = arg1->m.digit[DMantLen];
   for(e1=arg1->e; highdigit; highdigit >>=1, e1++);

   highdigit = h2 = arg2->m.digit[DMantLen];
   for(e2=arg2->e; highdigit; highdigit >>=1, e2++);

   if (e1!=e2)
      return (int)(e1>e2 ? arg1->s : -arg1->s);

   /* --- compare mant --- */
   if(h1) {
      dmshift(h1, &(arg1->m), &m);
      memcpy(&(arg1->m), &m, sizeof(DMant));
      arg1->e = e1;
   }
   if(h2) {
      dmshift(h2, &(arg2->m), &m);
      memcpy(&(arg2->m), &m, sizeof(DMant));
      arg2->e = e2;
   }
   return mantcmp(&(arg1->m), &(arg2->m));

} /* cmpdd() */

/*--------------------------------------------------------------*/
/* cmpabsdd return 1 if abs(arg1) > abs(arg2), 0 if ==, -1 if < */
/*--------------------------------------------------------------*/
#ifdef LINT_ARGS
int cmpabsdd(DReal *arg1, DReal *arg2)
#else
int cmpabsdd(arg1, arg2)
DReal   *arg1;
DReal   *arg2;
#endif /* LINT_ARGS */
{
   Digit highdigit, h1, h2;
   DMant m;
   register DExp  e1;
   register DExp  e2;

   /* --- compare sign --- */
   if(arg1->s==0 && arg2->s==0)
      return 0;
   if(arg1->s==0)
      return -1;
   if(arg2->s==0)
      return 1;

   /* --- compare exponent --- */
   highdigit = h1 = arg1->m.digit[DMantLen];
   for(e1=arg1->e; highdigit; highdigit >>=1, e1++);

   highdigit = h2 = arg2->m.digit[DMantLen];
   for(e2=arg2->e; highdigit; highdigit >>=1, e2++);

   if (e1!=e2)
      return (int)(e1>e2 ? 1 : -1);

   /* --- compare mant --- */
   if(h1) {
      dmshift(h1, &(arg1->m), &m);
      memcpy(&(arg1->m), &m, sizeof(DMant));
      arg1->e = e1;
   }
   if(h2) {
      dmshift(h2, &(arg2->m), &m);
      memcpy(&(arg2->m), &m, sizeof(DMant));
      arg2->e = e2;
   }
   return mantcmp(&(arg1->m), &(arg2->m));

} /* cmpabsdd() */

/*--------------------------------------------------------------*/
#ifdef ANSI_C
#ifdef LINT_ARGS
static int mantcmp(const DMant *arg1, const DMant *arg2)
#else
static int mantcmp(arg1, arg2)
const DMant *arg1;
const DMant *arg2;
#endif /* LINT_ARGS */
#else  /* NOT ANSI_C */
#ifdef LINT_ARGS
static int mantcmp(DMant *arg1, DMant *arg2)
#else
static int mantcmp(arg1, arg2)
DMant *arg1;
DMant *arg2;
#endif /* LINT_ARGS */
#endif /* ANSI_C */
{
   register int i;

   for(i=(int)sizeof(DMant);
       --i>=0 && arg1->digit[i]==arg2->digit[i];);
   if(i<0) return 0;
   return (arg1->digit[i]>arg2->digit[i]? 1 : -1);
} /* mantcmp() */
