11 template <
unsigned int BITS>
13 static_assert(BITS / 32 > 0 && BITS % 32 == 0,
14 "Template parameter BITS must be a positive multiple of 32.");
19 template <
unsigned int BITS>
22 for (
int i = 0; i < WIDTH; i++) {
27 for (
int i = 0; i < WIDTH; i++) {
28 if (i + k + 1 < WIDTH && shift != 0) {
29 pn[i + k + 1] |= (a.pn[i] >> (32 - shift));
32 pn[i + k] |= (a.pn[i] << shift);
38 template <
unsigned int BITS>
41 for (
int i = 0; i < WIDTH; i++) {
46 for (
int i = 0; i < WIDTH; i++) {
47 if (i - k - 1 >= 0 && shift != 0) {
48 pn[i - k - 1] |= (a.
pn[i] << (32 - shift));
51 pn[i - k] |= (a.
pn[i] >> shift);
57 template <
unsigned int BITS>
60 for (
int i = 0; i < WIDTH; i++) {
61 uint64_t n = carry + (uint64_t)b32 * pn[i];
62 pn[i] = n & 0xffffffff;
68 template <
unsigned int BITS>
71 for (
int j = 0; j < WIDTH; j++) {
73 for (
int i = 0; i + j < WIDTH; i++) {
74 uint64_t n = carry + a.
pn[i + j] + (uint64_t)pn[j] * b.
pn[i];
75 a.
pn[i + j] = n & 0xffffffff;
83 template <
unsigned int BITS>
91 int num_bits = num.
bits();
92 int div_bits = div.
bits();
97 if (div_bits > num_bits) {
100 int shift = num_bits - div_bits;
107 pn[shift / 32] |= (1U << (shift & 31));
117 template <
unsigned int BITS>
119 for (
int i = WIDTH - 1; i >= 0; i--) {
120 if (pn[i] < b.
pn[i]) {
123 if (pn[i] > b.
pn[i]) {
131 for (
int i = WIDTH - 1; i >= 2; i--) {
136 if (pn[1] != (b >> 32)) {
139 if (pn[0] != (b & 0xfffffffful)) {
148 for (
int i = 0; i < WIDTH; i++) {
150 fact *= 4294967296.0;
163 template <
unsigned int BITS>
173 for (
int pos = WIDTH - 1; pos >= 0; pos--) {
175 for (
int nbits = 31; nbits > 0; nbits--) {
176 if (pn[pos] & 1U << nbits) {
177 return 32 * pos + nbits + 1;
206 int nSize = nCompact >> 24;
207 uint32_t nWord = nCompact & 0x007fffff;
209 nWord >>= 8 * (3 - nSize);
213 *
this <<= 8 * (nSize - 3);
216 *pfNegative = nWord != 0 && (nCompact & 0x00800000) != 0;
220 nWord != 0 && ((nSize > 34) || (nWord > 0xff && nSize > 33) ||
221 (nWord > 0xffff && nSize > 32));
227 int nSize = (
bits() + 7) / 8;
228 uint32_t nCompact = 0;
230 nCompact =
GetLow64() << 8 * (3 - nSize);
238 if (nCompact & 0x00800000) {
242 assert((nCompact & ~0x007fffffU) == 0);
244 nCompact |= nSize << 24;
245 nCompact |= (fNegative && (nCompact & 0x007fffff) ? 0x00800000 : 0);
251 for (
int x = 0; x < a.
WIDTH; ++x) {
258 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
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 *.