Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r78055 - sandbox/fixed_point/boost/fixed_point
From: vicente.botet_at_[hidden]
Date: 2012-04-17 19:10:54


Author: viboes
Date: 2012-04-17 19:10:54 EDT (Tue, 17 Apr 2012)
New Revision: 78055
URL: http://svn.boost.org/trac/boost/changeset/78055

Log:
FixedPoint: Added add_result and replace it whenever is can be used.
Text files modified:
   sandbox/fixed_point/boost/fixed_point/number.hpp | 341 ++++++++++++++++++---------------------
   1 files changed, 155 insertions(+), 186 deletions(-)

Modified: sandbox/fixed_point/boost/fixed_point/number.hpp
==============================================================================
--- sandbox/fixed_point/boost/fixed_point/number.hpp (original)
+++ sandbox/fixed_point/boost/fixed_point/number.hpp 2012-04-17 19:10:54 EDT (Tue, 17 Apr 2012)
@@ -540,14 +540,15 @@
       struct f1
       {
         typedef storage::space storage_type;
- typedef conversion::explicitly conversion_type;
+ typedef conversion::explicitly conversion_from_fixed_point_type;
+ typedef conversion::explicitly conversion_from_builtin_type;
         typedef arithmetic::open arithmetic_type;
         typedef bound::bounded bound_type;
       };
     }
 
     template <typename F>
- struct is_explicit: public is_same<typename F::conversion_type, conversion::explicitly>
+ struct is_explicit: public is_same<typename F::conversion_from_fixed_point_type, conversion::explicitly>
     {
     };
     template <typename F>
@@ -558,6 +559,10 @@
     struct is_open: public is_same<typename F::arithmetic_type, arithmetic::open>
     {
     };
+ template <typename F>
+ struct is_closed: public is_same<typename F::arithmetic_type, arithmetic::closed>
+ {
+ };
 
     template <int Range, int Resolution, typename Rounding = round::negative, typename Overflow = overflow::exception,
         typename Family = family::f1>
@@ -1523,6 +1528,7 @@
     template <typename T, typename U>
     struct is_more_precisse;
 
+#if ! defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
     template <
     int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2
@@ -1563,7 +1569,7 @@
     template <typename S, typename T>
     struct is_explicitly_convertible :
     mpl::and_<
- is_explicit<typename T::family_type>,
+ is_explicit<T>,
     mpl::not_<is_more_precisse<T,S> >
>
     {};
@@ -1571,10 +1577,11 @@
     template <typename S, typename T>
     struct is_implicitly_convertible :
     mpl::and_<
- mpl::not_<is_explicit<typename T::family_type> >,
+ mpl::not_<is_explicit<T> >,
     mpl::not_<is_more_precisse<T,S> >
>
     {};
+#endif
 
     /**
      * @brief Signed fixed point number.
@@ -1608,8 +1615,16 @@
       //! the Family parameter.
       typedef Family family_type;
 
+ //! the storage policy
+ typedef typename family_type::storage_type storage_type;
+ //! the conversion policy
+ typedef typename family_type::conversion_from_fixed_point_type conversion_from_fixed_point_type;
+ //! the arithmetic policy
+ typedef typename family_type::arithmetic_type arithmetic_type;
+ //! the arithmetic policy
+ typedef typename family_type::bound_type bound_type;
+
       //! The underlying integer type
- typedef typename Family::storage_type storage_type;
       typedef typename storage_type::template signed_integer_type<Range,Resolution>::type underlying_type;
 #if !defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
       BOOST_MPL_ASSERT_MSG((sizeof(underlying_type)*8) >= (Range-Resolution+1), UNDERLYING_TYPE_MUST_BE_LARGE_ENOUGH, (underlying_type));
@@ -2104,8 +2119,16 @@
       //! the Family parameter.
       typedef Family family_type;
 
+ //! the storage policy
+ typedef typename family_type::storage_type storage_type;
+ //! the conversion policy
+ typedef typename family_type::conversion_from_fixed_point_type conversion_from_fixed_point_type;
+ //! the arithmetic policy
+ typedef typename family_type::arithmetic_type arithmetic_type;
+ //! the arithmetic policy
+ typedef typename family_type::bound_type bound_type;
+
       //! The underlying integer type
- typedef typename Family::storage_type storage_type;
       typedef typename storage_type::template unsigned_integer_type<Range,Resolution>::type underlying_type;
 
 #if !defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
@@ -2574,13 +2597,120 @@
 
     // mixed fixed point arithmetic
 
+
+
+ /**
+ * Add type metafunction.
+ *
+ * The result type depends on whether the types are open/closed.
+ * The specializations must define a nested typedef type.
+ */
+ template <typename T1, typename T2=T1, bool B1=is_open<T1>::value, bool B2=is_open<T2>::value >
+ struct add_result{
+ };
+#if ! defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
+ /**
+ * When of of them is close, both must be the same and the result is itself.
+ */
+ template <typename T>
+ struct add_result<T, T, false, false >
+ {
+ typedef T type;
+ };
+
+ /**
+ * When one of them is open, the result is open
+ */
+ template <int R1, int P1, typename RP1, typename OP1, typename F1,
+ int R2, int P2, typename RP2, typename OP2, typename F2, bool B1, bool B2>
+ struct add_result<real_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2>, B1, B2 >
+ {
+ typedef
+ real_t<
+ #if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
+ MAX(R1,R2)+1, MIN(P1,P2), DT(RP1,RP2), DT(OP1,OP2), DT(F1,F2)
+ #else
+ mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
+ mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
+ typename default_type<RP1,RP2>::type,
+ typename default_type<OP1,OP2>::type,
+ typename default_type<F1,F2>::type
+ #endif
+ >
+ type;
+ };
+ /**
+ * When one of them is open, the result is open
+ */
+ template <int R1, int P1, typename RP1, typename OP1, typename F1,
+ int R2, int P2, typename RP2, typename OP2, typename F2, bool B1, bool B2>
+ struct add_result<ureal_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2>, B1, B2 >
+ {
+ typedef
+ real_t<
+ #if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
+ MAX(R1,R2)+1, MIN(P1,P2), DT(RP1,RP2), DT(OP1,OP2), DT(F1,F2)
+ #else
+ mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
+ mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
+ typename default_type<RP1,RP2>::type,
+ typename default_type<OP1,OP2>::type,
+ typename default_type<F1,F2>::type
+ #endif
+ >
+ type;
+ };
+ /**
+ * When one of them is open, the result is open
+ */
+ template <int R1, int P1, typename RP1, typename OP1, typename F1,
+ int R2, int P2, typename RP2, typename OP2, typename F2, bool B1, bool B2>
+ struct add_result<real_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2>, B1, B2 >
+ {
+ typedef
+ real_t<
+ #if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
+ MAX(R1,R2)+1, MIN(P1,P2), DT(RP1,RP2), DT(OP1,OP2), DT(F1,F2)
+ #else
+ mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
+ mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
+ typename default_type<RP1,RP2>::type,
+ typename default_type<OP1,OP2>::type,
+ typename default_type<F1,F2>::type
+ #endif
+ >
+ type;
+ };
+ /**
+ * When one of them is open, the result is open
+ */
+ template <int R1, int P1, typename RP1, typename OP1, typename F1,
+ int R2, int P2, typename RP2, typename OP2, typename F2, bool B1, bool B2>
+ struct add_result<ureal_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2>, B1, B2 >
+ {
+ typedef
+ ureal_t<
+ #if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
+ MAX(R1,R2)+1, MIN(P1,P2), DT(RP1,RP2), DT(OP1,OP2), DT(F1,F2)
+ #else
+ mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
+ mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
+ typename default_type<RP1,RP2>::type,
+ typename default_type<OP1,OP2>::type,
+ typename default_type<F1,F2>::type
+ #endif
+ >
+ type;
+ };
+#endif
+
     /**
      * signed + int -> signed.
      * @Returns <c>lhs+AT(rhs)</c>.
      */
     template <int R1, int P1, typename RP1, typename OP1, typename F1>
     inline
- real_t<R1+1,P1,RP1,OP1,F1>
+ typename add_result<real_t<R1,P1,RP1,OP1,F1> >::type
     operator+(real_t<R1,P1,RP1,OP1,F1> const& lhs, int rhs)
     {
       typedef real_t<R1,P1,RP1,OP1,F1> arg_type;
@@ -2593,13 +2723,14 @@
      */
     template <int R1, int P1, typename RP1, typename OP1, typename F1>
     inline
- ureal_t<R1+1,P1,RP1,OP1,F1>
+ typename add_result<ureal_t<R1,P1,RP1,OP1,F1> >::type
     operator+(ureal_t<R1,P1,RP1,OP1,F1> const& lhs, unsigned int rhs)
     {
       typedef ureal_t<R1,P1,RP1,OP1,F1> arg_type;
 
       return lhs + arg_type(rhs);
     }
+
     /**
      * signed + signed -> signed.
      * @Returns <c>RT(incex(RT(lhs).count()+RT(rhs).count())</c>.
@@ -2607,30 +2738,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<real_t<R1,P1,RP1,OP1,F1> , real_t<R2,P2,RP2,OP2,F2> >::type
     operator+(real_t<R1,P1,RP1,OP1,F1> const& lhs, real_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<real_t<R1,P1,RP1,OP1,F1> , real_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()+result_type(rhs).count()));
     }
@@ -2642,30 +2753,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<ureal_t<R1,P1,RP1,OP1,F1> , real_t<R2,P2,RP2,OP2,F2> >::type
     operator+(ureal_t<R1,P1,RP1,OP1,F1> const& lhs, real_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<ureal_t<R1,P1,RP1,OP1,F1> , real_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()+result_type(rhs).count()));
     }
@@ -2676,32 +2767,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<real_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type
     operator+(real_t<R1,P1,RP1,OP1,F1> const& lhs, ureal_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
-
- return result_type(index(result_type(lhs).count()+result_type(rhs).count()));
+ return rhs+lhs;
     }
     /**
      * unsigned + unsigned -> unsigned.
@@ -2710,30 +2779,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- ureal_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type
     operator+(ureal_t<R1,P1,RP1,OP1,F1> const& lhs, ureal_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef ureal_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()+result_type(rhs).count()));
     }
@@ -2745,30 +2794,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<real_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2> >::type
     operator-(real_t<R1,P1,RP1,OP1,F1> const& lhs, real_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<real_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()-result_type(rhs).count()));
     }
@@ -2780,30 +2809,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2> >::type
     operator-(ureal_t<R1,P1,RP1,OP1,F1> const& lhs, real_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, real_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()-result_type(rhs).count()));
     }
@@ -2815,30 +2824,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<real_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type
     operator-(real_t<R1,P1,RP1,OP1,F1> const& lhs, ureal_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<real_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()-result_type(rhs).count()));
     }
@@ -2850,30 +2839,10 @@
     template <int R1, int P1, typename RP1, typename OP1, typename F1,
     int R2, int P2, typename RP2, typename OP2, typename F2>
     inline
- real_t<
-#if defined(BOOST_FIXED_POINT_DOXYGEN_INVOKED)
- MAX(R1,R2)+1,
- MIN(P1,P2),
- DT(RP1,RP2),
- DT(OP1,OP2),
- DT(F1,F2)
-#else
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
-#endif
- >
+ typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type
     operator-(ureal_t<R1,P1,RP1,OP1,F1> const& lhs, ureal_t<R2,P2,RP2,OP2,F2> const& rhs)
     {
- typedef real_t<
- mpl::max<mpl::int_<R1>,mpl::int_<R2> >::type::value+1,
- mpl::min<mpl::int_<P1>,mpl::int_<P2> >::type::value,
- typename default_type<RP1,RP2>::type,
- typename default_type<OP1,OP2>::type,
- typename default_type<F1,F2>::type
- > result_type;
+ typedef typename add_result<ureal_t<R1,P1,RP1,OP1,F1>, ureal_t<R2,P2,RP2,OP2,F2> >::type result_type;
 
       return result_type(index(result_type(lhs).count()-result_type(rhs).count()));
     }


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