0

Quick question (hopefully it is clear).

I was told that performance when considering speed can be drastically improved if you handle the problem of pointer aliasing (always reloading values from memory because compiler isn't sure the value pointed to hasn't been changed by another pointer). This could be resolved by making pointers constant for example:

 void multiply (float a[2][2],float b[2][2], float c[2][2]){
 a[0][0] = b[0][0]*c[1][1];
 a[0][1] = b[1][1]*c[0][0];
 a[1][0] = b[0][1]*c[1][0];
 a[1][1] = b[1][0]*c[0][1];

   }
  // function below is supposedly much faster

  void multiply (float a[2][2],float b[2][2], float c[2][2]){

     const float b00(b[0][0]),b01(b[0][1]),b10([1][0]),b11([1][1]);
     const float c00(c[0][0]),c01([0][1]),c10(c[1][0]),c11(c[1][1]);

     a[0][0] = b00*c11;
     a[0][1] = b11*c00;
     a[1][0] = b02*c10;
     a[1][1] = b10*c01;
  }

My question is how to implement this feature for a much larger array of data in a loop such as:

  void multiply (float a[100][100],float b[100][100], float c[100][100]){

      // cant possibly declare 100 pointer here
      const float b00(b[0][0]),b01(b[0][1]),b10([1][0]),b11([1][1]).......
      const float c00(c[0][0]),c01([0][1]),c10(c[1][0]),c11(c[1][1])......


      for(i=0;1<100;i+=3){
          for(i=0;1<100;i+=3){

              a[i][j+1] = b[i][j]*c[i][j];
              a[i][j+2] = b[i][j+1]*c[i][j+1];
              a[i][j+3] = b[i][j+2]*c[i][j+2];

              a[i+1][j+1] = b[i+1][j]*c[i+1][j];
              a[i+2][j+2] = b[i+1][j+1]*c[i+1][j+1];
               // and so on..
          }
      }
  }
user3332240
  • 21
  • 1
  • 2
  • use `restrict` keyword? – Bryan Chen Mar 12 '14 at 02:29
  • "This could be resolved by making pointers constant for example." This is usually false. Even though the pointer is `const`, some other code could have a non-const pointer to the same data, so generally speaking, `const` doesn't help. – David Schwartz Mar 12 '14 at 02:40
  • ahh .. I never came across the **restrict** keyword before, but from a short search it seems it does what I am talking about. I gotta read more on it. thanks @Bryan – user3332240 Mar 12 '14 at 02:45

0 Answers0