5#ifndef DUNE_BIGUNSIGNEDINT_HH
6#define DUNE_BIGUNSIGNEDINT_HH
46 enum { bits=std::numeric_limits<unsigned short>::digits, n=k/bits+(k%bits!=0),
47 hexdigits=4, bitmask=0xFFFF, compbitmask=0xFFFF0000,
60 void print (std::ostream& s)
const ;
126 unsigned int touint()
const;
141 return hash_range(arg.digit,arg.digit + arg.n);
147 unsigned short digit[n];
151 inline void assign(std::size_t x);
166 std::size_t x = std::abs(y);
178 int no=std::min(
static_cast<int>(n),
179 static_cast<int>(std::numeric_limits<std::size_t>::digits/bits));
181 for(
int i=0; i<no; ++i) {
182 digit[i] = (x&bitmask);
185 for (
unsigned int i=no; i<n; i++) digit[i]=0;
192 return (digit[1]<<bits)+digit[0];
198 int firstInZeroRange=n;
199 for(
int i=n-1; i>=0; --i)
204 int representableDigits=std::numeric_limits<double>::digits/bits;
205 int lastInRepresentableRange=0;
206 if(representableDigits<firstInZeroRange)
207 lastInRepresentableRange=firstInZeroRange-representableDigits;
209 for(
int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
210 val =val*(1<<bits)+digit[i];
211 return val*(1<<(bits*lastInRepresentableRange));
220 for (
int i=n-1; i>=0; i--)
221 for (
int d=hexdigits-1; d>=0; d--)
224 int current = (digit[i]>>(d*4))&0xF;
228 s << std::hex << current;
231 else if (!leading) s << std::hex << current;
233 if (leading) s <<
"0";
252 for (
unsigned int i=0; i<n; i++)
254 int sum = ((int)digit[i]) + ((int)x.digit[i]) + overflow;
255 result.digit[i] = sum&bitmask;
256 overflow = (sum>>bits)&overflowmask;
267 for (
unsigned int i=0; i<n; i++)
269 int diff = ((int)digit[i]) - (((int)x.digit[i]) + overflow);
271 result.digit[i] = (
unsigned short) diff;
274 result.digit[i] = (
unsigned short) (diff+bitmask);
286 for (
unsigned int m=0; m<n; m++)
289 unsigned int overflow(0);
290 for (
unsigned int i=0; i<n; i++)
292 unsigned int digitproduct = ((
unsigned int)digit[i])*((
unsigned int)x.digit[m])+overflow;
293 singleproduct.digit[i+m] = (
unsigned short) (digitproduct&bitmask);
294 overflow = (digitproduct>>bits)&bitmask;
296 finalproduct = finalproduct+singleproduct;
300 for (
unsigned int i=0; i<n; i++) result.digit[i] = finalproduct.digit[i];
309 for (
unsigned int i=0; i<n; i++)
311 int sum = ((int)digit[i]) + overflow;
312 digit[i] = sum&bitmask;
313 overflow = (sum>>bits)&overflowmask;
358 for (
unsigned int i=0; i<n; i++)
359 result.digit[i] = digit[i]&x.digit[i];
367 for (
unsigned int i=0; i<n; i++)
368 result.digit[i] = digit[i]^x.digit[i];
376 for (
unsigned int i=0; i<n; i++)
377 result.digit[i] = digit[i]|x.digit[i];
385 for (
unsigned int i=0; i<n; i++)
386 result.digit[i] = ~digit[i];
397 for (
int i=n-1-j; i>=0; i--)
398 result.digit[i+j] = digit[i];
402 for (
int i=n-1; i>=0; i--)
404 unsigned int temp = result.digit[i];
406 result.digit[i] = (
unsigned short) (temp&bitmask);
409 result.digit[i+1] = result.digit[i+1]|temp;
422 for (
unsigned int i=0; i<n-j; i++)
423 result.digit[i] = digit[i+j];
427 for (
unsigned int i=0; i<n; i++)
429 unsigned int temp = result.digit[i];
430 temp = temp<<(bits-j);
431 result.digit[i] = (
unsigned short) ((temp&compbitmask)>>bits);
433 result.digit[i-1] = result.digit[i-1] | (temp&bitmask);
442 for (
unsigned int i=0; i<n; i++)
443 if (digit[i]!=x.digit[i])
return true;
450 return !((*this)!=x);
456 for (
int i=n-1; i>=0; i--)
457 if (digit[i]<x.digit[i])
return true;
458 else if (digit[i]>x.digit[i])
return false;
465 for (
int i=n-1; i>=0; i--)
466 if (digit[i]<x.digit[i])
return true;
467 else if (digit[i]>x.digit[i])
return false;
474 return !((*this)<=x);
492 inline bigunsignedint<k> operator- (
const bigunsignedint<k>& x, std::size_t y)
494 bigunsignedint<k> temp(y);
499 inline bigunsignedint<k> operator* (
const bigunsignedint<k>& x, std::size_t y)
501 bigunsignedint<k> temp(y);
506 inline bigunsignedint<k> operator/ (
const bigunsignedint<k>& x, std::size_t y)
508 bigunsignedint<k> temp(y);
513 inline bigunsignedint<k> operator% (
const bigunsignedint<k>& x, std::size_t y)
515 bigunsignedint<k> temp(y);
520 inline bigunsignedint<k> operator+ (std::size_t x,
const bigunsignedint<k>& y)
522 bigunsignedint<k> temp(x);
527 inline bigunsignedint<k> operator- (std::size_t x,
const bigunsignedint<k>& y)
529 bigunsignedint<k> temp(x);
534 inline bigunsignedint<k> operator* (std::size_t x,
const bigunsignedint<k>& y)
536 bigunsignedint<k> temp(x);
541 inline bigunsignedint<k> operator/ (std::size_t x,
const bigunsignedint<k>& y)
543 bigunsignedint<k> temp(x);
548 inline bigunsignedint<k> operator% (std::size_t x,
const bigunsignedint<k>& y)
550 bigunsignedint<k> temp(x);
561 struct numeric_limits<
Dune::bigunsignedint<k> >
564 static const bool is_specialized =
true;
574 for(std::size_t i=0; i < Dune::bigunsignedint<k>::n; ++i)
575 max_.digit[i]=std::numeric_limits<unsigned short>::max();
582 static const bool is_signed =
false;
583 static const bool is_integer =
true;
584 static const bool is_exact =
true;
585 static const int radix = 2;
597 static const int min_exponent = 0;
598 static const int min_exponent10 = 0;
599 static const int max_exponent = 0;
600 static const int max_exponent10 = 0;
602 static const bool has_infinity =
false;
603 static const bool has_quiet_NaN =
false;
604 static const bool has_signaling_NaN =
false;
606 static const float_denorm_style has_denorm = denorm_absent;
607 static const bool has_denorm_loss =
false;
629 static const bool is_iec559 =
false;
630 static const bool is_bounded =
true;
631 static const bool is_modulo =
true;
633 static const bool traps =
false;
634 static const bool tinyness_before =
false;
635 static const float_round_style round_style = round_toward_zero;
Default exception class for mathematical errors.
Definition: exceptions.hh:267
Portable very large unsigned integers.
Definition: bigunsignedint.hh:42
A few common exception classes.
bigunsignedint< k > operator|(const bigunsignedint< k > &x) const
bitwise or
Definition: bigunsignedint.hh:373
bigunsignedint< k > operator<<(int i) const
left shift1/
Definition: bigunsignedint.hh:391
unsigned int touint() const
export to other types
Definition: bigunsignedint.hh:190
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition: bigunsignedint.hh:463
bigunsignedint< k > operator^(const bigunsignedint< k > &x) const
bitwise exor
Definition: bigunsignedint.hh:364
void print(std::ostream &s) const
Print number in hex notation.
Definition: bigunsignedint.hh:215
bigunsignedint< k > operator*(const bigunsignedint< k > &x) const
multiply
Definition: bigunsignedint.hh:282
bool operator<(const bigunsignedint< k > &x) const
less than
Definition: bigunsignedint.hh:454
bool operator==(const bigunsignedint< k > &x) const
equal
Definition: bigunsignedint.hh:448
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition: bigunsignedint.hh:440
bigunsignedint< k > operator%(const bigunsignedint< k > &x) const
Definition: bigunsignedint.hh:338
bigunsignedint()
Construct uninitialized.
Definition: bigunsignedint.hh:158
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition: bigunsignedint.hh:478
std::ostream & operator<<(std::ostream &s, const array< T, N > &e)
Output operator for array.
Definition: array.hh:159
bigunsignedint< k > operator>>(int i) const
right shift
Definition: bigunsignedint.hh:416
bigunsignedint< k > operator~() const
bitwise komplement
Definition: bigunsignedint.hh:382
bigunsignedint< k > operator-(const bigunsignedint< k > &x) const
subtract
Definition: bigunsignedint.hh:262
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:305
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:196
bigunsignedint< k > operator&(const bigunsignedint< k > &x) const
bitwise and
Definition: bigunsignedint.hh:355
bigunsignedint< k > operator/(const bigunsignedint< k > &x) const
Definition: bigunsignedint.hh:319
bigunsignedint< k > operator+(const bigunsignedint< k > &x) const
add
Definition: bigunsignedint.hh:247
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition: bigunsignedint.hh:472
#define DUNE_THROW(E, m)
Definition: exceptions.hh:244
Support for calculating hash values of objects.
#define DUNE_DEFINE_HASH(template_args, type)
Defines the required struct specialization to make type hashable via Dune::hash.
Definition: hash.hh:126
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition: hash.hh:143
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition: hash.hh:135
Dune namespace.
Definition: alignment.hh:14
std::size_t hash_range(It first, It last)
Hashes all elements in the range [first,last) and returns the combined hash.
Definition: hash.hh:449
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:37