Skip to content

Commit 110c89d

Browse files
committed
Style (normalize power implementations).
1 parent 41bbd82 commit 110c89d

2 files changed

Lines changed: 6 additions & 15 deletions

File tree

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

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ namespace system {
3333

3434
// Called by bc::base85 (for number coding).
3535
// Called by wallet::electrum_v1 (for number coding).
36+
// Overflow is allowed behavior as this models a mathematical operator.
3637
template <typename Value, typename Base, typename Exponent,
3738
if_integer<Value> = true,
3839
if_integer<Base> = true,
@@ -42,19 +43,11 @@ constexpr Value power_(Base base, Exponent exponent) NOEXCEPT
4243
if (is_power_overflow(base, exponent))
4344
return 0;
4445

45-
if (is_zero(exponent))
46-
return 1;
47-
48-
auto value = possible_narrow_and_sign_cast<Value>(base);
49-
50-
// Overflow is allowed behavior as this models a mathematical operator.
51-
BC_PUSH_WARNING(NARROWING_CONVERSION)
52-
BC_PUSH_WARNING(SIZE_NARROWING_CONVERSION)
53-
while (--exponent > 0u) { value *= base; }
54-
BC_POP_WARNING()
55-
BC_POP_WARNING()
46+
Value product{ 1 };
47+
while (exponent-- > 0u)
48+
product *= possible_narrow_and_sign_cast<Value>(base);
5649

57-
return value;
50+
return product;
5851
}
5952

6053
// published

test/math/power.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,21 @@ static_assert(power(3, 0u) == 1u);
3737
static_assert(power(3u, 0u) == 1u);
3838
static_assert(power(3, 10u) == 0xe6a9_size);
3939
static_assert(power(3u, 10u) == 0xe6a9_size);
40-
static_assert(power<uint16_t>(3, 11u) == 0xb3fb_u16);
4140

4241
static_assert(power(-1, 0u) == 1u);
4342
static_assert(power(-1, 1u) == 1_nsize);
4443

4544
static_assert(power(-3, 0u) == 1u);
4645
static_assert(power(-3, 1u) == 3_nsize);
4746
static_assert(power(-3, 10u) == 0xe6a9_size);
48-
static_assert(power<uint16_t>(-3, 11u) == 0x4c05_u16);
4947

5048
// power2
5149
static_assert(power2(0u) == 1u);
5250
static_assert(power2(0u) == 1u);
5351
static_assert(power2(1u) == 2u);
5452
static_assert(power2(1u) == 2u);
5553
static_assert(power2<uint16_t>(15u) == 0b1000'0000'0000'0000_u16);
56-
static_assert(power2<uint16_t>(16u) == 0_u16);
54+
static_assert(power2<uint16_t>(16u) == 0_u16); // overflow
5755

5856
// power<>
5957
static_assert(power<0>(16u) == power(0u, 16u));

0 commit comments

Comments
 (0)