Changeset View
Standalone View
src/arith_uint256.h
Show All 22 Lines | |||||
/** Template base class for unsigned big integers. */ | /** Template base class for unsigned big integers. */ | ||||
template <unsigned int BITS> class base_uint { | template <unsigned int BITS> class base_uint { | ||||
protected: | protected: | ||||
static constexpr int WIDTH = BITS / 32; | static constexpr int WIDTH = BITS / 32; | ||||
uint32_t pn[WIDTH]; | uint32_t pn[WIDTH]; | ||||
public: | public: | ||||
base_uint() { | base_uint() { | ||||
static_assert( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"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( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"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( | |||||
BITS / 32 > 0 && BITS % 32 == 0, | |||||
"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); | ||||
▲ Show 20 Lines • Show All 86 Lines • ▼ Show 20 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 (++pn[i] == 0 && i < WIDTH - 1) | 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 (--pn[i] == (uint32_t)-1 && i < WIDTH - 1) | while (i < WIDTH && --pn[i] == (uint32_t)-1) | ||||
Fabien: I'd like to suggest `std::numeric_limits<uint32_t>::max()` as a replacement for `(uint32_t)-1`… | |||||
markblundebergUnsubmitted Not Done Inline ActionsI looked this up a bit the other day -- apparently Visual Studio in earlier versions didn't have constexpr for ...::max(), but C++11 make constexpr mandatory and I guess Visual Studio has fixed that since. https://stackoverflow.com/questions/12240085/are-numeric-limits-min-max-constexpr markblundeberg: I looked this up a bit the other day -- apparently Visual Studio in earlier versions didn't… | |||||
FabienUnsubmitted Not Done Inline ActionsAh good catch I missed the (until C++11) for the non constexpr version ! I don't think we support the VS compiler, or at least there is no instruction for it, only cross build with MinGw, so I guess it should be safe to use std::numeric_limits. @nakihito can you please update accordingly ? Fabien: Ah good catch I missed the `(until C++11)` for the non `constexpr` version !
I don't think we… | |||||
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); | ||||
▲ Show 20 Lines • Show All 126 Lines • Show Last 20 Lines |
I'd like to suggest std::numeric_limits<uint32_t>::max() as a replacement for (uint32_t)-1, but I wonder if this will be built as a constexpr by the compiler or generate a call at each loop increment ? Ping @deadalnix