Since the answer will depend on your architecture, let's take a look at the generated code on x86-64 (with gcc -O3):
#include <math.h>
int t_gt(int x) { // note! not equivalent to the others
return abs(x) > 1;
}
int t_ge(int x) {
return abs(x) >= 1;
}
int t_ne(int x) {
return abs(x) != 1;
}
becomes:
Disassembly of section .text:
0000000000000000 <t_gt>:
#include <math.h>
int t_gt(int x) {
0: 89 f8 mov %edi,%eax
2: c1 f8 1f sar $0x1f,%eax
5: 31 c7 xor %eax,%edi
7: 29 c7 sub %eax,%edi
9: 31 c0 xor %eax,%eax
b: 83 ff 01 cmp $0x1,%edi
e: 0f 9f c0 setg %al
return abs(x) > 1;
}
11: c3 retq
12: 66 66 66 66 66 2e 0f nopw %cs:0x0(%rax,%rax,1)
19: 1f 84 00 00 00 00 00
0000000000000020 <t_ge>:
int t_ge(int x) {
20: 89 f8 mov %edi,%eax
22: c1 f8 1f sar $0x1f,%eax
25: 31 c7 xor %eax,%edi
27: 29 c7 sub %eax,%edi
29: 31 c0 xor %eax,%eax
2b: 85 ff test %edi,%edi
2d: 0f 9f c0 setg %al
return abs(x) >= 1;
}
30: c3 retq
31: 66 66 66 66 66 66 2e nopw %cs:0x0(%rax,%rax,1)
38: 0f 1f 84 00 00 00 00
3f: 00
0000000000000040 <t_ne>:
int t_ne(int x) {
40: 89 f8 mov %edi,%eax
42: c1 f8 1f sar $0x1f,%eax
45: 31 c7 xor %eax,%edi
47: 29 c7 sub %eax,%edi
49: 31 c0 xor %eax,%eax
4b: 83 ff 01 cmp $0x1,%edi
4e: 0f 95 c0 setne %al
return abs(x) != 1;
}
51: c3 retq
As you can see, there are two differences:
- The condition codes on the set* ops are different. This likely won't affect speed.
- For t_ge, a two-byte register test (AND) is used, while the other two use cmp (subtraction) and a literal one-byte operand to compare against.
While the difference between the various set* variants, and between test and cmp is likely zero, the additional one-byte operand to cmp may decrease performance by an immeasurably small amount.
Of course, the best performance will be given by getting rid of the pointless abs() entirely:
0000000000000060 <t_ne2>:
int t_ne2(int x) {
60: 31 c0 xor %eax,%eax
62: 85 ff test %edi,%edi
64: 0f 95 c0 setne %al
return (x != 0);
}
67: c3 retq
Keep in mind that these findings may not apply on other architectures; however losing the abs is sure to be faster anywhere.