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 c/68110] New: __builtin_sub_overflow unsigned performance issue


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68110

            Bug ID: 68110
           Summary: __builtin_sub_overflow unsigned performance issue
           Product: gcc
           Version: 5.2.1
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eggert at gnu dot org
  Target Milestone: ---

I ran into this minor performance issue when changing Gnulib's lib/intprops.h
to use the new __builtin_sub_overflow function. I found that
__builtin_sub_overflow is less efficient than the portable C code that it
replaced, when the operands and result are unsigned, or unsigned long, or
unsigned long long. To reproduce the problem, compile and run the following
program with 'gcc -O2 -S' on x86-64:

  _Bool f1 (unsigned long long a, unsigned long long b)
  {
    return a < b;
  }

  _Bool f2 (unsigned long long a, unsigned long long b)
  {
    unsigned long long r;
    return __builtin_sub_overflow (a, b, &r);
  }

Although the functions are semantically equivalent, f1 uses only 3
instructions:

        f1:
                cmpq    %rsi, %rdi
                setb    %al
                ret

whereas f2 uses 5 instructions:

        f2:
                movq    %rdi, %rax
                subq    %rsi, %rax
                cmpq    %rdi, %rax
                seta    %al
                ret

There is a similar problem for x86.


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