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

Rework specialization for uint256 #321

Merged
merged 1 commit into from
Sep 2, 2024
Merged
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
178 changes: 86 additions & 92 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -984,22 +984,22 @@ struct uint

using uint256 = uint<256>;

inline constexpr bool operator<(const uint256& x, const uint256& y) noexcept
{
auto xp = uint128{x[2], x[3]};
auto yp = uint128{y[2], y[3]};
if (xp == yp)
{
xp = uint128{x[0], x[1]};
yp = uint128{y[0], y[1]};
}
return xp < yp;
}

template <unsigned N>
inline constexpr bool operator<(const uint<N>& x, const uint<N>& y) noexcept
{
return subc(x, y).carry;
if constexpr (N == 256)
{
auto xp = uint128{x[2], x[3]};
auto yp = uint128{y[2], y[3]};
if (xp == yp)
{
xp = uint128{x[0], x[1]};
yp = uint128{y[0], y[1]};
}
return xp < yp;
}
else
return subc(x, y).carry;
}

template <unsigned N, typename T>
Expand Down Expand Up @@ -1092,109 +1092,103 @@ inline constexpr bool slt(const uint<N>& x, const uint<N>& y) noexcept
return ((x_neg ^ y_neg) != 0) ? x_neg : x < y;
}


inline constexpr uint256 operator<<(const uint256& x, uint64_t shift) noexcept
{
if (INTX_UNLIKELY(shift >= uint256::num_bits))
return 0;

constexpr auto num_bits = uint256::num_bits;
constexpr auto half_bits = num_bits / 2;

const auto xlo = uint128{x[0], x[1]};

if (shift < half_bits)
{
const auto lo = xlo << shift;

const auto xhi = uint128{x[2], x[3]};

// Find the part moved from lo to hi.
// The shift right here can be invalid:
// for shift == 0 => rshift == half_bits.
// Split it into 2 valid shifts by (rshift - 1) and 1.
const auto rshift = half_bits - shift;
const auto lo_overflow = (xlo >> (rshift - 1)) >> 1;
const auto hi = (xhi << shift) | lo_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
}

const auto hi = xlo << (shift - half_bits);
return {0, 0, hi[0], hi[1]};
}

template <unsigned N>
inline constexpr uint<N> operator<<(const uint<N>& x, uint64_t shift) noexcept
{
if (INTX_UNLIKELY(shift >= uint<N>::num_bits))
if (shift >= uint<N>::num_bits) [[unlikely]]
return 0;

constexpr auto word_bits = sizeof(uint64_t) * 8;

const auto s = shift % word_bits;
const auto skip = static_cast<size_t>(shift / word_bits);

uint<N> r;
uint64_t carry = 0;
for (size_t i = 0; i < (uint<N>::num_words - skip); ++i)
if constexpr (N == 256)
{
r[i + skip] = (x[i] << s) | carry;
carry = (x[i] >> (word_bits - s - 1)) >> 1;
}
return r;
}
constexpr auto half_bits = uint<N>::num_bits / 2;

const auto xlo = uint128{x[0], x[1]};

inline constexpr uint256 operator>>(const uint256& x, uint64_t shift) noexcept
{
if (INTX_UNLIKELY(shift >= uint256::num_bits))
return 0;

constexpr auto num_bits = uint256::num_bits;
constexpr auto half_bits = num_bits / 2;

const auto xhi = uint128{x[2], x[3]};
if (shift < half_bits)
{
const auto lo = xlo << shift;

const auto xhi = uint128{x[2], x[3]};

// Find the part moved from lo to hi.
// The shift right here can be invalid:
// for shift == 0 => rshift == half_bits.
// Split it into 2 valid shifts by (rshift - 1) and 1.
const auto rshift = half_bits - shift;
const auto lo_overflow = (xlo >> (rshift - 1)) >> 1;
const auto hi = (xhi << shift) | lo_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
}

if (shift < half_bits)
const auto hi = xlo << (shift - half_bits);
return {0, 0, hi[0], hi[1]};
}
else
{
const auto hi = xhi >> shift;
constexpr auto word_bits = sizeof(uint64_t) * 8;

const auto xlo = uint128{x[0], x[1]};
const auto s = shift % word_bits;
const auto skip = static_cast<size_t>(shift / word_bits);

// Find the part moved from hi to lo.
// The shift left here can be invalid:
// for shift == 0 => lshift == half_bits.
// Split it into 2 valid shifts by (lshift - 1) and 1.
const auto lshift = half_bits - shift;
const auto hi_overflow = (xhi << (lshift - 1)) << 1;
const auto lo = (xlo >> shift) | hi_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
uint<N> r;
uint64_t carry = 0;
for (size_t i = 0; i < (uint<N>::num_words - skip); ++i)
{
r[i + skip] = (x[i] << s) | carry;
carry = (x[i] >> (word_bits - s - 1)) >> 1;
}
return r;
}

const auto lo = xhi >> (shift - half_bits);
return {lo[0], lo[1], 0, 0};
}

template <unsigned N>
inline constexpr uint<N> operator>>(const uint<N>& x, uint64_t shift) noexcept
{
if (INTX_UNLIKELY(shift >= uint<N>::num_bits))
if (shift >= uint<N>::num_bits) [[unlikely]]
return 0;

constexpr auto num_words = uint<N>::num_words;
constexpr auto word_bits = sizeof(uint64_t) * 8;
if constexpr (N == 256)
{
constexpr auto half_bits = uint<N>::num_bits / 2;

const auto s = shift % word_bits;
const auto skip = static_cast<size_t>(shift / word_bits);
const auto xhi = uint128{x[2], x[3]};

uint<N> r;
uint64_t carry = 0;
for (size_t i = 0; i < (num_words - skip); ++i)
if (shift < half_bits)
{
const auto hi = xhi >> shift;

const auto xlo = uint128{x[0], x[1]};

// Find the part moved from hi to lo.
// The shift left here can be invalid:
// for shift == 0 => lshift == half_bits.
// Split it into 2 valid shifts by (lshift - 1) and 1.
const auto lshift = half_bits - shift;
const auto hi_overflow = (xhi << (lshift - 1)) << 1;
const auto lo = (xlo >> shift) | hi_overflow;
return {lo[0], lo[1], hi[0], hi[1]};
}

const auto lo = xhi >> (shift - half_bits);
return {lo[0], lo[1], 0, 0};
}
else
{
r[num_words - 1 - i - skip] = (x[num_words - 1 - i] >> s) | carry;
carry = (x[num_words - 1 - i] << (word_bits - s - 1)) << 1;
constexpr auto num_words = uint<N>::num_words;
constexpr auto word_bits = sizeof(uint64_t) * 8;

const auto s = shift % word_bits;
const auto skip = static_cast<size_t>(shift / word_bits);

uint<N> r;
uint64_t carry = 0;
for (size_t i = 0; i < (num_words - skip); ++i)
{
r[num_words - 1 - i - skip] = (x[num_words - 1 - i] >> s) | carry;
carry = (x[num_words - 1 - i] << (word_bits - s - 1)) << 1;
}
return r;
}
return r;
}

template <unsigned N>
Expand Down