Skip to content

Commit

Permalink
Trivial refactorings based on #234
Browse files Browse the repository at this point in the history
  • Loading branch information
ckormanyos committed Mar 2, 2022
1 parent d1d50fc commit 8549f7c
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 107 deletions.
20 changes: 13 additions & 7 deletions boost/multiprecision/uintwide_t_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@
#pragma GCC diagnostic ignored "-Wdeprecated-copy"
#endif

#include <boost/version.hpp>

#if !defined(BOOST_VERSION)
#error BOOST_VERSION is not defined. Ensure that <boost/version.hpp> is properly included.
#endif

#if (BOOST_VERSION < 107900)
#include <boost/config.hpp>
#endif
#include <boost/multiprecision/number.hpp>
#include <boost/version.hpp>


#include <math/wide_integer/uintwide_t.h>

Expand All @@ -57,7 +65,7 @@
// Define the number category as an integer number kind
// for the uintwide_t_backend. This is needed for properly
// interacting as a backend with boost::muliprecision.
#if (defined(BOOST_VERSION) && (BOOST_VERSION <= 107200))
#if (BOOST_VERSION <= 107200)
template<
#if defined(WIDE_INTEGER_NAMESPACE)
const WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t MyWidth2,
Expand All @@ -68,7 +76,7 @@
typename MyAllocatorType>
struct number_category<uintwide_t_backend<MyWidth2, MyLimbType, MyAllocatorType>>
: public boost::mpl::int_<number_kind_integer> { };
#elif (defined(BOOST_VERSION) && (BOOST_VERSION <= 107500))
#elif (BOOST_VERSION <= 107500)
template<
#if defined(WIDE_INTEGER_NAMESPACE)
const WIDE_INTEGER_NAMESPACE::math::wide_integer::size_t MyWidth2,
Expand Down Expand Up @@ -111,7 +119,7 @@
::math::wide_integer::uintwide_t<MyWidth2, MyLimbType, MyAllocatorType>;
#endif

#if (defined(BOOST_VERSION) && (BOOST_VERSION <= 107500))
#if (BOOST_VERSION <= 107500)
using signed_types = mpl::list<std::int64_t>;
using unsigned_types = mpl::list<std::uint64_t>;
using float_types = mpl::list<long double>;
Expand Down Expand Up @@ -235,8 +243,7 @@
{
auto result = static_cast<std::size_t>(0U);

#if defined(BOOST_VERSION)
#if(BOOST_VERSION < 107800)
#if (BOOST_VERSION < 107800)
using boost::hash_combine;
#else
using boost::multiprecision::detail::hash_combine;
Expand All @@ -247,7 +254,6 @@
{
hash_combine(result, crepresentation().crepresentation()[i]);
}
#endif

return result;
}
Expand Down
73 changes: 40 additions & 33 deletions math/wide_integer/uintwide_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -1593,11 +1593,10 @@
// Cast operator to built-in Boolean type.
explicit constexpr operator bool() const { return (!is_zero()); }

// Cast operator that casts to a uintwide_t having a different width
// (but having the same limb type) and possibly a different signed-ness.
// Cast operator that casts to a uintwide_t possibly having a different width
// and/or possibly having a different signed-ness, but having the same limb type.
template<const size_t OtherWidth2,
const bool OtherIsSigned,
typename = typename std::enable_if<(Width2 != OtherWidth2), uintwide_t<OtherWidth2, LimbType, AllocatorType, OtherIsSigned>>::type>
const bool OtherIsSigned>
WIDE_INTEGER_CONSTEXPR operator uintwide_t<OtherWidth2, LimbType, AllocatorType, OtherIsSigned>() const // NOLINT(hicpp-explicit-conversions,google-explicit-constructor)
{
const bool this_is_neg = (is_neg(*this));
Expand Down Expand Up @@ -1645,23 +1644,6 @@
return other;
}

// Cast operator that casts to a uintwide_t having a type with the same width
// (and having the same limb type) but definitely having a different signed-ness.
template<const bool OtherIsSigned,
typename = typename std::enable_if<(OtherIsSigned != IsSigned), uintwide_t<Width2, LimbType, AllocatorType, OtherIsSigned>>::type>
WIDE_INTEGER_CONSTEXPR operator uintwide_t<Width2, LimbType, AllocatorType, OtherIsSigned>() const // NOLINT(hicpp-explicit-conversions,google-explicit-constructor)
{
using other_wide_integer_type = uintwide_t<Width2, LimbType, AllocatorType, OtherIsSigned>;

other_wide_integer_type other;

std::copy(crepresentation().cbegin(),
crepresentation().cend(),
other.representation().begin());

return other;
}

// Provide a user interface to the internal data representation.
WIDE_INTEGER_CONSTEXPR auto representation() -> representation_type& { return values; }
WIDE_INTEGER_NODISCARD WIDE_INTEGER_CONSTEXPR auto representation() const -> const representation_type& { return values; }
Expand Down Expand Up @@ -3307,11 +3289,13 @@

for(auto j = static_cast<unsigned_fast_type>(0U); j < static_cast<unsigned_fast_type>(count - i); ++j)
{
const auto i_plus_j = static_cast<result_difference_type>(i + j);

carry = static_cast<local_double_limb_type>(carry + static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(*(a + static_cast<left_difference_type>(i))) * *(b + static_cast<right_difference_type>(j))));
carry = static_cast<local_double_limb_type>(carry + *(r + static_cast<result_difference_type>(i + j)));
carry = static_cast<local_double_limb_type>(carry + *(r + i_plus_j));

*(r + static_cast<result_difference_type>(i + j)) = static_cast<local_limb_type>(carry);
carry = detail::make_hi<local_limb_type>(carry);
*(r + i_plus_j) = static_cast<local_limb_type>(carry);
carry = detail::make_hi<local_limb_type>(carry);
}
}
}
Expand Down Expand Up @@ -3353,14 +3337,28 @@

for( ; j < count; ++j)
{
const auto i_plus_j =
static_cast<result_difference_type>
(
static_cast<result_difference_type>(i) + static_cast<result_difference_type>(j)
);

carry = static_cast<local_double_limb_type>(carry + static_cast<local_double_limb_type>(static_cast<local_double_limb_type>(*(a + static_cast<left_difference_type>(i))) * *(b + static_cast<right_difference_type>(j))));
carry = static_cast<local_double_limb_type>(carry + *(r + static_cast<result_difference_type>(i + j)));
carry = static_cast<local_double_limb_type>(carry + *(r + i_plus_j));

*(r + static_cast<result_difference_type>(i + j)) = static_cast<local_limb_type>(carry);
carry = detail::make_hi<local_limb_type>(carry);
*(r + i_plus_j) = static_cast<local_limb_type>(carry);
carry = detail::make_hi<local_limb_type>(carry);
}

*(r + static_cast<result_difference_type>(i + j)) = static_cast<local_limb_type>(carry);
{
const auto i_plus_count =
static_cast<result_difference_type>
(
static_cast<result_difference_type>(i) + static_cast<result_difference_type>(count)
);

*(r + i_plus_count) = static_cast<local_limb_type>(carry);
}
}
}
}
Expand Down Expand Up @@ -3536,7 +3534,7 @@
// Check the borrow signs. If a1-a0 and b0-b1 have the same signs,
// then add |a1-a0|*|b0-b1| to r1, otherwise subtract it from r1.

const unsigned_fast_type nh = n / 2U;
const auto nh = static_cast<unsigned_fast_type>(n / 2U);

const InputIteratorLeft a0 = a + static_cast<left_difference_type>(0);
const InputIteratorLeft a1 = a + static_cast<left_difference_type>(nh);
Expand All @@ -3547,20 +3545,20 @@
ResultIterator r0 = r + static_cast<result_difference_type>(0);
ResultIterator r1 = r + static_cast<result_difference_type>(nh);
ResultIterator r2 = r + static_cast<result_difference_type>(n);
ResultIterator r3 = r + static_cast<result_difference_type>(n + nh);
ResultIterator r3 = r + static_cast<result_difference_type>(static_cast<result_difference_type>(n) + static_cast<result_difference_type>(nh));

InputIteratorTemp t0 = t + static_cast<temp_difference_type>(0);
InputIteratorTemp t1 = t + static_cast<temp_difference_type>(nh);
InputIteratorTemp t2 = t + static_cast<temp_difference_type>(n);
InputIteratorTemp t4 = t + static_cast<temp_difference_type>(n + n);
InputIteratorTemp t4 = t + static_cast<temp_difference_type>(static_cast<result_difference_type>(n) + static_cast<result_difference_type>(n));

// Step 1
// a1*b1 -> r2
// a0*b0 -> r0
// r -> t0
eval_multiply_kara_n_by_n_to_2n(r2, a1, b1, nh, t0);
eval_multiply_kara_n_by_n_to_2n(r0, a0, b0, nh, t0);
std::copy(r0, r0 + (2U * n), t0);
std::copy(r0, r0 + static_cast<result_difference_type>(static_cast<result_difference_type>(n) * 2U), t0);

local_limb_type carry;

Expand Down Expand Up @@ -3815,7 +3813,16 @@

// Clear the data elements that have not
// been computed in the division algorithm.
std::fill(values.begin() + static_cast<local_uint_index_type>(m + 1U), values.end(), static_cast<limb_type>(0U));
{
const auto m_plus_one =
static_cast<local_uint_index_type>
(
static_cast<local_uint_index_type>(m) + 1U
);

std::fill(values.begin() + m_plus_one, values.end(), static_cast<limb_type>(0U));
}


if(remainder != nullptr)
{
Expand Down
3 changes: 3 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
// When using -std=c++20 and g++-10
// g++-10 -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -Wconversion -Wsign-conversion -std=c++20 -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -DWIDE_INTEGER_HAS_MUL_8_BY_8_UNROLL -I. -I/mnt/c/boost/boost_1_78_0 -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp examples/example000a_builtin_convert.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp -o wide_integer.exe

// When using -std=c++11 and g++
// g++ -finline-functions -march=native -mtune=native -O3 -Wall -Wextra -Wconversion -Wsign-conversion -std=c++11 -DWIDE_INTEGER_HAS_LIMB_TYPE_UINT64 -DWIDE_INTEGER_HAS_MUL_8_BY_8_UNROLL -I. -I/mnt/c/boost/boost_1_78_0 -pthread -lpthread test/test.cpp test/test_uintwide_t_boost_backend.cpp test/test_uintwide_t_edge_cases.cpp test/test_uintwide_t_examples.cpp test/test_uintwide_t_float_convert.cpp test/test_uintwide_t_int_convert.cpp test/test_uintwide_t_n_base.cpp test/test_uintwide_t_n_binary_ops_base.cpp examples/example000a_builtin_convert.cpp test/test_uintwide_t_spot_values.cpp examples/example000_numeric_limits.cpp examples/example001_mul_div.cpp examples/example001a_div_mod.cpp examples/example002_shl_shr.cpp examples/example003_sqrt.cpp examples/example003a_cbrt.cpp examples/example004_rootk_pow.cpp examples/example005_powm.cpp examples/example005a_pow_factors_of_p99.cpp examples/example006_gcd.cpp examples/example007_random_generator.cpp examples/example008_miller_rabin_prime.cpp examples/example008a_miller_rabin_prime.cpp examples/example009_timed_mul.cpp examples/example009a_timed_mul_4_by_4.cpp examples/example009b_timed_mul_8_by_8.cpp examples/example010_uint48_t.cpp examples/example011_uint24_t.cpp examples/example012_rsa_crypto.cpp -o wide_integer.exe

// cd .tidy/make
// make prepare -f make_tidy_01_generic.gmk MY_BOOST_ROOT=/mnt/c/boost/boost_1_78_0
// make tidy -f make_tidy_01_generic.gmk --jobs=8 MY_BOOST_ROOT=/mnt/c/boost/boost_1_78_0
Expand Down
10 changes: 8 additions & 2 deletions test/test_uintwide_t_edge_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ namespace local_edge_cases
} // namespace local_edge_cases

using local_uint_backend_type =
boost::multiprecision::uintwide_t_backend<local_edge_cases::local_digits2, std::uint32_t, std::allocator<void>>;
boost::multiprecision::uintwide_t_backend<local_edge_cases::local_digits2,
std::uint32_t,
std::allocator<std::uint32_t>>;

using boost_uint_backend_allocator_type = void;

using boost_uint_backend_type =
boost::multiprecision::cpp_int_backend<local_edge_cases::local_digits2,
local_edge_cases::local_digits2,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_allocator_type>;

using local_uint_type =
boost::multiprecision::number<local_uint_backend_type,
Expand Down
20 changes: 16 additions & 4 deletions test/test_uintwide_t_n_binary_ops_mul_n_by_m_template.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2019 - 2022.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
Expand Down Expand Up @@ -42,20 +42,32 @@
WIDE_INTEGER_NODISCARD auto get_digits2 () const -> math::wide_integer::size_t override { return digits2a + digits2b; }
#endif

using boost_uint_backend_a_allocator_type = void;

using boost_uint_backend_a_type =
boost::multiprecision::cpp_int_backend<digits2a,
digits2a,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_a_allocator_type>;

using boost_uint_backend_b_allocator_type = void;

using boost_uint_backend_b_type =
boost::multiprecision::cpp_int_backend<digits2b,
digits2b,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_b_allocator_type>;

using boost_uint_backend_c_allocator_type = void;

using boost_uint_backend_c_type =
boost::multiprecision::cpp_int_backend<digits2a + digits2b,
digits2a + digits2b,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_c_allocator_type>;

using boost_uint_a_type = boost::multiprecision::number<boost_uint_backend_a_type, boost::multiprecision::et_off>;
using boost_uint_b_type = boost::multiprecision::number<boost_uint_backend_b_type, boost::multiprecision::et_off>;
Expand Down
8 changes: 6 additions & 2 deletions test/test_uintwide_t_n_binary_ops_template.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2019 - 2022.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
Expand Down Expand Up @@ -34,10 +34,14 @@
static constexpr auto digits2 = static_cast<math::wide_integer::size_t>(MyDigits2);
#endif

using boost_uint_backend_allocator_type = void;

using boost_uint_backend_type =
boost::multiprecision::cpp_int_backend<digits2,
digits2,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_allocator_type>;

using boost_uint_type = boost::multiprecision::number<boost_uint_backend_type, boost::multiprecision::et_on>;

Expand Down
15 changes: 12 additions & 3 deletions test/test_uintwide_t_n_binary_ops_template_signed.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2021 - 2022.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
Expand Down Expand Up @@ -34,14 +34,23 @@
static constexpr auto digits2 = static_cast<math::wide_integer::size_t>(MyDigits2);
#endif

using boost_uint_backend_allocator_type = void;

using boost_uint_backend_type =
boost::multiprecision::cpp_int_backend<digits2,
digits2,
boost::multiprecision::unsigned_magnitude>;
boost::multiprecision::unsigned_magnitude,
boost::multiprecision::unchecked,
boost_uint_backend_allocator_type>;

using boost_sint_backend_allocator_type = void;

using boost_sint_backend_type =
boost::multiprecision::cpp_int_backend<digits2,
digits2,
boost::multiprecision::signed_magnitude>;
boost::multiprecision::signed_magnitude,
boost::multiprecision::unchecked,
boost_sint_backend_allocator_type>;

using boost_uint_type = boost::multiprecision::number<boost_uint_backend_type, boost::multiprecision::et_on>;
using boost_sint_type = boost::multiprecision::number<boost_sint_backend_type, boost::multiprecision::et_on>;
Expand Down
Loading

0 comments on commit 8549f7c

Please sign in to comment.