Skip to content

Commit 3c87a2e

Browse files
authored
Fix log_int implicit conversion
Disable implicit conversion from larger to smaller integer type
1 parent 17f62a6 commit 3c87a2e

1 file changed

Lines changed: 36 additions & 3 deletions

File tree

include/long_int.h

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
//
3131
////////////////////////////////////////////////////////////////////////////////////////////////////
3232
//
33-
// $Id: long_int.h 151 2021-09-07 11:33:21Z ykalmykov $
33+
// $Id: long_int.h 152 2021-09-07 14:14:03Z ykalmykov $
3434
//
3535
////////////////////////////////////////////////////////////////////////////////////////////////////
3636

@@ -63,6 +63,8 @@ class long_int_t : public long_uint_t<native_t, size>
6363
constexpr long_int_t(const long_int_t& that) noexcept = default;
6464
constexpr long_int_t(long_int_t&& that) noexcept = default;
6565
constexpr long_int_t(const long_uint_t<native_t, size>& that) noexcept;
66+
template<uint_t other_size, std::enable_if_t<(other_size < size), int> = 0>
67+
constexpr long_int_t(const long_int_t<native_t, other_size>& that) noexcept;
6668
constexpr long_int_t(native_array_t digits) noexcept;
6769
template<typename type_t, std::enable_if_t<std::is_unsigned_v<type_t>, int> = 0>
6870
constexpr long_int_t(type_t value) noexcept;
@@ -78,6 +80,8 @@ class long_int_t : public long_uint_t<native_t, size>
7880
constexpr long_int_t& operator=(long_int_t&& that) noexcept = default;
7981

8082
constexpr bool sign() const noexcept;
83+
template<uint_t other_size, std::enable_if_t<(other_size < size), int> = 0>
84+
explicit constexpr operator long_int_t<native_t, other_size>() const noexcept;
8185
template<typename type_t, std::enable_if_t<std::is_signed_v<type_t>, int> = 0>
8286
explicit constexpr operator type_t() const noexcept;
8387
constexpr bool operator==(const long_int_t& that) const noexcept;
@@ -146,6 +150,19 @@ constexpr long_int_t<native_t, size>::long_int_t(const long_uint_t<native_t, siz
146150
{
147151
}
148152

153+
template<typename native_t, uint_t size>
154+
template<uint_t other_size, std::enable_if_t<(other_size < size), int>>
155+
constexpr long_int_t<native_t, size>::long_int_t(const long_int_t<native_t, other_size>& that) noexcept
156+
{
157+
constexpr uint_t value_size = std::min(size, other_size);
158+
const native_t extension = static_cast<native_t>(!that.sign() ? 0 : native_t(~0));
159+
160+
for (uint_t n = 0; n < value_size; ++n)
161+
digits[n] = that.digits[n];
162+
for (uint_t n = value_size; n < std::size(digits); ++n)
163+
digits[n] = extension;
164+
}
165+
149166
template<typename native_t, uint_t size>
150167
constexpr long_int_t<native_t, size>::long_int_t(native_array_t digits) noexcept
151168
: long_uint_t<native_t, size>(std::move(digits))
@@ -196,6 +213,21 @@ constexpr bool long_int_t<native_t, size>::sign() const noexcept
196213

197214

198215

216+
////////////////////////////////////////////////////////////////////////////////////////////////////
217+
template<typename native_t, uint_t size>
218+
template<uint_t other_size, std::enable_if_t<(other_size < size), int>>
219+
constexpr long_int_t<native_t, size>::operator long_int_t<native_t, other_size>() const noexcept
220+
{
221+
long_int_t<native_t, other_size> tmp;
222+
223+
for (uint_t n = 0; n < other_size; ++n)
224+
tmp.digits[n] = digits[n];
225+
226+
return tmp;
227+
}
228+
229+
230+
199231
////////////////////////////////////////////////////////////////////////////////////////////////////
200232
template<typename native_t, uint_t size>
201233
template<typename type_t, std::enable_if_t<std::is_signed_v<type_t>, int>>
@@ -390,7 +422,7 @@ constexpr long_int_t<native_t, size>& long_int_t<native_t, size>::operator/=(con
390422
result = -result;
391423

392424
*this = result;
393-
425+
394426
return *this;
395427
}
396428

@@ -521,7 +553,8 @@ constexpr type_t muldiv(const type_t& value, const type_t& multiplier, const typ
521553
using int128_t = long_int_t<uint64_t, 2>;
522554
using int256_t = long_int_t<uint64_t, 4>;
523555

524-
namespace literals {
556+
namespace literals
557+
{
525558

526559
template<char... chars>
527560
constexpr int128_t operator"" _si128() noexcept

0 commit comments

Comments
 (0)