Branches in built-in functions

I think I have a serious distrust of the compiler. Do branches inside inline functions make them optimized if they have consistent results?

For an example function:

#define MODE_FROM_X_TO_Y 0
#define MODE_FROM_Y_TO_X 1

inline void swapValues(int & x, int & y, int mode) {
    switch(mode) {
        case MODE_FROM_X_TO_Y:
            y = x;
            break;
        case MODE_FROM_Y_TO_X:
            x = y;
            break;
    }
}

Will be:

swapValues(n,m,MODE_FROM_X_TO_Y);

optimized as:

n = m;
+4
source share
2 answers

First, it won’t even compile (until you add the return type).

Secondly, it swapis a very poorly chosen name (since it does not execute swapand conflicts with the name std::swap).

Third, go to http://gcc.godbolt.org/ :

Live On Godbolt

enter image description here

+3
source

Generally speaking, the answers to these questions depend on the compiler.

, ( ) (, ), .

- - "", , .

, inline, inlining. , C/++.

+1

Source: https://habr.com/ru/post/1599671/


All Articles