Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to check if your compiler is hoisting the conditional out the loop is to really inspect the assembly after compiling it with full optimizations.</p> <p>After building your example with:</p> <pre><code>g++ -O3 -c example.cpp -o example.o objdump -d -M intel example.o &gt; example.S </code></pre> <p>Here's what I got for <code>f1</code>:</p> <pre><code>00000020 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)&gt;: ; ... 23: 8b 54 24 10 mov edx,DWORD PTR [esp+0x10] 27: 8b 7c 24 14 mov edi,DWORD PTR [esp+0x14] 2b: 8b 02 mov eax,DWORD PTR [edx] 2d: 8b 4a 04 mov ecx,DWORD PTR [edx+0x4] 30: 29 c1 sub ecx,eax 32: c1 f9 02 sar ecx,0x2 35: 83 f9 01 cmp ecx,0x1 38: 76 d jbe 57 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)+0x37&gt; 3a: 31 db xor ebx,ebx 3c: 85 ff test edi,edi 3e: ba 01 00 00 00 mov edx,0x1 43: 75 b jne 60 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)+0x40&gt; 45: 8b 34 18 mov esi,DWORD PTR [eax+ebx*1] 48: 83 c3 04 add ebx,0x4 4b: 01 f6 add esi,esi 4d: 89 34 90 mov DWORD PTR [eax+edx*4],esi 50: 83 c2 01 add edx,0x1 53: 39 d1 cmp ecx,edx 55: 75 ee jne 45 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)+0x25&gt; 57: 5b pop ebx 58: 5e pop esi 59: 5f pop edi 5a: c3 ret 5b: 90 nop 5c: 8d 74 26 00 lea esi,[esi+eiz*1+0x0] 60: 8b 34 18 mov esi,DWORD PTR [eax+ebx*1] 63: 83 c3 04 add ebx,0x4 66: 0f af f7 imul esi,edi 69: 89 34 90 mov DWORD PTR [eax+edx*4],esi 6c: 83 c2 01 add edx,0x1 6f: 39 ca cmp edx,ecx 71: 75 ed jne 60 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)+0x40&gt; 73: eb e2 jmp 57 &lt;f1(std::vector&lt;int, std::allocator&lt;int&gt; &gt;&amp;, int)+0x37&gt; </code></pre> <p>On line 3c, you find the conditional being checked:</p> <pre><code> ; if(x != 0) 3c: 85 ff test edi,edi 43: 75 b jne 60 ; ... </code></pre> <p>From that point on after the check, <code>x</code> is never tested again and the loops are just executed for each part. The first loop starting from line 45 to 55 is done when <code>x == 0</code>. The second loop starting from line 60 to 71 is done when <code>x != 0</code>.</p> <p>So yes, at least in this case, gcc is able to hoist the conditional out of the loop with full optimizations enable.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload