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++/67962] New: Optimization opportunity with conditional swap


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

            Bug ID: 67962
           Summary: Optimization opportunity with conditional swap
           Product: gcc
           Version: 5.2.0
            Status: UNCONFIRMED
          Severity: minor
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: morwenn29 at hotmail dot fr
  Target Milestone: ---

If have some algorithms that use an extensive number of conditional swaps like
this (a few hundreds I guess):

    if (y < x)
    {
        std::swap(x, y);
    }

I thought that such a construct could be optimized by the compiler, but it
appears that the following function is more performant with integers most of
the time:

    void swap_if(int& x, int& y)
    {
        int dx = x;
        int dy = y;
        int tmp = x = std::min(dx, dy);
        y ^= dx ^ tmp;
    }

Would it be possible for g++ to recognize this kind of construct and optimize
it, at least for integer types? Reordering two values seems like something
common enough so that optimizing it could also benefit existing code.

As a side note, I hope that Bugzilla is he right place for this kind of
request. Sorry if it isn't.


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