Beispiel #1
0
void conv(RR& z, double a)
{
   if (a == 0) {
      clear(z);
      return;
   }

   if (a == 1) {
      set(z);
      return;
   }

   if (!IsFinite(&a))
      ArithmeticError("RR: conversion of a non-finite double");

   int e;
   double f;
   NTL_TLS_LOCAL(RR, t);

   f = frexp(a, &e);

   f = f * NTL_FDOUBLE_PRECISION;
   f = f * 4;

   conv(t.x, f);
   t.e = e - (NTL_DOUBLE_PRECISION + 1);

   xcopy(z, t);
}
Beispiel #2
0
void RoundToZZ(ZZ& z, const RR& a)
{
   if (a.e >= 0) {
      LeftShift(z, a.x, a.e);
      return;
   }

   long len = NumBits(a.x);

   if (-a.e > len) {
      z = 0;
      return;
   }

   if (-a.e == len) {
      if (len == 1)
         z = 0;
      else
         z = sign(a.x);

      return;
   }

   NTL_TLS_LOCAL(RR, t);

   ConvPrec(t, a, len+a.e);

   LeftShift(z, t.x, t.e);
}
Beispiel #3
0
long compare(const RR& a, const RR& b)
{
   NTL_TLS_LOCAL(RR, t);

   SubPrec(t, a, b, 1);
   return sign(t);
}
Beispiel #4
0
void random(RR& z)
{
   NTL_TLS_LOCAL(RR, t);
   RandomBits(t.x, RR::prec); 
   t.e = -RR::prec;
   normalize(z, t);
}
Beispiel #5
0
void round(RR& z, const RR& a)
{
   if (a.e >= 0) {
      xcopy(z, a);
      return;
   }

   long len = NumBits(a.x);

   if (-a.e > len) {
      z = 0;
      return;
   }

   if (-a.e == len) {
      if (len == 1)
         z = 0;
      else
         z = sign(a.x);

      return;
   }

   NTL_TLS_LOCAL(RR, t);
   ConvPrec(t, a, len+a.e);
   xcopy(z, t);
}
Beispiel #6
0
void sqr(RR& z, const RR& a)
{
   NTL_TLS_LOCAL(RR, t);

   sqr(t.x, a.x);
   t.e = a.e + a.e;
   xcopy(z, t);
}
Beispiel #7
0
void mul(RR& z, const RR& a, const RR& b)
{
   NTL_TLS_LOCAL(RR, t);

   mul(t.x, a.x, b.x);
   t.e = a.e + b.e;
   xcopy(z, t);
}
Beispiel #8
0
long compare(const RR& a, double b)
{
   if (b == 0) return sign(a);

   NTL_TLS_LOCAL(RR, B);
   B = b;
   return compare(a, B);
}
Beispiel #9
0
long operator==(const RR& a, double b) 
{
   if (b == 0) return IsZero(a);
   if (b == 1) return IsOne(a);

   NTL_TLS_LOCAL(RR, B);
   B = b;
   return a == B;
}
const char *FileName(const char* stem, long d)
{
   NTL_TLS_LOCAL(string, sbuf);

   stringstream ss;
   ss << "tmp-ntl-" << stem;
   ss << "-" << setfill('0') << setw(5) << d << "-";
   sbuf = ss.str() + UniqueID();
   return sbuf.c_str();
}
Beispiel #11
0
void conv(ZZ& x, const xdouble& a)
{
   xdouble b = floor(a);

   RRPush push;
   RR::SetPrecision(NTL_DOUBLE_PRECISION);

   NTL_TLS_LOCAL(RR, t);
   conv(t, b);
   conv(x, t);
}
Beispiel #12
0
void conv(double& z, const RR& aa)
{
   double x;
   NTL_TLS_LOCAL(RR, a);

   ConvPrec(a, aa, NTL_DOUBLE_PRECISION);
   // round to NTL_DOUBLE_PRECISION bits to avoid double overflow

   conv(x, a.x);
   z = _ntl_ldexp(x, a.e);
}
Beispiel #13
0
void trunc(RR& z, const RR& a)
{
   NTL_TLS_LOCAL(RR, t);

   if (a.e >= 0) 
      xcopy(z, a);
   else {
      RightShift(t.x, a.x, -a.e);
      t.e = 0;
      xcopy(z, t);
   }
}
Beispiel #14
0
void ceil(RR& z, const RR& a)
{
   NTL_TLS_LOCAL(RR, t);

   if (a.e >= 0)
      xcopy(z, a);
   else {
      RightShift(t.x, a.x, -a.e);
      if (sign(a.x) > 0)
         add(t.x, t.x, 1);
      t.e = 0;
      xcopy(z, t);
   }
}
Beispiel #15
0
void CopySwap(WordVector& x, WordVector& y)
{
   NTL_TLS_LOCAL(WordVector, t);
   WordVectorWatcher watch_t(t);

   long sz_x = x.length();
   long sz_y = y.length();
   long sz = (sz_x > sz_y) ? sz_x : sz_y;

   x.SetMaxLength(sz);
   y.SetMaxLength(sz);

   // EXCEPTIONS: all of the above ensures that swap provides strong ES

   t = x;
   x = y;
   y = t;
}
Beispiel #16
0
xdouble PowerOf10(const ZZ& e)
{
   static NTL_CHEAP_THREAD_LOCAL long init = 0;
   static NTL_CHEAP_THREAD_LOCAL long k = 0;

   NTL_TLS_LOCAL(xdouble, v10k);

   if (!init) {
      k = ComputeMax10Power();
      RRPush push;
      RR::SetPrecision(NTL_DOUBLE_PRECISION);
      v10k = to_xdouble(power(to_RR(10), k)); 
      init = 1;
   }

   ZZ e1;
   long neg;

   if (e < 0) {
      e1 = -e;
      neg = 1;
   }
   else {
      e1 = e;
      neg = 0;
   }

   long r;
   ZZ q;

   r = DivRem(q, e1, k);

   RRPush push;
   RR::SetPrecision(NTL_DOUBLE_PRECISION);
   xdouble x1 = to_xdouble(power(to_RR(10), r));

   xdouble x2 = power(v10k, q);
   xdouble x3 = x1*x2;

   if (neg) x3 = 1/x3;

   return x3;
}
Beispiel #17
0
void sub(RR& z, const RR& a, const RR& b)
{
   NTL_TLS_LOCAL(RR, t);

   if (IsZero(a.x)) {
      negate(z, b);
      return;
   }

   if (IsZero(b.x)) {
      xcopy(z, a);
      return;
   }

   if (a.e > b.e) {
      if (a.e-b.e - max(RR::prec-NumBits(a.x),0) >= NumBits(b.x) + 2)
         normalize(z, a, -sign(b));
      else {
         LeftShift(t.x, a.x, a.e-b.e);
         sub(t.x, t.x, b.x);
         t.e = b.e;
         xcopy(z, t);
      }
   }
   else if (a.e < b.e) {
      if (b.e-a.e - max(RR::prec-NumBits(b.x),0) >= NumBits(a.x) + 2) {
         normalize(z, b, -sign(a));
         negate(z.x, z.x);
      }
      else {
         LeftShift(t.x, b.x, b.e-a.e);
         sub(t.x, a.x, t.x);
         t.e = a.e;
         xcopy(z, t);
      }
   }
   else {
      sub(t.x, a.x, b.x);
      t.e = a.e;
      normalize(z, t);
   }
}
Beispiel #18
0
xdouble to_xdouble(const ZZ& a)
{
   RRPush push;
   RR::SetPrecision(NTL_DOUBLE_PRECISION);
   
   NTL_TLS_LOCAL(RR, t);
   conv(t, a);

   double x;
   conv(x, t.mantissa());

   xdouble y, z, res;

   conv(y, x);
   power2(z, t.exponent());

   res = y*z;

   return res;
}
Beispiel #19
0
ostream& operator<<(ostream& s, const quad_float& a)
{
   quad_float aa = a;

   if (!IsFinite(&aa)) {
      s << "NaN";
      return s;
   }

   RRPush push;
   RROutputPush opush;

   RR::SetPrecision(long(3.33*quad_float::oprec) + 10);
   RR::SetOutputPrecision(quad_float::oprec);

   NTL_TLS_LOCAL(RR, t);

   conv(t, a);
   s << t;

   return s;
}
const string& UniqueID()
{
   static AtomicCounter cnt; // a GLOBAL counter
   

   NTL_TLS_LOCAL(string, ID);

   NTL_TLS_LOCAL_INIT(bool, initialized, (false));
   NTL_TLS_LOCAL_INIT(unsigned long, local_cnt, (cnt.inc()));
   NTL_TLS_LOCAL_INIT(unsigned long, local_time, (time(0)));
   NTL_TLS_LOCAL_INIT(unsigned long, local_clock, (clock()));

   if (!initialized) {
      stringstream ss;
      ss << local_cnt << "-" << local_time << "-" 
         << local_clock << "-" << GetPID()  << "-" << CurrentThreadID();  
      ID = ss.str();
      initialized = true;
   }

   return ID;
}
Beispiel #21
0
void div(RR& z, const RR& a, const RR& b)
{
   if (IsZero(b))
      ArithmeticError("RR: division by zero");

   if (IsZero(a)) {
      clear(z);
      return;
   }

   long la = NumBits(a.x);
   long lb = NumBits(b.x);

   long neg = (sign(a) != sign(b));

   long k = RR::prec - la + lb + 1;
   if (k < 0) k = 0;

   NTL_TLS_LOCAL(RR, t);
   NTL_ZZRegister(A);
   NTL_ZZRegister(B);
   NTL_ZZRegister(R);

   abs(A, a.x);
   LeftShift(A, A, k);

   abs(B, b.x);
   DivRem(t.x, R, A, B);

   t.e = a.e - b.e - k;

   normalize(z, t, !IsZero(R));

   if (neg)
      negate(z.x, z.x);
}
Beispiel #22
0
void add(RR& z, const RR& a, double b)
{
   NTL_TLS_LOCAL(RR, B);
   B = b;
   add(z, a, B);
}
Beispiel #23
0
void sub(RR& z, const RR& a, double b)
{
   NTL_TLS_LOCAL(RR, B);
   B = b;
   sub(z, a, B);
}
Beispiel #24
0
void sub(RR& z, double a, const RR& b)
{
   NTL_TLS_LOCAL(RR, A);
   A = a;
   sub(z, A, b);
}
Beispiel #25
0
void mul(RR& z, const RR& a, double b)
{
   NTL_TLS_LOCAL(RR, B);
   B = b;
   mul(z, a, B);
}
Beispiel #26
0
void div(RR& z, const RR& a, double b)
{
   NTL_TLS_LOCAL(RR, B);
   B = b;
   div(z, a, B);
}
Beispiel #27
0
void div(RR& z, double a, const RR& b)
{
   NTL_TLS_LOCAL(RR, A);
   A = a;
   div(z, A, b);
}