13template <
unsigned int BITS>
15 static_assert(BITS / 32 > 0 && BITS % 32 == 0,
16 "Template parameter BITS must be a positive multiple of 32.");
21template <
unsigned int BITS>
24 for (
int i = 0; i < WIDTH; i++) {
29 for (
int i = 0; i < WIDTH; i++) {
30 if (i + k + 1 < WIDTH && shift != 0) {
31 pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
34 pn[i + k] |= (a.pn[i] << shift);
40template <
unsigned int BITS>
43 for (
int i = 0; i < WIDTH; i++) {
48 for (
int i = 0; i < WIDTH; i++) {
49 if (i - k - 1 >= 0 && shift != 0) {
50 pn[i - k - 1] |= (a.
pn[i] << (32 - shift));
53 pn[i - k] |= (a.
pn[i] >> shift);
59template <
unsigned int BITS>
62 for (
int i = 0; i < WIDTH; i++) {
63 uint64_t n = carry + (uint64_t)b32 * pn[i];
64 pn[i] = n & 0xffffffff;
70template <
unsigned int BITS>
73 for (
int j = 0; j < WIDTH; j++) {
75 for (
int i = 0; i + j < WIDTH; i++) {
76 uint64_t n = carry + a.
pn[i + j] + (uint64_t)pn[j] * b.
pn[i];
77 a.
pn[i + j] = n & 0xffffffff;
85template <
unsigned int BITS>
93 int num_bits = num.
bits();
94 int div_bits = div.
bits();
99 if (div_bits > num_bits) {
102 int shift = num_bits - div_bits;
109 pn[shift / 32] |= (1U << (shift & 31));
119template <
unsigned int BITS>
121 for (
int i = WIDTH - 1; i >= 0; i--) {
122 if (pn[i] < b.
pn[i]) {
125 if (pn[i] > b.
pn[i]) {
133 for (
int i = WIDTH - 1; i >= 2; i--) {
138 if (pn[1] != (b >> 32)) {
141 if (pn[0] != (b & 0xfffffffful)) {
150 for (
int i = 0; i < WIDTH; i++) {
152 fact *= 4294967296.0;
165template <
unsigned int BITS>
175 for (
int pos = WIDTH - 1; pos >= 0; pos--) {
177 for (
int nbits = 31; nbits > 0; nbits--) {
178 if (pn[pos] & 1U << nbits) {
179 return 32 * pos + nbits + 1;
208 int nSize = nCompact >> 24;
209 uint32_t nWord = nCompact & 0x007fffff;
211 nWord >>= 8 * (3 - nSize);
215 *
this <<= 8 * (nSize - 3);
218 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
222 nWord != 0 && ((nSize > 34) || (nWord > 0xff && nSize > 33) ||
223 (nWord > 0xffff && nSize > 32));
229 int nSize = (
bits() + 7) / 8;
230 uint32_t nCompact = 0;
232 nCompact =
GetLow64() << 8 * (3 - nSize);
240 if (nCompact & 0x00800000) {
244 assert((nCompact & ~0x007fffffU) == 0);
246 nCompact |= nSize << 24;
247 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
253 for (
int i = b.
WIDTH - 1; i >= 0; i--) {
254 const double fact = std::pow(4294967296.0, i);
255 b.
pn[i] = uint32_t(d / fact);
263 for (
int x = 0; x < a.
WIDTH; ++x) {
270 for (
int x = 0; x < b.
WIDTH; ++x) {
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
256-bit unsigned big integer.
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
uint32_t GetCompact(bool fNegative=false) const
static arith_uint256 fromDouble(double d)
std::string GetHex() const
Template base class for unsigned big integers.
int CompareTo(const base_uint &b) const
base_uint & operator>>=(unsigned int shift)
static constexpr int WIDTH
base_uint & operator*=(uint32_t b32)
bool EqualTo(uint64_t b) const
base_uint & operator<<=(unsigned int shift)
std::string ToString() const
base_uint & operator/=(const base_uint &b)
uint64_t GetLow64() const
void SetHex(const char *psz)
std::string GetHex() const
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.
static void WriteLE32(uint8_t *ptr, uint32_t x)
static uint32_t ReadLE32(const uint8_t *ptr)
uint256 uint256S(const char *str)
uint256 from const char *.