Skip to content

Commit

Permalink
Merge pull request #271 from chfast/bitwise_op_simplify
Browse files Browse the repository at this point in the history
Simplify conversions around bitwise operators
  • Loading branch information
chfast authored May 24, 2022
2 parents ea6a54f + 43de973 commit 94a1f70
Showing 1 changed file with 41 additions and 101 deletions.
142 changes: 41 additions & 101 deletions include/intx/intx.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,8 @@ inline constexpr uint128 operator~(uint128 x) noexcept

inline constexpr uint128 operator|(uint128 x, uint128 y) noexcept
{
// Clang7: perfect.
// GCC8: stupidly uses a vector instruction in all bitwise operators.
// Clang: perfect.
// GCC 8-12: stupidly uses a vector instruction in all bitwise operators.
return {x[0] | y[0], x[1] | y[1]};
}

Expand Down Expand Up @@ -1120,6 +1120,45 @@ struct uint
inline constexpr uint& operator/=(const uint& y) noexcept { return *this = *this / y; }

inline constexpr uint& operator%=(const uint& y) noexcept { return *this = *this % y; }


inline constexpr uint operator~() const noexcept
{
uint z;
for (size_t i = 0; i < num_words; ++i)
z[i] = ~words_[i];
return z;
}

friend inline constexpr uint operator|(const uint& x, const uint& y) noexcept
{
uint z;
for (size_t i = 0; i < num_words; ++i)
z[i] = x[i] | y[i];
return z;
}

inline constexpr uint& operator|=(const uint& y) noexcept { return *this = *this | y; }

friend inline constexpr uint operator&(const uint& x, const uint& y) noexcept
{
uint z;
for (size_t i = 0; i < num_words; ++i)
z[i] = x[i] & y[i];
return z;
}

inline constexpr uint& operator&=(const uint& y) noexcept { return *this = *this & y; }

friend inline constexpr uint operator^(const uint& x, const uint& y) noexcept
{
uint z;
for (size_t i = 0; i < num_words; ++i)
z[i] = x[i] ^ y[i];
return z;
}

inline constexpr uint& operator^=(const uint& y) noexcept { return *this = *this ^ y; }
};

using uint192 = uint<192>;
Expand Down Expand Up @@ -1282,42 +1321,6 @@ inline constexpr bool slt(const uint<N>& x, const uint<N>& y) noexcept
return ((x_neg ^ y_neg) != 0) ? x_neg : x < y;
}

template <unsigned N>
inline constexpr uint<N> operator|(const uint<N>& x, const uint<N>& y) noexcept
{
uint<N> z;
for (size_t i = 0; i < uint<N>::num_words; ++i)
z[i] = x[i] | y[i];
return z;
}

template <unsigned N>
inline constexpr uint<N> operator&(const uint<N>& x, const uint<N>& y) noexcept
{
uint<N> z;
for (size_t i = 0; i < uint<N>::num_words; ++i)
z[i] = x[i] & y[i];
return z;
}

template <unsigned N>
inline constexpr uint<N> operator^(const uint<N>& x, const uint<N>& y) noexcept
{
uint<N> z;
for (size_t i = 0; i < uint<N>::num_words; ++i)
z[i] = x[i] ^ y[i];
return z;
}

template <unsigned N>
inline constexpr uint<N> operator~(const uint<N>& x) noexcept
{
uint<N> z;
for (size_t i = 0; i < uint<N>::num_words; ++i)
z[i] = ~x[i];
return z;
}


inline constexpr uint256 operator<<(const uint256& x, uint64_t shift) noexcept
{
Expand Down Expand Up @@ -1842,69 +1845,6 @@ inline constexpr uint<N> bswap(const uint<N>& x) noexcept

// Support for type conversions for binary operators.

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator|(const uint<N>& x, const T& y) noexcept
{
return x | uint<N>(y);
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator|(const T& x, const uint<N>& y) noexcept
{
return uint<N>(x) | y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator&(const uint<N>& x, const T& y) noexcept
{
return x & uint<N>(y);
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator&(const T& x, const uint<N>& y) noexcept
{
return uint<N>(x) & y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator^(const uint<N>& x, const T& y) noexcept
{
return x ^ uint<N>(y);
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N> operator^(const T& x, const uint<N>& y) noexcept
{
return uint<N>(x) ^ y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator|=(uint<N>& x, const T& y) noexcept
{
return x = x | y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator&=(uint<N>& x, const T& y) noexcept
{
return x = x & y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator^=(uint<N>& x, const T& y) noexcept
{
return x = x ^ y;
}

template <unsigned N, typename T,
typename = typename std::enable_if<std::is_convertible<T, uint<N>>::value>::type>
inline constexpr uint<N>& operator<<=(uint<N>& x, const T& y) noexcept
Expand Down

0 comments on commit 94a1f70

Please sign in to comment.