From cd0b7ea37ed212c6d67e9bde5e46c61139678785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 23 May 2019 10:26:09 +0200 Subject: [PATCH 1/3] int128: Compile with -Wpedantic --- CMakeLists.txt | 2 +- include/intx/int128.hpp | 10 ++++++++-- test/benchmarks/utils.cpp | 1 + test/unittests/test_int128.cpp | 13 ++++++++----- test/utils/gmp.hpp | 4 ++-- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a76941d..44acb2e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/include/intx/int128.hpp b/include/intx/int128.hpp index 1c36fd11..f5aefc0f 100644 --- a/include/intx/int128.hpp +++ b/include/intx/int128.hpp @@ -45,14 +45,17 @@ struct uint<128> {} #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(hi) << 64) | lo; } +#pragma GCC diagnostic pop #endif constexpr explicit operator bool() const noexcept { return hi | lo; } @@ -287,8 +290,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(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); diff --git a/test/benchmarks/utils.cpp b/test/benchmarks/utils.cpp index 03ec6a77..f284d41c 100644 --- a/test/benchmarks/utils.cpp +++ b/test/benchmarks/utils.cpp @@ -7,6 +7,7 @@ #include #include +#pragma GCC diagnostic ignored "-Wpedantic" uint64_t udiv_native(uint64_t x, uint64_t y) noexcept { diff --git a/test/unittests/test_int128.cpp b/test/unittests/test_int128.cpp index 2026fb95..e846f9c3 100644 --- a/test/unittests/test_int128.cpp +++ b/test/unittests/test_int128.cpp @@ -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; @@ -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; diff --git a/test/utils/gmp.hpp b/test/utils/gmp.hpp index 3f88c01a..db4e4b83 100644 --- a/test/utils/gmp.hpp +++ b/test/utils/gmp.hpp @@ -52,7 +52,7 @@ inline div_result 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 inline div_result sdivrem(const Int& x, const Int& y) noexcept @@ -98,7 +98,7 @@ inline div_result sdivrem(const Int& x, const Int& y) noexcept mpz_clears(x_gmp, y_gmp, q_gmp, r_gmp, NULL); return {q, r}; -}; +} template inline Int add(const Int& x, const Int& y) noexcept From 03c3f1fdbcb66fc67043ca59b148135e37b9718f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 23 May 2019 10:38:16 +0200 Subject: [PATCH 2/3] int128: Make conversions explict --- include/intx/int128.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/intx/int128.hpp b/include/intx/int128.hpp index f5aefc0f..92045f65 100644 --- a/include/intx/int128.hpp +++ b/include/intx/int128.hpp @@ -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> { @@ -37,7 +37,7 @@ struct uint<128> template ::value>::type> - constexpr uint(T x) noexcept : lo(x) // NOLINT + constexpr uint(T x) noexcept : lo(static_cast(x)) // NOLINT {} template ::value>::type> @@ -374,7 +374,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 } @@ -385,7 +385,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 } From e31eadc597bb615c238e8816a8bb5c357cead5df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Thu, 23 May 2019 10:57:30 +0200 Subject: [PATCH 3/3] int128: Remove redundant constructor --- include/intx/int128.hpp | 6 +----- test/unittests/test_intx_api.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/include/intx/int128.hpp b/include/intx/int128.hpp index 92045f65..b4ed5910 100644 --- a/include/intx/int128.hpp +++ b/include/intx/int128.hpp @@ -36,14 +36,10 @@ struct uint<128> constexpr uint(uint64_t high, uint64_t low) noexcept : lo{low}, hi{high} {} template ::value>::type> + typename = typename std::enable_if_t::value>> constexpr uint(T x) noexcept : lo(static_cast(x)) // NOLINT {} - template ::value>::type> - constexpr explicit uint(T x) noexcept : lo(x) - {} - #ifdef __SIZEOF_INT128__ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpedantic" diff --git a/test/unittests/test_intx_api.cpp b/test/unittests/test_intx_api.cpp index f5d4cb77..d39e401a 100644 --- a/test/unittests/test_intx_api.cpp +++ b/test/unittests/test_intx_api.cpp @@ -53,6 +53,17 @@ class uint_api : public testing::Test using types = testing::Types; 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{};