R
ranjeet.gupta
Dear Readers !!
What does C exactly means for loop unrolling ?
Thanks in Advance
Ranjeet
What does C exactly means for loop unrolling ?
Thanks in Advance
Ranjeet
Dear Readers !!
What does C exactly means for loop unrolling ?
SM said:(e-mail address removed) wrote:
# Dear Readers !!
#
# What does C exactly means for loop unrolling ?
I don't know anything specific about C unless you want to use Duff's device, but
loop unrolling refers to replicating the body of a loop and reducing the total
number of iterations.
for (i=0; i<n; i++) {
body(i);
}
can be unrolled by a factor of 4 to
for (i=0; i+3<n; i+=4) {
body(i+0);
body(i+1);
body(i+2);
body(i+3);
}
for (; i<n; i++) {
body(i); // last 1 to 3 iterations.
}
This can reduce loop overhead and can open new optimisation possibilities
if the individual instructions of body(i+0);...;body(i+3); can be
intermingled.
For example if memcpy knows it's moving word aligned source and destination
on 4 byte words
for (i=0; i<n; i++) dd = ss;
can be unrolled to
for (i=0; i+3<n; i+=4) {
dd[i+0] = ss[i+0];
dd[i+1] = ss[i+1];
dd[i+2] = ss[i+2];
dd[i+3] = ss[i+3];
}
for (; i<n; i++) dd = ss;
and might then be optimised to
if ((dd&3)==0 && (ss&3)==0) {
for (i=0; i+3<n; i+=4) {
*((int*)(dd+i)) = *((int*)(ss+i));
}
}else if ((dd&1)==0 && (ss&1)==0) {
for (i=0; i+1<n; i+=2) {
*((short*)(dd+i)) = *((short*)(ss+i));
}
}else {
for (i=0; i+3<n; i+=4) {
dd[i+0] = ss[i+0];
dd[i+1] = ss[i+1];
dd[i+2] = ss[i+2];
dd[i+3] = ss[i+3];
}
}
for (; i<n; i++) dd = ss;
and run up to four times as fast.
SM said:(e-mail address removed) wrote:
# Dear Readers !!
#
# What does C exactly means for loop unrolling ?
I don't know anything specific about C unless you want to use Duff's device, but
loop unrolling refers to replicating the body of a loop and reducing the total
number of iterations.
for (i=0; i<n; i++) {
body(i);
}
can be unrolled by a factor of 4 to
for (i=0; i+3<n; i+=4) {
body(i+0);
body(i+1);
body(i+2);
body(i+3);
}
for (; i<n; i++) {
body(i); // last 1 to 3 iterations.
}
This can reduce loop overhead and can open new optimisation possibilities
if the individual instructions of body(i+0);...;body(i+3); can be
intermingled.
For example if memcpy knows it's moving word aligned source and destination
on 4 byte words
for (i=0; i<n; i++) dd = ss;
can be unrolled to
for (i=0; i+3<n; i+=4) {
dd[i+0] = ss[i+0];
dd[i+1] = ss[i+1];
dd[i+2] = ss[i+2];
dd[i+3] = ss[i+3];
}
for (; i<n; i++) dd = ss;
and might then be optimised to
if ((dd&3)==0 && (ss&3)==0) {
for (i=0; i+3<n; i+=4) {
*((int*)(dd+i)) = *((int*)(ss+i));
}
}else if ((dd&1)==0 && (ss&1)==0) {
for (i=0; i+1<n; i+=2) {
*((short*)(dd+i)) = *((short*)(ss+i));
}
}else {
for (i=0; i+3<n; i+=4) {
dd[i+0] = ss[i+0];
dd[i+1] = ss[i+1];
dd[i+2] = ss[i+2];
dd[i+3] = ss[i+3];
}
}
for (; i<n; i++) dd = ss;
and run up to four times as fast.
So it means that the loop unrooling is the compiler dependent ?
means varry from compiler to compiler, Leading to the various
factors of unrolled i.e may be 3, may be 4, may be 5 ..on
diffrent diffrent compilers ??
So it means that the loop unrooling is the compiler dependent ?
means varry from compiler to compiler, Leading to the various
factors of unrolled i.e may be 3, may be 4, may be 5 ..on
diffrent diffrent compilers ??
SM Ryan said:# other optimizations such as pipelining. This is not specific to C, or
# part of the Standard, and is off-topic here. Check out
http://catb.org/~esr/jargon/html/D/Duffs-device.html
quote
register n = (count + 7) / 8; /* count > 0 assumed */
switch (count % 8)
case 0: do { *to++ = *from++;
case 7: *to++ = *from++;
case 6: *to++ = *from++;
case 5: *to++ = *from++;
case 4: *to++ = *from++;
case 3: *to++ = *from++;
case 2: *to++ = *from++;
case 1: *to++ = *from++;
} while (--n > 0);
Shocking though it appears to all who encounter it for the first time,
the device is actually perfectly valid, legal C. C's default fall
through in case statements has long been its most controversial
single feature; Duff observed that Ã’This code forms some sort of
argument in that debate, but I'm not sure whether it's for or against.Ó
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.