4 ms·
In fact, newer gcc versions are unable to optimize (x & 1) == 0 at level -O1, and require -O2. I assume that the code emitted at -O2 and -O3 for both constructs
by Hello71 8y ago
In fact, newer gcc versions are unable to optimize (x & 1) == 0 at level -O1, and require -O2. I assume that the code emitted at -O2 and -O3 for both constructs is the more efficient version than the literal translation, even though they are the same number of instructions.
- BearOso 8y agoI highly doubt it. That’s 1:1 test eax, 1 ; use result
- Hello71 8y agogcc -O2, clang, and icc all produce the sequence: is_even(int): mov eax, edi not eax and eax, 1 ret I am far from a x86 expert, but I assume this version avoids contention on the flags registers and increases pipelining. edit: my mistake, icc and clang produce the equivalent sequence "not edi; and edi, 1; mov eax, edi; ret".
- vardump 8y agoApparently not true, at least in a simple function. https://godbolt.org/g/hfpHz7 https://godbolt.org/g/hfpHz7 int isEven(int number) { return number & 1; } gcc 7.3, -O1, -O2 and -O3 (identical codegen for all optimization levels): isEven(int): mov eax, edi and eax, 1 ret
- Hello71 8y agothat function checks if a number is odd, not even. https://godbolt.org/g/7Khcg9 https://godbolt.org/g/7Khcg9
- vardump 8y agoMy bad. Didn't even check it at any point. Anyways, you still prove my point. All those three are pretty close. -O1 sete instruction is very slightly unoptimal, but even it is just a fractional clock cycle.