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;
79 constexpr static int n = k/bits+(k%bits!=0);
80 constexpr static int hexdigits = 4;
81 constexpr static int bitmask = 0xFFFF;
82 constexpr static int compbitmask = 0xFFFF0000;
83 constexpr static int overflowmask = 0x1;
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)));
205 return hash_range(arg.digit,arg.digit + arg.n);
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";
307 #define DUNE_BINOP(OP) \
309 inline bigunsignedint<k> bigunsignedint<k>::operator OP (const bigunsignedint<k> &x) const \
328 inline bigunsignedint<k>& bigunsignedint<k>::operator+= (
const bigunsignedint<k>& x)
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;
342 inline bigunsignedint<k>& bigunsignedint<k>::operator-= (
const bigunsignedint<k>& x)
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);
364 inline bigunsignedint<k>& bigunsignedint<k>::operator*= (
const bigunsignedint<k>& x)
366 bigunsignedint<2*k> finalproduct(0);
368 for (
unsigned int m=0; m<n; m++)
370 bigunsignedint<2*k> singleproduct(0);
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;
419 inline bigunsignedint<k>& bigunsignedint<k>::operator%= (
const bigunsignedint<k>& x)
431 inline bigunsignedint<k>& bigunsignedint<k>::operator&= (
const bigunsignedint<k>& x)
433 for (
unsigned int i=0; i<n; i++)
434 digit[i] = digit[i]&x.digit[i];
439 inline bigunsignedint<k>& bigunsignedint<k>::operator^= (
const bigunsignedint<k>& x)
441 for (
unsigned int i=0; i<n; i++)
442 digit[i] = digit[i]^x.digit[i];
447 inline bigunsignedint<k>& bigunsignedint<k>::operator|= (
const bigunsignedint<k>& x)
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);
565 inline bigunsignedint<k> operator- (
const bigunsignedint<k>& x, std::uintmax_t y)
567 bigunsignedint<k> temp(y);
572 inline bigunsignedint<k> operator* (
const bigunsignedint<k>& x, std::uintmax_t y)
574 bigunsignedint<k> temp(y);
579 inline bigunsignedint<k> operator/ (
const bigunsignedint<k>& x, std::uintmax_t y)
581 bigunsignedint<k> temp(y);
586 inline bigunsignedint<k> operator% (
const bigunsignedint<k>& x, std::uintmax_t y)
588 bigunsignedint<k> temp(y);
593 inline bigunsignedint<k> operator+ (std::uintmax_t x,
const bigunsignedint<k>& y)
595 bigunsignedint<k> temp(x);
600 inline bigunsignedint<k> operator- (std::uintmax_t x,
const bigunsignedint<k>& y)
602 bigunsignedint<k> temp(x);
607 inline bigunsignedint<k> operator* (std::uintmax_t x,
const bigunsignedint<k>& y)
609 bigunsignedint<k> temp(x);
614 inline bigunsignedint<k> operator/ (std::uintmax_t x,
const bigunsignedint<k>& y)
616 bigunsignedint<k> temp(x);
621 inline bigunsignedint<k> operator% (std::uintmax_t x,
const bigunsignedint<k>& y)
623 bigunsignedint<k> temp(x);
628 template<
class T>
struct IsNumber;
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;
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Default exception class for mathematical errors.
Definition: exceptions.hh:333
Portable very large unsigned integers.
Definition: bigunsignedint.hh:74
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
friend int bit_width(const bigunsignedint &arg)
Bit width.
Definition: bigunsignedint.hh:184
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
friend int countl_zero(const bigunsignedint &arg)
Count left zero bits.
Definition: bigunsignedint.hh:198
A few common exception classes.
#define DUNE_THROW(E,...)
Definition: exceptions.hh:312
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:485
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:507
bigunsignedint< k > operator<<(int i) const
left shift
Definition: bigunsignedint.hh:464
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
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
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
bitwise complement
Definition: bigunsignedint.hh:455
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:386
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:259
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.
#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
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
Whether this type acts as a scalar in the context of (hierarchically blocked) containers.
Definition: typetraits.hh:194