6#ifndef DUNE_BIGUNSIGNEDINT_HH
7#define DUNE_BIGUNSIGNEDINT_HH
50 struct numeric_limits_helper
55 static std::uint16_t& digit(T& big_unsigned_int, std::size_t i)
57 return big_unsigned_int.digit[i];
78 constexpr static int bits = std::numeric_limits<std::uint16_t>::digits;
89 template<
typename Signed>
90 bigunsignedint (Signed x,
typename std::enable_if<std::is_integral<Signed>::value && std::is_signed<Signed>::value>
::type* = 0);
96 void print (std::ostream& s)
const ;
169 std::uint_least32_t
touint()
const;
186 int width =
bits * std::size(arg.digit);
187 auto it = std::rbegin(arg.digit);
189 width -= (
bits - std::bit_width(*it));
190 }
while ((*it == 0) and ((++it) != std::rend(arg.digit)));
209 std::uint16_t digit[
n];
213 inline void assign(std::uintmax_t x);
226 template<
typename Signed>
240 void bigunsignedint<k>::assign(std::uintmax_t x)
242 static const int no=std::min<int>(n, std::numeric_limits<std::uintmax_t>::digits/bits);
244 for(
int i=0; i<no; ++i) {
245 digit[i] = (x&bitmask);
248 for (
unsigned int i=no; i<n; i++) digit[i]=0;
255 return (digit[1]<<
bits)+digit[0];
261 int firstInZeroRange=
n;
262 for(
int i=
n-1; i>=0; --i)
267 int representableDigits=std::numeric_limits<double>::digits/
bits;
268 int lastInRepresentableRange=0;
269 if(representableDigits<firstInZeroRange)
270 lastInRepresentableRange=firstInZeroRange-representableDigits;
272 for(
int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
273 val =val*(1<<
bits)+digit[i];
274 return val*(1<<(
bits*lastInRepresentableRange));
283 for (
int i=
n-1; i>=0; i--)
287 int current = (digit[i]>>(d*4))&0xF;
291 s << std::hex << current;
294 else if (!leading) s << std::hex << current;
296 if (leading) s <<
"0";
307 #define DUNE_BINOP(OP) \
309 inline bigunsignedint<k> bigunsignedint<k>::operator OP (const bigunsignedint<k> &x) const \
330 std::uint_fast32_t overflow=0;
332 for (
unsigned int i=0; i<
n; i++)
334 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) +
static_cast<std::uint_fast32_t
>(x.digit[i]) + overflow;
344 std::int_fast32_t overflow=0;
346 for (
unsigned int i=0; i<
n; i++)
348 std::int_fast32_t diff =
static_cast<std::int_fast32_t
>(digit[i]) -
static_cast<std::int_fast32_t
>(x.digit[i]) - overflow;
351 digit[i] =
static_cast<std::uint16_t
>(diff);
356 digit[i] =
static_cast<std::uint16_t
>(diff+
bitmask+1);
368 for (
unsigned int m=0; m<
n; m++)
371 std::uint_fast32_t overflow(0);
372 for (
unsigned int i=0; i<
n; i++)
374 std::uint_fast32_t digitproduct =
static_cast<std::uint_fast32_t
>(digit[i])*
static_cast<std::uint_fast32_t
>(x.digit[m])+overflow;
375 singleproduct.digit[i+m] =
static_cast<std::uint16_t
>(digitproduct&
bitmask);
378 finalproduct = finalproduct+singleproduct;
381 for (
unsigned int i=0; i<
n; i++) digit[i] = finalproduct.digit[i];
388 std::uint_fast32_t overflow=1;
390 for (
unsigned int i=0; i<
n; i++)
392 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) + overflow;
433 for (
unsigned int i=0; i<
n; i++)
434 digit[i] = digit[i]&x.digit[i];
441 for (
unsigned int i=0; i<
n; i++)
442 digit[i] = digit[i]^x.digit[i];
449 for (
unsigned int i=0; i<
n; i++)
450 digit[i] = digit[i]|x.digit[i];
458 for (
unsigned int i=0; i<
n; i++)
459 result.digit[i] = ~digit[i];
470 for (
int i=n-1-j; i>=0; i--)
471 result.digit[i+j] = digit[i];
475 for (
int i=n-1; i>=0; i--)
477 unsigned int temp = result.digit[i];
479 result.digit[i] =
static_cast<std::uint16_t
>(temp&bitmask);
482 result.digit[i+1] = result.digit[i+1]|temp;
495 for (
unsigned int i=0; i<
n-j; i++)
496 result.digit[i] = digit[i+j];
500 for (
unsigned int i=0; i<
n; i++)
502 std::uint_fast32_t temp = result.digit[i];
503 temp = temp<<(
bits-j);
504 result.digit[i] =
static_cast<std::uint16_t
>((temp&
compbitmask)>>
bits);
506 result.digit[i-1] = result.digit[i-1] | (temp&
bitmask);
515 for (
unsigned int i=0; i<
n; i++)
516 if (digit[i]!=x.digit[i])
return true;
523 return !((*this)!=x);
529 for (
int i=
n-1; i>=0; i--)
530 if (digit[i]<x.digit[i])
return true;
531 else if (digit[i]>x.digit[i])
return false;
538 for (
int i=
n-1; i>=0; i--)
539 if (digit[i]<x.digit[i])
return true;
540 else if (digit[i]>x.digit[i])
return false;
547 return !((*this)<=x);
628 template<
class T>
struct IsNumber;
640 struct numeric_limits<
Dune::bigunsignedint<k> >
641 :
private Dune::Impl::numeric_limits_helper<Dune::bigunsignedint<k> >
654 for(std::size_t i=0; i < Dune::bigunsignedint<k>::n; ++i)
657 digit(max_,i)=std::numeric_limits<std::uint16_t>::max();
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:100
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition hash.hh:117
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition hash.hh:109
A few common exception classes.
bigunsignedint< k > & operator/=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:400
bigunsignedint< k > operator<<(int i) const
left shift
Definition bigunsignedint.hh:464
bigunsignedint< k > & operator+=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:328
bigunsignedint< k > & operator^=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:439
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
Definition bigunsignedint.hh:301
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition bigunsignedint.hh:536
void print(std::ostream &s) const
Print number in hex notation.
Definition bigunsignedint.hh:278
bool operator<(const bigunsignedint< k > &x) const
less than
Definition bigunsignedint.hh:527
bigunsignedint< k > operator/(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:579
bool operator==(const bigunsignedint< k > &x) const
equal
Definition bigunsignedint.hh:521
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition bigunsignedint.hh:513
bigunsignedint()
Construct uninitialized.
Definition bigunsignedint.hh:220
bigunsignedint< k > & operator&=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:431
bigunsignedint< k > operator-(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:565
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition bigunsignedint.hh:551
bigunsignedint< k > operator>>(int i) const
right shift
Definition bigunsignedint.hh:489
bigunsignedint< k > operator+(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:558
bigunsignedint< k > operator~() const
bitwise complement
Definition bigunsignedint.hh:455
bigunsignedint< k > & operator++()
prefix increment
Definition bigunsignedint.hh:386
bigunsignedint< k > & operator|=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:447
bigunsignedint< k > operator%(const bigunsignedint< k > &x, std::uintmax_t y)
Definition bigunsignedint.hh:586
bigunsignedint< k > & operator%=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:419
double todouble() const
Convert to a double.
Definition bigunsignedint.hh:259
bigunsignedint< k > & operator-=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:342
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition bigunsignedint.hh:545
bigunsignedint< k > & operator*=(const bigunsignedint< k > &x)
Definition bigunsignedint.hh:364
std::uint_least32_t touint() const
export to other types
Definition bigunsignedint.hh:253
#define DUNE_BINOP(OP)
Definition bigunsignedint.hh:307
#define DUNE_THROW(E,...)
Definition exceptions.hh:314
constexpr decltype(auto) operator*() const
Dereferencing operator.
Definition iteratorfacades.hh:1119
Dune namespace
Definition alignedallocator.hh:13
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:322
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition simd.hh:447
A traits class describing the mapping of types onto MPI_Datatypes.
Definition mpitraits.hh:41
Portable very large unsigned integers.
Definition bigunsignedint.hh:74
static constexpr int overflowmask
Definition bigunsignedint.hh:83
bigunsignedint< k > operator|(const bigunsignedint< k > &x) const
bitwise or
static constexpr int hexdigits
Definition bigunsignedint.hh:80
bigunsignedint< k > operator^(const bigunsignedint< k > &x) const
bitwise exor
bigunsignedint< k > operator%(const bigunsignedint< k > &x) const
static constexpr int n
Definition bigunsignedint.hh:79
friend int bit_width(const bigunsignedint &arg)
Bit width.
Definition bigunsignedint.hh:184
bigunsignedint< k > operator-(const bigunsignedint< k > &x) const
subtract
static constexpr int compbitmask
Definition bigunsignedint.hh:82
bigunsignedint< k > operator&(const bigunsignedint< k > &x) const
bitwise and
static constexpr int bits
Definition bigunsignedint.hh:78
bigunsignedint< k > operator/(const bigunsignedint< k > &x) const
bigunsignedint< k > operator+(const bigunsignedint< k > &x) const
add
friend std::size_t hash_value(const bigunsignedint &arg)
Definition bigunsignedint.hh:203
static constexpr int bitmask
Definition bigunsignedint.hh:81
friend int countl_zero(const bigunsignedint &arg)
Count left zero bits.
Definition bigunsignedint.hh:198
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition typetraits.hh:194
static const bool is_iec559
Definition bigunsignedint.hh:711
static const int min_exponent
Definition bigunsignedint.hh:679
static const int radix
Definition bigunsignedint.hh:667
static const bool is_specialized
Definition bigunsignedint.hh:644
static const bool has_quiet_NaN
Definition bigunsignedint.hh:685
static const bool is_signed
Definition bigunsignedint.hh:664
static const bool is_integer
Definition bigunsignedint.hh:665
static const bool is_bounded
Definition bigunsignedint.hh:712
static Dune::bigunsignedint< k > denorm_min() noexcept
Definition bigunsignedint.hh:706
static const bool is_modulo
Definition bigunsignedint.hh:713
static const int digits
Definition bigunsignedint.hh:662
static const bool traps
Definition bigunsignedint.hh:715
static const bool tinyness_before
Definition bigunsignedint.hh:716
static const int min_exponent10
Definition bigunsignedint.hh:680
static Dune::bigunsignedint< k > max()
Definition bigunsignedint.hh:651
static Dune::bigunsignedint< k > min()
Definition bigunsignedint.hh:646
static Dune::bigunsignedint< k > epsilon()
Definition bigunsignedint.hh:669
static const float_round_style round_style
Definition bigunsignedint.hh:717
static const bool has_denorm_loss
Definition bigunsignedint.hh:689
static Dune::bigunsignedint< k > signaling_NaN() noexcept
Definition bigunsignedint.hh:701
static const int max_exponent10
Definition bigunsignedint.hh:682
static const bool is_exact
Definition bigunsignedint.hh:666
static const float_denorm_style has_denorm
Definition bigunsignedint.hh:688
static const bool has_signaling_NaN
Definition bigunsignedint.hh:686
static Dune::bigunsignedint< k > infinity() noexcept
Definition bigunsignedint.hh:691
static const int max_exponent
Definition bigunsignedint.hh:681
static Dune::bigunsignedint< k > quiet_NaN() noexcept
Definition bigunsignedint.hh:696
static const bool has_infinity
Definition bigunsignedint.hh:684
static Dune::bigunsignedint< k > round_error()
Definition bigunsignedint.hh:674
Base class for Dune-Exceptions.
Definition exceptions.hh:98
Default exception class for mathematical errors.
Definition exceptions.hh:335