Skip to content

Commit 38050ca

Browse files
authored
Merge pull request #1789 from evoskuil/master
Fix ob1 errors in ceilinged log.
2 parents 5c0b0eb + 4f964e5 commit 38050ca

7 files changed

Lines changed: 175 additions & 179 deletions

File tree

include/bitcoin/system/impl/math/bits.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ template <typename Value, if_unsigned_integer<Value>>
3939
constexpr size_t bit_width(Value value) NOEXCEPT
4040
{
4141
// zero-based position of msb.
42-
return ceilinged_log2(value);
42+
return is_zero(value) ? zero : add1(floored_log2(value));
4343
}
4444

4545
// Called by machine::number (for to_unnegated).

include/bitcoin/system/impl/math/bytes.ipp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ constexpr size_t byte_width(Integral value) NOEXCEPT
3838
// (zero-based position of msb) + 7 / 8.
3939
// (bit_width(value) + 7) / 8
4040
// (ceilinged_log2(value) + 7) / 8
41-
return ceilinged_log256(value);
41+
return is_zero(value) ? zero : add1(floored_log256(value));
4242
}
4343

4444
// Called by machine::number (for little-endian chunk sizing).

include/bitcoin/system/impl/math/logarithm.ipp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ constexpr Exponent ceilinged_log_(Base base, Value value) NOEXCEPT
5353
const auto factor = possible_narrow_and_sign_cast<Value>(base);
5454

5555
Exponent exponent = 0;
56-
while (value > 0) { ++exponent; value /= factor; }
56+
while (value > 1) { ++exponent; value /= factor; }
5757
return exponent;
5858
}
5959
}
@@ -125,7 +125,7 @@ constexpr Exponent ceilinged_log2(Value value) NOEXCEPT
125125

126126
// base2 integral optimization over normal form.
127127
return possible_narrow_and_sign_cast<Exponent>(
128-
std::bit_width(to_unsigned(value)));
128+
std::bit_width(sub1(to_unsigned(value))));
129129
}
130130

131131
// Called by bc::bit_width.
@@ -134,12 +134,12 @@ template <typename Exponent, typename Value,
134134
if_non_integral_integer<Value>>
135135
constexpr Exponent ceilinged_log2(Value value) NOEXCEPT
136136
{
137-
if (is_log_overflow<2>(value))
137+
if (is_log_overflow<2>(value) || is_one(value))
138138
return 0;
139139

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

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

160160
if constexpr (size == sizeof(uint64_t))
161161
{
162-
if (compare > 0x00ffffffffffffff_u64) return 8;
163-
if (compare > 0x0000ffffffffffff_u64) return 7;
164-
if (compare > 0x000000ffffffffff_u64) return 6;
165-
if (compare > 0x00000000ffffffff_u64) return 5;
162+
if (compare > 0x0100000000000000_u64) return 8;
163+
if (compare > 0x0001000000000000_u64) return 7;
164+
if (compare > 0x0000010000000000_u64) return 6;
165+
if (compare > 0x0000000100000000_u64) return 5;
166166
}
167167

168168
if constexpr (size >= sizeof(uint32_t))
169169
{
170-
if (compare > 0x00ffffff_u32) return 4;
171-
if (compare > 0x0000ffff_u32) return 3;
170+
if (compare > 0x01000000_u32) return 4;
171+
if (compare > 0x00010000_u32) return 3;
172172
}
173173

174174
if constexpr (size >= sizeof(uint16_t))
175175
{
176-
if (compare > 0x00ff_u16) return 2;
176+
if (compare > 0x0100_u16) return 2;
177177
}
178178

179-
return (compare > 0x00_u8) ? 1 : 0;
179+
return (compare > 0x01_u8) ? 1 : 0;
180180
}
181181

182182
// Called by bc::byte_width.

test/chain/block.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ BOOST_AUTO_TEST_CASE(block__merkle_branch__medium_power_of_two__expected)
780780
BOOST_AUTO_TEST_CASE(block__merkle_branch__power_of_two_minus_one__expected)
781781
{
782782
constexpr auto leaf = 1023u;
783-
constexpr auto size = sub1(ceilinged_log2(add1(leaf)));
783+
constexpr auto size = ceilinged_log2(add1(leaf));
784784
const auto branch = block::merkle_branch(leaf, add1(leaf));
785785
BOOST_REQUIRE_EQUAL(branch.size(), size);
786786
BOOST_REQUIRE_EQUAL(branch.front().sibling, 1022u);
@@ -792,7 +792,7 @@ BOOST_AUTO_TEST_CASE(block__merkle_branch__power_of_two_minus_one__expected)
792792
BOOST_AUTO_TEST_CASE(block__merkle_branch__odd_large_leaf_with_duplication__expected)
793793
{
794794
constexpr auto leaf = 2047u;
795-
constexpr auto size = sub1(ceilinged_log2(add1(leaf)));
795+
constexpr auto size = ceilinged_log2(add1(leaf));
796796
const auto branch = block::merkle_branch(leaf, add1(leaf));
797797
BOOST_REQUIRE_EQUAL(branch.size(), size);
798798
BOOST_REQUIRE_EQUAL(branch.front().sibling, 2046u);

test/chain/compact.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,11 @@ static_assert(compact::expand(compact::compress(uint256_t(42))) == uint256_t(42)
261261
// (exponent > 32 && mantissa > 0x00ffff) // strict (33 with 1|2)
262262
//)
263263

264-
// > 0x0000 <= 0x00ff (overflow if exponent > 31 and ceilinged_log256(mantissa) > 1)
265-
static_assert(ceilinged_log256(0x00000001ul) == 1);
266-
static_assert(ceilinged_log256(0x000000fful) == 1);
264+
// > 0x0000 <= 0x00ff (overflow if exponent > 31 and byte_width(mantissa) > 1)
265+
static_assert(byte_width(0x000000fful) == 1);
267266

268-
// > 0x00ff <= 0x0000ffff (eoverflow if exponent > 30 and ceilinged_log256(mantissa) > 2)
269-
static_assert(ceilinged_log256(0x00000100ul) == 2);
270-
static_assert(ceilinged_log256(0x0000fffful) == 2);
267+
// > 0x00ff <= 0x0000ffff (overflow if exponent > 30 and byte_width(mantissa) > 2)
268+
static_assert(byte_width(0x0000fffful) == 2);
271269

272-
// > 0x0000ffff (<= 0x007ffffful) (overflow if exponent > 29 and ceilinged_log256(mantissa) > 3)
273-
static_assert(ceilinged_log256(0x00010000ul) == 3);
274-
static_assert(ceilinged_log256(0x007ffffful) == 3);
270+
// > 0x0000ffff (<= 0x007ffffful) (overflow if exponent > 29 and byte_width(mantissa) > 3)
271+
static_assert(byte_width(0x007ffffful) == 3);

test/math/bytes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020

2121
BOOST_AUTO_TEST_SUITE(bytes_tests)
2222

23+
constexpr auto foo = byte_width(1_u8);
24+
2325
// byte_width (unsigned/positive)
2426
static_assert(byte_width(0_u8) == 0);
2527
static_assert(byte_width(1_u8) == 1);

0 commit comments

Comments
 (0)