A simple way to do compile time loop unrolling

finalpatch fengli at gmail.com
Fri May 31 18:11:37 PDT 2013


Wow! That's so very cool! We can make it even nicer with

template Unroll(alias CODE, alias N, alias SEP="")
{
     enum t = replace(CODE, "%", "%1$d");
     enum Unroll = iota(N).map!(i => format(t, i)).join(SEP);
}

And use % as the placeholder instead of the ugly %1$d:

mixin(Unroll!("v1[%]*v2[%]", 3, "+"));

It actually gets quite readable now.

On Friday, 31 May 2013 at 17:30:13 UTC, Peter Alexander wrote:
> Remember that in D, most side-effect free functions can be run 
> at compile time. No need for recursive template trickery:
>
> mixin(iota(3).map!(i => format("v[%1$d]+=rhs.v[%1$d];", 
> i)).join());



More information about the Digitalmars-d mailing list