Boost logo

Boost :

From: Stephen Nutt (snutt_at_[hidden])
Date: 2002-09-04 10:24:59


> > It is not clear to me why [having the scale as part of the
> > type] would have added lots of member functions.
> >
>
> There would be member function templates for construction,
> assignment and the usual op= operators taking decimals of
> other scales as arguments; then specializations for
> efficiency when the scale is known to be the same.

I was hoping that *most* compilers would be able to optimise this. So given
the following template

template <int scale>
struct fixed
{
  template <int rhsScale>
  fixed<scale>& operator = (fixed<rhsScale> rhs)
  {
    val = scale > rhsScale ? rhs.val << (scale - rhsScale) : rhs.val >>
(rhsScale - scale);
    return *this;
  }
}

I would have hoped that the assembler produced would be a register load, an
optional register shift if the scales were different, followed by a register
store. I guess it could be more explicit if some compilers were unable to
optimise out a shift of 0 to be
    val = scale == rhsScale ? rhs.val : scale > rhsScale ? rhs.val <<
(scale - rhsScale) : rhs.val >> (rhsScale - scale);

If it turns out not to be the case I can duplicate some of the member
functions. Agreed a pain, but I've had to do worse!

Thanks for the continuing feedback.

Stephen


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk