6 #ifndef DUNE_BIGUNSIGNEDINT_HH 7 #define DUNE_BIGUNSIGNEDINT_HH 15 #include <type_traits> 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 ;
149 bool operator< (const bigunsignedint<k>& x)
const;
152 bool operator<= (const bigunsignedint<k>& x)
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>
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--)
284 for (
int d=hexdigits-1; d>=0; d--)
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";
301 inline std::ostream& operator<< (std::ostream& s, const bigunsignedint<k>& x)
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;
335 digit[
i] = sum&bitmask;
336 overflow = (sum>>bits)&overflowmask;
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);
376 overflow = (digitproduct>>bits)&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;
393 digit[
i] = sum&bitmask;
394 overflow = (sum>>bits)&overflowmask;
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);
640 struct numeric_limits<
Dune::bigunsignedint<k> >
641 :
private Dune::Impl::numeric_limits_helper<Dune::bigunsignedint<k> >
644 static const bool is_specialized =
true;
654 for(std::size_t
i=0; i < Dune::bigunsignedint<k>::n; ++
i)
664 static const bool is_signed =
false;
665 static const bool is_integer =
true;
666 static const bool is_exact =
true;
667 static const int radix = 2;
679 static const int min_exponent = 0;
680 static const int min_exponent10 = 0;
681 static const int max_exponent = 0;
682 static const int max_exponent10 = 0;
684 static const bool has_infinity =
false;
685 static const bool has_quiet_NaN =
false;
686 static const bool has_signaling_NaN =
false;
688 static const float_denorm_style has_denorm = denorm_absent;
689 static const bool has_denorm_loss =
false;
711 static const bool is_iec559 =
false;
712 static const bool is_bounded =
true;
713 static const bool is_modulo =
true;
715 static const bool traps =
false;
716 static const bool tinyness_before =
false;
717 static const float_round_style round_style = round_toward_zero;
bigunsignedint< k > operator*(const bigunsignedint< k > &x) const
multiply
static Dune::bigunsignedint< k > epsilon()
Definition: bigunsignedint.hh:669
bigunsignedint< k > operator>>(int i) const
right shift
Definition: bigunsignedint.hh:489
static constexpr int overflowmask
Definition: bigunsignedint.hh:83
static constexpr int hexdigits
Definition: bigunsignedint.hh:80
bigunsignedint< k > & operator^=(const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:439
bigunsignedint< k > operator%(const bigunsignedint< k > &x) const
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:259
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition: bigunsignedint.hh:628
bigunsignedint< k > operator-(const bigunsignedint< k > &x) const
subtract
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:489
bigunsignedint< k > & operator-=(const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:342
static Dune::bigunsignedint< k > quiet_NaN() noexcept
Definition: bigunsignedint.hh:696
bigunsignedint< k > operator+(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:558
bigunsignedint< k > operator|(const bigunsignedint< k > &x) const
bitwise or
friend std::size_t hash_value(const bigunsignedint &arg)
Definition: bigunsignedint.hh:203
bigunsignedint< k > operator*(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:572
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition: bigunsignedint.hh:513
friend int bit_width(const bigunsignedint &arg)
Bit width.
Definition: bigunsignedint.hh:184
static Dune::bigunsignedint< k > max()
Definition: bigunsignedint.hh:651
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
static constexpr int n
Definition: bigunsignedint.hh:79
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:386
static Dune::bigunsignedint< k > denorm_min() noexcept
Definition: bigunsignedint.hh:706
static constexpr int bits
Definition: bigunsignedint.hh:78
I i
Definition: hybridmultiindex.hh:328
bool operator==(const bigunsignedint< k > &x) const
equal
Definition: bigunsignedint.hh:521
friend int countl_zero(const bigunsignedint &arg)
Count left zero bits.
Definition: bigunsignedint.hh:198
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition: bigunsignedint.hh:551
#define DUNE_BINOP(OP)
Definition: bigunsignedint.hh:307
void assign(T &dst, const T &src, bool mask)
masked Simd assignment (scalar version)
Definition: simd.hh:447
Portable very large unsigned integers.
Definition: bigunsignedint.hh:74
bigunsignedint< k > & operator|=(const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:447
static constexpr int bitmask
Definition: bigunsignedint.hh:81
Dune namespace
Definition: alignedallocator.hh:12
static Dune::bigunsignedint< k > signaling_NaN() noexcept
Definition: bigunsignedint.hh:701
void print(std::ostream &s) const
Print number in hex notation.
Definition: bigunsignedint.hh:278
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition: bigunsignedint.hh:545
std::uint_least32_t touint() const
export to other types
Definition: bigunsignedint.hh:253
Support for calculating hash values of objects.
bigunsignedint< k > operator/(const bigunsignedint< k > &x) const
#define DUNE_THROW(E,...)
Definition: exceptions.hh:314
bigunsignedint< k > operator-(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:565
bigunsignedint< k > operator/(const bigunsignedint< k > &x, std::uintmax_t y)
Definition: bigunsignedint.hh:579
bigunsignedint< k > operator+(const bigunsignedint< k > &x) const
add
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, std::uintmax_t y)
Definition: bigunsignedint.hh:586
A few common exception classes.
bool operator<(const bigunsignedint< k > &x) const
less than
Definition: bigunsignedint.hh:527
static Dune::bigunsignedint< k > round_error()
Definition: bigunsignedint.hh:674
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
Default exception class for mathematical errors.
Definition: exceptions.hh:335
bigunsignedint< k > operator^(const bigunsignedint< k > &x) const
bitwise exor
#define DUNE_HASH_TYPE(...)
Wrapper macro for the type to be hashed in DUNE_DEFINE_HASH.
Definition: hash.hh:117
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition: bigunsignedint.hh:536
bigunsignedint< k > & operator%=(const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:419
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: bigunsignedint.hh:29
static Dune::bigunsignedint< k > infinity() noexcept
Definition: bigunsignedint.hh:691
static constexpr int compbitmask
Definition: bigunsignedint.hh:82
bigunsignedint< k > & operator*=(const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:364
bigunsignedint< k > operator~() const
bitwise complement
Definition: bigunsignedint.hh:455
static Dune::bigunsignedint< k > min()
Definition: bigunsignedint.hh:646
#define DUNE_DEFINE_HASH(template_args, type)
Defines the required struct specialization to make type hashable via Dune::hash.
Definition: hash.hh:100
bigunsignedint< k > & operator &=(const bigunsignedint< k > &x)
bigunsignedint< k > operator &(const bigunsignedint< k > &x) const
bitwise and
#define DUNE_HASH_TEMPLATE_ARGS(...)
Wrapper macro for the template arguments in DUNE_DEFINE_HASH.
Definition: hash.hh:109
bigunsignedint()
Construct uninitialized.
Definition: bigunsignedint.hh:220