<?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>8-bit MicrocontrollersのトピックHCS08: Why is incrementing a 32 bit integer so slow?</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166465#M10624</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My target is an MC9S08QG8 an I'm wondering why incrementing a 32 bit integer is so expensive.&lt;/P&gt;&lt;P&gt;All measurements were done in the simulator.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;UINT32 Counter=0;&lt;/P&gt;&lt;P&gt;UINT16 lc;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for (lc=;lc&amp;lt;30000;lc++)&lt;/P&gt;&lt;P&gt;&amp;nbsp; Counter++;&amp;nbsp; // this line will take about 156 CPU cycles!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So incrementing Counter will take about 156 CPU cycles which is MUCH more than incrementing a 16 bit integer. It's clear that incrementing a 32 bit integer is more effort than a 16 bit integer but the following is still much faster:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// this function takes 37 CPU cycles&lt;/P&gt;&lt;P&gt;void IncUINT32(UINT32 * li)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; ((UINT16 *)li)[1]++;&lt;BR /&gt;&amp;nbsp; if (!((UINT16 *)li)[1])&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((UINT16 *)li)[0]++;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// This macro takes only 18 CPU cycles but it does not fit in every situation, e.g. for loops&lt;BR /&gt;#define INC2_LI(li) ((UINT16 *)&amp;amp;li)[1]++; if (!((UINT16 *)&amp;amp;li)[1]) ((UINT16 *)&amp;amp;li)[0]++;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, any idea why the compiler produces so slow code with the ++ operator? In the debugger I see that the function "_LINC" is called in RTSHC08.c.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bye,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 12 Jan 2010 17:06:55 GMT</pubDate>
    <dc:creator>Superberti</dc:creator>
    <dc:date>2010-01-12T17:06:55Z</dc:date>
    <item>
      <title>HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166465#M10624</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My target is an MC9S08QG8 an I'm wondering why incrementing a 32 bit integer is so expensive.&lt;/P&gt;&lt;P&gt;All measurements were done in the simulator.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;...&lt;/P&gt;&lt;P&gt;UINT32 Counter=0;&lt;/P&gt;&lt;P&gt;UINT16 lc;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for (lc=;lc&amp;lt;30000;lc++)&lt;/P&gt;&lt;P&gt;&amp;nbsp; Counter++;&amp;nbsp; // this line will take about 156 CPU cycles!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So incrementing Counter will take about 156 CPU cycles which is MUCH more than incrementing a 16 bit integer. It's clear that incrementing a 32 bit integer is more effort than a 16 bit integer but the following is still much faster:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// this function takes 37 CPU cycles&lt;/P&gt;&lt;P&gt;void IncUINT32(UINT32 * li)&lt;BR /&gt;{&lt;BR /&gt;&amp;nbsp; ((UINT16 *)li)[1]++;&lt;BR /&gt;&amp;nbsp; if (!((UINT16 *)li)[1])&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((UINT16 *)li)[0]++;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;// This macro takes only 18 CPU cycles but it does not fit in every situation, e.g. for loops&lt;BR /&gt;#define INC2_LI(li) ((UINT16 *)&amp;amp;li)[1]++; if (!((UINT16 *)&amp;amp;li)[1]) ((UINT16 *)&amp;amp;li)[0]++;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, any idea why the compiler produces so slow code with the ++ operator? In the debugger I see that the function "_LINC" is called in RTSHC08.c.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Bye,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jan 2010 17:06:55 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166465#M10624</guid>
      <dc:creator>Superberti</dc:creator>
      <dc:date>2010-01-12T17:06:55Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166466#M10625</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The function _LINC simply increments a 32-bit number on the stack.&amp;nbsp; Including the JSR/BSR it consumes 36 cycles (43 for the 9S08).&amp;nbsp; So, there must be more code that you include in your cycle measurement.&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One possibility is you're measuring the complete code for a whole FOR loop iteration, which should be about right.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Another possibility is that as you step over instructions in the simulator some ISR is executed messing up your cycle count.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try single-stepping in the simulator with assembly code (not source code) displayed so you can see what instructions are actually executed/counted.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jan 2010 19:46:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166466#M10625</guid>
      <dc:creator>tonyp</dc:creator>
      <dc:date>2010-01-13T19:46:40Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166467#M10626</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;On second look, LINC starts with ENTER_UNARY and ends with EXIT_UNARY which are setup and exit code, respectively.&amp;nbsp; This brings the total cycle count to 109 (118 for 9S08) for LINC.&amp;nbsp; Still a few more cycles are missing but if you look close enough (possibly right before the call to LINC) I'm sure you'll find them.&amp;nbsp; So, the problem is not with the actual 32-bit increment, but with all the overhead associated with setting up the stack, pointers, return address, and making the call to the LINC routine.&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by tonyp on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2010-01-13&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;02:18 PM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jan 2010 20:17:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166467#M10626</guid>
      <dc:creator>tonyp</dc:creator>
      <dc:date>2010-01-13T20:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166468#M10627</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi tonyp,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I step with F10 to the Counter++ line in the loop, reset the cycle counter and press once F10. After that the CPU cycle counter is on 156.&lt;/P&gt;&lt;P&gt;Nevertheless it is still not clear why my own C-Function "IncUINT32" is so much faster. It also has to set up the stack, save regs and so on.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks and bye,&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 13 Jan 2010 22:04:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166468#M10627</guid>
      <dc:creator>Superberti</dc:creator>
      <dc:date>2010-01-13T22:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166469#M10628</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;_LINC and IncUINT32 do not perform the same operation,&amp;nbsp;&lt;/P&gt;&lt;P&gt;_LINC is not incrementing a long in place, it is a general ADD 1 function which places the result on&lt;/P&gt;&lt;P&gt;the stack. Therefore it can be (and is) used for code like "long src,dest; dest= src+1;" or for "f(src+1)" as well.&lt;/P&gt;&lt;P&gt;For the "src++;" case the compiler generates "LDHX&amp;nbsp; @src; JSR&amp;nbsp;&amp;nbsp; _LINC; JSR&amp;nbsp;&amp;nbsp; _POP32;" which is not&lt;/P&gt;&lt;P&gt;especially big for a long operation, but I agree it is slow.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So I would suggest you file a service request to suggest the compiler should be extended to handle the&lt;/P&gt;&lt;P&gt;long increment/decrement in place as especially.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Daniel&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Jan 2010 02:07:40 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166469#M10628</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-01-14T02:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166470#M10629</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;would ++Src&amp;nbsp; compile any differently?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jan 2010 00:26:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166470#M10629</guid>
      <dc:creator>TurboBob</dc:creator>
      <dc:date>2010-01-15T00:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: HCS08: Why is incrementing a 32 bit integer so slow?</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166471#M10630</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&amp;gt; would ++Src&amp;nbsp; compile any differently?&lt;/P&gt;&lt;P&gt;No.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 15 Jan 2010 01:23:56 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/HCS08-Why-is-incrementing-a-32-bit-integer-so-slow/m-p/166471#M10630</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2010-01-15T01:23:56Z</dc:date>
    </item>
  </channel>
</rss>

