|
Boost Users : |
From: AJD (n.outpost_at_[hidden])
Date: 2007-12-06 22:27:30
AJD <n.outpost <at> gmail.com> writes:
> Here's some of the generated assembly:
>
> int main()
> {
> 00401800 sub esp,18h
> vector2< float[3] > v1 = { 0, 1, 2 };
> 00401803 xorps xmm0,xmm0
> vector2< float[3] > v2 = { 3, 4, 5 };
> vector2< float[3] > v3 = { 6, 7, 8 };
>
> // Add two vectors lazily and get the 2nd element.
> #if 1
> // this sticks temporary junk all over the stack, even with 2 terms
> std::cout << (v1 + v2)[0];
> 00401806 fld dword ptr [__real <at> 40400000 (402128h)]
> 0040180C movss dword ptr [esp],xmm0
> 00401811 movss xmm0,dword ptr [__real <at> 3f800000 (402124h)]
> 00401819 movss dword ptr [esp+4],xmm0
> 0040181F movss xmm0,dword ptr [__real <at> 40000000 (402120h)]
> 00401827 movss dword ptr [esp+8],xmm0
> 0040182D movss xmm0,dword ptr [__real <at> 40400000 (402128h)]
> 00401835 movss dword ptr [esp+0Ch],xmm0
> 0040183B movss xmm0,dword ptr [__real <at> 40800000 (40211Ch)]
> 00401843 push ecx
> 00401844 mov ecx,dword ptr [__imp_std::cout (402044h)]
> 0040184A fstp dword ptr [esp]
> 0040184D movss dword ptr [esp+14h],xmm0
> 00401853 movss xmm0,dword ptr [__real <at> 40a00000 (402118h)]
> 0040185B movss dword ptr [esp+18h],xmm0
> 00401861 call dword ptr
> [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<<
> (402040h)]
>
> std::cin.get();
> 00401867 mov ecx,dword ptr [__imp_std::cin (40203Ch)]
> 0040186D call dword ptr
> [__imp_std::basic_istream<char,std::char_traits<char> >::get (402038h)]
>
> return 0;
> 00401873 xor eax,eax
> }
> 00401875 add esp,18h
> 00401878 ret
>
Just to be clear, the following is the expected assembly:
00401800 fld dword ptr [__real_at_41100000 (402118h)]
00401806 push ecx
00401807 mov ecx,dword ptr [__imp_std::cout (402044h)]
0040180D fstp dword ptr [esp]
00401810 call dword ptr
[__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402040h)]
00401816 mov ecx,dword ptr [__imp_std::cin (40203Ch)]
0040181C call dword ptr
[__imp_std::basic_istream<char,std::char_traits<char> >::get (402038h)]
00401822 xor eax,eax
00401824 ret
Notice how in both versions the actual printed value is folded as a constant at
compile-time, loaded onto the stack through the fp instructions, and printed. In
the first version of course all that extra data (the vectors) is being copied
onto the stack through xmm0, and this data is never touched.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net