$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r85241 - in trunk: boost/multiprecision boost/multiprecision/detail libs/multiprecision/test
From: john_at_[hidden]
Date: 2013-08-08 13:43:55
Author: johnmaddock
Date: 2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)
New Revision: 85241
URL: http://svn.boost.org/trac/boost/changeset/85241
Log:
Add serialization support to float128 and adaptor backends.
Use __float128 via typedefs so recent Intel compilers don't complain.
Added:
   trunk/libs/multiprecision/test/test_adapt_serial.cpp   (contents, props changed)
   trunk/libs/multiprecision/test/test_float128_serial.cpp   (contents, props changed)
   trunk/libs/multiprecision/test/test_float_serial.hpp   (contents, props changed)
Text files modified: 
   trunk/boost/multiprecision/debug_adaptor.hpp                 |     8 ++                                      
   trunk/boost/multiprecision/detail/float_string_cvt.hpp       |     2                                         
   trunk/boost/multiprecision/float128.hpp                      |    74 +++++++++++++++++++--                   
   trunk/boost/multiprecision/logged_adaptor.hpp                |     7 ++                                      
   trunk/libs/multiprecision/test/Jamfile.v2                    |     2                                         
   trunk/libs/multiprecision/test/test_adapt_serial.cpp         |   136 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp |    59 -----------------                       
   trunk/libs/multiprecision/test/test_float128_serial.cpp      |    26 +++++++                                 
   trunk/libs/multiprecision/test/test_float_serial.hpp         |    91 ++++++++++++++++++++++++++              
   9 files changed, 338 insertions(+), 67 deletions(-)
Modified: trunk/boost/multiprecision/debug_adaptor.hpp
==============================================================================
--- trunk/boost/multiprecision/debug_adaptor.hpp	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/boost/multiprecision/debug_adaptor.hpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -108,6 +108,14 @@
    {
       return m_value;
    }
+   template <class Archive>
+   void serialize(Archive& ar, const unsigned int /*version*/)
+   {
+      ar & m_value;
+      typedef typename Archive::is_loading tag;
+      if(tag::value)
+         update_view();
+   }
 };
 
 template <class Backend>
Modified: trunk/boost/multiprecision/detail/float_string_cvt.hpp
==============================================================================
--- trunk/boost/multiprecision/detail/float_string_cvt.hpp	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/boost/multiprecision/detail/float_string_cvt.hpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -54,7 +54,7 @@
    std::string result;
    bool iszero = false;
    bool isneg = false;
-   exponent_type expon;
+   exponent_type expon = 0;
    std::streamsize org_digits = digits;
    BOOST_ASSERT(digits > 0);
 
Modified: trunk/boost/multiprecision/float128.hpp
==============================================================================
--- trunk/boost/multiprecision/float128.hpp	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/boost/multiprecision/float128.hpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -17,11 +17,13 @@
 #include <quadmath.h>
 }
 
+typedef __float128 float128_type;
+
 #else
 
 #include <boost/multiprecision/detail/float_string_cvt.hpp>
 
-typedef _Quad __float128;
+typedef _Quad float128_type;
 
 extern "C" {
 _Quad __ldexpq(_Quad, int);
@@ -95,7 +97,7 @@
 template<>
 struct number_category<backends::float128_backend> : public mpl::int_<number_kind_floating_point> {};
 template<>
-struct number_category<__float128> : public mpl::int_<number_kind_floating_point> {};
+struct number_category<float128_type> : public mpl::int_<number_kind_floating_point> {};
 
 typedef number<float128_backend, et_off> float128;
 
@@ -110,7 +112,7 @@
    typedef int                                                   exponent_type;
 
 private:
-   __float128 m_value;
+   float128_type m_value;
 public:
    BOOST_CONSTEXPR float128_backend() : m_value(0) {}
    BOOST_CONSTEXPR float128_backend(const float128_backend& o) : m_value(o.m_value) {}
@@ -120,10 +122,10 @@
       return *this;
    }
    template <class T>
-   BOOST_CONSTEXPR float128_backend(const T& i, const typename enable_if_c<is_convertible<T, __float128>::value>::type* = 0)
+   BOOST_CONSTEXPR float128_backend(const T& i, const typename enable_if_c<is_convertible<T, float128_type>::value>::type* = 0)
       : m_value(i) {}
    template <class T>
-   typename enable_if_c<is_arithmetic<T>::value || is_convertible<T, __float128>::value, float128_backend&>::type operator = (const T& i)
+   typename enable_if_c<is_arithmetic<T>::value || is_convertible<T, float128_type>::value, float128_backend&>::type operator = (const T& i)
    {
       m_value = i;
       return *this;
@@ -176,7 +178,7 @@
          v = quadmath_snprintf (&buf2[0], v_max + 3, format.c_str(), digits, m_value);
          if(v >= v_max + 3)
          {
-            BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of __float128 failed."));
+            BOOST_THROW_EXCEPTION(std::runtime_error("Formatting of float128_type failed."));
          }
          return &buf2[0];
       }
@@ -198,11 +200,11 @@
    {
       return m_value == i ? 0 : m_value < i ? -1 : 1;
    }
-   __float128& value()
+   float128_type& value()
    {
       return m_value;
    }
-   const __float128& value()const
+   const float128_type& value()const
    {
       return m_value;
    }
@@ -442,6 +444,62 @@
 
 }} // namespaces
 
+namespace boost{ 
+namespace archive{
+
+class binary_oarchive;
+class binary_iarchive;
+
+}
+   
+namespace serialization{ namespace float128_detail{
+
+template <class Archive>
+void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::false_&)
+{
+   // saving
+   // non-binary
+   std::string s(val.str(0, std::ios_base::scientific));
+   ar & s;
+}
+template <class Archive>
+void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::false_&)
+{
+   // loading
+   // non-binary
+   std::string s;
+   ar & s;
+   val = s.c_str();
+}
+
+template <class Archive>
+void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::false_&, const mpl::true_&)
+{
+   // saving
+   // binary
+   ar.save_binary(&val, sizeof(val));
+}
+template <class Archive>
+void do_serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, const mpl::true_&, const mpl::true_&)
+{
+   // loading
+   // binary
+   ar.load_binary(&val, sizeof(val));
+}
+
+} // detail
+
+template <class Archive>
+void serialize(Archive& ar, boost::multiprecision::backends::float128_backend& val, unsigned int /*version*/)
+{
+   typedef typename Archive::is_loading load_tag;
+   typedef typename mpl::bool_<boost::is_same<Archive, boost::archive::binary_oarchive>::value || boost::is_same<Archive, boost::archive::binary_iarchive>::value> binary_tag;
+
+   float128_detail::do_serialize(ar, val, load_tag(), binary_tag());
+}
+
+}}
+
 namespace std{
 
 template <boost::multiprecision::expression_template_option ExpressionTemplates>
Modified: trunk/boost/multiprecision/logged_adaptor.hpp
==============================================================================
--- trunk/boost/multiprecision/logged_adaptor.hpp	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/boost/multiprecision/logged_adaptor.hpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -132,6 +132,13 @@
    {
       return m_value;
    }
+   template <class Archive>
+   void serialize(Archive& ar, const unsigned int /*version*/)
+   {
+      log_prefix_event(m_value, "serialize");
+      ar & m_value;
+      log_postfix_event(m_value, "serialize");
+   }
 };
 
 template <class T>
Modified: trunk/libs/multiprecision/test/Jamfile.v2
==============================================================================
--- trunk/libs/multiprecision/test/Jamfile.v2	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/libs/multiprecision/test/Jamfile.v2	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -577,8 +577,10 @@
 run test_cpp_int_serial.cpp ../../serialization/build//boost_serialization : : : release <define>TEST4 : test_cpp_int_serial_4 ;
 run test_cpp_int_deserial.cpp ../../serialization/build//boost_serialization : : : release ;
 run test_cpp_rat_serial.cpp ../../serialization/build//boost_serialization : : : release ;
+run test_adapt_serial.cpp ../../serialization/build//boost_serialization : : : release ;
 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 ;
+run test_float128_serial.cpp ../../serialization/build//boost_serialization quadmath : : : release [ check-target-builds ../config//has_float128 : : <build>no ]  ;
 
 
 if $(enable-specfun)
Added: trunk/libs/multiprecision/test/test_adapt_serial.cpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/libs/multiprecision/test/test_adapt_serial.cpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -0,0 +1,136 @@
+///////////////////////////////////////////////////////////////
+//  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/multiprecision/logged_adaptor.hpp>
+#include <boost/multiprecision/debug_adaptor.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);
+   ib >> val2;
+   BOOST_CHECK_EQUAL(val, val2);
+}
+template <class T>
+void test_neg(const T& , 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);
+      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() > 30)
+      {
+         std::cout << "Timeout reached, aborting tests now....\n";
+         break;
+      }
+   }
+}
+
+int main()
+{
+   using namespace boost::multiprecision;
+   test<number<logged_adaptor<cpp_int_backend<> > > >();
+   test<number<debug_adaptor<cpp_int_backend<> > > >();
+   return boost::report_errors();
+}
+
+
+
Modified: trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp
==============================================================================
--- trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp	Thu Aug  8 11:32:11 2013	(r85240)
+++ trunk/libs/multiprecision/test/test_cpp_dec_float_serial.cpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -12,64 +12,7 @@
 #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;
-      }
-   }
-}
+#include "test_float_serial.hpp"
 
 #if !defined(TEST1) && !defined(TEST2)
 #  define TEST1
Added: trunk/libs/multiprecision/test/test_float128_serial.cpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/libs/multiprecision/test/test_float128_serial.cpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -0,0 +1,26 @@
+///////////////////////////////////////////////////////////////
+//  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/float128.hpp>
+#include "test_float_serial.hpp"
+
+
+int main()
+{
+   using namespace boost::multiprecision;
+   test<float128>();
+   return boost::report_errors();
+}
+
+
+
Added: trunk/libs/multiprecision/test/test_float_serial.hpp
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ trunk/libs/multiprecision/test/test_float_serial.hpp	2013-08-08 13:43:54 EDT (Thu, 08 Aug 2013)	(r85241)
@@ -0,0 +1,91 @@
+///////////////////////////////////////////////////////////////
+//  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_
+
+#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>
+
+
+#ifndef BOOST_MP_TEST_FLOAT_SERIAL_HPP
+#define BOOST_MP_TEST_FLOAT_SERIAL_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);
+      ss.clear();
+      boost::archive::binary_oarchive ba(ss);
+      ba << static_cast<const T&>(val);
+      boost::archive::binary_iarchive ib(ss);
+      ib >> val2;
+      BOOST_CHECK_EQUAL(val, val2);
+      
+      val = -val;
+      ss.clear();
+      boost::archive::text_oarchive oa2(ss);
+      oa2 << static_cast<const T&>(val);
+      boost::archive::text_iarchive ia2(ss);
+      ia2 >> val2;
+      BOOST_CHECK_EQUAL(val, val2);
+      ss.clear();
+      boost::archive::binary_oarchive ba2(ss);
+      ba2 << static_cast<const T&>(val);
+      boost::archive::binary_iarchive ib2(ss);
+      ib2 >> 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;
+      }
+   }
+}
+
+#endif
\ No newline at end of file