Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85212 - in trunk: boost/multiprecision libs/multiprecision/test
From: john_at_[hidden]
Date: 2013-08-05 07:52:08


Author: johnmaddock
Date: 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013)
New Revision: 85212
URL: http://svn.boost.org/trac/boost/changeset/85212

Log:
Add tentative serialization support.

Added:
   trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp (contents, props changed)
   trunk/libs/multiprecision/test/test_cpp_int_serial.cpp (contents, props changed)
Text files modified:
   trunk/boost/multiprecision/cpp_dec_float.hpp | 11 ++
   trunk/boost/multiprecision/cpp_int.hpp | 1
   trunk/boost/multiprecision/number.hpp | 5 +
   trunk/libs/multiprecision/test/Jamfile.v2 | 11 ++
   trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp | 93 ++++++++++++++++++++++++
   trunk/libs/multiprecision/test/test_cpp_int_serial.cpp | 153 ++++++++++++++++++++++++++++++++++++++++
   6 files changed, 274 insertions(+), 0 deletions(-)

Modified: trunk/boost/multiprecision/cpp_dec_float.hpp
==============================================================================
--- trunk/boost/multiprecision/cpp_dec_float.hpp Sun Aug 4 18:10:03 2013 (r85211)
+++ trunk/boost/multiprecision/cpp_dec_float.hpp 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -554,6 +554,17 @@
       return (bo_order_is_zero ? static_cast<ExponentType>(0) : static_cast<ExponentType>(exp + prefix));
    }
 
+ template<class Archive>
+ void serialize(Archive & ar, const unsigned int /*version*/)
+ {
+ for(unsigned i = 0; i < data.size(); ++i)
+ ar & data[i];
+ ar & exp;
+ ar & neg;
+ ar & fpclass;
+ ar & prec_elem;
+ }
+
 private:
    static bool data_elem_is_non_zero_predicate(const boost::uint32_t& d) { return (d != static_cast<boost::uint32_t>(0u)); }
    static bool data_elem_is_non_nine_predicate(const boost::uint32_t& d) { return (d != static_cast<boost::uint32_t>(cpp_dec_float::cpp_dec_float_elem_mask - 1)); }

Modified: trunk/boost/multiprecision/cpp_int.hpp
==============================================================================
--- trunk/boost/multiprecision/cpp_int.hpp Sun Aug 4 18:10:03 2013 (r85211)
+++ trunk/boost/multiprecision/cpp_int.hpp 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -1800,5 +1800,6 @@
 #ifdef BOOST_MP_USER_DEFINED_LITERALS
 #include <boost/multiprecision/cpp_int/literals.hpp>
 #endif
+#include <boost/multiprecision/cpp_int/serialize.hpp>
 
 #endif

Modified: trunk/boost/multiprecision/number.hpp
==============================================================================
--- trunk/boost/multiprecision/number.hpp Sun Aug 4 18:10:03 2013 (r85211)
+++ trunk/boost/multiprecision/number.hpp 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -565,6 +565,11 @@
    {
       return m_backend.str(digits, f);
    }
+ template<class Archive>
+ void serialize(Archive & ar, const unsigned int /*version*/)
+ {
+ ar & m_backend;
+ }
 private:
    template <class T>
    void convert_to_imp(T* result)const

Modified: trunk/libs/multiprecision/test/Jamfile.v2
==============================================================================
--- trunk/libs/multiprecision/test/Jamfile.v2 Sun Aug 4 18:10:03 2013 (r85211)
+++ trunk/libs/multiprecision/test/Jamfile.v2 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -568,6 +568,17 @@
 run ublas_interop/test6.cpp ublas_interop/test62.cpp ublas_interop/test63.cpp : : : <define>TEST_ET=1 : ublas5_et ;
 #run ublas_interop/test7.cpp ublas_interop/test71.cpp ublas_interop/test72.cpp ublas_interop/test73.cpp : : : <define>TEST_ET=1 : ublas6_et ;
 
+#
+# Serialization tests, run in release mode so we cycle through more values:
+#
+run test_cpp_int_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST1 : test_cpp_int_serial_1 ;
+run test_cpp_int_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST2 : test_cpp_int_serial_2 ;
+run test_cpp_int_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST3 : test_cpp_int_serial_3 ;
+run test_cpp_int_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST4 : test_cpp_int_serial_4 ;
+run test_cpp_dec_float_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST1 : test_cpp_dec_float_serial_1 ;
+run test_cpp_dec_float_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST2 : test_cpp_dec_float_serial_2 ;
+
+
 if $(enable-specfun)
 {
 

Added: trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -0,0 +1,93 @@
+///////////////////////////////////////////////////////////////
+// Copyright 2013 John Maddock. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
+
+//
+// Compare arithmetic results using fixed_int to GMP results.
+//
+
+#ifdef _MSC_VER
+# define _SCL_SECURE_NO_WARNINGS
+#endif
+
+#include <boost/multiprecision/cpp_dec_float.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/timer.hpp>
+#include "test.hpp"
+
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <boost/archive/text_iarchive.hpp>
+#include <boost/archive/text_oarchive.hpp>
+
+template <class T>
+T generate_random(unsigned bits_wanted)
+{
+ typedef typename T::backend_type::exponent_type e_type;
+ static boost::random::mt19937 gen;
+ T val = gen();
+ T prev_val = -1;
+ while(val != prev_val)
+ {
+ val *= (gen.max)();
+ prev_val = val;
+ val += gen();
+ }
+ e_type e;
+ val = frexp(val, &e);
+
+ static boost::random::uniform_int_distribution<e_type> ui(std::numeric_limits<T>::min_exponent + 1, std::numeric_limits<T>::max_exponent - 1);
+ return ldexp(val, ui(gen));
+}
+
+template <class T>
+void test()
+{
+ boost::timer tim;
+
+ while(true)
+ {
+ T val = generate_random<T>(boost::math::tools::digits<T>());
+ std::stringstream ss;
+ boost::archive::text_oarchive oa(ss);
+ oa << static_cast<const T&>(val);
+ boost::archive::text_iarchive ia(ss);
+ T val2;
+ ia >> val2;
+ BOOST_CHECK_EQUAL(val, val2);
+ //
+ // Check to see if test is taking too long.
+ // Tests run on the compiler farm time out after 300 seconds,
+ // so don't get too close to that:
+ //
+ if(tim.elapsed() > 150)
+ {
+ std::cout << "Timeout reached, aborting tests now....\n";
+ break;
+ }
+ }
+}
+
+#if !defined(TEST1) && !defined(TEST2)
+# define TEST1
+# define TEST2
+#endif
+
+
+int main()
+{
+ using namespace boost::multiprecision;
+#ifdef TEST1
+ test<cpp_dec_float_50>();
+#endif
+#ifndef TEST2
+ test<number<cpp_dec_float<100, boost::int64_t, std::allocator<void> > > >();
+#endif
+ return boost::report_errors();
+}
+
+
+

Added: trunk/libs/multiprecision/test/test_cpp_int_serial.cpp
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ trunk/libs/multiprecision/test/test_cpp_int_serial.cpp 2013-08-05 07:52:07 EDT (Mon, 05 Aug 2013) (r85212)
@@ -0,0 +1,153 @@
+///////////////////////////////////////////////////////////////
+// Copyright 2012 John Maddock. Distributed under the Boost
+// Software License, Version 1.0. (See accompanying file
+// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
+
+//
+// Compare arithmetic results using fixed_int to GMP results.
+//
+
+#ifdef _MSC_VER
+# define _SCL_SECURE_NO_WARNINGS
+#endif
+
+#include <boost/multiprecision/cpp_int.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_int.hpp>
+#include <boost/timer.hpp>
+#include "test.hpp"
+
+#include <iostream>
+#include <iomanip>
+#include <sstream>
+#include <boost/archive/text_iarchive.hpp>
+#include <boost/archive/text_oarchive.hpp>
+#include <boost/archive/binary_iarchive.hpp>
+#include <boost/archive/binary_oarchive.hpp>
+
+template <class T>
+T generate_random(unsigned bits_wanted)
+{
+ static boost::random::mt19937 gen;
+ typedef boost::random::mt19937::result_type random_type;
+
+ T max_val;
+ unsigned digits;
+ if(std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
+ {
+ max_val = (std::numeric_limits<T>::max)();
+ digits = std::numeric_limits<T>::digits;
+ }
+ else
+ {
+ max_val = T(1) << bits_wanted;
+ digits = bits_wanted;
+ }
+
+ unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
+ while((random_type(1) << bits_per_r_val) > (gen.max)()) --bits_per_r_val;
+
+ unsigned terms_needed = digits / bits_per_r_val + 1;
+
+ T val = 0;
+ for(unsigned i = 0; i < terms_needed; ++i)
+ {
+ val *= (gen.max)();
+ val += gen();
+ }
+ val %= max_val;
+ return val;
+}
+
+template <class T>
+void test_neg(const T& x, const boost::mpl::true_&)
+{
+ T val = -x;
+ std::stringstream ss;
+ boost::archive::text_oarchive oa(ss);
+ oa << static_cast<const T&>(val);
+ boost::archive::text_iarchive ia(ss);
+ T val2;
+ ia >> val2;
+ BOOST_CHECK_EQUAL(val, val2);
+
+ ss.clear();
+ boost::archive::binary_oarchive ob(ss);
+ ob << static_cast<const T&>(val);
+ boost::archive::binary_iarchive ib(ss);
+ val2;
+ ib >> val2;
+ BOOST_CHECK_EQUAL(val, val2);
+}
+template <class T>
+void test_neg(const T& val, const boost::mpl::false_&){}
+
+template <class T>
+void test()
+{
+ using namespace boost::multiprecision;
+
+ boost::random::mt19937 gen;
+ boost::uniform_int<> d(3, std::numeric_limits<T>::is_bounded ? std::numeric_limits<T>::digits : 3000);
+ boost::timer tim;
+
+ while(true)
+ {
+ T val = generate_random<T>(d(gen));
+ std::stringstream ss;
+ boost::archive::text_oarchive oa(ss);
+ oa << static_cast<const T&>(val);
+ boost::archive::text_iarchive ia(ss);
+ T val2;
+ ia >> val2;
+ BOOST_CHECK_EQUAL(val, val2);
+
+ ss.clear();
+ boost::archive::binary_oarchive ob(ss);
+ ob << static_cast<const T&>(val);
+ boost::archive::binary_iarchive ib(ss);
+ val2;
+ ib >> val2;
+ BOOST_CHECK_EQUAL(val, val2);
+
+ test_neg(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
+ //
+ // Check to see if test is taking too long.
+ // Tests run on the compiler farm time out after 300 seconds,
+ // so don't get too close to that:
+ //
+ if(tim.elapsed() > 150)
+ {
+ std::cout << "Timeout reached, aborting tests now....\n";
+ break;
+ }
+ }
+}
+
+#if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
+# define TEST1
+# define TEST2
+# define TEST3
+# define TEST4
+#endif
+
+int main()
+{
+ using namespace boost::multiprecision;
+#ifdef TEST1
+ test<cpp_int>();
+#endif
+#ifdef TEST2
+ test<number<cpp_int_backend<61, 61, unsigned_magnitude, unchecked, void> > >();
+#endif
+#ifdef TEST3
+ test<number<cpp_int_backend<120, 120, signed_magnitude, unchecked, void> > >();
+#endif
+#ifdef TEST4
+ test<int1024_t>();
+#endif
+ return boost::report_errors();
+}
+
+
+


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