// 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 <string.h>
#include <ctype.h>
#include <math.h>

#include <iostream>

using namespace std;

#include "Int.h"

#define MAX(a,b) ((a>b)?a:b)
#define MIN(a,b) ((a<b)?a:b)
#define EVEN(n) ((n%2)==0)
#define ODD(n) ((n%2)==1)
typedef unsigned char UCHAR;
typedef int BOOL;

//**********************************************************************
//
//  Class BigInt
//
//**********************************************************************

#define LTZERO(x) (x.HighByte()>127)

// dump
void BigInt::Dump(ostream& s) const
{
   s << Size << ":";
   for(int i=(Size-1);i>=0;i--) s << " " << int(Data[i]);
   s << endl;
   return;
}

// constructor
BigInt::BigInt(int siz,int val)
{
   Size = siz;
   Data = new UCHAR[Size];
   int tmp = val;
   UCHAR *p = (UCHAR *)&tmp;
   int i;
   for(i=0;i<sizeof(int);i++) Data[i] = p[i];
   if(val>=0)
      for(i=sizeof(int);i<Size;i++) Data[i] = 0;
   else
      for(i=sizeof(int);i<Size;i++) Data[i] = 255;
}

BigInt::BigInt(int siz,double val)
{
   Size = siz;
   Data = new UCHAR[Size];
   int ix = 0;
   double tmp = val;
   while(fabs(tmp)>=1) {
      Data[ix] = int(fmod(tmp,256));
      tmp = tmp/256;
      ix++;
   }
   int i;
   if(val>=0)
      for(i=ix;i<Size;i++) Data[i] = 0;
   else
      for(i=ix;i<Size;i++) Data[i] = 255;
}

BigInt::BigInt(int siz,char *val)
{
   Size = siz;
   Data = new UCHAR[Size];
   class BigInt tmp(siz,0);
   BOOL neg = FALSE;
   for(int i=0;i<strlen(val);i++) {
      if(isdigit(val[i])) {
         tmp = 10*tmp + (val[i]-'0');
      } else {
         neg = TRUE;
      }
   }
   if(neg) tmp = 0 - tmp;
   *this = tmp;
}

BigInt::BigInt(const class BigInt& z)
{
   Size = z.Size;
   Data = new UCHAR[Size];
   for(int i=0;i<Size;i++) Data[i] = z.Data[i];
}

void BigInt::operator=(const class BigInt& z)
{
   for(int i=0;i<Size;i++) Data[i] = z.Data[i];
   return;
}

void BigInt::operator=(const int z)
{
   class BigInt tmp(Size,z);
   for(int i=0;i<Size;i++) Data[i] = tmp.Data[i];
   return;
}

// leading equal bits (0's or 1's)
int BigInt::LeadingEqualBits() const
{
   int tmp = 0;
   int ix = Size - 1;
   int b;
   if(Data[ix]<128) {
      // positive => look for leading 0's
      while((Data[ix]==0)&&(ix>0)) {
         tmp = tmp + BITS_PR_CHAR;
         ix--;
      }
      b =  128;
      while(Data[ix]<b) {
         tmp++;
         b = b >> 1;
      }
   } else {
      // negative => look for leading 1's
      while((Data[ix]==255)&&(ix>0)) {
         tmp = tmp + BITS_PR_CHAR;
         ix--;
      }
      b =  128;
      while(Data[ix]>=b) {
         tmp++;
         b = b >> 1;
      }
      tmp--;
   }
   return tmp;
}

// destructor
BigInt::~BigInt()
{
   delete Data;
}

// operators
class BigInt operator+(const class BigInt& z1,const class BigInt& z2)
{
   class BigInt tmp(z1.Size);
   int result,carry;
   carry = 0;
   for(int i=0;i<tmp.Size;i++) {
      result = z1.Data[i] + z2.Data[i] + carry;
      if(result<256) {
         carry = 0;
      } else {
         result = result - 256;
         carry = 1;
      }
      tmp.Data[i] = result;
   }
   return tmp;
}

class BigInt operator-(const class BigInt& z1,const class BigInt& z2)
{
   class BigInt tmp(z1.Size);
   int result,borrow;
   borrow = 0;
   for(int i=0;i<tmp.Size;i++) {
      result = z1.Data[i] - z2.Data[i] - borrow;
      if(result>=0) {
         borrow = 0;
      } else {
         result = result + 256;
         borrow = 1;
      }
      tmp.Data[i] = result;
   }
   return tmp;
}

class BigInt operator*(const class BigInt& z1,const class BigInt& z2)
{
   // basic algorithm: (a)(b) * (c)(d) = (ac)(0)(0) + (ad)(0) + (bc)(0) + (bd)
   class BigInt tmp(z1.Size+z2.Size,0);
   int i,j;
   long int result,carry;
   for(i=0;i<z1.Size;i++) {
      carry = 0;
      for(j=0;j<z2.Size;j++) {
         result = tmp.Data[i+j] + ULONG(z1.Data[i])*ULONG(z2.Data[j]) + carry;
         carry = result >> BITS_PR_CHAR;
         result = result - (carry << BITS_PR_CHAR);
         tmp.Data[i+j] = result;
      }
      for(j=z2.Size;j<(z1.Size+z2.Size-i);j++) {
         result = tmp.Data[i+j] + carry;
         carry = result >> BITS_PR_CHAR;
         result = result - (carry << BITS_PR_CHAR);
         tmp.Data[i+j] = result;
      }
   }
   // truncate high part
   return HalfSize(tmp);
}

class BigInt div_help(const class BigInt& z1,const class BigInt& z2)
{
   // find biggest tmp where tmp*z2<z1 by shifting tmp to the left
   // based on the leading equal bits in z2 and z1 (equivalent to
   // the number of used bits) and use that x/y = z/y + (x-z)/y
   // and that tmp2*z2 = z2 << b if tmp2 = 1 << b but avoid recursion
   // with a loop
   class BigInt leftover = z1;
   class BigInt result(z1.Size,0);
   class BigInt tmp(z1.Size);
   int b;
   while(leftover>=z2) {
      tmp = 1;
      b = MAX(z2.LeadingEqualBits()-leftover.LeadingEqualBits()-1,0);
      tmp = tmp << b;
      if((z2 << (b+1))<=leftover) {
         tmp = tmp << 1;
         b = b+1;
      }
      result = result + tmp;
      leftover = leftover - (z2 << b);
   }
   return result;
}

class BigInt operator/(const class BigInt& z1,const class BigInt& z2)
{
   // checks
   if(z1==0) return BigInt(z1.Size,0);
   // convert:
   // + + -> + + && flag = FALSE
   // - - -> + + && flag = FALSE
   // + - -> + + && flag = TRUE
   // - + -> + + && falg = TRUE
   class BigInt zz1(z1);
   class BigInt zz2(z2);
   BOOL flag = LTZERO(zz1)^LTZERO(zz2);
   if(LTZERO(zz1)) zz1 = 0 - zz1;
   if(LTZERO(zz2)) zz2 = 0 - zz2;
   if(zz1<zz2) return BigInt(z1.Size,0);
   // calculate tmp=zz1/zz2
   class BigInt tmp = div_help(zz1,zz2);
   // negate if necesarry
   if(flag) tmp = 0 - tmp;
   return tmp;
}

class BigInt operator%(const class BigInt& z1,const class BigInt& z2)
{
   return (z1 - z2*(z1/z2));
}

BOOL operator==(const class BigInt& z1,const class BigInt& z2)
{
   for(int i=0;i<z1.Size;i++) if(z1.Data[i]!=z2.Data[i]) return FALSE;
   return TRUE;
}

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

BOOL operator>(const class BigInt& z1,const class BigInt& z2)
{
   class BigInt tmp = z2 - z1;
   return (LTZERO(tmp));
}

BOOL operator<(const class BigInt& z1,const class BigInt& z2)
{
   class BigInt tmp = z1 - z2;
   return (LTZERO(tmp));
}

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

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

class BigInt operator+(const int z1,const class BigInt& z2)
{
   if(z1==0) return z2;
   if((z1>0)&&(z1<=255)) {
      class BigInt tmp(z2.Size);
      int result,carry;
      result = z1 + z2.Data[0];
      if(result<256) {
         carry = 0;
      } else {
         result = result - 256;
         carry = 1;
      }
      tmp.Data[0] = result;
      for(int i=1;i<tmp.Size;i++) {
         result = z2.Data[i] + carry;
         if(result<256) {
            carry = 0;
         } else {
            result = result - 256;
            carry = 1;
         }
         tmp.Data[i] = result;
      }
      return tmp;
   } else {
      class BigInt tmp(z2.Size,z1);
      return (tmp+z2);
   }
}

class BigInt operator-(const int z1,const class BigInt& z2)
{
   if((z1>=0)&&(z1<=255)) {
      class BigInt tmp(z2.Size);
      int result,borrow;
      result = z1 - z2.Data[0];
      if(result>=0) {
         borrow = 0;
      } else {
         result = result + 256;
         borrow = 1;
      }
      tmp.Data[0] = result;
      for(int i=1;i<tmp.Size;i++) {
         result = -z2.Data[i] - borrow;
         if(result>=0) {
            borrow = 0;
         } else {
            result = result + 256;
            borrow = 1;
         }
         tmp.Data[i] = result;
      }
      return tmp;
   } else {
      class BigInt tmp(z2.Size,z1);
      return (tmp-z2);
   }
}

class BigInt operator*(const int z1,const class BigInt& z2)
{
   if(z1==0) return BigInt(z2.Size,0);
   if((z1>0)&&(z1<=255)) {
      class BigInt tmp(z2.Size);
      long int result,carry;
      carry = 0;
      for(int i=0;i<tmp.Size;i++) {
         result = ULONG(z1) * ULONG(z2.Data[i]) + carry;
         carry = result >> BITS_PR_CHAR;
         result = result - (carry << BITS_PR_CHAR);
         tmp.Data[i] = result;
      }
      return tmp;
   } else {
      class BigInt tmp(z2.Size,z1);
      return (tmp*z2);
   }
}

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

class BigInt operator%(const int z1,const class BigInt& z2)
{
   class BigInt tmp(z2.Size,z1);
   return (tmp%z2);
}

BOOL operator==(const int z1,const class BigInt& z2)
{
   if(z1==0) {
      for(int i=0;i<z2.Size;i++) if(0!=z2.Data[i]) return FALSE;
      return TRUE;
   } else {
      class BigInt tmp(z2.Size,z1);
      return (tmp==z2);
   }
}

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

BOOL operator>(const int z1,const class BigInt& z2)
{
   if(z1==0) {
      return (LTZERO(z2));
   } else {
     class BigInt tmp(z2.Size,z1);
     return (tmp>z2);
   }
}

BOOL operator<(const int z1,const class BigInt& z2)
{
   if(z1==0) {
      if(0==z2) return FALSE;
      return !(LTZERO(z2));
   } else {
     class BigInt tmp(z2.Size,z1);
     return (tmp<z2);
   }
}

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

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

class BigInt operator+(const class BigInt& z1,const int z2)
{
   return (z2+z1);
}

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

class BigInt operator*(const class BigInt& z1,const int z2)
{
   return z2*z1;
}

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

class BigInt operator%(const class BigInt& z1,const int z2)
{
   class BigInt tmp(z1.Size,z2);
   return (z1%tmp);
}

BOOL operator==(const class BigInt& z1,const int z2)
{
   return (z2==z1);
}

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

BOOL operator>(const class BigInt& z1,const int z2)
{
   return (z2<z1);
}

BOOL operator<(const class BigInt& z1,const int z2)
{
   return (z2>z1);
}

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

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

class BigInt operator>>(const class BigInt& z,int b)
{
   if(b<0) return (z<<(-b));
   UCHAR fill;
   if(LTZERO(z)) fill=255; else fill=0;
   class BigInt tmp(z.Size,0);
   int nbyt = b/BITS_PR_CHAR;
   int nbit = b%BITS_PR_CHAR;
   int i;
   USHORT buf;
   if(nbit==0) {
      for(i=0;i<z.Size;i++) tmp.Data[i] = (i+nbyt<z.Size) ? z.Data[i+nbyt] : fill;
   } else {
      for(i=0;i<z.Size;i++) {
         buf = (i+nbyt+1<z.Size ? z.Data[i+nbyt+1] : fill);
         buf = (buf << BITS_PR_CHAR) + (i+nbyt<z.Size ? z.Data[i+nbyt] : fill);
         tmp.Data[i] = (buf >> nbit) & 0xFF;
      }
   }
   return tmp;
}

class BigInt operator<<(const class BigInt& z,int b)
{
   if(b<0) return (z>>(-b));
   class BigInt tmp(z.Size);
   int nbyt = b/BITS_PR_CHAR;
   int nbit = b%BITS_PR_CHAR;
   int i;
   USHORT buf;
   if(nbit==0) {
      for(i=0;i<z.Size;i++) tmp.Data[i] = (i-nbyt>=0) ? z.Data[i-nbyt] : 0;
   } else {
      for(i=0;i<z.Size;i++) {
         buf = (i-nbyt>=0 ? z.Data[i-nbyt] : 0);
         buf = (buf << BITS_PR_CHAR) + (i-nbyt-1>=0 ? z.Data[i-nbyt-1] : 0);
         tmp.Data[i] = (buf >> (BITS_PR_CHAR-nbit)) & 0xFF;
      }
   }
   return tmp;
}

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

ostream& operator<<(ostream& s,const class BigInt& z)
{
   class BigInt tmp(z);
   if(LTZERO(tmp)) {
      s << "-";
      tmp = 0 - tmp;
   }
   class BigInt digit(tmp.Size);
   int *stor = new int[3*tmp.Size];
   int n = 0;
   do {
      digit = tmp;
      tmp = tmp/10;
      digit = digit - 10*tmp;
      stor[n] = digit.Data[0];
      n++;
   } while(tmp>0);
   for(int i=(n-1);i>=0;i--) s << stor[i];
   delete stor;
   return s;
}

// functions
class BigInt pow(const class BigInt& z,int p)
{
   if(p==0) return BigInt(z.Size,1);
   if(p<0) return BigInt(z.Size,0);
   if(EVEN(p)) {
      return pow(z*z,p/2);
   } else {
      return z*pow(z,p-1);
   }
}

// special functions
class BigInt DoubleSize(const class BigInt& z)
{
   int i;
   class BigInt tmp(2*z.Size);
   for(i=0;i<z.Size;i++) tmp.Data[i] = z.Data[i];
   if(!LTZERO(z))
      for(i=z.Size+1;i<2*z.Size;i++) tmp.Data[i] = 0;
   else
      for(i=z.Size+1;i<2*z.Size;i++) tmp.Data[i] = 255;
   return tmp;
}

class BigInt HalfSize(const class BigInt& z)
{
   class BigInt tmp(z.Size/2);
   for(int i=0;i<z.Size/2;i++) tmp.Data[i] = z.Data[i];
   return tmp;
}

int intExtract(const class BigInt& z)
{
   int tmp;
   UCHAR *p = (UCHAR *)&tmp;
   for(int i=0;i<sizeof(int);i++) p[i] = z.Data[i];
   return tmp;
}

//**********************************************************************
//
//  Class BigInt16
//
//**********************************************************************

// constructor
BigInt16::BigInt16(int val) : BigInt(16,val)
{
}

BigInt16::BigInt16(double val) : BigInt(16,val)
{
}

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

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

//**********************************************************************
//
//  Class BigInt32
//
//**********************************************************************

// constructor
BigInt32::BigInt32(int val) : BigInt(32,val)
{
}

BigInt32::BigInt32(double val) : BigInt(32,val)
{
}

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

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

