Skip to content

Commit 99afe94

Browse files
committed
Do the 16 step twice since 32 is not valid
1 parent 8be21d1 commit 99afe94

File tree

2 files changed

+17
-44
lines changed

2 files changed

+17
-44
lines changed

include/boost/decimal/detail/remove_trailing_zeros.hpp

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

98-
/*
99-
constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zeros_return<uint128>
100-
{
101-
if (n.high == UINT64_C(0))
102-
{
103-
const auto temp {remove_trailing_zeros(n.low)};
104-
return {static_cast<uint128>(temp.trimmed_number), temp.number_of_removed_zeros};
105-
}
106-
107-
std::size_t s = 0;
108-
constexpr detail::uint128 m {UINT64_C(0xCCCCCCCCCCCCCCCC), UINT64_C(0xCCCCCCCCCCCCCCCD)};
109-
constexpr detail::uint128 threshold_value {UINT64_C(0x1999999999999999), UINT64_C(0x999999999999999A)};
110-
111-
while (true)
112-
{
113-
const auto r = rotr<128>(n * m, 1);
114-
115-
if (r < threshold_value)
116-
{
117-
n = r;
118-
s += 1;
119-
}
120-
else
121-
{
122-
break;
123-
}
124-
}
125-
126-
return {n, s};
127-
}
128-
*/
129-
13098
constexpr auto remove_trailing_zeros(uint128 n) noexcept -> remove_trailing_zeros_return<uint128>
13199
{
132100
std::size_t s {};
133101

134-
auto r = rotr<128>(n * uint128{UINT64_C(0x3275305C1066), UINT64_C(0xE4A4D1417CD9A041)}, 16);
135-
auto b = r < uint128{UINT64_C(0x734), UINT64_C(0xACA5F6226F0ADA62)};
102+
auto r = rotr<128>(n * uint128 {UINT64_C(0x3275305C1066), UINT64_C(0xE4A4D1417CD9A041)}, 16);
103+
auto b = r < uint128 {UINT64_C(0x734), UINT64_C(0xACA5F6226F0ADA62)};
104+
s = s * 2U + static_cast<std::size_t>(b);
105+
n = b ? r : n;
106+
107+
r = rotr<128>(n * uint128 {UINT64_C(0x3275305C1066), UINT64_C(0xE4A4D1417CD9A041)}, 16);
108+
b = r < uint128 {UINT64_C(0x734), UINT64_C(0xACA5F6226F0ADA62)};
136109
s = s * 2U + static_cast<std::size_t>(b);
137110
n = b ? r : n;
138111

139-
r = rotr<128>(n * uint128{UINT64_C(0x6B7213EE9F5A78), UINT64_C(0xC767074B22E90E21)}, 8);
140-
b = r < uint128{UINT64_C(0x2AF31DC461), UINT64_C(0x1873BF3F70834ACE)};
112+
r = rotr<128>(n * uint128 {UINT64_C(0x6B7213EE9F5A78), UINT64_C(0xC767074B22E90E21)}, 8);
113+
b = r < uint128 {UINT64_C(0x2AF31DC461), UINT64_C(0x1873BF3F70834ACE)};
141114
s = s * 2U + static_cast<std::size_t>(b);
142115
n = b ? r : n;
143116

144-
r = rotr<128>(n * uint128{UINT64_C(0x95182A9930BE0DE), UINT64_C(0xD288CE703AFB7E91)}, 4);
145-
b = r < uint128{UINT64_C(0x68DB8BAC710CB), UINT64_C(0x295E9E1B089A0276)};
117+
r = rotr<128>(n * uint128 {UINT64_C(0x95182A9930BE0DE), UINT64_C(0xD288CE703AFB7E91)}, 4);
118+
b = r < uint128 {UINT64_C(0x68DB8BAC710CB), UINT64_C(0x295E9E1B089A0276)};
146119
s = s * 2U + static_cast<std::size_t>(b);
147120
n = b ? r : n;
148121

149-
r = rotr<128>(n * uint128{UINT64_C(0x28F5C28F5C28F5C2), UINT64_C(0x8F5C28F5C28F5C29)}, 2);
150-
b = r < uint128{UINT64_C(0x28F5C28F5C28F5C), UINT64_C(0x28F5C28F5C28F5C3)};
122+
r = rotr<128>(n * uint128 {UINT64_C(0x28F5C28F5C28F5C2), UINT64_C(0x8F5C28F5C28F5C29)}, 2);
123+
b = r < uint128 {UINT64_C(0x28F5C28F5C28F5C), UINT64_C(0x28F5C28F5C28F5C3)};
151124
s = s * 2U + static_cast<std::size_t>(b);
152125
n = b ? r : n;
153126

154-
r = rotr<128>(n * uint128{UINT64_C(0xCCCCCCCCCCCCCCCC), UINT64_C(0xCCCCCCCCCCCCCCCD)}, 1);
155-
b = r < uint128{UINT64_C(0x1999999999999999), UINT64_C(0x999999999999999A)};
127+
r = rotr<128>(n * uint128 {UINT64_C(0xCCCCCCCCCCCCCCCC), UINT64_C(0xCCCCCCCCCCCCCCCD)}, 1);
128+
b = r < uint128 {UINT64_C(0x1999999999999999), UINT64_C(0x999999999999999A)};
156129
s = s * 2U + static_cast<std::size_t>(b);
157130
n = b ? r : n;
158131

tools/granlund-montgomery.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ def extended_euclidean(a, b):
99
def mod_inverse(a, m):
1010
gcd, x, _ = extended_euclidean(a, m)
1111
if gcd != 1:
12-
return None # Modular inverse doesn't exist
12+
return 0 # Modular inverse doesn't exist
1313
else:
1414
return x % m
1515

1616
# Constants
1717
bits = int(128)
18-
t = int(2)
18+
t = int(32)
1919

2020
q = int(10)**t
2121
q0 = int(q / int(2)**t)

0 commit comments

Comments
 (0)