title: Why doesn't my small C loop print correctly to the frame buffer, but its unrolled version does?
body: <p>I'm trying to write a simple C program that draws a 4x4 pixels white solid square at position (x, y) = (50, 50).</p>
  
  <p>The idea is to write directly to the Linux frame buffer, which starts at the mapped memory fb->fp.</p>
  
  <p>Now, the problem is that the following code is working well:</p>
  
  <pre><code>uint16_t color = 0xffff;
  
  memcpy(fb-&gt;fp + (50+0) * fb-&gt;line_length + (50+0) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+0) * fb-&gt;line_length + (50+1) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+0) * fb-&gt;line_length + (50+2) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+0) * fb-&gt;line_length + (50+3) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  
  memcpy(fb-&gt;fp + (50+1) * fb-&gt;line_length + (50+0) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+1) * fb-&gt;line_length + (50+1) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+1) * fb-&gt;line_length + (50+2) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+1) * fb-&gt;line_length + (50+3) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  
  memcpy(fb-&gt;fp + (50+2) * fb-&gt;line_length + (50+0) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+2) * fb-&gt;line_length + (50+1) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+2) * fb-&gt;line_length + (50+2) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+2) * fb-&gt;line_length + (50+3) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  
  memcpy(fb-&gt;fp + (50+3) * fb-&gt;line_length + (50+0) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+3) * fb-&gt;line_length + (50+1) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+3) * fb-&gt;line_length + (50+2) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  memcpy(fb-&gt;fp + (50+3) * fb-&gt;line_length + (50+3) * fb-&gt;bytes_per_pixel, &amp;color, fb-&gt;bytes_per_pixel);
  </code></pre>
  
  <p>However, the following doesn't.
  It produces a 1x4 line instead of a 4x4 square.</p>
  
  <pre><code>uint16_t color = 0xffff;
  
  int i = 0;
  int j = 0;
  for (; j &lt; 4; j++) {
      for (; i &lt; 4; i++) {
  
          int y_offset = 50 + j;
          int x_offset = 50 + i;
  
          memcpy(fb-&gt;fp + y_offset * fb-&gt;line_length + x_offset * fb-&gt;bytes_per_pixel, 
                 &amp;color, fb-&gt;bytes_per_pixel);
      }
  }
  </code></pre>
  
  <p>As far as I can see, they should be equivalent.
  The assembly version I've got from the compiler doesn't look very easy to understand.</p>
  
  <p>This runs in a ARM embedded Linux device.
  There's no X server or anything else writing to the frame buffer at this point.</p>
  
  <p><em>fb->bytes_per_pixel</em> equals <em>2</em>.</p>
  
  <p>I couldn't find any documentation on exactly how the frame buffer is mapped into memory. The offsets I've got are from random code I found on Google.</p>
  
  <p>Maybe there is something wrong with these offsets.
  But at least the two codes should be equivalent, isn't it?
  Am I going crazy?</p>