Search code examples
compiler-constructioncomputer-sciencecompiler-optimizationcpu-architecturemicro-optimization

How to create branchless code for this piece of code?


I need to generate branchless code for the if statement in the inner loop if(i != j). I am confused how to generate branchless code.

  for (int i = start; i < n; i++)
  {
        results[i] = 999;
        for (int j = 0; j < n; j++)
        {
            if (i != j)
            {
                d = myfunction(x, y, i, j);
                if (d < results[i])
                    results[i] = d;
            }
        }
    }

Solution

  • A comparison returns 0 (false) or 1 (true) in C++. So you can convert the innermost condition as follows:

    int condition = d < results[i] 
    results[i] = d * condition + results[i] * !condition;
    

    To skip over i in the inner loop, just add one to the arg at i and beyond:

    ...
    for (int j = 0; j < n - 1; j++) {
        d = myfunction(x, y, i, j + (j >= i));
        int condition = d < results[i] 
        results[i] = d * condition + results[i] * !condition;
    }
    ...
    

    An alternative with fewer comparisons would be to split the inner loop into two loops:

    for (int j = 0; j < i; j++) {
       ...
    }
    for (int j = i + i; j < n; j++) {
       ...
    }
    

    Edit: Complex increment / loop start mangling replaced.

    P.S.: An optimizaion option might be to build the minimum in a local variable and assign to results[i] after the inner loop only.