Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change shift amount param from unsigned to uint64_t #202

Merged
merged 2 commits into from
Mar 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions include/intx/int128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ inline constexpr uint128 operator^(uint128 x, uint128 y) noexcept
return {x.hi ^ y.hi, x.lo ^ y.lo};
}

inline constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept
inline constexpr uint128 operator<<(uint128 x, uint64_t shift) noexcept
{
return (shift < 64) ?
// Find the part moved from lo to hi.
Expand All @@ -338,11 +338,11 @@ inline constexpr uint128 operator<<(uint128 x, unsigned shift) noexcept
inline constexpr uint128 operator<<(uint128 x, uint128 shift) noexcept
{
if (shift < 128)
return x << unsigned(shift);
return x << static_cast<uint64_t>(shift);
return 0;
}

inline constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept
inline constexpr uint128 operator>>(uint128 x, uint64_t shift) noexcept
{
return (shift < 64) ?
// Find the part moved from lo to hi.
Expand All @@ -357,7 +357,7 @@ inline constexpr uint128 operator>>(uint128 x, unsigned shift) noexcept
inline constexpr uint128 operator>>(uint128 x, uint128 shift) noexcept
{
if (shift < 128)
return x >> unsigned(shift);
return x >> static_cast<uint64_t>(shift);
return 0;
}

Expand Down Expand Up @@ -452,12 +452,12 @@ inline constexpr uint128& operator^=(uint128& x, uint128 y) noexcept
return x = x ^ y;
}

inline constexpr uint128& operator<<=(uint128& x, unsigned shift) noexcept
inline constexpr uint128& operator<<=(uint128& x, uint64_t shift) noexcept
{
return x = x << shift;
}

inline constexpr uint128& operator>>=(uint128& x, unsigned shift) noexcept
inline constexpr uint128& operator>>=(uint128& x, uint64_t shift) noexcept
{
return x = x >> shift;
}
Expand Down
31 changes: 16 additions & 15 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ inline constexpr uint<N> operator~(const uint<N>& x) noexcept
}

template <unsigned N>
inline constexpr uint<N> operator<<(const uint<N>& x, unsigned shift) noexcept
inline constexpr uint<N> operator<<(const uint<N>& x, uint64_t shift) noexcept
{
constexpr auto num_bits = N;
constexpr auto half_bits = num_bits / 2;
Expand Down Expand Up @@ -301,25 +301,26 @@ inline Target narrow_cast(const Int& x) noexcept
}

template <unsigned N>
inline constexpr uint<N> operator>>(const uint<N>& x, unsigned shift) noexcept
inline constexpr uint<N> operator>>(const uint<N>& x, uint64_t shift) noexcept
{
constexpr auto half_bits = N / 2;
constexpr auto num_bits = N;
constexpr auto half_bits = num_bits / 2;

if (shift < half_bits)
{
auto hi = x.hi >> shift;
const auto hi = x.hi >> shift;

// Find the part moved from hi to lo.
// To avoid invalid shift left,
// split them into 2 valid shifts by (lshift - 1) and 1.
unsigned lshift = half_bits - shift;
auto hi_overflow = (x.hi << (lshift - 1)) << 1;
auto lo_part = x.lo >> shift;
auto lo = lo_part | hi_overflow;
const auto lshift = half_bits - shift;
const auto hi_overflow = (x.hi << (lshift - 1)) << 1;
const auto lo_part = x.lo >> shift;
const auto lo = lo_part | hi_overflow;
return {hi, lo};
}

if (shift < num_bits(x))
if (shift < num_bits)
return {0, x.hi >> (shift - half_bits)};

return 0;
Expand All @@ -331,7 +332,7 @@ template <unsigned N, typename T,
inline constexpr uint<N> operator<<(const uint<N>& x, const T& shift) noexcept
{
if (shift < T{sizeof(x) * 8})
return x << static_cast<unsigned>(shift);
return x << static_cast<uint64_t>(shift);
return 0;
}

Expand All @@ -340,12 +341,12 @@ template <unsigned N, typename T,
inline constexpr uint<N> operator>>(const uint<N>& x, const T& shift) noexcept
{
if (shift < T{sizeof(x) * 8})
return x >> static_cast<unsigned>(shift);
return x >> static_cast<uint64_t>(shift);
return 0;
}

template <unsigned N>
inline constexpr uint<N>& operator>>=(uint<N>& x, unsigned shift) noexcept
inline constexpr uint<N>& operator>>=(uint<N>& x, uint64_t shift) noexcept
{
return x = x >> shift;
}
Expand Down Expand Up @@ -388,15 +389,15 @@ inline const uint8_t* as_bytes(const uint<N>& x) noexcept
/// Implementation of shift left as a loop.
/// This one is slower than the one using "split" strategy.
template <unsigned N>
inline uint<N> shl_loop(const uint<N>& x, unsigned shift)
inline uint<N> shl_loop(const uint<N>& x, uint64_t shift)
{
auto r = uint<N>{};
constexpr unsigned word_bits = sizeof(uint64_t) * 8;
constexpr size_t num_words = sizeof(uint<N>) / sizeof(uint64_t);
auto rw = as_words(r);
auto words = as_words(x);
unsigned s = shift % word_bits;
unsigned skip = shift / word_bits;
const auto s = shift % word_bits;
const auto skip = shift / word_bits;

uint64_t carry = 0;
for (size_t i = 0; i < (num_words - skip); ++i)
Expand Down
33 changes: 22 additions & 11 deletions test/unittests/test_intx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,32 +341,43 @@ TYPED_TEST(uint_test, shift_one_bit)
{
for (unsigned shift = 0; shift < sizeof(TypeParam) * 8; ++shift)
{
auto x = TypeParam{1};
auto y = x << shift;
auto z = y >> shift;
EXPECT_EQ(x, z) << "shift: " << shift;
SCOPED_TRACE(shift);
constexpr auto x = TypeParam{1};

const auto a = x << shift;
const auto b = shl_loop(x, shift);
EXPECT_EQ(b, a);

EXPECT_EQ(x, a >> shift);
}
}

TYPED_TEST(uint_test, shift_loop_one_bit)
TYPED_TEST(uint_test, shift_left_overflow)
{
for (unsigned shift = 0; shift < sizeof(TypeParam) * 8; ++shift)
const auto x = ~TypeParam{};

for (unsigned n = 0; n <= sizeof(TypeParam) * 7; ++n)
{
const auto sh = x >> n;
EXPECT_EQ(x << sh, 0) << "n=" << n;
}

for (unsigned n = 0; n <= sizeof(TypeParam) * 7; ++n)
{
auto x = TypeParam{1};
auto y = shl_loop(x, shift);
auto z = y >> shift;
EXPECT_EQ(x, z) << "shift: " << shift;
const auto sh = TypeParam{sizeof(TypeParam) * 8} << n;
EXPECT_EQ(x << sh, 0) << "n=" << n;
}
}

TYPED_TEST(uint_test, shift_overflow)
{
unsigned sh = sizeof(TypeParam) * 8;
const uint64_t sh = sizeof(TypeParam) * 8;
const auto value = ~TypeParam{};
EXPECT_EQ(value >> sh, 0);
EXPECT_EQ(value >> TypeParam{sh}, 0);
EXPECT_EQ(value << sh, 0);
EXPECT_EQ(value << TypeParam{sh}, 0);
EXPECT_EQ(shl_loop(value, sh), 0);
}

TYPED_TEST(uint_test, not_of_zero)
Expand Down