It seems that there's a problem in the compiler when using optimization option(-O0 through -O3, I didn't test -O4 option). Refer to the following two sample codes. Note that the only difference in each code is just one line which marked as bold red in the Code 2.
Even though wTuneCnt and cntTest has the same value, iTest101 and iTest102 has a different value.
The tested compiler versions are 4.1.3, 5.2.12, 6.4.9.
(Code 1)
case STEP5:
pTraceData[(wTuneCnt - 1) * 2] = iLAMdssVoltEst;
pTraceData[(wTuneCnt - 1) * 2 + 1] = iIdseSamp ;
iTest100 = iLAMdssVoltEst;
iTest101 = pTraceData[(wTuneCnt - 1) * 2];
iTest102 = pTraceData[cntTest*2 - 2];
iTest103 = pTraceData[(cntTest - 1) * 2];
wTest100 = wTuneCnt;
wTest101 = cntTest;
// wTest103 = cntTest*2 - 2;
Result
iTest101 = -10748
iTest102 = 0
(Code 2)
case STEP5:
pTraceData[(wTuneCnt - 1) * 2] = iLAMdssVoltEst;
pTraceData[(wTuneCnt - 1) * 2 + 1] = iIdseSamp ;
iTest100 = iLAMdssVoltEst;
iTest101 = pTraceData[(wTuneCnt - 1) * 2];
iTest102 = pTraceData[cntTest*2 - 2];
iTest103 = pTraceData[(cntTest - 1) * 2];
wTest100 = wTuneCnt;
wTest101 = cntTest;
wTest103 = cntTest*2 - 2;
iTest101 = -10748
iTest102 = -10748
The asm file for each code is shown below. The left is code 1, the right is code 2.
I can't figure out what is the problem?