Skip to content

Commit d0ba82c

Browse files
committed
Two passes for 128 bit types
1 parent 99afe94 commit d0ba82c

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

include/boost/decimal/detail/remove_trailing_zeros.hpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ constexpr auto remove_trailing_zeros(std::uint64_t n) noexcept -> remove_trailin
9595
return {n, s};
9696
}
9797

98-
constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zeros_return<uint128>
98+
namespace impl {
99+
100+
constexpr auto remove_trailing_zeros_impl(uint128 n) noexcept -> remove_trailing_zeros_return<uint128>
99101
{
100102
std::size_t s {};
101103

@@ -104,11 +106,6 @@ constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zero
104106
s = s * 2U + static_cast<std::size_t>(b);
105107
n = b ? r : n;
106108

107-
r = rotr<128>(n * uint128 {UINT64_C(0x3275305C1066), UINT64_C(0xE4A4D1417CD9A041)}, 16);
108-
b = r < uint128 {UINT64_C(0x734), UINT64_C(0xACA5F6226F0ADA62)};
109-
s = s * 2U + static_cast<std::size_t>(b);
110-
n = b ? r : n;
111-
112109
r = rotr<128>(n * uint128 {UINT64_C(0x6B7213EE9F5A78), UINT64_C(0xC767074B22E90E21)}, 8);
113110
b = r < uint128 {UINT64_C(0x2AF31DC461), UINT64_C(0x1873BF3F70834ACE)};
114111
s = s * 2U + static_cast<std::size_t>(b);
@@ -132,6 +129,21 @@ constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zero
132129
return {n, s};
133130
}
134131

132+
} // namespace impl
133+
134+
constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zeros_return<uint128>
135+
{
136+
const auto temp {impl::remove_trailing_zeros_impl(n)};
137+
if (BOOST_DECIMAL_LIKELY(temp.number_of_removed_zeros < 31))
138+
{
139+
return temp;
140+
}
141+
142+
// Since 32 is not a valid value of t we need to keep removing zeros
143+
const auto round2 {remove_trailing_zeros(static_cast<std::uint32_t>(temp.trimmed_number))};
144+
return {static_cast<uint128>(round2.trimmed_number), round2.number_of_removed_zeros + 31};
145+
}
146+
135147
} // namespace detail
136148
} // namespace decimal
137149
} // namespace boost

0 commit comments

Comments
 (0)