Hi,
I am working with C28346 DSP and now migrating my code from CCS v3.3 to CCS v5.
A code which runs well in CCS v3.3 shows strange behaviour in CCS v.5. After spending one day, I found that problem comes from matrix multiplication function:
void mat_mat_mult(float **m1, float **m2, float **mout, int n, int p, int m)
{
int i, j, k;
for(i=0; i < n; i++)
{
for(j=0; j < m; j++)
{
mout[i][j] = 0.;
for(k=0; k<m; k++)
{
mout[i][j] += m1[i][k]*m2[k][j];
}
}
}
}
Obviously, above code runs ok in Debug mode. But it malfunctions in Release mode with any optimization, -o2,-o3
So I declared index variable 'k' as volatile with some suspicion :
volatile int k;
Then it runs OK. I don't understand why I should put volatile keyword in automatic variable.
As far as I know, valatile is declared for a variable shared by several threads.
But an automatic variable is used only in fuction.
Followings are code I tested to mulitipliy two matrices A=[1 2; 3 4] and B=[5 6; 7 8].
In order to make code simple, multiplication code is modified to use 2-dim array.
The answer is C=[19 22; 43 50]; But in Release mode, it gave me C=[21 24; 35 40];
Is there any compiler option to suppress the volatile declaration for automatic variable?
void main(void)
{
float A[2][2], B[2][2], C[2][2], C[2][2];
A[0][0] = 1; A[0][1] = 2;
A[1][0] = 3; A[1][1] = 4;
B[0][0] = 5; B[0][1] = 6;
B[1][0] = 7; B[1][1] = 8;
mat_mat_mult(A,B,C,2,2,2);
}
void mat_mat_mult(float m1[][2], float m2[][2], float mout[][2], int n, int p, int m)
{
int i, j;
int k; // need to declare volatile, volatile int k;
for(i=0; i < n; i++)
{
for(j=0; j < m; j++)
{
mout[i][j] = 0.;
for(k=0; k<m; k++)
{
mout[i][j] += m1[i][k]*m2[k][j];
}
}
}
}