Changeset View
Changeset View
Standalone View
Standalone View
src/arith_uint256.h
Show All 27 Lines | protected: | ||||
uint32_t pn[WIDTH]; | uint32_t pn[WIDTH]; | ||||
public: | public: | ||||
base_uint() { | base_uint() { | ||||
static_assert( | static_assert( | ||||
BITS / 32 > 0 && BITS % 32 == 0, | BITS / 32 > 0 && BITS % 32 == 0, | ||||
"Template parameter BITS must be a positive multiple of 32."); | "Template parameter BITS must be a positive multiple of 32."); | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
} | |||||
base_uint(const base_uint &b) { | base_uint(const base_uint &b) { | ||||
static_assert( | static_assert( | ||||
BITS / 32 > 0 && BITS % 32 == 0, | BITS / 32 > 0 && BITS % 32 == 0, | ||||
"Template parameter BITS must be a positive multiple of 32."); | "Template parameter BITS must be a positive multiple of 32."); | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
} | } | ||||
} | |||||
base_uint &operator=(const base_uint &b) { | base_uint &operator=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] = b.pn[i]; | pn[i] = b.pn[i]; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
base_uint(uint64_t b) { | base_uint(uint64_t b) { | ||||
static_assert( | static_assert( | ||||
BITS / 32 > 0 && BITS % 32 == 0, | BITS / 32 > 0 && BITS % 32 == 0, | ||||
"Template parameter BITS must be a positive multiple of 32."); | "Template parameter BITS must be a positive multiple of 32."); | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) { | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | } | ||||
} | |||||
explicit base_uint(const std::string &str); | explicit base_uint(const std::string &str); | ||||
const base_uint operator~() const { | const base_uint operator~() const { | ||||
base_uint ret; | base_uint ret; | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
ret.pn[i] = ~pn[i]; | ret.pn[i] = ~pn[i]; | ||||
} | |||||
return ret; | return ret; | ||||
} | } | ||||
const base_uint operator-() const { | const base_uint operator-() const { | ||||
base_uint ret; | base_uint ret; | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
ret.pn[i] = ~pn[i]; | ret.pn[i] = ~pn[i]; | ||||
} | |||||
++ret; | ++ret; | ||||
return ret; | return ret; | ||||
} | } | ||||
double getdouble() const; | double getdouble() const; | ||||
base_uint &operator=(uint64_t b) { | base_uint &operator=(uint64_t b) { | ||||
pn[0] = (unsigned int)b; | pn[0] = (unsigned int)b; | ||||
pn[1] = (unsigned int)(b >> 32); | pn[1] = (unsigned int)(b >> 32); | ||||
for (int i = 2; i < WIDTH; i++) | for (int i = 2; i < WIDTH; i++) { | ||||
pn[i] = 0; | pn[i] = 0; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator^=(const base_uint &b) { | base_uint &operator^=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] ^= b.pn[i]; | pn[i] ^= b.pn[i]; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator&=(const base_uint &b) { | base_uint &operator&=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] &= b.pn[i]; | pn[i] &= b.pn[i]; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator|=(const base_uint &b) { | base_uint &operator|=(const base_uint &b) { | ||||
for (int i = 0; i < WIDTH; i++) | for (int i = 0; i < WIDTH; i++) { | ||||
pn[i] |= b.pn[i]; | pn[i] |= b.pn[i]; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
base_uint &operator^=(uint64_t b) { | base_uint &operator^=(uint64_t b) { | ||||
pn[0] ^= (unsigned int)b; | pn[0] ^= (unsigned int)b; | ||||
pn[1] ^= (unsigned int)(b >> 32); | pn[1] ^= (unsigned int)(b >> 32); | ||||
return *this; | return *this; | ||||
} | } | ||||
Show All 38 Lines | public: | ||||
base_uint &operator*=(uint32_t b32); | base_uint &operator*=(uint32_t b32); | ||||
base_uint &operator*=(const base_uint &b); | base_uint &operator*=(const base_uint &b); | ||||
base_uint &operator/=(const base_uint &b); | base_uint &operator/=(const base_uint &b); | ||||
base_uint &operator++() { | base_uint &operator++() { | ||||
// prefix operator | // prefix operator | ||||
int i = 0; | int i = 0; | ||||
while (i < WIDTH && ++pn[i] == 0) | while (i < WIDTH && ++pn[i] == 0) { | ||||
i++; | i++; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
const base_uint operator++(int) { | const base_uint operator++(int) { | ||||
// postfix operator | // postfix operator | ||||
const base_uint ret = *this; | const base_uint ret = *this; | ||||
++(*this); | ++(*this); | ||||
return ret; | return ret; | ||||
} | } | ||||
base_uint &operator--() { | base_uint &operator--() { | ||||
// prefix operator | // prefix operator | ||||
int i = 0; | int i = 0; | ||||
while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max()) | while (i < WIDTH && --pn[i] == std::numeric_limits<uint32_t>::max()) { | ||||
i++; | i++; | ||||
} | |||||
return *this; | return *this; | ||||
} | } | ||||
const base_uint operator--(int) { | const base_uint operator--(int) { | ||||
// postfix operator | // postfix operator | ||||
const base_uint ret = *this; | const base_uint ret = *this; | ||||
--(*this); | --(*this); | ||||
return ret; | return ret; | ||||
▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | public: | ||||
* Returns the position of the highest bit set plus one, or zero if the | * Returns the position of the highest bit set plus one, or zero if the | ||||
* value is zero. | * value is zero. | ||||
*/ | */ | ||||
unsigned int bits() const; | unsigned int bits() const; | ||||
uint64_t GetLow64() const { | uint64_t GetLow64() const { | ||||
static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / " | static_assert(WIDTH >= 2, "Assertion WIDTH >= 2 failed (WIDTH = BITS / " | ||||
"32). BITS is a template parameter."); | "32). BITS is a template parameter."); | ||||
return pn[0] | (uint64_t)pn[1] << 32; | return pn[0] | uint64_t(pn[1]) << 32; | ||||
} | } | ||||
}; | }; | ||||
/** 256-bit unsigned big integer. */ | /** 256-bit unsigned big integer. */ | ||||
class arith_uint256 : public base_uint<256> { | class arith_uint256 : public base_uint<256> { | ||||
public: | public: | ||||
arith_uint256() {} | arith_uint256() {} | ||||
arith_uint256(const base_uint<256> &b) : base_uint<256>(b) {} | arith_uint256(const base_uint<256> &b) : base_uint<256>(b) {} | ||||
Show All 33 Lines |