This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug target/68110] __builtin_sub_overflow unsigned performance issue


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]