## digitalmars.D - Does DMD unroll loops?

Jascha Wetzel <"[firstname]" mainia.de> writes:
```is there a way to make DMD unroll loops?
i can use mixins instead, but it's ugly...

struct Vec(T,int dim)
{
alias Vec!(T,dim) vec_t;

T[dim] data;

{
foreach ( int i, inout d; data )
d += v.data[i];
// alternatively:
// for ( int i = 0; i < dim; ++i )
//  data[i] += v.data[i];
}
}
```
Mar 10 2007
Walter Bright <newshound digitalmars.com> writes:
```Jascha Wetzel wrote:
is there a way to make DMD unroll loops?

Sorry, no.
```
Mar 12 2007
Daniel Keep <daniel.keep.lists gmail.com> writes:
```Jascha Wetzel wrote:
is there a way to make DMD unroll loops?
i can use mixins instead, but it's ugly...

struct Vec(T,int dim)
{
alias Vec!(T,dim) vec_t;

T[dim] data;

{
foreach ( int i, inout d; data )
d += v.data[i];
// alternatively:
// for ( int i = 0; i < dim; ++i )
//  data[i] += v.data[i];
}
}

template Tuple(T...)
{
alias T Tuple;
}

template Range(int n)
{
static if( n < 1 )
alias Tuple!() Range;
else
alias Tuple!(Range!(n-1), n) Range;
}

struct Vec(T,int dim)
{
alias Vec!(T,dim) vec_t;

T[dim] data;

{
// foreach ( int i, inout d; data )
//   d += v.data[i];
// alternatively:
// for ( int i = 0; i < dim; ++i )
//  data[i] += v.data[i];
foreach( i ; Range!(dim) )
data[i] += v.data[i];
}
}

Note: not tested, but you should be able to do something like the
above... :P

-- Daniel

--
Unlike Knuth, I have neither proven or tried the above; it may not even
make sense.

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/
```
Mar 13 2007
Jascha Wetzel <"[firstname]" mainia.de> writes:
```ah, that's nice (and works)

Daniel Keep wrote:

Jascha Wetzel wrote:
is there a way to make DMD unroll loops?
i can use mixins instead, but it's ugly...

struct Vec(T,int dim)
{
alias Vec!(T,dim) vec_t;

T[dim] data;

{
foreach ( int i, inout d; data )
d += v.data[i];
// alternatively:
// for ( int i = 0; i < dim; ++i )
//  data[i] += v.data[i];
}
}

template Tuple(T...)
{
alias T Tuple;
}

template Range(int n)
{
static if( n < 1 )
alias Tuple!() Range;
else
alias Tuple!(Range!(n-1), n) Range;
}

struct Vec(T,int dim)
{
alias Vec!(T,dim) vec_t;

T[dim] data;

{
// foreach ( int i, inout d; data )
//   d += v.data[i];
// alternatively:
// for ( int i = 0; i < dim; ++i )
//  data[i] += v.data[i];
foreach( i ; Range!(dim) )
data[i] += v.data[i];
}
}

Note: not tested, but you should be able to do something like the
above... :P

-- Daniel

```
Mar 13 2007