<?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>Kinetis MicrocontrollersのトピックRe: Mark Butcher - Your Opinion on Variable Usage</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614420#M36497</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Earl.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Insights I did not know about.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 03 Nov 2016 01:19:50 GMT</pubDate>
    <dc:creator>JHinkle</dc:creator>
    <dc:date>2016-11-03T01:19:50Z</dc:date>
    <item>
      <title>Mark Butcher - Your Opinion on Variable Usage</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614416#M36493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm looking at ways to minimize my ram usage and increase performance.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Based on your past experience - please comment on an opinion I have. &amp;nbsp;I'm looking for confirmation or - "Your Head is up your Ass on that one" reply.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm using an RTOS, so each task associated with the RTOS has a while loop - so you never exit from the function (standard RTOS implementation). &amp;nbsp;Local stack variables are always consumed and never reused.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm using Kinetis ARM micros - so they are all 32 bit cpus.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Local variables used within the RTOS task function always utilize the space of a 32 bit word (I'll call it a DWORD), whether ii's a BYTE (8 bits), or a WORD (16 bits). &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So a local BYTE consumes 4 bytes from the stack. &amp;nbsp;The compiler also has to add additional code to make sure the outcome of operations stay within the BYTE range.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SO .. local BYTEs and WORDs - consume more space than required and adds a small performance hit due to the additional processing required to make sure the operations are within range.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;My opinion -- NOW ....&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;To save memory (actually stack space which could then be reduced) - move local BYTE and WORD variable out into global space. &amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When a BYTE is declare global -- you only consume 1 byte of memory instead of 4 on the stack and the additional range operations are not required.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I looked at a task where I had 6 byte variables defined - that's 24 bytes of stack consumed. &amp;nbsp;By making them global -- I only consume 6.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your thoughts based on your experience.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Oct 2016 00:55:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614416#M36493</guid>
      <dc:creator>JHinkle</dc:creator>
      <dc:date>2016-10-31T00:55:49Z</dc:date>
    </item>
    <item>
      <title>Re: Mark Butcher - Your Opinion on Variable Usage</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614417#M36494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What happens if you don't use local variables as you do at the moment but instead use them in a single &lt;EM&gt;struct&lt;/EM&gt; that then uses a pack option (so that it allows packing bytes as bytes). You may find that the stack size reduces accordingly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Eg.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var1 = 0;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var2 = 0;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var3 = 0;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;BYTE var4 = 0;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;presumably consumes 4 long words on the stack (which I expect to be the compiler behavior and not related to the OS (?)).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;typedef struct _PACK stSTACK_VARS&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;{&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var1;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var2;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;BYTE var3;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;BYTE var4;&lt;/STRONG&gt;&lt;BR /&gt;&lt;STRONG&gt;} STACK_VARS;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;STACK_VARS myVars = {{0}};&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For GCC (as used by KDS)&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;#define _PACK&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; __attribute__((__packed__))&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In use, &lt;STRONG&gt;var1 = (2 * 4);&lt;/STRONG&gt; becomes &lt;STRONG&gt;myVars.var1 = (2 * 4);&lt;/STRONG&gt; There shouldn't be any code impact apart from the stack not being able to locate some variables directly in registers (only relevant for very few variables).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Maybe there is also a compiler option to force packing on the stack too? (I avoid compiler options as far as possible to aid in IDE portability since there is often a generic technique that can be used at the coding level).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Mark&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Oct 2016 10:51:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614417#M36494</guid>
      <dc:creator>mjbcswitzerland</dc:creator>
      <dc:date>2016-10-31T10:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Mark Butcher - Your Opinion on Variable Usage</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614418#M36495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Mark.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Local variables associated with a task function are for the most part global in the sense that their location never changes and their scope is forever.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I considered struct packing (I use it a lot) - but it did not make sense for the RTOS variables I am using as the example since added code for struct variable location (base + offset) adds a performance hit.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks again for your comments.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Oct 2016 11:39:03 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614418#M36495</guid>
      <dc:creator>JHinkle</dc:creator>
      <dc:date>2016-10-31T11:39:03Z</dc:date>
    </item>
    <item>
      <title>Re: Mark Butcher - Your Opinion on Variable Usage</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614419#M36496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;A couple other comments:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ARM is a 32bit processor -- it does NOT do 8-bit-math.&amp;nbsp; So while it can directly read and write bytes, to do any math it has to be extended with 'XB' extend-byte instructions -- truncation on write-back is 'automatic' in the byte-write.&amp;nbsp; So, for instance, NEVER would I use 8-bits for a loop counter -- might as well be 32 (most likely staying in a register!) to avoid unnecessary extensions.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The other is that the ARM instruction-set can do 'very nice' indexed-access (i.e., stack-relative) 'within the range that fits' in those fields of overall 32bit instructions.&amp;nbsp; But what ARM CANNOT do is load items directly from a full 32bit memory address, i.e. a globally-defined address-fixed-by-linker var.&amp;nbsp; Any such access takes TWO instructions, one to load a 'constant' from PC-relative-memory (using the basic indexing instruction) that IS the global address (as fixed-up in the linking process!) allocated by your compiler at the end of each program-module, then the actual 'access' instruction.&amp;nbsp; So while 'global' variables have their place, keep this 'cost' in mind!.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;And one final 'Kinetis specific' comment about 'aligned stack'.&amp;nbsp; IF you ever allowed your stack to 'grow' to where it would cross the SRAM_U/SRAM_L boundary (center of RAM space) you would HAVE to insure 'aligned accesses' at that point -- else you get a bus-fault.&amp;nbsp; In that eventually, stack-packing would be 'dangerous'.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Nov 2016 00:55:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614419#M36496</guid>
      <dc:creator>egoodii</dc:creator>
      <dc:date>2016-11-03T00:55:23Z</dc:date>
    </item>
    <item>
      <title>Re: Mark Butcher - Your Opinion on Variable Usage</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614420#M36497</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks Earl.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Insights I did not know about.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Joe&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 03 Nov 2016 01:19:50 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Mark-Butcher-Your-Opinion-on-Variable-Usage/m-p/614420#M36497</guid>
      <dc:creator>JHinkle</dc:creator>
      <dc:date>2016-11-03T01:19:50Z</dc:date>
    </item>
  </channel>
</rss>

