ZHCUCO7B December 2024 – November 2025 F29H850TU , F29H859TU-Q1
开发人员编写 C 代码的方式会对性能产生影响。本节说明可能发生这种情况的具体示例场景。
uint8_T Bit_Manipulation_Test_Case(void)
{
uint32_T result;
uint32_T i;
uint8_T valid;
result = 0u;
valid = TC_OK;
i = 0u;
/* Or Test Case */
for(i=0; i<BIT_MANIPULATION_ARRAY_SIZE; i++)
{
result = (Swc1_Bit_Manipulation.Operand_A[i] | Swc1_Bit_Manipulation.Operand_B[i]);
if(result != Swc1_Bit_Manipulation.Result_Or[i])
{
valid = TC_NOK;
}
}
/* And Test Case */
for(i=0; i<BIT_MANIPULATION_ARRAY_SIZE; i++)
{
result = (Swc1_Bit_Manipulation.Operand_A[i] & Swc1_Bit_Manipulation.Operand_B[i]);
if(result != Swc1_Bit_Manipulation.Result_And[i])
{
valid = TC_NOK;
}
}
/* Xor Test Case */
for(i=0; i<BIT_MANIPULATION_ARRAY_SIZE; i++) {
result = (Swc1_Bit_Manipulation.Operand_A[i] ^ Swc1_Bit_Manipulation.Operand_B[i]);
if(result != Swc1_Bit_Manipulation.Result_Xor[i]) {
valid = TC_NOK;
}
}
return valid;
}uint8_T Bit_Manipulation_Test_Case(void)
{
uint32_T result_or,result_and,result_xor;
uint32_T i;
uint8_T valid;
result_or = 0u;
result_and = 0u;
result_xor = 0u;
valid = TC_OK;
i = 0u;
/* Or, And, Xor Test Case */
for(i=0; i<BIT_MANIPULATION_ARRAY_SIZE; i++)
{
result_or = (Swc1_Bit_Manipulation.Operand_A[i] | Swc1_Bit_Manipulation.Operand_B[i]);
if(result_or != Swc1_Bit_Manipulation.Result_Or[i])
{
valid = TC_NOK;
}
result_and = (Swc1_Bit_Manipulation.Operand_A[i] & Swc1_Bit_Manipulation.Operand_B[i]);
if(result_and != Swc1_Bit_Manipulation.Result_And[i])
{
valid = TC_NOK;
}
result_xor = (Swc1_Bit_Manipulation.Operand_A[i] ^ Swc1_Bit_Manipulation.Operand_B[i]);
if(result_xor != Swc1_Bit_Manipulation.Result_Xor[i])
{
valid = TC_NOK;
}
}
return valid;
}此外,如果条件语句涉及从存储器加载或访问全局变量,则在可能的情况下将它们预加载到局部变量中可能会有所帮助。这样可以更多地使用 C29 CPU 上的更宽寄存器集。它还可防止从存储器加载值并立即对其执行条件检查时出现流水线停顿。下面的第一个代码块会生成次优代码,第二个代码块的优化程度更高。
// Variables are globals
if(xx ==FALSE)
{
A = b * c + d;
E = f * c + d;
if(dd > high)
{
D = high;
} elseif (dd < low) {
if(kk == RUN)
{
D = low;
} else {
D = dd;
}
} else {
D=dd;
}
}// Local copies of globals
float b_temp=b, c_temp=c, d_temp=d, f_temp=f, high_temp=high, low_temp=low, dd_temp=dd, kk_temp=kk, D_temp=D, g_temp=g, h_temp=h;
if(xx==FALSE)
{
A = b_temp * c_temp + d_temp;
E = f_temp * c_temp + d_temp;
if(dd_temp > high_temp)
{
D_temp = high_temp;
} elseif (dd_temp < low_temp) {
if(kk_temp == RUN)
{
D_temp = low_temp;
} else {
D_temp = dd_temp;
}
} else {
D_temp=dd_temp;
}
}