Re:Why is this C++ code faster than my hand-written assembly...
by moefh on Tuesday August 22, 2017 @05:27PM
(#15076543)
That's sad. Both gcc and clang at -O3 can optimize it++ to ++it (see [1]).
Hopefully MSVC will improve soon.
Re:Why is this C++ code faster than my hand-written assembly...
by vardump on Tuesday August 22, 2017 @03:45PM
(#15075728)
That's very surprising, perhaps a compiler bug? Does it perform any differently?
Just tried gcc 7.1, both post and pre-increment compile identically.
Re:Why is this C++ code faster than my hand-written assembly...
by Arnavion on Tuesday August 22, 2017 @09:11PM
(#15077730)
Looks identical to me.
Re:Why is this C++ code faster than my hand-written assembly...
by apta on Wednesday August 23, 2017 @02:49AM
(#15079110)
For the it++ case:
std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > >::operator++, COMDAT PROC
mov rax, QWORD PTR [rcx]
mov QWORD PTR [rdx], rax
add rax, 32 ; 00000020H
mov QWORD PTR [rcx], rax
mov rax, rdx
ret 0
For the ++it case: std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > > >::operator++, COMDAT PROC
add QWORD PTR [rcx], 32 ; 00000020H
mov rax, rcx
ret
Re:Why is this C++ code faster than my hand-written assembly...
by vardump on Wednesday August 23, 2017 @09:25AM
(#15081309)
Interestingly the code you quoted is not called at all. I guess eventually linking phase removes it as dead code.
That considered, both pre- and post-increment generate identical code, even with VS2017.
This matches my previous experience about pretty much any compiler in last 15 years or so -- there's no difference between ++i and i++, unless, of course, it's in a statement and changes the actual meaning of code.
"it++" case. Note that iterator function is not called.
foo PROC
mov rdx, QWORD PTR [rcx]
xor eax, eax
mov rcx, QWORD PTR [rcx+8]
cmp rdx, rcx
je SHORT $LN70@foo
mov r8, rcx
sub r8, rdx
sar r8, 5
npad 8
$LL4@foo:
add rax, r8
add rdx, 32 ; 00000020H
cmp rdx, rcx
jne SHORT $LL4@foo
$LN70@foo:
ret 0
foo ENDP
Here's the code generated for "++it" case. Iterator function is not called here either. foo PROC
mov rdx, QWORD PTR [rcx]
xor eax, eax
mov rcx, QWORD PTR [rcx+8]
cmp rdx, rcx
je SHORT $LN68@foo
mov r8, rcx
sub r8, rdx
sar r8, 5
npad 8
$LL4@foo:
add rax, r8
add rdx, 32 ; 00000020H
cmp rdx, rcx
jne SHORT $LL4@foo
$LN68@foo:
ret 0
foo ENDP
Even for the following trivial function:
MSVC 2017 generates different assembly for ++it vs it++, at the highest optimization level.