Skip to content

Commit 853c920

Browse files
committed
uint256 cxx-20 constexpr patch
1 parent 4af72d8 commit 853c920

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ double AddrInfo::GetChance(NodeSeconds now) const
108108

109109
AddrManImpl::AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
110110
: insecure_rand{deterministic}
111-
, nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
111+
, nKey{deterministic ? uint256{1ULL} : insecure_rand.rand256()}
112112
, m_consistency_check_ratio{consistency_check_ratio}
113113
, m_netgroupman{netgroupman}
114114
{

src/uint256.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ template std::string base_blob<160>::ToString() const;
3131
template std::string base_blob<256>::GetHex() const;
3232
template std::string base_blob<256>::ToString() const;
3333

34-
const uint256 uint256::ZERO(0);
35-
const uint256 uint256::ONE(1);
34+
const uint256 uint256::ZERO(0ULL);
35+
const uint256 uint256::ONE(1ULL);

src/uint256.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,22 @@ class uint256 : public base_blob<256> {
198198
static std::optional<uint256> FromHex(std::string_view str) { return detail::FromHex<uint256>(str); }
199199
static std::optional<uint256> FromUserHex(std::string_view str) { return detail::FromUserHex<uint256>(str); }
200200
constexpr uint256() = default;
201-
consteval explicit uint256(std::string_view hex_str) : base_blob<256>(hex_str) {}
201+
constexpr explicit uint256(uint64_t val) : base_blob<256>() {
202+
for (size_t i = 0; i < 8; ++i) {
203+
m_data[i] = (val >> (i * 8)) & 0xFF;
204+
}
205+
}
202206
constexpr explicit uint256(uint8_t v) : base_blob<256>(v) {}
207+
consteval explicit uint256(std::string_view hex_str) : base_blob<256>() {
208+
if (hex_str.size() > 64) {
209+
throw "Invalid hex string size in consteval uint256";
210+
}
211+
for (size_t i = 0; i < 32; ++i) {
212+
unsigned char high = util::ConstevalHexDigit(hex_str[2 * i]);
213+
unsigned char low = util::ConstevalHexDigit(hex_str[2 * i + 1]);
214+
m_data[31 - i] = (high << 4) | low;
215+
}
216+
}
203217
constexpr explicit uint256(std::span<const unsigned char> vch) : base_blob<256>(vch) {}
204218
static const uint256 ZERO;
205219
static const uint256 ONE;

0 commit comments

Comments
 (0)