Integer comparison performance on x86

3 weeks ago 19
ARTICLE AD BOX

Do all the integer comparison operators(>=, >, <=, <, ==) have the same performance? If we look at this c++ code for example:

#include <iostream> int main(int argc, char * argv[]) { int a = 5; if(a > 1) { std::cout << "test" << std::endl; } }

The compiler can generate something like this for the branching:

cmp dword ptr [rbp - 20], 1 jle .LBB0_2

If we replace the condition from > to >= the assembly could look like this:

cmp dword ptr [rbp - 20], 1 jl .LBB0_2

we can see that both jump instructions are preceeded by a cmp instruction, so the comparison is always the same(or is it?), only the jump instruction changes. Is it correct to say that all these comparisons have the same performance when talking about x86_64? If the performance is the same, is it the same for all integral types like signed, unsigned, 8bit - 64bit?

Again I am talking about the cpu instructions(jg, jge, jl, jle, ...) not what the c++ compiler will emit when it encounters expressions like this. In this question I also ignore the branch predictor and whether he predicted right or wrong.

Read Entire Article