Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r77970 - in sandbox/fixed_point: boost/fixed_point libs/fixed_point/example libs/fixed_point/test
From: vicente.botet_at_[hidden]
Date: 2012-04-14 11:59:35


Author: viboes
Date: 2012-04-14 11:59:34 EDT (Sat, 14 Apr 2012)
New Revision: 77970
URL: http://svn.boost.org/trac/boost/changeset/77970

Log:
FixedPoint: Added conversion from builtins(need to be revised yet) + remove some warnings
Text files modified:
   sandbox/fixed_point/boost/fixed_point/number.hpp | 135 +++++++++++++++++++++++++++------------
   sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp | 22 +++++
   sandbox/fixed_point/libs/fixed_point/test/Jamfile.v2 | 50 +++++++++++++-
   3 files changed, 159 insertions(+), 48 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-14 11:59:34 EDT (Sat, 14 Apr 2012)
@@ -33,6 +33,7 @@
 #include <boost/integer/static_log2.hpp>
 #include <boost/ratio/detail/mpl/abs.hpp>
 #include <limits>
+#include <cmath>
 #include <boost/integer_traits.hpp>
 
 #include <boost/config.hpp>
@@ -52,7 +53,7 @@
       struct shift_impl;
       template <typename From, typename To>
       struct shift_impl<From, To, true> {
- BOOST_STATIC_ASSERT(From::digits>To::resolution_exp);
+ //BOOST_STATIC_ASSERT(From::digits>To::resolution_exp);
         BOOST_STATIC_CONSTEXPR std::size_t digits = From::digits-To::resolution_exp;
         typedef typename From::underlying_type result_type;
         static result_type apply(typename From::underlying_type v)
@@ -75,6 +76,28 @@
         return shift_impl<From,To>::apply(v);
       }
 
+ template <int amt, typename T>
+ T shift_left(T val) {
+ if (amt>0) {
+ unsigned int u_amt = amt;
+ return val<<u_amt;
+ } else {
+ unsigned int u_amt = -amt;
+ return val>>u_amt;
+ }
+ }
+
+ template <int amt, typename T>
+ T shift_right(T val) {
+ if (amt>0) {
+ unsigned int u_amt = amt;
+ return val>>u_amt;
+ } else {
+ unsigned int u_amt = -amt;
+ return val<<u_amt;
+ }
+ }
+
       template <bool IsSigned>
       struct max_type;
       template <>
@@ -241,27 +264,31 @@
       struct impossible {
         BOOST_STATIC_CONSTEXPR bool is_modulo = false;
         template <typename T, typename U>
- static typename T::underlying_type on_negative_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_negative_overflow(U value)
         {
+#if defined(BOOST_NO_CONSTEXPR)
           BOOST_ASSERT_MSG(false,"Negative overflow while trying to convert fixed point numbers");
+#endif
           return value;
         }
         template <typename T, typename U>
- static typename T::underlying_type on_positive_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_positive_overflow(U value)
         {
+#if defined(BOOST_NO_CONSTEXPR)
           BOOST_ASSERT_MSG(false,"Positive overflow while trying to convert fixed point numbers");
+#endif
           return value;
         }
       };
       struct undefined {
         BOOST_STATIC_CONSTEXPR bool is_modulo = false;
         template <typename T, typename U>
- static typename T::underlying_type on_negative_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_negative_overflow(U value)
         {
           return value;
         }
         template <typename T, typename U>
- static typename T::underlying_type on_positive_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_positive_overflow(U value)
         {
           return value;
         }
@@ -274,7 +301,7 @@
         template <typename T, typename U>
         struct modulus_on_negative_overflow<T,U, false>
         {
- static typename T::underlying_type value(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type value(U value)
           {
             return (value%(T::max_index-T::min_index+1))+(T::max_index-T::min_index+1);
           }
@@ -283,7 +310,7 @@
         template <typename T, typename U>
         struct modulus_on_negative_overflow<T,U, true>
         {
- static typename T::underlying_type value(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type value(U value)
           {
             return ((value-T::min_index)%(T::max_index-T::min_index+1))-T::min_index;
           }
@@ -296,7 +323,7 @@
         template <typename T, typename U>
         struct modulus_on_positive_overflow<T,U, true>
         {
- static typename T::underlying_type value(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type value(U value)
           {
             return ((value-T::max_index)%(T::max_index-T::min_index+1))-T::max_index;
           }
@@ -304,7 +331,7 @@
         template <typename T, typename U>
         struct modulus_on_positive_overflow<T,U, false>
         {
- static typename T::underlying_type value(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type value(U value)
           {
             return value%(T::max_index-T::min_index+1);
           }
@@ -313,12 +340,12 @@
       struct modulus {
         BOOST_STATIC_CONSTEXPR bool is_modulo = true;
         template <typename T, typename U>
- static typename T::underlying_type on_negative_overflow(U val)
+ static BOOST_CONSTEXPR typename T::underlying_type on_negative_overflow(U val)
         {
           return detail::modulus_on_negative_overflow<T,U>::value(val);
         }
         template <typename T, typename U>
- static typename T::underlying_type modulus_on_positive_overflow(U val)
+ static BOOST_CONSTEXPR typename T::underlying_type modulus_on_positive_overflow(U val)
         {
           return detail::modulus_on_negative_overflow<T,U>::value(val);
         }
@@ -326,12 +353,12 @@
       struct saturate {
         BOOST_STATIC_CONSTEXPR bool is_modulo = false;
         template <typename T, typename U>
- static typename T::underlying_type on_negative_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_negative_overflow(U )
         {
           return T::min_index;
         }
         template <typename T, typename U>
- static typename T::underlying_type on_positive_overflow(U value)
+ static BOOST_CONSTEXPR typename T::underlying_type on_positive_overflow(U )
         {
           return T::max_index;
         }
@@ -340,12 +367,12 @@
       struct exception {
         BOOST_STATIC_CONSTEXPR bool is_modulo = false;
         template <typename T, typename U>
- static typename T::underlying_type on_negative_overflow(U value)
+ static typename T::underlying_type on_negative_overflow(U )
         {
           throw negative_overflow();
         }
         template <typename T, typename U>
- static typename T::underlying_type on_positive_overflow(U value)
+ static typename T::underlying_type on_positive_overflow(U )
         {
           throw positive_overflow();
         }
@@ -489,7 +516,7 @@
      * helper function to make easier the use of index_tag.
      */
     template <typename T>
- struct index_tag<T> index(T v) { return index_tag<T>(v); }
+ BOOST_CONSTEXPR index_tag<T> index(T v) { return index_tag<T>(v); }
 
     /**
      * explicit conversion between fixed_point numbers.
@@ -504,14 +531,14 @@
         BOOST_STATIC_CONSTEXPR std::size_t digits = (Range-Resolution)+1;
         BOOST_STATIC_ASSERT_MSG((sizeof(T)*8)>=digits, "LLLL");
         //BOOST_MPL_ASSERT_MSG((sizeof(T)*8)>=digits, LLLL, (mpl::int_<sizeof(T)*8>, mpl::int_<digits>));
- BOOST_STATIC_CONSTEXPR T const_max = (1<<(digits-1)) - 1;
+ BOOST_STATIC_CONSTEXPR T const_max = (1LL<<(digits-1)) - 1;
         BOOST_STATIC_CONSTEXPR T const_min = -const_max;
 
       };
       template <typename T, int Range, int Resolution >
       struct unsigned_integer_traits {
         BOOST_STATIC_CONSTEXPR std::size_t digits = (Range-Resolution);
- BOOST_STATIC_CONSTEXPR T const_max = (1<<(digits)) - 1;
+ BOOST_STATIC_CONSTEXPR T const_max = (1LL<<(digits)) - 1;
         BOOST_STATIC_CONSTEXPR T const_min = 0;
 
       };
@@ -711,7 +738,8 @@
 
           // Overflow impossible
           // No round needed
- return (
+ return
+ (
               (((underlying_type(rhs.count()) << (P1-P2))) > To::max_index)
             ? To(index(OP2::template on_positive_overflow<To,underlying_type>(((underlying_type(rhs.count()) << (P1-P2))))))
             : (
@@ -1535,8 +1563,12 @@
       template <typename FP>
       static FP factor()
       {
- if (Resolution>=0) return FP(1 << Resolution);
- else return FP(1)/(1 << -Resolution);
+// if (Resolution>=0) return FP(1 << Resolution);
+// else return FP(1)/(1 << -Resolution);
+ if (Resolution>=0) return FP(detail::shift_left<Resolution>(1));
+ else return FP(1)/(detail::shift_left<-Resolution>(1));
+
+
       }
       template <typename FP>
       static FP reconstruct(underlying_type k)
@@ -1588,39 +1620,42 @@
       }
 #endif
 
-#if 0
+#if 1
 
       template <typename FP>
       static underlying_type integer_part(FP x)
       {
- return underlying_type(floor(x));
+ return underlying_type(std::floor(x));
       }
       template <typename FP>
- static underlying_type classify(FP x) const
+ static underlying_type classify(FP x)
       {
 
- underlying_type indx =
- // Overflow
- if (x>max().as<FP>())
- {
- return overflow_type::template on_positive_overflow<To,underlying_type>(indx)));
- }
- if (rhs.count() < (typename From::underlying_type(To::min_index)<<(P2-P1)))
- {
- return To(index(OP2::template on_negative_overflow<To,underlying_type>(indx)));
- }
-
- // Round
- return To(index(RP2::template round<From,To>(rhs)));
+// underlying_type indx =
+// // Overflow
+// if (x>max().as<FP>())
+// {
+// return overflow_type::template on_positive_overflow<To,underlying_type>(indx)));
+// }
+// if (rhs.count() < (typename From::underlying_type(To::min_index)<<(P2-P1)))
+// {
+// return To(index(OP2::template on_negative_overflow<To,underlying_type>(indx)));
+// }
+//
+// // Round
+// return To(index(RP2::template round<From,To>(rhs)));
 
         if (x<min().as<FP>())
           return min_index;
         if (x>max().as<FP>())
           return max_index;
- return integer_part(x/factor());
+ return integer_part(x/factor<FP>());
       }
 
       //! implicit conversion from float
+ signed_number(int x) : value_(classify(x))
+ {}
+ //! implicit conversion from float
       signed_number(float x) : value_(classify(x))
       {}
       //! implicit conversion from double
@@ -1772,7 +1807,8 @@
       {
         if (N>=0)
         {
- value_ <<= N;
+ //value_ <<= N;
+ value_ = detail::shift_left<N>(value_);
         }
         else
         {
@@ -1899,7 +1935,7 @@
         std::cout << __FILE__ << "[" <<__LINE__<<"] "<< int(max_index) << std::endl;
         std::cout << __FILE__ << "[" <<__LINE__<<"] "<< int(digits) << std::endl;
 
- BOOST_ASSERT(i.get()>=min_index);
+ //BOOST_ASSERT(i.get()>=min_index);
         BOOST_ASSERT(i.get()<=max_index);
       }
 
@@ -2147,9 +2183,10 @@
       template <int N, typename RP>
       void scale()
       {
- if (N>=0)
+ if (N >= 0)
         {
- value_ <<= N;
+ //value_ <<= N;
+ value_ = detail::shift_left<N>(value_);
         }
         else
         {
@@ -2232,6 +2269,20 @@
     // mixed fixed point arithmetic
 
     /**
+ * signed + int -> signed.
+ * @Returns <c>lhs+AT(rhs)</c>.
+ */
+ template <int R1, int P1, typename RP1, typename OP1, typename Opt1>
+ inline
+ signed_number<R1+1,P1,RP1,OP1,Opt1>
+ operator+(signed_number<R1,P1,RP1,OP1,Opt1> const& lhs, int rhs)
+ {
+ typedef signed_number<R1,P1,RP1,OP1,Opt1> arg_type;
+
+ return lhs + arg_type(rhs);
+ }
+
+ /**
      * signed + signed -> signed.
      * @Returns <c>RT(incex(RT(lhs).count()+RT(rhs).count())</c>.
      */

Modified: sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp
==============================================================================
--- sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp (original)
+++ sandbox/fixed_point/libs/fixed_point/example/ex_xx.cpp 2012-04-14 11:59:34 EDT (Sat, 14 Apr 2012)
@@ -15,6 +15,8 @@
   BOOST_AUTO(scale, (to_unsigned_number<255,0>()));
   BOOST_AUTO(a_r, a.r / scale);
   BOOST_AUTO(b_r, b.r / scale);
+ (void)a_r;
+ (void)b_r;
   BOOST_AUTO(aia, b.a * (to_unsigned_number<1,0>() - a.a));
   BOOST_AUTO(c_a, a.a + aia);
   BOOST_AUTO(c_r, (a.r*a.a + b.r*aia) / c_a);
@@ -70,7 +72,7 @@
   }
   {
     std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
- unsigned_number<1,-32> n1((index(1)));
+ unsigned_number<1,-32> n1((index(1U)));
     std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
     unsigned_number<64,31,round::negative> n2(n1);
     std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
@@ -492,7 +494,7 @@
   }
   {
     std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
- unsigned_number<6,-2> n1((index(4)));
+ unsigned_number<6,-2> n1((index(4U)));
     n1.scale<-2,round::truncated>();
     std::cout << int(n1.count()) << std::endl;
     BOOST_TEST(n1.count()==1);
@@ -961,17 +963,33 @@
     BOOST_TEST(scale.count()==3);
   }
   {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
     typedef unsigned_number<8,0> T;
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
     std::cout << T::min_index << std::endl;
     std::cout << T::max_index << std::endl;
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
   }
   {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
     typedef signed_number<8,0> T;
     std::cout << T::min_index << std::endl;
     std::cout << T::max_index << std::endl;
     std::cout << sizeof(long int) << std::endl;
   }
 
+ {
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ typedef signed_number<15,-16> fp_15__16; // Signed fixed-point values with 15 bits of integer part
+ // and 16 bits of fractional part.
+ fp_15__16 f1, f2;
+ f1 = 1.2345; // Conversion from floating point.
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ f2 = fp_15__16(f1 + 2); // Mixed arithmetic with integers.
+ std::cout << __FILE__ << "[" <<__LINE__<<"]"<<std::endl;
+ f2 = fp_15__16(f1 - fp_15__16(2)); // Mixed arithmetic with integers.
+ f2 = fp_15__16(f1 / f2); // Arithmetic on fixed-point values.
+ }
   return boost::report_errors();
 }
 

Modified: sandbox/fixed_point/libs/fixed_point/test/Jamfile.v2
==============================================================================
--- sandbox/fixed_point/libs/fixed_point/test/Jamfile.v2 (original)
+++ sandbox/fixed_point/libs/fixed_point/test/Jamfile.v2 2012-04-14 11:59:34 EDT (Sat, 14 Apr 2012)
@@ -8,14 +8,56 @@
         #<library>/boost/test//boost_test_exec_monitor/<link>static
         <include>../../..
         <define>BOOST_ALL_NO_LIB=1
+
         <warnings>all
         <toolset>gcc:<cxxflags>-Wextra
- <toolset>gcc:<cxxflags>-Wshadow
- <toolset>gcc:<warnings-as-errors>on
+ <toolset>gcc:<cxxflags>-pedantic
+ <toolset>gcc:<cxxflags>-Wno-long-long
+ #<toolset>gcc:<cxxflags>-ansi
+ #<toolset>gcc:<cxxflags>-fpermissive
         <toolset>gcc:<cxxflags>-Wno-long-long
+ <toolset>gcc:<cxxflags>-Wshadow
         <toolset>gcc:<cxxflags>-Wcast-align
- <toolset>intel:<warnings-as-errors>on
- <toolset>msvc:<warnings-as-errors>on
+
+ <toolset>darwin:<cxxflags>-Wextra
+ <toolset>darwin:<cxxflags>-pedantic
+ <toolset>darwin:<cxxflags>-ansi
+ <toolset>darwin:<cxxflags>-fpermissive
+ <toolset>darwin:<cxxflags>-Wno-long-long
+ <toolset>darwin:<cxxflags>-Wno-type-limits
+
+ #<toolset>pathscale:<cxxflags>-Wextra
+ <toolset>pathscale:<cxxflags>-Wno-long-long
+ <toolset>pathscale:<cxxflags>-pedantic
+
+ <toolset>clang:<cxxflags>-Wextra
+ <toolset>clang:<cxxflags>-pedantic
+ <toolset>clang:<cxxflags>-ansi
+ #<toolset>clang:<cxxflags>-fpermissive
+ <toolset>clang:<cxxflags>-Wno-long-long
+
+ <toolset>gcc-mingw-4.4.0:<cxxflags>-fdiagnostics-show-option
+ <toolset>gcc-mingw-4.5.0:<cxxflags>-fdiagnostics-show-option
+ <toolset>gcc-mingw-4.6.0:<cxxflags>-fdiagnostics-show-option
+ <toolset>gcc-mingw-4.6.3:<cxxflags>-fdiagnostics-show-option
+ <toolset>gcc-mingw-4.7.0:<cxxflags>-fdiagnostics-show-option
+ <toolset>gcc-mingw-4.8.0:<cxxflags>-fdiagnostics-show-option
+
+ <toolset>darwin-4.6.2:<cxxflags>-Wno-delete-non-virtual-dtor
+ <toolset>darwin-4.7.0:<cxxflags>-Wno-delete-non-virtual-dtor
+
+ #<toolset>clang-2.8:<cxxflags>-Wno-delete-non-virtual-dtor
+ #<toolset>clang-2.8:<cxxflags>-Wno-unused-function
+ <toolset>clang-2.8:<cxxflags>-Wno-sign-compare
+ #<toolset>clang-2.9:<cxxflags>-Wno-delete-non-virtual-dtor
+ #<toolset>clang-2.9:<cxxflags>-Wno-unused-function
+ <toolset>clang-3.0:<cxxflags>-Wno-delete-non-virtual-dtor
+ #<toolset>clang-3.0:<cxxflags>-Wno-unused-function
+ #<toolset>clang-3.0:<cxxflags>-Wno-unused-variable
+
+ #<toolset>gcc:<warnings-as-errors>on
+ #<toolset>intel:<warnings-as-errors>on
+ #<toolset>msvc:<warnings-as-errors>on
     ;
 
 import testing ;


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