16 template <
unsigned int BITS>
19 static_assert(BITS/32 > 0 && BITS%32 == 0,
"Template parameter BITS must be a positive multiple of 32.");
24 template <
unsigned int BITS>
28 for (
int i = 0; i < WIDTH; i++)
32 for (
int i = 0; i < WIDTH; i++) {
33 if (i + k + 1 < WIDTH && shift != 0)
34 pn[i + k + 1] |= (a.
pn[i] >> (32 - shift));
36 pn[i + k] |= (a.
pn[i] << shift);
41 template <
unsigned int BITS>
45 for (
int i = 0; i < WIDTH; i++)
49 for (
int i = 0; i < WIDTH; i++) {
50 if (i - k - 1 >= 0 && shift != 0)
51 pn[i - k - 1] |= (a.
pn[i] << (32 - shift));
53 pn[i - k] |= (a.
pn[i] >> shift);
58 template <
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;
70 template <
unsigned int BITS>
75 for (
int j = 0; j < WIDTH; j++) {
77 for (
int i = 0; i + j < WIDTH; i++) {
78 uint64_t n = carry + pn[i + j] + (uint64_t)a.
pn[j] * b.
pn[i];
79 pn[i + j] = n & 0xffffffff;
86 template <
unsigned int BITS>
92 int num_bits = num.
bits();
93 int div_bits = div.
bits();
96 if (div_bits > num_bits)
98 int shift = num_bits - div_bits;
103 pn[shift / 32] |= (1 << (shift & 31));
112 template <
unsigned int BITS>
115 for (
int i = WIDTH - 1; i >= 0; i--) {
124 template <
unsigned int BITS>
127 for (
int i = WIDTH - 1; i >= 2; i--) {
131 if (pn[1] != (b >> 32))
133 if (pn[0] != (b & 0xfffffffful))
138 template <
unsigned int BITS>
143 for (
int i = 0; i < WIDTH; i++) {
145 fact *= 4294967296.0;
150 template <
unsigned int BITS>
156 template <
unsigned int BITS>
162 template <
unsigned int BITS>
168 template <
unsigned int BITS>
174 template <
unsigned int BITS>
177 for (
int pos = WIDTH - 1; pos >= 0; pos--) {
179 for (
int nbits = 31; nbits > 0; nbits--) {
180 if (pn[pos] & 1 << nbits)
181 return 32 * pos + nbits + 1;
209 int nSize = nCompact >> 24;
210 uint32_t nWord = nCompact & 0x007fffff;
212 nWord >>= 8 * (3 - nSize);
216 *
this <<= 8 * (nSize - 3);
219 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
221 *pfOverflow = nWord != 0 && ((nSize > 34) ||
222 (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);
239 if (nCompact & 0x00800000) {
243 assert((nCompact & ~0x007fffff) == 0);
245 nCompact |= nSize << 24;
246 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
253 for(
int x=0; x<a.
WIDTH; ++x)
254 WriteLE32(b.
begin() + x*4, a.
pn[x]);
260 for(
int x=0; x<b.
WIDTH; ++x)
261 b.
pn[x] = ReadLE32(a.
begin() + x*4);
bool EqualTo(uint64_t b) const
void SetHex(const char *psz)
std::string ToString() const
base_uint & operator/=(const base_uint &b)
Template base class for unsigned big integers.
base_uint & operator<<=(unsigned int shift)
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
uint256 uint256S(const char *str)
int CompareTo(const base_uint &b) const
friend uint256 ArithToUint256(const arith_uint256 &)
256-bit unsigned big integer.
uint256 ArithToUint256(const arith_uint256 &a)
uint64_t GetLow64() const
std::string GetHex() const
base_uint & operator*=(uint32_t b32)
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...
std::string GetHex() const
friend arith_uint256 UintToArith256(const uint256 &)
base_uint & operator>>=(unsigned int shift)
unsigned int bits() const
Returns the position of the highest bit set plus one, or zero if the value is zero.