// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
#include <ctype.h>
#include <math.h>

#include <iostream>

using namespace std;

#include "FP.h"

#define EVEN(n) ((n%2)==0)
#define ODD(n) ((n%2)==1)
typedef unsigned long int ULONG;
const UINT MAX_SIGNED_INT = 2147483647UL;

const long int FP_BIAS = MAX_SIGNED_INT / 2;
const long int FP_EXPLIM = 1000000;

//**********************************************************************
//
//  Class BigFP
//
//**********************************************************************

#define ZERO(z) (z.Exponent==0)

// dump
void BigFP::Dump(ostream& s) const
{
   if(Exponent==0) {
      s << 0 << endl;
   } else {
     if(Sign) s << "-";
     s << Mantissa << "*2^" << (Exponent-FP_BIAS) << endl;
   }
   return;
}

// normalize
void BigFP::Normalize()
{
   // find unormalized bits
   int b = Mantissa.LeadingEqualBits() - 1;
   // zero
   if(b==(Mantissa.GetSize()*BITS_PR_CHAR-1)) {
      Sign = FALSE;
      Exponent = 0;
      return;
   }
   // normalize
   if(b>0) {
      Mantissa = Mantissa << b;
      Exponent = Exponent - b;
   }
   return;
}

// constructor
BigFP::BigFP(int siz,int val) : Mantissa(siz-sizeof(long int),0)
{
   Size = siz;
   // zero
   if(val==0) {
      Sign = 0;
      class BigInt tmp(siz-sizeof(long int),0);
      Mantissa = tmp;
      Exponent = 0;
      return;
   }
   // nonzero
   int absval;
   if(val>0) {
      Sign = FALSE;
      absval = val;
   } else {
      Sign = TRUE;
      absval = -val;
   }
   Exponent = FP_BIAS;
   int addbindig = (Size-sizeof(long int))*BITS_PR_CHAR -
                   int(3+log((double)absval)/log((double)2));
   class BigInt tmp(siz-sizeof(long int),absval);
   Mantissa = tmp << addbindig;
   Exponent = FP_BIAS - addbindig;
   Normalize();
}

BigFP::BigFP(int siz,double val) : Mantissa(siz-sizeof(long int),0)
{
   Size = siz;
   // zero
   if(val==0) {
      Sign = 0;
      class BigInt tmp(siz-sizeof(long int),0);
      Mantissa = tmp;
      Exponent = 0;
      return;
   }
   // nonzero
   double absval;
   if(val>0) {
      Sign = FALSE;
      absval = val;
   } else {
      Sign = TRUE;
      absval = -val;
   }
   Exponent = FP_BIAS;
   int addbindig = (Size-sizeof(long int))*BITS_PR_CHAR -
                   int(3+log((double)absval)/log((double)2));
   class BigInt tmp(siz-sizeof(long int),absval*pow(2.0,double(addbindig)));
   Mantissa = tmp;
   Exponent = FP_BIAS - addbindig;
   Normalize();
}

BigFP::BigFP(int siz,char *val) : Mantissa(siz-sizeof(long int),0)
{
   Size = siz;
   BOOL neg = FALSE;
   BOOL deci = FALSE;
   class BigFP tmp(siz,0);
   class BigFP scale(siz,1);
   for(int i=0;i<strlen(val);i++) {
      if(isdigit(val[i])) {
         tmp = 10*tmp + double(val[i]-'0');
         if(deci) scale=10*scale;
      } else {
         if(val[i]=='-') {
            neg = TRUE;
         } else if(val[i]=='.') {
            deci = TRUE;
         }
      }
   }
   tmp = tmp / scale;
   if(neg) tmp = 0 - tmp;
   *this = tmp;
}

BigFP::BigFP(const class BigFP& z) : Mantissa(z.Size-sizeof(long int),0)
{
   Size = z.Size;
   Sign = z.Sign;
   Mantissa = z.Mantissa;
   Exponent = z.Exponent;
   Normalize();
}

void BigFP::operator=(const class BigFP& z)
{
   Sign = z.Sign;
   Mantissa = z.Mantissa;
   Exponent = z.Exponent;
   Normalize();
   return;
}

void BigFP::operator=(const double z)
{
   class BigFP tmp(Size,z);
   Sign = tmp.Sign;
   Mantissa = tmp.Mantissa;
   Exponent = tmp.Exponent;
   Normalize();
   return;
}

// destructor
BigFP::~BigFP()
{
}

// operators
class BigFP operator+(const class BigFP& z1,const class BigFP& z2)
{
   class BigFP tmp(z1.Size);
   // test for zero
   if(ZERO(z1)) return z2;
   if(ZERO(z2)) return z1;
   // both nonzero
   if(!z1.Sign) {
      if(!z2.Sign) {
         // positive + positive
         if(z1.Exponent>=z2.Exponent) {
            int b = z1.Exponent - z2.Exponent;
            tmp.Mantissa = (z1.Mantissa >> 1) + (z2.Mantissa >> (b+1));
            tmp.Exponent = z1.Exponent + 1;
         } else {
            int b = z2.Exponent - z1.Exponent;
            tmp.Mantissa = (z1.Mantissa >> (b+1)) + (z2.Mantissa >> 1);
            tmp.Exponent = z2.Exponent + 1;
         }
         tmp.Normalize();
      } else {
         // positive + negative = positive - (-negative)
         class BigFP zz2 = z2;
         zz2.Sign = !z2.Sign;
         tmp = z1 - zz2;
      }
   } else {
      if(!z2.Sign) {
         // negative + positive = positive - (-negative)
         class BigFP zz1 = z1;
         zz1.Sign = !z1.Sign;
         tmp = z2 - zz1;
      } else {
         // negative + negative = -( (-negative) + (-negative) )
         class BigFP zz1 = z1;
         zz1.Sign = !z1.Sign;
         class BigFP zz2 = z2;
         zz2.Sign = !z2.Sign;
         tmp = zz1 + zz2;
         tmp.Sign = !tmp.Sign;
      }
   }
   return tmp;
}

class BigFP operator-(const class BigFP& z1,const class BigFP& z2)
{
   class BigFP tmp(z1.Size);
   // test for zero
   if(z1.Exponent==0) {
      tmp = z2;
      tmp.Sign = !tmp.Sign;
      return tmp;
   }
   if(z2.Exponent==0) return z1;
   // both nonzero
   if(!z1.Sign) {
      if(!z2.Sign) {
         // positive - positive
         if(z1.Exponent>=z2.Exponent) {
            int b = z1.Exponent - z2.Exponent;
            tmp.Mantissa = z1.Mantissa - (z2.Mantissa >> b);
            tmp.Exponent = z1.Exponent;
         } else {
            int b = z2.Exponent - z1.Exponent;
            tmp.Mantissa = (z1.Mantissa >> b) - z2.Mantissa;
            tmp.Exponent = z2.Exponent;
         }
         if(tmp.Mantissa.HighByte()>127) {
            tmp.Mantissa = 0 - tmp.Mantissa;
            tmp.Sign = !tmp.Sign;
         }
         tmp.Normalize();
      } else {
         // positive - negative = positive + (-negative)
         class BigFP zz2 = z2;
         zz2.Sign = !z2.Sign;
         tmp = z1 + zz2;
      }
   } else {
      if(!z2.Sign) {
         // negative - positive = -(positive - (-negative))
         class BigFP zz1 = z1;
         zz1.Sign = !z1.Sign;
         tmp = z2 + zz1;
         tmp.Sign = !tmp.Sign;
      } else {
         // negative - negative = -((-negative) - (-negative))
         class BigFP zz1 = z1;
         zz1.Sign = !z1.Sign;
         class BigFP zz2 = z2;
         zz2.Sign = !z2.Sign;
         tmp = zz1 - zz2;
         tmp.Sign = !tmp.Sign;
      }
   }
   return tmp;
}

class BigFP operator*(const class BigFP& z1,const class BigFP& z2)
{
   class BigFP tmp(z1.Size,0);
   // test for zero
   if(ZERO(z1)||ZERO(z2)) return tmp;
   // both nonzero
   tmp.Sign = z1.Sign ^ z2.Sign;
   tmp.Mantissa = HalfSize((DoubleSize(z1.Mantissa)*DoubleSize(z2.Mantissa))>>
                  (tmp.Mantissa.GetSize()*BITS_PR_CHAR));
   tmp.Exponent = FP_BIAS + (z1.Exponent-FP_BIAS) + (z2.Exponent-FP_BIAS) +
                  (tmp.Mantissa.GetSize()*BITS_PR_CHAR);
   tmp.Normalize();
   return tmp;
}

class BigFP operator/(const class BigFP& z1,const class BigFP& z2)
{
   // test for zero
   if(ZERO(z1)) return BigFP(z1.Size,0);
   // use iterative algorithm based on guess from highbytes of mantissa
   class BigFP leftover(z1.Size);
   class BigFP guess(z1.Size);
   class BigFP result(z1.Size,0);
   for(int i=0;i<(z1.Size/sizeof(long int)-1);i++) {
      leftover = z1 - result*z2;
      if((z1.Exponent - leftover.Exponent)>(leftover.Size-sizeof(long int))*BITS_PR_CHAR) break;
      guess = double(leftover.Mantissa.HighLong())/double(z2.Mantissa.HighLong());
      guess.Exponent = guess.Exponent + (leftover.Exponent - z2.Exponent);
      if(leftover.Sign^z2.Sign)
         result = result - guess;
      else
         result = result + guess;
   }
   return result;
}

BOOL operator==(const class BigFP& z1,const class BigFP& z2)
{
   return ((z1.Sign==z2.Sign)&&
           (z1.Mantissa==z2.Mantissa)&&
           (z1.Exponent==z2.Exponent));
}

BOOL operator!=(const class BigFP& z1,const class BigFP& z2)
{
   return (!(z1==z2));
}

BOOL operator>(const class BigFP& z1,const class BigFP& z2)
{
   class BigFP tmp = z2 - z1;
   return tmp.Sign;
}

BOOL operator<(const class BigFP& z1,const class BigFP& z2)
{
   class BigFP tmp = z1 - z2;
   return tmp.Sign;
}

BOOL operator>=(const class BigFP& z1,const class BigFP& z2)
{
   return (!(z1<z2));
}

BOOL operator<=(const class BigFP& z1,const class BigFP& z2)
{
   return (!(z1>z2));
}

class BigFP operator+(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp+z2);
}

class BigFP operator-(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp-z2);
}

class BigFP operator*(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp*z2);
}

class BigFP operator/(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp/z2);
}

BOOL operator==(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp==z2);
}

BOOL operator!=(const double z1,const class BigFP& z2)
{
   return !(z1==z2);
}

BOOL operator>(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp>z2);
}

BOOL operator<(const double z1,const class BigFP& z2)
{
   class BigFP tmp(z2.Size,z1);
   return (tmp<z2);
}

BOOL operator>=(const double z1,const class BigFP& z2)
{
   return !(z1<z2);
}

BOOL operator<=(const double z1,const class BigFP& z2)
{
   return !(z1>z2);
}

class BigFP operator+(const class BigFP& z1,const double z2)
{
   return (z2+z1);
}

class BigFP operator-(const class BigFP& z1,const double z2)
{
   double tmp = -z2;
   return (tmp+z1);
}

class BigFP operator*(const class BigFP& z1,const double z2)
{
   return z2*z1;
}

class BigFP operator/(const class BigFP& z1,const double z2)
{
   class BigFP tmp(z1.Size,z2);
   return (z1/tmp);
}

BOOL operator==(const class BigFP& z1,const double z2)
{
   return (z2==z1);
}

BOOL operator!=(const class BigFP& z1,const double z2)
{
   return (z2!=z1);
}

BOOL operator>(const class BigFP& z1,const double z2)
{
   return (z2<=z1);
}

BOOL operator<(const class BigFP& z1,const double z2)
{
   return (z2>=z1);
}

BOOL operator>=(const class BigFP& z1,const double z2)
{
   return (z2<z1);
}

BOOL operator<=(const class BigFP& z1,const double z2)
{
   return (z2>z1);
}

istream& operator>>(istream& s,class BigFP& z)
{
   char *buf = new char[3*z.Mantissa.GetSize()+3];
   s >> buf;
   z = BigFP(z.Size,buf);
   delete buf;
   return s;
}

double fp_pow10[] = { 1, 10, 100, 1000, 10000,
                      100000, 1000000, 10000000, 100000000, 1000000000 };
#define POW10(n) fp_pow10[n]

ostream& operator<<(ostream& s,const class BigFP& z)
{
   // test if negative
   if(z.Sign) {
      s << "-" << (0 - z);
      return s;
   }
   // output zero dot
   s << "0.";
   // zero
   if(ZERO(z)) {
      s << "0E0";
      return s;
   }
   // scale so that 0.1 <= x 1.0
   class BigFP tmp(z.Size);
   int e = int(log10((double)2)*(int(z.Exponent-FP_BIAS)+
                         int(z.Size-sizeof(long int))*BITS_PR_CHAR)+1);
   if((e>-10)&&(e<10)) {
      if(e>=0) {
         tmp = POW10(e);
         tmp = z / tmp;
      } else {
         tmp = POW10(-e)*z;
      }
   } else {
      tmp = 10.0;
      tmp = pow(tmp,-e)*z;
   }
   if(tmp<0.1) {
      tmp = 10.0*tmp;
      e = e - 1;
   }
   // find digits
   int l = 2*z.Mantissa.GetSize();
   int *store = new int[l];
   int i,j;
   for(i=0;i<l;i++) {
      tmp = 10*tmp;
      if(tmp<4) {
         if(tmp<2) {
            if(tmp<1) {
               j = 0;
            } else {
               j = 1;
            }
         } else {
            if(tmp<3) {
               j = 2;
            } else {
               j = 3;
            }
         }
      } else {
         if(tmp<7) {
            if(tmp<5) {
               j = 4;
            } else if(tmp<6) {
               j = 5;
            } else {
               j = 6;
            }
         } else {
            if(tmp<8) {
               j = 7;
            } else if(tmp<9) {
               j = 8;
            } else {
               j = 9;
            }
         }
      }
      tmp = tmp - j;
      store[i] = j;
   }
   // output digits
   if(store[l-1]>=5) store[l-2]=store[l-2]+1;
   for(i=(l-2);i>0;i--) {
      if(store[i]>=10) {
         store[i]=store[i]-10;
         store[i-1]=store[i-1]+1;
      }
   }
   if(store[0]>=10) {
      for(i=(l-1);i>0;i--) store[i]=store[i-1];
      store[1]=store[1]-10;
      store[0]=1;
      l=l+1;
      e=e+1;
   }
   l=l-1;
   while(store[l-1]==0) l--;
   for(i=0;i<l;i++) s << char('0'+store[i]);
   delete store;
   // output exponent
   s << "E" << e;
   return s;
}

// functions
const double signval[2] = { 1, -1 };
#define SIGN(n) signval[(n)%2]
#define POW2(i) (1<<(i))

int trunc(const class BigFP& z)
{
   if(ZERO(z)) return 0;
   class BigInt tmp1(z.Size-sizeof(long int));
   tmp1 = z.Mantissa << (z.Exponent - FP_BIAS);
   int tmp2 = intExtract(tmp1);
   if(z.Sign) tmp2 = -tmp2;
   return tmp2;
}

class BigFP inv(const class BigFP& z)
{
   class BigFP one(z.Size,1);
   class BigFP tmp(z.Size);
   tmp.Sign = one.Sign ^ z.Sign;
   int b = tmp.Mantissa.GetSize()*BITS_PR_CHAR - 2;
   tmp.Mantissa = HalfSize((DoubleSize(one.Mantissa)<<b)/
                  DoubleSize(z.Mantissa));
   tmp.Exponent = FP_BIAS + (one.Exponent-FP_BIAS) - (z.Exponent-FP_BIAS) - b;
   tmp.Normalize();
   return tmp;
}

class BigFP sqrt(const class BigFP& z)
{
   if(ZERO(z)) return BigFP(z.Size,0);
   class BigFP tmp = z;
   tmp.Exponent = FP_BIAS + (tmp.Exponent-FP_BIAS)/2 -
                  (BITS_PR_CHAR*tmp.Mantissa.GetSize())/2;
   for(int i=0;i<z.Size/2;i++) tmp = (tmp+z/tmp)/2;
   return tmp;
}

class BigFP pow(const class BigFP& z,int p)
{
   if(p<0) return 1.0/pow(z,-p);
   if(p==0) return BigFP(z.Size,1.0);
   if(p==1) return z;
   if(p==2) return z*z;
   if(ODD(p)) {
      return z*pow(z,p-1);
   } else {
      class BigFP tmp = pow(z,p/2);
      return tmp*tmp;
   }
}

class BigFP pow(const class BigFP& z,double p)
{
   return exp(p*log(z));
}

class BigFP exp_help(const class BigFP& z)
{
   class BigFP tmp(z.Size,1);
   class BigFP q1(z.Size,1);
   class BigFP q2(z.Size,1);
   for(int i=1;i<=2*z.Size;i++) {
      q1 = q1 * z;
      q2 = q2 * i;
      tmp = tmp + q1/q2;
   }
   return tmp;
}

class BigFP exp(const class BigFP& z)
{
   if(z==0) return 1;
   if(z<0) {
      return 1/exp(0-z);
   }
   if(z>1) {
      class BigFP tmp = exp(z/2);
      return tmp*tmp;
   } else {
      return exp_help(z);
   }
}

class BigFP log_help(const class BigFP& z)
{
   class BigFP z1 = z-1;
   class BigFP tmp(z.Size,0);
   class BigFP q(z.Size,1);
   for(int i=1;i<=4*z.Size;i++) {
      q = q * z1;
      tmp = tmp + SIGN(i-1)*q/i;
   }
   return tmp;
}

class BigFP log(const class BigFP& z,const class BigFP& logtwo)
{
   if(z>1) {
      return logtwo+log(z/2,logtwo);
   } else {
      return log_help(z);
   }
}

class BigFP log(const class BigFP& z)
{
   return log(z,2*log_help(sqrt(BigFP(z.Size,2))));
}

char SPI120[] = "3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066";

class BigFP pi_help(int n)
{
   // test if fast calculation from string is possible
   if((n-sizeof(long int))<=40) return BigFP(n,SPI120);
   // Tamura Kanada
   class BigFP a(n),b(n),c(n),aold(n);
   a = 1;
   b = 1/sqrt(BigFP(n,2));
   c = 0.25;
   for(int i=0;i<n;i++) {
      aold = a;
      a = (a+b)/2;
      b = sqrt(b*aold);
      c = c - POW2(i)*(a-aold)*(a-aold);
   }
   return (a+b)*(a+b)/(4*c);
}

class BigFP cos_help(const class BigFP& z)
{
   class BigFP tmp(z.Size,1);
   class BigFP q1(z.Size,1);
   class BigFP q2(z.Size,1);
   for(int i=1;i<=(3*z.Size)/2;i++) {
      q1 = q1 * z * z;
      q2 = q2 * (2*i-1) * (2*i);
      tmp = tmp + SIGN(i)*q1/q2;
   }
   return tmp;
}


class BigFP cos(const class BigFP& z)
{
   class BigFP pi = pi_help(z.Size);
   class BigFP two_pi = 2*pi;
   class BigFP half_pi = pi/2;
   class BigFP z2 = z - trunc(z/two_pi)*two_pi;
   if(z2<half_pi)
      return cos_help(z2);
   else if(z2<pi)
      return (0-sin_help(z2-half_pi));
   else
      return (0-cos_help(z2-pi));
}

class BigFP sin_help(const class BigFP& z)
{
   class BigFP tmp(z);
   class BigFP q1(z);
   class BigFP q2(z.Size,1);
   for(int i=1;i<=(3*z.Size)/2;i++) {
      q1 = q1 * z * z;
      q2 = q2 * (2*i) * (2*i+1);
      tmp = tmp + SIGN(i)*q1/q2;
   }
   return tmp;
}

class BigFP sin(const class BigFP& z)
{
   class BigFP pi = pi_help(z.Size);
   class BigFP two_pi = 2*pi;
   class BigFP half_pi = pi/2;
   class BigFP z2 = z - trunc(z/two_pi)*two_pi;
   if(z2<half_pi)
      return sin_help(z2);
   else if(z2<pi)
      return cos_help(z2-half_pi);
   else
      return (0-sin_help(z2-pi));
}

class BigFP tan(const class BigFP& z)
{
   class BigFP pi = pi_help(z.Size);
   class BigFP z2 = z - trunc(z/pi)*pi;
   class BigFP tmp = cos_help(z2);
   if(tmp.Sign)
      return 0-sqrt(1.0/(tmp*tmp)-1.0);
   else
      return sqrt(1.0/(tmp*tmp)-1.0);
}

//**********************************************************************
//
//  Class BigFP16
//
//**********************************************************************

// constructor
BigFP16::BigFP16(double val) : BigFP(16,val)
{
}

void BigFP16::operator=(const class BigFP& z)
{
   *((class BigFP *)this) = z;
   return;
}

// destructor
BigFP16::~BigFP16()
{
}

//**********************************************************************
//
//  Class BigFP32
//
//**********************************************************************

// constructor
BigFP32::BigFP32(double val) : BigFP(32,val)
{
}

void BigFP32::operator=(const class BigFP& z)
{
   *((class BigFP *)this) = z;
   return;
}

// destructor
BigFP32::~BigFP32()
{
}

