Boost logo

Boost Users :

Subject: [Boost-users] [proto] applying proto to real problems
From: Michael Marcin (mike.marcin_at_[hidden])
Date: 2009-01-17 14:24:44


I haven't used proto before and as I read the documentation everything
makes sense but I can't figure out how to apply the information there to
solve my problems.

I think if I can figure out how to solve a simplified example from my
actual problem I can figure out the rest.

// typical 3d vector
template< typename T >
struct vec3
{
     T x,y,z;
};

// typical 3d dot product
template< typename T >
T dot_product( const vec3<T>& a, const vec3<T>& b )
{
     return a.x*b.x + a.y*b.y + a.z*b.z;
}

// typical 16.16 fixed point number
struct fixed
{
     boost::int32_t data;
};

// typical multiplication for this fixed point number
fixed operator*( fixed lhs, fixed rhs )
{
     fixed result =
     {
         static_cast<boost::int32_t> (
          ( boost::int64_t(lhs.data)*rhs.data ) >> 16
         )
     };
     return result;
}

// however using the typical dot product with
// fixed numbers is too lossy and inefficient
// so we have to specialize dot_product for
// these fixed point numbers to do this
template<>
fixed dot_product( const vec3<fixed>& a, const vec3<fixed>& b )
{
     fixed result =
     {
         static_cast<boost::int32_t> (
          ( boost::int64_t(a.x.data) * b.x.data +
            boost::int64_t(a.y.data) * b.y.data +
            boost::int64_t(a.z.data) * b.z.data ) >> 16
         )
     };
     return result;
}

What I want to be able to do is to make the typical dot product generate
code that is equivalent to the specialized dot product when instantiated
with T = fixed. This seems like something proto should handle easily
from the description of the library but I just don't get proto yet.

Thanks,

-- 
Michael Marcin

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