Skip to content

Commit a0d1401

Browse files
committed
Reduce branches in decoding
1 parent acd3dc6 commit a0d1401

File tree

3 files changed

+49
-59
lines changed

3 files changed

+49
-59
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -627,17 +627,19 @@ constexpr auto decimal128::unbiased_exponent() const noexcept -> std::uint64_t
627627

628628
const auto exp_comb_bits {(bits_.high & detail::d128_comb_11_mask.high)};
629629

630-
if (exp_comb_bits == detail::d128_comb_11_mask.high)
631-
{
632-
expval = (bits_.high & detail::d128_comb_11_mask.high) >> (high_word_significand_bits + 1);
633-
}
634-
else if (exp_comb_bits == detail::d128_comb_10_mask.high)
635-
{
636-
expval = UINT64_C(0b10000000000000);
637-
}
638-
else if (exp_comb_bits == detail::d128_comb_01_mask.high)
639-
{
640-
expval = UINT64_C(0b01000000000000);
630+
switch (exp_comb_bits)
631+
{
632+
case detail::d128_comb_11_mask.high:
633+
expval = (bits_.high & detail::d128_comb_11_mask.high) >> (high_word_significand_bits + 1);
634+
break;
635+
case detail::d128_comb_10_mask.high:
636+
expval = UINT64_C(0b10000000000000);
637+
break;
638+
case detail::d128_comb_01_mask.high:
639+
expval = UINT64_C(0b01000000000000);
640+
break;
641+
default:
642+
BOOST_DECIMAL_UNREACHABLE;
641643
}
642644

643645
expval |= (bits_.high & detail::d128_exponent_mask.high) >> high_word_significand_bits;
@@ -657,14 +659,9 @@ constexpr auto decimal128::full_significand() const noexcept -> detail::uint128
657659
if ((bits_.high & detail::d128_comb_11_mask.high) == detail::d128_comb_11_mask.high)
658660
{
659661
// Only need the one bit of T because the other 3 are implied 0s
660-
if ((bits_.high & detail::d128_comb_11_significand_bits.high) == detail::d128_comb_11_significand_bits.high)
661-
{
662-
significand = detail::uint128{0b10010000000000000000000000000000000000000000000000,0};
663-
}
664-
else
665-
{
666-
significand = detail::uint128{0b10000000000000000000000000000000000000000000000000,0};
667-
}
662+
significand = (bits_.high & detail::d128_comb_11_significand_bits.high) == detail::d128_comb_11_significand_bits.high ?
663+
detail::uint128{0b10010000000000000000000000000000000000000000000000,0} :
664+
detail::uint128{0b10000000000000000000000000000000000000000000000000,0};
668665
}
669666
else
670667
{

include/boost/decimal/decimal32.hpp

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,18 +1348,20 @@ constexpr auto decimal32::unbiased_exponent() const noexcept -> std::uint32_t
13481348

13491349
const auto exp_comb_bits {(bits_ & detail::d32_comb_11_mask)};
13501350

1351-
if (exp_comb_bits == detail::d32_comb_11_mask)
1352-
{
1353-
// bits 2 and 3 are the exp part of the combination field
1354-
expval = (bits_ & detail::d32_comb_11_exp_bits) >> (detail::d32_significand_bits + 1);
1355-
}
1356-
else if (exp_comb_bits == detail::d32_comb_10_mask)
1357-
{
1358-
expval = UINT32_C(0b10000000);
1359-
}
1360-
else if (exp_comb_bits == detail::d32_comb_01_mask)
1361-
{
1362-
expval = UINT32_C(0b01000000);
1351+
switch (exp_comb_bits)
1352+
{
1353+
case detail::d32_comb_11_mask:
1354+
// bits 2 and 3 are the exp part of the combination field
1355+
expval = (bits_ & detail::d32_comb_11_exp_bits) >> (detail::d32_significand_bits + 1);
1356+
break;
1357+
case detail::d32_comb_10_mask:
1358+
expval = UINT32_C(0b10000000);
1359+
break;
1360+
case detail::d32_comb_01_mask:
1361+
expval = UINT32_C(0b01000000);
1362+
break;
1363+
default:
1364+
BOOST_DECIMAL_UNREACHABLE;
13631365
}
13641366

13651367
expval |= (bits_ & detail::d32_exponent_mask) >> detail::d32_significand_bits;
@@ -1386,14 +1388,9 @@ constexpr auto decimal32::full_significand() const noexcept -> std::uint32_t
13861388
if ((bits_ & detail::d32_comb_11_mask) == detail::d32_comb_11_mask)
13871389
{
13881390
// Only need the one bit of T because the other 3 are implied
1389-
if ((bits_ & detail::d32_comb_11_significand_bits) == detail::d32_comb_11_significand_bits)
1390-
{
1391-
significand = UINT32_C(0b1001'0000000000'0000000000);
1392-
}
1393-
else
1394-
{
1395-
significand = UINT32_C(0b1000'0000000000'0000000000);
1396-
}
1391+
significand = (bits_ & detail::d32_comb_11_significand_bits) == detail::d32_comb_11_significand_bits ?
1392+
UINT32_C(0b1001'0000000000'0000000000) :
1393+
UINT32_C(0b1000'0000000000'0000000000);
13971394
}
13981395
else
13991396
{

include/boost/decimal/decimal64.hpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -991,18 +991,19 @@ constexpr auto decimal64::unbiased_exponent() const noexcept -> std::uint64_t
991991

992992
const auto exp_comb_bits {(bits_ & detail::d64_comb_11_mask)};
993993

994-
if (exp_comb_bits == detail::d64_comb_11_mask)
995-
{
996-
// bits 2 and 3 are the exp part of the combination field
997-
expval = (bits_ & detail::d64_comb_11_exp_bits) >> (detail::d64_significand_bits + 1);
998-
}
999-
else if (exp_comb_bits == detail::d64_comb_10_mask)
1000-
{
1001-
expval = UINT64_C(0b1000000000);
1002-
}
1003-
else if (exp_comb_bits == detail::d64_comb_01_mask)
1004-
{
1005-
expval = UINT64_C(0b0100000000);
994+
switch (exp_comb_bits)
995+
{
996+
case detail::d64_comb_11_mask:
997+
expval = (bits_ & detail::d64_comb_11_exp_bits) >> (detail::d64_significand_bits + 1);
998+
break;
999+
case detail::d64_comb_10_mask:
1000+
expval = UINT64_C(0b1000000000);
1001+
break;
1002+
case detail::d64_comb_01_mask:
1003+
expval = UINT64_C(0b0100000000);
1004+
break;
1005+
default:
1006+
BOOST_DECIMAL_UNREACHABLE;
10061007
}
10071008

10081009
expval |= (bits_ & detail::d64_exponent_mask) >> detail::d64_significand_bits;
@@ -1022,14 +1023,9 @@ constexpr auto decimal64::full_significand() const noexcept -> std::uint64_t
10221023
if ((bits_ & detail::d64_comb_11_mask) == detail::d64_comb_11_mask)
10231024
{
10241025
// Only need the one bit of T because the other 3 are implied
1025-
if ((bits_ & detail::d64_comb_11_significand_bits) == detail::d64_comb_11_significand_bits)
1026-
{
1027-
significand = UINT64_C(0b1001'0000000000'0000000000'0000000000'0000000000'0000000000);
1028-
}
1029-
else
1030-
{
1031-
significand = UINT64_C(0b1000'0000000000'0000000000'0000000000'0000000000'0000000000);
1032-
}
1026+
significand = (bits_ & detail::d64_comb_11_significand_bits) == detail::d64_comb_11_significand_bits ?
1027+
UINT64_C(0b1001'0000000000'0000000000'0000000000'0000000000'0000000000) :
1028+
UINT64_C(0b1000'0000000000'0000000000'0000000000'0000000000'0000000000);
10331029
}
10341030
else
10351031
{

0 commit comments

Comments
 (0)