/**************************************************************************/
/*                                                                        */
/*                                 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_ieee.h,v 1.3 1997/09/25 16:48:02 ae59 Exp $ */
/*
$Log: t_ieee.h,v $
 * Revision 1.3  1997/09/25  16:48:02  ae59
 * Copyright-message
 *
 * Revision 1.2  1996/03/22  15:19:13  ae59
 * Hardware support for RS6000 with precise and imprecise (default) traps
 *
 * Revision 1.1  1994/01/27  13:25:32  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : t_ieee.h                              */
/*                                                              */
/*      Date    : 1993-12-22                                    */
/*      1993-12-22 : CLOCKS_PER_SEC indicator removed      =b=  */
/*      1992-10-19 : conditional definition on CHOP...     =b=  */
/*      1992-07-23 : changed def of ExtReal for SUN4_CPP_c =b=  */
/*      1992-07-22 : add atanee2               =b=              */
/*      1992-07-21 : add sun4_gnu_c sun4_cpp_c =b=              */
/*      1992-06-23 : switched back to 80bit SW (SPARC)          */
/*      1992-05-11 : support for SPARC FP HW                    */
/*      1991-12-30 : CLOCKS_PER_SEC used as indicator for       */
/*                   o_defs.h to be included                    */
/*      1991-12-19 : macro ExtExpMask changed to 0x7FFF         */
/*      1991-12-02 : #define const   in any case                */
/*      1991-12-02 : t_srnd() prototype moved to t_fcth.h       */
/*      1991-08-05 : no prototypes for b_t?[) in this file      */
/*                   do not #undef extern                       */
/****************************************************************/
/*#include "c:\cmp\c\include\stdio.h"  */
/*#include "c:\cmp\c\include\math.h"   */
/*#include "c:\cmp\c\include\limits.h" */
/* #include <math.h> */ /* 121090 Baumhof, siehe auch exc.h */
#include <stdio.h>      /* FILE in fprinte()                */
/* entfernt 080591 wegen Kollision von size_t in sys/types.h und o_defs.h */
/* #include <limits.h>  */ /* CHAR_BIT fuer BitsPerDigit       */
#ifndef CHAR_BIT
#define CHAR_BIT 8         /* aus limits.h 080591 cb           */
#endif
#ifdef AIX
/* #ifndef CLOCKS_PER_SEC */
#include "/u/p88c/runtime/o_defs.h"
/* #endif*/
#include "/u/p88c/runtime/tbyte/t_exc_.h"
#include "/u/p88c/runtime/tbyte/t_mach.h"
#else
/* #ifndef CLOCKS_PER_SEC */
#include "o_defs.h"
/* #endif */
#include "t_exc_.h"        /* ieee exception codes             */
#include "t_mach.h"       /* Maschinenabhaengigkeiten         */
#endif
/* ---------------------------------------------------------*/

#ifdef ANSI_C
#define const
#else
#define const
#endif

/* ------------------------------------------------------------- */
/* Typ Declarationen                                             */
/* ------------------------------------------------------------- */
#define LongReal EByte  /* IEEE basic format                     */
#if SUN4_CPP_C
#define ExtReal  TByte  /* IEEE extended format                  */
/*typedef char ExtReal[t_size];*/
#else
#define ExtReal  TByte  /* IEEE extended format                  */
#endif

/* --- Digit --- */
/* moved to o_type.h
typedef unsigned char Digit;
*/
                                             /* eine MantissenStelle  */

/* Anzahl Bits pro Digit */
#define BitsPerDigit         ((int)(sizeof(Digit)*CHAR_BIT))

/* --- ExtReal Format --- */   /* 030990 Baumhof "short" eingefuegt */
/* moved to o_type.h
typedef unsigned short int  ExtExp;
*/
/* Typ Exponent        */
#define ExtExpBias (ExtExp) (0x3FFF)         /* Exponent Bias       */
#define ExtExpMask (ExtExp) (0x7FFF)         /* Exponent Maske      */
/* moved to o_type.h
#define ExtMantLen 8
*/
                                             /* Laenge Mantisse     */
#define ExtSignMask (ExtExp) (0x8000)        /* Vorzeichen Maske    */

/* --- EightByte: 64-bit-IEEE-Gleitpunktzahl                        */

#ifdef IEEE_HARDWARE
#if SUN4_OS4_C+SUN4_GNU_C+SUN4_CPP_C
/* #define __CB_SUN4_OS4_HARDWARE */
#endif
#endif

#ifdef __CB_SUN4_OS4_HARDWARE
typedef double EByte;
#else
typedef struct { char c[8]; } EByte;
#endif

/* --- TenByte: Typ wird im Intel 80x86 Assembler zur Darstellung von
   Extended Gleitpunktzahlen fuer die 80x87 Coprozessoren verwendet ---  */

/* moved to o_type.h
typedef union {
   char c[10];
   struct s{
#if LSBFIRST
      Digit  digit[ExtMantLen];
      ExtExp exp;
#else
      ExtExp exp;
      Digit  digit[ExtMantLen];
#endif
   } s;
} TByte; 
*/

/* Makro zum Zugriff auf die Digits im Tenbyte-Format  080391 cb */
/* Verwendung: tenbyte.s.DIGIT(0..7) */
#if LSBFIRST
#  define DIGIT(a) digit[a]
#else
#  define DIGIT(a) digit[7-(a)]
#endif

/* --- Intervalle --- */
typedef struct IExtReal{
   ExtReal u;
   ExtReal l;
} IExtReal;

typedef struct ILongReal{
   LongReal u;
   LongReal l;
} ILongReal;
/* --- end of type declarations --- */

/* ------------------------------------------------------------- */
/* Grundoperationen umleiten auf Cordes-80-bit-Routinen          */
/* ------------------------------------------------------------- */

#define addee b_tadd
#define subee b_tsub
#define mulee b_tmul
#define divee b_tdiv

#define setrndmode t_srnd
#define getrndmode t_grnd

/* -------------------------------------------------------------- */
/* Spezialfallabfrage bei Intervallfunktionen                     */
/* -------------------------------------------------------------- */
#define INT_HPREC       1

/* -------------------------------------------------------------- */
/* Funktions Declarationen                                        */
/* -------------------------------------------------------------- */

#ifdef LINT_ARGS

/* --- Arithmetik --- */
/* already declared in b_fcth.h
extern int  addee(const  ExtReal *arg1, const  ExtReal *arg2,  ExtReal *res);
extern int  subee(const  ExtReal *arg1, const  ExtReal *arg2,  ExtReal *res);
extern int  mulee(const  ExtReal *arg1, const  ExtReal *arg2,  ExtReal *res);
extern int  divee(const  ExtReal *arg1, const  ExtReal *arg2,  ExtReal *res);
*/

extern int  sqrtee  (const  ExtReal *arg,  ExtReal *res);
extern int  sqrtm1ee(const  ExtReal *arg,  ExtReal *res);
extern int isqrtee  (const IExtReal *arg, IExtReal *res);

/* --- Punktstandardfunktionen-- */
extern int sinee(const ExtReal *arg,  ExtReal *res);
extern int cosee(const ExtReal *arg,  ExtReal *res);
extern int tanee(const ExtReal *arg,  ExtReal *res);
extern int cotee(const ExtReal *arg,  ExtReal *res);

extern int asinee(const ExtReal *arg,  ExtReal *res);
extern int acosee(const ExtReal *arg,  ExtReal *res);
extern int atanee(const ExtReal *arg,  ExtReal *res);
extern int atanee2(const ExtReal *arg,  const ExtReal *y, ExtReal *res);
extern int acotee(const ExtReal *arg,  ExtReal *res);

extern int expm1ee(const ExtReal *arg,  ExtReal *res);
extern int lnp1ee (const ExtReal *arg,  ExtReal *res);

extern int expee (const ExtReal *arg,  ExtReal *res);
extern int lnee  (const ExtReal *arg,  ExtReal *res);
extern int sinhee(const ExtReal *arg,  ExtReal *res);
extern int coshee(const ExtReal *arg,  ExtReal *res);
extern int tanhee(const ExtReal *arg,  ExtReal *res);
extern int cothee(const ExtReal *arg,  ExtReal *res);

extern int asinhee(const ExtReal *arg,  ExtReal *res);
extern int acoshee(const ExtReal *arg,  ExtReal *res);
extern int atanhee(const ExtReal *arg,  ExtReal *res);
extern int acothee(const ExtReal *arg,  ExtReal *res);

extern int powee  (const ExtReal *bas,  const ExtReal *exp, ExtReal *res);
extern int logaee (const ExtReal *arg,  const ExtReal *bas, ExtReal *res);

/* --- Intervallstandardfunktionen-- */
extern int isinee   (const IExtReal *arg,  IExtReal *res);
extern int icosee   (const IExtReal *arg,  IExtReal *res);
extern int itanee   (const IExtReal *arg,  IExtReal *res);
extern int icotee   (const IExtReal *arg,  IExtReal *res);

extern int iasinee  (const IExtReal *arg,  IExtReal *res);
extern int iacosee  (const IExtReal *arg,  IExtReal *res);
extern int iatanee  (const IExtReal *arg,  IExtReal *res);
extern int iacotee  (const IExtReal *arg,  IExtReal *res);

extern int ilnee    (const IExtReal *arg,  IExtReal *res);
extern int iexpee   (const IExtReal *arg,  IExtReal *res);
extern int isinhee  (const IExtReal *arg,  IExtReal *res);
extern int icoshee  (const IExtReal *arg,  IExtReal *res);
extern int itanhee  (const IExtReal *arg,  IExtReal *res);
extern int icothee  (const IExtReal *arg,  IExtReal *res);

extern int iasinhee (const IExtReal *arg,  IExtReal *res);
extern int iacoshee (const IExtReal *arg,  IExtReal *res);
extern int iatanhee (const IExtReal *arg,  IExtReal *res);
extern int iacothee (const IExtReal *arg,  IExtReal *res);

extern int ipowee   (const IExtReal *bas,const IExtReal *exp,IExtReal *res);

#else

/* already declared in b_fcth.h
extern int addee(), divee(), subee(), mulee();
*/
extern int sqrtee(), sqrtm1ee(), isqrtee();

extern int sinee(), cosee(), tanee(), cotee(), asinee(), acosee();
extern int atanee(), atanee2(), acotee(), expm1ee(), lnp1ee(), expee(), lnee();
extern int sinhee(), coshee(), tanhee(), cothee(), asinhee(), acoshee();
extern int atanhee(), acothee(), powee(), logaee();

extern int isinee(), icosee(), itanee(), icotee(), iasinee(), iacosee();
extern int iatanee(), iacotee(), ilnee(), iexpee(), isinhee(), icoshee();
extern int itanhee(), icothee(), iasinhee();
extern int iacoshee(), iatanhee(), iacothee();
extern int ipowee();

#endif /* LINT_ARGS */

/* ------------------------------------------------------------ */
/* weitere Funktionen                                           */
/* ------------------------------------------------------------ */
/* --- Vorzeichen --- */
#define POS 1
#define NEG (-POS)
/* --- ExtReal Vorzeichen, result = NEG or POS --- */
#define SGNE(arg) ((int)(((arg)->s.exp&ExtSignMask)==0? POS: NEG))

#ifdef LINT_ARGS

/*--- convert --- */
extern int  extreal_to_longreal(const  ExtReal  *arg,  LongReal *res);
/* --- unused ---
extern int iextreal_to_longreal(const IExtReal  *arg, ILongReal *res);
*/
extern int  longreal_to_extreal(const  LongReal *arg,  ExtReal  *res);
/* --- unused ---
extern int ilongreal_to_extreal(const ILongReal *arg, IExtReal  *res);
*/

extern int  extreal_to_int(const  ExtReal  *arg,  int *res);

/*--- Vergleich --- */
/* cmp == -1 falls arg1<arg2; 0 falls arg1==arg2; 1 falls arg1>arg2 */
extern int  cmpee    (const  ExtReal  *arg1, const  ExtReal  *arg2);
extern int  cmpabsee (const  ExtReal  *arg1, const  ExtReal  *arg2);

/*--- print ExtReal --- */
extern int  printe(const ExtReal *arg);
extern int  fprinte(FILE *stream, const ExtReal *arg);

/*--- Vorzeichenwechsel --- */
extern int  chsee(const  ExtReal *arg, ExtReal *res);
extern int ichsee(const IExtReal *arg,IExtReal *res);

/*--- Extract: Teile in Mantisse und Exponent --- */
extern int xtracte(const ExtReal *arg, ExtReal *rmant, ExtReal *rexp);
extern int xtrexpe(const ExtReal *arg, int *exponent);

/*--- absolute value --- */
extern int  absee(const  ExtReal *arg, ExtReal *res);
extern int iabsee(const IExtReal *arg,IExtReal *res);

/*--- copy --- */
extern int  copyee(const  ExtReal *arg,  ExtReal *res);
extern int icopyee(const IExtReal *arg, IExtReal *res);

#else

extern int extreal_to_longreal() /* , iextreal_to_longreal()  */ ;
extern int longreal_to_extreal() /* , ilongreal_to_extreal()  */ ;
extern int extreal_to_int();
extern int cmpee(), cmpabsee();
extern int printe(), fprinte();
extern int chsee(), ichsee();
extern int xtracte(), xtrexpe();
extern int absee(), iabsee();
extern int copyee(), icopyee();

#endif /* LINT_ARGS */

/* --------------------------------------------------------------------*/
/* RundungsModi setzen und abfragen                                    */
/*    z.B.: setrndmode(DOWN);             Rundung nach oben setzen     */
/*          if(NEAR==getrndmode()) ...    falls Rundung nach unten ... */
/* --------------------------------------------------------------------*/
#ifdef LINT_ARGS
#ifdef ANSI_C
/* extern int setrndmode(int rndmod);     set rounding control   */
extern int getrndmode(void);           /* get rounding control   */
#else
/* extern int setrndmode(int rndmod); */
extern int getrndmode(void);
#endif
#else  /* NOT LINT_ARGS */
extern int /* setrndmode(), */ getrndmode();
#endif /* LINT_ARGS */

/* --- RoundModes --- */
/* maybe this macros have been already defined in a_defs(Ibm_370 sensibility */
#ifndef DOWN
#define DOWN (-UP)
#endif
#ifndef NEAR
#define NEAR 0
#endif
#ifndef UP  
#define UP   1
#endif
#ifndef CHOP
#define CHOP 2
#endif

#ifdef LINT_ARGS

/* --- RundungsFunktionen --- */
extern int iround_rel(const IExtReal *arg,
                      const ExtReal *eps_rel,IExtReal *res);
extern int iround_abs(const IExtReal *arg,
                      const ExtReal *eps_abs,IExtReal *res);

extern int round_rel(int rnd,const ExtReal *arg,
                     const ExtReal *eps_rel,ExtReal *res);
extern int round_abs(int rnd,const ExtReal *arg,
                     const ExtReal *eps_abs,ExtReal *res);

extern int sround(int rnd, const ExtReal *arg, const ExtReal *eps_rel,
                   const ExtReal *eps_abs, ExtReal *res);

/* --- Rundung auf ganze Zahl --- */
extern int  rndintee (const  ExtReal *arg,  ExtReal *res);
extern int irndintee (const IExtReal *arg, IExtReal *res);

/* --- end of function declarations --- */

#else

extern int iround_rel(), iround_abs(), round_rel(), round_abs(), sround();
extern int rndintee(), irndintee();

#endif /* LINT_ARGS */

/* --- macros ---------------------------------------------------*/
#define ADDULP(arg) addee(arg, &MinExtReal, arg)      /* + ulp   */
#define SUBULP(arg) subee(arg, &MinExtReal, arg)      /* - ulp   */

/* ------------------------------------------------------------------- */
/* FehlerBehandlung                                                    */
/* ieee_matherr liegt als Souce vor und ist vom Anwender veraenderbar  */
/* Rueckgabe                                                           */
/*    TakeOver:                                                        */
/*       Uebernahme type als RueckgabeWert der Abgebrochenen IeeeFunktion */
/*       Uebernahme res als Ergebnis der Abgebrochenen IeeeFunktion    */
/*       keine weitere Standard Fehlerbehandlung                       */
/*    NoTakeOver:                                                      */
/*       Standard Fehlerbehandlung                                     */
/* ------------------------------------------------------------------- */
struct ieee_exception{
         int      type;  /* FehlerArt, kann veraendert werden (s.o.)    */
   const char     *name; /* Name der Abgebrochenen Funktion (string)    */
   const IExtReal *arg1; /* Argument 1                                  */
   const IExtReal *arg2; /* Argument 2                                  */
         IExtReal *res;  /* Ergebnis, kann veraendert werden (TakeOver) */
};
#ifdef LINT_ARGS
extern int ieee_matherr(struct ieee_exception *exception);
#else
extern int ieee_matherr();
#endif /* LINT_ARGS */
#define TakeOver   1
#define NoTakeOver 0

/* -------------------------------------------------------------------- */
/* Konstanten                                                           */
/* -------------------------------------------------------------------- */
extern const ExtReal Zero;        /* 0                                  */
extern const ExtReal Half;        /* 1/2                                */
extern const ExtReal Quarter;     /* 1/4                                */
extern const ExtReal ThreeQuart;  /* 3/4                                */
extern const ExtReal One;         /* 1                                  */
extern const ExtReal Two;         /* 2                                  */
extern const ExtReal Four;        /* 4                                  */
extern const ExtReal Eight;       /* 8                                  */
extern const ExtReal MinusOne;    /* -1                                 */
extern const ExtReal MinExtReal;  /* kleinste denormale ExtReal         */
extern const ExtReal MinNormExtReal;/* kleinste normale ExtReal (positiv) */
extern const ExtReal MaxNormExtReal;/* groesste normale ExtReal (positiv) */
extern const ExtReal SqrtHalf;    /* 1/sqrt(2)                (near)    */
extern const ExtReal TwoPow63;    /* 2**63                              */
extern const ExtReal TwoPow127;   /* 2**127                             */
extern const ExtReal Pi;          /* pi                       (near)    */
extern const ExtReal MinusPi;     /* -pi                      (near)    */
extern const ExtReal PiHalf;      /* pi/2                     (near)    */
extern const ExtReal MinusPiHalf; /* -pi/2                    (near)    */
extern const ExtReal PiQuart;     /* pi/4                     (near)    */
extern const ExtReal PiDiv8;      /* pi/8                     (near)    */
extern const ExtReal PiDiv16;     /* pi/16                    (near)    */
extern const ExtReal Pi3Div16;    /* pi*3/16                  (near)    */
extern const ExtReal SqrtTwo;     /* sqrt(2)                  (near)    */
extern const ExtReal Ln2;         /* ln(2)                    (near)    */
/* log2(e)=1/ln(2)          (near)    */
/* --- eigene Konstante in dexp ---
extern const ExtReal LdE;
*/
extern const ExtReal PInfty;      /* +Infinity                          */
extern const ExtReal MInfty;      /* -Infinity                          */

extern const IExtReal IOne;
extern const IExtReal IHalf;
extern const IExtReal IPi;
extern const IExtReal IPiHalf;
extern const IExtReal ITwoDivPi;
extern const IExtReal ISqrtTwo;

/* ------------------------------------------------------------------ */

#ifdef ANSI_C
#else
#undef const
/*
#undef extern
*/
#endif

/* ------------------------------------------------------------------- */
