<?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>CodeWarrior for MCUのトピックTesting for an overflow condition</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134060#M1504</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;How do you infer the BVS (Branch if overflow set) instruction using C?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Example:&lt;/DIV&gt;&lt;DIV&gt;I have two signed 16 bit variables that I need to add together.&amp;nbsp; If, however,&amp;nbsp;the addition will cause the result to overflow a 16 bit value,&amp;nbsp;the code needs to do something different.&amp;nbsp; Currently my code looks something like:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;INT16&amp;nbsp; temp1, temp2;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if (((INT32)(temp1 + temp2) &amp;gt; 32767) || ((INT32)(temp1+ temp2) &amp;lt; -32768))&lt;BR /&gt;{&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;else&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The code compiles without warnings, but it generates excessive assembly instructions.&amp;nbsp;&amp;nbsp;I want my C code to effectively compile down to an ADD instruction followed by an BVS instruction.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The reason I care, is because I am&amp;nbsp;porting an Intel 196&amp;nbsp;design written in assembly to a&amp;nbsp;9S12E design written&amp;nbsp;in C.&amp;nbsp;&amp;nbsp;So far, I'm getting quite a bit of code bloat associated with the conversion to C.&amp;nbsp; I&amp;nbsp;know that I could just inline some assembly instructions, but this defeats our goal of code &lt;SPAN&gt;portability&lt;/SPAN&gt;.&amp;nbsp; My&amp;nbsp;manager is also assembly code phobic and whines that it is hard to understand.&amp;nbsp; I, on the other hand, enjoy the&amp;nbsp;control and &lt;SPAN&gt;efficiency&amp;nbsp;&lt;/SPAN&gt;that&amp;nbsp;assembly affords.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Since my C programming expertise is minimal, I thought I'd ask for help.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks -&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;A reluctant C programmer&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 18 May 2007 04:25:36 GMT</pubDate>
    <dc:creator>Beaker</dc:creator>
    <dc:date>2007-05-18T04:25:36Z</dc:date>
    <item>
      <title>Testing for an overflow condition</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134060#M1504</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt; &lt;/DIV&gt;&lt;DIV&gt;How do you infer the BVS (Branch if overflow set) instruction using C?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Example:&lt;/DIV&gt;&lt;DIV&gt;I have two signed 16 bit variables that I need to add together.&amp;nbsp; If, however,&amp;nbsp;the addition will cause the result to overflow a 16 bit value,&amp;nbsp;the code needs to do something different.&amp;nbsp; Currently my code looks something like:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;INT16&amp;nbsp; temp1, temp2;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;if (((INT32)(temp1 + temp2) &amp;gt; 32767) || ((INT32)(temp1+ temp2) &amp;lt; -32768))&lt;BR /&gt;{&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;else&lt;/DIV&gt;&lt;DIV&gt;{&lt;/DIV&gt;&lt;DIV&gt;}&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The code compiles without warnings, but it generates excessive assembly instructions.&amp;nbsp;&amp;nbsp;I want my C code to effectively compile down to an ADD instruction followed by an BVS instruction.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;The reason I care, is because I am&amp;nbsp;porting an Intel 196&amp;nbsp;design written in assembly to a&amp;nbsp;9S12E design written&amp;nbsp;in C.&amp;nbsp;&amp;nbsp;So far, I'm getting quite a bit of code bloat associated with the conversion to C.&amp;nbsp; I&amp;nbsp;know that I could just inline some assembly instructions, but this defeats our goal of code &lt;SPAN&gt;portability&lt;/SPAN&gt;.&amp;nbsp; My&amp;nbsp;manager is also assembly code phobic and whines that it is hard to understand.&amp;nbsp; I, on the other hand, enjoy the&amp;nbsp;control and &lt;SPAN&gt;efficiency&amp;nbsp;&lt;/SPAN&gt;that&amp;nbsp;assembly affords.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Since my C programming expertise is minimal, I thought I'd ask for help.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks -&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;A reluctant C programmer&amp;nbsp;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 May 2007 04:25:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134060#M1504</guid>
      <dc:creator>Beaker</dc:creator>
      <dc:date>2007-05-18T04:25:36Z</dc:date>
    </item>
    <item>
      <title>Re: Testing for an overflow condition</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134061#M1505</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;There is no direct, portable construct which maps to the overflow condition in C.&lt;BR /&gt;Therefore your options are:&lt;BR /&gt;- write something in C which does the same thing.&lt;BR /&gt;&amp;nbsp; When not using long's it would run a bit more efficient, well, but then it is not so straight forward anymore.&lt;BR /&gt;- use HLI (high level inline assembly). I know this is not portable, but if your want to be as efficient as assembly, then this is a case you will have to do this.&lt;BR /&gt;&lt;BR /&gt;First note that in the code you show below, the overflow test does not actually work (I assume your INT16 is signed). The reason is that for (temp1 + temp2), the computation is done on 16 bit and then you convert the already overflowed result to a long. So the result of (temp1 + temp2) is always in the 16 bit range.&lt;BR /&gt;So a simple (and slow) version could be:&lt;BR /&gt;&lt;FONT face="Courier New"&gt;INT16&amp;nbsp; temp1, temp2;&lt;BR /&gt;INT32 r= (long)temp1 +&lt;/FONT&gt; &lt;FONT face="Courier New"&gt;(long)&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;temp2;&lt;BR /&gt;if (r &amp;gt;= 0x8000 || r &amp;lt; -0x8000L) ....&lt;BR /&gt;&lt;/FONT&gt;&lt;BR /&gt;A faster version would do the operation with 16 bits and then detect the overflow&amp;nbsp; somehow else.&lt;BR /&gt;For example if the sign of temp1 and&amp;nbsp; temp2 is not the same, it cannot overflow.&lt;BR /&gt;If it is the same, the positive positive case is simple with unsigned arithmetic, the negative/negative case has the special -&lt;FONT face="Courier New"&gt;0x8000+&lt;/FONT&gt;-&lt;FONT face="Courier New"&gt;0x8000 overflow not to miss too.&lt;BR /&gt;&lt;BR /&gt;What I usually write (not looking at the efficiency, just for simplicity)&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;INT16&amp;nbsp; temp1, temp2;&lt;BR /&gt;INT32 r= (long)temp1 +&lt;/FONT&gt; &lt;FONT face="Courier New"&gt;(long)&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;temp2;&lt;BR /&gt;if (r != (INT16)r) {&lt;BR /&gt;...&lt;BR /&gt;}&lt;BR /&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;BR /&gt;&lt;BR /&gt;Oh, and BTW, here a snippet for unsigned arithmetic I once wrote.&lt;BR /&gt;It's to implement saturation for the addition for unsigned arithmetic. For unsigned the case it a bit simpler as it only can overflow in one direction.&lt;BR /&gt;&lt;BR /&gt;unsigned int AddWithSaturation(unsigned int a, unsigned int b, unsigned int maxN) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp; unsigned int res;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if (maxN &amp;lt;= b || maxN - b &amp;lt; a) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; res = maxN;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; } else {&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; res = a + b;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;BR /&gt;&amp;nbsp;&amp;nbsp; return res;&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Daniel&lt;BR /&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 May 2007 01:48:45 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134061#M1505</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2007-05-19T01:48:45Z</dc:date>
    </item>
    <item>
      <title>Re: Testing for an overflow condition</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134062#M1506</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;Thank you for your detailed reply.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;You are correct about my example code not working.&amp;nbsp;&amp;nbsp;You are also correct that&amp;nbsp;INT16&amp;nbsp;is&amp;nbsp;defined as a signed 16 bit word (aka. short).&amp;nbsp; When I originally disassembled it, I thought I was ok since every instruction was using the D accumulator.&amp;nbsp; I, however, forgot that the D (aka. double) accumulator was the concatenation of two 8 bit accumulators and not two 16 bit accumulators.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Now with the code looking like:&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;if ((((INT32)temp1 + (INT32)temp2) &amp;gt; 32767) || (((INT32)temp1 + (INT32)temp2) &amp;lt; -32768))&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;It literally compiles down to 40 assembly instructions.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I think I'll try some of your&amp;nbsp;suggestions and see how effecient they are.&amp;nbsp; This is quite silly, as I think some assembly code similar to this would work:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;LDD&amp;nbsp;&amp;nbsp; temp1&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;ADDD&amp;nbsp; temp2&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Courier New" size="2"&gt;BVS&amp;nbsp;&amp;nbsp; x&lt;/FONT&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I was hoping that the complier would be able to figure out that I was testing for an overflow condition by looking at the magic numbers 32767 and -32768, and therefore use the BVS instruction.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks again for your help -&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 May 2007 02:48:27 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134062#M1506</guid>
      <dc:creator>Beaker</dc:creator>
      <dc:date>2007-05-19T02:48:27Z</dc:date>
    </item>
    <item>
      <title>Re: Testing for an overflow condition</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134063#M1507</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;Speaking about BVS/BVC. CPU12 manual tells us what will happen after ADDD instruction, when it sets V or clears it.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;FONT face="Arial"&gt;&lt;/FONT&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;V: D15 &lt;FONT face="Times New Roman"&gt;•&lt;/FONT&gt; &lt;FONT face="Arial"&gt;M15&lt;/FONT&gt; &lt;FONT face="Times New Roman"&gt;•&lt;/FONT&gt; &lt;FONT face="Arial"&gt;R15&lt;/FONT&gt; &lt;FONT face="Times New Roman"&gt;+&lt;/FONT&gt; &lt;FONT face="Arial"&gt;D15&lt;/FONT&gt; &lt;FONT face="Times New Roman"&gt;•&lt;/FONT&gt; &lt;FONT face="Arial"&gt;M15&lt;/FONT&gt; &lt;FONT face="Times New Roman"&gt;•&lt;/FONT&gt; &lt;FONT face="Arial"&gt;R15&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;&lt;FONT face="Arial"&gt;If both args are negative but result positive or if both args are positive but result negative... 15th bit is sign bit, isn't it?&lt;/FONT&gt; &lt;FONT face="Arial"&gt;This directly&amp;nbsp;translates to something like this&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;&lt;FONT face="Arial"&gt;&lt;SPAN class="msg_source_code"&gt;&amp;nbsp; if( (a&amp;lt;0 &amp;amp;&amp;amp; b&amp;lt;0 &amp;amp;&amp;amp; (a+b)&amp;gt;=0) || (a&amp;gt;=0 &amp;amp;&amp;amp; b&amp;gt;=0 &amp;amp;&amp;amp; (a+b)&amp;lt;0) ) {&lt;BR /&gt;&amp;nbsp;&amp;nbsp; }&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;FONT face="Arial"&gt;I tried compiling it in CW4.5 and it looks much lighter than "if( ((long)a +b) &amp;gt; 32767 || ((long)a+b)&amp;lt;-32768)"&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;here's the disassembled code&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;FONT face="Arial"&gt;&lt;SPAN class="msg_source_code"&gt;&amp;nbsp;&amp;nbsp; 5:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if( (a&amp;lt;0 &amp;amp;&amp;amp; b&amp;lt;0 &amp;amp;&amp;amp; (a+b)&amp;gt;=0) || (a&amp;gt;=0 &amp;amp;&amp;amp; b&amp;gt;=0 &amp;amp;&amp;amp; (a+b)&amp;lt;0) ) {&lt;BR /&gt;&amp;nbsp; 0002 fc0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDD&amp;nbsp;&amp;nbsp; a&lt;BR /&gt;&amp;nbsp; 0005 2c0d&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BGE&amp;nbsp;&amp;nbsp; *+15 ;abs = 0014&lt;BR /&gt;&amp;nbsp; 0007 fe0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDX&amp;nbsp;&amp;nbsp; b&lt;BR /&gt;&amp;nbsp; 000a 2c08&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BGE&amp;nbsp;&amp;nbsp; *+10 ;abs = 0014&lt;BR /&gt;&amp;nbsp; 000c f30000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADDD&amp;nbsp; b&lt;BR /&gt;&amp;nbsp; 000f 8c0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CPD&amp;nbsp;&amp;nbsp; #0&lt;BR /&gt;&amp;nbsp; 0012 2c12&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BGE&amp;nbsp;&amp;nbsp; *+20 ;abs = 0026&lt;BR /&gt;&amp;nbsp; 0014 fc0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDD&amp;nbsp;&amp;nbsp; a&lt;BR /&gt;&amp;nbsp; 0017 2d10&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BLT&amp;nbsp;&amp;nbsp; *+18 ;abs = 0029&lt;BR /&gt;&amp;nbsp; 0019 fe0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LDX&amp;nbsp;&amp;nbsp; b&lt;BR /&gt;&amp;nbsp; 001c 2d0b&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BLT&amp;nbsp;&amp;nbsp; *+13 ;abs = 0029&lt;BR /&gt;&amp;nbsp; 001e f30000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ADDD&amp;nbsp; b&lt;BR /&gt;&amp;nbsp; 0021 8c0000&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [2]&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CPD&amp;nbsp;&amp;nbsp; #0&lt;BR /&gt;&amp;nbsp; 0024 2c03&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; [3/1]&amp;nbsp;&amp;nbsp; BGE&amp;nbsp;&amp;nbsp; *+5 ;abs = 0029&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;Actually "if(a&amp;lt;0)" and "if(a&amp;gt;=0) could be compiled using BMI/BPL branches. BLT/BGT after LDD is the same as BMI/BGT. But BMI/BGT could eliminate the need to CPD #0&amp;nbsp;:&lt;/P&gt;&lt;P align="left"&gt;&lt;SPAN class="msg_source_code"&gt;if ((a+b) &amp;lt; 0)&lt;BR /&gt;&lt;BR /&gt;LDD&amp;nbsp;&amp;nbsp; a&lt;BR /&gt;ADDD&amp;nbsp; b&lt;BR /&gt;BPL&amp;nbsp;&amp;nbsp; sumispositive&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; branch to &amp;gt;=0 case&lt;/SPAN&gt;&lt;SPAN class="msg_source_code"&gt;BMI&amp;nbsp;&amp;nbsp; sumisnegative&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ; branch to &amp;lt;0 case&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P align="left"&gt;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 May 2007 16:37:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134063#M1507</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2007-05-19T16:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: Testing for an overflow condition</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134064#M1508</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;DIV&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;Thanks for your help as well.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Your suggestion does compile down a little nicer than my original C code, but I'm still not happy with the&amp;nbsp;amount of code that is generated.&amp;nbsp; For this reason, I just decided to inline some assembly instructions and add some good comments.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;My code now looks something like this:&lt;/DIV&gt;&lt;PRE&gt;&lt;FONT size="2"&gt;__asm{   LDD  temp2      // put temp2 into the D accumulator   ADDD temp1      // add temp1 to temp2   BVC  noOverFlow // did the addition cause an overflow—   CLRB            // yes, so defer this addition until temp2 gets smaller   RTS             //   the calling routine checks the B accumulatornoOverFlow:        //   Therefore CLRB followed by RTS is the same as a return(0) C instruction    STD  temp2      // no, so store the result into temp2}&lt;/FONT&gt;&lt;/PRE&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I'm guessing that a number of the assembly instructions that check status bits are never used by the complier when writing in C.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks again -&lt;/DIV&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 21 May 2007 08:57:28 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Testing-for-an-overflow-condition/m-p/134064#M1508</guid>
      <dc:creator>Beaker</dc:creator>
      <dc:date>2007-05-21T08:57:28Z</dc:date>
    </item>
  </channel>
</rss>

