1

When I compiled my application, the performance was worse than I expected and I figured out the compiler was reporting a warning like the below.

remark #25461: Imperfect Loop Unroll-Jammed by 2 (pre-vector)

And this is my short code.

for(i=0; i<num; i++){
    values[i] = func(a, b, c);
}

The index variable, num is alwasy 1024. Why is the loop unroll-jammmed imperfectly? Also, When I modify the code like the below, unroll-jamming doesn't occur! What is the condition of occuring unroll-jamming?

for(i=0; i<num; i++){
    value += func(a, b, c);
}

Actually, the code is a part of large file, so I can't write the entire code here. But the below is the code including func().

int values[num];

int func(int a, int b, int c)
{
    int i=0;
    int total=0;
    for(i=0; i<num; i++){
        total = a*b+c;
    }
    return total;
}

int main(void){
    int i=0;
    int min=0;
    for(i=0; i<num; i++){
        values[i] = func(a, b, c);
    }

    for(i=0; i<num; i++){
        value = values[i];
    }
    printf("value: %d\n", value);
    return 0;
}
Sunwoo Lee
  • 29
  • 3

1 Answers1

0

I created a version of your code that actually compiles:

overflow in the math may have occurred, but I did not check for that

then ran it.

It ran in the 'blink of an eye'

Here is the code:

#include <stdio.h>

#define num (1024)

int values[num];
int value;
int a=2;
int b=3;
int c=4;

int func(int a, int b, int c)
{
    int i=0;
    int total=0;
    for(i=0; i<num; i++){
        total = a*b+c;
    }
    return total;
}

int main(void){
    int i=0;
    //int min=0;
    for(i=0; i<num; i++){
        values[i] = func(a, b, c);
    }

    for(i=0; i<num; i++){
        value = values[i];
    }
    printf("value: %d\n", value);
    return 0;
}
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • note: this line: 'total = a*b+c;' does not accumulate the calculated value, so no worries about math overflow. things would be more 'interesting' if the line were: 'total += a*b+c;' – user3629249 Aug 13 '15 at 14:00
  • in the func() function, a good compiler can optimize the loop out of existence and/or move the constant expression total = a*b+c; to outside the loop. – user3629249 Aug 13 '15 at 14:02