Boost logo

Boost Users :

From: François Duranleau (duranlef_at_[hidden])
Date: 2006-05-08 23:07:07


On Mon, 8 May 2006, Brian Budge wrote:

> Thanks for the ideas guys.
>
> Compile options are like so:
> g++ -O3 -msse -mfpmath=sse
>
> I tried the metaprogramming technique (which is pretty nifty :) ), and
> got interesting results.
>
> Basically, it made my += operator run twice as SLOW, while making my +
> operator run twice as FAST.
>
> I have a feeling that this is all due to the different optimizations
> that gcc is doing at multiple stages of compilation. For example, it
> may be doing autovectorization of the simple loop case of +=, which it
> can't figure out with the metaprogramming technique. I'm still
> stumped as to why I'm roughly an order of magnitude slower with + than
> with +=.
>
> Any more insights?

Did you try with -funroll-loops ? I once did a few tests with vectors, one
version with loops and the other with manually unrolled loops, and with
options -O3 -funroll-loops, the generated code was identical. But then
again, that was with g++-3.3.

As for the use of boost::operators, I don't know, I did a small test using
the following, and the generated code with g++-4.0 and g++-4.1 (with
option -O3 -msse -mfpmath=sse and with and without -DUSE_OP) is identical
(diff reports no difference).

#ifdef USE_OP
#include <boost/operators.hpp>
#endif

template < typename T >
class vector
#ifdef USE_OP
     : boost::addable< vector< T > >
#endif
{
   private :

     T data_[ 3 ] ;

   public :

     vector()
     {
     }

     explicit vector( const T* v )
     {
         data_[ 0 ] = v[ 0 ] ;
         data_[ 1 ] = v[ 1 ] ;
         data_[ 2 ] = v[ 2 ] ;
     }

     const T& operator [] ( ::std::size_t i ) const
     {
         return data_[ i ] ;
     }

     vector& operator += ( const vector& rhs )
     {
         data_[ 0 ] += rhs[ 0 ] ;
         data_[ 1 ] += rhs[ 1 ] ;
         data_[ 2 ] += rhs[ 2 ] ;
         return * this ;
     }

#ifndef USE_OP
     friend
     vector operator + ( const vector& lhs ,
                         const vector& rhs )
     {
         vector r ;
         r.data_[ 0 ] = lhs[ 0 ] + rhs[ 0 ] ;
         r.data_[ 1 ] = lhs[ 1 ] + rhs[ 1 ] ;
         r.data_[ 2 ] = lhs[ 2 ] + rhs[ 2 ] ;
         return r ;
     }
#endif

} ;

-- 
François Duranleau
LIGUM, Université de Montréal
"Any sufficiently advanced technology is indistinguishable from magic"
                                                         - Arthur C. Clarke

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net