Skip to content

Commit

Permalink
Merge pull request #98 from chfast/pedantic
Browse files Browse the repository at this point in the history
int128: Clear compiler warnings
  • Loading branch information
chfast authored May 23, 2019
2 parents 50a3f4e + e31eadc commit 3359f3d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ include(Hunter/init)
project(intx LANGUAGES CXX)
set(PROJECT_VERSION 0.3.0-dev)

cable_configure_compiler(NO_PEDANTIC NO_CONVERSION_WARNINGS NO_STACK_PROTECTION)
cable_configure_compiler(NO_CONVERSION_WARNINGS NO_STACK_PROTECTION)

if(INTX_TESTING)
enable_testing()
Expand Down
24 changes: 13 additions & 11 deletions include/intx/int128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct uint;
/// The 128-bit unsigned integer.
///
/// This type is defined as a specialization of uint<> to easier integration with full intx package,
/// however, uint128 may be used indepedently.
/// however, uint128 may be used independently.
template <>
struct uint<128>
{
Expand All @@ -36,23 +36,22 @@ struct uint<128>
constexpr uint(uint64_t high, uint64_t low) noexcept : lo{low}, hi{high} {}

template <typename T,
typename = typename std::enable_if<std::is_convertible<T, uint64_t>::value>::type>
constexpr uint(T x) noexcept : lo(x) // NOLINT
{}

template <typename T, typename std::enable_if<std::is_integral<T>::value>::type>
constexpr explicit uint(T x) noexcept : lo(x)
typename = typename std::enable_if_t<std::is_convertible<T, uint64_t>::value>>
constexpr uint(T x) noexcept : lo(static_cast<uint64_t>(x)) // NOLINT
{}

#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
constexpr uint(unsigned __int128 x) noexcept // NOLINT
: lo{uint64_t(x)}, hi{uint64_t(x >> 64)}
{}

constexpr explicit operator unsigned __int128() const noexcept
{
return ((unsigned __int128){hi} << 64) | lo;
return (static_cast<unsigned __int128>(hi) << 64) | lo;
}
#pragma GCC diagnostic pop
#endif

constexpr explicit operator bool() const noexcept { return hi | lo; }
Expand Down Expand Up @@ -287,8 +286,11 @@ constexpr uint128 constexpr_umul(uint64_t x, uint64_t y) noexcept
inline uint128 umul(uint64_t x, uint64_t y) noexcept
{
#if defined(__SIZEOF_INT128__)
const auto p = (unsigned __int128){x} * y;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
const auto p = static_cast<unsigned __int128>(x) * y;
return {uint64_t(p >> 64), uint64_t(p)};
#pragma GCC diagnostic pop
#elif defined(_MSC_VER)
unsigned __int64 hi;
const auto lo = _umul128(x, y, &hi);
Expand Down Expand Up @@ -368,7 +370,7 @@ inline unsigned clz(uint32_t x) noexcept
_BitScanReverse(&most_significant_bit, x);
return 31 ^ (unsigned)most_significant_bit;
#else
return __builtin_clz(x);
return unsigned(__builtin_clz(x));
#endif
}

Expand All @@ -379,7 +381,7 @@ inline unsigned clz(uint64_t x) noexcept
_BitScanReverse64(&most_significant_bit, x);
return 63 ^ (unsigned)most_significant_bit;
#else
return __builtin_clzll(x);
return unsigned(__builtin_clzll(x));
#endif
}

Expand Down
1 change: 1 addition & 0 deletions test/benchmarks/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstdint>
#include <cstring>

#pragma GCC diagnostic ignored "-Wpedantic"

uint64_t udiv_native(uint64_t x, uint64_t y) noexcept
{
Expand Down
13 changes: 8 additions & 5 deletions test/unittests/test_int128.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ TEST(int128, div)
}

#ifdef __SIZEOF_INT128__
#pragma GCC diagnostic ignored "-Wpedantic"
using uint128_ty = unsigned __int128;

TEST(int128, arith_random_args)
{
int c = 1000000;
Expand All @@ -311,11 +314,11 @@ TEST(int128, arith_random_args)
auto q = x / y;
auto r = x % y;

auto expected_s = uint128{(unsigned __int128){x} + (unsigned __int128){y}};
auto expected_d = uint128{(unsigned __int128){x} - (unsigned __int128){y}};
auto expected_p = uint128{(unsigned __int128){x} * (unsigned __int128){y}};
auto expected_q = uint128{(unsigned __int128){x} / (unsigned __int128){y}};
auto expected_r = uint128{(unsigned __int128){x} % (unsigned __int128){y}};
auto expected_s = uint128{uint128_ty{x} + uint128_ty{y}};
auto expected_d = uint128{uint128_ty{x} - uint128_ty{y}};
auto expected_p = uint128{uint128_ty{x} * uint128_ty{y}};
auto expected_q = uint128{uint128_ty{x} / uint128_ty{y}};
auto expected_r = uint128{uint128_ty{x} % uint128_ty{y}};

EXPECT_EQ(s, expected_s) << c;
EXPECT_EQ(d, expected_d) << c;
Expand Down
11 changes: 11 additions & 0 deletions test/unittests/test_intx_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ class uint_api : public testing::Test
using types = testing::Types<uint128, uint256, uint512>;
TYPED_TEST_CASE(uint_api, types);

TYPED_TEST(uint_api, constructor)
{
auto i = int{-1};
auto x = TypeParam{i};
TypeParam y = i;
auto z = TypeParam(i);

EXPECT_EQ(x, y);
EXPECT_EQ(x, z);
}

TYPED_TEST(uint_api, arithmetic)
{
auto a = int{};
Expand Down
4 changes: 2 additions & 2 deletions test/utils/gmp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ inline div_result<Int> udivrem(const Int& x, const Int& y) noexcept
auto p_y = (mp_srcptr)&y;
mpn_tdiv_qr(p_q, p_r, 0, p_x, x_limbs, p_y, y_limbs);
return {q, r};
};
}

template <typename Int>
inline div_result<Int> sdivrem(const Int& x, const Int& y) noexcept
Expand Down Expand Up @@ -98,7 +98,7 @@ inline div_result<Int> sdivrem(const Int& x, const Int& y) noexcept
mpz_clears(x_gmp, y_gmp, q_gmp, r_gmp, NULL);

return {q, r};
};
}

template <typename Int>
inline Int add(const Int& x, const Int& y) noexcept
Expand Down

0 comments on commit 3359f3d

Please sign in to comment.