<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Prevent compiler creating potential unaligned 32 bit access. in i.MX Processors</title>
    <link>https://community.nxp.com/t5/i-MX-Processors/Prevent-compiler-creating-potential-unaligned-32-bit-access/m-p/1606777#M202118</link>
    <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;When using highest optimization level following code for RT1170:&lt;/P&gt;&lt;P&gt;for (nWFixed = obj-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;mConstrMax; nWFixed++) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; obj-&amp;gt;isActiveConstr.data[nWFixed - 1] = false;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Compiles to this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;11485       for (nWFixed = obj-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;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 &amp;lt;ThrustAllocation_fmincon_m+4342&amp;gt;
30016b98:   subs    r0, r5, r2
30016b9a:   adds    r1, r5, #1
30016b9c:   cmp     r0, #2
30016b9e:   sub.w   r1, r1, r2
30016ba2:   bls.n   0x30016bce &amp;lt;ThrustAllocation_fmincon_m+4310&amp;gt;
30016ba4:   movw    r0, #12880      ; 0x3250
30016ba8:   ldr     r3, [pc, #276]  ; (0x30016cc0 &amp;lt;ThrustAllocation_fmincon_m+4552&amp;gt;)
11486         obj-&amp;gt;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-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;mConstrMax; nWFixed++) {
30016bbe:   cmp     r3, r0
30016bc0:   bne.n   0x30016bba &amp;lt;ThrustAllocation_fmincon_m+4290&amp;gt;
30016bc2:   bic.w   r0, r1, #3
30016bc6:   cmp     r0, r1
30016bc8:   add.w   r3, r2, r0
30016bcc:   beq.n   0x30016bee &amp;lt;ThrustAllocation_fmincon_m+4342&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problematic line is this:&lt;/P&gt;&lt;P&gt;30016bba: str.w r4, [r3], #4&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words, the compiler uptimizes the code to clear 4 bytes at a time, instead of clearing 1 by 1 byte.&lt;/P&gt;&lt;P&gt;Is there a way to instruct the compiler to avoid creating potential unaligned 32 bit access.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kasper.&lt;/P&gt;</description>
    <pubDate>Tue, 28 Feb 2023 10:23:45 GMT</pubDate>
    <dc:creator>kvleonhardt</dc:creator>
    <dc:date>2023-02-28T10:23:45Z</dc:date>
    <item>
      <title>Prevent compiler creating potential unaligned 32 bit access.</title>
      <link>https://community.nxp.com/t5/i-MX-Processors/Prevent-compiler-creating-potential-unaligned-32-bit-access/m-p/1606777#M202118</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;When using highest optimization level following code for RT1170:&lt;/P&gt;&lt;P&gt;for (nWFixed = obj-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;mConstrMax; nWFixed++) {&lt;BR /&gt;&amp;nbsp; &amp;nbsp; obj-&amp;gt;isActiveConstr.data[nWFixed - 1] = false;&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;Compiles to this:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;11485       for (nWFixed = obj-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;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 &amp;lt;ThrustAllocation_fmincon_m+4342&amp;gt;
30016b98:   subs    r0, r5, r2
30016b9a:   adds    r1, r5, #1
30016b9c:   cmp     r0, #2
30016b9e:   sub.w   r1, r1, r2
30016ba2:   bls.n   0x30016bce &amp;lt;ThrustAllocation_fmincon_m+4310&amp;gt;
30016ba4:   movw    r0, #12880      ; 0x3250
30016ba8:   ldr     r3, [pc, #276]  ; (0x30016cc0 &amp;lt;ThrustAllocation_fmincon_m+4552&amp;gt;)
11486         obj-&amp;gt;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-&amp;gt;isActiveIdx[2]; nWFixed &amp;lt;= obj-&amp;gt;mConstrMax; nWFixed++) {
30016bbe:   cmp     r3, r0
30016bc0:   bne.n   0x30016bba &amp;lt;ThrustAllocation_fmincon_m+4290&amp;gt;
30016bc2:   bic.w   r0, r1, #3
30016bc6:   cmp     r0, r1
30016bc8:   add.w   r3, r2, r0
30016bcc:   beq.n   0x30016bee &amp;lt;ThrustAllocation_fmincon_m+4342&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problematic line is this:&lt;/P&gt;&lt;P&gt;30016bba: str.w r4, [r3], #4&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In other words, the compiler uptimizes the code to clear 4 bytes at a time, instead of clearing 1 by 1 byte.&lt;/P&gt;&lt;P&gt;Is there a way to instruct the compiler to avoid creating potential unaligned 32 bit access.&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Kasper.&lt;/P&gt;</description>
      <pubDate>Tue, 28 Feb 2023 10:23:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/i-MX-Processors/Prevent-compiler-creating-potential-unaligned-32-bit-access/m-p/1606777#M202118</guid>
      <dc:creator>kvleonhardt</dc:creator>
      <dc:date>2023-02-28T10:23:45Z</dc:date>
    </item>
  </channel>
</rss>

