title: C picture rotation optimization
body: <p>This is for all you C experts out there..</p>
  
  <p>The first function takes a two-dimensional matrix src[dim][dim] representing pixels of an image, and rotates it 90 degrees into a destination matrix dst[dim][dim].  The second function takes the same src[dim][dim] and smoothens the image by replacing every pixel value with the average of all the pixels around it (in a maximum of 3 × 3 window centered at that pixel). </p>
  
  <p>I need to optimize the program in account for time and cycles, how else would I be able to optimize the following?:</p>
  
  <pre><code>void rotate(int dim, pixel *src, pixel *dst,) 
  {
      int i, j, nj;
      nj = 0;
  
      /* below are the main computations for the implementation of rotate. */
      for (j = 0; j &lt; dim; j++) {
          nj = dim-1-j;               /* Code Motion moved operation outside inner for loop */
          for (i = 0; i &lt; dim; i++) {
              dst[RIDX(nj, i, dim)] = src[RIDX(i, j, dim)];
          }
      }
  }
  
  
  
  /* A struct used to compute averaged pixel value */
  typedef struct {
      int red;
      int green;
      int blue;
      int num;
  } pixel_sum;
  
  /* Compute min and max of two integers, respectively */
  static int minimum(int a, int b) 
  { return (a &lt; b ? a : b); }
  static int maximum(int a, int b) 
  { return (a &gt; b ? a : b); }
  
  /* 
   * initialize_pixel_sum - Initializes all fields of sum to 0 
   */
  static void initialize_pixel_sum(pixel_sum *sum) 
  {
      sum-&gt;red = sum-&gt;green = sum-&gt;blue = 0;
      sum-&gt;num = 0;
      return;
  }
  
  /* 
   * accumulate_sum - Accumulates field values of p in corresponding 
   * fields of sum 
   */
  static void accumulate_sum(pixel_sum *sum, pixel p) 
  {
      sum-&gt;red += (int) p.red;
      sum-&gt;green += (int) p.green;
      sum-&gt;blue += (int) p.blue;
      sum-&gt;num++;
      return;
  }
  
  /* 
   * assign_sum_to_pixel - Computes averaged pixel value in current_pixel 
   */
  static void assign_sum_to_pixel(pixel *current_pixel, pixel_sum sum) 
  {
      current_pixel-&gt;red = (unsigned short) (sum.red/sum.num);
      current_pixel-&gt;green = (unsigned short) (sum.green/sum.num);
      current_pixel-&gt;blue = (unsigned short) (sum.blue/sum.num);
      return;
  }
  
  /* 
   * avg - Returns averaged pixel value at (i,j) 
   */
  static pixel avg(int dim, int i, int j, pixel *src) 
  {
      int ii, jj;
      pixel_sum sum;
      pixel current_pixel;
  
      initialize_pixel_sum(&amp;sum);
      for(ii = maximum(i-1, 0); ii &lt;= minimum(i+1, dim-1); ii++) 
          for(jj = maximum(j-1, 0); jj &lt;= minimum(j+1, dim-1); jj++) 
              accumulate_sum(&amp;sum, src[RIDX(ii, jj, dim)]);
  
      assign_sum_to_pixel(&amp;current_pixel, sum);
      return current_pixel;
  }
  
  void smooth(int dim, pixel *src, pixel *dst) 
  {
      int i, j;
  
  
      /* below are the main computations for the implementation of the smooth function. */
  
      for (j = 0; j &lt; dim; j++)
          for (i = 0; i &lt; dim; i++)
              dst[RIDX(i, j, dim)] = avg(dim, i, j, src);
  }
  </code></pre>
  
  <p>I moved dim-1-j outside of the inner for loop of rotate which reduces time and cycles used in the program, but is there anything else that can be used for either main function?</p>
  
  <p>Thanks!</p>