Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/math/bits.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ template <typename Value, if_unsigned_integer<Value>>
constexpr size_t bit_width(Value value) NOEXCEPT
{
// zero-based position of msb.
return ceilinged_log2(value);
return is_zero(value) ? zero : add1(floored_log2(value));
}

// Called by machine::number (for to_unnegated).
Expand Down
2 changes: 1 addition & 1 deletion include/bitcoin/system/impl/math/bytes.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ constexpr size_t byte_width(Integral value) NOEXCEPT
// (zero-based position of msb) + 7 / 8.
// (bit_width(value) + 7) / 8
// (ceilinged_log2(value) + 7) / 8
return ceilinged_log256(value);
return is_zero(value) ? zero : add1(floored_log256(value));
}

// Called by machine::number (for little-endian chunk sizing).
Expand Down
24 changes: 12 additions & 12 deletions include/bitcoin/system/impl/math/logarithm.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ constexpr Exponent ceilinged_log_(Base base, Value value) NOEXCEPT
const auto factor = possible_narrow_and_sign_cast<Value>(base);

Exponent exponent = 0;
while (value > 0) { ++exponent; value /= factor; }
while (value > 1) { ++exponent; value /= factor; }
return exponent;
}
}
Expand Down Expand Up @@ -125,7 +125,7 @@ constexpr Exponent ceilinged_log2(Value value) NOEXCEPT

// base2 integral optimization over normal form.
return possible_narrow_and_sign_cast<Exponent>(
std::bit_width(to_unsigned(value)));
std::bit_width(sub1(to_unsigned(value))));
}

// Called by bc::bit_width.
Expand All @@ -134,12 +134,12 @@ template <typename Exponent, typename Value,
if_non_integral_integer<Value>>
constexpr Exponent ceilinged_log2(Value value) NOEXCEPT
{
if (is_log_overflow<2>(value))
if (is_log_overflow<2>(value) || is_one(value))
return 0;

// base2 uintx optimization over normal form.
return possible_narrow_and_sign_cast<Exponent>(
add1(mp::msb(value)));
add1(mp::msb(value - 1)));
}

// Called by bc::byte_width.
Expand All @@ -159,24 +159,24 @@ constexpr Exponent ceilinged_log256(Value value) NOEXCEPT

if constexpr (size == sizeof(uint64_t))
{
if (compare > 0x00ffffffffffffff_u64) return 8;
if (compare > 0x0000ffffffffffff_u64) return 7;
if (compare > 0x000000ffffffffff_u64) return 6;
if (compare > 0x00000000ffffffff_u64) return 5;
if (compare > 0x0100000000000000_u64) return 8;
if (compare > 0x0001000000000000_u64) return 7;
if (compare > 0x0000010000000000_u64) return 6;
if (compare > 0x0000000100000000_u64) return 5;
}

if constexpr (size >= sizeof(uint32_t))
{
if (compare > 0x00ffffff_u32) return 4;
if (compare > 0x0000ffff_u32) return 3;
if (compare > 0x01000000_u32) return 4;
if (compare > 0x00010000_u32) return 3;
}

if constexpr (size >= sizeof(uint16_t))
{
if (compare > 0x00ff_u16) return 2;
if (compare > 0x0100_u16) return 2;
}

return (compare > 0x00_u8) ? 1 : 0;
return (compare > 0x01_u8) ? 1 : 0;
}

// Called by bc::byte_width.
Expand Down
4 changes: 2 additions & 2 deletions test/chain/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ BOOST_AUTO_TEST_CASE(block__merkle_branch__medium_power_of_two__expected)
BOOST_AUTO_TEST_CASE(block__merkle_branch__power_of_two_minus_one__expected)
{
constexpr auto leaf = 1023u;
constexpr auto size = sub1(ceilinged_log2(add1(leaf)));
constexpr auto size = ceilinged_log2(add1(leaf));
const auto branch = block::merkle_branch(leaf, add1(leaf));
BOOST_REQUIRE_EQUAL(branch.size(), size);
BOOST_REQUIRE_EQUAL(branch.front().sibling, 1022u);
Expand All @@ -792,7 +792,7 @@ BOOST_AUTO_TEST_CASE(block__merkle_branch__power_of_two_minus_one__expected)
BOOST_AUTO_TEST_CASE(block__merkle_branch__odd_large_leaf_with_duplication__expected)
{
constexpr auto leaf = 2047u;
constexpr auto size = sub1(ceilinged_log2(add1(leaf)));
constexpr auto size = ceilinged_log2(add1(leaf));
const auto branch = block::merkle_branch(leaf, add1(leaf));
BOOST_REQUIRE_EQUAL(branch.size(), size);
BOOST_REQUIRE_EQUAL(branch.front().sibling, 2046u);
Expand Down
15 changes: 6 additions & 9 deletions test/chain/compact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,14 +261,11 @@ static_assert(compact::expand(compact::compress(uint256_t(42))) == uint256_t(42)
// (exponent > 32 && mantissa > 0x00ffff) // strict (33 with 1|2)
//)

// > 0x0000 <= 0x00ff (overflow if exponent > 31 and ceilinged_log256(mantissa) > 1)
static_assert(ceilinged_log256(0x00000001ul) == 1);
static_assert(ceilinged_log256(0x000000fful) == 1);
// > 0x0000 <= 0x00ff (overflow if exponent > 31 and byte_width(mantissa) > 1)
static_assert(byte_width(0x000000fful) == 1);

// > 0x00ff <= 0x0000ffff (eoverflow if exponent > 30 and ceilinged_log256(mantissa) > 2)
static_assert(ceilinged_log256(0x00000100ul) == 2);
static_assert(ceilinged_log256(0x0000fffful) == 2);
// > 0x00ff <= 0x0000ffff (overflow if exponent > 30 and byte_width(mantissa) > 2)
static_assert(byte_width(0x0000fffful) == 2);

// > 0x0000ffff (<= 0x007ffffful) (overflow if exponent > 29 and ceilinged_log256(mantissa) > 3)
static_assert(ceilinged_log256(0x00010000ul) == 3);
static_assert(ceilinged_log256(0x007ffffful) == 3);
// > 0x0000ffff (<= 0x007ffffful) (overflow if exponent > 29 and byte_width(mantissa) > 3)
static_assert(byte_width(0x007ffffful) == 3);
2 changes: 2 additions & 0 deletions test/math/bytes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

BOOST_AUTO_TEST_SUITE(bytes_tests)

constexpr auto foo = byte_width(1_u8);

// byte_width (unsigned/positive)
static_assert(byte_width(0_u8) == 0);
static_assert(byte_width(1_u8) == 1);
Expand Down
Loading
Loading