This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug target/68110] __builtin_sub_overflow unsigned performance issue
- From: "eggert at gnu dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: Tue, 27 Oct 2015 06:34:35 +0000
- Subject: [Bug target/68110] __builtin_sub_overflow unsigned performance issue
- Auto-submitted: auto-generated
- References: <bug-68110-4 at http dot gcc dot gnu dot org/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68110
--- Comment #3 from Paul Eggert <eggert at gnu dot org> ---
(In reply to Andrew Pinski from comment #2)
> So the question is does anyone use this function without "a - b" later on?
Not that I know of. The usual pattern for callers of the Gnulib macro is to use
the macro to check whether there's overflow, and then to subtract if the result
wouldn't overflow. So you're correct that my example is a microbenchmark and is
not a good representative.
That being said, and I'm just thinking aloud here, it may be helpful for GCC to
try "a < b" as an alternative way to test for unsigned subtraction overflow, or
conversely to have GCC use __builtin_sub_overflow as an alternative way to
compute "a < b". Here's a slightly-more-realistic example:
unsigned long
sub1 (unsigned long a, unsigned long b)
{
if (a < b)
return b - a;
else
return a - b;
}
unsigned long
sub2 (unsigned long a, unsigned long b)
{
unsigned long c;
if (__builtin_sub_overflow (a, b, &c))
return b - a;
else
return c;
}
On x86-64, gcc -O2 -S generates this:
sub1:
movq %rsi, %rdx
movq %rdi, %rax
subq %rdi, %rdx
subq %rsi, %rax
cmpq %rsi, %rdi
cmovb %rdx, %rax
ret
sub2:
movq %rdi, %rax
subq %rsi, %rax
cmpq %rdi, %rax
ja .L13
rep ret
.L13:
movq %rsi, %rax
subq %rdi, %rax
ret
The two functions have the same behavior. Presumably one implementation is
better than the other, and could be used for both sub1 and sub2.