This code:

#include <string>
int foo(const std::string &a, const std::string &b) { return a.compare(b); }

compiles to code like this on x86-64:
[...]
        subq    %rdx, %r8
        movl    $2147483647, %eax
        cmpq    $2147483647, %r8
        jg      .L2
        movl    $-2147483648, %eax
        cmpq    $-2147483648, %r8
        cmovge  %r8d, %eax
.L2:
[...]

Since compare need only return a value greater, less, or equal to zero, the
code in _S_compare would be more efficient as simple code that just returns -1,
0, or 1 on most targets where difference_type is wider than int.


-- 
           Summary: inefficient code for std::string::compare on x86-64
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dgohman at gmail dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44413

Reply via email to