Boost logo

Boost :

From: Bruno Lalande (bruno.lalande_at_[hidden])
Date: 2008-02-02 14:30:52


Hi Joaquin,

Yes you're right, thanks for the advice. I really thought that an optimizing
compiler was able to figure out this by itself, that is, transform things
like n*n*n*n into (n*n)*(n*n). But a few tests shew me I was wrong.

I have tested your version and indeed, it's much faster in most cases.
However, the gain of performance was not present in all cases until I add a
specialization for the exponent 3:

template <>
struct positive_power<3>
{
        template <typename T>
        static float result(T base)
        { return base*base*base; }
};

An example of problematic case is power<12>(anyvalue). Good performances are
only achieved with this specialization. I think this is due to useless
temporaries that the optimizer can't get to remove (I tested with GCC 4.1.3,
with -O3 optimization).

With your modification the function is finally much more than a syntactic
helper since it runs faster than what an average programmer would type by
hand. Shouldn't it be integrated somewhere into Boost ?

Thanks
Bruno

On Feb 2, 2008 1:57 PM, "JOAQUIN LOPEZ MU?Z" <joaquin_at_[hidden]> wrote:

>
> ----- Mensaje original -----
> De: Bruno Lalande <bruno.lalande_at_[hidden]>
> Fecha: Sábado, Febrero 2, 2008 12:18 pm
> Asunto: [boost] [Boost] Compile-time power of a run-time base
> Para: boost_at_[hidden]
>
> > Hello,
> >
> > I have written for the need of a coworker a function template that
> > computesthe compile-time power of a base that is known only at
> > runtime. The original
> > need was just to avoid having to write things like
> > "value*value*value*value*value".
> >
> > Before doing this, I've been searching for such a thing in the Boost
> > documentation and mailing lists, but could only find discussions
> > about an
> > entierely compile-time version (compile-time exponent of compile-
> > time base)
> > intended for use in metaprograms. Is there any reason why the
> > "semi-compile-time" version I describe here hasn't been discussed?
> > Eventhough it can be viewed as a mere syntactic sugar, it can be
> > very practical
> > when it comes to write by hand a power with a big exponent.
> >
> >
> > In case of interest, here is my code :
> >
> >
> > template <int N>
> > struct positive_power
> > {
> > template <typename T>
> > static float result(T base)
> > { return base*positive_power<N-1>::result(base); }
> > };
>
> I think this is suboptimal: you need only log(N) multiplications
> to get positive_power<N>::result(base):
>
> template <int N>
> struct positive_power
> {
> template <typename T>
> static float result(T base)
> {
> float aux=positive_power<N/2>::result(base);
> return (N%2)?base*aux*aux:aux*aux;
> }
> };
>
> HTH,
>
> Joaquín M López Muñoz
> Telefónica, Investigación y Desarrollo
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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