4#ifndef DUNE_BIGUNSIGNEDINT_HH
5#define DUNE_BIGUNSIGNEDINT_HH
46 struct numeric_limits_helper
51 static std::uint16_t& digit(T& big_unsigned_int, std::size_t i)
53 return big_unsigned_int.digit[i];
74 enum { bits=std::numeric_limits<std::uint16_t>::digits, n=k/bits+(k%bits!=0),
75 hexdigits=4, bitmask=0xFFFF, compbitmask=0xFFFF0000,
82 template<
typename Signed>
83 bigunsignedint (Signed x,
typename std::enable_if<std::is_integral<Signed>::value && std::is_signed<Signed>::value>::type* = 0);
89 void print (std::ostream& s)
const ;
162 std::uint_least32_t
touint()
const;
175 return hash_range(arg.digit,arg.digit + arg.n);
179 std::uint16_t digit[n];
183 inline void assign(std::uintmax_t x);
196 template<
typename Signed>
212 static const int no=std::min(
static_cast<int>(n),
213 static_cast<int>(std::numeric_limits<std::uintmax_t>::digits/bits));
215 for(
int i=0; i<no; ++i) {
216 digit[i] = (x&bitmask);
219 for (
unsigned int i=no; i<n; i++) digit[i]=0;
226 return (digit[1]<<bits)+digit[0];
232 int firstInZeroRange=n;
233 for(
int i=n-1; i>=0; --i)
238 int representableDigits=std::numeric_limits<double>::digits/bits;
239 int lastInRepresentableRange=0;
240 if(representableDigits<firstInZeroRange)
241 lastInRepresentableRange=firstInZeroRange-representableDigits;
243 for(
int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
244 val =val*(1<<bits)+digit[i];
245 return val*(1<<(bits*lastInRepresentableRange));
254 for (
int i=n-1; i>=0; i--)
255 for (
int d=hexdigits-1; d>=0; d--)
258 int current = (digit[i]>>(d*4))&0xF;
262 s << std::hex << current;
265 else if (!leading) s << std::hex << current;
267 if (leading) s <<
"0";
278 #define DUNE_BINOP(OP) \
280 inline bigunsignedint<k> bigunsignedint<k>::operator OP (const bigunsignedint<k> &x) const \
299 inline bigunsignedint<k>& bigunsignedint<k>::operator+= (
const bigunsignedint<k>& x)
301 std::uint_fast32_t overflow=0;
303 for (
unsigned int i=0; i<n; i++)
305 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) +
static_cast<std::uint_fast32_t
>(x.digit[i]) + overflow;
306 digit[i] = sum&bitmask;
307 overflow = (sum>>bits)&overflowmask;
313 inline bigunsignedint<k>& bigunsignedint<k>::operator-= (
const bigunsignedint<k>& x)
315 std::int_fast32_t overflow=0;
317 for (
unsigned int i=0; i<n; i++)
319 std::int_fast32_t diff =
static_cast<std::int_fast32_t
>(digit[i]) -
static_cast<std::int_fast32_t
>(x.digit[i]) - overflow;
322 digit[i] =
static_cast<std::uint16_t
>(diff);
327 digit[i] =
static_cast<std::uint16_t
>(diff+bitmask+1);
335 inline bigunsignedint<k>& bigunsignedint<k>::operator*= (
const bigunsignedint<k>& x)
337 bigunsignedint<2*k> finalproduct(0);
339 for (
unsigned int m=0; m<n; m++)
341 bigunsignedint<2*k> singleproduct(0);
342 std::uint_fast32_t overflow(0);
343 for (
unsigned int i=0; i<n; i++)
345 std::uint_fast32_t digitproduct =
static_cast<std::uint_fast32_t
>(digit[i])*
static_cast<std::uint_fast32_t
>(x.digit[m])+overflow;
346 singleproduct.digit[i+m] =
static_cast<std::uint16_t
>(digitproduct&bitmask);
347 overflow = (digitproduct>>bits)&bitmask;
349 finalproduct = finalproduct+singleproduct;
352 for (
unsigned int i=0; i<n; i++) digit[i] = finalproduct.digit[i];
359 std::uint_fast32_t overflow=1;
361 for (
unsigned int i=0; i<n; i++)
363 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) + overflow;
364 digit[i] = sum&bitmask;
365 overflow = (sum>>bits)&overflowmask;
390 inline bigunsignedint<k>& bigunsignedint<k>::operator%= (
const bigunsignedint<k>& x)
402 inline bigunsignedint<k>& bigunsignedint<k>::operator&= (
const bigunsignedint<k>& x)
404 for (
unsigned int i=0; i<n; i++)
405 digit[i] = digit[i]&x.digit[i];
410 inline bigunsignedint<k>& bigunsignedint<k>::operator^= (
const bigunsignedint<k>& x)
412 for (
unsigned int i=0; i<n; i++)
413 digit[i] = digit[i]^x.digit[i];
418 inline bigunsignedint<k>& bigunsignedint<k>::operator|= (
const bigunsignedint<k>& x)
420 for (
unsigned int i=0; i<n; i++)
421 digit[i] = digit[i]|x.digit[i];
429 for (
unsigned int i=0; i<n; i++)
430 result.digit[i] = ~digit[i];
441 for (
int i=n-1-j; i>=0; i--)
442 result.digit[i+j] = digit[i];
446 for (
int i=n-1; i>=0; i--)
448 unsigned int temp = result.digit[i];
450 result.digit[i] =
static_cast<std::uint16_t
>(temp&bitmask);
453 result.digit[i+1] = result.digit[i+1]|temp;
466 for (
unsigned int i=0; i<n-j; i++)
467 result.digit[i] = digit[i+j];
471 for (
unsigned int i=0; i<n; i++)
473 std::uint_fast32_t temp = result.digit[i];
474 temp = temp<<(bits-j);
475 result.digit[i] =
static_cast<std::uint16_t
>((temp&compbitmask)>>bits);
477 result.digit[i-1] = result.digit[i-1] | (temp&bitmask);
486 for (
unsigned int i=0; i<n; i++)
487 if (digit[i]!=x.digit[i])
return true;
494 return !((*this)!=x);
500 for (
int i=n-1; i>=0; i--)
501 if (digit[i]<x.digit[i])
return true;
502 else if (digit[i]>x.digit[i])
return false;
509 for (
int i=n-1; i>=0; i--)
510 if (digit[i]<x.digit[i])
return true;
511 else if (digit[i]>x.digit[i])
return false;
518 return !((*this)<=x);
536 inline bigunsignedint<k> operator- (
const bigunsignedint<k>& x, std::uintmax_t y)
538 bigunsignedint<k> temp(y);
543 inline bigunsignedint<k> operator* (
const bigunsignedint<k>& x, std::uintmax_t y)
545 bigunsignedint<k> temp(y);
550 inline bigunsignedint<k> operator/ (
const bigunsignedint<k>& x, std::uintmax_t y)
552 bigunsignedint<k> temp(y);
557 inline bigunsignedint<k> operator% (
const bigunsignedint<k>& x, std::uintmax_t y)
559 bigunsignedint<k> temp(y);
564 inline bigunsignedint<k> operator+ (std::uintmax_t x,
const bigunsignedint<k>& y)
566 bigunsignedint<k> temp(x);
571 inline bigunsignedint<k> operator- (std::uintmax_t x,
const bigunsignedint<k>& y)
573 bigunsignedint<k> temp(x);
578 inline bigunsignedint<k> operator* (std::uintmax_t x,
const bigunsignedint<k>& y)
580 bigunsignedint<k> temp(x);
585 inline bigunsignedint<k> operator/ (std::uintmax_t x,
const bigunsignedint<k>& y)
587 bigunsignedint<k> temp(x);
592 inline bigunsignedint<k> operator% (std::uintmax_t x,
const bigunsignedint<k>& y)
594 bigunsignedint<k> temp(x);
605 struct numeric_limits<
Dune::bigunsignedint<k> >
606 :
private Dune::Impl::numeric_limits_helper<Dune::bigunsignedint<k> >
609 static const bool is_specialized =
true;
619 for(std::size_t i=0; i < Dune::bigunsignedint<k>::n; ++i)
622 digit(max_,i)=std::numeric_limits<std::uint16_t>::max();
629 static const bool is_signed =
false;
630 static const bool is_integer =
true;
631 static const bool is_exact =
true;
632 static const int radix = 2;
644 static const int min_exponent = 0;
645 static const int min_exponent10 = 0;
646 static const int max_exponent = 0;
647 static const int max_exponent10 = 0;
649 static const bool has_infinity =
false;
650 static const bool has_quiet_NaN =
false;
651 static const bool has_signaling_NaN =
false;
653 static const float_denorm_style has_denorm = denorm_absent;
654 static const bool has_denorm_loss =
false;
676 static const bool is_iec559 =
false;
677 static const bool is_bounded =
true;
678 static const bool is_modulo =
true;
680 static const bool traps =
false;
681 static const bool tinyness_before =
false;
682 static const float_round_style round_style = round_toward_zero;
Base class for Dune-Exceptions.
Definition: exceptions.hh:94
Default exception class for mathematical errors.
Definition: exceptions.hh:239
Portable very large unsigned integers.
Definition: bigunsignedint.hh:70
bigunsignedint< k > operator|(const bigunsignedint< k > &x) const
bitwise or
bigunsignedint< k > operator^(const bigunsignedint< k > &x) const
bitwise exor
bigunsignedint< k > operator*(const bigunsignedint< k > &x) const
multiply
bigunsignedint< k > operator%(const bigunsignedint< k > &x) const
bigunsignedint< k > operator-(const bigunsignedint< k > &x) const
subtract
bigunsignedint< k > operator&(const bigunsignedint< k > &x) const
bitwise and
bigunsignedint< k > operator/(const bigunsignedint< k > &x) const
bigunsignedint< k > operator+(const bigunsignedint< k > &x) const
add
A few common exception classes.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:216
bigunsignedint< k > operator<<(int i) const
left shift
Definition: bigunsignedint.hh:435
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition: bigunsignedint.hh:507
void print(std::ostream &s) const
Print number in hex notation.
Definition: bigunsignedint.hh:249
bool operator<(const bigunsignedint< k > &x) const
less than
Definition: bigunsignedint.hh:498
bool operator==(const bigunsignedint< k > &x) const
equal
Definition: bigunsignedint.hh:492
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition: bigunsignedint.hh:484
bigunsignedint()
Construct uninitialized.
Definition: bigunsignedint.hh:190
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition: bigunsignedint.hh:522
bigunsignedint< k > operator>>(int i) const
right shift
Definition: bigunsignedint.hh:460
bigunsignedint< k > operator~() const
bitwise complement
Definition: bigunsignedint.hh:426
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:357
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:230
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition: bigunsignedint.hh:516
std::uint_least32_t touint() const
export to other types
Definition: bigunsignedint.hh:224
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:98
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition: hash.hh:115
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition: hash.hh:107
Dune namespace.
Definition: alignedallocator.hh:10
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:320
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:421
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:38