You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@stdcxx.apache.org by "Mark Brown (JIRA)" <ji...@apache.org> on 2007/02/07 22:06:05 UTC

[jira] Created: (STDCXX-335) std::min() suboptimal

std::min() suboptimal
---------------------

                 Key: STDCXX-335
                 URL: https://issues.apache.org/jira/browse/STDCXX-335
             Project: C++ Standard Library
          Issue Type: Bug
          Components: 25. Algorithms
    Affects Versions: 4.1.3
         Environment: gcc 3.2.3, x86 Linux
            Reporter: Mark Brown


I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():

#include <algorithm>
int test_min (int x, int y) { return std::min (x, y); }
int my_min (int x, int y) { return x < y ? x : y; }

_Z8test_minii:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        cmpl    %eax, 8(%ebp)
        jg      .L2
        leal    8(%ebp), %eax   <<< extra load?
.L4:
        movl    (%eax), %eax
        leave
        ret

_Z6my_minii:
        pushl   %ebp
        movl    %esp, %ebp
        movl    12(%ebp), %eax
        cmpl    8(%ebp), %eax
        jle     .L6
        movl    8(%ebp), %eax
.L6:
        leave
        ret


-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (STDCXX-335) std::min() suboptimal

Posted by "Martin Sebor (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/STDCXX-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12474290 ] 

Martin Sebor commented on STDCXX-335:
-------------------------------------

See also this Sun C++ compiler bug for some relevant detail: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6498247

> std::min() suboptimal
> ---------------------
>
>                 Key: STDCXX-335
>                 URL: https://issues.apache.org/jira/browse/STDCXX-335
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 25. Algorithms
>    Affects Versions: 4.1.3
>         Environment: gcc 3.2.3, x86 Linux
>            Reporter: Mark Brown
>
> I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():
> #include <algorithm>
> int test_min (int x, int y) { return std::min (x, y); }
> int my_min (int x, int y) { return x < y ? x : y; }
> _Z8test_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    %eax, 8(%ebp)
>         jg      .L2
>         leal    8(%ebp), %eax   <<< extra load?
> .L4:
>         movl    (%eax), %eax
>         leave
>         ret
> _Z6my_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    8(%ebp), %eax
>         jle     .L6
>         movl    8(%ebp), %eax
> .L6:
>         leave
>         ret

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (STDCXX-335) std::min() suboptimal

Posted by "Martin Sebor (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/STDCXX-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12471120 ] 

Martin Sebor commented on STDCXX-335:
-------------------------------------

Here's the IPF assembly for a similar test not involving <algorithm> compiled and optimized with HP aCC 6.05 at +O2 (test_min_ref calls a by-reference min, test_min_val calls a by-value min):

_Z12test_min_refii::
        add             r9 = 0, sp
        cmp4.ge.unc     p6, p0 = r32, r33
        addp4           r8 = 4, r9
        add             r10 = 4, r9
        addp4           r11 = 12, r9
        nop.i           0
        st4             [r8] = r32
        st4             [r11] = r33
        addp4           r8 = 12, r9
(p6)    add             r10 = 0, r8
        nop.i           0
        addp4           r8 = 0, r10
        ld4             r8 = [r8]
        nop.m           0
.restore sp
        br.ret.sptk.many rp

_Z12test_min_valii::
        cmp4.ge.unc     p6, p0 = r32, r33
        add             r8 = 0, r32
(p6)    add             r8 = 0, r33
        nop.m           0
        nop.m           0
.restore sp
        br.ret.sptk.many rp


> std::min() suboptimal
> ---------------------
>
>                 Key: STDCXX-335
>                 URL: https://issues.apache.org/jira/browse/STDCXX-335
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 25. Algorithms
>    Affects Versions: 4.1.3
>         Environment: gcc 3.2.3, x86 Linux
>            Reporter: Mark Brown
>
> I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():
> #include <algorithm>
> int test_min (int x, int y) { return std::min (x, y); }
> int my_min (int x, int y) { return x < y ? x : y; }
> _Z8test_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    %eax, 8(%ebp)
>         jg      .L2
>         leal    8(%ebp), %eax   <<< extra load?
> .L4:
>         movl    (%eax), %eax
>         leave
>         ret
> _Z6my_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    8(%ebp), %eax
>         jle     .L6
>         movl    8(%ebp), %eax
> .L6:
>         leave
>         ret

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (STDCXX-335) std::min() suboptimal

Posted by "Martin Sebor (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/STDCXX-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12477431 ] 

Martin Sebor commented on STDCXX-335:
-------------------------------------

Here's a comp.std.c++ discussion on the same topic:
http://groups.google.com/group/comp.std.c++/browse_frm/thread/5a8ba7181f92ff2c

> std::min() suboptimal
> ---------------------
>
>                 Key: STDCXX-335
>                 URL: https://issues.apache.org/jira/browse/STDCXX-335
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 25. Algorithms
>    Affects Versions: 4.1.3
>         Environment: gcc 3.2.3, x86 Linux
>            Reporter: Mark Brown
>
> I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():
> #include <algorithm>
> int test_min (int x, int y) { return std::min (x, y); }
> int my_min (int x, int y) { return x < y ? x : y; }
> _Z8test_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    %eax, 8(%ebp)
>         jg      .L2
>         leal    8(%ebp), %eax   <<< extra load?
> .L4:
>         movl    (%eax), %eax
>         leave
>         ret
> _Z6my_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    8(%ebp), %eax
>         jle     .L6
>         movl    8(%ebp), %eax
> .L6:
>         leave
>         ret

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Commented: (STDCXX-335) std::min() suboptimal

Posted by "Martin Sebor (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/STDCXX-335?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12471106 ] 

Martin Sebor commented on STDCXX-335:
-------------------------------------

I see something similar with the latest trunk, gcc 4.1.0 on SPARC:

_Z8test_minii:
        cmp     %o1, %o0
        st      %o0, [%sp+68]
        st      %o1, [%sp+72]
        bge     .LL11
         add    %sp, 72, %o0
        jmp     %o7+8
         ld     [%o0], %o0
.LL11:
        add     %sp, 68, %o0
        jmp     %o7+8
         ld     [%o0], %o0

_Z6my_minii:
        cmp     %o0, %o1
        bg,a    .LL5
         mov    %o1, %o0
.LL5:
        jmp     %o7+8
         nop


Sun C++ 5.8 also produces worse code:

                        __1cItest_min6Fii_i_:
/* 000000          3 */         st      %o0,[%sp+68]
/* 0x0004          4 */         cmp     %o1,%o0
/* 0x0008            */         bge,pn  %icc,.L77000003
/* 0x000c          3 */         st      %o1,[%sp+72]
/* 0x0010          4 */         ba      .L77000004
/* 0x0014            */         add     %sp,72,%o5
                        .L77000003:
/* 0x0018          4 */         add     %sp,68,%o5
                        .L77000004:
/* 0x001c          4 */         retl    ! Result =  %o0
/* 0x0020            */         ld      [%o5],%o0 ! volatile

                        __1cGmy_min6Fii_i_:
/* 000000          9 */         cmp     %o0,%o1
/* 0x0004            */         bl,a,pt %icc,.L77000011
/* 0x0008          8 */         or      %g0,%o0,%o1
                        .L77000011:
/* 0x000c          9 */         retl    ! Result =  %o0
/* 0x0010            */         or      %g0,%o1,%o0


> std::min() suboptimal
> ---------------------
>
>                 Key: STDCXX-335
>                 URL: https://issues.apache.org/jira/browse/STDCXX-335
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 25. Algorithms
>    Affects Versions: 4.1.3
>         Environment: gcc 3.2.3, x86 Linux
>            Reporter: Mark Brown
>
> I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():
> #include <algorithm>
> int test_min (int x, int y) { return std::min (x, y); }
> int my_min (int x, int y) { return x < y ? x : y; }
> _Z8test_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    %eax, 8(%ebp)
>         jg      .L2
>         leal    8(%ebp), %eax   <<< extra load?
> .L4:
>         movl    (%eax), %eax
>         leave
>         ret
> _Z6my_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    8(%ebp), %eax
>         jle     .L6
>         movl    8(%ebp), %eax
> .L6:
>         leave
>         ret

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


[jira] Updated: (STDCXX-335) std::min() suboptimal

Posted by "Martin Sebor (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/STDCXX-335?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Martin Sebor updated STDCXX-335:
--------------------------------

    Fix Version/s: 4.3

Not sure we'll be able to do anything about this w/o the blessing of the committee since changing the signature of the function would be easily detectable even for fundamental types. See the test case below. Scheduled for 4.3.

#include <cassert>
#include <algorithm>

int main ()
{
    int x = 0;
    int y = 2;
    const int &z = std::min (x, y);

    x = 1;

    assert (&z == &x);
    assert (z == 1);
}


> std::min() suboptimal
> ---------------------
>
>                 Key: STDCXX-335
>                 URL: https://issues.apache.org/jira/browse/STDCXX-335
>             Project: C++ Standard Library
>          Issue Type: Bug
>          Components: 25. Algorithms
>    Affects Versions: 4.1.3
>         Environment: gcc 3.2.3, x86 Linux
>            Reporter: Mark Brown
>             Fix For: 4.3
>
>
> I don't know if it's the compiler that's generating worse code for the stdcxx version of min or if the function is not implemented as efficiently as it could be but the disassembly for the two functions below shows that my_min() is one instruction shorter than test_min():
> #include <algorithm>
> int test_min (int x, int y) { return std::min (x, y); }
> int my_min (int x, int y) { return x < y ? x : y; }
> _Z8test_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    %eax, 8(%ebp)
>         jg      .L2
>         leal    8(%ebp), %eax   <<< extra load?
> .L4:
>         movl    (%eax), %eax
>         leave
>         ret
> _Z6my_minii:
>         pushl   %ebp
>         movl    %esp, %ebp
>         movl    12(%ebp), %eax
>         cmpl    8(%ebp), %eax
>         jle     .L6
>         movl    8(%ebp), %eax
> .L6:
>         leave
>         ret

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.