4#ifndef DUNE_BIGUNSIGNEDINT_HH
5#define DUNE_BIGUNSIGNEDINT_HH
47 struct numeric_limits_helper
52 static std::uint16_t& digit(T& big_unsigned_int, std::size_t i)
54 return big_unsigned_int.digit[i];
75 enum { bits=std::numeric_limits<std::uint16_t>::digits, n=k/bits+(k%bits!=0),
76 hexdigits=4, bitmask=0xFFFF, compbitmask=0xFFFF0000,
83 template<
typename Signed>
84 bigunsignedint (Signed x,
typename std::enable_if<std::is_integral<Signed>::value && std::is_signed<Signed>::value>::type* = 0);
90 void print (std::ostream& s)
const ;
163 std::uint_least32_t
touint()
const;
176 return hash_range(arg.digit,arg.digit + arg.n);
180 std::uint16_t digit[n];
184 inline void assign(std::uintmax_t x);
197 template<
typename Signed>
213 static const int no=
std::min(
static_cast<int>(n),
214 static_cast<int>(std::numeric_limits<std::uintmax_t>::digits/bits));
216 for(
int i=0; i<no; ++i) {
217 digit[i] = (x&bitmask);
220 for (
unsigned int i=no; i<n; i++) digit[i]=0;
227 return (digit[1]<<bits)+digit[0];
233 int firstInZeroRange=n;
234 for(
int i=n-1; i>=0; --i)
239 int representableDigits=std::numeric_limits<double>::digits/bits;
240 int lastInRepresentableRange=0;
241 if(representableDigits<firstInZeroRange)
242 lastInRepresentableRange=firstInZeroRange-representableDigits;
244 for(
int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
245 val =val*(1<<bits)+digit[i];
246 return val*(1<<(bits*lastInRepresentableRange));
255 for (
int i=n-1; i>=0; i--)
256 for (
int d=hexdigits-1; d>=0; d--)
259 int current = (digit[i]>>(d*4))&0xF;
263 s << std::hex << current;
266 else if (!leading) s << std::hex << current;
268 if (leading) s <<
"0";
279 #define DUNE_BINOP(OP) \
281 inline bigunsignedint<k> bigunsignedint<k>::operator OP (const bigunsignedint<k> &x) const \
300 inline bigunsignedint<k>& bigunsignedint<k>::operator+= (
const bigunsignedint<k>& x)
302 std::uint_fast32_t overflow=0;
304 for (
unsigned int i=0; i<n; i++)
306 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) +
static_cast<std::uint_fast32_t
>(x.digit[i]) + overflow;
307 digit[i] = sum&bitmask;
308 overflow = (sum>>bits)&overflowmask;
314 inline bigunsignedint<k>& bigunsignedint<k>::operator-= (
const bigunsignedint<k>& x)
316 std::int_fast32_t overflow=0;
318 for (
unsigned int i=0; i<n; i++)
320 std::int_fast32_t diff =
static_cast<std::int_fast32_t
>(digit[i]) -
static_cast<std::int_fast32_t
>(x.digit[i]) - overflow;
323 digit[i] =
static_cast<std::uint16_t
>(diff);
328 digit[i] =
static_cast<std::uint16_t
>(diff+bitmask+1);
336 inline bigunsignedint<k>& bigunsignedint<k>::operator*= (
const bigunsignedint<k>& x)
338 bigunsignedint<2*k> finalproduct(0);
340 for (
unsigned int m=0; m<n; m++)
342 bigunsignedint<2*k> singleproduct(0);
343 std::uint_fast32_t overflow(0);
344 for (
unsigned int i=0; i<n; i++)
346 std::uint_fast32_t digitproduct =
static_cast<std::uint_fast32_t
>(digit[i])*
static_cast<std::uint_fast32_t
>(x.digit[m])+overflow;
347 singleproduct.digit[i+m] =
static_cast<std::uint16_t
>(digitproduct&bitmask);
348 overflow = (digitproduct>>bits)&bitmask;
350 finalproduct = finalproduct+singleproduct;
353 for (
unsigned int i=0; i<n; i++) digit[i] = finalproduct.digit[i];
360 std::uint_fast32_t overflow=1;
362 for (
unsigned int i=0; i<n; i++)
364 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) + overflow;
365 digit[i] = sum&bitmask;
366 overflow = (sum>>bits)&overflowmask;
391 inline bigunsignedint<k>& bigunsignedint<k>::operator%= (
const bigunsignedint<k>& x)
403 inline bigunsignedint<k>& bigunsignedint<k>::operator&= (
const bigunsignedint<k>& x)
405 for (
unsigned int i=0; i<n; i++)
406 digit[i] = digit[i]&x.digit[i];
411 inline bigunsignedint<k>& bigunsignedint<k>::operator^= (
const bigunsignedint<k>& x)
413 for (
unsigned int i=0; i<n; i++)
414 digit[i] = digit[i]^x.digit[i];
419 inline bigunsignedint<k>& bigunsignedint<k>::operator|= (
const bigunsignedint<k>& x)
421 for (
unsigned int i=0; i<n; i++)
422 digit[i] = digit[i]|x.digit[i];
430 for (
unsigned int i=0; i<n; i++)
431 result.digit[i] = ~digit[i];
442 for (
int i=n-1-j; i>=0; i--)
443 result.digit[i+j] = digit[i];
447 for (
int i=n-1; i>=0; i--)
449 unsigned int temp = result.digit[i];
451 result.digit[i] =
static_cast<std::uint16_t
>(temp&bitmask);
454 result.digit[i+1] = result.digit[i+1]|temp;
467 for (
unsigned int i=0; i<n-j; i++)
468 result.digit[i] = digit[i+j];
472 for (
unsigned int i=0; i<n; i++)
474 std::uint_fast32_t temp = result.digit[i];
475 temp = temp<<(bits-j);
476 result.digit[i] =
static_cast<std::uint16_t
>((temp&compbitmask)>>bits);
478 result.digit[i-1] = result.digit[i-1] | (temp&bitmask);
487 for (
unsigned int i=0; i<n; i++)
488 if (digit[i]!=x.digit[i])
return true;
495 return !((*this)!=x);
501 for (
int i=n-1; i>=0; i--)
502 if (digit[i]<x.digit[i])
return true;
503 else if (digit[i]>x.digit[i])
return false;
510 for (
int i=n-1; i>=0; i--)
511 if (digit[i]<x.digit[i])
return true;
512 else if (digit[i]>x.digit[i])
return false;
519 return !((*this)<=x);
537 inline bigunsignedint<k> operator- (
const bigunsignedint<k>& x, std::uintmax_t y)
539 bigunsignedint<k> temp(y);
544 inline bigunsignedint<k> operator* (
const bigunsignedint<k>& x, std::uintmax_t y)
546 bigunsignedint<k> temp(y);
551 inline bigunsignedint<k> operator/ (
const bigunsignedint<k>& x, std::uintmax_t y)
553 bigunsignedint<k> temp(y);
558 inline bigunsignedint<k> operator% (
const bigunsignedint<k>& x, std::uintmax_t y)
560 bigunsignedint<k> temp(y);
565 inline bigunsignedint<k> operator+ (std::uintmax_t x,
const bigunsignedint<k>& y)
567 bigunsignedint<k> temp(x);
572 inline bigunsignedint<k> operator- (std::uintmax_t x,
const bigunsignedint<k>& y)
574 bigunsignedint<k> temp(x);
579 inline bigunsignedint<k> operator* (std::uintmax_t x,
const bigunsignedint<k>& y)
581 bigunsignedint<k> temp(x);
586 inline bigunsignedint<k> operator/ (std::uintmax_t x,
const bigunsignedint<k>& y)
588 bigunsignedint<k> temp(x);
593 inline bigunsignedint<k> operator% (std::uintmax_t x,
const bigunsignedint<k>& y)
595 bigunsignedint<k> temp(x);
606 struct numeric_limits<
Dune::bigunsignedint<k> >
607 :
private Dune::Impl::numeric_limits_helper<Dune::bigunsignedint<k> >
610 static const bool is_specialized =
true;
620 for(std::size_t i=0; i < Dune::bigunsignedint<k>::n; ++i)
630 static const bool is_signed =
false;
631 static const bool is_integer =
true;
632 static const bool is_exact =
true;
633 static const int radix = 2;
645 static const int min_exponent = 0;
646 static const int min_exponent10 = 0;
647 static const int max_exponent = 0;
648 static const int max_exponent10 = 0;
650 static const bool has_infinity =
false;
651 static const bool has_quiet_NaN =
false;
652 static const bool has_signaling_NaN =
false;
654 static const float_denorm_style has_denorm = denorm_absent;
655 static const bool has_denorm_loss =
false;
677 static const bool is_iec559 =
false;
678 static const bool is_bounded =
true;
679 static const bool is_modulo =
true;
681 static const bool traps =
false;
682 static const bool tinyness_before =
false;
683 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:71
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:436
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition: bigunsignedint.hh:508
void print(std::ostream &s) const
Print number in hex notation.
Definition: bigunsignedint.hh:250
bool operator<(const bigunsignedint< k > &x) const
less than
Definition: bigunsignedint.hh:499
bool operator==(const bigunsignedint< k > &x) const
equal
Definition: bigunsignedint.hh:493
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition: bigunsignedint.hh:485
bigunsignedint()
Construct uninitialized.
Definition: bigunsignedint.hh:191
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition: bigunsignedint.hh:523
bigunsignedint< k > operator>>(int i) const
right shift
Definition: bigunsignedint.hh:461
bigunsignedint< k > operator~() const
bitwise complement
Definition: bigunsignedint.hh:427
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:358
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:231
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition: bigunsignedint.hh:517
std::uint_least32_t touint() const
export to other types
Definition: bigunsignedint.hh:225
auto min(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::min()
Definition: defaults.hh:87
auto max(ADLTag< 0 >, const V &v1, const V &v2)
implements binary Simd::max()
Definition: defaults.hh:79
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: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:320
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:445
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:38