Prevent compiler creating potential unaligned 32 bit access.

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Prevent compiler creating potential unaligned 32 bit access.

539 次查看
kvleonhardt
Contributor I

Hi

When using highest optimization level following code for RT1170:

for (nWFixed = obj->isActiveIdx[2]; nWFixed <= obj->mConstrMax; nWFixed++) {
    obj->isActiveConstr.data[nWFixed - 1] = false;
}

Compiles to this:

11485       for (nWFixed = obj->isActiveIdx[2]; nWFixed <= obj->mConstrMax; nWFixed++) {
30016b8c:   ldr     r3, [sp, #152]  ; 0x98
30016b8e:   ldr.w   r5, [r3, #1112] ; 0x458
30016b92:   mov     r3, r2
30016b94:   cmp     r5, r2
30016b96:   blt.n   0x30016bee <ThrustAllocation_fmincon_m+4342>
30016b98:   subs    r0, r5, r2
30016b9a:   adds    r1, r5, #1
30016b9c:   cmp     r0, #2
30016b9e:   sub.w   r1, r1, r2
30016ba2:   bls.n   0x30016bce <ThrustAllocation_fmincon_m+4310>
30016ba4:   movw    r0, #12880      ; 0x3250
30016ba8:   ldr     r3, [pc, #276]  ; (0x30016cc0 <ThrustAllocation_fmincon_m+4552>)
11486         obj->isActiveConstr.data[nWFixed - 1] = false;
30016baa:   movs    r4, #0
30016bac:   add     r0, sp
30016bae:   add     r3, r2
30016bb0:   ldr     r0, [r0, #0]
30016bb2:   add     r3, r0
30016bb4:   bic.w   r0, r1, #3
30016bb8:   add     r0, r3
30016bba:   str.w   r4, [r3], #4
11485       for (nWFixed = obj->isActiveIdx[2]; nWFixed <= obj->mConstrMax; nWFixed++) {
30016bbe:   cmp     r3, r0
30016bc0:   bne.n   0x30016bba <ThrustAllocation_fmincon_m+4290>
30016bc2:   bic.w   r0, r1, #3
30016bc6:   cmp     r0, r1
30016bc8:   add.w   r3, r2, r0
30016bcc:   beq.n   0x30016bee <ThrustAllocation_fmincon_m+4342>

 

The problematic line is this:

30016bba: str.w r4, [r3], #4

As the operation could result in unaligned access. The array is placed in external SDRAM, which does not support unaligned access. Both arrays are basically of unsigned char. 

In other words, the compiler uptimizes the code to clear 4 bytes at a time, instead of clearing 1 by 1 byte.

Is there a way to instruct the compiler to avoid creating potential unaligned 32 bit access.

I cannot rewrite the code, as it is a part of an auto generated code from Matlab/Simulink. An I do not want to use lower uptimization level.

 

Kasper.

0 项奖励
回复
0 回复数