<?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 Re: Implicit cast in CodeWarrior for MCU</title>
    <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140570#M2497</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;It depends on the nature of the values. If (iFac1 * iFac2) is less than 65535 for most of the time, it would be hard to detect. It also depends on how critical the value is. Because it will be truncated to a value less than 65536 if you get overflow.&lt;BR /&gt;&lt;BR /&gt;There is also a chance that the compiler used to write the code on isn't ANSI C compatible. There are plenty of crappy compilers for embedded systems out there.&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 19 Dec 2006 21:59:51 GMT</pubDate>
    <dc:creator>Lundin</dc:creator>
    <dc:date>2006-12-19T21:59:51Z</dc:date>
    <item>
      <title>Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140565#M2492</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am using ANSI-C/cC++ Compiler for HC12 V-5.0.28 Build 5073&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;and I am looking for a compiler option to solve the following problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;----code----&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;unsigned long lResult; // 32bit&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;unsinged int iFac1 = 0x1000, iFac2 = 0x1000; // 16bit&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;lResult = iFac1 * iFac2;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;^^-------------&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So, lResult should hold the 32bit result of the multiplication. Actually its only an 16bit value, which I fully agree with, but I need to know whether its possible to assign the correct 32bit result to lResult without casting.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Typecasting one of the Factors it not an option for me, due to some difficult reasons!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is there a way that the compiler can recognize the 32bit type of lResult and apply this knowlege to the computation of the R-value before assignment?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thx,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;wolfy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 18 Dec 2006 23:30:47 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140565#M2492</guid>
      <dc:creator>wolfy</dc:creator>
      <dc:date>2006-12-18T23:30:47Z</dc:date>
    </item>
    <item>
      <title>Re: Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140566#M2493</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Hi,&lt;BR /&gt;Try this:&lt;BR /&gt;&lt;BR /&gt;unsigned long lResult; // 32bit&lt;BR /&gt;unsinged int iFac1 = 0x1000, iFac2 = 0x1000; // 16bit&lt;BR /&gt;&lt;BR /&gt;lResult = iFac1;&lt;BR /&gt;lResult *= iFac2;&lt;BR /&gt;&lt;BR /&gt;&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" id="smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Dec 2006 03:13:07 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140566#M2493</guid>
      <dc:creator>pittbull</dc:creator>
      <dc:date>2006-12-19T03:13:07Z</dc:date>
    </item>
    <item>
      <title>Re: Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140567#M2494</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;The ANSI-C standard states that this computation has to be done with int arithmetic, so no, there is no option to explicitly disable this part of the ANSI C specification.&lt;BR /&gt;Realistically, you will have to perform some sort of cast, either by an explicit cast to one of the arguments or by an implicit cast by assigning iFac1 or iFac2 to a long first and then performing the computation with this new local variable.&lt;BR /&gt;&lt;PRE&gt;
unsigned long lResult; // 32bit
unsigned int iFac1 = 0x1000, iFac2 = 0x1000; // 16bit
void fun(void) {
  lResult = iFac1 * (unsigned long)iFac2;
}

void fun2(void) {
  unsigned long local= iFac2;
  lResult = iFac1 * l;
}
&lt;/PRE&gt;&lt;BR /&gt;Both of these operations result in a simple EMUL for the multiplication, so it generates less code than without the cast.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 29 Oct 2020 08:41:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140567#M2494</guid>
      <dc:creator>CompilerGuru</dc:creator>
      <dc:date>2020-10-29T08:41:10Z</dc:date>
    </item>
    <item>
      <title>Re: Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140568#M2495</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Actually, this is a common problem with code written by PC-programmers, tested on a PC and then ported to a 16-bit environment. The PC programmer might be unaware of the so called "integer promotions" in ANSI C, since every operation like the one in the example will be using 32-bit integers. It would have worked fine on a PC for that reason.&lt;BR /&gt;&lt;BR /&gt;Further, the programmer is the one responsible for estimating whether a certain variable will overflow, not the compiler.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Dec 2006 18:38:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140568#M2495</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2006-12-19T18:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140569#M2496</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Thanks for helping me so fast.&lt;BR /&gt;&lt;BR /&gt;Your posts also reflect my opinion, that the code has to be corrected.&lt;BR /&gt;(It's not my code, I just got it to make it run on a HCS12X )&lt;BR /&gt;&lt;BR /&gt;According to the author it already has been tested on other 16bit-Processors&lt;BR /&gt;and worked fine. Do you, from your experience, think that's possible?&lt;BR /&gt;Or didn't he test it "long" enough :smileywink:&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Dec 2006 19:41:44 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140569#M2496</guid>
      <dc:creator>wolfy</dc:creator>
      <dc:date>2006-12-19T19:41:44Z</dc:date>
    </item>
    <item>
      <title>Re: Implicit cast</title>
      <link>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140570#M2497</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;It depends on the nature of the values. If (iFac1 * iFac2) is less than 65535 for most of the time, it would be hard to detect. It also depends on how critical the value is. Because it will be truncated to a value less than 65536 if you get overflow.&lt;BR /&gt;&lt;BR /&gt;There is also a chance that the compiler used to write the code on isn't ANSI C compatible. There are plenty of crappy compilers for embedded systems out there.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 19 Dec 2006 21:59:51 GMT</pubDate>
      <guid>https://community.nxp.com/t5/CodeWarrior-for-MCU/Implicit-cast/m-p/140570#M2497</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2006-12-19T21:59:51Z</dc:date>
    </item>
  </channel>
</rss>

