6#ifndef DUNE_BIGUNSIGNEDINT_HH
7#define DUNE_BIGUNSIGNEDINT_HH
49 struct numeric_limits_helper
54 static std::uint16_t& digit(T& big_unsigned_int, std::size_t i)
56 return big_unsigned_int.digit[i];
77 constexpr static int bits = std::numeric_limits<std::uint16_t>::digits;
78 constexpr static int n = k/bits+(k%bits!=0);
79 constexpr static int hexdigits = 4;
80 constexpr static int bitmask = 0xFFFF;
81 constexpr static int compbitmask = 0xFFFF0000;
82 constexpr static int overflowmask = 0x1;
88 template<
typename Signed>
89 bigunsignedint (Signed x,
typename std::enable_if<std::is_integral<Signed>::value && std::is_signed<Signed>::value>::type* = 0);
95 void print (std::ostream& s)
const ;
168 std::uint_least32_t
touint()
const;
181 return hash_range(arg.digit,arg.digit + arg.n);
185 std::uint16_t digit[n];
189 inline void assign(std::uintmax_t x);
202 template<
typename Signed>
218 static const int no=std::min<int>(n, std::numeric_limits<std::uintmax_t>::digits/bits);
220 for(
int i=0; i<no; ++i) {
221 digit[i] = (x&bitmask);
224 for (
unsigned int i=no; i<n; i++) digit[i]=0;
231 return (digit[1]<<bits)+digit[0];
237 int firstInZeroRange=n;
238 for(
int i=n-1; i>=0; --i)
243 int representableDigits=std::numeric_limits<double>::digits/bits;
244 int lastInRepresentableRange=0;
245 if(representableDigits<firstInZeroRange)
246 lastInRepresentableRange=firstInZeroRange-representableDigits;
248 for(
int i=firstInZeroRange-1; i>=lastInRepresentableRange; --i)
249 val =val*(1<<bits)+digit[i];
250 return val*(1<<(bits*lastInRepresentableRange));
259 for (
int i=n-1; i>=0; i--)
260 for (
int d=hexdigits-1; d>=0; d--)
263 int current = (digit[i]>>(d*4))&0xF;
267 s << std::hex << current;
270 else if (!leading) s << std::hex << current;
272 if (leading) s <<
"0";
283 #define DUNE_BINOP(OP) \
285 inline bigunsignedint<k> bigunsignedint<k>::operator OP (const bigunsignedint<k> &x) const \
304 inline bigunsignedint<k>& bigunsignedint<k>::operator+= (
const bigunsignedint<k>& x)
306 std::uint_fast32_t overflow=0;
308 for (
unsigned int i=0; i<n; i++)
310 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) +
static_cast<std::uint_fast32_t
>(x.digit[i]) + overflow;
311 digit[i] = sum&bitmask;
312 overflow = (sum>>bits)&overflowmask;
318 inline bigunsignedint<k>& bigunsignedint<k>::operator-= (
const bigunsignedint<k>& x)
320 std::int_fast32_t overflow=0;
322 for (
unsigned int i=0; i<n; i++)
324 std::int_fast32_t diff =
static_cast<std::int_fast32_t
>(digit[i]) -
static_cast<std::int_fast32_t
>(x.digit[i]) - overflow;
327 digit[i] =
static_cast<std::uint16_t
>(diff);
332 digit[i] =
static_cast<std::uint16_t
>(diff+bitmask+1);
340 inline bigunsignedint<k>& bigunsignedint<k>::operator*= (
const bigunsignedint<k>& x)
342 bigunsignedint<2*k> finalproduct(0);
344 for (
unsigned int m=0; m<n; m++)
346 bigunsignedint<2*k> singleproduct(0);
347 std::uint_fast32_t overflow(0);
348 for (
unsigned int i=0; i<n; i++)
350 std::uint_fast32_t digitproduct =
static_cast<std::uint_fast32_t
>(digit[i])*
static_cast<std::uint_fast32_t
>(x.digit[m])+overflow;
351 singleproduct.digit[i+m] =
static_cast<std::uint16_t
>(digitproduct&bitmask);
352 overflow = (digitproduct>>bits)&bitmask;
354 finalproduct = finalproduct+singleproduct;
357 for (
unsigned int i=0; i<n; i++) digit[i] = finalproduct.digit[i];
364 std::uint_fast32_t overflow=1;
366 for (
unsigned int i=0; i<n; i++)
368 std::uint_fast32_t sum =
static_cast<std::uint_fast32_t
>(digit[i]) + overflow;
369 digit[i] = sum&bitmask;
370 overflow = (sum>>bits)&overflowmask;
395 inline bigunsignedint<k>& bigunsignedint<k>::operator%= (
const bigunsignedint<k>& x)
407 inline bigunsignedint<k>& bigunsignedint<k>::operator&= (
const bigunsignedint<k>& x)
409 for (
unsigned int i=0; i<n; i++)
410 digit[i] = digit[i]&x.digit[i];
415 inline bigunsignedint<k>& bigunsignedint<k>::operator^= (
const bigunsignedint<k>& x)
417 for (
unsigned int i=0; i<n; i++)
418 digit[i] = digit[i]^x.digit[i];
423 inline bigunsignedint<k>& bigunsignedint<k>::operator|= (
const bigunsignedint<k>& x)
425 for (
unsigned int i=0; i<n; i++)
426 digit[i] = digit[i]|x.digit[i];
434 for (
unsigned int i=0; i<n; i++)
435 result.digit[i] = ~digit[i];
446 for (
int i=n-1-j; i>=0; i--)
447 result.digit[i+j] = digit[i];
451 for (
int i=n-1; i>=0; i--)
453 unsigned int temp = result.digit[i];
455 result.digit[i] =
static_cast<std::uint16_t
>(temp&bitmask);
458 result.digit[i+1] = result.digit[i+1]|temp;
471 for (
unsigned int i=0; i<n-j; i++)
472 result.digit[i] = digit[i+j];
476 for (
unsigned int i=0; i<n; i++)
478 std::uint_fast32_t temp = result.digit[i];
479 temp = temp<<(bits-j);
480 result.digit[i] =
static_cast<std::uint16_t
>((temp&compbitmask)>>bits);
482 result.digit[i-1] = result.digit[i-1] | (temp&bitmask);
491 for (
unsigned int i=0; i<n; i++)
492 if (digit[i]!=x.digit[i])
return true;
499 return !((*this)!=x);
505 for (
int i=n-1; i>=0; i--)
506 if (digit[i]<x.digit[i])
return true;
507 else if (digit[i]>x.digit[i])
return false;
514 for (
int i=n-1; i>=0; i--)
515 if (digit[i]<x.digit[i])
return true;
516 else if (digit[i]>x.digit[i])
return false;
523 return !((*this)<=x);
541 inline bigunsignedint<k> operator- (
const bigunsignedint<k>& x, std::uintmax_t y)
543 bigunsignedint<k> temp(y);
548 inline bigunsignedint<k> operator* (
const bigunsignedint<k>& x, std::uintmax_t y)
550 bigunsignedint<k> temp(y);
555 inline bigunsignedint<k> operator/ (
const bigunsignedint<k>& x, std::uintmax_t y)
557 bigunsignedint<k> temp(y);
562 inline bigunsignedint<k> operator% (
const bigunsignedint<k>& x, std::uintmax_t y)
564 bigunsignedint<k> temp(y);
569 inline bigunsignedint<k> operator+ (std::uintmax_t x,
const bigunsignedint<k>& y)
571 bigunsignedint<k> temp(x);
576 inline bigunsignedint<k> operator- (std::uintmax_t x,
const bigunsignedint<k>& y)
578 bigunsignedint<k> temp(x);
583 inline bigunsignedint<k> operator* (std::uintmax_t x,
const bigunsignedint<k>& y)
585 bigunsignedint<k> temp(x);
590 inline bigunsignedint<k> operator/ (std::uintmax_t x,
const bigunsignedint<k>& y)
592 bigunsignedint<k> temp(x);
597 inline bigunsignedint<k> operator% (std::uintmax_t x,
const bigunsignedint<k>& y)
599 bigunsignedint<k> temp(x);
604 template<
class T>
struct IsNumber;
616 struct numeric_limits<
Dune::bigunsignedint<k> >
617 :
private Dune::Impl::numeric_limits_helper<Dune::bigunsignedint<k> >
620 static const bool is_specialized =
true;
630 for(std::size_t i=0; i < Dune::bigunsignedint<k>::n; ++i)
640 static const bool is_signed =
false;
641 static const bool is_integer =
true;
642 static const bool is_exact =
true;
643 static const int radix = 2;
655 static const int min_exponent = 0;
656 static const int min_exponent10 = 0;
657 static const int max_exponent = 0;
658 static const int max_exponent10 = 0;
660 static const bool has_infinity =
false;
661 static const bool has_quiet_NaN =
false;
662 static const bool has_signaling_NaN =
false;
664 static const float_denorm_style has_denorm = denorm_absent;
665 static const bool has_denorm_loss =
false;
687 static const bool is_iec559 =
false;
688 static const bool is_bounded =
true;
689 static const bool is_modulo =
true;
691 static const bool traps =
false;
692 static const bool tinyness_before =
false;
693 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:73
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,...)
Definition: exceptions.hh:312
constexpr auto max
Function object that returns the greater of the given values.
Definition: hybridutilities.hh:484
constexpr auto min
Function object that returns the smaller of the given values.
Definition: hybridutilities.hh:506
bigunsignedint< k > operator<<(int i) const
left shift
Definition: bigunsignedint.hh:440
bool operator<=(const bigunsignedint< k > &x) const
less than or equal
Definition: bigunsignedint.hh:512
void print(std::ostream &s) const
Print number in hex notation.
Definition: bigunsignedint.hh:254
bool operator<(const bigunsignedint< k > &x) const
less than
Definition: bigunsignedint.hh:503
bool operator==(const bigunsignedint< k > &x) const
equal
Definition: bigunsignedint.hh:497
bool operator!=(const bigunsignedint< k > &x) const
not equal
Definition: bigunsignedint.hh:489
bigunsignedint()
Construct uninitialized.
Definition: bigunsignedint.hh:196
bool operator>=(const bigunsignedint< k > &x) const
greater or equal
Definition: bigunsignedint.hh:527
bigunsignedint< k > operator>>(int i) const
right shift
Definition: bigunsignedint.hh:465
bigunsignedint< k > operator~() const
bitwise complement
Definition: bigunsignedint.hh:431
bigunsignedint< k > & operator++()
prefix increment
Definition: bigunsignedint.hh:362
double todouble() const
Convert to a double.
Definition: bigunsignedint.hh:235
bool operator>(const bigunsignedint< k > &x) const
greater than
Definition: bigunsignedint.hh:521
std::uint_least32_t touint() const
export to other types
Definition: bigunsignedint.hh:229
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
A traits class describing the mapping of types onto MPI_Datatypes.
Definition: mpitraits.hh:41