Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78423 - in sandbox/big_number: boost/multiprecision boost/multiprecision/detail libs/multiprecision/test
From: john_at_[hidden]
Date: 2012-05-11 13:19:15


Author: johnmaddock
Date: 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
New Revision: 78423
URL: http://svn.boost.org/trac/boost/changeset/78423

Log:
Enhance pow and powm tests.
Quash some warnings.
Fix the IO tests on GCC.
Text files modified:
   sandbox/big_number/boost/multiprecision/cpp_int.hpp | 5 +++--
   sandbox/big_number/boost/multiprecision/detail/generic_interconvert.hpp | 7 +++++++
   sandbox/big_number/boost/multiprecision/integer_ops.hpp | 25 +++++++++++++++++++++++++
   sandbox/big_number/boost/multiprecision/miller_rabin.hpp | 30 +++++++++---------------------
   sandbox/big_number/boost/multiprecision/mp_number.hpp | 16 ++++++++--------
   sandbox/big_number/libs/multiprecision/test/Jamfile.v2 | 3 +++
   sandbox/big_number/libs/multiprecision/test/test_arithmetic.cpp | 25 ++++++++++++++++++++++++-
   sandbox/big_number/libs/multiprecision/test/test_cpp_int.cpp | 2 +-
   sandbox/big_number/libs/multiprecision/test/test_float_io.cpp | 29 ++++++++++++++++++++++++++++-
   9 files changed, 108 insertions(+), 34 deletions(-)

Modified: sandbox/big_number/boost/multiprecision/cpp_int.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/cpp_int.hpp (original)
+++ sandbox/big_number/boost/multiprecision/cpp_int.hpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -119,14 +119,14 @@
    {
       *limbs() = 0;
    }
- cpp_int_base(const cpp_int_base& o) : m_limbs(0), m_internal(true)
+ cpp_int_base(const cpp_int_base& o) : allocator_type(o), m_limbs(0), m_internal(true)
    {
       resize(o.size());
       std::memcpy(limbs(), o.limbs(), size() * sizeof(limb_type));
       m_sign = o.m_sign;
    }
 #ifndef BOOST_NO_RVALUE_REFERENCES
- cpp_int_base(cpp_int_base&& o) : m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal)
+ cpp_int_base(cpp_int_base&& o) : allocator_type(o), m_limbs(o.m_limbs), m_sign(o.m_sign), m_internal(o.m_internal)
    {
       if(m_internal)
       {
@@ -149,6 +149,7 @@
    {
       if(this != &o)
       {
+ static_cast<allocator_type&>(*this) = static_cast<const allocator_type&>(o);
          m_limbs = 0;
          resize(o.size());
          std::memcpy(limbs(), o.limbs(), size() * sizeof(limb_type));

Modified: sandbox/big_number/boost/multiprecision/detail/generic_interconvert.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/detail/generic_interconvert.hpp (original)
+++ sandbox/big_number/boost/multiprecision/detail/generic_interconvert.hpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -107,6 +107,10 @@
 template <class To, class From>
 void generic_interconvert(To& to, const From& from, const mpl::int_<number_kind_floating_point>& /*to_type*/, const mpl::int_<number_kind_floating_point>& /*from_type*/)
 {
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4127)
+#endif
    //
    // The code here only works when the radix of "From" is 2, we could try shifting by other
    // radixes but it would complicate things.... use a string convertion when the radix is other
@@ -178,6 +182,9 @@
       return;
    }
    eval_ldexp(to, to, static_cast<to_exponent>(e));
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif
 }
 
 template <class To, class From>

Modified: sandbox/big_number/boost/multiprecision/integer_ops.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/integer_ops.hpp (original)
+++ sandbox/big_number/boost/multiprecision/integer_ops.hpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -160,7 +160,14 @@
 template <class Backend>
 void eval_powm(Backend& result, const Backend& a, const Backend& p, const Backend& c)
 {
+ using default_ops::eval_bit_test;
+ using default_ops::eval_get_sign;
+ using default_ops::eval_multiply;
+ using default_ops::eval_modulus;
+ using default_ops::eval_right_shift;
+
    typedef typename canonical<unsigned char, Backend>::type ui_type;
+
    Backend x, y(a), b(p);
    x = ui_type(1u);
    while(eval_get_sign(b) > 0)
@@ -183,6 +190,12 @@
    typedef typename canonical<unsigned char, Backend>::type ui_type;
    typedef typename canonical<Integer, Backend>::type i_type;
 
+ using default_ops::eval_bit_test;
+ using default_ops::eval_get_sign;
+ using default_ops::eval_multiply;
+ using default_ops::eval_modulus;
+ using default_ops::eval_right_shift;
+
    if(eval_get_sign(p) < 0)
    {
       BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
@@ -210,6 +223,12 @@
    typedef typename canonical<unsigned char, Backend>::type ui_type;
    typedef typename canonical<Integer, Backend>::type i_type;
 
+ using default_ops::eval_bit_test;
+ using default_ops::eval_get_sign;
+ using default_ops::eval_multiply;
+ using default_ops::eval_modulus;
+ using default_ops::eval_right_shift;
+
    if(b < 0)
    {
       BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));
@@ -238,6 +257,12 @@
    typedef typename canonical<Integer1, Backend>::type i1_type;
    typedef typename canonical<Integer2, Backend>::type i2_type;
 
+ using default_ops::eval_bit_test;
+ using default_ops::eval_get_sign;
+ using default_ops::eval_multiply;
+ using default_ops::eval_modulus;
+ using default_ops::eval_right_shift;
+
    if(b < 0)
    {
       BOOST_THROW_EXCEPTION(std::runtime_error("powm requires a positive exponent."));

Modified: sandbox/big_number/boost/multiprecision/miller_rabin.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/miller_rabin.hpp (original)
+++ sandbox/big_number/boost/multiprecision/miller_rabin.hpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -10,25 +10,6 @@
 
 namespace boost{
 namespace multiprecision{
-//
-// Calculate (a^b)%c:
-//
-template <class Backend, bool ExpressionTemplates>
-void expmod(const mp_number<Backend, ExpressionTemplates>& a, mp_number<Backend, ExpressionTemplates> b, const mp_number<Backend, ExpressionTemplates>& c, mp_number<Backend, ExpressionTemplates>& result)
-{
- typedef mp_number<Backend, ExpressionTemplates> number_type;
- number_type x(1), y(a);
- while(b > 0)
- {
- if(b & 1)
- {
- x = (x * y) % c;
- }
- y = (y * y) % c;
- b /= 2;
- }
- result = x % c;
-}
 
 template <class Backend, bool ExpressionTemplates>
 bool check_small_factors(const mp_number<Backend, ExpressionTemplates>& n)
@@ -140,6 +121,10 @@
 typename enable_if_c<number_category<Backend>::value == number_kind_integer, bool>::type
    miller_rabin_test(const mp_number<Backend, ExpressionTemplates>& n, unsigned trials, Engine& gen)
 {
+#ifdef BOOST_MSVC
+#pragma warning(push)
+#pragma warning(disable:4127)
+#endif
    typedef mp_number<Backend, ExpressionTemplates> number_type;
 
    if(n <= 227)
@@ -155,7 +140,7 @@
    // Begin with a single Fermat test - it excludes a lot of candidates:
    //
    number_type q(228), x, y; // We know n is greater than this, as we've excluded small factors
- expmod(q, nm1, n, x);
+ x = powm(q, nm1, n);
    if(x != 1u)
       return false;
 
@@ -174,7 +159,7 @@
    for(unsigned i = 0; i < trials; ++i)
    {
       x = dist(gen);
- expmod(x, q, n, y);
+ y = powm(x, q, n);
       unsigned j = 0;
       while(true)
       {
@@ -188,6 +173,9 @@
       }
    }
    return true; // Yeheh! probably prime.
+#ifdef BOOST_MSVC
+#pragma warning(pop)
+#endif
 }
 
 template <class Backend, bool ExpressionTemplates>

Modified: sandbox/big_number/boost/multiprecision/mp_number.hpp
==============================================================================
--- sandbox/big_number/boost/multiprecision/mp_number.hpp (original)
+++ sandbox/big_number/boost/multiprecision/mp_number.hpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -59,12 +59,12 @@
    mp_number(const mp_number<Backend, ET>& val) : m_backend(val.m_backend) {}
 
    template <class Other, bool ET>
- mp_number(const mp_number<Other, ET>& val, typename enable_if<boost::is_convertible<Other, Backend> >::type* dummy1 = 0)
+ mp_number(const mp_number<Other, ET>& val, typename enable_if<boost::is_convertible<Other, Backend> >::type* = 0)
    {
       m_backend = val.backend();
    }
    template <class Other, bool ET>
- mp_number(const mp_number<Other, ET>& val, typename disable_if<boost::is_convertible<Other, Backend> >::type* dummy1 = 0)
+ mp_number(const mp_number<Other, ET>& val, typename disable_if<boost::is_convertible<Other, Backend> >::type* = 0)
    {
       //
       // Attempt a generic interconvertion:
@@ -72,13 +72,13 @@
       detail::generic_interconvert(backend(), val.backend(), number_category<Backend>(), number_category<Other>());
    }
    template <class V>
- mp_number(V v1, V v2, typename enable_if<mpl::or_<boost::is_arithmetic<V>, is_same<std::string, V>, is_convertible<V, const char*> > >::type* dummy1 = 0)
+ mp_number(V v1, V v2, typename enable_if<mpl::or_<boost::is_arithmetic<V>, is_same<std::string, V>, is_convertible<V, const char*> > >::type* = 0)
    {
       using default_ops::assign_components;
       assign_components(m_backend, canonical_value(v1), canonical_value(v2));
    }
    template <class Other, bool ET>
- mp_number(const mp_number<Other, ET>& v1, const mp_number<Other, ET>& v2, typename enable_if<boost::is_convertible<Other, Backend> >::type* dummy1 = 0)
+ mp_number(const mp_number<Other, ET>& v1, const mp_number<Other, ET>& v2, typename enable_if<boost::is_convertible<Other, Backend> >::type* = 0)
    {
       using default_ops::assign_components;
       assign_components(m_backend, v1.backend(), v2.backend());
@@ -1136,7 +1136,7 @@
       do_assign_function_3c(f, val1, val2, val3, t3);
    }
    template <class F, class Exp1, class Exp2, class Exp3, class Tag2, class Tag3>
- void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag2& t2, const Tag3& t3)
+ void do_assign_function_3b(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag2& /*t2*/, const Tag3& t3)
    {
       mp_number t(val2);
       do_assign_function_3c(f, val1, t, val3, t3);
@@ -1147,7 +1147,7 @@
       f(m_backend, function_arg_value(val1), function_arg_value(val2), function_arg_value(val3));
    }
    template <class F, class Exp1, class Exp2, class Exp3, class Tag3>
- void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag3& t3)
+ void do_assign_function_3c(const F& f, const Exp1& val1, const Exp2& val2, const Exp3& val3, const Tag3& /*t3*/)
    {
       mp_number t(val3);
       do_assign_function_3c(f, val1, val2, t, detail::terminal());
@@ -1727,7 +1727,7 @@
    char c;
    bool have_hex = false;
 
- while((EOF != (c = is.peek())) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
+ while((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
    {
       if(c == 'x' || c == 'X')
          have_hex = true;
@@ -1739,7 +1739,7 @@
    if(c == '/')
    {
       is.get();
- while((EOF != (c = is.peek())) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
+ while((EOF != (c = static_cast<char>(is.peek()))) && (c == 'x' || c == 'X' || c == '-' || c == '+' || (c >= '0' && c <= '9') || (have_hex && (c >= 'a' && c <= 'f')) || (have_hex && (c >= 'A' && c <= 'F'))))
       {
          if(c == 'x' || c == 'X')
             have_hex = true;

Modified: sandbox/big_number/libs/multiprecision/test/Jamfile.v2
==============================================================================
--- sandbox/big_number/libs/multiprecision/test/Jamfile.v2 (original)
+++ sandbox/big_number/libs/multiprecision/test/Jamfile.v2 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -23,6 +23,9 @@
    <search>$(mpfr_path)/build.vc10/lib/Win32/Debug
    <search>$(tommath_path)
    <toolset>msvc:<runtime-link>static
+ <toolset>msvc:<warnings>all
+ <toolset>gcc:<cxxflags>-Wall
+ <toolset>gcc:<cxxflags>-Wextra
    ;
 
 local enable-specfun = [ MATCH (--enable-specfun) : [ modules.peek : ARGV ] ] ;

Modified: sandbox/big_number/libs/multiprecision/test/test_arithmetic.cpp
==============================================================================
--- sandbox/big_number/libs/multiprecision/test/test_arithmetic.cpp (original)
+++ sandbox/big_number/libs/multiprecision/test/test_arithmetic.cpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -412,7 +412,7 @@
    }
    for(unsigned i = 0; i < 20; ++i)
    {
- if(std::numeric_limits<Real>::is_specialized && (!std::numeric_limits<Real>::is_bounded || (i * 17 < std::numeric_limits<Real>::digits)))
+ if(std::numeric_limits<Real>::is_specialized && (!std::numeric_limits<Real>::is_bounded || ((int)i * 17 < std::numeric_limits<Real>::digits)))
       {
          BOOST_TEST(lsb(Real(1) << (i * 17)) == i * 17);
          BOOST_TEST(bit_test(Real(1) << (i * 17), i * 17));
@@ -428,6 +428,29 @@
          BOOST_TEST(bit_unset(zero, i * 17) == 0);
       }
    }
+ //
+ // pow, powm:
+ //
+ BOOST_TEST(pow(Real(3), 4) == 81);
+ BOOST_TEST(pow(Real(3) + Real(0), 4) == 81);
+ BOOST_TEST(powm(Real(3), Real(4), Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3), Real(4), 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3), Real(4), Real(13) + 0) == 81 % 13);
+ BOOST_TEST(powm(Real(3), Real(4) + 0, Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3), Real(4) + 0, 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3), Real(4) + 0, Real(13) + 0) == 81 % 13);
+ BOOST_TEST(powm(Real(3), 4 + 0, Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3), 4 + 0, 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3), 4 + 0, Real(13) + 0) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4), Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4), 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4), Real(13) + 0) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4) + 0, Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4) + 0, 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, Real(4) + 0, Real(13) + 0) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, 4 + 0, Real(13)) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, 4 + 0, 13) == 81 % 13);
+ BOOST_TEST(powm(Real(3) + 0, 4 + 0, Real(13) + 0) == 81 % 13);
 }
 
 template <class Real, class T>

Modified: sandbox/big_number/libs/multiprecision/test/test_cpp_int.cpp
==============================================================================
--- sandbox/big_number/libs/multiprecision/test/test_cpp_int.cpp (original)
+++ sandbox/big_number/libs/multiprecision/test/test_cpp_int.cpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -284,7 +284,7 @@
          BOOST_CHECK_EQUAL(mpz_int(powm(a, ui, c)).str(), test_type(powm(a, ui, c)).str());
       }
 
- if(last_error_count != boost::detail::test_errors())
+ if(last_error_count != (unsigned)boost::detail::test_errors())
       {
          last_error_count = boost::detail::test_errors();
          std::cout << std::hex << std::showbase;

Modified: sandbox/big_number/libs/multiprecision/test/test_float_io.cpp
==============================================================================
--- sandbox/big_number/libs/multiprecision/test/test_float_io.cpp (original)
+++ sandbox/big_number/libs/multiprecision/test/test_float_io.cpp 2012-05-11 13:19:13 EDT (Fri, 11 May 2012)
@@ -209,13 +209,40 @@
 }
 
 template <class T>
+struct max_digits10_proxy
+{
+ static const unsigned value = std::numeric_limits<T>::digits10 + 5;
+};
+#ifdef TEST_CPP_DEC_FLOAT
+template <unsigned D, bool ET>
+struct max_digits10_proxy<boost::multiprecision::mp_number<boost::multiprecision::cpp_dec_float<D>, ET> >
+{
+ static const unsigned value = std::numeric_limits<boost::multiprecision::mp_number<boost::multiprecision::cpp_dec_float<D>, ET> >::max_digits10;
+};
+#endif
+#ifdef TEST_MPF_50
+template <unsigned D, bool ET>
+struct max_digits10_proxy<boost::multiprecision::mp_number<boost::multiprecision::gmp_float<D>, ET> >
+{
+ static const unsigned value = std::numeric_limits<boost::multiprecision::mp_number<boost::multiprecision::gmp_float<D>, ET> >::max_digits10;
+};
+#endif
+#ifdef TEST_MPFR_50
+template <unsigned D, bool ET>
+struct max_digits10_proxy<boost::multiprecision::mp_number<boost::multiprecision::mpfr_float_backend<D>, ET> >
+{
+ static const unsigned value = std::numeric_limits<boost::multiprecision::mp_number<boost::multiprecision::mpfr_float_backend<D>, ET> >::max_digits10;
+};
+#endif
+
+template <class T>
 void do_round_trip(const T& val, std::ios_base::fmtflags f)
 {
    std::stringstream ss;
 #ifndef BOOST_NO_NUMERIC_LIMITS_LOWEST
    ss << std::setprecision(std::numeric_limits<T>::max_digits10);
 #else
- ss << std::setprecision(std::numeric_limits<T>::digits10 + 5);
+ ss << std::setprecision(max_digits10_proxy<T>::value);
 #endif
    ss.flags(f);
    ss << val;


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk